Skip to content

Commit 24d3759

Browse files
committed
jit: Add optional LLVM JITLink support.
LLVM's JITLink API is replacing RuntimeDyld, and the latter will eventually be deprecated and disappear. Support currently varies by architecture/platform and LLVM version. In the case of RISC-V, only JITLink works, while in some other cases especially in older LLVM versions, only RuntimeDyld seems to be mature enough. XXX WIP -- see comments
1 parent 726b0e6 commit 24d3759

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

src/backend/jit/llvm/llvmjit.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,15 +1172,29 @@ llvm_log_jit_error(void *ctx, LLVMErrorRef error)
11721172
static LLVMOrcObjectLayerRef
11731173
llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple)
11741174
{
1175-
#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
1176-
LLVMOrcObjectLayerRef objlayer =
1177-
LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(ES);
1175+
LLVMOrcObjectLayerRef objlayer;
1176+
1177+
#if defined(USE_LLVM_JITLINK)
1178+
objlayer = LLVMOrcCreateJITLinkObjectLinkingLayer(ES);
1179+
#elif defined(USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER)
1180+
objlayer = LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(ES);
11781181
#else
1179-
LLVMOrcObjectLayerRef objlayer =
1180-
LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(ES);
1182+
objlayer = LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(ES);
11811183
#endif
11821184

1185+
/*
1186+
* XXX The JITLink equivalents of the following seem to be:
1187+
*
1188+
* llvm::orc::PerfSupportPlugin
1189+
* llvm::orc::DebuggerSupportPlugin
1190+
*
1191+
* At least the first arrived in LLVM 18? Need to decide how to access
1192+
* those, either from C like below, or just shove it inot the C++ wrapper
1193+
* function, and check if they are feature-equivalent and
1194+
* portability-equivalent.
1195+
*/
11831196

1197+
#if !defined(USE_LLVM_JITLINK)
11841198
#if defined(HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER) && HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER
11851199
if (jit_debugging_support)
11861200
{
@@ -1197,6 +1211,7 @@ llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *T
11971211

11981212
LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(objlayer, l);
11991213
}
1214+
#endif
12001215
#endif
12011216

12021217
return objlayer;

src/backend/jit/llvm/llvmjit_wrap.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ extern "C"
2222
#include "jit/llvmjit.h"
2323
#include "jit/llvmjit_backport.h"
2424

25+
#ifdef USE_LLVM_JITLINK
26+
#include <llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h>
27+
#endif
28+
2529
#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
2630
#include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
2731
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
@@ -46,14 +50,37 @@ LLVMGetFunctionType(LLVMValueRef r)
4650
return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getFunctionType());
4751
}
4852

49-
#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
53+
#if defined(USE_LLVM_JITLINK) || defined(USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER)
5054
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ExecutionSession, LLVMOrcExecutionSessionRef)
51-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ObjectLayer, LLVMOrcObjectLayerRef);
55+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ObjectLayer, LLVMOrcObjectLayerRef)
56+
#endif
57+
58+
#if defined(USE_LLVM_JITLINK)
5259

60+
LLVMOrcObjectLayerRef
61+
LLVMOrcCreateJITLinkObjectLinkingLayer(LLVMOrcExecutionSessionRef ES)
62+
{
63+
auto expected_mm = llvm::jitlink::InProcessMemoryManager::Create();
64+
65+
/* XXX how should this error be reported? */
66+
if (!expected_mm)
67+
elog(ERROR,
68+
"could not create JITLink memory manager: %s",
69+
llvm::toString(expected_mm.takeError()).c_str());
70+
71+
return wrap(new llvm::orc::ObjectLinkingLayer(*unwrap(ES), std::move(*expected_mm)));
72+
}
73+
74+
#elif defined(USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER)
75+
76+
/*
77+
* Using the older RuntimeDyld API, but with our backported memory manager.
78+
*/
5379
LLVMOrcObjectLayerRef
5480
LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef ES)
5581
{
5682
return wrap(new llvm::orc::RTDyldObjectLinkingLayer(
5783
*unwrap(ES), [] { return std::make_unique<llvm::backport::SectionMemoryManager>(nullptr, true); }));
5884
}
85+
5986
#endif

src/include/jit/llvmjit.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "jit/llvmjit_backport.h"
2121

2222
#include <llvm-c/Types.h>
23-
#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
23+
#if defined(USE_LLVM_JITLINK) || defined(USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER)
2424
#include <llvm-c/OrcEE.h>
2525
#endif
2626

@@ -141,6 +141,9 @@ extern LLVMValueRef slot_compile_deform(struct LLVMJitContext *context, TupleDes
141141
*/
142142
extern LLVMTypeRef LLVMGetFunctionReturnType(LLVMValueRef r);
143143
extern LLVMTypeRef LLVMGetFunctionType(LLVMValueRef r);
144+
#ifdef USE_LLVM_JITLINK
145+
extern LLVMOrcObjectLayerRef LLVMOrcCreateJITLinkObjectLinkingLayer(LLVMOrcExecutionSessionRef ES);
146+
#endif
144147
#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
145148
extern LLVMOrcObjectLayerRef LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef ES);
146149
#endif

src/include/jit/llvmjit_backport.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77

88
#include <llvm/Config/llvm-config.h>
99

10+
/*
11+
* On newer LLVM versions, prefer JITLink over RuntimeDyld for linking.
12+
* Earlier versions may also work on some platforms.
13+
*
14+
* XXX What should the conditions be for this?
15+
*/
16+
#if LLVM_VERSION_MAJOR >= 19
17+
#define USE_LLVM_JITLINK
18+
#else
1019
/*
1120
* LLVM's RuntimeDyld can produce code that crashes on larger memory ARM
1221
* systems, because llvm::SectionMemoryManager allocates multiple pieces of
@@ -18,5 +27,6 @@
1827
#if defined(__aarch64__)
1928
#define USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
2029
#endif
30+
#endif
2131

2232
#endif

0 commit comments

Comments
 (0)