[llvm] [ORC] Add EPCGenericJITLinkMemoryManager::Create named constructor. (PR #188191)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 24 00:50:54 PDT 2026


https://github.com/lhames created https://github.com/llvm/llvm-project/pull/188191

Create takes a JITDylib and a SymbolNames struct, looks up the implementation symbol addresses in the given JITDylib, and uses them to construct an EPCGenericJITLinkMemoryManager instance. This makes it easier for ORC clients to construct the memory manager from named symbols (e.g. in a bootstrap JITDylib) rather than raw addresses.

>From 4dc0f8de040e7fe7c2c8c35603fe6c567426f258 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Tue, 24 Mar 2026 16:24:56 +1100
Subject: [PATCH] [ORC] Add EPCGenericJITLinkMemoryManager::Create named
 constructor.

Create takes a JITDylib and a SymbolNames struct, looks up the implementation
symbol addresses in the given JITDylib, and uses them to construct an
EPCGenericJITLinkMemoryManager instance. This makes it easier for ORC clients
to construct the memory manager from named symbols (e.g. in a bootstrap
JITDylib) rather than raw addresses.
---
 .../Orc/EPCGenericJITLinkMemoryManager.h      | 16 ++++-
 .../Orc/EPCGenericJITLinkMemoryManager.cpp    | 19 ++++++
 .../EPCGenericJITLinkMemoryManagerTest.cpp    | 58 +++++++++++++++++++
 3 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h b/llvm/include/llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h
index eb71e9a162197..95b1c5abd5393 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h
@@ -28,7 +28,7 @@ namespace orc {
 class LLVM_ABI EPCGenericJITLinkMemoryManager
     : public jitlink::JITLinkMemoryManager {
 public:
-  /// Function addresses for memory access.
+  /// Symbol addresses for memory management implementation.
   struct SymbolAddrs {
     ExecutorAddr Allocator;
     ExecutorAddr Reserve;
@@ -37,11 +37,25 @@ class LLVM_ABI EPCGenericJITLinkMemoryManager
     ExecutorAddr Release;
   };
 
+  /// Symbol names for memory management implementation.
+  struct SymbolNames {
+    StringRef AllocatorName;
+    StringRef ReserveName;
+    StringRef InitializeName;
+    StringRef DeinitializeName;
+    StringRef ReleaseName;
+  };
+
   /// Create an EPCGenericJITLinkMemoryManager instance from a given set of
   /// function addrs.
   EPCGenericJITLinkMemoryManager(ExecutorProcessControl &EPC, SymbolAddrs SAs)
       : EPC(EPC), SAs(SAs) {}
 
+  /// Create an EPCGenericJITLinkMemoryManager using the given implementation
+  /// symbol names. These will be looked up in the given JITDylib.
+  static Expected<std::unique_ptr<EPCGenericJITLinkMemoryManager>>
+  Create(JITDylib &JD, SymbolNames SNs);
+
   void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G,
                 OnAllocatedFunction OnAllocated) override;
 
diff --git a/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp b/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
index 0833af7f020fc..0014949ec2cdb 100644
--- a/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp
@@ -9,6 +9,7 @@
 #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
 
 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
+#include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"
 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
 
 #include <limits>
@@ -97,6 +98,24 @@ class EPCGenericJITLinkMemoryManager::InFlightAlloc
   SegInfoMap Segs;
 };
 
+Expected<std::unique_ptr<EPCGenericJITLinkMemoryManager>>
+EPCGenericJITLinkMemoryManager::Create(JITDylib &JD, SymbolNames SNs) {
+  auto &ES = JD.getExecutionSession();
+  SymbolAddrs SAs;
+  if (auto Err = lookupAndRecordAddrs(
+          ES, LookupKind::Static, makeJITDylibSearchOrder({&JD}),
+          {
+              {ES.intern(SNs.AllocatorName), &SAs.Allocator},
+              {ES.intern(SNs.ReserveName), &SAs.Reserve},
+              {ES.intern(SNs.InitializeName), &SAs.Initialize},
+              {ES.intern(SNs.DeinitializeName), &SAs.Deinitialize},
+              {ES.intern(SNs.ReleaseName), &SAs.Release},
+          }))
+    return Err;
+  return std::make_unique<EPCGenericJITLinkMemoryManager>(
+      ES.getExecutorProcessControl(), SAs);
+}
+
 void EPCGenericJITLinkMemoryManager::allocate(const JITLinkDylib *JD,
                                               LinkGraph &G,
                                               OnAllocatedFunction OnAllocated) {
diff --git a/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp
index fe0ff4f4c0a2a..e0d36986b6767 100644
--- a/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp
@@ -11,6 +11,7 @@
 #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
 
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
 #include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h"
 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
 #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
@@ -140,4 +141,61 @@ TEST(EPCGenericJITLinkMemoryManagerTest, AllocFinalizeFree) {
   cantFail(SelfEPC->disconnect());
 }
 
+TEST(EPCGenericJITLinkMemoryManagerTest, CreateFromSymbolNames) {
+  // Verify that Create successfully looks up symbols and constructs
+  // the memory manager.
+  auto SSP = std::make_shared<SymbolStringPool>();
+  auto EPC =
+      std::make_unique<UnsupportedExecutorProcessControl>(std::move(SSP));
+  ExecutionSession ES(std::move(EPC));
+  auto &JD = ES.createBareJITDylib("JD");
+
+  ExecutorAddr AllocatorAddr(1), ReserveAddr(2), InitAddr(3), DeinitAddr(4),
+      ReleaseAddr(5);
+
+  cantFail(JD.define(absoluteSymbols({
+      {ES.intern("allocator_instance"),
+       {AllocatorAddr, JITSymbolFlags::Exported}},
+      {ES.intern("allocator_reserve"), {ReserveAddr, JITSymbolFlags::Exported}},
+      {ES.intern("allocator_init"), {InitAddr, JITSymbolFlags::Exported}},
+      {ES.intern("allocator_deinit"), {DeinitAddr, JITSymbolFlags::Exported}},
+      {ES.intern("allocator_release"), {ReleaseAddr, JITSymbolFlags::Exported}},
+  })));
+
+  auto Result = EPCGenericJITLinkMemoryManager::Create(
+      JD, {.AllocatorName = "allocator_instance",
+           .ReserveName = "allocator_reserve",
+           .InitializeName = "allocator_init",
+           .DeinitializeName = "allocator_deinit",
+           .ReleaseName = "allocator_release"});
+  EXPECT_THAT_EXPECTED(Result, Succeeded());
+
+  cantFail(ES.endSession());
+}
+
+TEST(EPCGenericJITLinkMemoryManagerTest, CreateFailsOnMissingSymbol) {
+  // Verify that Create returns an error when a symbol is missing.
+  auto SSP = std::make_shared<SymbolStringPool>();
+  auto EPC =
+      std::make_unique<UnsupportedExecutorProcessControl>(std::move(SSP));
+  ExecutionSession ES(std::move(EPC));
+  auto &JD = ES.createBareJITDylib("JD");
+
+  // Only define some of the required symbols.
+  cantFail(JD.define(absoluteSymbols({
+      {ES.intern("allocator_instance"),
+       {ExecutorAddr(1), JITSymbolFlags::Exported}},
+  })));
+
+  auto Result = EPCGenericJITLinkMemoryManager::Create(
+      JD, {.AllocatorName = "allocator_instance",
+           .ReserveName = "allocator_reserve",     // missing
+           .InitializeName = "allocator_init",     // missing
+           .DeinitializeName = "allocator_deinit", // missing
+           .ReleaseName = "allocator_release"});   // missing
+  EXPECT_THAT_EXPECTED(Result, Failed());
+
+  cantFail(ES.endSession());
+}
+
 } // namespace



More information about the llvm-commits mailing list