diff --git a/include/llvm/ExecutionEngine/ExecutionEngine.h b/include/llvm/ExecutionEngine/ExecutionEngine.h index b9c0b61b6033..d79bd3c4dfc8 100644 --- a/include/llvm/ExecutionEngine/ExecutionEngine.h +++ b/include/llvm/ExecutionEngine/ExecutionEngine.h @@ -138,10 +138,11 @@ class ExecutionEngine { /// getMemoryforGV - Allocate memory for a global variable. virtual char *getMemoryForGV(const GlobalVariable *GV); - static ExecutionEngine *(*MCJITCtor)(std::unique_ptr M, - std::string *ErrorStr, - RTDyldMemoryManager *MCJMM, - std::unique_ptr TM); + static ExecutionEngine *(*MCJITCtor)( + std::unique_ptr M, + std::string *ErrorStr, + std::unique_ptr MCJMM, + std::unique_ptr TM); static ExecutionEngine *(*InterpCtor)(std::unique_ptr M, std::string *ErrorStr); @@ -492,7 +493,7 @@ class EngineBuilder { EngineKind::Kind WhichEngine; std::string *ErrorStr; CodeGenOpt::Level OptLevel; - RTDyldMemoryManager *MCJMM; + std::unique_ptr MCJMM; TargetOptions Options; Reloc::Model RelocModel; CodeModel::Model CMModel; @@ -506,9 +507,10 @@ class EngineBuilder { public: /// Constructor for EngineBuilder. - EngineBuilder(std::unique_ptr M) : M(std::move(M)) { - InitEngine(); - } + EngineBuilder(std::unique_ptr M); + + // Out-of-line since we don't have the def'n of RTDyldMemoryManager here. + ~EngineBuilder(); /// setEngineKind - Controls whether the user wants the interpreter, the JIT, /// or whichever engine works. This option defaults to EngineKind::Either. @@ -523,10 +525,7 @@ class EngineBuilder { /// to create anything other than MCJIT will cause a runtime error. If create() /// is called and is successful, the created engine takes ownership of the /// memory manager. This option defaults to NULL. - EngineBuilder &setMCJITMemoryManager(RTDyldMemoryManager *mcjmm) { - MCJMM = mcjmm; - return *this; - } + EngineBuilder &setMCJITMemoryManager(std::unique_ptr mcjmm); /// setErrorStr - Set the error string to write to on error. This option /// defaults to NULL. diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp index 9ca0330d792c..32d20ea0e304 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -44,7 +44,8 @@ STATISTIC(NumGlobals , "Number of global vars initialized"); ExecutionEngine *(*ExecutionEngine::MCJITCtor)( std::unique_ptr M, std::string *ErrorStr, - RTDyldMemoryManager *MCJMM, std::unique_ptr TM) = nullptr; + std::unique_ptr MCJMM, + std::unique_ptr TM) = nullptr; ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr M, std::string *ErrorStr) =nullptr; @@ -392,6 +393,19 @@ int ExecutionEngine::runFunctionAsMain(Function *Fn, return runFunction(Fn, GVArgs).IntVal.getZExtValue(); } +EngineBuilder::EngineBuilder(std::unique_ptr M) + : M(std::move(M)), MCJMM(nullptr) { + InitEngine(); +} + +EngineBuilder::~EngineBuilder() {} + +EngineBuilder &EngineBuilder::setMCJITMemoryManager( + std::unique_ptr mcjmm) { + MCJMM = std::move(mcjmm); + return *this; +} + void EngineBuilder::InitEngine() { WhichEngine = EngineKind::Either; ErrorStr = nullptr; @@ -443,7 +457,7 @@ ExecutionEngine *EngineBuilder::create(TargetMachine *TM) { ExecutionEngine *EE = nullptr; if (ExecutionEngine::MCJITCtor) - EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, MCJMM, + EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MCJMM), std::move(TheTM)); if (EE) { EE->setVerifyModules(VerifyModules); diff --git a/lib/ExecutionEngine/ExecutionEngineBindings.cpp b/lib/ExecutionEngine/ExecutionEngineBindings.cpp index 58271df85a3a..7fc72ae19c52 100644 --- a/lib/ExecutionEngine/ExecutionEngineBindings.cpp +++ b/lib/ExecutionEngine/ExecutionEngineBindings.cpp @@ -188,7 +188,8 @@ LLVMBool LLVMCreateMCJITCompilerForModule( .setCodeModel(unwrap(options.CodeModel)) .setTargetOptions(targetOptions); if (options.MCJMM) - builder.setMCJITMemoryManager(unwrap(options.MCJMM)); + builder.setMCJITMemoryManager( + std::unique_ptr(unwrap(options.MCJMM))); if (ExecutionEngine *JIT = builder.create()) { *OutJIT = wrap(JIT); return 0; diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 58cf4e5e6dd8..f2d53f5326d1 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -45,21 +45,24 @@ extern "C" void LLVMLinkInMCJIT() { ExecutionEngine *MCJIT::createJIT(std::unique_ptr M, std::string *ErrorStr, - RTDyldMemoryManager *MemMgr, + std::unique_ptr MemMgr, std::unique_ptr TM) { // Try to register the program as a source of symbols to resolve against. // // FIXME: Don't do this here. sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr); - return new MCJIT(std::move(M), std::move(TM), - MemMgr ? MemMgr : new SectionMemoryManager()); + std::unique_ptr MM = std::move(MemMgr); + if (!MM) + MM = std::unique_ptr(new SectionMemoryManager()); + + return new MCJIT(std::move(M), std::move(TM), std::move(MM)); } MCJIT::MCJIT(std::unique_ptr M, std::unique_ptr tm, - RTDyldMemoryManager *MM) + std::unique_ptr MM) : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr), - MemMgr(this, MM), Dyld(&MemMgr), ObjCache(nullptr) { + MemMgr(this, std::move(MM)), Dyld(&MemMgr), ObjCache(nullptr) { // FIXME: We are managing our modules, so we do not want the base class // ExecutionEngine to manage them as well. To avoid double destruction // of the first (and only) module added in ExecutionEngine constructor diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.h b/lib/ExecutionEngine/MCJIT/MCJIT.h index 6f92e51b64c8..f55dd60ecba9 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.h +++ b/lib/ExecutionEngine/MCJIT/MCJIT.h @@ -28,8 +28,9 @@ class MCJIT; // to that object. class LinkingMemoryManager : public RTDyldMemoryManager { public: - LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM) - : ParentEngine(Parent), ClientMM(MM) {} + LinkingMemoryManager(MCJIT *Parent, + std::unique_ptr MM) + : ParentEngine(Parent), ClientMM(std::move(MM)) {} uint64_t getSymbolAddress(const std::string &Name) override; @@ -102,7 +103,7 @@ class LinkingMemoryManager : public RTDyldMemoryManager { class MCJIT : public ExecutionEngine { MCJIT(std::unique_ptr M, std::unique_ptr tm, - RTDyldMemoryManager *MemMgr); + std::unique_ptr MemMgr); typedef llvm::SmallPtrSet ModulePtrSet; @@ -325,7 +326,7 @@ class MCJIT : public ExecutionEngine { static ExecutionEngine *createJIT(std::unique_ptr M, std::string *ErrorStr, - RTDyldMemoryManager *MemMgr, + std::unique_ptr MemMgr, std::unique_ptr TM); // @} diff --git a/tools/lli/lli.cpp b/tools/lli/lli.cpp index feaffef15f52..730911b07c65 100644 --- a/tools/lli/lli.cpp +++ b/tools/lli/lli.cpp @@ -433,7 +433,11 @@ int main(int argc, char **argv, char * const *envp) { RTDyldMM = new RemoteMemoryManager(); else RTDyldMM = new SectionMemoryManager(); - builder.setMCJITMemoryManager(RTDyldMM); + + // Deliberately construct a temp std::unique_ptr to pass in. Do not null out + // RTDyldMM: We still use it below, even though we don't own it. + builder.setMCJITMemoryManager( + std::unique_ptr(RTDyldMM)); } else if (RemoteMCJIT) { errs() << "error: Remote process execution does not work with the " "interpreter.\n"; diff --git a/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp b/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp index 2736383bb755..2e38dd88b0fd 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp +++ b/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp @@ -163,7 +163,7 @@ TEST_F(MCJITObjectCacheTest, VerifyLoadFromCache) { TheJIT.reset(); // Create a new memory manager. - MM = new SectionMemoryManager; + MM.reset(new SectionMemoryManager()); // Create a new module and save it. Use a different return code so we can // tell if MCJIT compiled this module or used the cache. @@ -197,7 +197,7 @@ TEST_F(MCJITObjectCacheTest, VerifyNonLoadFromCache) { TheJIT.reset(); // Create a new memory manager. - MM = new SectionMemoryManager; + MM.reset(new SectionMemoryManager()); // Create a new module and save it. Use a different return code so we can // tell if MCJIT compiled this module or used the cache. Note that we use diff --git a/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h b/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h index eea88bbe3f43..35af417bf147 100644 --- a/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h +++ b/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h @@ -325,7 +325,7 @@ class MCJITTestBase : public MCJITTestAPICommon, public TrivialModuleBuilder { EngineBuilder EB(std::move(M)); std::string Error; TheJIT.reset(EB.setEngineKind(EngineKind::JIT) - .setMCJITMemoryManager(MM) + .setMCJITMemoryManager(std::move(MM)) .setErrorStr(&Error) .setOptLevel(CodeGenOpt::None) .setCodeModel(CodeModel::JITDefault) @@ -344,7 +344,7 @@ class MCJITTestBase : public MCJITTestAPICommon, public TrivialModuleBuilder { StringRef MArch; SmallVector MAttrs; std::unique_ptr TheJIT; - RTDyldMemoryManager *MM; + std::unique_ptr MM; std::unique_ptr M; };