Skip to content

Precompile all the CodeInstances #42016

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

Closed
wants to merge 21 commits into from
Closed
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
12 changes: 11 additions & 1 deletion base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# Tracking of newly-inferred MethodInstances during precompilation
const track_newly_inferred = RefValue{Bool}(false)
const newly_inferred = MethodInstance[]

# build (and start inferring) the inference frame for the top-level MethodInstance
function typeinf(interp::AbstractInterpreter, result::InferenceResult, cache::Symbol)
frame = InferenceState(result, cache, interp)
Expand Down Expand Up @@ -336,7 +340,7 @@ function maybe_compress_codeinfo(interp::AbstractInterpreter, linfo::MethodInsta
cache_the_tree = true
end
if cache_the_tree
if may_compress(interp)
if JLOptions().incremental == Int8(0) && may_compress(interp)
nslots = length(ci.slotflags)
resize!(ci.slottypes::Vector{Any}, nslots)
resize!(ci.slotnames, nslots)
Expand Down Expand Up @@ -387,6 +391,12 @@ function cache_result!(interp::AbstractInterpreter, result::InferenceResult)
if !already_inferred
inferred_result = transform_result_for_cache(interp, linfo, valid_worlds, result.src)
code_cache(interp)[linfo] = CodeInstance(result, inferred_result, valid_worlds)
if track_newly_inferred[]
m = linfo.def
if isa(m, Method)
m.module != Core && push!(newly_inferred, linfo)
end
end
end
unlock_mi_inference(interp, linfo)
nothing
Expand Down
3 changes: 3 additions & 0 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1369,13 +1369,16 @@ function include_package_for_output(pkg::PkgId, input::String, depot_path::Vecto
task_local_storage()[:SOURCE_PATH] = source
end

Core.Compiler.track_newly_inferred.x = true
try
Base.include(Base.__toplevel__, input)
catch ex
precompilableerror(ex) || rethrow()
@debug "Aborting `create_expr_cache'" exception=(ErrorException("Declaration of __precompile__(false) not allowed"), catch_backtrace())
exit(125) # we define status = 125 means PrecompileableError
end
Core.Compiler.track_newly_inferred.x = false
ccall(:jl_set_newly_inferred, Cvoid, (Any,), Core.Compiler.newly_inferred)
end

const PRECOMPILE_TRACE_COMPILE = Ref{String}()
Expand Down
20 changes: 17 additions & 3 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7415,16 +7415,22 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
}
}

// if we created any new roots during codegen,
// copy ctx.roots into m->roots
// if we created any new roots during codegen
if (ctx.roots) {
jl_method_t *m = lam->def.method;
bool added = false;
bool external = jl_precompile_toplevel_module != NULL && jl_parent_module(m->module) != jl_precompile_toplevel_module;
int rootslen;
JL_LOCK(&m->writelock);
if (m->roots == NULL) {
rootslen = 0;
m->roots = ctx.roots;
jl_gc_wb(m, m->roots);
added = true;
}
else {
rootslen = jl_array_len(m->roots);
size_t i, ilen = jl_array_dim0(ctx.roots);
size_t j, jlen = jl_array_dim0(m->roots);
for (i = 0; i < ilen; i++) {
Expand All @@ -7434,11 +7440,19 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
if (ival == jval)
break;
}
if (j == jlen) // not found - add to array
if (j == jlen) { // not found - add to array
jl_array_ptr_1d_push(m->roots, ival);
added = true;
}
}
}
ctx.roots = NULL;
if (added & external) {
uint64_t key = jl_precompile_toplevel_module->build_id;
if (jl_current_block_key(m) != key) {
jl_add_root_block(m, key, rootslen);
}
}
JL_UNLOCK(&m->writelock);
}

Expand Down Expand Up @@ -7568,7 +7582,7 @@ jl_compile_result_t jl_emit_codeinst(
jl_options.debug_level > 1) {
// update the stored code
if (codeinst->inferred != (jl_value_t*)src) {
if (jl_is_method(def))
if (jl_is_method(def) && !jl_options.incremental)
src = (jl_code_info_t*)jl_compress_ir(def, src);
codeinst->inferred = (jl_value_t*)src;
jl_gc_wb(codeinst, src);
Expand Down
1 change: 0 additions & 1 deletion src/common_symbols1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,3 @@ jl_symbol("undef"),
jl_symbol("sizeof"),
jl_symbol("String"),
jl_symbol("namedtuple.jl"),
jl_symbol("pop"),
2 changes: 1 addition & 1 deletion src/common_symbols2.inc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
jl_symbol("pop"),
jl_symbol("inbounds"),
jl_symbol("strings/string.jl"),
jl_symbol("Ref"),
Expand Down Expand Up @@ -251,4 +252,3 @@ jl_symbol("GitError"),
jl_symbol("zeros"),
jl_symbol("InexactError"),
jl_symbol("LogLevel"),
jl_symbol("between"),
Loading