Skip to content

Commit 2a8a279

Browse files
committed
Make it explicit that ExecutionEngine takes ownership of the modules.
llvm-svn: 215967
1 parent 687744d commit 2a8a279

File tree

27 files changed

+242
-223
lines changed

27 files changed

+242
-223
lines changed

llvm/examples/BrainF/BrainFDriver.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ int main(int argc, char **argv) {
125125

126126
//Read the BrainF program
127127
BrainF bf;
128-
Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
128+
std::unique_ptr<Module> Mod(bf.parse(in, 65536, cf, Context)); // 64 KiB
129129
if (in != &std::cin)
130130
delete in;
131-
addMainFunction(mod);
131+
addMainFunction(Mod.get());
132132

133133
//Verify generated code
134-
if (verifyModule(*mod)) {
134+
if (verifyModule(*Mod)) {
135135
errs() << "Error: module failed verification. This shouldn't happen.\n";
136136
abort();
137137
}
@@ -141,18 +141,18 @@ int main(int argc, char **argv) {
141141
InitializeNativeTarget();
142142

143143
outs() << "------- Running JIT -------\n";
144-
ExecutionEngine *ee = EngineBuilder(mod).create();
144+
Module &M = *Mod;
145+
ExecutionEngine *ee = EngineBuilder(std::move(Mod)).create();
145146
std::vector<GenericValue> args;
146-
Function *brainf_func = mod->getFunction("brainf");
147+
Function *brainf_func = M.getFunction("brainf");
147148
GenericValue gv = ee->runFunction(brainf_func, args);
148149
} else {
149-
WriteBitcodeToFile(mod, *out);
150+
WriteBitcodeToFile(Mod.get(), *out);
150151
}
151152

152153
//Clean up
153154
if (out != &outs())
154155
delete out;
155-
delete mod;
156156

157157
llvm_shutdown();
158158

llvm/examples/ExceptionDemo/ExceptionDemo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,12 +1957,14 @@ int main(int argc, char *argv[]) {
19571957
llvm::IRBuilder<> theBuilder(context);
19581958

19591959
// Make the module, which holds all the code.
1960-
llvm::Module *module = new llvm::Module("my cool jit", context);
1960+
std::unique_ptr<llvm::Module> Owner =
1961+
llvm::make_unique<llvm::Module>("my cool jit", context);
1962+
llvm::Module *module = Owner.get();
19611963

19621964
llvm::RTDyldMemoryManager *MemMgr = new llvm::SectionMemoryManager();
19631965

19641966
// Build engine with JIT
1965-
llvm::EngineBuilder factory(module);
1967+
llvm::EngineBuilder factory(std::move(Owner));
19661968
factory.setEngineKind(llvm::EngineKind::JIT);
19671969
factory.setAllocateGVsWithCode(false);
19681970
factory.setTargetOptions(Opts);

llvm/examples/Fibonacci/fibonacci.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ int main(int argc, char **argv) {
9696
LLVMContext Context;
9797

9898
// Create some module to put our function into it.
99-
std::unique_ptr<Module> M(new Module("test", Context));
99+
std::unique_ptr<Module> Owner(new Module("test", Context));
100+
Module *M = Owner.get();
100101

101102
// We are about to create the "fib" function:
102-
Function *FibF = CreateFibFunction(M.get(), Context);
103+
Function *FibF = CreateFibFunction(M, Context);
103104

104105
// Now we going to create JIT
105106
std::string errStr;
106107
ExecutionEngine *EE =
107-
EngineBuilder(M.get())
108+
EngineBuilder(std::move(Owner))
108109
.setErrorStr(&errStr)
109110
.setEngineKind(EngineKind::JIT)
110111
.create();

llvm/examples/HowToUseJIT/HowToUseJIT.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ int main() {
5656
LLVMContext Context;
5757

5858
// Create some module to put our function into it.
59-
Module *M = new Module("test", Context);
59+
std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
60+
Module *M = Owner.get();
6061

6162
// Create the add1 function entry and insert this entry into module M. The
6263
// function will have a return type of "int" and take an argument of "int".
@@ -114,7 +115,7 @@ int main() {
114115
builder.CreateRet(Add1CallRes);
115116

116117
// Now we create the JIT.
117-
ExecutionEngine* EE = EngineBuilder(M).create();
118+
ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
118119

119120
outs() << "We just constructed this LLVM module:\n\n" << *M;
120121
outs() << "\n\nRunning foo: ";

llvm/examples/Kaleidoscope/Chapter4/toy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,13 @@ int main() {
572572
getNextToken();
573573

574574
// Make the module, which holds all the code.
575-
TheModule = new Module("my cool jit", Context);
575+
std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
576+
TheModule = Owner.get();
576577

577578
// Create the JIT. This takes ownership of the module.
578579
std::string ErrStr;
579-
TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
580+
TheExecutionEngine =
581+
EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
580582
if (!TheExecutionEngine) {
581583
fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
582584
exit(1);

llvm/examples/Kaleidoscope/Chapter5/toy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,11 +817,13 @@ int main() {
817817
getNextToken();
818818

819819
// Make the module, which holds all the code.
820-
TheModule = new Module("my cool jit", Context);
820+
std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
821+
TheModule = Owner.get();
821822

822823
// Create the JIT. This takes ownership of the module.
823824
std::string ErrStr;
824-
TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
825+
TheExecutionEngine =
826+
EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
825827
if (!TheExecutionEngine) {
826828
fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
827829
exit(1);

llvm/examples/Kaleidoscope/Chapter6/toy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,11 +935,13 @@ int main() {
935935
getNextToken();
936936

937937
// Make the module, which holds all the code.
938-
TheModule = new Module("my cool jit", Context);
938+
std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
939+
TheModule = Owner.get();
939940

940941
// Create the JIT. This takes ownership of the module.
941942
std::string ErrStr;
942-
TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
943+
TheExecutionEngine =
944+
EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
943945
if (!TheExecutionEngine) {
944946
fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
945947
exit(1);

llvm/examples/Kaleidoscope/Chapter7/toy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,11 +1099,13 @@ int main() {
10991099
getNextToken();
11001100

11011101
// Make the module, which holds all the code.
1102-
TheModule = new Module("my cool jit", Context);
1102+
std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
1103+
TheModule = Owner.get();
11031104

11041105
// Create the JIT. This takes ownership of the module.
11051106
std::string ErrStr;
1106-
TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1107+
TheExecutionEngine =
1108+
EngineBuilder(std::move(Owner)).setErrorStr(&ErrStr).create();
11071109
if (!TheExecutionEngine) {
11081110
fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
11091111
exit(1);

llvm/examples/ParallelJIT/ParallelJIT.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,14 @@ int main() {
243243
LLVMContext Context;
244244

245245
// Create some module to put our function into it.
246-
Module *M = new Module("test", Context);
246+
std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
247+
Module *M = Owner.get();
247248

248249
Function* add1F = createAdd1( M );
249250
Function* fibF = CreateFibFunction( M );
250251

251252
// Now we create the JIT.
252-
ExecutionEngine* EE = EngineBuilder(M).create();
253+
ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
253254

254255
//~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
255256
//~ std::cout << "\n\nRunning foo: " << std::flush;

llvm/include/llvm/ExecutionEngine/ExecutionEngine.h

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm-c/ExecutionEngine.h"
1919
#include "llvm/ADT/SmallVector.h"
2020
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/IR/Module.h"
2122
#include "llvm/IR/ValueHandle.h"
2223
#include "llvm/IR/ValueMap.h"
2324
#include "llvm/MC/MCCodeGenInfo.h"
@@ -41,7 +42,6 @@ class GlobalValue;
4142
class JITEventListener;
4243
class JITMemoryManager;
4344
class MachineCodeInfo;
44-
class Module;
4545
class MutexGuard;
4646
class ObjectCache;
4747
class RTDyldMemoryManager;
@@ -131,7 +131,7 @@ class ExecutionEngine {
131131
protected:
132132
/// The list of Modules that we are JIT'ing from. We use a SmallVector to
133133
/// optimize for the case where there is only one module.
134-
SmallVector<Module*, 1> Modules;
134+
SmallVector<std::unique_ptr<Module>, 1> Modules;
135135

136136
void setDataLayout(const DataLayout *Val) { DL = Val; }
137137

@@ -142,17 +142,18 @@ class ExecutionEngine {
142142
// libraries, the execution engine implementations set these functions to ctor
143143
// pointers at startup time if they are linked in.
144144
static ExecutionEngine *(*JITCtor)(
145-
Module *M,
145+
std::unique_ptr<Module> M,
146146
std::string *ErrorStr,
147147
JITMemoryManager *JMM,
148148
bool GVsWithCode,
149149
TargetMachine *TM);
150150
static ExecutionEngine *(*MCJITCtor)(
151-
Module *M,
151+
std::unique_ptr<Module> M,
152152
std::string *ErrorStr,
153153
RTDyldMemoryManager *MCJMM,
154154
TargetMachine *TM);
155-
static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
155+
static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
156+
std::string *ErrorStr);
156157

157158
/// LazyFunctionCreator - If an unknown function is needed, this function
158159
/// pointer is invoked to create it. If this returns null, the JIT will
@@ -171,11 +172,9 @@ class ExecutionEngine {
171172

172173
virtual ~ExecutionEngine();
173174

174-
/// addModule - Add a Module to the list of modules that we can JIT from.
175-
/// Note that this takes ownership of the Module: when the ExecutionEngine is
176-
/// destroyed, it destroys the Module as well.
177-
virtual void addModule(Module *M) {
178-
Modules.push_back(M);
175+
/// Add a Module to the list of modules that we can JIT from.
176+
virtual void addModule(std::unique_ptr<Module> M) {
177+
Modules.push_back(std::move(M));
179178
}
180179

181180
/// addObjectFile - Add an ObjectFile to the execution engine.
@@ -274,11 +273,11 @@ class ExecutionEngine {
274273
/// \param isDtors - Run the destructors instead of constructors.
275274
virtual void runStaticConstructorsDestructors(bool isDtors);
276275

277-
/// runStaticConstructorsDestructors - This method is used to execute all of
278-
/// the static constructors or destructors for a particular module.
276+
/// This method is used to execute all of the static constructors or
277+
/// destructors for a particular module.
279278
///
280279
/// \param isDtors - Run the destructors instead of constructors.
281-
void runStaticConstructorsDestructors(Module *module, bool isDtors);
280+
void runStaticConstructorsDestructors(Module &module, bool isDtors);
282281

283282

284283
/// runFunctionAsMain - This is a helper function which wraps runFunction to
@@ -506,7 +505,7 @@ class ExecutionEngine {
506505
}
507506

508507
protected:
509-
explicit ExecutionEngine(Module *M);
508+
explicit ExecutionEngine(std::unique_ptr<Module> M);
510509

511510
void emitGlobals();
512511

@@ -526,12 +525,12 @@ namespace EngineKind {
526525
const static Kind Either = (Kind)(JIT | Interpreter);
527526
}
528527

529-
/// EngineBuilder - Builder class for ExecutionEngines. Use this by
530-
/// stack-allocating a builder, chaining the various set* methods, and
531-
/// terminating it with a .create() call.
528+
/// Builder class for ExecutionEngines. Use this by stack-allocating a builder,
529+
/// chaining the various set* methods, and terminating it with a .create()
530+
/// call.
532531
class EngineBuilder {
533532
private:
534-
Module *M;
533+
std::unique_ptr<Module> M;
535534
EngineKind::Kind WhichEngine;
536535
std::string *ErrorStr;
537536
CodeGenOpt::Level OptLevel;
@@ -551,9 +550,8 @@ class EngineBuilder {
551550
void InitEngine();
552551

553552
public:
554-
/// EngineBuilder - Constructor for EngineBuilder. If create() is called and
555-
/// is successful, the created engine takes ownership of the module.
556-
EngineBuilder(Module *m) : M(m) {
553+
/// Constructor for EngineBuilder.
554+
EngineBuilder(std::unique_ptr<Module> M) : M(std::move(M)) {
557555
InitEngine();
558556
}
559557

llvm/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ void ObjectBuffer::anchor() {}
4949
void ObjectBufferStream::anchor() {}
5050

5151
ExecutionEngine *(*ExecutionEngine::JITCtor)(
52-
Module *M,
52+
std::unique_ptr<Module> M,
5353
std::string *ErrorStr,
5454
JITMemoryManager *JMM,
5555
bool GVsWithCode,
5656
TargetMachine *TM) = nullptr;
5757
ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
58-
Module *M,
58+
std::unique_ptr<Module >M,
5959
std::string *ErrorStr,
6060
RTDyldMemoryManager *MCJMM,
6161
TargetMachine *TM) = nullptr;
62-
ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
62+
ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
6363
std::string *ErrorStr) =nullptr;
6464

65-
ExecutionEngine::ExecutionEngine(Module *M)
65+
ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
6666
: EEState(*this),
6767
LazyFunctionCreator(nullptr) {
6868
CompilingLazily = false;
@@ -77,14 +77,12 @@ ExecutionEngine::ExecutionEngine(Module *M)
7777
VerifyModules = false;
7878
#endif
7979

80-
Modules.push_back(M);
8180
assert(M && "Module is null?");
81+
Modules.push_back(std::move(M));
8282
}
8383

8484
ExecutionEngine::~ExecutionEngine() {
8585
clearAllGlobalMappings();
86-
for (unsigned i = 0, e = Modules.size(); i != e; ++i)
87-
delete Modules[i];
8886
}
8987

9088
namespace {
@@ -131,10 +129,10 @@ void ExecutionEngine::addArchive(std::unique_ptr<object::Archive> A) {
131129
}
132130

133131
bool ExecutionEngine::removeModule(Module *M) {
134-
for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
135-
E = Modules.end(); I != E; ++I) {
136-
Module *Found = *I;
132+
for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
133+
Module *Found = I->get();
137134
if (Found == M) {
135+
I->release();
138136
Modules.erase(I);
139137
clearGlobalMappingsFromModule(M);
140138
return true;
@@ -307,10 +305,10 @@ void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
307305
return Array;
308306
}
309307

310-
void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
308+
void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
311309
bool isDtors) {
312310
const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
313-
GlobalVariable *GV = module->getNamedGlobal(Name);
311+
GlobalVariable *GV = module.getNamedGlobal(Name);
314312

315313
// If this global has internal linkage, or if it has a use, then it must be
316314
// an old-style (llvmgcc3) static ctor with __main linked in and in use. If
@@ -348,8 +346,8 @@ void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
348346

349347
void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
350348
// Execute global ctors/dtors for each module in the program.
351-
for (Module *M : Modules)
352-
runStaticConstructorsDestructors(M, isDtors);
349+
for (std::unique_ptr<Module> &M : Modules)
350+
runStaticConstructorsDestructors(*M, isDtors);
353351
}
354352

355353
#ifndef NDEBUG
@@ -474,10 +472,10 @@ ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
474472

475473
ExecutionEngine *EE = nullptr;
476474
if (UseMCJIT && ExecutionEngine::MCJITCtor)
477-
EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
478-
TheTM.release());
475+
EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
476+
MCJMM ? MCJMM : JMM, TheTM.release());
479477
else if (ExecutionEngine::JITCtor)
480-
EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
478+
EE = ExecutionEngine::JITCtor(std::move(M), ErrorStr, JMM,
481479
AllocateGVsWithCode, TheTM.release());
482480

483481
if (EE) {
@@ -490,7 +488,7 @@ ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
490488
// an interpreter instead.
491489
if (WhichEngine & EngineKind::Interpreter) {
492490
if (ExecutionEngine::InterpCtor)
493-
return ExecutionEngine::InterpCtor(M, ErrorStr);
491+
return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
494492
if (ErrorStr)
495493
*ErrorStr = "Interpreter has not been linked in.";
496494
return nullptr;

0 commit comments

Comments
 (0)