llvm.org GIT mirror llvm / 3f4ed32
Make it explicit that ExecutionEngine takes ownership of the modules. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215967 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
27 changed file(s) with 248 addition(s) and 229 deletion(s). Raw diff Collapse all Expand all
124124
125125 //Read the BrainF program
126126 BrainF bf;
127 Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
127 std::unique_ptr Mod(bf.parse(in, 65536, cf, Context)); // 64 KiB
128128 if (in != &std::cin)
129129 delete in;
130 addMainFunction(mod);
130 addMainFunction(Mod.get());
131131
132132 //Verify generated code
133 if (verifyModule(*mod)) {
133 if (verifyModule(*Mod)) {
134134 errs() << "Error: module failed verification. This shouldn't happen.\n";
135135 abort();
136136 }
140140 InitializeNativeTarget();
141141
142142 outs() << "------- Running JIT -------\n";
143 ExecutionEngine *ee = EngineBuilder(mod).create();
143 Module &M = *Mod;
144 ExecutionEngine *ee = EngineBuilder(std::move(Mod)).create();
144145 std::vector args;
145 Function *brainf_func = mod->getFunction("brainf");
146 Function *brainf_func = M.getFunction("brainf");
146147 GenericValue gv = ee->runFunction(brainf_func, args);
147148 } else {
148 WriteBitcodeToFile(mod, *out);
149 WriteBitcodeToFile(Mod.get(), *out);
149150 }
150151
151152 //Clean up
152153 if (out != &outs())
153154 delete out;
154 delete mod;
155155
156156 llvm_shutdown();
157157
19561956 llvm::IRBuilder<> theBuilder(context);
19571957
19581958 // Make the module, which holds all the code.
1959 llvm::Module *module = new llvm::Module("my cool jit", context);
1959 std::unique_ptr Owner =
1960 llvm::make_unique("my cool jit", context);
1961 llvm::Module *module = Owner.get();
19601962
19611963 llvm::RTDyldMemoryManager *MemMgr = new llvm::SectionMemoryManager();
19621964
19631965 // Build engine with JIT
1964 llvm::EngineBuilder factory(module);
1966 llvm::EngineBuilder factory(std::move(Owner));
19651967 factory.setEngineKind(llvm::EngineKind::JIT);
19661968 factory.setAllocateGVsWithCode(false);
19671969 factory.setTargetOptions(Opts);
9595 LLVMContext Context;
9696
9797 // Create some module to put our function into it.
98 std::unique_ptr M(new Module("test", Context));
98 std::unique_ptr Owner(new Module("test", Context));
99 Module *M = Owner.get();
99100
100101 // We are about to create the "fib" function:
101 Function *FibF = CreateFibFunction(M.get(), Context);
102 Function *FibF = CreateFibFunction(M, Context);
102103
103104 // Now we going to create JIT
104105 std::string errStr;
105106 ExecutionEngine *EE =
106 EngineBuilder(M.get())
107 EngineBuilder(std::move(Owner))
107108 .setErrorStr(&errStr)
108109 .setEngineKind(EngineKind::JIT)
109110 .create();
5555 LLVMContext Context;
5656
5757 // Create some module to put our function into it.
58 Module *M = new Module("test", Context);
58 std::unique_ptr Owner = make_unique("test", Context);
59 Module *M = Owner.get();
5960
6061 // Create the add1 function entry and insert this entry into module M. The
6162 // function will have a return type of "int" and take an argument of "int".
113114 builder.CreateRet(Add1CallRes);
114115
115116 // Now we create the JIT.
116 ExecutionEngine* EE = EngineBuilder(M).create();
117 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
117118
118119 outs() << "We just constructed this LLVM module:\n\n" << *M;
119120 outs() << "\n\nRunning foo: ";
571571 getNextToken();
572572
573573 // Make the module, which holds all the code.
574 TheModule = new Module("my cool jit", Context);
574 std::unique_ptr Owner = make_unique("my cool jit", Context);
575 TheModule = Owner.get();
575576
576577 // Create the JIT. This takes ownership of the module.
577578 std::string ErrStr;
578 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
579 TheExecutionEngine =
580 EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
579581 if (!TheExecutionEngine) {
580582 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
581583 exit(1);
816816 getNextToken();
817817
818818 // Make the module, which holds all the code.
819 TheModule = new Module("my cool jit", Context);
819 std::unique_ptr Owner = make_unique("my cool jit", Context);
820 TheModule = Owner.get();
820821
821822 // Create the JIT. This takes ownership of the module.
822823 std::string ErrStr;
823 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
824 TheExecutionEngine =
825 EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
824826 if (!TheExecutionEngine) {
825827 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
826828 exit(1);
934934 getNextToken();
935935
936936 // Make the module, which holds all the code.
937 TheModule = new Module("my cool jit", Context);
937 std::unique_ptr Owner = make_unique("my cool jit", Context);
938 TheModule = Owner.get();
938939
939940 // Create the JIT. This takes ownership of the module.
940941 std::string ErrStr;
941 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
942 TheExecutionEngine =
943 EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
942944 if (!TheExecutionEngine) {
943945 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
944946 exit(1);
10981098 getNextToken();
10991099
11001100 // Make the module, which holds all the code.
1101 TheModule = new Module("my cool jit", Context);
1101 std::unique_ptr Owner = make_unique("my cool jit", Context);
1102 TheModule = Owner.get();
11021103
11031104 // Create the JIT. This takes ownership of the module.
11041105 std::string ErrStr;
1105 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1106 TheExecutionEngine =
1107 EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
11061108 if (!TheExecutionEngine) {
11071109 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
11081110 exit(1);
242242 LLVMContext Context;
243243
244244 // Create some module to put our function into it.
245 Module *M = new Module("test", Context);
245 std::unique_ptr Owner = make_unique("test", Context);
246 Module *M = Owner.get();
246247
247248 Function* add1F = createAdd1( M );
248249 Function* fibF = CreateFibFunction( M );
249250
250251 // Now we create the JIT.
251 ExecutionEngine* EE = EngineBuilder(M).create();
252 ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
252253
253254 //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
254255 //~ std::cout << "\n\nRunning foo: " << std::flush;
1717 #include "llvm-c/ExecutionEngine.h"
1818 #include "llvm/ADT/SmallVector.h"
1919 #include "llvm/ADT/StringRef.h"
20 #include "llvm/IR/Module.h"
2021 #include "llvm/IR/ValueHandle.h"
2122 #include "llvm/IR/ValueMap.h"
2223 #include "llvm/MC/MCCodeGenInfo.h"
4041 class JITEventListener;
4142 class JITMemoryManager;
4243 class MachineCodeInfo;
43 class Module;
4444 class MutexGuard;
4545 class ObjectCache;
4646 class RTDyldMemoryManager;
130130 protected:
131131 /// The list of Modules that we are JIT'ing from. We use a SmallVector to
132132 /// optimize for the case where there is only one module.
133 SmallVector<Module*, 1> Modules;
133 SmallVector<std::unique_ptr, 1> Modules;
134134
135135 void setDataLayout(const DataLayout *Val) { DL = Val; }
136136
141141 // libraries, the execution engine implementations set these functions to ctor
142142 // pointers at startup time if they are linked in.
143143 static ExecutionEngine *(*JITCtor)(
144 Module *M,
144 std::unique_ptr M,
145145 std::string *ErrorStr,
146146 JITMemoryManager *JMM,
147147 bool GVsWithCode,
148148 TargetMachine *TM);
149149 static ExecutionEngine *(*MCJITCtor)(
150 Module *M,
150 std::unique_ptr M,
151151 std::string *ErrorStr,
152152 RTDyldMemoryManager *MCJMM,
153153 TargetMachine *TM);
154 static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
154 static ExecutionEngine *(*InterpCtor)(std::unique_ptr M,
155 std::string *ErrorStr);
155156
156157 /// LazyFunctionCreator - If an unknown function is needed, this function
157158 /// pointer is invoked to create it. If this returns null, the JIT will
170171
171172 virtual ~ExecutionEngine();
172173
173 /// addModule - Add a Module to the list of modules that we can JIT from.
174 /// Note that this takes ownership of the Module: when the ExecutionEngine is
175 /// destroyed, it destroys the Module as well.
176 virtual void addModule(Module *M) {
177 Modules.push_back(M);
174 /// Add a Module to the list of modules that we can JIT from.
175 virtual void addModule(std::unique_ptr M) {
176 Modules.push_back(std::move(M));
178177 }
179178
180179 /// addObjectFile - Add an ObjectFile to the execution engine.
273272 /// \param isDtors - Run the destructors instead of constructors.
274273 virtual void runStaticConstructorsDestructors(bool isDtors);
275274
276 /// runStaticConstructorsDestructors - This method is used to execute all of
277 /// the static constructors or destructors for a particular module.
275 /// This method is used to execute all of the static constructors or
276 /// destructors for a particular module.
278277 ///
279278 /// \param isDtors - Run the destructors instead of constructors.
280 void runStaticConstructorsDestructors(Module *module, bool isDtors);
279 void runStaticConstructorsDestructors(Module &module, bool isDtors);
281280
282281
283282 /// runFunctionAsMain - This is a helper function which wraps runFunction to
505504 }
506505
507506 protected:
508 explicit ExecutionEngine(Module *M);
507 explicit ExecutionEngine(std::unique_ptr M);
509508
510509 void emitGlobals();
511510
525524 const static Kind Either = (Kind)(JIT | Interpreter);
526525 }
527526
528 /// EngineBuilder - Builder class for ExecutionEngines. Use this by
529 /// stack-allocating a builder, chaining the various set* methods, and
530 /// terminating it with a .create() call.
527 /// Builder class for ExecutionEngines. Use this by stack-allocating a builder,
528 /// chaining the various set* methods, and terminating it with a .create()
529 /// call.
531530 class EngineBuilder {
532531 private:
533 Module *M;
532 std::unique_ptr M;
534533 EngineKind::Kind WhichEngine;
535534 std::string *ErrorStr;
536535 CodeGenOpt::Level OptLevel;
550549 void InitEngine();
551550
552551 public:
553 /// EngineBuilder - Constructor for EngineBuilder. If create() is called and
554 /// is successful, the created engine takes ownership of the module.
555 EngineBuilder(Module *m) : M(m) {
552 /// Constructor for EngineBuilder.
553 EngineBuilder(std::unique_ptr M) : M(std::move(M)) {
556554 InitEngine();
557555 }
558556
4848 void ObjectBufferStream::anchor() {}
4949
5050 ExecutionEngine *(*ExecutionEngine::JITCtor)(
51 Module *M,
51 std::unique_ptr M,
5252 std::string *ErrorStr,
5353 JITMemoryManager *JMM,
5454 bool GVsWithCode,
5555 TargetMachine *TM) = nullptr;
5656 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
57 Module *M,
57 std::unique_ptrM,
5858 std::string *ErrorStr,
5959 RTDyldMemoryManager *MCJMM,
6060 TargetMachine *TM) = nullptr;
61 ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
61 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr M,
6262 std::string *ErrorStr) =nullptr;
6363
64 ExecutionEngine::ExecutionEngine(Module *M)
64 ExecutionEngine::ExecutionEngine(std::unique_ptr M)
6565 : EEState(*this),
6666 LazyFunctionCreator(nullptr) {
6767 CompilingLazily = false;
7676 VerifyModules = false;
7777 #endif
7878
79 Modules.push_back(M);
8079 assert(M && "Module is null?");
80 Modules.push_back(std::move(M));
8181 }
8282
8383 ExecutionEngine::~ExecutionEngine() {
8484 clearAllGlobalMappings();
85 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
86 delete Modules[i];
8785 }
8886
8987 namespace {
130128 }
131129
132130 bool ExecutionEngine::removeModule(Module *M) {
133 for(SmallVectorImpl::iterator I = Modules.begin(),
134 E = Modules.end(); I != E; ++I) {
135 Module *Found = *I;
131 for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
132 Module *Found = I->get();
136133 if (Found == M) {
134 I->release();
137135 Modules.erase(I);
138136 clearGlobalMappingsFromModule(M);
139137 return true;
306304 return Array;
307305 }
308306
309 void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
307 void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
310308 bool isDtors) {
311309 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
312 GlobalVariable *GV = module->getNamedGlobal(Name);
310 GlobalVariable *GV = module.getNamedGlobal(Name);
313311
314312 // If this global has internal linkage, or if it has a use, then it must be
315313 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
347345
348346 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
349347 // Execute global ctors/dtors for each module in the program.
350 for (Module *M : Modules)
351 runStaticConstructorsDestructors(M, isDtors);
348 for (std::unique_ptr &M : Modules)
349 runStaticConstructorsDestructors(*M, isDtors);
352350 }
353351
354352 #ifndef NDEBUG
473471
474472 ExecutionEngine *EE = nullptr;
475473 if (UseMCJIT && ExecutionEngine::MCJITCtor)
476 EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
477 TheTM.release());
474 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
475 MCJMM ? MCJMM : JMM, TheTM.release());
478476 else if (ExecutionEngine::JITCtor)
479 EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
477 EE = ExecutionEngine::JITCtor(std::move(M), ErrorStr, JMM,
480478 AllocateGVsWithCode, TheTM.release());
481479
482480 if (EE) {
489487 // an interpreter instead.
490488 if (WhichEngine & EngineKind::Interpreter) {
491489 if (ExecutionEngine::InterpCtor)
492 return ExecutionEngine::InterpCtor(M, ErrorStr);
490 return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
493491 if (ErrorStr)
494492 *ErrorStr = "Interpreter has not been linked in.";
495493 return nullptr;
109109 LLVMModuleRef M,
110110 char **OutError) {
111111 std::string Error;
112 EngineBuilder builder(unwrap(M));
112 EngineBuilder builder(std::unique_ptr(unwrap(M)));
113113 builder.setEngineKind(EngineKind::Either)
114114 .setErrorStr(&Error);
115115 if (ExecutionEngine *EE = builder.create()){
124124 LLVMModuleRef M,
125125 char **OutError) {
126126 std::string Error;
127 EngineBuilder builder(unwrap(M));
127 EngineBuilder builder(std::unique_ptr(unwrap(M)));
128128 builder.setEngineKind(EngineKind::Interpreter)
129129 .setErrorStr(&Error);
130130 if (ExecutionEngine *Interp = builder.create()) {
140140 unsigned OptLevel,
141141 char **OutError) {
142142 std::string Error;
143 EngineBuilder builder(unwrap(M));
143 EngineBuilder builder(std::unique_ptr(unwrap(M)));
144144 builder.setEngineKind(EngineKind::JIT)
145145 .setErrorStr(&Error)
146146 .setOptLevel((CodeGenOpt::Level)OptLevel);
188188 targetOptions.EnableFastISel = options.EnableFastISel;
189189
190190 std::string Error;
191 EngineBuilder builder(unwrap(M));
191 EngineBuilder builder(std::unique_ptr(unwrap(M)));
192192 builder.setEngineKind(EngineKind::JIT)
193193 .setErrorStr(&Error)
194194 .setUseMCJIT(true)
278278 }
279279
280280 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
281 unwrap(EE)->addModule(unwrap(M));
281 unwrap(EE)->addModule(std::unique_ptr(unwrap(M)));
282282 }
283283
284284 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
2929
3030 extern "C" void LLVMLinkInInterpreter() { }
3131
32 /// create - Create a new interpreter object. This can never fail.
32 /// Create a new interpreter object.
3333 ///
34 ExecutionEngine *Interpreter::create(Module *M, std::string* ErrStr) {
34 ExecutionEngine *Interpreter::create(std::unique_ptr M,
35 std::string *ErrStr) {
3536 // Tell this Module to materialize everything and release the GVMaterializer.
3637 if (std::error_code EC = M->materializeAllPermanently()) {
3738 if (ErrStr)
4041 return nullptr;
4142 }
4243
43 return new Interpreter(M);
44 return new Interpreter(std::move(M));
4445 }
4546
4647 //===----------------------------------------------------------------------===//
4748 // Interpreter ctor - Initialize stuff
4849 //
49 Interpreter::Interpreter(Module *M)
50 : ExecutionEngine(M), TD(M) {
51
50 Interpreter::Interpreter(std::unique_ptr M)
51 : ExecutionEngine(std::move(M)), TD(Modules.back().get()) {
52
5253 memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
5354 setDataLayout(&TD);
5455 // Initialize the "backend"
9393 std::vector AtExitHandlers;
9494
9595 public:
96 explicit Interpreter(Module *M);
96 explicit Interpreter(std::unique_ptr M);
9797 ~Interpreter();
9898
9999 /// runAtExitHandlers - Run any functions registered by the program's calls to
104104 static void Register() {
105105 InterpCtor = create;
106106 }
107
108 /// create - Create an interpreter ExecutionEngine. This can never fail.
109 ///
110 static ExecutionEngine *create(Module *M, std::string *ErrorStr = nullptr);
107
108 /// Create an interpreter ExecutionEngine.
109 ///
110 static ExecutionEngine *create(std::unique_ptr M,
111 std::string *ErrorStr = nullptr);
111112
112113 /// run - Start execution with the specified function and arguments.
113114 ///
6868 extern "C" void LLVMLinkInJIT() {
6969 }
7070
71 /// createJIT - This is the factory method for creating a JIT for the current
72 /// machine, it does not fall back to the interpreter. This takes ownership
73 /// of the module.
74 ExecutionEngine *JIT::createJIT(Module *M,
71 /// This is the factory method for creating a JIT for the current machine, it
72 /// does not fall back to the interpreter.
73 ExecutionEngine *JIT::createJIT(std::unique_ptr M,
7574 std::string *ErrorStr,
7675 JITMemoryManager *JMM,
7776 bool GVsWithCode,
8382
8483 // If the target supports JIT code generation, create the JIT.
8584 if (TargetJITInfo *TJ = TM->getSubtargetImpl()->getJITInfo()) {
86 return new JIT(M, *TM, *TJ, JMM, GVsWithCode);
85 return new JIT(std::move(M), *TM, *TJ, JMM, GVsWithCode);
8786 } else {
8887 if (ErrorStr)
8988 *ErrorStr = "target does not support JIT code generation";
134133 }
135134 }
136135
137 JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
136 JIT::JIT(std::unique_ptr M, TargetMachine &tm, TargetJITInfo &tji,
138137 JITMemoryManager *jmm, bool GVsWithCode)
139 : ExecutionEngine(M), TM(tm), TJI(tji),
138 : ExecutionEngine(std::move(M)), TM(tm), TJI(tji),
140139 JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
141140 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
142141 setDataLayout(TM.getSubtargetImpl()->getDataLayout());
143142
144 jitstate = new JITState(M);
143 Module *Mod = Modules.back().get();
144 jitstate = new JITState(Mod);
145145
146146 // Initialize JCE
147147 JCE = createEmitter(*this, JMM, TM);
152152 // Add target data
153153 MutexGuard locked(lock);
154154 FunctionPassManager &PM = jitstate->getPM();
155 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
156 PM.add(new DataLayoutPass(M));
155 Mod->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
156 PM.add(new DataLayoutPass(Mod));
157157
158158 // Turn the machine code intermediate representation into bytes in memory that
159159 // may be executed.
174174 delete &TM;
175175 }
176176
177 /// addModule - Add a new Module to the JIT. If we previously removed the last
178 /// Module, we need re-initialize jitstate with a valid Module.
179 void JIT::addModule(Module *M) {
177 /// Add a new Module to the JIT. If we previously removed the last Module, we
178 /// need re-initialize jitstate with a valid Module.
179 void JIT::addModule(std::unique_ptr M) {
180180 MutexGuard locked(lock);
181181
182182 if (Modules.empty()) {
183183 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
184184
185 jitstate = new JITState(M);
185 jitstate = new JITState(M.get());
186186
187187 FunctionPassManager &PM = jitstate->getPM();
188188 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
189 PM.add(new DataLayoutPass(M));
189 PM.add(new DataLayoutPass(M.get()));
190190
191191 // Turn the machine code intermediate representation into bytes in memory
192192 // that may be executed.
198198 PM.doInitialization();
199199 }
200200
201 ExecutionEngine::addModule(M);
202 }
203
204 /// removeModule - If we are removing the last Module, invalidate the jitstate
205 /// since the PassManager it contains references a released Module.
201 ExecutionEngine::addModule(std::move(M));
202 }
203
204 /// If we are removing the last Module, invalidate the jitstate since the
205 /// PassManager it contains references a released Module.
206206 bool JIT::removeModule(Module *M) {
207207 bool result = ExecutionEngine::removeModule(M);
208208
214214 }
215215
216216 if (!jitstate && !Modules.empty()) {
217 jitstate = new JITState(Modules[0]);
217 jitstate = new JITState(Modules[0].get());
218218
219219 FunctionPassManager &PM = jitstate->getPM();
220220 M->setDataLayout(TM.getSubtargetImpl()->getDataLayout());
7777 BasicBlockAddressMapTy BasicBlockAddressMap;
7878
7979
80 JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
80 JIT(std::unique_ptr M, TargetMachine &tm, TargetJITInfo &tji,
8181 JITMemoryManager *JMM, bool AllocateGVsWithCode);
8282 public:
8383 ~JIT();
9090 ///
9191 TargetJITInfo &getJITInfo() const { return TJI; }
9292
93 void addModule(Module *M) override;
93 void addModule(std::unique_ptr M) override;
9494
9595 /// removeModule - Remove a Module from the list of modules. Returns true if
9696 /// M is found.
166166 ///
167167 JITCodeEmitter *getCodeEmitter() const { return JCE; }
168168
169 static ExecutionEngine *createJIT(Module *M,
169 static ExecutionEngine *createJIT(std::unique_ptr M,
170170 std::string *ErrorStr,
171171 JITMemoryManager *JMM,
172172 bool GVsWithCode,
4242 extern "C" void LLVMLinkInMCJIT() {
4343 }
4444
45 ExecutionEngine *MCJIT::createJIT(Module *M,
45 ExecutionEngine *MCJIT::createJIT(std::unique_ptr M,
4646 std::string *ErrorStr,
4747 RTDyldMemoryManager *MemMgr,
4848 TargetMachine *TM) {
5151 // FIXME: Don't do this here.
5252 sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
5353
54 return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager());
55 }
56
57 MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM)
58 : ExecutionEngine(m), TM(tm), Ctx(nullptr), MemMgr(this, MM), Dyld(&MemMgr),
59 ObjCache(nullptr) {
60
61 OwnedModules.addModule(m);
62 setDataLayout(TM->getSubtargetImpl()->getDataLayout());
63 }
64
65 MCJIT::~MCJIT() {
66 MutexGuard locked(lock);
54 return new MCJIT(std::move(M), TM,
55 MemMgr ? MemMgr : new SectionMemoryManager());
56 }
57
58 MCJIT::MCJIT(std::unique_ptr M, TargetMachine *tm,
59 RTDyldMemoryManager *MM)
60 : ExecutionEngine(std::move(M)), TM(tm), Ctx(nullptr), MemMgr(this, MM),
61 Dyld(&MemMgr), ObjCache(nullptr) {
6762 // FIXME: We are managing our modules, so we do not want the base class
6863 // ExecutionEngine to manage them as well. To avoid double destruction
6964 // of the first (and only) module added in ExecutionEngine constructor
7469 // If so, additional functions: addModule, removeModule, FindFunctionNamed,
7570 // runStaticConstructorsDestructors could be moved back to EE as well.
7671 //
72 std::unique_ptr First = std::move(Modules[0]);
7773 Modules.clear();
74
75 OwnedModules.addModule(std::move(First));
76 setDataLayout(TM->getSubtargetImpl()->getDataLayout());
77 }
78
79 MCJIT::~MCJIT() {
80 MutexGuard locked(lock);
81
7882 Dyld.deregisterEHFrames();
7983
8084 LoadedObjectList::iterator it, end;
9296 delete TM;
9397 }
9498
95 void MCJIT::addModule(Module *M) {
96 MutexGuard locked(lock);
97 OwnedModules.addModule(M);
99 void MCJIT::addModule(std::unique_ptr M) {
100 MutexGuard locked(lock);
101 OwnedModules.addModule(std::move(M));
98102 }
99103
100104 bool MCJIT::removeModule(Module *M) {
388392 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
389393 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
390394 for (; I != E; ++I) {
391 ExecutionEngine::runStaticConstructorsDestructors(*I, isDtors);
395 ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
392396 }
393397 }
394398
100100 // called.
101101
102102 class MCJIT : public ExecutionEngine {
103 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr);
103 MCJIT(std::unique_ptr M, TargetMachine *tm,
104 RTDyldMemoryManager *MemMgr);
104105
105106 typedef llvm::SmallPtrSet ModulePtrSet;
106107
123124 ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); }
124125 ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); }
125126
126 void addModule(Module *M) {
127 AddedModules.insert(M);
127 void addModule(std::unique_ptr M) {
128 AddedModules.insert(M.release());
128129 }
129130
130131 bool removeModule(Module *M) {
236237
237238 /// @name ExecutionEngine interface implementation
238239 /// @{
239 void addModule(Module *M) override;
240 void addModule(std::unique_ptr M) override;
240241 void addObjectFile(std::unique_ptr O) override;
241242 void addArchive(std::unique_ptr O) override;
242243 bool removeModule(Module *M) override;
323324 MCJITCtor = createJIT;
324325 }
325326
326 static ExecutionEngine *createJIT(Module *M,
327 static ExecutionEngine *createJIT(std::unique_ptr M,
327328 std::string *ErrorStr,
328329 RTDyldMemoryManager *MemMgr,
329330 TargetMachine *TM);
345345 Triple TargetTriple(TargetTripleStr);
346346
347347 // Create a new module.
348 Module *M = new Module("CygMingHelper", Context);
348 std::unique_ptr M = make_unique("CygMingHelper", Context);
349349 M->setTargetTriple(TargetTripleStr);
350350
351351 // Create an empty function named "__main".
353353 if (TargetTriple.isArch64Bit()) {
354354 Result = Function::Create(
355355 TypeBuilder::get(Context),
356 GlobalValue::ExternalLinkage, "__main", M);
356 GlobalValue::ExternalLinkage, "__main", M.get());
357357 } else {
358358 Result = Function::Create(
359359 TypeBuilder::get(Context),
360 GlobalValue::ExternalLinkage, "__main", M);
360 GlobalValue::ExternalLinkage, "__main", M.get());
361361 }
362362 BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
363363 Builder.SetInsertPoint(BB);
369369 Builder.CreateRet(ReturnVal);
370370
371371 // Add this new module to the ExecutionEngine.
372 EE->addModule(M);
372 EE->addModule(std::move(M));
373373 }
374374
375375
398398
399399 // Load the bitcode...
400400 SMDiagnostic Err;
401 Module *Mod = ParseIRFile(InputFile, Err, Context);
401 std::unique_ptr Owner(ParseIRFile(InputFile, Err, Context));
402 Module *Mod = Owner.get();
402403 if (!Mod) {
403404 Err.print(argv[0], errs());
404405 return 1;
434435 }
435436
436437 std::string ErrorMsg;
437 EngineBuilder builder(Mod);
438 EngineBuilder builder(std::move(Owner));
438439 builder.setMArch(MArch);
439440 builder.setMCPU(MCPU);
440441 builder.setMAttrs(MAttrs);
511512
512513 // Load any additional modules specified on the command line.
513514 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
514 Module *XMod = ParseIRFile(ExtraModules[i], Err, Context);
515 std::unique_ptr XMod(ParseIRFile(ExtraModules[i], Err, Context));
515516 if (!XMod) {
516517 Err.print(argv[0], errs());
517518 return 1;
524525 }
525526 // else, we already printed a warning above.
526527 }
527 EE->addModule(XMod);
528 EE->addModule(std::move(XMod));
528529 }
529530
530531 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
1919
2020 class ExecutionEngineTest : public testing::Test {
2121 protected:
22 ExecutionEngineTest()
23 : M(new Module("
", getGlobalContext())), Error(""),
24 Engine(EngineBuilder(M).setErrorStr(&Error).create()) {
22 ExecutionEngineTest() {
23 auto Owner = make_unique("
", getGlobalContext());
24 M = Owner.get();
25 Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
2526 }
2627
2728 virtual void SetUp() {
3435 GlobalValue::ExternalLinkage, nullptr, Name);
3536 }
3637
37 Module *const M;
3838 std::string Error;
39 const std::unique_ptr Engine;
39 Module *M; // Owned by ExecutionEngine.
40 std::unique_ptr Engine;
4041 };
4142
4243 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
5959
6060 class JITEventListenerTest : public testing::Test {
6161 protected:
62 JITEventListenerTest()
63 : M(new Module("module", getGlobalContext())),
64 EE(EngineBuilder(M)
65 .setEngineKind(EngineKind::JIT)
66 .create()) {
67 }
62 JITEventListenerTest() {
63 auto Owner = make_unique("module", getGlobalContext());
64 M = Owner.get();
65 EE.reset(EngineBuilder(std::move(Owner))
66 .setEngineKind(EngineKind::JIT)
67 .create());
68 }
6869
6970 Module *M;
70 const std::unique_ptr EE;
71 std::unique_ptr EE;
7172 };
7273
7374 // Tests on SystemZ disabled as we're running the old JIT
183183 }
184184
185185 virtual void SetUp() {
186 M = new Module("
", Context);
186 std::unique_ptr Owner = make_unique("
", Context);
187 M = Owner.get();
187188 RJMM = createMemoryManager();
188189 RJMM->setPoisonMemory(true);
189190 std::string Error;
190191 TargetOptions Options;
191 TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT)
192 .setJITMemoryManager(RJMM)
193 .setErrorStr(&Error)
194 .setTargetOptions(Options).create());
192 TheJIT.reset(EngineBuilder(std::move(Owner))
193 .setEngineKind(EngineKind::JIT)
194 .setJITMemoryManager(RJMM)
195 .setErrorStr(&Error)
196 .setTargetOptions(Options)
197 .create());
195198 ASSERT_TRUE(TheJIT.get() != nullptr) << Error;
196199 }
197200
212215 // stays alive after that.
213216 TEST(JIT, GlobalInFunction) {
214217 LLVMContext context;
215 Module *M = new Module("
", context);
218 std::unique_ptr Owner = make_unique("
", context);
219 Module *M = Owner.get();
216220
217221 JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
218222 // Tell the memory manager to poison freed memory so that accessing freed
219223 // memory is more easily tested.
220224 MemMgr->setPoisonMemory(true);
221225 std::string Error;
222 std::unique_ptr JIT(EngineBuilder(M)
226 std::unique_ptr JIT(EngineBuilder(std::move(Owner))
223227 .setEngineKind(EngineKind::JIT)
224228 .setErrorStr(&Error)
225229 .setJITMemoryManager(MemMgr)
637641 delete BitcodeBuffer;
638642 return nullptr;
639643 }
640 M = ModuleOrErr.get();
644 std::unique_ptr Owner(ModuleOrErr.get());
645 M = Owner.get();
641646 std::string errMsg;
642 ExecutionEngine *TheJIT = EngineBuilder(M)
647 ExecutionEngine *TheJIT = EngineBuilder(std::move(Owner))
643648 .setEngineKind(EngineKind::JIT)
644649 .setErrorStr(&errMsg)
645650 .create();
2323 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__) \
2424 && !defined(__aarch64__)
2525
26 bool LoadAssemblyInto(Module *M, const char *assembly) {
26 std::unique_ptr loadAssembly(LLVMContext &Context,
27 const char *Assembly) {
2728 SMDiagnostic Error;
28 bool success =
29 nullptr != ParseAssemblyString(assembly, M, Error, M->getContext());
29 std::unique_ptr Ret(
30 ParseAssemblyString(Assembly, nullptr, Error, Context));
3031 std::string errMsg;
3132 raw_string_ostream os(errMsg);
3233 Error.print("", os);
33 EXPECT_TRUE(success) << os.str();
34 return success;
34 EXPECT_TRUE((bool)Ret) << os.str();
35 return std::move(Ret);
3536 }
3637
37 void createModule1(LLVMContext &Context1, Module *&M1, Function *&FooF1) {
38 M1 = new Module("test1", Context1);
39 LoadAssemblyInto(M1,
38 std::unique_ptr createModule1(LLVMContext &Context1, Function *&FooF1) {
39 std::unique_ptr Ret = loadAssembly(Context1,
4040 "define i32 @add1(i32 %ArgX1) { "
4141 "entry: "
4242 " %addresult = add i32 1, %ArgX1 "
4848 " %add1 = call i32 @add1(i32 10) "
4949 " ret i32 %add1 "
5050 "} ");
51 FooF1 = M1->getFunction("foo1");
51 FooF1 = Ret->getFunction("foo1");
52 return std::move(Ret);
5253 }
5354
54 void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
55 M2 = new Module("test2", Context2);
56 LoadAssemblyInto(M2,
55 std::unique_ptr createModule2(LLVMContext &Context2, Function *&FooF2) {
56 std::unique_ptr Ret = loadAssembly(Context2,
5757 "define i32 @add2(i32 %ArgX2) { "
5858 "entry: "
5959 " %addresult = add i32 2, %ArgX2 "
6565 " %add2 = call i32 @add2(i32 10) "
6666 " ret i32 %add2 "
6767 "} ");
68 FooF2 = M2->getFunction("foo2");
68 FooF2 = Ret->getFunction("foo2");
69 return std::move(Ret);
6970 }
7071
7172 TEST(MultiJitTest, EagerMode) {
7273 LLVMContext Context1;
73 Module *M1 = nullptr;
7474 Function *FooF1 = nullptr;
75 createModule1(Context1, M1, FooF1);
75 std::unique_ptr M1 = createModule1(Context1, FooF1);
7676
7777 LLVMContext Context2;
78 Module *M2 = nullptr;
7978 Function *FooF2 = nullptr;
80 createModule2(Context2, M2, FooF2);
79 std::unique_ptr M2 = createModule2(Context2, FooF2);
8180
8281 // Now we create the JIT in eager mode
83 std::unique_ptr EE1(EngineBuilder(M1).create());
82 std::unique_ptr EE1(EngineBuilder(std::move(M1)).create());
8483 EE1->DisableLazyCompilation(true);
85 std::unique_ptr EE2(EngineBuilder(M2).create());
84 std::unique_ptr EE2(EngineBuilder(std::move(M2)).create());
8685 EE2->DisableLazyCompilation(true);
8786
8887 // Call the `foo' function with no arguments:
10099
101100 TEST(MultiJitTest, LazyMode) {
102101 LLVMContext Context1;
103 Module *M1 = nullptr;
104102 Function *FooF1 = nullptr;
105 createModule1(Context1, M1, FooF1);
103 std::unique_ptr M1 = createModule1(Context1, FooF1);
106104
107105 LLVMContext Context2;
108 Module *M2 = nullptr;
109106 Function *FooF2 = nullptr;
110 createModule2(Context2, M2, FooF2);
107 std::unique_ptr M2 = createModule2(Context2, FooF2);
111108
112109 // Now we create the JIT in lazy mode
113 std::unique_ptr EE1(EngineBuilder(M1).create());
110 std::unique_ptr EE1(EngineBuilder(std::move(M1)).create());
114111 EE1->DisableLazyCompilation(false);
115 std::unique_ptr EE2(EngineBuilder(M2).create());
112 std::unique_ptr EE2(EngineBuilder(std::move(M2)).create());
116113 EE2->DisableLazyCompilation(false);
117114
118115 // Call the `foo' function with no arguments:
134131
135132 TEST(MultiJitTest, JitPool) {
136133 LLVMContext Context1;
137 Module *M1 = nullptr;
138134 Function *FooF1 = nullptr;
139 createModule1(Context1, M1, FooF1);
135 std::unique_ptr M1 = createModule1(Context1, FooF1);
140136
141137 LLVMContext Context2;
142 Module *M2 = nullptr;
143138 Function *FooF2 = nullptr;
144 createModule2(Context2, M2, FooF2);
139 std::unique_ptr M2 = createModule2(Context2, FooF2);
145140
146141 // Now we create two JITs
147 std::unique_ptr EE1(EngineBuilder(M1).create());
148 std::unique_ptr EE2(EngineBuilder(M2).create());
142 std::unique_ptr EE1(EngineBuilder(std::move(M1)).create());
143 std::unique_ptr EE2(EngineBuilder(std::move(M2)).create());
149144
150145 Function *F1 = EE1->FindFunctionNamed("foo1");
151146 void *foo1 = EE1->getPointerToFunction(F1);
9393 Function *FA, *FB;
9494 createTwoModuleCase(A, FA, B, FB);
9595
96 createJIT(A.release());
97 TheJIT->addModule(B.release());
96 createJIT(std::move(A));
97 TheJIT->addModule(std::move(B));
9898
9999 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
100100 checkAdd(ptr);
113113 Function *FA, *FB;
114114 createTwoModuleCase(A, FA, B, FB);
115115
116 createJIT(A.release());
117 TheJIT->addModule(B.release());
116 createJIT(std::move(A));
117 TheJIT->addModule(std::move(B));
118118
119119 uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
120120 TheJIT->finalizeObject();
134134 Function *FA, *FB;
135135 createTwoModuleExternCase(A, FA, B, FB);
136136
137 createJIT(A.release());
138 TheJIT->addModule(B.release());
137 createJIT(std::move(A));
138 TheJIT->addModule(std::move(B));
139139
140140 uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
141141 TheJIT->finalizeObject();
155155 Function *FA, *FB;
156156 createTwoModuleExternCase(A, FA, B, FB);
157157
158 createJIT(A.release());
159 TheJIT->addModule(B.release());
158 createJIT(std::move(A));
159 TheJIT->addModule(std::move(B));
160160
161161 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
162162 checkAdd(ptr);
176176 createTwoModuleExternCase(A, FA1, B, FB);
177177 FA2 = insertSimpleCallFunction(A.get(), FA1);
178178
179 createJIT(A.release());
180 TheJIT->addModule(B.release());
179 createJIT(std::move(A));
180 TheJIT->addModule(std::move(B));
181181
182182 uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
183183 TheJIT->finalizeObject();
212212 FB = startFunction(B.get(), "FB");
213213 endFunctionWithRet(FB, Builder.CreateLoad(GVB));
214214
215 createJIT(A.release());
216 TheJIT->addModule(B.release());
215 createJIT(std::move(A));
216 TheJIT->addModule(std::move(B));
217217
218218 uint64_t FBPtr = TheJIT->getFunctionAddress(FB->getName().str());
219219 TheJIT->finalizeObject();
240240 Function *FA, *FB, *FC;
241241 createThreeModuleCase(A, FA, B, FB, C, FC);
242242
243 createJIT(A.release());
244 TheJIT->addModule(B.release());
245 TheJIT->addModule(C.release());
243 createJIT(std::move(A));
244 TheJIT->addModule(std::move(B));
245 TheJIT->addModule(std::move(C));
246246
247247 uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
248248 checkAdd(ptr);
265265 Function *FA, *FB, *FC;
266266 createThreeModuleCase(A, FA, B, FB, C, FC);
267267
268 createJIT(A.release());
269 TheJIT->addModule(B.release());
270 TheJIT->addModule(C.release());
268 createJIT(std::move(A));
269 TheJIT->addModule(std::move(B));
270 TheJIT->addModule(std::move(C));
271271
272272 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
273273 checkAdd(ptr);
290290 Function *FA, *FB, *FC;
291291 createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
292292
293 createJIT(A.release());
294 TheJIT->addModule(B.release());
295 TheJIT->addModule(C.release());
293 createJIT(std::move(A));
294 TheJIT->addModule(std::move(B));
295 TheJIT->addModule(std::move(C));
296296
297297 uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
298298 checkAdd(ptr);
315315 Function *FA, *FB, *FC;
316316 createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
317317
318 createJIT(A.release());
319 TheJIT->addModule(B.release());
320 TheJIT->addModule(C.release());
318 createJIT(std::move(A));
319 TheJIT->addModule(std::move(B));
320 TheJIT->addModule(std::move(C));
321321
322322 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
323323 checkAdd(ptr);
340340 Function *FA, *FB1, *FB2;
341341 createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
342342
343 createJIT(A.release());
344 TheJIT->addModule(B.release());
343 createJIT(std::move(A));
344 TheJIT->addModule(std::move(B));
345345
346346 uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
347347 checkAccumulate(ptr);
361361 Function *FA, *FB1, *FB2;
362362 createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
363363
364 createJIT(A.release());
365 TheJIT->addModule(B.release());
364 createJIT(std::move(A));
365 TheJIT->addModule(std::move(B));
366366
367367 uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
368368 checkAccumulate(ptr);
382382 Function *FA, *FB1, *FB2;
383383 createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
384384
385 createJIT(A.release());
386 TheJIT->addModule(B.release());
385 createJIT(std::move(A));
386 TheJIT->addModule(std::move(B));
387387
388388 uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
389389 checkAccumulate(ptr);
113113 TEST_F(MCJITObjectCacheTest, SetNullObjectCache) {
114114 SKIP_UNSUPPORTED_PLATFORM;
115115
116 createJIT(M.release());
116 createJIT(std::move(M));
117117
118118 TheJIT->setObjectCache(nullptr);
119119
129129 // Save a copy of the module pointer before handing it off to MCJIT.
130130 const Module * SavedModulePointer = M.get();
131131
132 createJIT(M.release());
132 createJIT(std::move(M));
133133
134134 TheJIT->setObjectCache(Cache.get());
135135
156156 std::unique_ptr Cache(new TestObjectCache);
157157
158158 // Compile this module with an MCJIT engine
159 createJIT(M.release());
159 createJIT(std::move(M));
160160 TheJIT->setObjectCache(Cache.get());
161161 TheJIT->finalizeObject();
162162
173173 const Module * SecondModulePointer = M.get();
174174
175175 // Create a new MCJIT instance to load this module then execute it.
176 createJIT(M.release());
176 createJIT(std::move(M));
177177 TheJIT->setObjectCache(Cache.get());
178178 compileAndRun();
179179
190190 std::unique_ptr Cache(new TestObjectCache);
191191
192192 // Compile this module with an MCJIT engine
193 createJIT(M.release());
193 createJIT(std::move(M));
194194 TheJIT->setObjectCache(Cache.get());
195195 TheJIT->finalizeObject();
196196
208208 const Module * SecondModulePointer = M.get();
209209
210210 // Create a new MCJIT instance to load this module then execute it.
211 createJIT(M.release());
211 createJIT(std::move(M));
212212 TheJIT->setObjectCache(Cache.get());
213213
214214 // Verify that our object cache does not contain the module yet.
4848
4949 int initialValue = 5;
5050 GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
51 createJIT(M.release());
51 createJIT(std::move(M));
5252 void *globalPtr = TheJIT->getPointerToGlobal(Global);
5353 EXPECT_TRUE(nullptr != globalPtr)
5454 << "Unable to get pointer to global value from JIT";
6161 SKIP_UNSUPPORTED_PLATFORM;
6262
6363 Function *F = insertAddFunction(M.get());
64 createJIT(M.release());
64 createJIT(std::move(M));
6565 uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
6666 EXPECT_TRUE(0 != addPtr)
6767 << "Unable to get pointer to function from JIT";
8282
8383 int rc = 6;
8484 Function *Main = insertMainFunction(M.get(), 6);
85 createJIT(M.release());
85 createJIT(std::move(M));
8686 uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
8787 EXPECT_TRUE(0 != ptr)
8888 << "Unable to get pointer to main() from JIT";
103103 Value *ReadGlobal = Builder.CreateLoad(GV);
104104 endFunctionWithRet(ReturnGlobal, ReadGlobal);
105105
106 createJIT(M.release());
106 createJIT(std::move(M));
107107 uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
108108 EXPECT_TRUE(0 != rgvPtr);
109109
174174 Inner = Outer;
175175 }
176176
177 createJIT(M.release());
177 createJIT(std::move(M));
178178 uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
179179 EXPECT_TRUE(0 != ptr)
180180 << "Unable to get pointer to outer function from JIT";
306306 UnsupportedEnvironments.push_back(Triple::Cygnus);
307307 }
308308
309 void createJIT(Module *M) {
309 void createJIT(std::unique_ptr M) {
310310
311311 // Due to the EngineBuilder constructor, it is required to have a Module
312312 // in order to construct an ExecutionEngine (i.e. MCJIT)
313313 assert(M != 0 && "a non-null Module must be provided to create MCJIT");
314314
315 EngineBuilder EB(M);
315 EngineBuilder EB(std::move(M));
316316 std::string Error;
317317 TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
318318 .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */