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

Refactor JIT engine to make it friendlier to multiple contexts #44573

Merged
merged 1 commit into from
Mar 13, 2022
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
31 changes: 14 additions & 17 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ void jl_dump_llvm_opt_impl(void *s)
dump_llvm_opt_stream = (JL_STREAM*)s;
}

static void jl_add_to_ee(std::unique_ptr<Module> m);
static void jl_add_to_ee(std::unique_ptr<Module> &M, StringMap<std::unique_ptr<Module>*> &NewExports);
static void jl_decorate_module(Module &M);
static uint64_t getAddressForFunction(StringRef fname);

void jl_link_global(GlobalVariable *GV, void *addr)
Expand Down Expand Up @@ -130,7 +130,7 @@ static jl_callptr_t _jl_compile_codeinst(
jl_compile_workqueue(emitted, params, CompilationPolicy::Default, context);

if (params._shared_module)
jl_add_to_ee(std::unique_ptr<Module>(params._shared_module));
jl_ExecutionEngine->addModule(std::unique_ptr<Module>(params._shared_module));
StringMap<std::unique_ptr<Module>*> NewExports;
StringMap<void*> NewGlobals;
for (auto &global : params.globals) {
Expand Down Expand Up @@ -236,10 +236,10 @@ int jl_compile_extern_c_impl(LLVMModuleRef llvmmod, void *p, void *sysimg, jl_va
jl_jit_globals(params.globals);
assert(params.workqueue.empty());
if (params._shared_module)
jl_add_to_ee(std::unique_ptr<Module>(params._shared_module));
jl_ExecutionEngine->addModule(std::unique_ptr<Module>(params._shared_module));
}
if (success && llvmmod == NULL)
jl_add_to_ee(std::unique_ptr<Module>(into));
jl_ExecutionEngine->addModule(std::unique_ptr<Module>(into));
}
if (jl_codegen_lock.count == 1 && measure_compile_time_enabled)
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time));
Expand Down Expand Up @@ -1016,6 +1016,8 @@ void JuliaOJIT::addGlobalMapping(StringRef Name, uint64_t Addr)
void JuliaOJIT::addModule(std::unique_ptr<Module> M)
{
JL_TIMING(LLVM_MODULE_FINISH);
jl_decorate_module(*M);
shareStrings(*M);
std::vector<std::string> NewExports;
for (auto &F : M->global_values()) {
if (!F.isDeclaration() && F.getLinkage() == GlobalValue::ExternalLinkage) {
Expand Down Expand Up @@ -1307,7 +1309,7 @@ void jl_merge_module(Module *dest, std::unique_ptr<Module> src)

// optimize memory by turning long strings into memoized copies, instead of
// making a copy per object file of output.
void jl_jit_share_data(Module &M)
void JuliaOJIT::shareStrings(Module &M)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a note, we discussed with Lang that this would be something we could do at the JITLink level instead, but that will have to wait until all platforms use JITLink.

{
std::vector<GlobalVariable*> erase;
for (auto &GV : M.globals()) {
Expand All @@ -1320,7 +1322,7 @@ void jl_jit_share_data(Module &M)
if (data.size() > 16) { // only for long strings: keep short ones as values
Type *T_size = Type::getIntNTy(GV.getContext(), sizeof(void*) * 8);
Constant *v = ConstantExpr::getIntToPtr(
ConstantInt::get(T_size, (uintptr_t)data.data()),
ConstantInt::get(T_size, (uintptr_t)(*ES.intern(data)).data()),
GV.getType());
GV.replaceAllUsesWith(v);
erase.push_back(&GV);
Expand All @@ -1330,26 +1332,21 @@ void jl_jit_share_data(Module &M)
GV->eraseFromParent();
}

static void jl_add_to_ee(std::unique_ptr<Module> m)
{
static void jl_decorate_module(Module &M) {
#if defined(_CPU_X86_64_) && defined(_OS_WINDOWS_)
// Add special values used by debuginfo to build the UnwindData table registration for Win64
Type *T_uint32 = Type::getInt32Ty(m->getContext());
ArrayType *atype = ArrayType::get(T_uint32, 3); // want 4-byte alignment of 12-bytes of data
ArrayType *atype = ArrayType::get(Type::getInt32Ty(M.getContext()), 3); // want 4-byte alignment of 12-bytes of data
GlobalVariable *gvs[2] = {
new GlobalVariable(*m, atype,
new GlobalVariable(M, atype,
false, GlobalVariable::InternalLinkage,
ConstantAggregateZero::get(atype), "__UnwindData"),
new GlobalVariable(*m, atype,
new GlobalVariable(M, atype,
false, GlobalVariable::InternalLinkage,
ConstantAggregateZero::get(atype), "__catchjmp") };
gvs[0]->setSection(".text");
gvs[1]->setSection(".text");
appendToCompilerUsed(*m, makeArrayRef((GlobalValue**)gvs, 2));
appendToCompilerUsed(M, makeArrayRef((GlobalValue**)gvs, 2));
#endif
jl_jit_share_data(*m);
assert(jl_ExecutionEngine);
jl_ExecutionEngine->addModule(std::move(m));
}

static int jl_add_to_ee(
Expand Down Expand Up @@ -1393,7 +1390,7 @@ static int jl_add_to_ee(
Queued.erase(CM->get());
jl_merge_module(M.get(), std::move(*CM));
}
jl_add_to_ee(std::move(M));
jl_ExecutionEngine->addModule(std::move(M));
MergeUp = 0;
}
else {
Expand Down
1 change: 1 addition & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class JuliaOJIT {
private:
std::string getMangledName(StringRef Name);
std::string getMangledName(const GlobalValue *GV);
void shareStrings(Module &M);

std::unique_ptr<TargetMachine> TM;
DataLayout DL;
Expand Down