Skip to content

Commit

Permalink
allow external AbstractInterpreter to overload throw-call handling (
Browse files Browse the repository at this point in the history
#52498)

By defining new `abstract_throw` interface, which allows external
`AbstractInterpreter` to customize the behavior of `throw`-call handling
just by overloading `abstract_throw` method. This is particularly useful
for JET.
  • Loading branch information
aviatesk authored Dec 12, 2023
1 parent e39e77f commit 9374e49
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2046,13 +2046,30 @@ function abstract_finalizer(interp::AbstractInterpreter, argtypes::Vector{Any},
return CallMeta(Nothing, Any, Effects(), NoCallInfo())
end

function abstract_throw(interp::AbstractInterpreter, argtypes::Vector{Any}, ::AbsIntState)
na = length(argtypes)
𝕃ᵢ = typeinf_lattice(interp)
if na == 2
argtype2 = argtypes[2]
if isvarargtype(argtype2)
exct = tmerge(𝕃ᵢ, unwrapva(argtype2), ArgumentError)
else
exct = argtype2
end
elseif na == 3 && isvarargtype(argtypes[3])
exct = tmerge(𝕃ᵢ, argtypes[2], ArgumentError)
else
exct = ArgumentError
end
return CallMeta(Union{}, exct, EFFECTS_THROWS, NoCallInfo())
end

# call where the function is known exactly
function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
arginfo::ArgInfo, si::StmtInfo, sv::AbsIntState,
max_methods::Int = get_max_methods(interp, f, sv))
(; fargs, argtypes) = arginfo
la = length(argtypes)

𝕃ᵢ = typeinf_lattice(interp)
if isa(f, Builtin)
if f === _apply_iterate
Expand All @@ -2066,19 +2083,7 @@ function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
elseif f === applicable
return abstract_applicable(interp, argtypes, sv, max_methods)
elseif f === throw
if la == 2
arg2 = argtypes[2]
if isvarargtype(arg2)
exct = tmerge(𝕃ᵢ, unwrapva(argtypes[2]), ArgumentError)
else
exct = arg2
end
elseif la == 3 && isvarargtype(argtypes[3])
exct = tmerge(𝕃ᵢ, argtypes[2], ArgumentError)
else
exct = ArgumentError
end
return CallMeta(Union{}, exct, EFFECTS_THROWS, NoCallInfo())
return abstract_throw(interp, argtypes, sv)
end
rt = abstract_call_builtin(interp, f, arginfo, sv)
ft = popfirst!(argtypes)
Expand Down

0 comments on commit 9374e49

Please sign in to comment.