Skip to content

Commit

Permalink
rm CodeInstance owner
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed May 7, 2024
1 parent 9a57859 commit b58ed0d
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 83 deletions.
6 changes: 3 additions & 3 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,13 @@ end


function CodeInstance(
mi::MethodInstance, owner, @nospecialize(rettype), @nospecialize(exctype), @nospecialize(inferred_const),
mi::MethodInstance, @nospecialize(rettype), @nospecialize(exctype), @nospecialize(inferred_const),
@nospecialize(inferred), const_flags::Int32, min_world::UInt, max_world::UInt,
ipo_effects::UInt32, effects::UInt32, @nospecialize(analysis_results),
relocatability::UInt8, edges::DebugInfo)
return ccall(:jl_new_codeinst, Ref{CodeInstance},
(Any, Any, Any, Any, Any, Any, Int32, UInt, UInt, UInt32, UInt32, Any, UInt8, Any),
mi, owner, rettype, exctype, inferred_const, inferred, const_flags, min_world, max_world,
(Any, Any, Any, Any, Any, Int32, UInt, UInt, UInt32, UInt32, Any, UInt8, Any),
mi, rettype, exctype, inferred_const, inferred, const_flags, min_world, max_world,
ipo_effects, effects, analysis_results, relocatability, edges)
end
GlobalRef(m::Module, s::Symbol) = ccall(:jl_module_globalref, Ref{GlobalRef}, (Any, Any), m, s)
Expand Down
8 changes: 3 additions & 5 deletions base/compiler/cicache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ that have been created for the given method instance, stratified by world age
ranges. This struct abstracts over access to this cache.
"""
struct InternalCodeCache
owner::Any # `jl_egal` is used for comparison
end

function setindex!(cache::InternalCodeCache, ci::CodeInstance, mi::MethodInstance)
@assert ci.owner === cache.owner
ccall(:jl_mi_cache_insert, Cvoid, (Any, Any), mi, ci)
return cache
end
Expand Down Expand Up @@ -50,11 +48,11 @@ WorldView(wvc::WorldView, wr::WorldRange) = WorldView(wvc.cache, wr)
WorldView(wvc::WorldView, args...) = WorldView(wvc.cache, args...)

function haskey(wvc::WorldView{InternalCodeCache}, mi::MethodInstance)
return ccall(:jl_rettype_inferred, Any, (Any, Any, UInt, UInt), wvc.cache.owner, mi, first(wvc.worlds), last(wvc.worlds)) !== nothing
return ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, first(wvc.worlds), last(wvc.worlds)) !== nothing
end

function get(wvc::WorldView{InternalCodeCache}, mi::MethodInstance, default)
r = ccall(:jl_rettype_inferred, Any, (Any, Any, UInt, UInt), wvc.cache.owner, mi, first(wvc.worlds), last(wvc.worlds))
r = ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, first(wvc.worlds), last(wvc.worlds))
if r === nothing
return default
end
Expand All @@ -73,7 +71,7 @@ function setindex!(wvc::WorldView{InternalCodeCache}, ci::CodeInstance, mi::Meth
end

function code_cache(interp::AbstractInterpreter)
cache = InternalCodeCache(cache_owner(interp))
cache = InternalCodeCache()
worlds = WorldRange(get_inference_world(interp))
return WorldView(cache, worlds)
end
6 changes: 3 additions & 3 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function CodeInstance(interp::AbstractInterpreter, result::InferenceResult;
if !@isdefined edges
edges = DebugInfo(result.linfo)
end
return CodeInstance(result.linfo, owner,
return CodeInstance(result.linfo,
widenconst(result_type), widenconst(result.exc_result), rettype_const, inferred_result,
const_flags, first(result.valid_worlds), last(result.valid_worlds),
# TODO: Actually do something with non-IPO effects
Expand Down Expand Up @@ -935,7 +935,7 @@ more details.
"""
function codeinstance_for_const_with_code(interp::AbstractInterpreter, code::CodeInstance)
src = codeinfo_for_const(interp, code.def, code.rettype_const)
return CodeInstance(code.def, cache_owner(interp), code.rettype, code.exctype, code.rettype_const, src,
return CodeInstance(code.def, code.rettype, code.exctype, code.rettype_const, src,
Int32(0x3), code.min_world, code.max_world,
code.ipo_purity_bits, code.purity_bits, code.analysis_results,
code.relocatability, src.debuginfo)
Expand Down Expand Up @@ -1109,7 +1109,7 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance, source_mod
if ccall(:jl_get_module_infer, Cint, (Any,), def.module) == 0 && !generating_output(#=incremental=#false)
src = retrieve_code_info(mi, get_inference_world(interp))
src isa CodeInfo || return nothing
return CodeInstance(mi, cache_owner(interp), Any, Any, nothing, src, Int32(0),
return CodeInstance(mi, Any, Any, nothing, src, Int32(0),
get_inference_world(interp), get_inference_world(interp),
UInt32(0), UInt32(0), nothing, UInt8(0), src.debuginfo)
end
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function get_staged(mi::MethodInstance, world::UInt)
end

function get_cached_uninferred(mi::MethodInstance, world::UInt)
ccall(:jl_cached_uninferred, Any, (Any, UInt), mi.cache, world)::CodeInstance
ccall(:jl_cached_uninferred, Any, (Any, UInt), mi, world)::CodeInstance
end

function retrieve_code_info(mi::MethodInstance, world::UInt)
Expand Down
1 change: 0 additions & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,6 @@ jl_fptr_args_t jl_get_builtin_fptr(jl_datatype_t *dt)
jl_typemap_entry_t *entry = (jl_typemap_entry_t*)jl_atomic_load_relaxed(&dt->name->mt->defs);
jl_method_instance_t *mi = jl_atomic_load_relaxed(&entry->func.method->unspecialized);
jl_code_instance_t *ci = jl_atomic_load_relaxed(&mi->cache);
assert(ci->owner == jl_nothing);
return jl_atomic_load_relaxed(&ci->specptr.fptr1);
}

Expand Down
33 changes: 12 additions & 21 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ jl_datatype_t *jl_mk_builtin_func(jl_datatype_t *dt, const char *name, jl_fptr_a
jl_atomic_store_relaxed(&m->unspecialized, mi);
jl_gc_wb(m, mi);

jl_code_instance_t *codeinst = jl_new_codeinst(mi, jl_nothing,
jl_code_instance_t *codeinst = jl_new_codeinst(mi,
(jl_value_t*)jl_any_type, (jl_value_t*)jl_any_type, jl_nothing, jl_nothing,
0, 1, ~(size_t)0, 0, 0, jl_nothing, 0, NULL);
jl_mi_cache_insert(mi, codeinst);
Expand Down Expand Up @@ -444,12 +444,10 @@ JL_DLLEXPORT jl_code_instance_t *jl_get_method_inferred(
jl_method_instance_t *mi JL_PROPAGATES_ROOT, jl_value_t *rettype,
size_t min_world, size_t max_world, jl_debuginfo_t *edges)
{
jl_value_t *owner = jl_nothing; // TODO: owner should be arg
jl_code_instance_t *codeinst = jl_atomic_load_relaxed(&mi->cache);
while (codeinst) {
if (jl_atomic_load_relaxed(&codeinst->min_world) == min_world &&
jl_atomic_load_relaxed(&codeinst->max_world) == max_world &&
jl_egal(codeinst->owner, owner) &&
jl_egal(codeinst->rettype, rettype)) {
if (edges == NULL)
return codeinst;
Expand All @@ -464,7 +462,7 @@ JL_DLLEXPORT jl_code_instance_t *jl_get_method_inferred(
codeinst = jl_atomic_load_relaxed(&codeinst->next);
}
codeinst = jl_new_codeinst(
mi, owner, rettype, (jl_value_t*)jl_any_type, NULL, NULL,
mi, rettype, (jl_value_t*)jl_any_type, NULL, NULL,
0, min_world, max_world, 0, 0, jl_nothing, 0, edges);
jl_mi_cache_insert(mi, codeinst);
return codeinst;
Expand All @@ -483,7 +481,7 @@ JL_DLLEXPORT int jl_mi_cache_has_ci(jl_method_instance_t *mi,
}

JL_DLLEXPORT jl_code_instance_t *jl_new_codeinst(
jl_method_instance_t *mi, jl_value_t *owner,
jl_method_instance_t *mi,
jl_value_t *rettype, jl_value_t *exctype,
jl_value_t *inferred_const, jl_value_t *inferred,
int32_t const_flags, size_t min_world, size_t max_world,
Expand All @@ -496,7 +494,6 @@ JL_DLLEXPORT jl_code_instance_t *jl_new_codeinst(
jl_code_instance_t *codeinst = (jl_code_instance_t*)jl_gc_alloc(ct->ptls, sizeof(jl_code_instance_t),
jl_code_instance_type);
codeinst->def = mi;
codeinst->owner = owner;
jl_atomic_store_relaxed(&codeinst->min_world, min_world);
jl_atomic_store_relaxed(&codeinst->max_world, max_world);
codeinst->rettype = rettype;
Expand Down Expand Up @@ -2353,13 +2350,12 @@ jl_method_instance_t *jl_get_unspecialized(jl_method_t *def JL_PROPAGATES_ROOT)
return unspec;
}

STATIC_INLINE jl_value_t *_jl_rettype_inferred(jl_value_t *owner, jl_method_instance_t *mi, size_t min_world, size_t max_world) JL_NOTSAFEPOINT
STATIC_INLINE jl_value_t *_jl_rettype_inferred(jl_method_instance_t *mi, size_t min_world, size_t max_world) JL_NOTSAFEPOINT
{
jl_code_instance_t *codeinst = jl_atomic_load_relaxed(&mi->cache);
while (codeinst) {
if (jl_atomic_load_relaxed(&codeinst->min_world) <= min_world &&
max_world <= jl_atomic_load_relaxed(&codeinst->max_world) &&
jl_egal(codeinst->owner, owner)) {
max_world <= jl_atomic_load_relaxed(&codeinst->max_world)) {

jl_value_t *code = jl_atomic_load_relaxed(&codeinst->inferred);
if (code)
Expand All @@ -2370,14 +2366,14 @@ STATIC_INLINE jl_value_t *_jl_rettype_inferred(jl_value_t *owner, jl_method_inst
return (jl_value_t*)jl_nothing;
}

JL_DLLEXPORT jl_value_t *jl_rettype_inferred(jl_value_t *owner, jl_method_instance_t *mi, size_t min_world, size_t max_world) JL_NOTSAFEPOINT
JL_DLLEXPORT jl_value_t *jl_rettype_inferred(jl_method_instance_t *mi, size_t min_world, size_t max_world) JL_NOTSAFEPOINT
{
return (jl_value_t*)_jl_rettype_inferred(owner, mi, min_world, max_world);
return (jl_value_t*)_jl_rettype_inferred(mi, min_world, max_world);
}

JL_DLLEXPORT jl_value_t *jl_rettype_inferred_native(jl_method_instance_t *mi, size_t min_world, size_t max_world) JL_NOTSAFEPOINT
{
return (jl_value_t*)_jl_rettype_inferred(jl_nothing, mi, min_world, max_world);
return (jl_value_t*)_jl_rettype_inferred(mi, min_world, max_world);
}

JL_DLLEXPORT jl_value_t *(*const jl_rettype_inferred_addr)(jl_method_instance_t *mi, size_t min_world, size_t max_world) JL_NOTSAFEPOINT = jl_rettype_inferred_native;
Expand All @@ -2386,8 +2382,6 @@ jl_code_instance_t *jl_method_compiled(jl_method_instance_t *mi, size_t world)
{
jl_code_instance_t *codeinst = jl_atomic_load_relaxed(&mi->cache);
for (; codeinst; codeinst = jl_atomic_load_relaxed(&codeinst->next)) {
if (codeinst->owner != jl_nothing)
continue;
if (jl_atomic_load_relaxed(&codeinst->min_world) <= world && world <= jl_atomic_load_relaxed(&codeinst->max_world)) {
if (jl_atomic_load_relaxed(&codeinst->invoke) != NULL)
return codeinst;
Expand All @@ -2401,9 +2395,6 @@ jl_code_instance_t *jl_method_inferred_with_abi(jl_method_instance_t *mi JL_PROP
{
jl_code_instance_t *codeinst = jl_atomic_load_relaxed(&mi->cache);
for (; codeinst; codeinst = jl_atomic_load_relaxed(&codeinst->next)) {
if (codeinst->owner != jl_nothing)
continue;

if (jl_atomic_load_relaxed(&codeinst->min_world) <= world && world <= jl_atomic_load_relaxed(&codeinst->max_world)) {
jl_value_t *code = jl_atomic_load_relaxed(&codeinst->inferred);
if (code && (code != jl_nothing || (jl_atomic_load_relaxed(&codeinst->invoke) != NULL)))
Expand Down Expand Up @@ -2521,7 +2512,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
jl_code_instance_t *unspec = jl_atomic_load_relaxed(&unspecmi->cache);
jl_callptr_t unspec_invoke = NULL;
if (unspec && (unspec_invoke = jl_atomic_load_acquire(&unspec->invoke))) {
jl_code_instance_t *codeinst = jl_new_codeinst(mi, jl_nothing,
jl_code_instance_t *codeinst = jl_new_codeinst(mi,
(jl_value_t*)jl_any_type, (jl_value_t*)jl_any_type, NULL, NULL,
0, 1, ~(size_t)0, 0, 0, jl_nothing, 0, NULL);
void *unspec_fptr = jl_atomic_load_relaxed(&unspec->specptr.fptr);
Expand All @@ -2548,7 +2539,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
compile_option == JL_OPTIONS_COMPILE_MIN) {
jl_code_info_t *src = jl_code_for_interpreter(mi, world);
if (!jl_code_requires_compiler(src, 0)) {
jl_code_instance_t *codeinst = jl_new_codeinst(mi, jl_nothing,
jl_code_instance_t *codeinst = jl_new_codeinst(mi,
(jl_value_t*)jl_any_type, (jl_value_t*)jl_any_type, NULL, NULL,
0, 1, ~(size_t)0, 0, 0, jl_nothing, 0, NULL);
jl_atomic_store_release(&codeinst->invoke, jl_fptr_interpret_call);
Expand Down Expand Up @@ -2610,7 +2601,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
jl_callptr_t ucache_invoke = jl_atomic_load_acquire(&ucache->invoke);
if (ucache_invoke == NULL) {
if ((!jl_is_method(def) || def->source == jl_nothing) &&
!jl_cached_uninferred(jl_atomic_load_relaxed(&ucache->def->cache), world)) {
!jl_cached_uninferred(ucache->def, world)) {
jl_throw(jl_new_struct(jl_missingcodeerror_type, (jl_value_t*)mi));
}
jl_generate_fptr_for_unspecialized(ucache);
Expand All @@ -2623,7 +2614,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t
jl_typeinf_timing_end(start, is_recompile);
return ucache;
}
codeinst = jl_new_codeinst(mi, jl_nothing,
codeinst = jl_new_codeinst(mi,
(jl_value_t*)jl_any_type, (jl_value_t*)jl_any_type, NULL, NULL,
0, 1, ~(size_t)0, 0, 0, jl_nothing, 0, NULL);
void *unspec_fptr = jl_atomic_load_relaxed(&ucache->specptr.fptr);
Expand Down
5 changes: 2 additions & 3 deletions src/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,7 @@ jl_value_t *jl_code_or_ci_for_interpreter(jl_method_instance_t *mi, size_t world
ret = (jl_value_t*)src;
}
else {
jl_code_instance_t *cache = jl_atomic_load_relaxed(&mi->cache);
jl_code_instance_t *uninferred = jl_cached_uninferred(cache, world);
jl_code_instance_t *uninferred = jl_cached_uninferred(mi, world);
if (!uninferred) {
assert(mi->def.method->generator);
src = jl_code_for_staged(mi, world, &uninferred);
Expand All @@ -715,7 +714,7 @@ jl_value_t *jl_code_or_ci_for_interpreter(jl_method_instance_t *mi, size_t world
}
}
else {
jl_code_instance_t *uninferred = jl_cached_uninferred(jl_atomic_load_relaxed(&mi->cache), world);
jl_code_instance_t *uninferred = jl_cached_uninferred(mi, world);
ret = (jl_value_t*)uninferred;
if (ret) {
src = (jl_code_info_t*)jl_atomic_load_relaxed(&uninferred->inferred);
Expand Down
3 changes: 1 addition & 2 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ void jl_generate_fptr_for_unspecialized_impl(jl_code_instance_t *unspec)
}
else {
jl_method_instance_t *mi = unspec->def;
jl_code_instance_t *uninferred = jl_cached_uninferred(
jl_atomic_load_relaxed(&mi->cache), 1);
jl_code_instance_t *uninferred = jl_cached_uninferred(mi, 1);
assert(uninferred);
src = (jl_code_info_t*)jl_atomic_load_relaxed(&uninferred->inferred);
assert(src);
Expand Down
1 change: 1 addition & 0 deletions src/jl_exported_data.inc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
XX(jl_method_instance_type) \
XX(jl_method_specialization_type) \
XX(jl_default_spec_type) \
XX(jl_method_uninferred_spec_type) \
XX(jl_method_match_type) \
XX(jl_method_type) \
XX(jl_methtable_type) \
Expand Down
28 changes: 19 additions & 9 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3297,6 +3297,13 @@ void jl_init_types(void) JL_GC_DISABLED
jl_bool_type),
jl_emptysvec, 0, 0, 4);

jl_datatype_t *uninferred_spec_type =
jl_new_datatype(jl_symbol("UninferredSpec"), core,
jl_any_type, jl_emptysvec,
jl_emptysvec,
jl_emptysvec,
jl_emptysvec, 0, 0, 0);

tv = jl_svec1(tvar("D"));
jl_datatype_t *jl_meth_spec_type =
jl_new_datatype(jl_symbol("MethodSpecialization"), core,
Expand All @@ -3309,7 +3316,7 @@ void jl_init_types(void) JL_GC_DISABLED
"next",
"data"),
jl_svec(6,
jl_new_struct(jl_uniontype_type, jl_method_type, jl_module_type),
jl_any_type, // Union{Method, Module, MethodSpecialization}
jl_any_type,
jl_array_any_type,
jl_any_type,/*jl_code_instance_type*/
Expand All @@ -3329,9 +3336,8 @@ void jl_init_types(void) JL_GC_DISABLED
jl_code_instance_type =
jl_new_datatype(jl_symbol("CodeInstance"), core,
jl_any_type, jl_emptysvec,
jl_perm_symsvec(18,
jl_perm_symsvec(17,
"def",
"owner",
"next",
"min_world",
"max_world",
Expand All @@ -3345,10 +3351,9 @@ void jl_init_types(void) JL_GC_DISABLED
"analysis_results",
"specsigflags", "precompile", "relocatability",
"invoke", "specptr"), // function object decls
jl_svec(18,
jl_svec(17,
jl_method_specialization_type,
jl_any_type,
jl_any_type,
jl_ulong_type,
jl_ulong_type,
jl_any_type,
Expand All @@ -3365,9 +3370,9 @@ void jl_init_types(void) JL_GC_DISABLED
jl_any_type, jl_any_type), // fptrs
jl_emptysvec,
0, 1, 1);
jl_svecset(jl_code_instance_type->types, 2, jl_code_instance_type);
const static uint32_t code_instance_constfields[1] = { 0b000001010011100011 }; // Set fields 1, 2, 6-8, 11, 13 as const
const static uint32_t code_instance_atomicfields[1] = { 0b110110101100011100 }; // Set fields 3-5, 9, 10, 12, 14-15, 17-18 as atomic
jl_svecset(jl_code_instance_type->types, 1, jl_code_instance_type);
const static uint32_t code_instance_constfields[1] = { 0b00000101001110001 }; // Set fields 1, 2, 6-8, 11, 13 as const
const static uint32_t code_instance_atomicfields[1] = { 0b11011010110001110 }; // Set fields 3-5, 9, 10, 12, 14-15, 17-18 as atomic
//Fields 4-5 are only operated on by construction and deserialization, so are const at runtime
//Fields 13 and 17 must be protected by locks, and thus all operations on jl_code_instance_t are threadsafe
//Except for field 9 (inferred), which is volatile unless you know which other places are currently using it
Expand Down Expand Up @@ -3509,10 +3514,13 @@ void jl_init_types(void) JL_GC_DISABLED
jl_svecset(jl_methtable_type->types, 9, jl_uint8_type);
jl_svecset(jl_methtable_type->types, 10, jl_uint8_type);
//jl_svecset(jl_debuginfo_type->types, 0, jl_method_instance_type); // union(jl_method_instance_type, jl_method_type, jl_symbol_type)
jl_svecset(jl_meth_spec_type->types, 0,
jl_new_struct(jl_uniontype_type, jl_meth_spec_type,
jl_new_struct(jl_uniontype_type, jl_method_type, jl_module_type)));
jl_svecset(jl_meth_spec_type->types, 3, jl_code_instance_type);
jl_svecset(jl_meth_spec_type->types, 4, jl_method_specialization_type);
jl_svecset(jl_code_instance_type->types, 15, jl_voidpointer_type);
jl_svecset(jl_code_instance_type->types, 16, jl_voidpointer_type);
jl_svecset(jl_code_instance_type->types, 17, jl_voidpointer_type);
jl_svecset(jl_binding_type->types, 1, jl_globalref_type);
jl_svecset(jl_binding_type->types, 2, jl_binding_type);

Expand All @@ -3535,6 +3543,8 @@ void jl_init_types(void) JL_GC_DISABLED
jl_method_instance_type = (jl_datatype_t*)jl_apply_type1((jl_value_t*)jl_method_specialization_type, (jl_value_t*)jl_default_spec_type);
jl_svecset(jl_method_type->types, 13, jl_method_instance_type);

jl_method_uninferred_spec_type = (jl_datatype_t*)jl_apply_type1((jl_value_t*)jl_method_specialization_type, (jl_value_t*)uninferred_spec_type);

jl_compute_field_offsets(jl_simplevector_type);
jl_compute_field_offsets(jl_default_spec_type);
jl_compute_field_offsets(jl_method_instance_type);
Expand Down
Loading

0 comments on commit b58ed0d

Please sign in to comment.