@@ -173,6 +173,39 @@ function finish!(interp::AbstractInterpreter, caller::InferenceState)
173173 return nothing
174174end
175175
176+ function finish! (interp:: AbstractInterpreter , mi:: MethodInstance , ci:: CodeInstance , src:: CodeInfo )
177+ user_edges = src. edges
178+ edges = user_edges isa SimpleVector ? user_edges : user_edges === nothing ? Core. svec () : Core. svec (user_edges... )
179+ relocatability = 0x1
180+ const_flag = false
181+ di = src. debuginfo
182+ rettype = Any
183+ exctype = Any
184+ rettype_const = nothing
185+ const_flags = 0x0
186+ ipo_effects = zero (UInt32)
187+ min_world = src. min_world
188+ max_world = src. max_world
189+ if max_world >= get_world_counter ()
190+ max_world = typemax (UInt)
191+ end
192+ if max_world == typemax (UInt)
193+ # if we can record all of the backedges in the global reverse-cache,
194+ # we can now widen our applicability in the global cache too
195+ store_backedges (ci, edges)
196+ end
197+ ccall (:jl_fill_codeinst , Cvoid, (Any, Any, Any, Any, Int32, UInt, UInt, UInt32, Any, Any, Any),
198+ ci, rettype, exctype, nothing , const_flags, min_world, max_world, ipo_effects, nothing , di, edges)
199+ ccall (:jl_update_codeinst , Cvoid, (Any, Any, Int32, UInt, UInt, UInt32, Any, UInt8, Any, Any),
200+ ci, nothing , const_flag, min_world, max_world, ipo_effects, nothing , relocatability, di, edges)
201+ code_cache (interp)[mi] = ci
202+ if isdefined (interp, :codegen )
203+ interp. codegen[ci] = src
204+ end
205+ engine_reject (interp, ci)
206+ return nothing
207+ end
208+
176209function finish_nocycle (:: AbstractInterpreter , frame:: InferenceState )
177210 finishinfer! (frame, frame. interp)
178211 opt = frame. result. src
@@ -826,7 +859,7 @@ function typeinf_edge(interp::AbstractInterpreter, method::Method, @nospecialize
826859 end
827860 end
828861 end
829- if ccall (:jl_get_module_infer , Cint, (Any,), method. module) == 0 && ! generating_output ( #= incremental =# false )
862+ if ccall (:jl_get_module_infer , Cint, (Any,), method. module) == 0
830863 add_remark! (interp, caller, " [typeinf_edge] Inference is disabled for the target module" )
831864 return Future (MethodCallResult (interp, caller, method, Any, Any, Effects (), nothing , edgecycle, edgelimited))
832865 end
@@ -1096,15 +1129,6 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance, source_mod
10961129 end
10971130 end
10981131 def = mi. def
1099- if isa (def, Method)
1100- if ccall (:jl_get_module_infer , Cint, (Any,), def. module) == 0 && ! generating_output (#= incremental=# false )
1101- src = retrieve_code_info (mi, get_inference_world (interp))
1102- src isa CodeInfo || return nothing
1103- return CodeInstance (mi, cache_owner (interp), Any, Any, nothing , src, Int32 (0 ),
1104- get_inference_world (interp), get_inference_world (interp),
1105- UInt32 (0 ), nothing , UInt8 (0 ), src. debuginfo, src. edges)
1106- end
1107- end
11081132 ci = engine_reserve (interp, mi)
11091133 # check cache again if it is still new after reserving in the engine
11101134 let code = get (code_cache (interp), mi, nothing )
@@ -1117,11 +1141,22 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance, source_mod
11171141 end
11181142 end
11191143 end
1144+ if isa (def, Method) && ccall (:jl_get_module_infer , Cint, (Any,), def. module) == 0
1145+ src = retrieve_code_info (mi, get_inference_world (interp))
1146+ if src isa CodeInfo
1147+ finish! (interp, mi, ci, src)
1148+ else
1149+ engine_reject (interp, ci)
1150+ end
1151+ ccall (:jl_typeinf_timing_end , Cvoid, (UInt64,), start_time)
1152+ return ci
1153+ end
11201154 result = InferenceResult (mi, typeinf_lattice (interp))
11211155 result. ci = ci
11221156 frame = InferenceState (result, #= cache_mode=# :global , interp)
11231157 if frame === nothing
11241158 engine_reject (interp, ci)
1159+ ccall (:jl_typeinf_timing_end , Cvoid, (UInt64,), start_time)
11251160 return nothing
11261161 end
11271162 typeinf (interp, frame)
@@ -1263,18 +1298,29 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim::
12631298 callee in inspected && continue
12641299 push! (inspected, callee)
12651300 # now make sure everything has source code, if desired
1266- # TODO : typeinf_code could return something with different edges/ages (needing an update to callee), which we don't handle here
1301+ mi = get_ci_mi (callee)
1302+ def = mi. def
12671303 if use_const_api (callee)
1268- src = codeinfo_for_const (interp, callee . def , code. rettype_const)
1304+ src = codeinfo_for_const (interp, mi , code. rettype_const)
12691305 elseif haskey (interp. codegen, callee)
12701306 src = interp. codegen[callee]
1307+ elseif isa (def, Method) && ccall (:jl_get_module_infer , Cint, (Any,), def. module) == 0 && ! trim
1308+ src = retrieve_code_info (mi, get_inference_world (interp))
12711309 else
1272- src = typeinf_code (interp, callee. def, true )
1310+ # TODO : typeinf_code could return something with different edges/ages/owner/abi (needing an update to callee), which we don't handle here
1311+ src = typeinf_code (interp, mi, true )
12731312 end
12741313 if src isa CodeInfo
12751314 collectinvokes! (tocompile, src)
1315+ # It is somewhat ambiguous if typeinf_ext might have callee in the caches,
1316+ # but for the purpose of native compile, we always want them put there.
1317+ if iszero (ccall (:jl_mi_cache_has_ci , Cint, (Any, Any), mi, callee))
1318+ code_cache (interp)[mi] = callee
1319+ end
12761320 push! (codeinfos, callee)
12771321 push! (codeinfos, src)
1322+ elseif trim
1323+ println (" warning: failed to get code for " , mi)
12781324 end
12791325 end
12801326 end
0 commit comments