Skip to content

Commit

Permalink
Use imaging mode generated code only (#50847)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi authored Aug 11, 2023
1 parent 555cd23 commit 2c0e1d8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 50 deletions.
7 changes: 3 additions & 4 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
orc::ThreadSafeModule backing;
if (!llvmmod) {
ctx = jl_ExecutionEngine->acquireContext();
backing = jl_create_ts_module("text", ctx, imaging);
backing = jl_create_ts_module("text", ctx);
}
orc::ThreadSafeModule &clone = llvmmod ? *unwrap(llvmmod) : backing;
auto ctxt = clone.getContext();
Expand Down Expand Up @@ -338,8 +338,7 @@ 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_ts_module(name_from_method_instance(codeinst->def),
params.tsctx, params.imaging_mode,
clone.getModuleUnlocked()->getDataLayout(),
params.tsctx, 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 @@ -2125,7 +2124,7 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, siz
// emit this function into a new llvm module
if (src && jl_is_code_info(src)) {
auto ctx = jl_ExecutionEngine->getContext();
orc::ThreadSafeModule m = jl_create_ts_module(name_from_method_instance(mi), *ctx, imaging_default());
orc::ThreadSafeModule m = jl_create_ts_module(name_from_method_instance(mi), *ctx);
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
28 changes: 10 additions & 18 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ static Constant *julia_pgv(jl_codectx_t &ctx, const char *cname, void *addr)
StringRef localname;
std::string gvname;
if (!gv) {
uint64_t id = ctx.emission_context.imaging_mode ? jl_atomic_fetch_add(&globalUniqueGeneratedNames, 1) : ctx.emission_context.global_targets.size();
uint64_t id = jl_atomic_fetch_add(&globalUniqueGeneratedNames, 1); // TODO: use ctx.emission_context.global_targets.size()
raw_string_ostream(gvname) << cname << id;
localname = StringRef(gvname);
}
Expand Down Expand Up @@ -1097,37 +1097,29 @@ static Value *emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p, bool maybenull
if (p.TIndex) {
Value *tindex = ctx.builder.CreateAnd(p.TIndex, ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 0x7f));
bool allunboxed = is_uniontype_allunboxed(p.typ);
Type *expr_type = justtag ? ctx.types().T_size : ctx.emission_context.imaging_mode ? ctx.types().T_pjlvalue : ctx.types().T_prjlvalue;
Value *datatype_or_p = Constant::getNullValue(ctx.emission_context.imaging_mode ? expr_type->getPointerTo() : expr_type);
Type *expr_type = justtag ? ctx.types().T_size : ctx.types().T_pjlvalue;
Value *datatype_or_p = Constant::getNullValue(expr_type->getPointerTo());
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));
Constant *ptr;
if (justtag && jt->smalltag) {
ptr = ConstantInt::get(expr_type, jt->smalltag << 4);
if (ctx.emission_context.imaging_mode)
ptr = get_pointer_to_constant(ctx.emission_context, ptr, Align(sizeof(jl_value_t*)), StringRef("_j_smalltag_") + jl_symbol_name(jt->name->name), *jl_Module);
ptr = get_pointer_to_constant(ctx.emission_context, ConstantInt::get(expr_type, jt->smalltag << 4), Align(sizeof(jl_value_t*)), StringRef("_j_smalltag_") + jl_symbol_name(jt->name->name), *jl_Module);
}
else if (ctx.emission_context.imaging_mode)
else {
ptr = ConstantExpr::getBitCast(literal_pointer_val_slot(ctx, (jl_value_t*)jt), datatype_or_p->getType());
else if (justtag)
ptr = ConstantInt::get(expr_type, (uintptr_t)jt);
else
ptr = ConstantExpr::getAddrSpaceCast(literal_static_pointer_val((jl_value_t*)jt, ctx.types().T_pjlvalue), expr_type);
}
datatype_or_p = ctx.builder.CreateSelect(cmp, ptr, datatype_or_p);
setName(ctx.emission_context, datatype_or_p, "typetag_ptr");
},
p.typ,
counter);
auto emit_unboxty = [&] () -> Value* {
if (ctx.emission_context.imaging_mode) {
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_const);
Value *datatype = ai.decorateInst(ctx.builder.CreateAlignedLoad(expr_type, datatype_or_p, Align(sizeof(void*))));
setName(ctx.emission_context, datatype, "typetag");
return justtag ? datatype : track_pjlvalue(ctx, datatype);
}
return datatype_or_p;
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_const);
Value *datatype = ai.decorateInst(ctx.builder.CreateAlignedLoad(expr_type, datatype_or_p, Align(sizeof(void*))));
setName(ctx.emission_context, datatype, "typetag");
return justtag ? datatype : track_pjlvalue(ctx, datatype);
};
Value *res;
if (!allunboxed) {
Expand Down
25 changes: 6 additions & 19 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2349,27 +2349,17 @@ 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);
}

std::unique_ptr<Module> jl_create_llvm_module(StringRef name, LLVMContext &context, bool imaging_mode, const DataLayout &DL, const Triple &triple)
std::unique_ptr<Module> jl_create_llvm_module(StringRef name, LLVMContext &context, const DataLayout &DL, const Triple &triple)
{
++ModulesCreated;
auto m = std::make_unique<Module>(name, context);
// Some linkers (*cough* OS X) don't understand DWARF v4, so we use v2 in
// imaging mode. The structure of v4 is slightly nicer for debugging JIT
// code.
// According to clang darwin above 10.10 supports dwarfv4
if (!m->getModuleFlag("Dwarf Version")) {
int dwarf_version = 4;
if (triple.isOSDarwin()) {
if (imaging_mode) {
dwarf_version = 2;
}
}
m->addModuleFlag(llvm::Module::Warning, "Dwarf Version", dwarf_version);
m->addModuleFlag(llvm::Module::Warning, "Dwarf Version", 4);
}
if (!m->getModuleFlag("Debug Info Version"))
m->addModuleFlag(llvm::Module::Warning, "Debug Info Version",
llvm::DEBUG_METADATA_VERSION);
if (imaging_mode)
m->addModuleFlag(llvm::Module::Error, "julia.imaging_mode", 1);
m->setDataLayout(DL);
m->setTargetTriple(triple.str());

Expand Down Expand Up @@ -5407,7 +5397,6 @@ 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_ts_module(
name_from_method_instance(mi), ctx.emission_context.tsctx,
ctx.emission_context.imaging_mode,
jl_Module->getDataLayout(), Triple(jl_Module->getTargetTriple()));
jl_llvm_functions_t closure_decls = emit_function(closure_m, mi, ir, rettype, ctx.emission_context);
JL_GC_POP();
Expand Down Expand Up @@ -8977,8 +8966,7 @@ void jl_compile_workqueue(
if (src) {
orc::ThreadSafeModule result_m =
jl_create_ts_module(name_from_method_instance(codeinst->def),
params.tsctx, params.imaging_mode,
params.DL, params.TargetTriple);
params.tsctx, params.DL, params.TargetTriple);
auto decls = jl_emit_code(result_m, codeinst->def, src, src->rettype, params);
if (result_m)
it = params.compiled_functions.insert(std::make_pair(codeinst, std::make_pair(std::move(result_m), std::move(decls)))).first;
Expand All @@ -8987,8 +8975,7 @@ void jl_compile_workqueue(
else {
orc::ThreadSafeModule result_m =
jl_create_ts_module(name_from_method_instance(codeinst->def),
params.tsctx, params.imaging_mode,
params.DL, params.TargetTriple);
params.tsctx, params.DL, params.TargetTriple);
auto decls = jl_emit_codeinst(result_m, codeinst, NULL, params);
if (result_m)
it = params.compiled_functions.insert(std::make_pair(codeinst, std::make_pair(std::move(result_m), std::move(decls)))).first;
Expand Down Expand Up @@ -9107,7 +9094,7 @@ void emitFloat16Wrappers(Module &M, bool external)
static void init_f16_funcs(void)
{
auto ctx = jl_ExecutionEngine->acquireContext();
auto TSM = jl_create_ts_module("F16Wrappers", ctx, imaging_default());
auto TSM = jl_create_ts_module("F16Wrappers", ctx);
auto aliasM = TSM.getModuleUnlocked();
emitFloat16Wrappers(*aliasM, true);
jl_ExecutionEngine->addModule(std::move(TSM));
Expand Down
10 changes: 5 additions & 5 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ void jl_jit_globals(std::map<void *, GlobalVariable*> &globals) JL_NOTSAFEPOINT

// used for image_codegen, where we keep all the gvs external
// so we can't jit them directly into each module
static orc::ThreadSafeModule jl_get_globals_module(orc::ThreadSafeContext &ctx, bool imaging_mode, const DataLayout &DL, const Triple &T, std::map<void *, GlobalVariable*> &globals) JL_NOTSAFEPOINT
static orc::ThreadSafeModule jl_get_globals_module(orc::ThreadSafeContext &ctx, const DataLayout &DL, const Triple &T, std::map<void *, GlobalVariable*> &globals) JL_NOTSAFEPOINT
{
auto lock = ctx.getLock();
auto GTSM = jl_create_ts_module("globals", ctx, imaging_mode, DL, T);
auto GTSM = jl_create_ts_module("globals", ctx, DL, T);
auto GM = GTSM.getModuleUnlocked();
for (auto &global : globals) {
auto GV = global.second;
Expand Down Expand Up @@ -217,7 +217,7 @@ static jl_callptr_t _jl_compile_codeinst(
params.debug_level = jl_options.debug_level;
{
orc::ThreadSafeModule result_m =
jl_create_ts_module(name_from_method_instance(codeinst->def), params.tsctx, params.imaging_mode, params.DL, params.TargetTriple);
jl_create_ts_module(name_from_method_instance(codeinst->def), params.tsctx, params.DL, params.TargetTriple);
jl_llvm_functions_t decls = jl_emit_codeinst(result_m, codeinst, src, params);
if (result_m)
params.compiled_functions[codeinst] = {std::move(result_m), std::move(decls)};
Expand All @@ -234,7 +234,7 @@ static jl_callptr_t _jl_compile_codeinst(
// to ensure that the globals are defined when they are compiled.
if (params.imaging_mode) {
// Won't contain any PLT/dlsym calls, so no need to optimize those
jl_ExecutionEngine->addModule(jl_get_globals_module(params.tsctx, params.imaging_mode, params.DL, params.TargetTriple, params.global_targets));
jl_ExecutionEngine->addModule(jl_get_globals_module(params.tsctx, params.DL, params.TargetTriple, params.global_targets));
} else {
StringMap<void*> NewGlobals;
for (auto &global : params.global_targets) {
Expand Down Expand Up @@ -377,7 +377,7 @@ int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *
if (!pparams) {
ctx = jl_ExecutionEngine->acquireContext();
}
backing = jl_create_ts_module("cextern", pparams ? pparams->tsctx : ctx, pparams ? pparams->imaging_mode : imaging_default(), pparams ? pparams->DL : jl_ExecutionEngine->getDataLayout(), pparams ? pparams->TargetTriple : jl_ExecutionEngine->getTargetTriple());
backing = jl_create_ts_module("cextern", pparams ? pparams->tsctx : ctx, pparams ? pparams->DL : jl_ExecutionEngine->getDataLayout(), pparams ? pparams->TargetTriple : jl_ExecutionEngine->getTargetTriple());
into = &backing;
}
JL_LOCK(&jl_codegen_lock);
Expand Down
8 changes: 4 additions & 4 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,15 @@ class JuliaOJIT {
CompileLayerT ExternalCompileLayer;
};
extern JuliaOJIT *jl_ExecutionEngine;
std::unique_ptr<Module> jl_create_llvm_module(StringRef name, LLVMContext &ctx, bool imaging_mode, const DataLayout &DL = jl_ExecutionEngine->getDataLayout(), const Triple &triple = jl_ExecutionEngine->getTargetTriple()) JL_NOTSAFEPOINT;
inline orc::ThreadSafeModule jl_create_ts_module(StringRef name, orc::ThreadSafeContext ctx, bool imaging_mode, const DataLayout &DL = jl_ExecutionEngine->getDataLayout(), const Triple &triple = jl_ExecutionEngine->getTargetTriple()) JL_NOTSAFEPOINT {
std::unique_ptr<Module> jl_create_llvm_module(StringRef name, LLVMContext &ctx, const DataLayout &DL = jl_ExecutionEngine->getDataLayout(), const Triple &triple = jl_ExecutionEngine->getTargetTriple()) JL_NOTSAFEPOINT;
inline orc::ThreadSafeModule jl_create_ts_module(StringRef name, orc::ThreadSafeContext ctx, const DataLayout &DL = jl_ExecutionEngine->getDataLayout(), const Triple &triple = jl_ExecutionEngine->getTargetTriple()) JL_NOTSAFEPOINT {
auto lock = ctx.getLock();
return orc::ThreadSafeModule(jl_create_llvm_module(name, *ctx.getContext(), imaging_mode, DL, triple), ctx);
return orc::ThreadSafeModule(jl_create_llvm_module(name, *ctx.getContext(), DL, triple), ctx);
}

Module &jl_codegen_params_t::shared_module() JL_NOTSAFEPOINT {
if (!_shared_module) {
_shared_module = jl_create_llvm_module("globals", getContext(), imaging_mode, DL, TargetTriple);
_shared_module = jl_create_llvm_module("globals", getContext(), DL, TargetTriple);
}
return *_shared_module;
}
Expand Down

0 comments on commit 2c0e1d8

Please sign in to comment.