Skip to content
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

Fix package precompilation (PrecompileTools) #57828

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,9 @@ JL_DLLEXPORT void jl_force_trace_compile_timing_disable(void);
JL_DLLEXPORT void jl_force_trace_dispatch_enable(void);
JL_DLLEXPORT void jl_force_trace_dispatch_disable(void);

JL_DLLEXPORT void jl_tag_newly_inferred_enable(void);
JL_DLLEXPORT void jl_tag_newly_inferred_disable(void);

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);

Expand Down
23 changes: 23 additions & 0 deletions src/staticdata_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ static jl_array_t *newly_inferred JL_GLOBALLY_ROOTED /*FIXME*/;
// Mutex for newly_inferred
jl_mutex_t newly_inferred_mutex;
extern jl_mutex_t world_counter_lock;
static _Atomic(uint8_t) jl_tag_newly_inferred_enabled = 0;

/**
* @brief Enable tagging of all newly inferred CodeInstances.
*/
JL_DLLEXPORT void jl_tag_newly_inferred_enable(void)
{
jl_atomic_fetch_add(&jl_tag_newly_inferred_enabled, 1); // FIXME overflow?
}
/**
* @brief Disable tagging of all newly inferred CodeInstances.
*/
JL_DLLEXPORT void jl_tag_newly_inferred_disable(void)
{
jl_atomic_fetch_add(&jl_tag_newly_inferred_enabled, -1); // FIXME underflow?
}


// Register array of newly-inferred MethodInstances
// This gets called as the first step of Base.include_package_for_output
Expand All @@ -101,6 +118,12 @@ JL_DLLEXPORT void jl_push_newly_inferred(jl_value_t* ci)
{
if (!newly_inferred)
return;
uint8_t tag_newly_inferred = jl_atomic_load_relaxed(&jl_tag_newly_inferred_enabled);
if (tag_newly_inferred) {
jl_method_instance_t *mi = jl_get_ci_mi((jl_code_instance_t*)ci);
uint8_t miflags = jl_atomic_load_relaxed(&mi->flags);
jl_atomic_store_relaxed(&mi->flags, miflags | JL_MI_FLAGS_MASK_PRECOMPILED);
}
JL_LOCK(&newly_inferred_mutex);
size_t end = jl_array_nrows(newly_inferred);
jl_array_grow_end(newly_inferred, 1);
Expand Down
40 changes: 39 additions & 1 deletion test/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,6 @@ end

# method root provenance & external code caching
precompile_test_harness("code caching") do dir
Bid = rootid(Base)
Cache_module = :Cacheb8321416e8a3e2f1
# Note: calling setindex!(::Dict{K,V}, ::Any, ::K) adds both compression and codegen roots
write(joinpath(dir, "$Cache_module.jl"),
Expand Down Expand Up @@ -1068,6 +1067,45 @@ precompile_test_harness("code caching") do dir
end
end

precompile_test_harness("precompiletools") do dir
PrecompileToolsModule = :PCTb8321416e8a3e2f1
write(joinpath(dir, "$PrecompileToolsModule.jl"),
"""
module $PrecompileToolsModule
struct MyType
x::Int
end

function call_findfirst(x, list)
# call a method defined in Base by runtime dispatch
return findfirst(==(Base.inferencebarrier(x)), Base.inferencebarrier(list))
end

let
ccall(:jl_tag_newly_inferred_enable, Cvoid, ())
call_findfirst(MyType(2), [MyType(1), MyType(2), MyType(3)])
ccall(:jl_tag_newly_inferred_disable, Cvoid, ())
end
end
"""
)
pkgid = Base.PkgId(string(PrecompileToolsModule))
@test !Base.isprecompiled(pkgid)
Base.compilecache(pkgid)
@test Base.isprecompiled(pkgid)
@eval using $PrecompileToolsModule
M = invokelatest(getfield, @__MODULE__, PrecompileToolsModule)
invokelatest() do
m = which(Tuple{typeof(findfirst), Base.Fix2{typeof(==), T}, Vector{T}} where T)
success = 0
for mi in Base.specializations(m)
sig = Base.unwrap_unionall(mi.specTypes)
success += sig.parameters[3] === Vector{M.MyType}
end
@test success == 1
end
end

precompile_test_harness("invoke") do dir
InvokeModule = :Invoke0x030e7e97c2365aad
CallerModule = :Caller0x030e7e97c2365aad
Expand Down