Skip to content

Commit 2222cfe

Browse files
authored
[C-API] LLVMOrcCreateObjectLinkingLayerWithInProcessMemoryManager (#169862)
Allow C programs to use JITLink with trivial new C-API wrapper. Modeled on `LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager` except that it has to deal with failure of `jitlink::InProcessMemoryManager::Create()`. Function name suggested by @lhames in #106203. I suppose failure of underlying platform-specific things like `sysconf(_SC_PAGESIZE)` shouldn't really happen. An alternative error reporting style might be to follow `LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess` and return `LLVMErrorRef` with an output parameter for the `LLVMOrcObjectLayerRef`, but then it wouldn't be a drop-in replacement for `LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager`. Thoughts? This is wanted by PostgreSQL (branch using this API: https://github.com/macdice/postgres/tree/llvm-22-proposed-c-api). (We're also using `LLVMCreatePerfJITEventListener`, `LLVMCreatePerfJITEventListener`, `LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener`, so it looks like we'll need to research `PerfSupportPlugin`, `DebuggerSupportPlugin`, `ObjectLinkingLayer::addPlugin()`...)
1 parent a82b97c commit 2222cfe

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/include/llvm-c/OrcEE.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ typedef void (*LLVMMemoryManagerNotifyTerminatingCallback)(void *CtxCtx);
4343
* @{
4444
*/
4545

46+
/**
47+
* Create a ObjectLinkingLayer instance using the standard JITLink
48+
* InProcessMemoryManager for memory management.
49+
*/
50+
LLVM_C_ABI LLVMErrorRef
51+
LLVMOrcCreateObjectLinkingLayerWithInProcessMemoryManager(
52+
LLVMOrcObjectLayerRef *Result, LLVMOrcExecutionSessionRef ES);
53+
4654
/**
4755
* Create a RTDyldObjectLinkingLayer instance using the standard
4856
* SectionMemoryManager for memory management.

llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#include "llvm-c/OrcEE.h"
1212
#include "llvm-c/TargetMachine.h"
1313

14+
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
1415
#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
1516
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
1617
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
18+
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
1719
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
1820
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
1921
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
@@ -1017,6 +1019,17 @@ LLVMOrcLLJITGetObjTransformLayer(LLVMOrcLLJITRef J) {
10171019
return wrap(&unwrap(J)->getObjTransformLayer());
10181020
}
10191021

1022+
LLVMErrorRef LLVMOrcCreateObjectLinkingLayerWithInProcessMemoryManager(
1023+
LLVMOrcObjectLayerRef *Result, LLVMOrcExecutionSessionRef ES) {
1024+
assert(Result && "Result must not be null");
1025+
assert(ES && "ES must not be null");
1026+
auto MM = jitlink::InProcessMemoryManager::Create();
1027+
if (!MM)
1028+
return wrap(MM.takeError());
1029+
*Result = wrap(new ObjectLinkingLayer(*unwrap(ES), std::move(*MM)));
1030+
return LLVMErrorSuccess;
1031+
}
1032+
10201033
LLVMOrcObjectLayerRef
10211034
LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(
10221035
LLVMOrcExecutionSessionRef ES) {

0 commit comments

Comments
 (0)