[ORC] Consolidate RTDyldObjectLinkingLayer GetMemMgr and GetResolver into a

unified GetResources callback.

Having a single 'GetResources' callback will simplify adding new resources in
the future.

llvm-svn: 325180
This commit is contained in:
Lang Hames 2018-02-14 22:13:02 +00:00
parent e833fe8ec3
commit 1cd3dd0bd8
12 changed files with 82 additions and 79 deletions

View File

@ -47,7 +47,6 @@ private:
IRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
public:
KaleidoscopeJIT()
: ES(SSP),
Resolver(createLegacyLookupResolver(
@ -63,10 +62,11 @@ public:
},
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
ObjectLayer(
ES,
[](VModuleKey) { return std::make_shared<SectionMemoryManager>(); },
[this](VModuleKey K) { return Resolver; }),
ObjectLayer(ES,
[this](VModuleKey) {
return RTDyldObjectLinkingLayer::Resources{
std::make_shared<SectionMemoryManager>(), Resolver};
}),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}

View File

@ -56,7 +56,6 @@ private:
IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer;
public:
KaleidoscopeJIT()
: ES(SSP),
Resolver(createLegacyLookupResolver(
@ -72,10 +71,11 @@ public:
},
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
ObjectLayer(
ES,
[](VModuleKey) { return std::make_shared<SectionMemoryManager>(); },
[this](VModuleKey K) { return Resolver; }),
ObjectLayer(ES,
[this](VModuleKey) {
return RTDyldObjectLinkingLayer::Resources{
std::make_shared<SectionMemoryManager>(), Resolver};
}),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
OptimizeLayer(CompileLayer, [this](std::shared_ptr<Module> M) {
return optimizeModule(std::move(M));

View File

@ -63,13 +63,14 @@ private:
CompileOnDemandLayer<decltype(OptimizeLayer)> CODLayer;
public:
KaleidoscopeJIT()
: ES(SSP), TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
ObjectLayer(
ES,
[](VModuleKey) { return std::make_shared<SectionMemoryManager>(); },
[&](orc::VModuleKey K) { return Resolvers[K]; }),
ObjectLayer(ES,
[this](VModuleKey K) {
return RTDyldObjectLinkingLayer::Resources{
std::make_shared<SectionMemoryManager>(),
Resolvers[K]};
}),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
OptimizeLayer(CompileLayer,
[this](std::shared_ptr<Module> M) {

View File

@ -89,7 +89,6 @@ private:
std::unique_ptr<IndirectStubsManager> IndirectStubsMgr;
public:
KaleidoscopeJIT()
: ES(SSP),
Resolver(createLegacyLookupResolver(
@ -107,10 +106,11 @@ public:
},
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
ObjectLayer(
ES,
[](VModuleKey) { return std::make_shared<SectionMemoryManager>(); },
[&](VModuleKey K) { return Resolver; }),
ObjectLayer(ES,
[this](VModuleKey K) {
return RTDyldObjectLinkingLayer::Resources{
std::make_shared<SectionMemoryManager>(), Resolver};
}),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
OptimizeLayer(CompileLayer,
[this](std::shared_ptr<Module> M) {

View File

@ -95,7 +95,6 @@ private:
MyRemote &Remote;
public:
KaleidoscopeJIT(MyRemote &Remote)
: ES(SSP),
Resolver(createLegacyLookupResolver(
@ -115,10 +114,11 @@ public:
"", SmallVector<std::string, 0>())),
DL(TM->createDataLayout()),
ObjectLayer(ES,
[&Remote](VModuleKey) {
return cantFail(Remote.createRemoteMemoryManager());
},
[this](VModuleKey) { return Resolver; }),
[this](VModuleKey K) {
return RTDyldObjectLinkingLayer::Resources{
cantFail(this->Remote.createRemoteMemoryManager()),
Resolver};
}),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
OptimizeLayer(CompileLayer,
[this](std::shared_ptr<Module> M) {

View File

@ -51,10 +51,11 @@ public:
},
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
ObjectLayer(
ES,
[](VModuleKey) { return std::make_shared<SectionMemoryManager>(); },
[this](VModuleKey) { return Resolver; }),
ObjectLayer(ES,
[this](VModuleKey) {
return ObjLayerT::Resources{
std::make_shared<SectionMemoryManager>(), Resolver};
}),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}

View File

@ -213,23 +213,20 @@ private:
}
public:
struct Resources {
std::shared_ptr<RuntimeDyld::MemoryManager> MemMgr;
std::shared_ptr<SymbolResolver> Resolver;
};
/// @brief Functor for creating memory managers.
using MemoryManagerGetter =
std::function<std::shared_ptr<RuntimeDyld::MemoryManager>(VModuleKey)>;
using ResolverGetter =
std::function<std::shared_ptr<SymbolResolver>(VModuleKey)>;
using ResourcesGetter = std::function<Resources(VModuleKey)>;
/// @brief Construct an ObjectLinkingLayer with the given NotifyLoaded,
/// and NotifyFinalized functors.
RTDyldObjectLinkingLayer(
ExecutionSession &ES, MemoryManagerGetter GetMemMgr,
ResolverGetter GetResolver,
ExecutionSession &ES, ResourcesGetter GetResources,
NotifyLoadedFtor NotifyLoaded = NotifyLoadedFtor(),
NotifyFinalizedFtor NotifyFinalized = NotifyFinalizedFtor())
: ES(ES), GetMemMgr(std::move(GetMemMgr)),
GetResolver(std::move(GetResolver)),
: ES(ES), GetResources(std::move(GetResources)),
NotifyLoaded(std::move(NotifyLoaded)),
NotifyFinalized(std::move(NotifyFinalized)), ProcessAllSections(false) {
}
@ -270,8 +267,10 @@ public:
assert(!LinkedObjects.count(K) && "VModuleKey already in use");
LinkedObjects[K] =
createLinkedObject(ES, std::move(Obj), GetMemMgr(K), GetResolver(K),
auto R = GetResources(K);
LinkedObjects[K] = createLinkedObject(
ES, std::move(Obj), std::move(R.MemMgr), std::move(R.Resolver),
std::move(Finalizer), ProcessAllSections);
return Error::success();
@ -339,8 +338,7 @@ private:
ExecutionSession &ES;
std::map<VModuleKey, std::unique_ptr<LinkedObject>> LinkedObjects;
MemoryManagerGetter GetMemMgr;
ResolverGetter GetResolver;
ResourcesGetter GetResources;
NotifyLoadedFtor NotifyLoaded;
NotifyFinalizedFtor NotifyFinalized;
bool ProcessAllSections = false;

View File

@ -205,16 +205,14 @@ public:
: ES(SSP), DL(TM.createDataLayout()),
IndirectStubsMgr(IndirectStubsMgrBuilder()), CCMgr(std::move(CCMgr)),
ObjectLayer(ES,
[](orc::VModuleKey K) {
return std::make_shared<SectionMemoryManager>();
},
[this](orc::VModuleKey K) {
auto ResolverI = Resolvers.find(K);
assert(ResolverI != Resolvers.end() &&
"No resolver for module K");
auto Resolver = std::move(ResolverI->second);
Resolvers.erase(ResolverI);
return Resolver;
return ObjLayerT::Resources{
std::make_shared<SectionMemoryManager>(), Resolver};
}),
CompileLayer(ObjectLayer, orc::SimpleCompiler(TM)),
CODLayer(ES, CompileLayer,

View File

@ -229,8 +229,11 @@ public:
Resolver(std::make_shared<LinkingORCResolver>(*this)),
ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this),
NotifyFinalized(*this),
ObjectLayer(ES, [this](VModuleKey K) { return this->MemMgr; },
[this](VModuleKey K) { return this->Resolver; },
ObjectLayer(
ES,
[this](VModuleKey K) {
return ObjectLayerT::Resources{this->MemMgr, this->Resolver};
},
NotifyObjectLoaded, NotifyFinalized),
CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
LazyEmitLayer(CompileLayer) {}

View File

@ -63,16 +63,15 @@ public:
: ES(SSP), TM(std::move(TM)), DL(this->TM->createDataLayout()),
CCMgr(std::move(CCMgr)),
ObjectLayer(ES,
[](orc::VModuleKey) {
return std::make_shared<SectionMemoryManager>();
},
[&](orc::VModuleKey K) {
[this](orc::VModuleKey K) {
auto ResolverI = Resolvers.find(K);
assert(ResolverI != Resolvers.end() &&
"Missing resolver for module K");
auto Resolver = std::move(ResolverI->second);
Resolvers.erase(ResolverI);
return Resolver;
return ObjLayerT::Resources{
std::make_shared<SectionMemoryManager>(),
std::move(Resolver)};
}),
CompileLayer(ObjectLayer, orc::SimpleCompiler(*this->TM)),
IRDumpLayer(CompileLayer, createDebugDumper()),

View File

@ -11,6 +11,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/NullResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Object/ObjectFile.h"
@ -281,11 +282,10 @@ TEST(ObjectTransformLayerTest, Main) {
};
// Construct the jit layers.
RTDyldObjectLinkingLayer BaseLayer(
ES,
[](VModuleKey) { return std::make_shared<llvm::SectionMemoryManager>(); },
[](VModuleKey) -> std::shared_ptr<SymbolResolver> {
llvm_unreachable("Should never be called");
RTDyldObjectLinkingLayer BaseLayer(ES, [](VModuleKey) {
return RTDyldObjectLinkingLayer::Resources{
std::make_shared<llvm::SectionMemoryManager>(),
std::make_shared<NullResolver>()};
});
auto IdentityTransform =

View File

@ -70,9 +70,10 @@ TEST(RTDyldObjectLinkingLayerTest, TestSetProcessAllSections) {
SymbolStringPool SSP;
ExecutionSession ES(SSP);
RTDyldObjectLinkingLayer ObjLayer(
ES, [&MM](VModuleKey) { return MM; },
[](orc::VModuleKey) { return std::make_shared<NullResolver>(); });
RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) {
return RTDyldObjectLinkingLayer::Resources{
MM, std::make_shared<NullResolver>()};
});
LLVMContext Context;
auto M = llvm::make_unique<Module>("", Context);
@ -131,14 +132,12 @@ TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoDuplicateFinalization) {
std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;
RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) { return MM; },
[&](VModuleKey K) {
RTDyldObjectLinkingLayer ObjLayer(ES, [&](VModuleKey K) {
auto I = Resolvers.find(K);
assert(I != Resolvers.end() &&
"Missing resolver");
assert(I != Resolvers.end() && "Missing resolver");
auto R = std::move(I->second);
Resolvers.erase(I);
return R;
return RTDyldObjectLinkingLayer::Resources{MM, std::move(R)};
});
SimpleCompiler Compile(*TM);
@ -220,9 +219,10 @@ TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoPrematureAllocation) {
auto MM = std::make_shared<SectionMemoryManagerWrapper>();
RTDyldObjectLinkingLayer ObjLayer(
ES, [&MM](VModuleKey) { return MM; },
[](VModuleKey) { return std::make_shared<NullResolver>(); });
RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey K) {
return RTDyldObjectLinkingLayer::Resources{
MM, std::make_shared<NullResolver>()};
});
SimpleCompiler Compile(*TM);
// Create a pair of unrelated modules:
@ -283,8 +283,11 @@ TEST_F(RTDyldObjectLinkingLayerExecutionTest, TestNotifyLoadedSignature) {
SymbolStringPool SSP;
ExecutionSession ES(SSP);
RTDyldObjectLinkingLayer ObjLayer(
ES, [](VModuleKey) { return nullptr; },
[](VModuleKey) { return std::make_shared<NullResolver>(); },
ES,
[](VModuleKey) {
return RTDyldObjectLinkingLayer::Resources{
nullptr, std::make_shared<NullResolver>()};
},
[](VModuleKey, const RTDyldObjectLinkingLayer::ObjectPtr &obj,
const RuntimeDyld::LoadedObjectInfo &info) {});
}