[MCJIT] Unique-ptrify the RTDyldMemoryManager member of MCJIT. NFC.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223183 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Lang Hames 2014-12-03 00:51:19 +00:00
parent 1dce7b19a0
commit 5ab94e7135
8 changed files with 51 additions and 29 deletions

View File

@ -138,10 +138,11 @@ protected:
/// getMemoryforGV - Allocate memory for a global variable. /// getMemoryforGV - Allocate memory for a global variable.
virtual char *getMemoryForGV(const GlobalVariable *GV); virtual char *getMemoryForGV(const GlobalVariable *GV);
static ExecutionEngine *(*MCJITCtor)(std::unique_ptr<Module> M, static ExecutionEngine *(*MCJITCtor)(
std::string *ErrorStr, std::unique_ptr<Module> M,
RTDyldMemoryManager *MCJMM, std::string *ErrorStr,
std::unique_ptr<TargetMachine> TM); std::unique_ptr<RTDyldMemoryManager> MCJMM,
std::unique_ptr<TargetMachine> TM);
static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M, static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
std::string *ErrorStr); std::string *ErrorStr);
@ -492,7 +493,7 @@ private:
EngineKind::Kind WhichEngine; EngineKind::Kind WhichEngine;
std::string *ErrorStr; std::string *ErrorStr;
CodeGenOpt::Level OptLevel; CodeGenOpt::Level OptLevel;
RTDyldMemoryManager *MCJMM; std::unique_ptr<RTDyldMemoryManager> MCJMM;
TargetOptions Options; TargetOptions Options;
Reloc::Model RelocModel; Reloc::Model RelocModel;
CodeModel::Model CMModel; CodeModel::Model CMModel;
@ -506,9 +507,10 @@ private:
public: public:
/// Constructor for EngineBuilder. /// Constructor for EngineBuilder.
EngineBuilder(std::unique_ptr<Module> M) : M(std::move(M)) { EngineBuilder(std::unique_ptr<Module> M);
InitEngine();
} // Out-of-line since we don't have the def'n of RTDyldMemoryManager here.
~EngineBuilder();
/// setEngineKind - Controls whether the user wants the interpreter, the JIT, /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
/// or whichever engine works. This option defaults to EngineKind::Either. /// or whichever engine works. This option defaults to EngineKind::Either.
@ -523,10 +525,7 @@ public:
/// to create anything other than MCJIT will cause a runtime error. If create() /// to create anything other than MCJIT will cause a runtime error. If create()
/// is called and is successful, the created engine takes ownership of the /// is called and is successful, the created engine takes ownership of the
/// memory manager. This option defaults to NULL. /// memory manager. This option defaults to NULL.
EngineBuilder &setMCJITMemoryManager(RTDyldMemoryManager *mcjmm) { EngineBuilder &setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
MCJMM = mcjmm;
return *this;
}
/// setErrorStr - Set the error string to write to on error. This option /// setErrorStr - Set the error string to write to on error. This option
/// defaults to NULL. /// defaults to NULL.

View File

@ -44,7 +44,8 @@ STATISTIC(NumGlobals , "Number of global vars initialized");
ExecutionEngine *(*ExecutionEngine::MCJITCtor)( ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
std::unique_ptr<Module> M, std::string *ErrorStr, std::unique_ptr<Module> M, std::string *ErrorStr,
RTDyldMemoryManager *MCJMM, std::unique_ptr<TargetMachine> TM) = nullptr; std::unique_ptr<RTDyldMemoryManager> MCJMM,
std::unique_ptr<TargetMachine> TM) = nullptr;
ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M, ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
std::string *ErrorStr) =nullptr; std::string *ErrorStr) =nullptr;
@ -392,6 +393,19 @@ int ExecutionEngine::runFunctionAsMain(Function *Fn,
return runFunction(Fn, GVArgs).IntVal.getZExtValue(); return runFunction(Fn, GVArgs).IntVal.getZExtValue();
} }
EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
: M(std::move(M)), MCJMM(nullptr) {
InitEngine();
}
EngineBuilder::~EngineBuilder() {}
EngineBuilder &EngineBuilder::setMCJITMemoryManager(
std::unique_ptr<RTDyldMemoryManager> mcjmm) {
MCJMM = std::move(mcjmm);
return *this;
}
void EngineBuilder::InitEngine() { void EngineBuilder::InitEngine() {
WhichEngine = EngineKind::Either; WhichEngine = EngineKind::Either;
ErrorStr = nullptr; ErrorStr = nullptr;
@ -443,7 +457,7 @@ ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
ExecutionEngine *EE = nullptr; ExecutionEngine *EE = nullptr;
if (ExecutionEngine::MCJITCtor) if (ExecutionEngine::MCJITCtor)
EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, MCJMM, EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MCJMM),
std::move(TheTM)); std::move(TheTM));
if (EE) { if (EE) {
EE->setVerifyModules(VerifyModules); EE->setVerifyModules(VerifyModules);

View File

@ -188,7 +188,8 @@ LLVMBool LLVMCreateMCJITCompilerForModule(
.setCodeModel(unwrap(options.CodeModel)) .setCodeModel(unwrap(options.CodeModel))
.setTargetOptions(targetOptions); .setTargetOptions(targetOptions);
if (options.MCJMM) if (options.MCJMM)
builder.setMCJITMemoryManager(unwrap(options.MCJMM)); builder.setMCJITMemoryManager(
std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
if (ExecutionEngine *JIT = builder.create()) { if (ExecutionEngine *JIT = builder.create()) {
*OutJIT = wrap(JIT); *OutJIT = wrap(JIT);
return 0; return 0;

View File

@ -45,21 +45,24 @@ extern "C" void LLVMLinkInMCJIT() {
ExecutionEngine *MCJIT::createJIT(std::unique_ptr<Module> M, ExecutionEngine *MCJIT::createJIT(std::unique_ptr<Module> M,
std::string *ErrorStr, std::string *ErrorStr,
RTDyldMemoryManager *MemMgr, std::unique_ptr<RTDyldMemoryManager> MemMgr,
std::unique_ptr<TargetMachine> TM) { std::unique_ptr<TargetMachine> TM) {
// Try to register the program as a source of symbols to resolve against. // Try to register the program as a source of symbols to resolve against.
// //
// FIXME: Don't do this here. // FIXME: Don't do this here.
sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr); sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
return new MCJIT(std::move(M), std::move(TM), std::unique_ptr<RTDyldMemoryManager> MM = std::move(MemMgr);
MemMgr ? MemMgr : new SectionMemoryManager()); if (!MM)
MM = std::unique_ptr<SectionMemoryManager>(new SectionMemoryManager());
return new MCJIT(std::move(M), std::move(TM), std::move(MM));
} }
MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm, MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
RTDyldMemoryManager *MM) std::unique_ptr<RTDyldMemoryManager> MM)
: ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr), : ExecutionEngine(std::move(M)), TM(std::move(tm)), Ctx(nullptr),
MemMgr(this, MM), Dyld(&MemMgr), ObjCache(nullptr) { MemMgr(this, std::move(MM)), Dyld(&MemMgr), ObjCache(nullptr) {
// FIXME: We are managing our modules, so we do not want the base class // FIXME: We are managing our modules, so we do not want the base class
// ExecutionEngine to manage them as well. To avoid double destruction // ExecutionEngine to manage them as well. To avoid double destruction
// of the first (and only) module added in ExecutionEngine constructor // of the first (and only) module added in ExecutionEngine constructor

View File

@ -28,8 +28,9 @@ class MCJIT;
// to that object. // to that object.
class LinkingMemoryManager : public RTDyldMemoryManager { class LinkingMemoryManager : public RTDyldMemoryManager {
public: public:
LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM) LinkingMemoryManager(MCJIT *Parent,
: ParentEngine(Parent), ClientMM(MM) {} std::unique_ptr<RTDyldMemoryManager> MM)
: ParentEngine(Parent), ClientMM(std::move(MM)) {}
uint64_t getSymbolAddress(const std::string &Name) override; uint64_t getSymbolAddress(const std::string &Name) override;
@ -102,7 +103,7 @@ private:
class MCJIT : public ExecutionEngine { class MCJIT : public ExecutionEngine {
MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm, MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
RTDyldMemoryManager *MemMgr); std::unique_ptr<RTDyldMemoryManager> MemMgr);
typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet; typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet;
@ -325,7 +326,7 @@ public:
static ExecutionEngine *createJIT(std::unique_ptr<Module> M, static ExecutionEngine *createJIT(std::unique_ptr<Module> M,
std::string *ErrorStr, std::string *ErrorStr,
RTDyldMemoryManager *MemMgr, std::unique_ptr<RTDyldMemoryManager> MemMgr,
std::unique_ptr<TargetMachine> TM); std::unique_ptr<TargetMachine> TM);
// @} // @}

View File

@ -433,7 +433,11 @@ int main(int argc, char **argv, char * const *envp) {
RTDyldMM = new RemoteMemoryManager(); RTDyldMM = new RemoteMemoryManager();
else else
RTDyldMM = new SectionMemoryManager(); RTDyldMM = new SectionMemoryManager();
builder.setMCJITMemoryManager(RTDyldMM);
// Deliberately construct a temp std::unique_ptr to pass in. Do not null out
// RTDyldMM: We still use it below, even though we don't own it.
builder.setMCJITMemoryManager(
std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
} else if (RemoteMCJIT) { } else if (RemoteMCJIT) {
errs() << "error: Remote process execution does not work with the " errs() << "error: Remote process execution does not work with the "
"interpreter.\n"; "interpreter.\n";

View File

@ -163,7 +163,7 @@ TEST_F(MCJITObjectCacheTest, VerifyLoadFromCache) {
TheJIT.reset(); TheJIT.reset();
// Create a new memory manager. // Create a new memory manager.
MM = new SectionMemoryManager; MM.reset(new SectionMemoryManager());
// Create a new module and save it. Use a different return code so we can // Create a new module and save it. Use a different return code so we can
// tell if MCJIT compiled this module or used the cache. // tell if MCJIT compiled this module or used the cache.
@ -197,7 +197,7 @@ TEST_F(MCJITObjectCacheTest, VerifyNonLoadFromCache) {
TheJIT.reset(); TheJIT.reset();
// Create a new memory manager. // Create a new memory manager.
MM = new SectionMemoryManager; MM.reset(new SectionMemoryManager());
// Create a new module and save it. Use a different return code so we can // Create a new module and save it. Use a different return code so we can
// tell if MCJIT compiled this module or used the cache. Note that we use // tell if MCJIT compiled this module or used the cache. Note that we use

View File

@ -325,7 +325,7 @@ protected:
EngineBuilder EB(std::move(M)); EngineBuilder EB(std::move(M));
std::string Error; std::string Error;
TheJIT.reset(EB.setEngineKind(EngineKind::JIT) TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
.setMCJITMemoryManager(MM) .setMCJITMemoryManager(std::move(MM))
.setErrorStr(&Error) .setErrorStr(&Error)
.setOptLevel(CodeGenOpt::None) .setOptLevel(CodeGenOpt::None)
.setCodeModel(CodeModel::JITDefault) .setCodeModel(CodeModel::JITDefault)
@ -344,7 +344,7 @@ protected:
StringRef MArch; StringRef MArch;
SmallVector<std::string, 1> MAttrs; SmallVector<std::string, 1> MAttrs;
std::unique_ptr<ExecutionEngine> TheJIT; std::unique_ptr<ExecutionEngine> TheJIT;
RTDyldMemoryManager *MM; std::unique_ptr<RTDyldMemoryManager> MM;
std::unique_ptr<Module> M; std::unique_ptr<Module> M;
}; };