Skip to content

Commit

Permalink
issue #164 try to get ORC v2 working
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Aug 17, 2019
1 parent 9c88c84 commit 5b15a2f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion include/ravi_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#define USE_ORC_JIT 0
#endif

#if LLVM_VERSION_MAJOR >= 8
#if LLVM_VERSION_MAJOR >= 8 && !defined(_WIN32)
#define USE_ORCv2_JIT 1
#else
#define USE_ORCv2_JIT 0
Expand Down
8 changes: 4 additions & 4 deletions include/ravi_llvmcodegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class RaviJITState {
#elif USE_ORC_JIT

// The LLVM Context
llvm::LLVMContext *context_;
std::unique_ptr<llvm::LLVMContext> context_;

// From LLVM version5 onwards we use the new ORC apis
// The main benefit is that memory management is tighter,
Expand Down Expand Up @@ -450,15 +450,15 @@ class RaviJITState {

// Not ORC_JIT
// The LLVM Context
llvm::LLVMContext *context_;
std::unique_ptr<llvm::LLVMContext> context_;

#endif // USE_ORCv2_JIT

// The triple represents the host target
std::string triple_;

// Lua type definitions
LuaLLVMTypes *types_;
std::unique_ptr<LuaLLVMTypes> types_;

// Should we auto compile what we can?
unsigned int auto_ : 1;
Expand Down Expand Up @@ -548,7 +548,7 @@ class RaviJITState {
void addGlobalSymbol(const std::string &name, void *address);

void dump();
LuaLLVMTypes *types() const { return types_; }
LuaLLVMTypes *types() const { return types_.get(); }
const std::string &triple() const { return triple_; }
bool is_auto() const { return auto_; }
void set_auto(bool value) { auto_ = value; }
Expand Down
2 changes: 1 addition & 1 deletion src/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#ifdef USE_LLVM
#define ravi_xstringify(s) ravi_stringify(s)
#define ravi_stringify(s) #s
#define RAVI_OPTION_STRING3 " LLVM-" LLVM_VERSION_STRING " ORC=" ravi_xstringify(USE_ORC_JIT)
#define RAVI_OPTION_STRING3 " LLVM-" LLVM_VERSION_STRING " ORC=" ravi_xstringify(USE_ORC_JIT) " v2=" ravi_xstringify(USE_ORCv2_JIT)
#elif USE_OMRJIT
#define RAVI_OPTION_STRING3 " omrjit"
#else
Expand Down
66 changes: 32 additions & 34 deletions src/ravi_llvmjit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,20 @@ RaviJITState::RaviJITState()
JTMB.setCodeGenOptLevel(llvm::CodeGenOpt::Default);
auto dataLayout = llvm::cantFail(JTMB.getDefaultDataLayoutForTarget());
TM = llvm::cantFail(JTMB.createTargetMachine());
ES = std::unique_ptr<llvm::orc::ExecutionSession>(new llvm::orc::ExecutionSession());
ObjectLayer = std::unique_ptr<llvm::orc::RTDyldObjectLinkingLayer>(
new llvm::orc::RTDyldObjectLinkingLayer(*ES, []() { return llvm::make_unique<llvm::SectionMemoryManager>(); }));
CompileLayer = std::unique_ptr<llvm::orc::IRCompileLayer>(
new llvm::orc::IRCompileLayer(*ES, *ObjectLayer, llvm::orc::SimpleCompiler(*TM)));
OptimizeLayer = std::unique_ptr<llvm::orc::IRTransformLayer>(new llvm::orc::IRTransformLayer(
ES = llvm::make_unique<llvm::orc::ExecutionSession>();
ObjectLayer = llvm::make_unique<llvm::orc::RTDyldObjectLinkingLayer>(
*ES, []() { return llvm::make_unique<llvm::SectionMemoryManager>(); });
CompileLayer = llvm::make_unique<llvm::orc::IRCompileLayer>(*ES, *ObjectLayer, llvm::orc::SimpleCompiler(*TM));
OptimizeLayer = llvm::make_unique<llvm::orc::IRTransformLayer>(
*ES, *CompileLayer, [this](llvm::orc::ThreadSafeModule TSM, const llvm::orc::MaterializationResponsibility &R) {
return this->optimizeModule(std::move(TSM), R);
}));
DL = std::unique_ptr<llvm::DataLayout>(new llvm::DataLayout(std::move(dataLayout)));
Mangle = std::unique_ptr<llvm::orc::MangleAndInterner>(new llvm::orc::MangleAndInterner(*ES, *this->DL));
Ctx = std::unique_ptr<llvm::orc::ThreadSafeContext>(
new llvm::orc::ThreadSafeContext(llvm::make_unique<llvm::LLVMContext>()));
});
DL = llvm::make_unique<llvm::DataLayout>(std::move(dataLayout));
Mangle = llvm::make_unique<llvm::orc::MangleAndInterner>(*ES, *this->DL);
Ctx = llvm::make_unique<llvm::orc::ThreadSafeContext>(llvm::make_unique<llvm::LLVMContext>());
ES->getMainJITDylib().setGenerator(cantFail(llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(*DL)));

types_ = new LuaLLVMTypes(*Ctx->getContext());
types_ = llvm::make_unique<LuaLLVMTypes>(*Ctx->getContext());

#else

Expand All @@ -237,7 +235,7 @@ RaviJITState::RaviJITState()
// format; LLVM 3.7 onwards COEFF is supported
triple_ += "-elf";
#endif
context_ = new llvm::LLVMContext();
context_ = std::unique_ptr<llvm::LLVMContext>(new llvm::LLVMContext());

#if USE_ORC_JIT
auto target = llvm::EngineBuilder().selectTarget();
Expand Down Expand Up @@ -274,18 +272,19 @@ RaviJITState::RaviJITState()

#endif

types_ = new LuaLLVMTypes(*context_);
types_ = std::unique_ptr<LuaLLVMTypes>(new LuaLLVMTypes(*context_));

#endif // USE_ORCv2_JIT

// Register global symbols
#if USE_ORCv2_JIT
auto &JD = ES->getMainJITDylib();
llvm::orc::MangleAndInterner mangle(*ES, *this->DL);
llvm::orc::SymbolMap Symbols;
for (int i = 0; global_syms[i].name != nullptr; i++) {
Symbols.insert( { mangle(global_syms[i].name),
llvm::JITEvaluatedSymbol(llvm::pointerToJITTargetAddress(global_syms[i].address),
llvm::JITSymbolFlags(llvm::JITSymbolFlags::FlagNames::Absolute)) });
Symbols.insert({mangle(global_syms[i].name),
llvm::JITEvaluatedSymbol(llvm::pointerToJITTargetAddress(global_syms[i].address),
llvm::JITSymbolFlags(llvm::JITSymbolFlags::FlagNames::Absolute))});
}
llvm::cantFail(JD.define(llvm::orc::absoluteSymbols(Symbols)), "Failed to install extern symbols");
#else
Expand All @@ -297,13 +296,7 @@ RaviJITState::RaviJITState()

// Destroy the JIT state freeing up any
// functions that were compiled
RaviJITState::~RaviJITState() {
assert(allocated_modules_ == 0);
delete types_;
#ifndef USE_ORCv2_JIT
delete context_;
#endif
}
RaviJITState::~RaviJITState() { assert(allocated_modules_ == 0); }

#if USE_ORC_JIT || USE_ORCv2_JIT
#if USE_ORCv2_JIT
Expand Down Expand Up @@ -350,7 +343,7 @@ std::shared_ptr<llvm::Module> RaviJITState::optimizeModule(std::shared_ptr<llvm:
FPM->doInitialization();
// Run the optimizations over all functions in the module being added to
// the JIT.
//llvm::dbgs() << "Before optimization:\n" << *M << "-->\n";
// llvm::dbgs() << "Before optimization:\n" << *M << "-->\n";
for (auto &F : *M)
FPM->run(F);
}
Expand All @@ -377,7 +370,7 @@ std::shared_ptr<llvm::Module> RaviJITState::optimizeModule(std::shared_ptr<llvm:
}
}
MPM->run(*M);
//llvm::dbgs() << "After optimization:\n" << *M << "-->\n";
// llvm::dbgs() << "After optimization:\n" << *M << "-->\n";
}
if (get_verbosity() == 2 && codestr.length() > 0)
llvm::errs() << codestr << "\n";
Expand Down Expand Up @@ -593,10 +586,13 @@ RaviJITFunction::~RaviJITFunction() {
module_->removeFunction(this);
}

// This is noop when ORC api is enabled
// Retained for backward compatibility
void RaviJITModule::runpasses(bool dumpAsm) {
#if !USE_ORC_JIT
if (!module_)
return;
#if !USE_ORC_JIT

#if LLVM_VERSION_MAJOR > 3 || LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7
using llvm::legacy::FunctionPassManager;
using llvm::legacy::PassManager;
Expand Down Expand Up @@ -673,7 +669,11 @@ void RaviJITModule::runpasses(bool dumpAsm) {
llvm::errs() << "unable to dump assembly\n";
break;
}
if (TM->addPassesToEmitFile(*MPM, formatted_stream, llvm::TargetMachine::CGFT_AssemblyFile)) {
if (TM->addPassesToEmitFile(*MPM, formatted_stream,
#if LLVM_VERSION_MAJOR >= 7
nullptr, // DWO output file
#endif
llvm::TargetMachine::CGFT_AssemblyFile)) {
llvm::errs() << "unable to add passes for generating assemblyfile\n";
break;
}
Expand Down Expand Up @@ -846,9 +846,8 @@ int raviV_compile(struct lua_State *L, struct Proto *p, ravi_compile_options_t *
}
if (doCompile) {
auto module = std::make_shared<ravi::RaviJITModule>(G->ravi_state->jit);
if (G->ravi_state->jit->is_use_dmrc() ?
G->ravi_state->code_generator->alt_compile(L, p, module, options) :
G->ravi_state->code_generator->compile(L, p, module, options)) {
if (G->ravi_state->jit->is_use_dmrc() ? G->ravi_state->code_generator->alt_compile(L, p, module, options)
: G->ravi_state->code_generator->compile(L, p, module, options)) {
module->runpasses();
module->finalize(G->ravi_state->jit->get_verbosity() == 3);
}
Expand All @@ -866,9 +865,8 @@ int raviV_compile_n(struct lua_State *L, struct Proto *p[], int n, ravi_compile_
return 0;
auto module = std::make_shared<ravi::RaviJITModule>(G->ravi_state->jit);
for (int i = 0; i < n; i++) {
if (G->ravi_state->jit->is_use_dmrc() ?
G->ravi_state->code_generator->alt_compile(L, p[i], module, options) :
G->ravi_state->code_generator->compile(L, p[i], module, options))
if (G->ravi_state->jit->is_use_dmrc() ? G->ravi_state->code_generator->alt_compile(L, p[i], module, options)
: G->ravi_state->code_generator->compile(L, p[i], module, options))
count++;
}
if (count) {
Expand Down

0 comments on commit 5b15a2f

Please sign in to comment.