Skip to content

Commit

Permalink
Make shared_module a basic LLVM module
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi authored and maleadt committed Jan 5, 2023
1 parent f6198d8 commit 2295309
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

#include <llvm/IR/LegacyPassManagers.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/Linker/Linker.h>


using namespace llvm;
Expand Down Expand Up @@ -400,6 +401,7 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm

// clones the contents of the module `m` to the shadow_output collector
// while examining and recording what kind of function pointer we have
Linker L(*clone.getModuleUnlocked());
for (auto &def : emitted) {
jl_merge_module(clone, std::move(std::get<0>(def.second)));
jl_code_instance_t *this_code = def.first;
Expand Down Expand Up @@ -427,7 +429,9 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
data->jl_fvar_map[this_code] = std::make_tuple(func_id, cfunc_id);
}
if (params._shared_module) {
jl_merge_module(clone, std::move(params._shared_module));
bool error = L.linkInModule(std::move(params._shared_module));
assert(!error && "Error linking in shared module");
(void)error;
}

// now get references to the globals in the merged module
Expand Down
7 changes: 2 additions & 5 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ GlobalVariable *jl_emit_RTLD_DEFAULT_var(Module *M)
static bool runtime_sym_gvs(jl_codectx_t &ctx, const char *f_lib, const char *f_name,
GlobalVariable *&lib, GlobalVariable *&sym)
{
auto &TSM = ctx.emission_context.shared_module(*jl_Module);
//Safe b/c emission context holds context lock
auto M = TSM.getModuleUnlocked();
auto M = &ctx.emission_context.shared_module(*jl_Module);
bool runtime_lib = false;
GlobalVariable *libptrgv;
jl_codegen_params_t::SymMapGV *symMap;
Expand Down Expand Up @@ -238,8 +236,7 @@ static GlobalVariable *emit_plt_thunk(
bool runtime_lib)
{
++PLTThunks;
auto &TSM = ctx.emission_context.shared_module(*jl_Module);
Module *M = TSM.getModuleUnlocked();
auto M = &ctx.emission_context.shared_module(*jl_Module);
PointerType *funcptype = PointerType::get(functype, 0);
libptrgv = prepare_global_in(M, libptrgv);
llvmgv = prepare_global_in(M, llvmgv);
Expand Down
2 changes: 1 addition & 1 deletion src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ static Value *julia_pgv(jl_codectx_t &ctx, const char *cname, void *addr)
}
if (gv == nullptr)
gv = new GlobalVariable(*M, ctx.types().T_pjlvalue,
false, GlobalVariable::PrivateLinkage,
false, GlobalVariable::ExternalLinkage,
NULL, localname);
// LLVM passes sometimes strip metadata when moving load around
// since the load at the new location satisfy the same condition as the original one.
Expand Down
4 changes: 2 additions & 2 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ static jl_callptr_t _jl_compile_codeinst(
}

if (params._shared_module)
jl_ExecutionEngine->addModule(std::move(params._shared_module));
jl_ExecutionEngine->addModule(orc::ThreadSafeModule(std::move(params._shared_module), params.tsctx));
StringMap<orc::ThreadSafeModule*> NewExports;
StringMap<void*> NewGlobals;
for (auto &global : params.globals) {
Expand Down Expand Up @@ -334,7 +334,7 @@ int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *
jl_jit_globals(params.globals);
assert(params.workqueue.empty());
if (params._shared_module)
jl_ExecutionEngine->addModule(std::move(params._shared_module));
jl_ExecutionEngine->addModule(orc::ThreadSafeModule(std::move(params._shared_module), params.tsctx));
}
if (success && llvmmod == NULL)
jl_ExecutionEngine->addModule(std::move(*into));
Expand Down
16 changes: 8 additions & 8 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ typedef struct _jl_codegen_params_t {
DenseMap<AttributeList, std::map<
std::tuple<GlobalVariable*, FunctionType*, CallingConv::ID>,
GlobalVariable*>> allPltMap;
orc::ThreadSafeModule _shared_module;
inline orc::ThreadSafeModule &shared_module(Module &from);
std::unique_ptr<Module> _shared_module;
inline Module &shared_module(Module &from);
// inputs
size_t world = 0;
const jl_cgparams_t *params = &jl_default_cgparams;
Expand Down Expand Up @@ -544,16 +544,16 @@ inline orc::ThreadSafeModule jl_create_ts_module(StringRef name, orc::ThreadSafe
return orc::ThreadSafeModule(jl_create_llvm_module(name, *ctx.getContext(), imaging_mode, DL, triple), ctx);
}

orc::ThreadSafeModule &jl_codegen_params_t::shared_module(Module &from) JL_NOTSAFEPOINT {
Module &jl_codegen_params_t::shared_module(Module &from) JL_NOTSAFEPOINT {
if (!_shared_module) {
_shared_module = jl_create_ts_module("globals", tsctx, imaging, from.getDataLayout(), Triple(from.getTargetTriple()));
_shared_module = jl_create_llvm_module("globals", getContext(), 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!");
assert(from.getDataLayout() == _shared_module.getModuleUnlocked()->getDataLayout() && "Module data layout differs from shared module data layout!");
assert(from.getTargetTriple() == _shared_module.getModuleUnlocked()->getTargetTriple() && "Module target triple differs from shared module target triple!");
assert(&from.getContext() == &getContext() && "Module context differs from shared module context!");
assert(from.getDataLayout() == _shared_module->getDataLayout() && "Module data layout differs from shared module data layout!");
assert(from.getTargetTriple() == _shared_module->getTargetTriple() && "Module target triple differs from shared module target triple!");
}
return _shared_module;
return *_shared_module;
}

Pass *createLowerPTLSPass(bool imaging_mode) JL_NOTSAFEPOINT;
Expand Down

0 comments on commit 2295309

Please sign in to comment.