Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
precompile: serialize the full edges graph
Browse files Browse the repository at this point in the history
Previously, we would flatten the edges graph during serialization, to
simplify the deserialization codes, but that now was adding complexity
and confusion and uncertainty to the code paths. Clean that all up, so
that we do not do that. This also avoids needing to interact with
backedges at all.

This uses the same algorithm now as #46749 for cycle convergence.
vtjnash committed Oct 4, 2022
1 parent 7f507e6 commit 8724481
Showing 6 changed files with 529 additions and 500 deletions.
972 changes: 502 additions & 470 deletions src/dump.c

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/gf.c
Original file line number Diff line number Diff line change
@@ -222,8 +222,6 @@ JL_DLLEXPORT jl_code_instance_t* jl_new_codeinst(
int32_t const_flags, size_t min_world, size_t max_world,
uint32_t ipo_effects, uint32_t effects, jl_value_t *argescapes,
uint8_t relocatability);
JL_DLLEXPORT void jl_mi_cache_insert(jl_method_instance_t *mi JL_ROOTING_ARGUMENT,
jl_code_instance_t *ci JL_ROOTED_ARGUMENT JL_MAYBE_UNROOTED);

jl_datatype_t *jl_mk_builtin_func(jl_datatype_t *dt, const char *name, jl_fptr_args_t fptr) JL_GC_DISABLED
{
@@ -436,7 +434,8 @@ JL_DLLEXPORT void jl_mi_cache_insert(jl_method_instance_t *mi JL_ROOTING_ARGUMEN
JL_LOCK(&mi->def.method->writelock);
jl_code_instance_t *oldci = jl_atomic_load_relaxed(&mi->cache);
jl_atomic_store_relaxed(&ci->next, oldci);
jl_gc_wb(ci, oldci); // likely older, but just being careful
if (oldci)
jl_gc_wb(ci, oldci);
jl_atomic_store_release(&mi->cache, ci);
jl_gc_wb(mi, ci);
if (jl_is_method(mi->def.method))
@@ -1434,6 +1433,7 @@ static void invalidate_method_instance(void (*f)(jl_code_instance_t*), jl_method
jl_array_ptr_1d_push(_jl_debug_method_invalidation, boxeddepth);
JL_GC_POP();
}
//jl_static_show(JL_STDERR, (jl_value_t*)replaced);
if (!jl_is_method(replaced->def.method))
return; // shouldn't happen, but better to be safe
JL_LOCK(&replaced->def.method->writelock);
@@ -1466,10 +1466,11 @@ static void invalidate_method_instance(void (*f)(jl_code_instance_t*), jl_method
}

// invalidate cached methods that overlap this definition
void invalidate_backedges(void (*f)(jl_code_instance_t*), jl_method_instance_t *replaced_mi, size_t max_world, const char *why)
static void invalidate_backedges(void (*f)(jl_code_instance_t*), jl_method_instance_t *replaced_mi, size_t max_world, const char *why)
{
JL_LOCK(&replaced_mi->def.method->writelock);
jl_array_t *backedges = replaced_mi->backedges;
//jl_static_show(JL_STDERR, (jl_value_t*)replaced_mi);
if (backedges) {
// invalidate callers (if any)
replaced_mi->backedges = NULL;
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
@@ -1839,6 +1839,7 @@ JL_DLLEXPORT jl_value_t *jl_compress_argnames(jl_array_t *syms);
JL_DLLEXPORT jl_array_t *jl_uncompress_argnames(jl_value_t *syms);
JL_DLLEXPORT jl_value_t *jl_uncompress_argname_n(jl_value_t *syms, size_t i);


JL_DLLEXPORT int jl_is_operator(char *sym);
JL_DLLEXPORT int jl_is_unary_operator(char *sym);
JL_DLLEXPORT int jl_is_unary_and_binary_operator(char *sym);
5 changes: 4 additions & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
@@ -289,7 +289,6 @@ extern tracer_cb jl_newmeth_tracer;
void jl_call_tracer(tracer_cb callback, jl_value_t *tracee);
void print_func_loc(JL_STREAM *s, jl_method_t *m);
extern jl_array_t *_jl_debug_method_invalidation JL_GLOBALLY_ROOTED;
void invalidate_backedges(void (*f)(jl_code_instance_t*), jl_method_instance_t *replaced_mi, size_t max_world, const char *why);

extern JL_DLLEXPORT size_t jl_page_size;
extern jl_function_t *jl_typeinf_func;
@@ -715,10 +714,12 @@ jl_method_instance_t *jl_method_lookup(jl_value_t **args, size_t nargs, size_t w

jl_value_t *jl_gf_invoke_by_method(jl_method_t *method, jl_value_t *gf, jl_value_t **args, size_t nargs);
jl_value_t *jl_gf_invoke(jl_value_t *types, jl_value_t *f, jl_value_t **args, size_t nargs);
JL_DLLEXPORT jl_value_t *jl_gf_invoke_lookup_worlds(jl_value_t *types, jl_value_t *mt, size_t world, size_t *min_world, size_t *max_world);
JL_DLLEXPORT jl_value_t *jl_matching_methods(jl_tupletype_t *types, jl_value_t *mt, int lim, int include_ambiguous,
size_t world, size_t *min_valid, size_t *max_valid, int *ambig);
JL_DLLEXPORT jl_value_t *jl_gf_invoke_lookup_worlds(jl_value_t *types, jl_value_t *mt, size_t world, size_t *min_world, size_t *max_world);


JL_DLLEXPORT jl_datatype_t *jl_first_argument_datatype(jl_value_t *argtypes JL_PROPAGATES_ROOT) JL_NOTSAFEPOINT;
JL_DLLEXPORT jl_value_t *jl_argument_datatype(jl_value_t *argt JL_PROPAGATES_ROOT) JL_NOTSAFEPOINT;
JL_DLLEXPORT jl_methtable_t *jl_method_table_for(
@@ -958,6 +959,8 @@ JL_DLLEXPORT jl_method_instance_t *jl_specializations_get_linfo(
jl_method_instance_t *jl_specializations_get_or_insert(jl_method_instance_t *mi_ins);
JL_DLLEXPORT void jl_method_instance_add_backedge(jl_method_instance_t *callee, jl_value_t *invokesig, jl_method_instance_t *caller);
JL_DLLEXPORT void jl_method_table_add_backedge(jl_methtable_t *mt, jl_value_t *typ, jl_value_t *caller);
JL_DLLEXPORT void jl_mi_cache_insert(jl_method_instance_t *mi JL_ROOTING_ARGUMENT,
jl_code_instance_t *ci JL_ROOTED_ARGUMENT JL_MAYBE_UNROOTED);

uint32_t jl_module_next_counter(jl_module_t *m) JL_NOTSAFEPOINT;
jl_tupletype_t *arg_type_tuple(jl_value_t *arg1, jl_value_t **args, size_t nargs);
3 changes: 0 additions & 3 deletions src/opaque_closure.c
Original file line number Diff line number Diff line change
@@ -110,9 +110,6 @@ JL_DLLEXPORT jl_code_instance_t* jl_new_codeinst(
uint32_t ipo_effects, uint32_t effects, jl_value_t *argescapes,
uint8_t relocatability);

JL_DLLEXPORT void jl_mi_cache_insert(jl_method_instance_t *mi JL_ROOTING_ARGUMENT,
jl_code_instance_t *ci JL_ROOTED_ARGUMENT JL_MAYBE_UNROOTED);

JL_DLLEXPORT jl_opaque_closure_t *jl_new_opaque_closure_from_code_info(jl_tupletype_t *argt, jl_value_t *rt_lb, jl_value_t *rt_ub,
jl_module_t *mod, jl_code_info_t *ci, int lineno, jl_value_t *file, int nargs, int isva, jl_value_t *env)
{
39 changes: 17 additions & 22 deletions test/precompile.jl
Original file line number Diff line number Diff line change
@@ -642,16 +642,11 @@ precompile_test_harness("code caching") do dir
msize = which(size, (Vector{<:Any},))
hasspec = false
for i = 1:length(msize.specializations)
if isassigned(msize.specializations, i)
mi = msize.specializations[i]
if isa(mi, Core.MethodInstance)
tt = Base.unwrap_unionall(mi.specTypes)
if tt.parameters[2] == Vector{Cacheb8321416e8a3e2f1.X}
if isdefined(mi, :cache) && isa(mi.cache, Core.CodeInstance) && mi.cache.max_world == typemax(UInt) && mi.cache.inferred !== nothing
hasspec = true
break
end
end
mi = msize.specializations[i]
if isa(mi, Core.MethodInstance) && mi.specTypes == Tuple{typeof(size),Vector{Cacheb8321416e8a3e2f1.X}}
if isdefined(mi, :cache) && isa(mi.cache, Core.CodeInstance) && mi.cache.max_world == typemax(UInt) && mi.cache.inferred !== nothing
hasspec = true
break
end
end
end
@@ -671,7 +666,7 @@ precompile_test_harness("code caching") do dir
# Check that internal methods and their roots are accounted appropriately
minternal = which(M.getelsize, (Vector,))
mi = minternal.specializations[1]
@test Base.unwrap_unionall(mi.specTypes).parameters[2] == Vector{Int32}
@test mi.specTypes == Tuple{typeof(M.getelsize),Vector{Int32}}
ci = mi.cache
@test ci.relocatability == 1
@test ci.inferred !== nothing
@@ -787,7 +782,7 @@ precompile_test_harness("code caching") do dir
end
end

# Invalidations (this test is adapted from from SnoopCompile)
# Invalidations (this test is adapted from SnoopCompile)
function hasvalid(mi, world)
isdefined(mi, :cache) || return false
ci = mi.cache
@@ -898,26 +893,26 @@ precompile_test_harness("code caching") do dir

# Reporting test
@test all(i -> isassigned(invalidations, i), eachindex(invalidations))
idxs = findall(==("insert_backedges"), invalidations)
m = only(methods(MB.call_nbits))
idxsbits = filter(idxs) do i
mi = invalidations[i-1]
mi.def == m
end
idx = only(idxsbits)
for mi in m.specializations
mi === nothing && continue
hv = hasvalid(mi, world)
@test mi.specTypes.parameters[end] === Integer ? !hv : hv
end

setglobal!(Main, :inval, invalidations)
idxs = findall(==("verify_methods"), invalidations)
idxsbits = filter(idxs) do i
mi = invalidations[i-1]
mi.def == m
end
idx = only(idxsbits)
tagbad = invalidations[idx+1]
buildid = invalidations[idx+2]
@test isa(buildid, UInt64)
@test isa(tagbad, Int32)
j = findfirst(==(tagbad), invalidations)
@test invalidations[j+1] == buildid
@test isa(invalidations[j-2], Type)
@test invalidations[j-1] == "insert_backedges_callee"
@test isa(invalidations[j-2], Type)
@test isa(invalidations[j+1], Vector{Any}) # [nbits(::UInt8)]

m = only(methods(MB.map_nbits))
@test !hasvalid(m.specializations[1], world+1) # insert_backedges invalidations also trigger their backedges

0 comments on commit 8724481

Please sign in to comment.