[llvm] r215967 - Make it explicit that ExecutionEngine takes ownership of the modules.

Rafael Espindola rafael.espindola at gmail.com
Mon Aug 18 21:04:25 PDT 2014


Author: rafael
Date: Mon Aug 18 23:04:25 2014
New Revision: 215967

URL: http://llvm.org/viewvc/llvm-project?rev=215967&view=rev
Log:
Make it explicit that ExecutionEngine takes ownership of the modules.

Modified:
    llvm/trunk/examples/BrainF/BrainFDriver.cpp
    llvm/trunk/examples/ExceptionDemo/ExceptionDemo.cpp
    llvm/trunk/examples/Fibonacci/fibonacci.cpp
    llvm/trunk/examples/HowToUseJIT/HowToUseJIT.cpp
    llvm/trunk/examples/Kaleidoscope/Chapter4/toy.cpp
    llvm/trunk/examples/Kaleidoscope/Chapter5/toy.cpp
    llvm/trunk/examples/Kaleidoscope/Chapter6/toy.cpp
    llvm/trunk/examples/Kaleidoscope/Chapter7/toy.cpp
    llvm/trunk/examples/ParallelJIT/ParallelJIT.cpp
    llvm/trunk/include/llvm/ExecutionEngine/ExecutionEngine.h
    llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp
    llvm/trunk/lib/ExecutionEngine/ExecutionEngineBindings.cpp
    llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.cpp
    llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.h
    llvm/trunk/lib/ExecutionEngine/JIT/JIT.cpp
    llvm/trunk/lib/ExecutionEngine/JIT/JIT.h
    llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.cpp
    llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.h
    llvm/trunk/tools/lli/lli.cpp
    llvm/trunk/unittests/ExecutionEngine/ExecutionEngineTest.cpp
    llvm/trunk/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp
    llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp
    llvm/trunk/unittests/ExecutionEngine/JIT/MultiJITTest.cpp
    llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp
    llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
    llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
    llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h

Modified: llvm/trunk/examples/BrainF/BrainFDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/BrainF/BrainFDriver.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/BrainF/BrainFDriver.cpp (original)
+++ llvm/trunk/examples/BrainF/BrainFDriver.cpp Mon Aug 18 23:04:25 2014
@@ -125,13 +125,13 @@ int main(int argc, char **argv) {
 
   //Read the BrainF program
   BrainF bf;
-  Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
+  std::unique_ptr<Module> Mod(bf.parse(in, 65536, cf, Context)); // 64 KiB
   if (in != &std::cin)
     delete in;
-  addMainFunction(mod);
+  addMainFunction(Mod.get());
 
   //Verify generated code
-  if (verifyModule(*mod)) {
+  if (verifyModule(*Mod)) {
     errs() << "Error: module failed verification.  This shouldn't happen.\n";
     abort();
   }
@@ -141,18 +141,18 @@ int main(int argc, char **argv) {
     InitializeNativeTarget();
 
     outs() << "------- Running JIT -------\n";
-    ExecutionEngine *ee = EngineBuilder(mod).create();
+    Module &M = *Mod;
+    ExecutionEngine *ee = EngineBuilder(std::move(Mod)).create();
     std::vector<GenericValue> args;
-    Function *brainf_func = mod->getFunction("brainf");
+    Function *brainf_func = M.getFunction("brainf");
     GenericValue gv = ee->runFunction(brainf_func, args);
   } else {
-    WriteBitcodeToFile(mod, *out);
+    WriteBitcodeToFile(Mod.get(), *out);
   }
 
   //Clean up
   if (out != &outs())
     delete out;
-  delete mod;
 
   llvm_shutdown();
 

Modified: llvm/trunk/examples/ExceptionDemo/ExceptionDemo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/ExceptionDemo/ExceptionDemo.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/ExceptionDemo/ExceptionDemo.cpp (original)
+++ llvm/trunk/examples/ExceptionDemo/ExceptionDemo.cpp Mon Aug 18 23:04:25 2014
@@ -1957,12 +1957,14 @@ int main(int argc, char *argv[]) {
   llvm::IRBuilder<> theBuilder(context);
 
   // Make the module, which holds all the code.
-  llvm::Module *module = new llvm::Module("my cool jit", context);
+  std::unique_ptr<llvm::Module> Owner =
+      llvm::make_unique<llvm::Module>("my cool jit", context);
+  llvm::Module *module = Owner.get();
 
   llvm::RTDyldMemoryManager *MemMgr = new llvm::SectionMemoryManager();
 
   // Build engine with JIT
-  llvm::EngineBuilder factory(module);
+  llvm::EngineBuilder factory(std::move(Owner));
   factory.setEngineKind(llvm::EngineKind::JIT);
   factory.setAllocateGVsWithCode(false);
   factory.setTargetOptions(Opts);

Modified: llvm/trunk/examples/Fibonacci/fibonacci.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Fibonacci/fibonacci.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/Fibonacci/fibonacci.cpp (original)
+++ llvm/trunk/examples/Fibonacci/fibonacci.cpp Mon Aug 18 23:04:25 2014
@@ -96,15 +96,16 @@ int main(int argc, char **argv) {
   LLVMContext Context;
 
   // Create some module to put our function into it.
-  std::unique_ptr<Module> M(new Module("test", Context));
+  std::unique_ptr<Module> Owner(new Module("test", Context));
+  Module *M = Owner.get();
 
   // We are about to create the "fib" function:
-  Function *FibF = CreateFibFunction(M.get(), Context);
+  Function *FibF = CreateFibFunction(M, Context);
 
   // Now we going to create JIT
   std::string errStr;
   ExecutionEngine *EE =
-    EngineBuilder(M.get())
+    EngineBuilder(std::move(Owner))
     .setErrorStr(&errStr)
     .setEngineKind(EngineKind::JIT)
     .create();

Modified: llvm/trunk/examples/HowToUseJIT/HowToUseJIT.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/HowToUseJIT/HowToUseJIT.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/HowToUseJIT/HowToUseJIT.cpp (original)
+++ llvm/trunk/examples/HowToUseJIT/HowToUseJIT.cpp Mon Aug 18 23:04:25 2014
@@ -56,7 +56,8 @@ int main() {
   LLVMContext Context;
   
   // Create some module to put our function into it.
-  Module *M = new Module("test", Context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
+  Module *M = Owner.get();
 
   // Create the add1 function entry and insert this entry into module M.  The
   // function will have a return type of "int" and take an argument of "int".
@@ -114,7 +115,7 @@ int main() {
   builder.CreateRet(Add1CallRes);
 
   // Now we create the JIT.
-  ExecutionEngine* EE = EngineBuilder(M).create();
+  ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
 
   outs() << "We just constructed this LLVM module:\n\n" << *M;
   outs() << "\n\nRunning foo: ";

Modified: llvm/trunk/examples/Kaleidoscope/Chapter4/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Chapter4/toy.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Chapter4/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Chapter4/toy.cpp Mon Aug 18 23:04:25 2014
@@ -572,11 +572,13 @@ int main() {
   getNextToken();
 
   // Make the module, which holds all the code.
-  TheModule = new Module("my cool jit", Context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
+  TheModule = Owner.get();
 
   // Create the JIT.  This takes ownership of the module.
   std::string ErrStr;
-  TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
+  TheExecutionEngine =
+      EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
   if (!TheExecutionEngine) {
     fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
     exit(1);

Modified: llvm/trunk/examples/Kaleidoscope/Chapter5/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Chapter5/toy.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Chapter5/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Chapter5/toy.cpp Mon Aug 18 23:04:25 2014
@@ -817,11 +817,13 @@ int main() {
   getNextToken();
 
   // Make the module, which holds all the code.
-  TheModule = new Module("my cool jit", Context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
+  TheModule = Owner.get();
 
   // Create the JIT.  This takes ownership of the module.
   std::string ErrStr;
-  TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
+  TheExecutionEngine =
+      EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
   if (!TheExecutionEngine) {
     fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
     exit(1);

Modified: llvm/trunk/examples/Kaleidoscope/Chapter6/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Chapter6/toy.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Chapter6/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Chapter6/toy.cpp Mon Aug 18 23:04:25 2014
@@ -935,11 +935,13 @@ int main() {
   getNextToken();
 
   // Make the module, which holds all the code.
-  TheModule = new Module("my cool jit", Context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
+  TheModule = Owner.get();
 
   // Create the JIT.  This takes ownership of the module.
   std::string ErrStr;
-  TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
+  TheExecutionEngine =
+      EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
   if (!TheExecutionEngine) {
     fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
     exit(1);

Modified: llvm/trunk/examples/Kaleidoscope/Chapter7/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Chapter7/toy.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Chapter7/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Chapter7/toy.cpp Mon Aug 18 23:04:25 2014
@@ -1099,11 +1099,13 @@ int main() {
   getNextToken();
 
   // Make the module, which holds all the code.
-  TheModule = new Module("my cool jit", Context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
+  TheModule = Owner.get();
 
   // Create the JIT.  This takes ownership of the module.
   std::string ErrStr;
-  TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
+  TheExecutionEngine =
+      EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
   if (!TheExecutionEngine) {
     fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
     exit(1);

Modified: llvm/trunk/examples/ParallelJIT/ParallelJIT.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/ParallelJIT/ParallelJIT.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/examples/ParallelJIT/ParallelJIT.cpp (original)
+++ llvm/trunk/examples/ParallelJIT/ParallelJIT.cpp Mon Aug 18 23:04:25 2014
@@ -243,13 +243,14 @@ int main() {
   LLVMContext Context;
 
   // Create some module to put our function into it.
-  Module *M = new Module("test", Context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
+  Module *M = Owner.get();
 
   Function* add1F = createAdd1( M );
   Function* fibF = CreateFibFunction( M );
 
   // Now we create the JIT.
-  ExecutionEngine* EE = EngineBuilder(M).create();
+  ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
 
   //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
   //~ std::cout << "\n\nRunning foo: " << std::flush;

Modified: llvm/trunk/include/llvm/ExecutionEngine/ExecutionEngine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/ExecutionEngine.h?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ExecutionEngine/ExecutionEngine.h (original)
+++ llvm/trunk/include/llvm/ExecutionEngine/ExecutionEngine.h Mon Aug 18 23:04:25 2014
@@ -18,6 +18,7 @@
 #include "llvm-c/ExecutionEngine.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/IR/Module.h"
 #include "llvm/IR/ValueHandle.h"
 #include "llvm/IR/ValueMap.h"
 #include "llvm/MC/MCCodeGenInfo.h"
@@ -41,7 +42,6 @@ class GlobalValue;
 class JITEventListener;
 class JITMemoryManager;
 class MachineCodeInfo;
-class Module;
 class MutexGuard;
 class ObjectCache;
 class RTDyldMemoryManager;
@@ -131,7 +131,7 @@ class ExecutionEngine {
 protected:
   /// The list of Modules that we are JIT'ing from.  We use a SmallVector to
   /// optimize for the case where there is only one module.
-  SmallVector<Module*, 1> Modules;
+  SmallVector<std::unique_ptr<Module>, 1> Modules;
 
   void setDataLayout(const DataLayout *Val) { DL = Val; }
 
@@ -142,17 +142,18 @@ protected:
   // libraries, the execution engine implementations set these functions to ctor
   // pointers at startup time if they are linked in.
   static ExecutionEngine *(*JITCtor)(
-    Module *M,
+    std::unique_ptr<Module> M,
     std::string *ErrorStr,
     JITMemoryManager *JMM,
     bool GVsWithCode,
     TargetMachine *TM);
   static ExecutionEngine *(*MCJITCtor)(
-    Module *M,
+    std::unique_ptr<Module> M,
     std::string *ErrorStr,
     RTDyldMemoryManager *MCJMM,
     TargetMachine *TM);
-  static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
+  static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
+                                        std::string *ErrorStr);
 
   /// LazyFunctionCreator - If an unknown function is needed, this function
   /// pointer is invoked to create it.  If this returns null, the JIT will
@@ -171,11 +172,9 @@ public:
 
   virtual ~ExecutionEngine();
 
-  /// addModule - Add a Module to the list of modules that we can JIT from.
-  /// Note that this takes ownership of the Module: when the ExecutionEngine is
-  /// destroyed, it destroys the Module as well.
-  virtual void addModule(Module *M) {
-    Modules.push_back(M);
+  /// Add a Module to the list of modules that we can JIT from.
+  virtual void addModule(std::unique_ptr<Module> M) {
+    Modules.push_back(std::move(M));
   }
 
   /// addObjectFile - Add an ObjectFile to the execution engine.
@@ -274,11 +273,11 @@ public:
   /// \param isDtors - Run the destructors instead of constructors.
   virtual void runStaticConstructorsDestructors(bool isDtors);
 
-  /// runStaticConstructorsDestructors - This method is used to execute all of
-  /// the static constructors or destructors for a particular module.
+  /// This method is used to execute all of the static constructors or
+  /// destructors for a particular module.
   ///
   /// \param isDtors - Run the destructors instead of constructors.
-  void runStaticConstructorsDestructors(Module *module, bool isDtors);
+  void runStaticConstructorsDestructors(Module &module, bool isDtors);
 
 
   /// runFunctionAsMain - This is a helper function which wraps runFunction to
@@ -506,7 +505,7 @@ public:
   }
 
 protected:
-  explicit ExecutionEngine(Module *M);
+  explicit ExecutionEngine(std::unique_ptr<Module> M);
 
   void emitGlobals();
 
@@ -526,12 +525,12 @@ namespace EngineKind {
   const static Kind Either = (Kind)(JIT | Interpreter);
 }
 
-/// EngineBuilder - Builder class for ExecutionEngines.  Use this by
-/// stack-allocating a builder, chaining the various set* methods, and
-/// terminating it with a .create() call.
+/// Builder class for ExecutionEngines. Use this by stack-allocating a builder,
+/// chaining the various set* methods, and terminating it with a .create()
+/// call.
 class EngineBuilder {
 private:
-  Module *M;
+  std::unique_ptr<Module> M;
   EngineKind::Kind WhichEngine;
   std::string *ErrorStr;
   CodeGenOpt::Level OptLevel;
@@ -551,9 +550,8 @@ private:
   void InitEngine();
 
 public:
-  /// EngineBuilder - Constructor for EngineBuilder.  If create() is called and
-  /// is successful, the created engine takes ownership of the module.
-  EngineBuilder(Module *m) : M(m) {
+  /// Constructor for EngineBuilder.
+  EngineBuilder(std::unique_ptr<Module> M) : M(std::move(M)) {
     InitEngine();
   }
 

Modified: llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/ExecutionEngine.cpp Mon Aug 18 23:04:25 2014
@@ -49,20 +49,20 @@ void ObjectBuffer::anchor() {}
 void ObjectBufferStream::anchor() {}
 
 ExecutionEngine *(*ExecutionEngine::JITCtor)(
-  Module *M,
+  std::unique_ptr<Module> M,
   std::string *ErrorStr,
   JITMemoryManager *JMM,
   bool GVsWithCode,
   TargetMachine *TM) = nullptr;
 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
-  Module *M,
+  std::unique_ptr<Module >M,
   std::string *ErrorStr,
   RTDyldMemoryManager *MCJMM,
   TargetMachine *TM) = nullptr;
-ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
+ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
                                                 std::string *ErrorStr) =nullptr;
 
-ExecutionEngine::ExecutionEngine(Module *M)
+ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
   : EEState(*this),
     LazyFunctionCreator(nullptr) {
   CompilingLazily         = false;
@@ -77,14 +77,12 @@ ExecutionEngine::ExecutionEngine(Module
   VerifyModules = false;
 #endif
 
-  Modules.push_back(M);
   assert(M && "Module is null?");
+  Modules.push_back(std::move(M));
 }
 
 ExecutionEngine::~ExecutionEngine() {
   clearAllGlobalMappings();
-  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
-    delete Modules[i];
 }
 
 namespace {
@@ -131,10 +129,10 @@ void ExecutionEngine::addArchive(std::un
 }
 
 bool ExecutionEngine::removeModule(Module *M) {
-  for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
-        E = Modules.end(); I != E; ++I) {
-    Module *Found = *I;
+  for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
+    Module *Found = I->get();
     if (Found == M) {
+      I->release();
       Modules.erase(I);
       clearGlobalMappingsFromModule(M);
       return true;
@@ -307,10 +305,10 @@ void *ArgvArray::reset(LLVMContext &C, E
   return Array;
 }
 
-void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
+void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
                                                        bool isDtors) {
   const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
-  GlobalVariable *GV = module->getNamedGlobal(Name);
+  GlobalVariable *GV = module.getNamedGlobal(Name);
 
   // If this global has internal linkage, or if it has a use, then it must be
   // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
@@ -348,8 +346,8 @@ void ExecutionEngine::runStaticConstruct
 
 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
   // Execute global ctors/dtors for each module in the program.
-  for (Module *M : Modules)
-    runStaticConstructorsDestructors(M, isDtors);
+  for (std::unique_ptr<Module> &M : Modules)
+    runStaticConstructorsDestructors(*M, isDtors);
 }
 
 #ifndef NDEBUG
@@ -474,10 +472,10 @@ ExecutionEngine *EngineBuilder::create(T
 
     ExecutionEngine *EE = nullptr;
     if (UseMCJIT && ExecutionEngine::MCJITCtor)
-      EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
-                                      TheTM.release());
+      EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
+                                      MCJMM ? MCJMM : JMM, TheTM.release());
     else if (ExecutionEngine::JITCtor)
-      EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
+      EE = ExecutionEngine::JITCtor(std::move(M), ErrorStr, JMM,
                                     AllocateGVsWithCode, TheTM.release());
 
     if (EE) {
@@ -490,7 +488,7 @@ ExecutionEngine *EngineBuilder::create(T
   // an interpreter instead.
   if (WhichEngine & EngineKind::Interpreter) {
     if (ExecutionEngine::InterpCtor)
-      return ExecutionEngine::InterpCtor(M, ErrorStr);
+      return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
     if (ErrorStr)
       *ErrorStr = "Interpreter has not been linked in.";
     return nullptr;

Modified: llvm/trunk/lib/ExecutionEngine/ExecutionEngineBindings.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/ExecutionEngineBindings.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/ExecutionEngineBindings.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/ExecutionEngineBindings.cpp Mon Aug 18 23:04:25 2014
@@ -110,7 +110,7 @@ LLVMBool LLVMCreateExecutionEngineForMod
                                             LLVMModuleRef M,
                                             char **OutError) {
   std::string Error;
-  EngineBuilder builder(unwrap(M));
+  EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
   builder.setEngineKind(EngineKind::Either)
          .setErrorStr(&Error);
   if (ExecutionEngine *EE = builder.create()){
@@ -125,7 +125,7 @@ LLVMBool LLVMCreateInterpreterForModule(
                                         LLVMModuleRef M,
                                         char **OutError) {
   std::string Error;
-  EngineBuilder builder(unwrap(M));
+  EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
   builder.setEngineKind(EngineKind::Interpreter)
          .setErrorStr(&Error);
   if (ExecutionEngine *Interp = builder.create()) {
@@ -141,7 +141,7 @@ LLVMBool LLVMCreateJITCompilerForModule(
                                         unsigned OptLevel,
                                         char **OutError) {
   std::string Error;
-  EngineBuilder builder(unwrap(M));
+  EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
   builder.setEngineKind(EngineKind::JIT)
          .setErrorStr(&Error)
          .setOptLevel((CodeGenOpt::Level)OptLevel);
@@ -189,7 +189,7 @@ LLVMBool LLVMCreateMCJITCompilerForModul
   targetOptions.EnableFastISel = options.EnableFastISel;
 
   std::string Error;
-  EngineBuilder builder(unwrap(M));
+  EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
   builder.setEngineKind(EngineKind::JIT)
          .setErrorStr(&Error)
          .setUseMCJIT(true)
@@ -279,7 +279,7 @@ void LLVMFreeMachineCodeForFunction(LLVM
 }
 
 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
-  unwrap(EE)->addModule(unwrap(M));
+  unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
 }
 
 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){

Modified: llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.cpp Mon Aug 18 23:04:25 2014
@@ -30,9 +30,10 @@ static struct RegisterInterp {
 
 extern "C" void LLVMLinkInInterpreter() { }
 
-/// create - Create a new interpreter object.  This can never fail.
+/// Create a new interpreter object.
 ///
-ExecutionEngine *Interpreter::create(Module *M, std::string* ErrStr) {
+ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
+                                     std::string *ErrStr) {
   // Tell this Module to materialize everything and release the GVMaterializer.
   if (std::error_code EC = M->materializeAllPermanently()) {
     if (ErrStr)
@@ -41,15 +42,15 @@ ExecutionEngine *Interpreter::create(Mod
     return nullptr;
   }
 
-  return new Interpreter(M);
+  return new Interpreter(std::move(M));
 }
 
 //===----------------------------------------------------------------------===//
 // Interpreter ctor - Initialize stuff
 //
-Interpreter::Interpreter(Module *M)
-  : ExecutionEngine(M), TD(M) {
-      
+Interpreter::Interpreter(std::unique_ptr<Module> M)
+  : ExecutionEngine(std::move(M)), TD(Modules.back().get()) {
+
   memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
   setDataLayout(&TD);
   // Initialize the "backend"

Modified: llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.h?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.h (original)
+++ llvm/trunk/lib/ExecutionEngine/Interpreter/Interpreter.h Mon Aug 18 23:04:25 2014
@@ -94,7 +94,7 @@ class Interpreter : public ExecutionEngi
   std::vector<Function*> AtExitHandlers;
 
 public:
-  explicit Interpreter(Module *M);
+  explicit Interpreter(std::unique_ptr<Module> M);
   ~Interpreter();
 
   /// runAtExitHandlers - Run any functions registered by the program's calls to
@@ -105,10 +105,11 @@ public:
   static void Register() {
     InterpCtor = create;
   }
-  
-  /// create - Create an interpreter ExecutionEngine. This can never fail.
+
+  /// Create an interpreter ExecutionEngine.
   ///
-  static ExecutionEngine *create(Module *M, std::string *ErrorStr = nullptr);
+  static ExecutionEngine *create(std::unique_ptr<Module> M,
+                                 std::string *ErrorStr = nullptr);
 
   /// run - Start execution with the specified function and arguments.
   ///

Modified: llvm/trunk/lib/ExecutionEngine/JIT/JIT.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JIT/JIT.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JIT/JIT.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/JIT/JIT.cpp Mon Aug 18 23:04:25 2014
@@ -69,10 +69,9 @@ static struct RegisterJIT {
 extern "C" void LLVMLinkInJIT() {
 }
 
-/// createJIT - This is the factory method for creating a JIT for the current
-/// machine, it does not fall back to the interpreter.  This takes ownership
-/// of the module.
-ExecutionEngine *JIT::createJIT(Module *M,
+/// This is the factory method for creating a JIT for the current machine, it
+/// does not fall back to the interpreter.
+ExecutionEngine *JIT::createJIT(std::unique_ptr<Module> M,
                                 std::string *ErrorStr,
                                 JITMemoryManager *JMM,
                                 bool GVsWithCode,
@@ -84,7 +83,7 @@ ExecutionEngine *JIT::createJIT(Module *
 
   // If the target supports JIT code generation, create the JIT.
   if (TargetJITInfo *TJ = TM->getSubtargetImpl()->getJITInfo()) {
-    return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
+    return new JIT(std::move(M), *TM, *TJ, JMM, GVsWithCode);
   } else {
     if (ErrorStr)
       *ErrorStr = "target does not support JIT code generation";
@@ -135,14 +134,15 @@ extern "C" {
   }
 }
 
-JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
+JIT::JIT(std::unique_ptr<Module> M, TargetMachine &tm, TargetJITInfo &tji,
          JITMemoryManager *jmm, bool GVsWithCode)
-  : ExecutionEngine(M), TM(tm), TJI(tji),
+  : ExecutionEngine(std::move(M)), TM(tm), TJI(tji),
     JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
     AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
   setDataLayout(TM.getSubtargetImpl()->getDataLayout());
 
-  jitstate = new JITState(M);
+  Module *Mod = Modules.back().get();
+  jitstate = new JITState(Mod);
 
   // Initialize JCE
   JCE = createEmitter(*this, JMM, TM);
@@ -153,8 +153,8 @@ JIT::JIT(Module *M, TargetMachine &tm, T
   // Add target data
   MutexGuard locked(lock);
   FunctionPassManager &PM = jitstate->getPM();
-  M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
-  PM.add(new DataLayoutPass(M));
+  Mod->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
+  PM.add(new DataLayoutPass(Mod));
 
   // Turn the machine code intermediate representation into bytes in memory that
   // may be executed.
@@ -175,19 +175,19 @@ JIT::~JIT() {
   delete &TM;
 }
 
-/// addModule - Add a new Module to the JIT.  If we previously removed the last
-/// Module, we need re-initialize jitstate with a valid Module.
-void JIT::addModule(Module *M) {
+/// Add a new Module to the JIT. If we previously removed the last Module, we
+/// need re-initialize jitstate with a valid Module.
+void JIT::addModule(std::unique_ptr<Module> M) {
   MutexGuard locked(lock);
 
   if (Modules.empty()) {
     assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
 
-    jitstate = new JITState(M);
+    jitstate = new JITState(M.get());
 
     FunctionPassManager &PM = jitstate->getPM();
     M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
-    PM.add(new DataLayoutPass(M));
+    PM.add(new DataLayoutPass(M.get()));
 
     // Turn the machine code intermediate representation into bytes in memory
     // that may be executed.
@@ -199,11 +199,11 @@ void JIT::addModule(Module *M) {
     PM.doInitialization();
   }
 
-  ExecutionEngine::addModule(M);
+  ExecutionEngine::addModule(std::move(M));
 }
 
-/// removeModule - If we are removing the last Module, invalidate the jitstate
-/// since the PassManager it contains references a released Module.
+///  If we are removing the last Module, invalidate the jitstate since the
+///  PassManager it contains references a released Module.
 bool JIT::removeModule(Module *M) {
   bool result = ExecutionEngine::removeModule(M);
 
@@ -215,7 +215,7 @@ bool JIT::removeModule(Module *M) {
   }
 
   if (!jitstate && !Modules.empty()) {
-    jitstate = new JITState(Modules[0]);
+    jitstate = new JITState(Modules[0].get());
 
     FunctionPassManager &PM = jitstate->getPM();
     M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());

Modified: llvm/trunk/lib/ExecutionEngine/JIT/JIT.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/JIT/JIT.h?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/JIT/JIT.h (original)
+++ llvm/trunk/lib/ExecutionEngine/JIT/JIT.h Mon Aug 18 23:04:25 2014
@@ -78,7 +78,7 @@ class JIT : public ExecutionEngine {
   BasicBlockAddressMapTy BasicBlockAddressMap;
 
 
-  JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
+  JIT(std::unique_ptr<Module> M, TargetMachine &tm, TargetJITInfo &tji,
       JITMemoryManager *JMM, bool AllocateGVsWithCode);
 public:
   ~JIT();
@@ -91,7 +91,7 @@ public:
   ///
   TargetJITInfo &getJITInfo() const { return TJI; }
 
-  void addModule(Module *M) override;
+  void addModule(std::unique_ptr<Module> M) override;
 
   /// removeModule - Remove a Module from the list of modules.  Returns true if
   /// M is found.
@@ -167,7 +167,7 @@ public:
   ///
   JITCodeEmitter *getCodeEmitter() const { return JCE; }
 
-  static ExecutionEngine *createJIT(Module *M,
+  static ExecutionEngine *createJIT(std::unique_ptr<Module> M,
                                     std::string *ErrorStr,
                                     JITMemoryManager *JMM,
                                     bool GVsWithCode,

Modified: llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.cpp (original)
+++ llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.cpp Mon Aug 18 23:04:25 2014
@@ -43,7 +43,7 @@ static struct RegisterJIT {
 extern "C" void LLVMLinkInMCJIT() {
 }
 
-ExecutionEngine *MCJIT::createJIT(Module *M,
+ExecutionEngine *MCJIT::createJIT(std::unique_ptr<Module> M,
                                   std::string *ErrorStr,
                                   RTDyldMemoryManager *MemMgr,
                                   TargetMachine *TM) {
@@ -52,19 +52,14 @@ ExecutionEngine *MCJIT::createJIT(Module
   // FIXME: Don't do this here.
   sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
 
-  return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager());
+  return new MCJIT(std::move(M), TM,
+                   MemMgr ? MemMgr : new SectionMemoryManager());
 }
 
-MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM)
-  : ExecutionEngine(m), TM(tm), Ctx(nullptr), MemMgr(this, MM), Dyld(&MemMgr),
-    ObjCache(nullptr) {
-
-  OwnedModules.addModule(m);
-  setDataLayout(TM->getSubtargetImpl()->getDataLayout());
-}
-
-MCJIT::~MCJIT() {
-  MutexGuard locked(lock);
+MCJIT::MCJIT(std::unique_ptr<Module> M, TargetMachine *tm,
+             RTDyldMemoryManager *MM)
+    : ExecutionEngine(std::move(M)), TM(tm), Ctx(nullptr), MemMgr(this, 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
@@ -75,7 +70,16 @@ MCJIT::~MCJIT() {
   // If so, additional functions: addModule, removeModule, FindFunctionNamed,
   // runStaticConstructorsDestructors could be moved back to EE as well.
   //
+  std::unique_ptr<Module> First = std::move(Modules[0]);
   Modules.clear();
+
+  OwnedModules.addModule(std::move(First));
+  setDataLayout(TM->getSubtargetImpl()->getDataLayout());
+}
+
+MCJIT::~MCJIT() {
+  MutexGuard locked(lock);
+
   Dyld.deregisterEHFrames();
 
   LoadedObjectList::iterator it, end;
@@ -93,9 +97,9 @@ MCJIT::~MCJIT() {
   delete TM;
 }
 
-void MCJIT::addModule(Module *M) {
+void MCJIT::addModule(std::unique_ptr<Module> M) {
   MutexGuard locked(lock);
-  OwnedModules.addModule(M);
+  OwnedModules.addModule(std::move(M));
 }
 
 bool MCJIT::removeModule(Module *M) {
@@ -389,7 +393,7 @@ void MCJIT::freeMachineCodeForFunction(F
 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
     bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
   for (; I != E; ++I) {
-    ExecutionEngine::runStaticConstructorsDestructors(*I, isDtors);
+    ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
   }
 }
 

Modified: llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.h?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.h (original)
+++ llvm/trunk/lib/ExecutionEngine/MCJIT/MCJIT.h Mon Aug 18 23:04:25 2014
@@ -101,7 +101,8 @@ private:
 // called.
 
 class MCJIT : public ExecutionEngine {
-  MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr);
+  MCJIT(std::unique_ptr<Module> M, TargetMachine *tm,
+        RTDyldMemoryManager *MemMgr);
 
   typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet;
 
@@ -124,8 +125,8 @@ class MCJIT : public ExecutionEngine {
     ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); }
     ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); }
 
-    void addModule(Module *M) {
-      AddedModules.insert(M);
+    void addModule(std::unique_ptr<Module> M) {
+      AddedModules.insert(M.release());
     }
 
     bool removeModule(Module *M) {
@@ -237,7 +238,7 @@ public:
 
   /// @name ExecutionEngine interface implementation
   /// @{
-  void addModule(Module *M) override;
+  void addModule(std::unique_ptr<Module> M) override;
   void addObjectFile(std::unique_ptr<object::ObjectFile> O) override;
   void addArchive(std::unique_ptr<object::Archive> O) override;
   bool removeModule(Module *M) override;
@@ -324,7 +325,7 @@ public:
     MCJITCtor = createJIT;
   }
 
-  static ExecutionEngine *createJIT(Module *M,
+  static ExecutionEngine *createJIT(std::unique_ptr<Module> M,
                                     std::string *ErrorStr,
                                     RTDyldMemoryManager *MemMgr,
                                     TargetMachine *TM);

Modified: llvm/trunk/tools/lli/lli.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/lli.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/tools/lli/lli.cpp (original)
+++ llvm/trunk/tools/lli/lli.cpp Mon Aug 18 23:04:25 2014
@@ -346,7 +346,7 @@ static void addCygMingExtraModule(Execut
   Triple TargetTriple(TargetTripleStr);
 
   // Create a new module.
-  Module *M = new Module("CygMingHelper", Context);
+  std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context);
   M->setTargetTriple(TargetTripleStr);
 
   // Create an empty function named "__main".
@@ -354,11 +354,11 @@ static void addCygMingExtraModule(Execut
   if (TargetTriple.isArch64Bit()) {
     Result = Function::Create(
       TypeBuilder<int64_t(void), false>::get(Context),
-      GlobalValue::ExternalLinkage, "__main", M);
+      GlobalValue::ExternalLinkage, "__main", M.get());
   } else {
     Result = Function::Create(
       TypeBuilder<int32_t(void), false>::get(Context),
-      GlobalValue::ExternalLinkage, "__main", M);
+      GlobalValue::ExternalLinkage, "__main", M.get());
   }
   BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
   Builder.SetInsertPoint(BB);
@@ -370,7 +370,7 @@ static void addCygMingExtraModule(Execut
   Builder.CreateRet(ReturnVal);
 
   // Add this new module to the ExecutionEngine.
-  EE->addModule(M);
+  EE->addModule(std::move(M));
 }
 
 
@@ -399,7 +399,8 @@ int main(int argc, char **argv, char * c
 
   // Load the bitcode...
   SMDiagnostic Err;
-  Module *Mod = ParseIRFile(InputFile, Err, Context);
+  std::unique_ptr<Module> Owner(ParseIRFile(InputFile, Err, Context));
+  Module *Mod = Owner.get();
   if (!Mod) {
     Err.print(argv[0], errs());
     return 1;
@@ -435,7 +436,7 @@ int main(int argc, char **argv, char * c
   }
 
   std::string ErrorMsg;
-  EngineBuilder builder(Mod);
+  EngineBuilder builder(std::move(Owner));
   builder.setMArch(MArch);
   builder.setMCPU(MCPU);
   builder.setMAttrs(MAttrs);
@@ -512,7 +513,7 @@ int main(int argc, char **argv, char * c
 
   // Load any additional modules specified on the command line.
   for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
-    Module *XMod = ParseIRFile(ExtraModules[i], Err, Context);
+    std::unique_ptr<Module> XMod(ParseIRFile(ExtraModules[i], Err, Context));
     if (!XMod) {
       Err.print(argv[0], errs());
       return 1;
@@ -525,7 +526,7 @@ int main(int argc, char **argv, char * c
       }
       // else, we already printed a warning above.
     }
-    EE->addModule(XMod);
+    EE->addModule(std::move(XMod));
   }
 
   for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {

Modified: llvm/trunk/unittests/ExecutionEngine/ExecutionEngineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/ExecutionEngineTest.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/ExecutionEngineTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/ExecutionEngineTest.cpp Mon Aug 18 23:04:25 2014
@@ -20,9 +20,10 @@ namespace {
 
 class ExecutionEngineTest : public testing::Test {
 protected:
-  ExecutionEngineTest()
-    : M(new Module("<main>", getGlobalContext())), Error(""),
-      Engine(EngineBuilder(M).setErrorStr(&Error).create()) {
+  ExecutionEngineTest() {
+    auto Owner = make_unique<Module>("<main>", getGlobalContext());
+    M = Owner.get();
+    Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
   }
 
   virtual void SetUp() {
@@ -35,9 +36,9 @@ protected:
                               GlobalValue::ExternalLinkage, nullptr, Name);
   }
 
-  Module *const M;
   std::string Error;
-  const std::unique_ptr<ExecutionEngine> Engine;
+  Module *M;  // Owned by ExecutionEngine.
+  std::unique_ptr<ExecutionEngine> Engine;
 };
 
 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {

Modified: llvm/trunk/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp Mon Aug 18 23:04:25 2014
@@ -60,15 +60,16 @@ struct RecordingJITEventListener : publi
 
 class JITEventListenerTest : public testing::Test {
  protected:
-  JITEventListenerTest()
-      : M(new Module("module", getGlobalContext())),
-        EE(EngineBuilder(M)
-           .setEngineKind(EngineKind::JIT)
-           .create()) {
-  }
+   JITEventListenerTest() {
+     auto Owner = make_unique<Module>("module", getGlobalContext());
+     M = Owner.get();
+     EE.reset(EngineBuilder(std::move(Owner))
+                  .setEngineKind(EngineKind::JIT)
+                  .create());
+   }
 
   Module *M;
-  const std::unique_ptr<ExecutionEngine> EE;
+  std::unique_ptr<ExecutionEngine> EE;
 };
 
 // Tests on SystemZ disabled as we're running the old JIT

Modified: llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/JIT/JITTest.cpp Mon Aug 18 23:04:25 2014
@@ -184,15 +184,18 @@ class JITTest : public testing::Test {
   }
 
   virtual void SetUp() {
-    M = new Module("<main>", Context);
+    std::unique_ptr<Module> Owner = make_unique<Module>("<main>", Context);
+    M = Owner.get();
     RJMM = createMemoryManager();
     RJMM->setPoisonMemory(true);
     std::string Error;
     TargetOptions Options;
-    TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT)
-                 .setJITMemoryManager(RJMM)
-                 .setErrorStr(&Error)
-                 .setTargetOptions(Options).create());
+    TheJIT.reset(EngineBuilder(std::move(Owner))
+                     .setEngineKind(EngineKind::JIT)
+                     .setJITMemoryManager(RJMM)
+                     .setErrorStr(&Error)
+                     .setTargetOptions(Options)
+                     .create());
     ASSERT_TRUE(TheJIT.get() != nullptr) << Error;
   }
 
@@ -213,14 +216,15 @@ class JITTest : public testing::Test {
 // stays alive after that.
 TEST(JIT, GlobalInFunction) {
   LLVMContext context;
-  Module *M = new Module("<main>", context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("<main>", context);
+  Module *M = Owner.get();
 
   JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
   // Tell the memory manager to poison freed memory so that accessing freed
   // memory is more easily tested.
   MemMgr->setPoisonMemory(true);
   std::string Error;
-  std::unique_ptr<ExecutionEngine> JIT(EngineBuilder(M)
+  std::unique_ptr<ExecutionEngine> JIT(EngineBuilder(std::move(Owner))
                                            .setEngineKind(EngineKind::JIT)
                                            .setErrorStr(&Error)
                                            .setJITMemoryManager(MemMgr)
@@ -638,9 +642,10 @@ ExecutionEngine *getJITFromBitcode(
     delete BitcodeBuffer;
     return nullptr;
   }
-  M = ModuleOrErr.get();
+  std::unique_ptr<Module> Owner(ModuleOrErr.get());
+  M = Owner.get();
   std::string errMsg;
-  ExecutionEngine *TheJIT = EngineBuilder(M)
+  ExecutionEngine *TheJIT = EngineBuilder(std::move(Owner))
     .setEngineKind(EngineKind::JIT)
     .setErrorStr(&errMsg)
     .create();

Modified: llvm/trunk/unittests/ExecutionEngine/JIT/MultiJITTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/JIT/MultiJITTest.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/JIT/MultiJITTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/JIT/MultiJITTest.cpp Mon Aug 18 23:04:25 2014
@@ -24,20 +24,20 @@ namespace {
 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__) \
                       && !defined(__aarch64__)
 
-bool LoadAssemblyInto(Module *M, const char *assembly) {
+std::unique_ptr<Module> loadAssembly(LLVMContext &Context,
+                                     const char *Assembly) {
   SMDiagnostic Error;
-  bool success =
-    nullptr != ParseAssemblyString(assembly, M, Error, M->getContext());
+  std::unique_ptr<Module> Ret(
+      ParseAssemblyString(Assembly, nullptr, Error, Context));
   std::string errMsg;
   raw_string_ostream os(errMsg);
   Error.print("", os);
-  EXPECT_TRUE(success) << os.str();
-  return success;
+  EXPECT_TRUE((bool)Ret) << os.str();
+  return std::move(Ret);
 }
 
-void createModule1(LLVMContext &Context1, Module *&M1, Function *&FooF1) {
-  M1 = new Module("test1", Context1);
-  LoadAssemblyInto(M1,
+std::unique_ptr<Module> createModule1(LLVMContext &Context1, Function *&FooF1) {
+  std::unique_ptr<Module> Ret =  loadAssembly(Context1,
                    "define i32 @add1(i32 %ArgX1) { "
                    "entry: "
                    "  %addresult = add i32 1, %ArgX1 "
@@ -49,12 +49,12 @@ void createModule1(LLVMContext &Context1
                    "  %add1 = call i32 @add1(i32 10) "
                    "  ret i32 %add1 "
                    "} ");
-  FooF1 = M1->getFunction("foo1");
+  FooF1 = Ret->getFunction("foo1");
+  return std::move(Ret);
 }
 
-void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
-  M2 = new Module("test2", Context2);
-  LoadAssemblyInto(M2,
+std::unique_ptr<Module> createModule2(LLVMContext &Context2, Function *&FooF2) {
+  std::unique_ptr<Module> Ret = loadAssembly(Context2,
                    "define i32 @add2(i32 %ArgX2) { "
                    "entry: "
                    "  %addresult = add i32 2, %ArgX2 "
@@ -66,24 +66,23 @@ void createModule2(LLVMContext &Context2
                    "  %add2 = call i32 @add2(i32 10) "
                    "  ret i32 %add2 "
                    "} ");
-  FooF2 = M2->getFunction("foo2");
+  FooF2 = Ret->getFunction("foo2");
+  return std::move(Ret);
 }
 
 TEST(MultiJitTest, EagerMode) {
   LLVMContext Context1;
-  Module *M1 = nullptr;
   Function *FooF1 = nullptr;
-  createModule1(Context1, M1, FooF1);
+  std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = nullptr;
   Function *FooF2 = nullptr;
-  createModule2(Context2, M2, FooF2);
+  std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
 
   // Now we create the JIT in eager mode
-  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
   EE1->DisableLazyCompilation(true);
-  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
   EE2->DisableLazyCompilation(true);
 
   // Call the `foo' function with no arguments:
@@ -101,19 +100,17 @@ TEST(MultiJitTest, EagerMode) {
 
 TEST(MultiJitTest, LazyMode) {
   LLVMContext Context1;
-  Module *M1 = nullptr;
   Function *FooF1 = nullptr;
-  createModule1(Context1, M1, FooF1);
+  std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = nullptr;
   Function *FooF2 = nullptr;
-  createModule2(Context2, M2, FooF2);
+  std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
 
   // Now we create the JIT in lazy mode
-  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
   EE1->DisableLazyCompilation(false);
-  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
   EE2->DisableLazyCompilation(false);
 
   // Call the `foo' function with no arguments:
@@ -135,18 +132,16 @@ extern "C" {
 
 TEST(MultiJitTest, JitPool) {
   LLVMContext Context1;
-  Module *M1 = nullptr;
   Function *FooF1 = nullptr;
-  createModule1(Context1, M1, FooF1);
+  std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = nullptr;
   Function *FooF2 = nullptr;
-  createModule2(Context2, M2, FooF2);
+  std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
 
   // Now we create two JITs
-  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
-  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
 
   Function *F1 = EE1->FindFunctionNamed("foo1");
   void *foo1 = EE1->getPointerToFunction(F1);

Modified: llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp Mon Aug 18 23:04:25 2014
@@ -94,8 +94,8 @@ TEST_F(MCJITMultipleModuleTest, two_modu
   Function *FA, *FB;
   createTwoModuleCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -114,8 +114,8 @@ TEST_F(MCJITMultipleModuleTest, two_modu
   Function *FA, *FB;
   createTwoModuleCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -135,8 +135,8 @@ TEST_F(MCJITMultipleModuleTest, two_modu
   Function *FA, *FB;
   createTwoModuleExternCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -156,8 +156,8 @@ TEST_F(MCJITMultipleModuleTest, two_modu
   Function *FA, *FB;
   createTwoModuleExternCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -177,8 +177,8 @@ TEST_F(MCJITMultipleModuleTest, two_modu
   createTwoModuleExternCase(A, FA1, B, FB);
   FA2 = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(A.get(), FA1);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -213,8 +213,8 @@ TEST_F(MCJITMultipleModuleTest, two_modu
   FB = startFunction<int32_t(void)>(B.get(), "FB");
   endFunctionWithRet(FB, Builder.CreateLoad(GVB));
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t FBPtr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -241,9 +241,9 @@ TEST_F(MCJITMultipleModuleTest, three_mo
   Function *FA, *FB, *FC;
   createThreeModuleCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
   checkAdd(ptr);
@@ -266,9 +266,9 @@ TEST_F(MCJITMultipleModuleTest, three_mo
   Function *FA, *FB, *FC;
   createThreeModuleCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -291,9 +291,9 @@ TEST_F(MCJITMultipleModuleTest, three_mo
   Function *FA, *FB, *FC;
   createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
   checkAdd(ptr);
@@ -316,9 +316,9 @@ TEST_F(MCJITMultipleModuleTest, three_mo
   Function *FA, *FB, *FC;
   createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -341,8 +341,8 @@ TEST_F(MCJITMultipleModuleTest, cross_mo
   Function *FA, *FB1, *FB2;
   createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAccumulate(ptr);
@@ -362,8 +362,8 @@ TEST_F(MCJITMultipleModuleTest, cross_mo
   Function *FA, *FB1, *FB2;
   createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
   checkAccumulate(ptr);
@@ -383,8 +383,8 @@ TEST_F(MCJITMultipleModuleTest, cross_mo
   Function *FA, *FB1, *FB2;
   createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
   checkAccumulate(ptr);

Modified: llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp Mon Aug 18 23:04:25 2014
@@ -114,7 +114,7 @@ protected:
 TEST_F(MCJITObjectCacheTest, SetNullObjectCache) {
   SKIP_UNSUPPORTED_PLATFORM;
 
-  createJIT(M.release());
+  createJIT(std::move(M));
 
   TheJIT->setObjectCache(nullptr);
 
@@ -130,7 +130,7 @@ TEST_F(MCJITObjectCacheTest, VerifyBasic
   // Save a copy of the module pointer before handing it off to MCJIT.
   const Module * SavedModulePointer = M.get();
 
-  createJIT(M.release());
+  createJIT(std::move(M));
 
   TheJIT->setObjectCache(Cache.get());
 
@@ -157,7 +157,7 @@ TEST_F(MCJITObjectCacheTest, VerifyLoadF
   std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
 
   // Compile this module with an MCJIT engine
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
   TheJIT->finalizeObject();
 
@@ -174,7 +174,7 @@ TEST_F(MCJITObjectCacheTest, VerifyLoadF
   const Module * SecondModulePointer = M.get();
 
   // Create a new MCJIT instance to load this module then execute it.
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
   compileAndRun();
 
@@ -191,7 +191,7 @@ TEST_F(MCJITObjectCacheTest, VerifyNonLo
   std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
 
   // Compile this module with an MCJIT engine
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
   TheJIT->finalizeObject();
 
@@ -209,7 +209,7 @@ TEST_F(MCJITObjectCacheTest, VerifyNonLo
   const Module * SecondModulePointer = M.get();
 
   // Create a new MCJIT instance to load this module then execute it.
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
 
   // Verify that our object cache does not contain the module yet.

Modified: llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp (original)
+++ llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp Mon Aug 18 23:04:25 2014
@@ -49,7 +49,7 @@ TEST_F(MCJITTest, global_variable) {
 
   int initialValue = 5;
   GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
-  createJIT(M.release());
+  createJIT(std::move(M));
   void *globalPtr =  TheJIT->getPointerToGlobal(Global);
   EXPECT_TRUE(nullptr != globalPtr)
     << "Unable to get pointer to global value from JIT";
@@ -62,7 +62,7 @@ TEST_F(MCJITTest, add_function) {
   SKIP_UNSUPPORTED_PLATFORM;
 
   Function *F = insertAddFunction(M.get());
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
   EXPECT_TRUE(0 != addPtr)
     << "Unable to get pointer to function from JIT";
@@ -83,7 +83,7 @@ TEST_F(MCJITTest, run_main) {
 
   int rc = 6;
   Function *Main = insertMainFunction(M.get(), 6);
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
   EXPECT_TRUE(0 != ptr)
     << "Unable to get pointer to main() from JIT";
@@ -104,7 +104,7 @@ TEST_F(MCJITTest, return_global) {
   Value *ReadGlobal = Builder.CreateLoad(GV);
   endFunctionWithRet(ReturnGlobal, ReadGlobal);
 
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
   EXPECT_TRUE(0 != rgvPtr);
 
@@ -175,7 +175,7 @@ TEST_F(MCJITTest, multiple_functions) {
     Inner = Outer;
   }
 
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
   EXPECT_TRUE(0 != ptr)
     << "Unable to get pointer to outer function from JIT";

Modified: llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h?rev=215967&r1=215966&r2=215967&view=diff
==============================================================================
--- llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h (original)
+++ llvm/trunk/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h Mon Aug 18 23:04:25 2014
@@ -307,13 +307,13 @@ protected:
     UnsupportedEnvironments.push_back(Triple::Cygnus);
   }
 
-  void createJIT(Module *M) {
+  void createJIT(std::unique_ptr<Module> M) {
 
     // Due to the EngineBuilder constructor, it is required to have a Module
     // in order to construct an ExecutionEngine (i.e. MCJIT)
     assert(M != 0 && "a non-null Module must be provided to create MCJIT");
 
-    EngineBuilder EB(M);
+    EngineBuilder EB(std::move(M));
     std::string Error;
     TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
                  .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */





More information about the llvm-commits mailing list