Skip to content

Commit

Permalink
[ORC] Enable use of TargetProcessControl::getMemMgr with ObjectLinkin…
Browse files Browse the repository at this point in the history
…gLayer.

This patch makes ownership of the JITLinkMemoryManager by ObjectLinkingLayer
optional: the layer can still own the memory manager but no longer has to.

Evevntually we want to move to a state where ObjectLinkingLayer never owns its
memory manager. For now allowing optional ownership makes it easier to develop
classes that can dynamically use either RTDyldObjectLinkingLayer, which owns
its memory managers, or ObjectLinkingLayer (e.g. LLJIT).
  • Loading branch information
lhames committed Jul 23, 2020
1 parent 96551c9 commit 69091eb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
Expand Up @@ -134,7 +134,8 @@ int main(int argc, char *argv[]) {
ExitOnErr.setBanner(std::string(argv[0]) + ": ");

// (1) Create LLJIT instance.
auto J = ExitOnErr(LLJITBuilder().create());
auto TPC = ExitOnErr(SelfTargetProcessControl::Create());
auto J = ExitOnErr(LLJITBuilder().setTargetProcessControl(*TPC).create());

// (2) Install transform to print modules as they are compiled:
J->getIRTransformLayer().setTransform(
Expand All @@ -145,8 +146,6 @@ int main(int argc, char *argv[]) {
});

// (3) Create stubs and call-through managers:

auto TPC = ExitOnErr(SelfTargetProcessControl::Create());
auto TPCIU = ExitOnErr(TPCIndirectionUtils::Create(*TPC));
ExitOnErr(TPCIU->writeResolverBlock(pointerToJITTargetAddress(&reenter),
pointerToJITTargetAddress(TPCIU.get())));
Expand Down
13 changes: 13 additions & 0 deletions llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h
Expand Up @@ -29,6 +29,7 @@ namespace orc {

class LLJITBuilderState;
class LLLazyJITBuilderState;
class TargetProcessControl;

/// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
///
Expand Down Expand Up @@ -272,6 +273,7 @@ class LLJITBuilderState {
CompileFunctionCreator CreateCompileFunction;
PlatformSetupFunction SetUpPlatform;
unsigned NumCompileThreads = 0;
TargetProcessControl *TPC = nullptr;

/// Called prior to JIT class construcion to fix up defaults.
Error prepareForConstruction();
Expand Down Expand Up @@ -354,6 +356,17 @@ class LLJITBuilderSetters {
return impl();
}

/// Set a TargetProcessControl object.
///
/// If the platform uses ObjectLinkingLayer by default and no
/// ObjectLinkingLayerCreator has been set then the TargetProcessControl
/// object will be used to supply the memory manager for the
/// ObjectLinkingLayer.
SetterImpl &setTargetProcessControl(TargetProcessControl &TPC) {
impl().TPC = &TPC;
return impl();
}

/// Create an instance of the JIT.
Expected<std::unique_ptr<JITType>> create() {
if (auto Err = impl().prepareForConstruction())
Expand Down
13 changes: 10 additions & 3 deletions llvm/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h
Expand Up @@ -90,8 +90,14 @@ class ObjectLinkingLayer : public ObjectLayer {
using ReturnObjectBufferFunction =
std::function<void(std::unique_ptr<MemoryBuffer>)>;

/// Construct an ObjectLinkingLayer with the given NotifyLoaded,
/// and NotifyEmitted functors.
/// Construct an ObjectLinkingLayer.
ObjectLinkingLayer(ExecutionSession &ES,
jitlink::JITLinkMemoryManager &MemMgr);

/// Construct an ObjectLinkingLayer. Takes ownership of the given
/// JITLinkMemoryManager. This method is a temporary hack to simplify
/// co-existence with RTDyldObjectLinkingLayer (which also owns its
/// allocators).
ObjectLinkingLayer(ExecutionSession &ES,
std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);

Expand Down Expand Up @@ -159,7 +165,8 @@ class ObjectLinkingLayer : public ObjectLayer {
Error removeAllModules();

mutable std::mutex LayerMutex;
std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr;
jitlink::JITLinkMemoryManager &MemMgr;
std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgrOwnership;
bool OverrideObjectFlags = false;
bool AutoClaimObjectSymbols = false;
ReturnObjectBufferFunction ReturnObjectBuffer;
Expand Down
14 changes: 10 additions & 4 deletions llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
Expand Up @@ -13,6 +13,7 @@
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/OrcError.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/TargetProcessControl.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
Expand Down Expand Up @@ -973,10 +974,15 @@ Error LLJITBuilderState::prepareForConstruction() {
JTMB->setRelocationModel(Reloc::PIC_);
JTMB->setCodeModel(CodeModel::Small);
CreateObjectLinkingLayer =
[](ExecutionSession &ES,
const Triple &) -> std::unique_ptr<ObjectLayer> {
auto ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(
ES, std::make_unique<jitlink::InProcessMemoryManager>());
[TPC = this->TPC](ExecutionSession &ES,
const Triple &) -> std::unique_ptr<ObjectLayer> {
std::unique_ptr<ObjectLinkingLayer> ObjLinkingLayer;
if (TPC)
ObjLinkingLayer =
std::make_unique<ObjectLinkingLayer>(ES, TPC->getMemMgr());
else
ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(
ES, std::make_unique<jitlink::InProcessMemoryManager>());
ObjLinkingLayer->addPlugin(std::make_unique<EHFrameRegistrationPlugin>(
jitlink::InProcessEHFrameRegistrar::getInstance()));
return std::move(ObjLinkingLayer);
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
Expand Up @@ -36,7 +36,7 @@ class ObjectLinkingLayerJITLinkContext final : public JITLinkContext {
Layer.ReturnObjectBuffer(std::move(ObjBuffer));
}

JITLinkMemoryManager &getMemoryManager() override { return *Layer.MemMgr; }
JITLinkMemoryManager &getMemoryManager() override { return Layer.MemMgr; }

MemoryBufferRef getObjectBuffer() const override {
return ObjBuffer->getMemBufferRef();
Expand Down Expand Up @@ -447,9 +447,13 @@ class ObjectLinkingLayerJITLinkContext final : public JITLinkContext {

ObjectLinkingLayer::Plugin::~Plugin() {}

ObjectLinkingLayer::ObjectLinkingLayer(ExecutionSession &ES,
JITLinkMemoryManager &MemMgr)
: ObjectLayer(ES), MemMgr(MemMgr) {}

ObjectLinkingLayer::ObjectLinkingLayer(
ExecutionSession &ES, std::unique_ptr<JITLinkMemoryManager> MemMgr)
: ObjectLayer(ES), MemMgr(std::move(MemMgr)) {}
: ObjectLayer(ES), MemMgr(*MemMgr), MemMgrOwnership(std::move(MemMgr)) {}

ObjectLinkingLayer::~ObjectLinkingLayer() {
if (auto Err = removeAllModules())
Expand Down

0 comments on commit 69091eb

Please sign in to comment.