Skip to content

Commit 0aed4e1

Browse files
committed
precompile: fix a number of mistakes in handling precompiled code instance caches
Get the real list of external code instances worthwhile to re-cache directly from `native_functions` instead of calling `jl_compute_new_ext_cis` a second time if we have `native_functions`. If not saving native code though, we can still call it to get a list of code instances that might be worth including even without native code.
1 parent 9427f33 commit 0aed4e1

File tree

13 files changed

+105
-131
lines changed

13 files changed

+105
-131
lines changed

Compiler/src/bindinginvalidations.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function scan_new_method!(method::Method, image_backedges_only::Bool)
188188
@atomic method.did_scan_source |= 0x1
189189
end
190190

191-
function scan_new_methods!(extext_methods::Vector{Any}, internal_methods::Vector{Any}, image_backedges_only::Bool)
191+
function scan_new_methods!(internal_methods::Vector{Any}, image_backedges_only::Bool)
192192
if image_backedges_only && generating_output(true)
193193
# Replacing image bindings is forbidden during incremental precompilation - skip backedge insertion
194194
return
@@ -198,7 +198,4 @@ function scan_new_methods!(extext_methods::Vector{Any}, internal_methods::Vector
198198
scan_new_method!(method, image_backedges_only)
199199
end
200200
end
201-
for tme::Core.TypeMapEntry in extext_methods
202-
scan_new_method!(tme.func::Method, image_backedges_only)
203-
end
204201
end

Compiler/src/precompile.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,6 @@ function compile_and_emit_native(worlds::Vector{UInt},
426426
invokelatest(exit, 1)
427427
end
428428

429-
# Step 5: Always set newly_inferred global for serialization use
430-
#ccall(:jl_set_newly_inferred, Cvoid, (Any,), filter(ci -> ci isa CodeInstance, codeinfos))
431-
432429
return codeinfos
433430

434431
end

Compiler/src/reinfer.jl

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using ..Compiler.Base
44
using ..Compiler: _findsup, store_backedges, JLOptions, get_world_counter,
5-
_methods_by_ftype, get_methodtable, get_ci_mi, should_instrument,
5+
_methods_by_ftype, get_methodtable, get_ci_mi, ci_has_abi, should_instrument,
66
morespecific, RefValue, get_require_world, Vector, IdDict
77
using .Core: CodeInstance, MethodInstance
88

@@ -69,37 +69,46 @@ end
6969
# Restore backedges to external targets
7070
# `edges` = [caller1, ...], the list of worklist-owned code instances internally
7171
# `ext_ci_list` = [caller1, ...], the list of worklist-owned code instances externally
72-
function insert_backedges(edges::Vector{Any}, ext_ci_list::Union{Nothing,Vector{Any}}, extext_methods::Vector{Any}, internal_methods::Vector{Any})
72+
function insert_backedges(ext_ci_list::Union{Nothing,Vector{Any}}, internal_methods::Vector{Any})
7373
# determine which CodeInstance objects are still valid in our image
7474
# to enable any applicable new codes
7575
backedges_only = unsafe_load(cglobal(:jl_first_image_replacement_world, UInt)) == typemax(UInt)
76-
scan_new_methods!(extext_methods, internal_methods, backedges_only)
76+
scan_new_methods!(internal_methods, backedges_only)
7777
workspace = VerifyMethodWorkspace()
78-
_insert_backedges(edges, workspace)
78+
scan_new_code!(internal_methods, workspace)
7979
if ext_ci_list !== nothing
80-
_insert_backedges(ext_ci_list, workspace, #=external=#true)
80+
insert_ext_cache(ext_ci_list)
8181
end
82+
nothing
8283
end
8384

84-
function _insert_backedges(edges::Vector{Any}, workspace::VerifyMethodWorkspace, external::Bool=false)
85-
for i = 1:length(edges)
86-
codeinst = edges[i]::CodeInstance
85+
function scan_new_code!(internal_methods::Vector{Any}, workspace::VerifyMethodWorkspace)
86+
for i = 1:length(internal_methods)
87+
codeinst = internal_methods[i]
88+
codeinst isa CodeInstance || continue
89+
# codeinst.owner === nothing || continue
8790
validation_world = get_world_counter()
8891
verify_method_graph(codeinst, validation_world, workspace)
8992
# After validation, under the world_counter_lock, set max_world to typemax(UInt) for all dependencies
9093
# (recursively). From that point onward the ordinary backedge mechanism is responsible for maintaining
9194
# validity.
9295
@ccall jl_promote_ci_to_current(codeinst::Any, validation_world::UInt)::Cvoid
96+
end
97+
end
98+
99+
function insert_ext_cache(edges::Vector{Any})
100+
for i = 1:length(edges)
101+
codeinst = edges[i]::CodeInstance
93102
minvalid = codeinst.min_world
94103
maxvalid = codeinst.max_world
95104
# Finally, if this CI is still valid in some world age and belongs to an external method(specialization),
96-
# poke it that mi's cache
97-
if maxvalid minvalid && external
105+
# poke it in that mi's cache, unless there is already one there that has an invoke pointer
106+
if maxvalid minvalid
98107
caller = get_ci_mi(codeinst)
99108
@assert isdefined(codeinst, :inferred) # See #53586, #53109
100109
inferred = @ccall jl_rettype_inferred(
101110
codeinst.owner::Any, caller::Any, minvalid::UInt, maxvalid::UInt)::Any
102-
if inferred !== nothing
111+
if inferred !== nothing && (ci_has_abi(inferred::CodeInstance) || !ci_has_abi(codeinst))
103112
# We already got a code instance for this world age range from
104113
# somewhere else - we don't need this one.
105114
else
@@ -658,7 +667,7 @@ function verify_invokesig(@nospecialize(invokesig), expected::Method, world::UIn
658667
end
659668

660669
# Wrapper to call insert_backedges in typeinf_world for external calls
661-
function insert_backedges_typeinf(edges::Vector{Any}, ext_ci_list::Union{Nothing,Vector{Any}}, extext_methods::Vector{Any}, internal_methods::Vector{Any})
662-
args = Any[insert_backedges, edges, ext_ci_list, extext_methods, internal_methods]
670+
function insert_backedges_typeinf(ext_ci_list::Union{Nothing,Vector{Any}}, internal_methods::Vector{Any})
671+
args = Any[insert_backedges, ext_ci_list, internal_methods]
663672
return ccall(:jl_call_in_typeinf_world, Any, (Ptr{Any}, Cint), args, length(args))
664673
end

Compiler/src/typeinfer.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,8 @@ prepared interp to be able to provide source code for it.
12881288
"""
12891289
const SOURCE_MODE_GET_SOURCE = 0xf
12901290

1291+
ci_has_abi(code::CodeInstance) = (@atomic :acquire code.invoke) !== C_NULL
1292+
12911293
"""
12921294
ci_has_abi(interp::AbstractInterpreter, code::CodeInstance)
12931295
@@ -1296,7 +1298,7 @@ interp gave it to the runtime system (either because it already has an ->invoke
12961298
ptr, or because interp has source that could be compiled).
12971299
"""
12981300
function ci_has_abi(interp::AbstractInterpreter, code::CodeInstance)
1299-
(@atomic :acquire code.invoke) !== C_NULL && return true
1301+
ci_has_abi(code) && return true
13001302
return ci_has_source(interp, code)
13011303
end
13021304

base/loading.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,12 +1283,10 @@ function _include_from_serialized(pkg::PkgId, path::String, ocachepath::Union{No
12831283
end
12841284

12851285
sv = sv::SimpleVector
1286-
edges = sv[3]::Vector{Any}
1287-
ext_edges = sv[4]::Union{Nothing,Vector{Any}}
1288-
extext_methods = sv[5]::Vector{Any}
1289-
internal_methods = sv[6]::Vector{Any}
1286+
ext_edges = sv[3]::Union{Nothing,Vector{Any}}
1287+
internal_methods = sv[4]::Vector{Any}
12901288
Compiler.@zone "CC: INSERT_BACKEDGES" begin
1291-
ReinferUtils.insert_backedges_typeinf(edges, ext_edges, extext_methods, internal_methods)
1289+
ReinferUtils.insert_backedges_typeinf(ext_edges, internal_methods)
12921290
end
12931291
restored = register_restored_modules(sv, pkg, path)
12941292

src/aotcompile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void jl_get_function_id_impl(void *native_code, jl_code_instance_t *codeinst,
9292
}
9393

9494
extern "C" JL_DLLEXPORT_CODEGEN void
95-
jl_get_llvm_mis_impl(void *native_code, size_t *num_elements, jl_method_instance_t **data)
95+
jl_get_llvm_cis_impl(void *native_code, size_t *num_elements, jl_code_instance_t **data)
9696
{
9797
jl_native_code_desc_t *desc = (jl_native_code_desc_t *)native_code;
9898
auto &map = desc->jl_fvar_map;
@@ -105,7 +105,7 @@ jl_get_llvm_mis_impl(void *native_code, size_t *num_elements, jl_method_instance
105105
assert(*num_elements == map.size());
106106
size_t i = 0;
107107
for (auto &ci : map) {
108-
data[i++] = jl_get_ci_mi(ci.first);
108+
data[i++] = ci.first;
109109
}
110110
}
111111

src/codegen-stubs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
JL_DLLEXPORT void jl_dump_native_fallback(void *native_code,
1414
const char *bc_fname, const char *unopt_bc_fname, const char *obj_fname, const char *asm_fname,
1515
ios_t *z, ios_t *s) UNAVAILABLE
16-
JL_DLLEXPORT void jl_get_llvm_gvs_fallback(void *native_code, arraylist_t *gvs) UNAVAILABLE
17-
JL_DLLEXPORT void jl_get_llvm_gv_inits_fallback(void *native_code, arraylist_t *inits) UNAVAILABLE
18-
JL_DLLEXPORT void jl_get_llvm_external_fns_fallback(void *native_code, arraylist_t *gvs) UNAVAILABLE
19-
JL_DLLEXPORT void jl_get_llvm_mis_fallback(void *native_code, arraylist_t* MIs) UNAVAILABLE
16+
JL_DLLEXPORT void jl_get_llvm_gvs_fallback(void *native_code, size_t *num, void **gvs) UNAVAILABLE
17+
JL_DLLEXPORT void jl_get_llvm_gv_inits_fallback(void *native_code, size_t *num, void **inits) UNAVAILABLE
18+
JL_DLLEXPORT void jl_get_llvm_external_fns_fallback(void *native_code, size_t *num, void **gvs) UNAVAILABLE
19+
JL_DLLEXPORT void jl_get_llvm_cis_fallback(void *native_code, size_t *num, void **CIs) UNAVAILABLE
2020

2121
JL_DLLEXPORT jl_value_t *jl_dump_method_asm_fallback(jl_method_instance_t *linfo, size_t world,
2222
char emit_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary) UNAVAILABLE

src/jl_exported_funcs.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@
517517
YY(jl_get_llvm_gvs) \
518518
YY(jl_get_llvm_gv_inits) \
519519
YY(jl_get_llvm_external_fns) \
520-
YY(jl_get_llvm_mis) \
520+
YY(jl_get_llvm_cis) \
521521
YY(jl_dump_function_asm) \
522522
YY(jl_LLVMCreateDisasm) \
523523
YY(jl_LLVMDisasmInstruction) \

src/julia_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,8 +2067,8 @@ JL_DLLIMPORT void jl_get_function_id(void *native_code, jl_code_instance_t *ncod
20672067
int32_t *func_idx, int32_t *specfunc_idx);
20682068
JL_DLLIMPORT void jl_register_fptrs(uint64_t image_base, const struct _jl_image_fptrs_t *fptrs,
20692069
jl_method_instance_t **linfos, size_t n);
2070-
JL_DLLIMPORT void jl_get_llvm_mis(void *native_code, size_t *num_els,
2071-
jl_method_instance_t *MIs);
2070+
JL_DLLIMPORT void jl_get_llvm_cis(void *native_code, size_t *num_els,
2071+
jl_code_instance_t **CIs);
20722072
JL_DLLIMPORT void jl_init_codegen(void);
20732073
JL_DLLIMPORT void jl_teardown_codegen(void) JL_NOTSAFEPOINT;
20742074
JL_DLLIMPORT int jl_getFunctionInfo(jl_frame_t **frames, uintptr_t pointer, int skipC, int noInline) JL_NOTSAFEPOINT;

0 commit comments

Comments
 (0)