diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2019-01-19 10:01:25 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2019-01-19 10:01:25 +0000 |
commit | d8e91e46262bc44006913e6796843909f1ac7bcd (patch) | |
tree | 7d0c143d9b38190e0fa0180805389da22cd834c5 /lib/ExecutionEngine/Orc/LLJIT.cpp | |
parent | b7eb8e35e481a74962664b63dfb09483b200209a (diff) |
Notes
Diffstat (limited to 'lib/ExecutionEngine/Orc/LLJIT.cpp')
-rw-r--r-- | lib/ExecutionEngine/Orc/LLJIT.cpp | 166 |
1 files changed, 121 insertions, 45 deletions
diff --git a/lib/ExecutionEngine/Orc/LLJIT.cpp b/lib/ExecutionEngine/Orc/LLJIT.cpp index 52ff4efe56b2..e2089f9106bd 100644 --- a/lib/ExecutionEngine/Orc/LLJIT.cpp +++ b/lib/ExecutionEngine/Orc/LLJIT.cpp @@ -12,49 +12,109 @@ #include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/IR/Mangler.h" +namespace { + + // A SimpleCompiler that owns its TargetMachine. + class TMOwningSimpleCompiler : public llvm::orc::SimpleCompiler { + public: + TMOwningSimpleCompiler(std::unique_ptr<llvm::TargetMachine> TM) + : llvm::orc::SimpleCompiler(*TM), TM(std::move(TM)) {} + private: + // FIXME: shared because std::functions (and thus + // IRCompileLayer::CompileFunction) are not moveable. + std::shared_ptr<llvm::TargetMachine> TM; + }; + +} // end anonymous namespace + namespace llvm { namespace orc { +LLJIT::~LLJIT() { + if (CompileThreads) + CompileThreads->wait(); +} + Expected<std::unique_ptr<LLJIT>> -LLJIT::Create(std::unique_ptr<ExecutionSession> ES, - std::unique_ptr<TargetMachine> TM, DataLayout DL) { - return std::unique_ptr<LLJIT>( - new LLJIT(std::move(ES), std::move(TM), std::move(DL))); +LLJIT::Create(JITTargetMachineBuilder JTMB, DataLayout DL, + unsigned NumCompileThreads) { + + if (NumCompileThreads == 0) { + // If NumCompileThreads == 0 then create a single-threaded LLJIT instance. + auto TM = JTMB.createTargetMachine(); + if (!TM) + return TM.takeError(); + return std::unique_ptr<LLJIT>(new LLJIT(llvm::make_unique<ExecutionSession>(), + std::move(*TM), std::move(DL))); + } + + return std::unique_ptr<LLJIT>(new LLJIT(llvm::make_unique<ExecutionSession>(), + std::move(JTMB), std::move(DL), + NumCompileThreads)); } Error LLJIT::defineAbsolute(StringRef Name, JITEvaluatedSymbol Sym) { - auto InternedName = ES->getSymbolStringPool().intern(Name); + auto InternedName = ES->intern(Name); SymbolMap Symbols({{InternedName, Sym}}); return Main.define(absoluteSymbols(std::move(Symbols))); } -Error LLJIT::addIRModule(VSO &V, std::unique_ptr<Module> M) { - assert(M && "Can not add null module"); +Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM) { + assert(TSM && "Can not add null module"); - if (auto Err = applyDataLayout(*M)) + if (auto Err = applyDataLayout(*TSM.getModule())) return Err; - auto K = ES->allocateVModule(); - return CompileLayer.add(V, K, std::move(M)); + return CompileLayer.add(JD, std::move(TSM), ES->allocateVModule()); +} + +Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) { + assert(Obj && "Can not add null object"); + + return ObjLinkingLayer.add(JD, std::move(Obj), ES->allocateVModule()); } -Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(VSO &V, +Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD, StringRef Name) { - return llvm::orc::lookup({&V}, ES->getSymbolStringPool().intern(Name)); + return ES->lookup(JITDylibSearchList({{&JD, true}}), ES->intern(Name)); } LLJIT::LLJIT(std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM, DataLayout DL) - : ES(std::move(ES)), Main(this->ES->createVSO("main")), TM(std::move(TM)), - DL(std::move(DL)), - ObjLinkingLayer(*this->ES, - [this](VModuleKey K) { return getMemoryManager(K); }), - CompileLayer(*this->ES, ObjLinkingLayer, SimpleCompiler(*this->TM)), + : ES(std::move(ES)), Main(this->ES->getMainJITDylib()), DL(std::move(DL)), + ObjLinkingLayer( + *this->ES, + []() { return llvm::make_unique<SectionMemoryManager>(); }), + CompileLayer(*this->ES, ObjLinkingLayer, + TMOwningSimpleCompiler(std::move(TM))), CtorRunner(Main), DtorRunner(Main) {} -std::shared_ptr<RuntimeDyld::MemoryManager> -LLJIT::getMemoryManager(VModuleKey K) { - return llvm::make_unique<SectionMemoryManager>(); +LLJIT::LLJIT(std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, + DataLayout DL, unsigned NumCompileThreads) + : ES(std::move(ES)), Main(this->ES->getMainJITDylib()), DL(std::move(DL)), + ObjLinkingLayer( + *this->ES, + []() { return llvm::make_unique<SectionMemoryManager>(); }), + CompileLayer(*this->ES, ObjLinkingLayer, + ConcurrentIRCompiler(std::move(JTMB))), + CtorRunner(Main), DtorRunner(Main) { + assert(NumCompileThreads != 0 && + "Multithreaded LLJIT instance can not be created with 0 threads"); + + // Move modules to new contexts when they're emitted so that we can compile + // them in parallel. + CompileLayer.setCloneToNewContextOnEmit(true); + + // Create a thread pool to compile on and set the execution session + // dispatcher to use the thread pool. + CompileThreads = llvm::make_unique<ThreadPool>(NumCompileThreads); + this->ES->setDispatchMaterialization( + [this](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) { + // FIXME: Switch to move capture once we have c++14. + auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU)); + auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); }; + CompileThreads->async(std::move(Work)); + }); } std::string LLJIT::mangle(StringRef UnmangledName) { @@ -84,16 +144,15 @@ void LLJIT::recordCtorDtors(Module &M) { } Expected<std::unique_ptr<LLLazyJIT>> -LLLazyJIT::Create(std::unique_ptr<ExecutionSession> ES, - std::unique_ptr<TargetMachine> TM, DataLayout DL, - LLVMContext &Ctx) { - const Triple &TT = TM->getTargetTriple(); +LLLazyJIT::Create(JITTargetMachineBuilder JTMB, DataLayout DL, + JITTargetAddress ErrorAddr, unsigned NumCompileThreads) { + auto ES = llvm::make_unique<ExecutionSession>(); - auto CCMgr = createLocalCompileCallbackManager(TT, *ES, 0); - if (!CCMgr) - return make_error<StringError>( - std::string("No callback manager available for ") + TT.str(), - inconvertibleErrorCode()); + const Triple &TT = JTMB.getTargetTriple(); + + auto LCTMgr = createLocalLazyCallThroughManager(TT, *ES, ErrorAddr); + if (!LCTMgr) + return LCTMgr.takeError(); auto ISMBuilder = createLocalIndirectStubsManagerBuilder(TT); if (!ISMBuilder) @@ -101,34 +160,51 @@ LLLazyJIT::Create(std::unique_ptr<ExecutionSession> ES, std::string("No indirect stubs manager builder for ") + TT.str(), inconvertibleErrorCode()); - return std::unique_ptr<LLLazyJIT>( - new LLLazyJIT(std::move(ES), std::move(TM), std::move(DL), Ctx, - std::move(CCMgr), std::move(ISMBuilder))); + if (NumCompileThreads == 0) { + auto TM = JTMB.createTargetMachine(); + if (!TM) + return TM.takeError(); + return std::unique_ptr<LLLazyJIT>( + new LLLazyJIT(std::move(ES), std::move(*TM), std::move(DL), + std::move(*LCTMgr), std::move(ISMBuilder))); + } + + return std::unique_ptr<LLLazyJIT>(new LLLazyJIT( + std::move(ES), std::move(JTMB), std::move(DL), NumCompileThreads, + std::move(*LCTMgr), std::move(ISMBuilder))); } -Error LLLazyJIT::addLazyIRModule(VSO &V, std::unique_ptr<Module> M) { - assert(M && "Can not add null module"); +Error LLLazyJIT::addLazyIRModule(JITDylib &JD, ThreadSafeModule TSM) { + assert(TSM && "Can not add null module"); - if (auto Err = applyDataLayout(*M)) + if (auto Err = applyDataLayout(*TSM.getModule())) return Err; - makeAllSymbolsExternallyAccessible(*M); + recordCtorDtors(*TSM.getModule()); - recordCtorDtors(*M); - - auto K = ES->allocateVModule(); - return CODLayer.add(V, K, std::move(M)); + return CODLayer.add(JD, std::move(TSM), ES->allocateVModule()); } LLLazyJIT::LLLazyJIT( std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM, - DataLayout DL, LLVMContext &Ctx, - std::unique_ptr<JITCompileCallbackManager> CCMgr, + DataLayout DL, std::unique_ptr<LazyCallThroughManager> LCTMgr, std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder) : LLJIT(std::move(ES), std::move(TM), std::move(DL)), - CCMgr(std::move(CCMgr)), TransformLayer(*this->ES, CompileLayer), - CODLayer(*this->ES, TransformLayer, *this->CCMgr, std::move(ISMBuilder), - [&]() -> LLVMContext & { return Ctx; }) {} + LCTMgr(std::move(LCTMgr)), TransformLayer(*this->ES, CompileLayer), + CODLayer(*this->ES, TransformLayer, *this->LCTMgr, + std::move(ISMBuilder)) {} + +LLLazyJIT::LLLazyJIT( + std::unique_ptr<ExecutionSession> ES, JITTargetMachineBuilder JTMB, + DataLayout DL, unsigned NumCompileThreads, + std::unique_ptr<LazyCallThroughManager> LCTMgr, + std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder) + : LLJIT(std::move(ES), std::move(JTMB), std::move(DL), NumCompileThreads), + LCTMgr(std::move(LCTMgr)), TransformLayer(*this->ES, CompileLayer), + CODLayer(*this->ES, TransformLayer, *this->LCTMgr, + std::move(ISMBuilder)) { + CODLayer.setCloneToNewContextOnEmit(true); +} } // End namespace orc. } // End namespace llvm. |