Skip to content

Pass memory buffer to RuntimeDyld::MemoryManager factory #142930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class KaleidoscopeJIT {
JITTargetMachineBuilder JTMB, DataLayout DL)
: ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),
ObjectLayer(*this->ES,
[]() { return std::make_unique<SectionMemoryManager>(); }),
[](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
}),
CompileLayer(*this->ES, ObjectLayer,
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
MainJD(this->ES->createBareJITDylib("<main>")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class KaleidoscopeJIT {
JITTargetMachineBuilder JTMB, DataLayout DL)
: ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),
ObjectLayer(*this->ES,
[]() { return std::make_unique<SectionMemoryManager>(); }),
[](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
}),
CompileLayer(*this->ES, ObjectLayer,
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
OptimizeLayer(*this->ES, CompileLayer, optimizeModule),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ class KaleidoscopeJIT {
: ES(std::move(ES)), EPCIU(std::move(EPCIU)), DL(std::move(DL)),
Mangle(*this->ES, this->DL),
ObjectLayer(*this->ES,
[]() { return std::make_unique<SectionMemoryManager>(); }),
[](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
}),
CompileLayer(*this->ES, ObjectLayer,
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
OptimizeLayer(*this->ES, CompileLayer, optimizeModule),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ class KaleidoscopeJIT {
: ES(std::move(ES)), EPCIU(std::move(EPCIU)), DL(std::move(DL)),
Mangle(*this->ES, this->DL),
ObjectLayer(*this->ES,
[]() { return std::make_unique<SectionMemoryManager>(); }),
[](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
}),
CompileLayer(*this->ES, ObjectLayer,
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
OptimizeLayer(*this->ES, CompileLayer, optimizeModule),
Expand Down
4 changes: 3 additions & 1 deletion llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class KaleidoscopeJIT {
JITTargetMachineBuilder JTMB, DataLayout DL)
: ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),
ObjectLayer(*this->ES,
[]() { return std::make_unique<SectionMemoryManager>(); }),
[](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
}),
CompileLayer(*this->ES, ObjectLayer,
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
MainJD(this->ES->createBareJITDylib("<main>")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class LLVM_ABI RTDyldObjectLinkingLayer
MaterializationResponsibility &R, std::unique_ptr<MemoryBuffer>)>;

using GetMemoryManagerFunction =
unique_function<std::unique_ptr<RuntimeDyld::MemoryManager>()>;
unique_function<std::unique_ptr<RuntimeDyld::MemoryManager>(
const MemoryBuffer &)>;

/// Construct an ObjectLinkingLayer with the given NotifyLoaded,
/// and NotifyEmitted functors.
Expand Down
13 changes: 7 additions & 6 deletions llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,8 @@ class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
}

void registerInitFunc(JITDylib &JD, SymbolStringPtr InitName) {
getExecutionSession().runSessionLocked([&]() {
InitFunctions[&JD].add(InitName);
});
getExecutionSession().runSessionLocked(
[&]() { InitFunctions[&JD].add(InitName); });
}

void registerDeInitFunc(JITDylib &JD, SymbolStringPtr DeInitName) {
Expand Down Expand Up @@ -935,8 +934,8 @@ Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
Expected<ExecutorAddr> LLJIT::lookupLinkerMangled(JITDylib &JD,
SymbolStringPtr Name) {
if (auto Sym = ES->lookup(
makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols),
Name))
makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols),
Name))
return Sym->getAddress();
else
return Sym.takeError();
Expand All @@ -951,7 +950,9 @@ LLJIT::createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES) {

// Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
// a new SectionMemoryManager for each object.
auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
auto GetMemMgr = [](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
};
auto Layer =
std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));

Expand Down
13 changes: 8 additions & 5 deletions llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,8 +1021,10 @@ LLVMOrcObjectLayerRef
LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(
LLVMOrcExecutionSessionRef ES) {
assert(ES && "ES must not be null");
return wrap(new RTDyldObjectLinkingLayer(
*unwrap(ES), [] { return std::make_unique<SectionMemoryManager>(); }));
return wrap(
new RTDyldObjectLinkingLayer(*unwrap(ES), [](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
}));
}

LLVMOrcObjectLayerRef
Expand Down Expand Up @@ -1128,9 +1130,10 @@ LLVMOrcCreateRTDyldObjectLinkingLayerWithMCJITMemoryManagerLikeCallbacks(
CreateContextCtx, CreateContext, NotifyTerminating, AllocateCodeSection,
AllocateDataSection, FinalizeMemory, Destroy);

return wrap(new RTDyldObjectLinkingLayer(*unwrap(ES), [CBs = std::move(CBs)] {
return std::make_unique<MCJITMemoryManagerLikeCallbacksMemMgr>(CBs);
}));
return wrap(new RTDyldObjectLinkingLayer(
*unwrap(ES), [CBs = std::move(CBs)](const MemoryBuffer &) {
return std::make_unique<MCJITMemoryManagerLikeCallbacksMemMgr>(CBs);
}));

return nullptr;
}
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class JITDylibSearchOrderResolver : public JITSymbolResolver {
SymbolDependenceMap &Deps)
: MR(MR), Deps(Deps) {}

void lookup(const LookupSet &Symbols, OnResolvedFunction OnResolved) override {
void lookup(const LookupSet &Symbols,
OnResolvedFunction OnResolved) override {
auto &ES = MR.getTargetJITDylib().getExecutionSession();
SymbolLookupSet InternedSymbols;

Expand Down Expand Up @@ -181,7 +182,7 @@ void RTDyldObjectLinkingLayer::emit(
}
}

auto MemMgr = GetMemoryManager();
auto MemMgr = GetMemoryManager(*O);
auto &MemMgrRef = *MemMgr;

// Switch to shared ownership of MR so that it can be captured by both
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ static bool testSetProcessAllSections(std::unique_ptr<MemoryBuffer> Obj,
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");

RTDyldObjectLinkingLayer ObjLayer(ES, [&NonAllocSectionSeen]() {
return std::make_unique<MemoryManagerWrapper>(NonAllocSectionSeen);
});
RTDyldObjectLinkingLayer ObjLayer(
ES, [&NonAllocSectionSeen](const MemoryBuffer &) {
return std::make_unique<MemoryManagerWrapper>(NonAllocSectionSeen);
});

auto OnResolveDoNothing = [](Expected<SymbolMap> R) {
cantFail(std::move(R));
Expand Down Expand Up @@ -156,8 +157,9 @@ TEST(RTDyldObjectLinkingLayerTest, TestOverrideObjectFlags) {
ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");
RTDyldObjectLinkingLayer ObjLayer(
ES, []() { return std::make_unique<SectionMemoryManager>(); });
RTDyldObjectLinkingLayer ObjLayer(ES, [](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
});
IRCompileLayer CompileLayer(ES, ObjLayer,
std::make_unique<FunkySimpleCompiler>(*TM));

Expand Down Expand Up @@ -226,8 +228,9 @@ TEST(RTDyldObjectLinkingLayerTest, TestAutoClaimResponsibilityForSymbols) {
ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");
RTDyldObjectLinkingLayer ObjLayer(
ES, []() { return std::make_unique<SectionMemoryManager>(); });
RTDyldObjectLinkingLayer ObjLayer(ES, [](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
});
IRCompileLayer CompileLayer(ES, ObjLayer,
std::make_unique<FunkySimpleCompiler>(*TM));

Expand All @@ -244,4 +247,63 @@ TEST(RTDyldObjectLinkingLayerTest, TestAutoClaimResponsibilityForSymbols) {
ES.reportError(std::move(Err));
}

TEST(RTDyldObjectLinkingLayerTest, TestMemoryBufferNamePropagation) {
OrcNativeTarget::initialize();

std::unique_ptr<TargetMachine> TM(
EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "",
SmallVector<std::string, 1>()));

if (!TM)
GTEST_SKIP();

// Create a module with two void() functions: foo and bar.
ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
ThreadSafeModule M;
{
ModuleBuilder MB(*TSCtx.getContext(), TM->getTargetTriple().str(), "dummy");
MB.getModule()->setDataLayout(TM->createDataLayout());

Function *FooImpl = MB.createFunctionDecl(
FunctionType::get(Type::getVoidTy(*TSCtx.getContext()), {}, false),
"foo");
BasicBlock *FooEntry =
BasicBlock::Create(*TSCtx.getContext(), "entry", FooImpl);
IRBuilder<> B1(FooEntry);
B1.CreateRetVoid();

M = ThreadSafeModule(MB.takeModule(), std::move(TSCtx));
}

ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");
std::string ObjectIdentifer;

RTDyldObjectLinkingLayer ObjLayer(
ES, [&ObjectIdentifer](const MemoryBuffer &Obj) {
// Capture the name of the object so that we can confirm that it
// contains the module name.
ObjectIdentifer = Obj.getBufferIdentifier().str();
return std::make_unique<SectionMemoryManager>();
});
IRCompileLayer CompileLayer(ES, ObjLayer,
std::make_unique<SimpleCompiler>(*TM));

// Capture the module name before we move the module.
std::string ModuleName = M.getModuleUnlocked()->getName().str();

cantFail(CompileLayer.add(JD, std::move(M)));
ES.lookup(
LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),
SymbolState::Resolved,
[](Expected<SymbolMap> R) { cantFail(std::move(R)); },
NoDependenciesToRegister);

if (auto Err = ES.endSession())
ES.reportError(std::move(Err));

EXPECT_TRUE(ObjectIdentifer.find(ModuleName) != std::string::npos);
}

} // end anonymous namespace
3 changes: 2 additions & 1 deletion mlir/lib/ExecutionEngine/ExecutionEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ ExecutionEngine::create(Operation *m, const ExecutionEngineOptions &options,
// process and dynamically linked libraries.
auto objectLinkingLayerCreator = [&](ExecutionSession &session) {
auto objectLayer = std::make_unique<RTDyldObjectLinkingLayer>(
session, [sectionMemoryMapper = options.sectionMemoryMapper]() {
session, [sectionMemoryMapper =
options.sectionMemoryMapper](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>(sectionMemoryMapper);
});

Expand Down