Skip to content

Commit

Permalink
Precompile correct invoke-targets (#46907)
Browse files Browse the repository at this point in the history
This fixes backedge-based invalidation when a precompiled `invoke`
is followed by loading a package that adds new specializations
for the `invoke`d method. An example is LowRankApprox.jl, where
FillArrays adds a specialization to `unique`.

(cherry picked from commit 698beed)
  • Loading branch information
timholy authored and vtjnash committed Nov 29, 2022
1 parent 10288cc commit 8d2e0f3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
90 changes: 58 additions & 32 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,7 @@ static void jl_collect_backedges(jl_array_t *edges, jl_array_t *ext_targets)
jl_value_t *invokeTypes;
jl_method_instance_t *c;
size_t i;
size_t world = jl_get_world_counter();
void **table = edges_map.table; // edges is caller => callees
size_t table_size = edges_map.size;
for (i = 0; i < table_size; i += 2) {
Expand Down Expand Up @@ -1369,15 +1370,28 @@ static void jl_collect_backedges(jl_array_t *edges, jl_array_t *ext_targets)
size_t min_valid = 0;
size_t max_valid = ~(size_t)0;
int ambig = 0;
jl_value_t *matches = jl_matching_methods((jl_tupletype_t*)sig, jl_nothing, -1, 0, jl_atomic_load_acquire(&jl_world_counter), &min_valid, &max_valid, &ambig);
if (matches == jl_false) {
valid = 0;
break;
}
size_t k;
for (k = 0; k < jl_array_len(matches); k++) {
jl_method_match_t *match = (jl_method_match_t *)jl_array_ptr_ref(matches, k);
jl_array_ptr_set(matches, k, match->method);
jl_value_t *matches;
if (mode == 2 && callee && jl_is_method_instance(callee) && jl_is_type(sig)) {
// invoke, use subtyping
jl_methtable_t *mt = jl_method_get_table(((jl_method_instance_t*)callee)->def.method);
size_t min_world, max_world;
matches = jl_gf_invoke_lookup_worlds(sig, (jl_value_t*)mt, world, &min_world, &max_world);
if (matches == jl_nothing) {
valid = 0;
break;
}
matches = (jl_value_t*)((jl_method_match_t*)matches)->method;
} else {
matches = jl_matching_methods((jl_tupletype_t*)sig, jl_nothing, -1, 0, jl_atomic_load_acquire(&jl_world_counter), &min_valid, &max_valid, &ambig);
if (matches == jl_false) {
valid = 0;
break;
}
size_t k;
for (k = 0; k < jl_array_len(matches); k++) {
jl_method_match_t *match = (jl_method_match_t *)jl_array_ptr_ref(matches, k);
jl_array_ptr_set(matches, k, match->method);
}
}
jl_array_ptr_1d_push(ext_targets, mode == 1 ? NULL : sig);
jl_array_ptr_1d_push(ext_targets, callee);
Expand Down Expand Up @@ -2495,8 +2509,10 @@ static void jl_verify_edges(jl_array_t *targets, jl_array_t **pvalids)
jl_array_t *valids = jl_alloc_array_1d(jl_array_uint8_type, l);
memset(jl_array_data(valids), 1, l);
jl_value_t *loctag = NULL, *matches = NULL;
JL_GC_PUSH2(&loctag, &matches);
jl_methtable_t *mt = NULL;
JL_GC_PUSH3(&loctag, &matches, &mt);
*pvalids = valids;
size_t world = jl_get_world_counter();
for (i = 0; i < l; i++) {
jl_value_t *invokesig = jl_array_ptr_ref(targets, i * 3);
jl_value_t *callee = jl_array_ptr_ref(targets, i * 3 + 1);
Expand All @@ -2508,33 +2524,43 @@ static void jl_verify_edges(jl_array_t *targets, jl_array_t **pvalids)
else {
sig = callee == NULL ? invokesig : callee;
}
jl_array_t *expected = (jl_array_t*)jl_array_ptr_ref(targets, i * 3 + 2);
assert(jl_is_array(expected));
jl_value_t *expected = jl_array_ptr_ref(targets, i * 3 + 2);
int valid = 1;
size_t min_valid = 0;
size_t max_valid = ~(size_t)0;
int ambig = 0;
// TODO: possibly need to included ambiguities too (for the optimizer correctness)?
matches = jl_matching_methods((jl_tupletype_t*)sig, jl_nothing, -1, 0, jl_atomic_load_acquire(&jl_world_counter), &min_valid, &max_valid, &ambig);
if (matches == jl_false || jl_array_len(matches) != jl_array_len(expected)) {
valid = 0;
}
else {
size_t j, k, l = jl_array_len(expected);
for (k = 0; k < jl_array_len(matches); k++) {
jl_method_match_t *match = (jl_method_match_t*)jl_array_ptr_ref(matches, k);
jl_method_t *m = match->method;
for (j = 0; j < l; j++) {
if (m == (jl_method_t*)jl_array_ptr_ref(expected, j))
int use_invoke = invokesig == NULL || callee == NULL ? 0 : 1;
if (!use_invoke) {
// TODO: possibly need to included ambiguities too (for the optimizer correctness)?
matches = jl_matching_methods((jl_tupletype_t*)sig, jl_nothing, -1, 0, jl_atomic_load_acquire(&jl_world_counter), &min_valid, &max_valid, &ambig);
if (matches == jl_false || jl_array_len(matches) != jl_array_len(expected)) {
valid = 0;
}
else {
assert(jl_is_array(expected));
size_t j, k, l = jl_array_len(expected);
for (k = 0; k < jl_array_len(matches); k++) {
jl_method_match_t *match = (jl_method_match_t*)jl_array_ptr_ref(matches, k);
jl_method_t *m = match->method;
for (j = 0; j < l; j++) {
if (m == (jl_method_t*)jl_array_ptr_ref(expected, j))
break;
}
if (j == l) {
// intersection has a new method or a method was
// deleted--this is now probably no good, just invalidate
// everything about it now
valid = 0;
break;
}
}
if (j == l) {
// intersection has a new method or a method was
// deleted--this is now probably no good, just invalidate
// everything about it now
valid = 0;
break;
}
}
} else {
mt = jl_method_get_table(((jl_method_instance_t*)callee)->def.method);
size_t min_world, max_world;
matches = jl_gf_invoke_lookup_worlds(invokesig, (jl_value_t*)mt, world, &min_world, &max_world);
if (matches == jl_nothing || expected != (jl_value_t*)((jl_method_match_t*)matches)->method) {
valid = 0;
}
}
jl_array_uint8_set(valids, i, valid);
Expand All @@ -2546,7 +2572,7 @@ static void jl_verify_edges(jl_array_t *targets, jl_array_t **pvalids)
jl_array_ptr_1d_push(_jl_debug_method_invalidation, loctag);
loctag = jl_box_uint64(jl_worklist_key(serializer_worklist));
jl_array_ptr_1d_push(_jl_debug_method_invalidation, loctag);
if (matches != jl_false) {
if (!use_invoke && matches != jl_false) {
// setdiff!(matches, expected)
size_t j, k, ins = 0;
for (j = 0; j < jl_array_len(matches); j++) {
Expand Down
3 changes: 2 additions & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,14 @@ jl_value_t *jl_gf_invoke_by_method(jl_method_t *method, jl_value_t *gf, jl_value
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_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(
jl_value_t *argtypes JL_PROPAGATES_ROOT) JL_NOTSAFEPOINT;
JL_DLLEXPORT jl_methtable_t *jl_method_get_table(
jl_method_t *method) JL_NOTSAFEPOINT;
jl_method_t *method JL_PROPAGATES_ROOT) JL_NOTSAFEPOINT;
jl_methtable_t *jl_argument_method_table(jl_value_t *argt JL_PROPAGATES_ROOT);

JL_DLLEXPORT int jl_pointer_egal(jl_value_t *t);
Expand Down

0 comments on commit 8d2e0f3

Please sign in to comment.