Skip to content

Commit fd72b43

Browse files
committed
change cycle-limit heuristic in inference to use method_for_inference_heuristics mechanism [ci skip]
1 parent 4fa8411 commit fd72b43

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

base/inference.jl

+37-17
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,14 @@ function InferenceState(result::InferenceResult,
361361
end
362362

363363
function get_staged(li::MethodInstance)
364-
return ccall(:jl_code_for_staged, Any, (Any,), li)::CodeInfo
364+
try
365+
# user code might throw errors – ignore them
366+
return ccall(:jl_code_for_staged, Any, (Any,), li)::CodeInfo
367+
catch
368+
return nothing
369+
end
365370
end
366371

367-
368372
mutable struct OptimizationState
369373
linfo::MethodInstance
370374
vararg_type_container #::Type
@@ -462,12 +466,7 @@ end
462466
function retrieve_code_info(linfo::MethodInstance)
463467
m = linfo.def::Method
464468
if isdefined(m, :generator)
465-
try
466-
# user code might throw errors – ignore them
467-
c = get_staged(linfo)
468-
catch
469-
return nothing
470-
end
469+
return get_staged(linfo)
471470
else
472471
# TODO: post-inference see if we can swap back to the original arrays?
473472
if isa(m.source, Array{UInt8,1})
@@ -2073,6 +2072,24 @@ function abstract_call_method_with_const_args(argtypes::Vector{Any}, match::Simp
20732072
return result
20742073
end
20752074

2075+
function method_for_inference_heuristics(infstate::InferenceState)
2076+
m = infstate.src.method_for_inference_heuristics
2077+
return isa(m, Method) ? m : infstate.linfo.def
2078+
end
2079+
2080+
function method_for_inference_heuristics(method::Method, @nospecialize(sig), sparams, world)
2081+
if isdefined(method, :generator) && method.generator.expand_early
2082+
method_instance = code_for_method(method, sig, sparams, world, false)
2083+
if isa(method_instance, MethodInstance)
2084+
cinfo = get_staged(method_instance)
2085+
if isa(cinfo, CodeInfo) && isa(cinfo.method_for_inference_heuristics, Method)
2086+
return cinfo.method_for_inference_heuristics
2087+
end
2088+
end
2089+
end
2090+
return method
2091+
end
2092+
20762093
function abstract_call_method(method::Method, @nospecialize(sig), sparams::SimpleVector, sv::InferenceState)
20772094
topmost = nothing
20782095
# Limit argument type tuple growth of functions:
@@ -2082,16 +2099,18 @@ function abstract_call_method(method::Method, @nospecialize(sig), sparams::Simpl
20822099
cyclei = 0
20832100
infstate = sv
20842101
edgecycle = false
2102+
checked_method = method_for_inference_heuristics(method, sig, sparams, sv.params.world)
20852103
while !(infstate === nothing)
20862104
infstate = infstate::InferenceState
2087-
if method === infstate.linfo.def
2088-
if infstate.linfo.specTypes == sig
2089-
# avoid widening when detecting self-recursion
2090-
# TODO: merge call cycle and return right away
2091-
topmost = nothing
2092-
edgecycle = true
2093-
break
2094-
end
2105+
if infstate.linfo.specTypes == sig
2106+
# avoid widening when detecting self-recursion
2107+
# TODO: merge call cycle and return right away
2108+
topmost = nothing
2109+
edgecycle = true
2110+
break
2111+
end
2112+
working_method = method_for_inference_heuristics(infstate)
2113+
if checked_method === working_method
20952114
if topmost === nothing
20962115
# inspect the parent of this edge,
20972116
# to see if they are the same Method as sv
@@ -2110,7 +2129,8 @@ function abstract_call_method(method::Method, @nospecialize(sig), sparams::Simpl
21102129
# then check the parent link
21112130
if topmost === nothing && parent !== nothing
21122131
parent = parent::InferenceState
2113-
if parent.cached && parent.linfo.def === sv.linfo.def
2132+
parent_method = method_for_inference_heuristics(parent)
2133+
if parent.cached && parent_method === working_method
21142134
topmost = infstate
21152135
edgecycle = true
21162136
end

0 commit comments

Comments
 (0)