Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Aug 22, 2022
1 parent 1b9535b commit d1923b0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ end

function invoke_signature(invokesig::Vector{Any})
ft, argtyps = widenconst(invokesig[2]), instanceof_tfunc(widenconst(invokesig[3]))[1]
return rewrap_unionall(Tuple{typeof(f), unwrap_unionall(argtyps).parameters...}, argtyps)
return rewrap_unionall(Tuple{ft, unwrap_unionall(argtyps).parameters...}, argtyps)
end

function concrete_eval_call(interp::AbstractInterpreter,
Expand Down
4 changes: 2 additions & 2 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,14 @@ function add_cycle_backedge!(frame::InferenceState, caller::InferenceState, curr
end

# temporarily accumulate our edges to later add as backedges in the callee
function add_backedge!(li::MethodInstance, caller::InferenceState, invokesig::Union{Nothing,Vector{Any}}=nothing)
function add_backedge!(li::MethodInstance, caller::InferenceState, invokesig::Union{Nothing,DataType}=nothing)
isa(caller.linfo.def, Method) || return # don't add backedges to toplevel exprs
edges = caller.stmt_edges[caller.currpc]
if edges === nothing
edges = caller.stmt_edges[caller.currpc] = []
end
if invokesig !== nothing
push!(edges, invoke_signature(invokesig))
push!(edges, invokesig)
end
push!(edges, li)
return nothing
Expand Down
8 changes: 3 additions & 5 deletions base/compiler/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,10 @@ is_no_constprop(method::Union{Method,CodeInfo}) = method.constprop == 0x02
#############

"""
BackedgeIterator(mi::MethodInstance)
BackedgeIterator(backedges::Vector{Any})
Return an iterator over a list of backedges, which may be extracted
from `mi`. Iteration returns `(sig, caller)` elements, which will be one of
the following:
Return an iterator over a list of backedges. Iteration returns `(sig, caller)` elements,
which will be one of the following:
- `(nothing, caller::MethodInstance)`: a call made by ordinary inferrable dispatch
- `(invokesig, caller::MethodInstance)`: a call made by `invoke(f, invokesig, args...)`
Expand All @@ -254,7 +252,7 @@ julia> callyou(2.0)
julia> mi = first(which(callme, (Any,)).specializations)
MethodInstance for callme(::Float64)
julia> @eval Core.Compiler for (sig, caller) in BackedgeIterator(Main.mi)
julia> @eval Core.Compiler for (sig, caller) in BackedgeIterator(Main.mi.backedges)
println(sig)
println(caller)
end
Expand Down
28 changes: 14 additions & 14 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static int has_backedge_to_worklist(jl_method_instance_t *mi, htable_t *visited)
size_t i = 0, n = jl_array_len(mi->backedges);
jl_method_instance_t *be;
while (i < n) {
i = get_next_backedge(mi->backedges, i, NULL, &be);
i = get_next_edge(mi->backedges, i, NULL, &be);
if (has_backedge_to_worklist(be, visited)) {
bp = ptrhash_bp(visited, mi); // re-acquire since rehashing might change the location
*bp = (void*)((char*)HT_NOTFOUND + 2); // found
Expand Down Expand Up @@ -954,7 +954,7 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
jl_value_t *invokeTypes;
jl_method_instance_t *backedge;
while (i < l) {
i = get_next_backedge(backedges, i, &invokeTypes, &backedge);
i = get_next_edge(backedges, i, &invokeTypes, &backedge);
if (module_in_worklist(backedge->def.method->module) || method_instance_in_queue(backedge)) {
if (invokeTypes)
b_edges[ins++] = invokeTypes;
Expand Down Expand Up @@ -1176,7 +1176,7 @@ static void jl_collect_missing_backedges_to_mod(jl_methtable_t *mt)
*edges = jl_alloc_vec_any(0);
// To stay synchronized with the format from MethodInstances (specifically for `invoke`d calls),
// we have to push a pair of values. But in this case the callee is unknown, so we leave it NULL.
push_backedge(*edges, missing_callee, NULL);
push_edge(*edges, missing_callee, NULL);
}
}
}
Expand All @@ -1190,11 +1190,11 @@ static void collect_backedges(jl_method_instance_t *callee) JL_GC_DISABLED
jl_value_t *invokeTypes;
jl_method_instance_t *caller;
while (i < l) {
i = get_next_backedge(backedges, i, &invokeTypes, &caller);
i = get_next_edge(backedges, i, &invokeTypes, &caller);
jl_array_t **edges = (jl_array_t**)ptrhash_bp(&edges_map, caller);
if (*edges == HT_NOTFOUND)
*edges = jl_alloc_vec_any(0);
push_backedge(*edges, invokeTypes, callee);
push_edge(*edges, invokeTypes, callee);
}
}
}
Expand Down Expand Up @@ -1301,7 +1301,7 @@ static void jl_collect_backedges_to(jl_method_instance_t *caller, htable_t *all_
jl_method_instance_t *c;
jl_value_t *invokeTypes;
while (i < l) {
i = get_next_backedge(callees, i, &invokeTypes, &c);
i = get_next_edge(callees, i, &invokeTypes, &c);
register_backedge(all_callees, invokeTypes, (jl_value_t*)c);
if (c && jl_is_method_instance(c)) {
jl_collect_backedges_to((jl_method_instance_t*)c, all_callees);
Expand Down Expand Up @@ -1334,7 +1334,7 @@ static void jl_collect_backedges( /* edges */ jl_array_t *s, /* ext_targets */ j
if (module_in_worklist(caller->def.method->module) || method_instance_in_queue(caller)) {
size_t i = 0, l = jl_array_len(callees);
while (i < l) {
i = get_next_backedge(callees, i, &invokeTypes, &c);
i = get_next_edge(callees, i, &invokeTypes, &c);
register_backedge(&all_callees, invokeTypes, (jl_value_t*)c);
if (jl_is_method_instance(c)) {
jl_collect_backedges_to((jl_method_instance_t*)c, &all_callees);
Expand Down Expand Up @@ -2347,7 +2347,7 @@ static void jl_insert_method_instances(jl_array_t *list) JL_GC_DISABLED
size_t jins = 0, j0, j = 0, nbe = jl_array_len(mi->backedges);
while (j < nbe) {
j0 = j;
j = get_next_backedge(mi->backedges, j, &invokeTypes, &caller);
j = get_next_edge(mi->backedges, j, &invokeTypes, &caller);
if (invokeTypes) {
struct jl_typemap_assoc search = {invokeTypes, world, NULL, 0, ~(size_t)0};
entry = jl_typemap_assoc_by_type(mt->defs, &search, /*offs*/0, /*subtype*/0);
Expand Down Expand Up @@ -2418,33 +2418,33 @@ static void jl_insert_method_instances(jl_array_t *list) JL_GC_DISABLED
milive->backedges = jl_alloc_vec_any(n);
jl_gc_wb(milive, milive->backedges);
while (j < n) {
j = get_next_backedge(mi->backedges, j, &invokeTypes, &be);
j = get_next_edge(mi->backedges, j, &invokeTypes, &be);
belive = (jl_method_instance_t*)ptrhash_get(&uniquing_table, be);
if (belive == HT_NOTFOUND)
belive = be;
jlive = set_next_backedge(milive->backedges, jlive, invokeTypes, belive);
jlive = set_next_edge(milive->backedges, jlive, invokeTypes, belive);
}
} else {
// Copy the missing backedges (this is an O(N^2) algorithm, but many methods have few MethodInstances)
size_t j = 0, k, n = jl_array_len(mi->backedges), nlive = jl_array_len(milive->backedges);
jl_value_t *invokeTypes2;
jl_method_instance_t *belive2;
while (j < n) {
j = get_next_backedge(mi->backedges, j, &invokeTypes, &be);
j = get_next_edge(mi->backedges, j, &invokeTypes, &be);
belive = (jl_method_instance_t*)ptrhash_get(&uniquing_table, be);
if (belive == HT_NOTFOUND)
belive = be;
int found = 0;
k = 0;
while (k < nlive) {
k = get_next_backedge(milive->backedges, k, &invokeTypes2, &belive2);
if (belive == belive2 && invokeTypes == invokeTypes2) {
k = get_next_edge(milive->backedges, k, &invokeTypes2, &belive2);
if (belive == belive2 && jl_egal(invokeTypes, invokeTypes2)) {
found = 1;
break;
}
}
if (!found)
push_backedge(milive->backedges, invokeTypes, belive);
push_edge(milive->backedges, invokeTypes, belive);
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ static void invalidate_method_instance(void (*f)(jl_code_instance_t*), jl_method
size_t i = 0, l = jl_array_len(backedges);
jl_method_instance_t *replaced;
while (i < l) {
i = get_next_backedge(backedges, i, NULL, &replaced);
i = get_next_edge(backedges, i, NULL, &replaced);
invalidate_method_instance(f, replaced, max_world, depth + 1);
}
JL_GC_POP();
Expand All @@ -1474,12 +1474,14 @@ void invalidate_backedges(void (*f)(jl_code_instance_t*), jl_method_instance_t *
if (backedges) {
// invalidate callers (if any)
replaced_mi->backedges = NULL;
JL_GC_PUSH1(&backedges);
size_t i = 0, l = jl_array_len(backedges);
jl_method_instance_t *replaced;
while (i < l) {
i = get_next_backedge(backedges, i, NULL, &replaced);
i = get_next_edge(backedges, i, NULL, &replaced);
invalidate_method_instance(f, replaced, max_world, 1);
}
JL_GC_POP();
}
JL_UNLOCK(&replaced_mi->def.method->writelock);
if (why && _jl_debug_method_invalidation) {
Expand All @@ -1501,15 +1503,15 @@ JL_DLLEXPORT void jl_method_instance_add_backedge(jl_method_instance_t *callee,
// lazy-init the backedges array
callee->backedges = jl_alloc_vec_any(0);
jl_gc_wb(callee, callee->backedges);
push_backedge(callee->backedges, invokesig, caller);
push_edge(callee->backedges, invokesig, caller);
}
else {
size_t i = 0, l = jl_array_len(callee->backedges);
int found = 0;
jl_value_t *invokeTypes;
jl_method_instance_t *mi;
while (i < l) {
i = get_next_backedge(callee->backedges, i, &invokeTypes, &mi);
i = get_next_edge(callee->backedges, i, &invokeTypes, &mi);
// TODO: it would be better to canonicalize (how?) the Tuple-type so
// that we don't have to call `jl_egal`
if (mi == caller && ((invokesig == NULL && invokeTypes == NULL) ||
Expand All @@ -1519,7 +1521,7 @@ JL_DLLEXPORT void jl_method_instance_add_backedge(jl_method_instance_t *callee,
}
}
if (!found) {
push_backedge(callee->backedges, invokesig, caller);
push_edge(callee->backedges, invokesig, caller);
}
}
JL_UNLOCK(&callee->def.method->writelock);
Expand Down
6 changes: 3 additions & 3 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,9 @@ JL_DLLEXPORT jl_code_info_t *jl_new_code_info_uninit(void);
void jl_resolve_globals_in_ir(jl_array_t *stmts, jl_module_t *m, jl_svec_t *sparam_vals,
int binding_effects);

int get_next_backedge(jl_array_t *list, int i, jl_value_t** invokesig, jl_method_instance_t **caller) JL_NOTSAFEPOINT;
int set_next_backedge(jl_array_t *list, int i, jl_value_t *invokesig, jl_method_instance_t *caller);
void push_backedge(jl_array_t *list, jl_value_t *invokesig, jl_method_instance_t *caller);
int get_next_edge(jl_array_t *list, int i, jl_value_t** invokesig, jl_method_instance_t **caller) JL_NOTSAFEPOINT;
int set_next_edge(jl_array_t *list, int i, jl_value_t *invokesig, jl_method_instance_t *caller);
void push_edge(jl_array_t *list, jl_value_t *invokesig, jl_method_instance_t *caller);

JL_DLLEXPORT void jl_add_method_root(jl_method_t *m, jl_module_t *mod, jl_value_t* root);
void jl_append_method_roots(jl_method_t *m, uint64_t modid, jl_array_t* roots);
Expand Down
6 changes: 3 additions & 3 deletions src/method.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ JL_DLLEXPORT jl_method_t *jl_new_method_uninit(jl_module_t *module)
// it will be the signature supplied in an `invoke` call.
// If you don't need `invokesig`, you can set it to NULL on input.
// Initialize iteration with `i = 0`. Returns `i` for the next backedge to be extracted.
int get_next_backedge(jl_array_t *list, int i, jl_value_t** invokesig, jl_method_instance_t **caller) JL_NOTSAFEPOINT
int get_next_edge(jl_array_t *list, int i, jl_value_t** invokesig, jl_method_instance_t **caller) JL_NOTSAFEPOINT
{
jl_value_t *item = jl_array_ptr_ref(list, i);
if (jl_is_method_instance(item)) {
Expand All @@ -811,15 +811,15 @@ int get_next_backedge(jl_array_t *list, int i, jl_value_t** invokesig, jl_method
return i + 2;
}

int set_next_backedge(jl_array_t *list, int i, jl_value_t *invokesig, jl_method_instance_t *caller)
int set_next_edge(jl_array_t *list, int i, jl_value_t *invokesig, jl_method_instance_t *caller)
{
if (invokesig)
jl_array_ptr_set(list, i++, invokesig);
jl_array_ptr_set(list, i++, caller);
return i;
}

void push_backedge(jl_array_t *list, jl_value_t *invokesig, jl_method_instance_t *caller)
void push_edge(jl_array_t *list, jl_value_t *invokesig, jl_method_instance_t *caller)
{
if (invokesig)
jl_array_ptr_1d_push(list, invokesig);
Expand Down

0 comments on commit d1923b0

Please sign in to comment.