18using namespace llvm::orc;
26 OrcJit(llvm::orc::JITTargetMachineBuilder JTMB, llvm::DataLayout
DL)
27 :
ObjectLayer(*
ES, []() {
return std::make_unique<llvm::SectionMemoryManager>(); })
32 ,
Ctx(std::make_unique<llvm::LLVMContext>())
36 llvm::cantFail(llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
DL.getGlobalPrefix())));
39 static llvm::Expected<std::unique_ptr<OrcJit>>
Create()
41 auto JTMB = llvm::orc::JITTargetMachineBuilder::detectHost();
43 return JTMB.takeError();
45 auto DL = JTMB->getDefaultDataLayoutForTarget();
47 return DL.takeError();
49 return std::make_unique<OrcJit>(std::move(*JTMB), std::move(*
DL));
60 llvm::Expected<compat::lookup_symbol_T>
lookup(llvm::StringRef Name)
65 bool loadLib(
const std::string &libName,
const std::set<std::string> &libPaths)
67 for (
const auto &libPath : libPaths)
69 SmallString<128> fullPath;
70 sys::path::append(fullPath, libPath,
"lib" + libName +
".so");
71 if (sys::fs::exists(fullPath))
74 llvm::orc::DynamicLibrarySearchGenerator::Load(fullPath.c_str(),
DL.getGlobalPrefix())));
82 static llvm::orc::ThreadSafeModule
optimizeModule(llvm::orc::ThreadSafeModule TSM,
83 const llvm::orc::MaterializationResponsibility &R)
85 auto lock = TSM.getContext();
86 auto M = TSM.getModuleUnlocked();
89 auto FPM = std::make_unique<llvm::legacy::FunctionPassManager>(M);
92 FPM->add(llvm::createInstructionCombiningPass());
93 FPM->add(llvm::createReassociatePass());
94 FPM->add(llvm::createGVNPass());
95 FPM->add(llvm::createCFGSimplificationPass());
96 FPM->doInitialization();
113 llvm::orc::ThreadSafeContext
Ctx;
125 InitializeNativeTarget();
126 InitializeNativeTargetAsmPrinter();
127 InitializeNativeTargetAsmParser();
136void *
LLVMJIT::translate(std::string code, std::set<std::string> headerpaths, std::set<std::string> librarypaths,
137 std::set<std::string> libraries, std::string &error,
bool debug)
139 clang::CompilerInstance CI;
141 auto pto = std::make_shared<clang::TargetOptions>();
142 pto->Triple = llvm::sys::getDefaultTargetTriple();
143 TargetInfo *pti = TargetInfo::CreateTargetInfo(CI.getDiagnostics(), pto);
145 CI.createFileManager();
146 CI.createSourceManager(CI.getFileManager());
147 CI.createPreprocessor(compat::tu_module_T);
150 std::vector<std::string> args;
153 args.push_back(
"-O0");
157 args.push_back(
"-O3");
159 args.push_back(
"-std=c99");
161 args.push_back(
"-isystem/usr/include");
162 for (
const auto &headerPath : headerpaths)
164 args.push_back(
"-isystem" + headerPath);
166 args.push_back(
"/etiss_llvm_clang_memory_mapped_file.c");
167 args.push_back(
"-isystem/usr/include/x86_64-linux-gnu");
169 for (
const auto &lib : libraries)
173 if (!
orcJit_->loadLib(lib, librarypaths))
175 error =
"could not load library";
183 std::vector<const char *> argsCStr;
184 for (
const auto &arg : args)
186 argsCStr.push_back(arg.c_str());
188 if (!CompilerInvocation::CreateFromArgs(CI.getInvocation(), argsCStr, CI.getDiagnostics()))
190 error =
"error on parsing args";
198 EmitLLVMOnlyAction action(&
orcJit_->getContext());
201 if (!CI.ExecuteAction(action))
203 error =
"failed to execute translation action ";
208 orcJit_->addModule(action.takeModule());
Header file of the ETISS library.
bool etiss_jit_llvm_init_done_
std::mutex etiss_jit_llvm_init_mu_
llvm::orc::RTDyldObjectLinkingLayer ObjectLayer
llvm::orc::IRCompileLayer CompileLayer
OrcJit(llvm::orc::JITTargetMachineBuilder JTMB, llvm::DataLayout DL)
llvm::orc::ThreadSafeContext Ctx
llvm::orc::IRTransformLayer OptimizeLayer
llvm::Expected< compat::lookup_symbol_T > lookup(llvm::StringRef Name)
std::unique_ptr< llvm::orc::ExecutionSession > ES
void addModule(std::unique_ptr< llvm::Module > M)
bool loadLib(const std::string &libName, const std::set< std::string > &libPaths)
llvm::LLVMContext & getContext()
llvm::orc::JITDylib & MainJITDylib
const llvm::DataLayout & getDataLayout() const
llvm::orc::MangleAndInterner Mangle
static llvm::orc::ThreadSafeModule optimizeModule(llvm::orc::ThreadSafeModule TSM, const llvm::orc::MaterializationResponsibility &R)
static llvm::Expected< std::unique_ptr< OrcJit > > Create()
compiler interface for just in time compilation of generated C code
virtual void * getFunction(void *handle, std::string name, std::string &error)
returns a function pointer to a compiled function from the handle returned by etiss::JIT::translate
std::unordered_set< std::string > loadedLibs_
virtual void free(void *handle)
clean up handled returned by etiss::JIT::translate
virtual void * translate(std::string code, std::set< std::string > headerpaths, std::set< std::string > librarypaths, std::set< std::string > libraries, std::string &error, bool debug=false)
translate C code to executable code and return a handle/pointer that identifies the compilation resul...
std::unique_ptr< OrcJit > orcJit_
std::unique_ptr< llvm::MemoryBuffer > get_virtual_source(llvm::StringRef code, clang::CompilerInstance &CI)
void * get_function_ptr(const compat::lookup_symbol_T &func)
void createDiagnostics(clang::CompilerInstance &CI)
std::unique_ptr< llvm::orc::ExecutionSession > createExecutionSession()
std::string jitFiles()
Get ETISS JIT files path.