@@ -1374,8 +1374,29 @@ function typeinf_type(interp::AbstractInterpreter, mi::MethodInstance)
1374
1374
return ci. rettype
1375
1375
end
1376
1376
1377
+ # Resolve a call, as described by `argtype` to a single matching
1378
+ # Method and return a compilable MethodInstance for the call, if
1379
+ # it will be runtime-dispatched to exactly that MethodInstance
1380
+ function compileable_specialization_for_call (interp:: AbstractInterpreter , @nospecialize (argtype))
1381
+ matches = findall (argtype, method_table (interp); limit = 1 )
1382
+ matches === nothing && return nothing
1383
+ length (matches. matches) == 0 && return nothing
1384
+ match = only (matches. matches)
1385
+
1386
+ compileable_atype = get_compileable_sig (match. method, match. spec_types, match. sparams)
1387
+ compileable_atype === nothing && return nothing
1388
+ if match. spec_types != = compileable_atype
1389
+ sp_ = ccall (:jl_type_intersection_with_env , Any, (Any, Any), compileable_atype, method. sig):: SimpleVector
1390
+ sparams = sp_[2 ]:: SimpleVector
1391
+ mi = specialize_method (match. method, compileable_atype, sparams)
1392
+ else
1393
+ mi = specialize_method (match. method, compileable_atype, match. sparams)
1394
+ end
1395
+ return mi
1396
+ end
1397
+
1377
1398
# collect a list of all code that is needed along with CodeInstance to codegen it fully
1378
- function collectinvokes! (wq:: Vector{CodeInstance} , ci:: CodeInfo )
1399
+ function collectinvokes! (wq:: Vector{CodeInstance} , ci:: CodeInfo , sptypes :: Vector{VarState} )
1379
1400
src = ci. code
1380
1401
for i = 1 : length (src)
1381
1402
stmt = src[i]
@@ -1384,6 +1405,31 @@ function collectinvokes!(wq::Vector{CodeInstance}, ci::CodeInfo)
1384
1405
edge = stmt. args[1 ]
1385
1406
edge isa CodeInstance && isdefined (edge, :inferred ) && push! (wq, edge)
1386
1407
end
1408
+
1409
+ if isexpr (stmt, :call )
1410
+ farg = stmt. args[1 ]
1411
+ ! applicable (argextype, farg, ci, sptypes) && continue # TODO : Why is this failing during bootstrap
1412
+ ftyp = widenconst (argextype (farg, ci, sptypes))
1413
+ if ftyp <: Builtin
1414
+ # TODO : Make interp elsewhere
1415
+ interp = NativeInterpreter (Base. get_world_counter ())
1416
+ if Core. finalizer isa ftyp && length (stmt. args) == 3
1417
+ finalizer = argextype (stmt. args[2 ], ci, sptypes)
1418
+ obj = argextype (stmt. args[3 ], ci, sptypes)
1419
+ atype = argtypes_to_type (Any[finalizer, obj])
1420
+
1421
+ mi = compileable_specialization_for_call (interp, atype)
1422
+ mi === nothing && continue
1423
+
1424
+ if mi. def. primary_world <= Base. get_world_counter () <= mi. def. deleted_world
1425
+ ci = typeinf_ext (interp, mi, SOURCE_MODE_GET_SOURCE)
1426
+ # TODO : separate workqueue for NativeInterpreter
1427
+ ci isa CodeInstance && push! (wq, ci)
1428
+ end
1429
+ # push!(wq, mi)
1430
+ end
1431
+ end
1432
+ end
1387
1433
# TODO : handle other StmtInfo like @cfunction and OpaqueClosure?
1388
1434
end
1389
1435
end
@@ -1420,8 +1466,9 @@ function add_codeinsts_to_jit!(interp::AbstractInterpreter, ci, source_mode::UIn
1420
1466
end
1421
1467
end
1422
1468
push! (inspected, callee)
1423
- collectinvokes! (tocompile, src)
1424
1469
mi = get_ci_mi (callee)
1470
+ sptypes = sptypes_from_meth_instance (mi)
1471
+ collectinvokes! (tocompile, src, sptypes)
1425
1472
if iszero (ccall (:jl_mi_cache_has_ci , Cint, (Any, Any), mi, callee))
1426
1473
cached = ccall (:jl_get_ci_equiv , Any, (Any, UInt), callee, get_inference_world (interp)):: CodeInstance
1427
1474
if cached === callee
@@ -1519,7 +1566,8 @@ function typeinf_ext_toplevel(methods::Vector{Any}, worlds::Vector{UInt}, trim_m
1519
1566
end
1520
1567
push! (inspected, callee)
1521
1568
if src isa CodeInfo
1522
- collectinvokes! (tocompile, src)
1569
+ sptypes = sptypes_from_meth_instance (mi)
1570
+ collectinvokes! (tocompile, src, sptypes)
1523
1571
# try to reuse an existing CodeInstance from before to avoid making duplicates in the cache
1524
1572
if iszero (ccall (:jl_mi_cache_has_ci , Cint, (Any, Any), mi, callee))
1525
1573
cached = ccall (:jl_get_ci_equiv , Any, (Any, UInt), callee, this_world):: CodeInstance
0 commit comments