Skip to content

Commit

Permalink
[ORC] Consolidate RTDyldObjectLinkingLayer GetMemMgr and GetResolver …
Browse files Browse the repository at this point in the history
…into a

unified GetResources callback.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325180 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
lhames committed Feb 14, 2018
1 parent d8d27ad commit 8115e01
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 79 deletions.
10 changes: 5 additions & 5 deletions examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class KaleidoscopeJIT {
IRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;

public:

KaleidoscopeJIT()
: ES(SSP),
Resolver(createLegacyLookupResolver(
Expand All @@ -63,10 +62,11 @@ class KaleidoscopeJIT {
},
[](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);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class KaleidoscopeJIT {
IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer;

public:

KaleidoscopeJIT()
: ES(SSP),
Resolver(createLegacyLookupResolver(
Expand All @@ -72,10 +71,11 @@ class KaleidoscopeJIT {
},
[](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));
Expand Down
11 changes: 6 additions & 5 deletions examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ class KaleidoscopeJIT {
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) {
Expand Down
10 changes: 5 additions & 5 deletions examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class KaleidoscopeJIT {
std::unique_ptr<IndirectStubsManager> IndirectStubsMgr;

public:

KaleidoscopeJIT()
: ES(SSP),
Resolver(createLegacyLookupResolver(
Expand All @@ -107,10 +106,11 @@ class KaleidoscopeJIT {
},
[](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) {
Expand Down
10 changes: 5 additions & 5 deletions examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class KaleidoscopeJIT {
MyRemote &Remote;

public:

KaleidoscopeJIT(MyRemote &Remote)
: ES(SSP),
Resolver(createLegacyLookupResolver(
Expand All @@ -115,10 +114,11 @@ class KaleidoscopeJIT {
"", 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) {
Expand Down
9 changes: 5 additions & 4 deletions examples/Kaleidoscope/include/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ class KaleidoscopeJIT {
},
[](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);
}
Expand Down
28 changes: 13 additions & 15 deletions include/llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,20 @@ class RTDyldObjectLinkingLayer : public RTDyldObjectLinkingLayerBase {
}

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) {
}
Expand Down Expand Up @@ -270,9 +267,11 @@ class RTDyldObjectLinkingLayer : public RTDyldObjectLinkingLayerBase {

assert(!LinkedObjects.count(K) && "VModuleKey already in use");

LinkedObjects[K] =
createLinkedObject(ES, std::move(Obj), GetMemMgr(K), GetResolver(K),
std::move(Finalizer), ProcessAllSections);
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();
}
Expand Down Expand Up @@ -339,8 +338,7 @@ class RTDyldObjectLinkingLayer : public RTDyldObjectLinkingLayerBase {
ExecutionSession &ES;

std::map<VModuleKey, std::unique_ptr<LinkedObject>> LinkedObjects;
MemoryManagerGetter GetMemMgr;
ResolverGetter GetResolver;
ResourcesGetter GetResources;
NotifyLoadedFtor NotifyLoaded;
NotifyFinalizedFtor NotifyFinalized;
bool ProcessAllSections = false;
Expand Down
6 changes: 2 additions & 4 deletions lib/ExecutionEngine/Orc/OrcCBindingsStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,14 @@ class OrcCBindingsStack {
: 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,
Expand Down
9 changes: 6 additions & 3 deletions lib/ExecutionEngine/Orc/OrcMCJITReplacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,12 @@ class OrcMCJITReplacement : public ExecutionEngine {
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; },
NotifyObjectLoaded, NotifyFinalized),
ObjectLayer(
ES,
[this](VModuleKey K) {
return ObjectLayerT::Resources{this->MemMgr, this->Resolver};
},
NotifyObjectLoaded, NotifyFinalized),
CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
LazyEmitLayer(CompileLayer) {}

Expand Down
9 changes: 4 additions & 5 deletions tools/lli/OrcLazyJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ class OrcLazyJIT {
: 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()),
Expand Down
12 changes: 6 additions & 6 deletions unittests/ExecutionEngine/Orc/ObjectTransformLayerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -281,12 +282,11 @@ 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 =
[](std::shared_ptr<llvm::object::OwningBinary<llvm::object::ObjectFile>>
Expand Down
37 changes: 20 additions & 17 deletions unittests/ExecutionEngine/Orc/RTDyldObjectLinkingLayerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -131,15 +132,13 @@ TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoDuplicateFinalization) {

std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;

RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) { return MM; },
[&](VModuleKey K) {
auto I = Resolvers.find(K);
assert(I != Resolvers.end() &&
"Missing resolver");
auto R = std::move(I->second);
Resolvers.erase(I);
return R;
});
RTDyldObjectLinkingLayer ObjLayer(ES, [&](VModuleKey K) {
auto I = Resolvers.find(K);
assert(I != Resolvers.end() && "Missing resolver");
auto R = std::move(I->second);
Resolvers.erase(I);
return RTDyldObjectLinkingLayer::Resources{MM, std::move(R)};
});
SimpleCompiler Compile(*TM);

// Create a pair of modules that will trigger recursive finalization:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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) {});
}
Expand Down

0 comments on commit 8115e01

Please sign in to comment.