Skip to content

Commit

Permalink
Remove the imaging mode global
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi committed Apr 4, 2022
1 parent f8f83da commit ea197ce
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 41 deletions.
17 changes: 8 additions & 9 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,11 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
if (cgparams == NULL)
cgparams = &jl_default_cgparams;
jl_native_code_desc_t *data = new jl_native_code_desc_t;
CompilationPolicy policy = (CompilationPolicy) _policy;
bool imaging = imaging_default() || policy == CompilationPolicy::ImagingMode;
orc::ThreadSafeModule backing;
if (!llvmmod) {
backing = jl_create_llvm_module("text", jl_ExecutionEngine->getContext());
backing = jl_create_llvm_module("text", jl_ExecutionEngine->getContext(), imaging);
}
orc::ThreadSafeModule &clone = llvmmod ? *reinterpret_cast<orc::ThreadSafeModule*>(llvmmod) : backing;
auto ctxt = clone.getContext();
Expand All @@ -269,9 +271,7 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
if (measure_compile_time_enabled)
compiler_start_time = jl_hrtime();

CompilationPolicy policy = (CompilationPolicy) _policy;
if (policy == CompilationPolicy::ImagingMode)
imaging_mode = 1;
params.imaging = imaging;

// compile all methods for the current world and type-inference world
size_t compile_for[] = { jl_typeinf_world, jl_atomic_load_acquire(&jl_world_counter) };
Expand Down Expand Up @@ -305,7 +305,8 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
// now add it to our compilation results
JL_GC_PROMISE_ROOTED(codeinst->rettype);
orc::ThreadSafeModule result_m = jl_create_llvm_module(name_from_method_instance(codeinst->def),
params.tsctx, clone.getModuleUnlocked()->getDataLayout(),
params.tsctx, params.imaging,
clone.getModuleUnlocked()->getDataLayout(),
Triple(clone.getModuleUnlocked()->getTargetTriple()));
jl_llvm_functions_t decls = jl_emit_code(result_m, mi, src, codeinst->rettype, params);
if (result_m)
Expand Down Expand Up @@ -401,8 +402,6 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
data->M = std::move(clone);
if (measure_compile_time_enabled)
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time));
if (policy == CompilationPolicy::ImagingMode)
imaging_mode = 0;
JL_UNLOCK(&jl_codegen_lock); // Might GC
return (void*)data;
}
Expand Down Expand Up @@ -521,7 +520,7 @@ void jl_dump_native_impl(void *native_code,
Type *T_psize = T_size->getPointerTo();

// add metadata information
if (imaging_mode) {
if (imaging_default()) {
emit_offset_table(*dataM, data->jl_sysimg_gvars, "jl_sysimg_gvars", T_psize);
emit_offset_table(*dataM, data->jl_sysimg_fvars, "jl_sysimg_fvars", T_psize);

Expand Down Expand Up @@ -1024,7 +1023,7 @@ void *jl_get_llvmf_defn_impl(jl_method_instance_t *mi, size_t world, char getwra
jl_codegen_params_t output(jl_ExecutionEngine->getContext());
output.world = world;
output.params = &params;
orc::ThreadSafeModule m = jl_create_llvm_module(name_from_method_instance(mi), output.tsctx);
orc::ThreadSafeModule m = jl_create_llvm_module(name_from_method_instance(mi), output.tsctx, output.imaging);
uint64_t compiler_start_time = 0;
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
if (measure_compile_time_enabled)
Expand Down
10 changes: 5 additions & 5 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ static Value *emit_plt(
const AttributeList &attrs,
CallingConv::ID cc, const char *f_lib, const char *f_name)
{
assert(imaging_mode);
assert(ctx.emission_context.imaging);
// Don't do this for vararg functions so that the `musttail` is only
// an optimization and is not required to function correctly.
assert(!functype->isVarArg());
Expand Down Expand Up @@ -658,14 +658,14 @@ static jl_cgval_t emit_cglobal(jl_codectx_t &ctx, jl_value_t **args, size_t narg
}
else if (sym.fptr != NULL) {
res = ConstantInt::get(lrt, (uint64_t)sym.fptr);
if (imaging_mode)
if (ctx.emission_context.imaging)
jl_printf(JL_STDERR,"WARNING: literal address used in cglobal for %s; code cannot be statically compiled\n", sym.f_name);
}
else {
if (sym.lib_expr) {
res = runtime_sym_lookup(ctx, cast<PointerType>(getInt8PtrTy(ctx.builder.getContext())), NULL, sym.lib_expr, sym.f_name, ctx.f);
}
else if (imaging_mode) {
else if (ctx.emission_context.imaging) {
res = runtime_sym_lookup(ctx, cast<PointerType>(getInt8PtrTy(ctx.builder.getContext())), sym.f_lib, NULL, sym.f_name, ctx.f);
res = ctx.builder.CreatePtrToInt(res, lrt);
}
Expand Down Expand Up @@ -1974,7 +1974,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
else if (symarg.fptr != NULL) {
Type *funcptype = PointerType::get(functype, 0);
llvmf = literal_static_pointer_val((void*)(uintptr_t)symarg.fptr, funcptype);
if (imaging_mode)
if (ctx.emission_context.imaging)
jl_printf(JL_STDERR,"WARNING: literal address used in ccall for %s; code cannot be statically compiled\n", symarg.f_name);
}
else {
Expand All @@ -1983,7 +1983,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
if (symarg.lib_expr) {
llvmf = runtime_sym_lookup(ctx, funcptype, NULL, symarg.lib_expr, symarg.f_name, ctx.f);
}
else if (imaging_mode) {
else if (ctx.emission_context.imaging) {
// vararg requires musttail,
// but musttail is incompatible with noreturn.
if (functype->isVarArg())
Expand Down
14 changes: 7 additions & 7 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ static Value *literal_pointer_val_slot(jl_codectx_t &ctx, jl_value_t *p)
{
// emit a pointer to a jl_value_t* which will allow it to be valid across reloading code
// also, try to give it a nice name for gdb, for easy identification
if (!imaging_mode) {
if (!ctx.emission_context.imaging) {
// TODO: this is an optimization, but is it useful or premature
// (it'll block any attempt to cache these, but can be simply deleted)
Module *M = jl_Module;
Expand Down Expand Up @@ -431,7 +431,7 @@ static Value *literal_pointer_val(jl_codectx_t &ctx, jl_value_t *p)
{
if (p == NULL)
return Constant::getNullValue(ctx.types().T_pjlvalue);
if (!imaging_mode)
if (!ctx.emission_context.imaging)
return literal_static_pointer_val(p, ctx.types().T_pjlvalue);
Value *pgv = literal_pointer_val_slot(ctx, p);
return tbaa_decorate(ctx.tbaa().tbaa_const, maybe_mark_load_dereferenceable(
Expand All @@ -445,7 +445,7 @@ static Value *literal_pointer_val(jl_codectx_t &ctx, jl_binding_t *p)
// emit a pointer to any jl_value_t which will be valid across reloading code
if (p == NULL)
return Constant::getNullValue(ctx.types().T_pjlvalue);
if (!imaging_mode)
if (!ctx.emission_context.imaging)
return literal_static_pointer_val(p, ctx.types().T_pjlvalue);
// bindings are prefixed with jl_bnd#
Value *pgv = julia_pgv(ctx, "jl_bnd#", p->name, p->owner, p);
Expand Down Expand Up @@ -485,7 +485,7 @@ static Value *julia_binding_gv(jl_codectx_t &ctx, jl_binding_t *b)
{
// emit a literal_pointer_val to a jl_binding_t
// binding->value are prefixed with *
if (imaging_mode)
if (ctx.emission_context.imaging)
return tbaa_decorate(ctx.tbaa().tbaa_const, ctx.builder.CreateAlignedLoad(ctx.types().T_pjlvalue,
julia_pgv(ctx, "*", b->name, b->owner, b), Align(sizeof(void*))));
else
Expand Down Expand Up @@ -881,13 +881,13 @@ static jl_cgval_t emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p)
if (p.TIndex) {
Value *tindex = ctx.builder.CreateAnd(p.TIndex, ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 0x7f));
bool allunboxed = is_uniontype_allunboxed(p.typ);
Value *datatype_or_p = imaging_mode ? Constant::getNullValue(ctx.types().T_ppjlvalue) : Constant::getNullValue(ctx.types().T_prjlvalue);
Value *datatype_or_p = ctx.emission_context.imaging ? Constant::getNullValue(ctx.types().T_ppjlvalue) : Constant::getNullValue(ctx.types().T_prjlvalue);
unsigned counter = 0;
for_each_uniontype_small(
[&](unsigned idx, jl_datatype_t *jt) {
Value *cmp = ctx.builder.CreateICmpEQ(tindex, ConstantInt::get(getInt8Ty(ctx.builder.getContext()), idx));
Value *ptr;
if (imaging_mode) {
if (ctx.emission_context.imaging) {
ptr = literal_pointer_val_slot(ctx, (jl_value_t*)jt);
}
else {
Expand All @@ -898,7 +898,7 @@ static jl_cgval_t emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p)
p.typ,
counter);
auto emit_unboxty = [&] () -> Value* {
if (imaging_mode)
if (ctx.emission_context.imaging)
return track_pjlvalue(
ctx, tbaa_decorate(ctx.tbaa().tbaa_const, ctx.builder.CreateAlignedLoad(ctx.types().T_pjlvalue, datatype_or_p, Align(sizeof(void*)))));
return datatype_or_p;
Expand Down
19 changes: 9 additions & 10 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ extern void _chkstk(void);
#endif
}

// for image reloading
bool imaging_mode = false;

// shared llvm state
#define jl_Module ctx.f->getParent()
#define jl_builderModule(builder) (builder).GetInsertBlock()->getParent()->getParent()
Expand Down Expand Up @@ -1953,7 +1950,7 @@ static jl_cgval_t convert_julia_type(jl_codectx_t &ctx, const jl_cgval_t &v, jl_
return jl_cgval_t(v, typ, new_tindex);
}

orc::ThreadSafeModule jl_create_llvm_module(StringRef name, orc::ThreadSafeContext context, const DataLayout &DL, const Triple &triple)
orc::ThreadSafeModule jl_create_llvm_module(StringRef name, orc::ThreadSafeContext context, bool imaging_mode, const DataLayout &DL, const Triple &triple)
{
auto lock = context.getLock();
Module *m = new Module(name, *context.getContext());
Expand Down Expand Up @@ -2099,7 +2096,7 @@ static void visitLine(jl_codectx_t &ctx, uint64_t *ptr, Value *addend, const cha

static void coverageVisitLine(jl_codectx_t &ctx, StringRef filename, int line)
{
assert(!imaging_mode);
assert(!ctx.emission_context.imaging);
if (filename == "" || filename == "none" || filename == "no file" || filename == "<missing>" || line < 0)
return;
visitLine(ctx, jl_coverage_data_pointer(filename, line), ConstantInt::get(getInt64Ty(ctx.builder.getContext()), 1), "lcnt");
Expand All @@ -2109,7 +2106,7 @@ static void coverageVisitLine(jl_codectx_t &ctx, StringRef filename, int line)

static void mallocVisitLine(jl_codectx_t &ctx, StringRef filename, int line, Value *sync)
{
assert(!imaging_mode);
assert(!ctx.emission_context.imaging);
if (filename == "" || filename == "none" || filename == "no file" || filename == "<missing>" || line < 0)
return;
Value *addend = sync
Expand Down Expand Up @@ -4723,6 +4720,7 @@ static std::pair<Function*, Function*> get_oc_function(jl_codectx_t &ctx, jl_met
// TODO: Emit this inline and outline it late using LLVM's coroutine support.
orc::ThreadSafeModule closure_m = jl_create_llvm_module(
name_from_method_instance(mi), ctx.emission_context.tsctx,
ctx.emission_context.imaging,
jl_Module->getDataLayout(), Triple(jl_Module->getTargetTriple()));
jl_llvm_functions_t closure_decls = emit_function(closure_m, mi, ir, rettype, ctx.emission_context);

Expand Down Expand Up @@ -8026,7 +8024,7 @@ jl_llvm_functions_t jl_emit_codeinst(
// don't delete inlineable code, unless it is constant
(codeinst->invoke == jl_fptr_const_return_addr || !jl_ir_flag_inlineable((jl_array_t*)codeinst->inferred)) &&
// don't delete code when generating a precompile file
!(imaging_mode || jl_options.incremental)) {
!(params.imaging || jl_options.incremental)) {
// if not inlineable, code won't be needed again
codeinst->inferred = jl_nothing;
}
Expand Down Expand Up @@ -8084,15 +8082,17 @@ void jl_compile_workqueue(
if (src) {
orc::ThreadSafeModule result_m =
jl_create_llvm_module(name_from_method_instance(codeinst->def),
params.tsctx, original.getDataLayout(), Triple(original.getTargetTriple()));
params.tsctx, params.imaging,
original.getDataLayout(), Triple(original.getTargetTriple()));
result.second = jl_emit_code(result_m, codeinst->def, src, src->rettype, params);
result.first = std::move(result_m);
}
}
else {
orc::ThreadSafeModule result_m =
jl_create_llvm_module(name_from_method_instance(codeinst->def),
params.tsctx, original.getDataLayout(), Triple(original.getTargetTriple()));
params.tsctx, params.imaging,
original.getDataLayout(), Triple(original.getTargetTriple()));
result.second = jl_emit_codeinst(result_m, codeinst, NULL, params);
result.first = std::move(result_m);
}
Expand Down Expand Up @@ -8286,7 +8286,6 @@ extern "C" void jl_init_llvm(void)
{
jl_page_size = jl_getpagesize();
jl_default_debug_info_kind = (int) DICompileUnit::DebugEmissionKind::FullDebug;
imaging_mode = jl_options.image_codegen || (jl_generating_output() && !jl_options.incremental);
jl_default_cgparams.generic_context = jl_nothing;
jl_init_debuginfo();

Expand Down
10 changes: 5 additions & 5 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ static jl_callptr_t _jl_compile_codeinst(
jl_workqueue_t emitted;
{
orc::ThreadSafeModule result_m =
jl_create_llvm_module(name_from_method_instance(codeinst->def), params.tsctx);
jl_create_llvm_module(name_from_method_instance(codeinst->def), params.tsctx, params.imaging);
jl_llvm_functions_t decls = jl_emit_codeinst(result_m, codeinst, src, params);
if (result_m)
emitted[codeinst] = {std::move(result_m), std::move(decls)};
{
auto temp_module = jl_create_llvm_module(name_from_method_instance(codeinst->def), params.tsctx);
auto temp_module = jl_create_llvm_module(name_from_method_instance(codeinst->def), params.tsctx, params.imaging);
jl_compile_workqueue(emitted, *temp_module.getModuleUnlocked(), params, CompilationPolicy::Default);
}

Expand Down Expand Up @@ -226,13 +226,13 @@ int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *
if (measure_compile_time_enabled)
compiler_start_time = jl_hrtime();
auto into = reinterpret_cast<orc::ThreadSafeModule*>(llvmmod);
jl_codegen_params_t *pparams = (jl_codegen_params_t*)p;
orc::ThreadSafeModule backing;
if (into == NULL) {
backing = jl_create_llvm_module("cextern", p ? ((jl_codegen_params_t*)p)->tsctx : jl_ExecutionEngine->getContext());
backing = jl_create_llvm_module("cextern", pparams ? pparams->tsctx : jl_ExecutionEngine->getContext(), pparams ? pparams->imaging : imaging_default());
into = &backing;
}
jl_codegen_params_t params(into->getContext());
jl_codegen_params_t *pparams = (jl_codegen_params_t*)p;
if (pparams == NULL)
pparams = &params;
const char *name = jl_generate_ccallable(reinterpret_cast<LLVMOrcThreadSafeModuleRef>(into), sysimg, declrt, sigt, *pparams);
Expand Down Expand Up @@ -834,7 +834,7 @@ namespace {
TheTriple.setObjectFormat(Triple::ELF);
#endif
uint32_t target_flags = 0;
auto target = jl_get_llvm_target(imaging_mode, target_flags);
auto target = jl_get_llvm_target(imaging_default(), target_flags);
auto &TheCPU = target.first;
SmallVector<std::string, 10> targetFeatures(target.second.begin(), target.second.end());
std::string errorstr;
Expand Down
13 changes: 8 additions & 5 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ using namespace llvm;

extern "C" jl_cgparams_t jl_default_cgparams;

extern bool imaging_mode;

void addTargetPasses(legacy::PassManagerBase *PM, TargetMachine *TM);
void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level, bool lower_intrinsics=true, bool dump_native=false, bool external_use=false);
void addMachinePasses(legacy::PassManagerBase *PM, TargetMachine *TM, int optlevel);
Expand All @@ -58,6 +56,10 @@ void jl_merge_module(orc::ThreadSafeModule &dest, orc::ThreadSafeModule src);
GlobalVariable *jl_emit_RTLD_DEFAULT_var(Module *M);
DataLayout jl_create_datalayout(TargetMachine &TM);

static inline bool imaging_default() {
return jl_options.image_codegen || (jl_generating_output() && !jl_options.incremental);
}

typedef struct _jl_llvm_functions_t {
std::string functionObject; // jlcall llvm Function name
std::string specFunctionObject; // specialized llvm Function name
Expand Down Expand Up @@ -119,7 +121,8 @@ typedef struct _jl_codegen_params_t {
size_t world = 0;
const jl_cgparams_t *params = &jl_default_cgparams;
bool cache = false;
_jl_codegen_params_t(orc::ThreadSafeContext ctx) : tsctx(std::move(ctx)), tsctx_lock(tsctx.getLock()) {}
bool imaging;
_jl_codegen_params_t(orc::ThreadSafeContext ctx) : tsctx(std::move(ctx)), tsctx_lock(tsctx.getLock()), imaging(imaging_default()) {}
} jl_codegen_params_t;

jl_llvm_functions_t jl_emit_code(
Expand Down Expand Up @@ -276,11 +279,11 @@ class JuliaOJIT {
DenseMap<void*, std::string> ReverseLocalSymbolTable;
};
extern JuliaOJIT *jl_ExecutionEngine;
orc::ThreadSafeModule jl_create_llvm_module(StringRef name, orc::ThreadSafeContext ctx, const DataLayout &DL = jl_ExecutionEngine->getDataLayout(), const Triple &triple = jl_ExecutionEngine->getTargetTriple());
orc::ThreadSafeModule jl_create_llvm_module(StringRef name, orc::ThreadSafeContext ctx, bool imaging_mode, const DataLayout &DL = jl_ExecutionEngine->getDataLayout(), const Triple &triple = jl_ExecutionEngine->getTargetTriple());

orc::ThreadSafeModule &jl_codegen_params_t::shared_module(Module &from) {
if (!_shared_module) {
_shared_module = jl_create_llvm_module("globals", tsctx, from.getDataLayout(), Triple(from.getTargetTriple()));
_shared_module = jl_create_llvm_module("globals", tsctx, imaging, from.getDataLayout(), Triple(from.getTargetTriple()));
assert(&from.getContext() == tsctx.getContext() && "Module context differs from codegen_params context!");
} else {
assert(&from.getContext() == _shared_module.getContext().getContext() && "Module context differs from shared module context!");
Expand Down

0 comments on commit ea197ce

Please sign in to comment.