Skip to content

Commit 9374e49

Browse files
authored
allow external AbstractInterpreter to overload throw-call handling (#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.
1 parent e39e77f commit 9374e49

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,13 +2046,30 @@ function abstract_finalizer(interp::AbstractInterpreter, argtypes::Vector{Any},
20462046
return CallMeta(Nothing, Any, Effects(), NoCallInfo())
20472047
end
20482048

2049+
function abstract_throw(interp::AbstractInterpreter, argtypes::Vector{Any}, ::AbsIntState)
2050+
na = length(argtypes)
2051+
𝕃ᵢ = typeinf_lattice(interp)
2052+
if na == 2
2053+
argtype2 = argtypes[2]
2054+
if isvarargtype(argtype2)
2055+
exct = tmerge(𝕃ᵢ, unwrapva(argtype2), ArgumentError)
2056+
else
2057+
exct = argtype2
2058+
end
2059+
elseif na == 3 && isvarargtype(argtypes[3])
2060+
exct = tmerge(𝕃ᵢ, argtypes[2], ArgumentError)
2061+
else
2062+
exct = ArgumentError
2063+
end
2064+
return CallMeta(Union{}, exct, EFFECTS_THROWS, NoCallInfo())
2065+
end
2066+
20492067
# call where the function is known exactly
20502068
function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
20512069
arginfo::ArgInfo, si::StmtInfo, sv::AbsIntState,
20522070
max_methods::Int = get_max_methods(interp, f, sv))
20532071
(; fargs, argtypes) = arginfo
20542072
la = length(argtypes)
2055-
20562073
𝕃ᵢ = typeinf_lattice(interp)
20572074
if isa(f, Builtin)
20582075
if f === _apply_iterate
@@ -2066,19 +2083,7 @@ function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
20662083
elseif f === applicable
20672084
return abstract_applicable(interp, argtypes, sv, max_methods)
20682085
elseif f === throw
2069-
if la == 2
2070-
arg2 = argtypes[2]
2071-
if isvarargtype(arg2)
2072-
exct = tmerge(𝕃ᵢ, unwrapva(argtypes[2]), ArgumentError)
2073-
else
2074-
exct = arg2
2075-
end
2076-
elseif la == 3 && isvarargtype(argtypes[3])
2077-
exct = tmerge(𝕃ᵢ, argtypes[2], ArgumentError)
2078-
else
2079-
exct = ArgumentError
2080-
end
2081-
return CallMeta(Union{}, exct, EFFECTS_THROWS, NoCallInfo())
2086+
return abstract_throw(interp, argtypes, sv)
20822087
end
20832088
rt = abstract_call_builtin(interp, f, arginfo, sv)
20842089
ft = popfirst!(argtypes)

0 commit comments

Comments
 (0)