-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic code for binding partition revalidation #56649
Draft
Keno
wants to merge
1
commit into
master
Choose a base branch
from
kf/bpartrevalidation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−2
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# This file is a part of Julia. License is MIT: https://julialang.org/license | ||
|
||
struct GlobalRefIterator | ||
mod::Module | ||
end | ||
IteratorSize(::Type{GlobalRefIterator}) = SizeUnknown() | ||
globalrefs(mod::Module) = GlobalRefIterator(mod) | ||
|
||
function iterate(gri::GlobalRefIterator, i = 1) | ||
m = gri.mod | ||
table = ccall(:jl_module_get_bindings, Ref{SimpleVector}, (Any,), m) | ||
i == length(table) && return nothing | ||
b = table[i] | ||
b === nothing && return iterate(gri, i+1) | ||
return ((b::Core.Binding).globalref, i+1) | ||
end | ||
|
||
const TYPE_TYPE_MT = Type.body.name.mt | ||
const NONFUNCTION_MT = Core.MethodTable.name.mt | ||
function foreach_module_mtable(visit, m::Module, world::UInt) | ||
for gb in globalrefs(m) | ||
binding = gb.binding | ||
bpart = lookup_binding_partition(world, binding) | ||
if is_some_const_binding(binding_kind(bpart)) | ||
isdefined(bpart, :restriction) || continue | ||
v = partition_restriction(bpart) | ||
uw = unwrap_unionall(v) | ||
name = gb.name | ||
if isa(uw, DataType) | ||
tn = uw.name | ||
if tn.module === m && tn.name === name && tn.wrapper === v && isdefined(tn, :mt) | ||
# this is the original/primary binding for the type (name/wrapper) | ||
mt = tn.mt | ||
if mt !== nothing && mt !== TYPE_TYPE_MT && mt !== NONFUNCTION_MT | ||
@assert mt.module === m | ||
visit(mt) || return false | ||
end | ||
end | ||
elseif isa(v, Module) && v !== m && parentmodule(v) === m && _nameof(v) === name | ||
# this is the original/primary binding for the submodule | ||
foreach_module_mtable(visit, v, world) || return false | ||
elseif isa(v, Core.MethodTable) && v.module === m && v.name === name | ||
# this is probably an external method table here, so let's | ||
# assume so as there is no way to precisely distinguish them | ||
visit(v) || return false | ||
end | ||
end | ||
end | ||
return true | ||
end | ||
|
||
function foreach_reachable_mtable(visit, world::UInt) | ||
visit(TYPE_TYPE_MT) || return | ||
visit(NONFUNCTION_MT) || return | ||
for mod in loaded_modules_array() | ||
foreach_module_mtable(visit, mod, world) | ||
end | ||
end | ||
|
||
function should_invalidate_code_for_globalref(gr::GlobalRef, src::CodeInfo) | ||
found_any = false | ||
labelchangemap = nothing | ||
stmts = src.code | ||
isgr(g::GlobalRef) = gr.mod == g.mod && gr.name === g.name | ||
isgr(g) = false | ||
for i = 1:length(stmts) | ||
stmt = stmts[i] | ||
if isgr(stmt) | ||
found_any = true | ||
continue | ||
end | ||
for ur in Compiler.userefs(stmt) | ||
arg = ur[] | ||
# If any of the GlobalRefs in this stmt match the one that | ||
# we are about, we need to move out all GlobalRefs to preserve | ||
# effect order, in case we later invalidate a different GR | ||
if isa(arg, GlobalRef) | ||
if isgr(arg) | ||
@assert !isa(stmt, PhiNode) | ||
found_any = true | ||
break | ||
end | ||
end | ||
end | ||
end | ||
return found_any | ||
end | ||
|
||
function invalidate_code_for_globalref!(gr::GlobalRef, new_max_world::UInt) | ||
valid_in_valuepos = false | ||
foreach_reachable_mtable(new_max_world) do mt::Core.MethodTable | ||
for method in MethodList(mt) | ||
if isdefined(method, :source) | ||
src = _uncompressed_ir(method) | ||
old_stmts = src.code | ||
if should_invalidate_code_for_globalref(gr, src) | ||
for mi in specializations(method) | ||
ci = mi.cache | ||
while true | ||
if ci.max_world > new_max_world | ||
ccall(:jl_invalidate_code_instance, Cvoid, (Any, UInt), ci, new_max_world) | ||
end | ||
isdefined(ci, :next) || break | ||
ci = ci.next | ||
end | ||
end | ||
end | ||
end | ||
end | ||
return true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -1025,6 +1025,21 @@ JL_DLLEXPORT void jl_set_const(jl_module_t *m JL_ROOTING_ARGUMENT, jl_sym_t *var | |||
jl_gc_wb(bpart, val); | ||||
} | ||||
|
||||
void jl_invalidate_binding_refs(jl_globalref_t *ref, size_t new_world) | ||||
{ | ||||
static jl_value_t *invalidate_code_for_globalref = NULL; | ||||
if (invalidate_code_for_globalref == NULL && jl_base_module != NULL) | ||||
invalidate_code_for_globalref = jl_get_global(jl_base_module, jl_symbol("invalidate_code_for_globalref!")); | ||||
if (!invalidate_code_for_globalref) | ||||
jl_error("Binding invalidation is not permitted during bootstrap."); | ||||
if (jl_generating_output()) | ||||
jl_error("Binding invalidation is not permitted during image generation."); | ||||
jl_value_t *boxed_world = jl_box_ulong(new_world); | ||||
JL_GC_PUSH1(&boxed_world); | ||||
jl_call2((jl_function_t*)invalidate_code_for_globalref, (jl_value_t*)ref, boxed_world); | ||||
JL_GC_POP(); | ||||
} | ||||
|
||||
extern jl_mutex_t world_counter_lock; | ||||
JL_DLLEXPORT void jl_disable_binding(jl_globalref_t *gr) | ||||
{ | ||||
|
@@ -1039,9 +1054,16 @@ JL_DLLEXPORT void jl_disable_binding(jl_globalref_t *gr) | |||
|
||||
JL_LOCK(&world_counter_lock); | ||||
jl_task_t *ct = jl_current_task; | ||||
size_t last_world = ct->world_age; | ||||
size_t new_max_world = jl_atomic_load_acquire(&jl_world_counter); | ||||
// TODO: Trigger invalidation here | ||||
(void)ct; | ||||
JL_TRY { | ||||
ct->world_age = jl_typeinf_world; | ||||
jl_invalidate_binding_refs(gr, new_max_world); | ||||
} JL_CATCH { | ||||
JL_UNLOCK(&world_counter_lock); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are forbidden from unlocking a lock that you didn't acquire in the same scope
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the try/catch will automatically unlock it? |
||||
jl_rethrow(); | ||||
} | ||||
ct->world_age = last_world; | ||||
jl_atomic_store_release(&bpart->max_world, new_max_world); | ||||
jl_atomic_store_release(&jl_world_counter, new_max_world + 1); | ||||
JL_UNLOCK(&world_counter_lock); | ||||
|
@@ -1327,6 +1349,11 @@ JL_DLLEXPORT void jl_add_to_module_init_list(jl_value_t *mod) | |||
jl_array_ptr_1d_push(jl_module_init_order, mod); | ||||
} | ||||
|
||||
JL_DLLEXPORT jl_svec_t *jl_module_get_bindings(jl_module_t *m) | ||||
{ | ||||
return jl_atomic_load_relaxed(&m->bindings); | ||||
} | ||||
|
||||
JL_DLLEXPORT void jl_init_restored_module(jl_value_t *mod) | ||||
{ | ||||
if (!jl_generating_output() || jl_options.incremental) { | ||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to CI, apparently this function is unsound currently? (may return some invalid reference, possibly a NULL)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, there's a bit of an awkward state where you have a something declared constant but the value is undefined. Originally that was what the
isdefined
check on the line before this was for, but on current master, it doesn't work, becauserestriction
is the magic pointer-value union. I'm planning to fix it up by making that case its ownbinding_kind
that's considered a guard kind, not a constant kind, so that constant kinds always have values.