On Thu, Sep 18, 2025 at 5:05 PM Thomas Munro <[email protected]> wrote:
> Here's a short unfinished but semi-working patch to try out JITLink
> instead of RuntimeDyld, since Greg expressed an interest in it.  I'm
> not actively working on this myself right now, but in case it helps, I
> think the main things to do next are probably: try out the recently
> added debugger and perf plugins (I hope these are the equivalents of
> the JITEventListener stuff we use), figure out how and when we can
> actually cut over, tidy/refactor and possibly consider upstreaming
> some C wrappers if we want to minimise the C++ glue we carry.  See
> patch for a few more details.

LLVMOrcCreateObjectLinkingLayerWithInProcessMemoryManager() has been
upstreamed into LLVM 22[1], so this patch became shorter.

We could optionally photocopy that function into our llvm_wrap.cpp
file if we have a need to use JITLink with older LLVM versions.  The
motivations for that might be that it is needed for RISC-V and we want
that to work on current Debian et al without waiting ages, or that we
want to kill off our llvm::backport::SectionMemoryManager sooner
without having to wait for LLVM 22 (which has finally fixed the
underlying problem[2]) to be our minimum version.  Someone said this
is also needed for Windows, and I read somewhere that Visual Studio
ships a copy of LLVM these days and it could be a few releases behind,
so that might also provide a motivation, IDK.

I still don't know how to enable GDB and Perf support with JITLink...

> With LLVM 22 (bleeding edge main branch) it builds and runs simple
> things, but I get a SIGBUS crash in the regression tests even on
> unpatched master, something to look into separately...

That turned out to be not our bug, and has been fixed.

[1] 
https://github.com/llvm/llvm-project/commit/2222cfe7e11ff3e0434bc696856629199ef0da7c
[2] 
https://www.postgresql.org/message-id/flat/CA%2BhUKGJTumad75o8Zao-LFseEbt%3DenbUFCM7LZVV%3Dc8yg2i7dg%40mail.gmail.com
From cac92d0786db7686cf449ff1535d8da558eec206 Mon Sep 17 00:00:00 2001
From: Thomas Munro <[email protected]>
Date: Thu, 10 Oct 2024 13:31:56 +1300
Subject: [PATCH v2] jit: Use JITLink for LLVM 22.

The RuntimeDyld ObjectLinkingLayer is semi-deprecated, unsupported and
likely to be removed.  Unfortunately we couldn't use the recommended
JITLink replacement earlier due to incompleteness.  Now we can.  This
avoids the need to use the back-patched SectionMemoryManager from commit
9044fc1d.

XXX TODO: figure out how to turn on GDB and Perf support...
---
 src/backend/jit/llvm/llvmjit.c        | 27 +++++++++++++++++++++++----
 src/backend/jit/llvm/llvmjit_wrap.cpp |  2 ++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 6a0db9de7c6..e1c04834d46 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -34,7 +34,9 @@
 #endif
 
 #include "jit/llvmjit.h"
+#if LLVM_VERSION_MAJOR < 22
 #include "jit/llvmjit_backport.h"
+#endif
 #include "jit/llvmjit_emit.h"
 #include "miscadmin.h"
 #include "portability/instr_time.h"
@@ -1172,12 +1174,27 @@ llvm_log_jit_error(void *ctx, LLVMErrorRef error)
 static LLVMOrcObjectLayerRef
 llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple)
 {
+	LLVMOrcObjectLayerRef objlayer;
+
+#if LLVM_VERSION_MAJOR >= 22
+	LLVMErrorRef error =
+		LLVMOrcCreateObjectLinkingLayerWithInProcessMemoryManager(&objlayer, ES);
+
+	if (error)
+		elog(FATAL, "could not create LLVM ObjectLinkingLayer: %s",
+			 llvm_error_message(error));
+
+	/*
+	 * TODO: llvm::orc::PerfSupportPlugin
+	 *
+	 * TODO: llvm::orc::DebuggerSupportPlugin
+	 */
+#else
+
 #ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
-	LLVMOrcObjectLayerRef objlayer =
-		LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(ES);
+	objlayer = LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(ES);
 #else
-	LLVMOrcObjectLayerRef objlayer =
-		LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(ES);
+	objlayer = LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(ES);
 #endif
 
 	if (jit_debugging_support)
@@ -1195,6 +1212,8 @@ llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *T
 			LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(objlayer, l);
 	}
 
+#endif
+
 	return objlayer;
 }
 
diff --git a/src/backend/jit/llvm/llvmjit_wrap.cpp b/src/backend/jit/llvm/llvmjit_wrap.cpp
index 0dd311c59df..15ba65c3bd3 100644
--- a/src/backend/jit/llvm/llvmjit_wrap.cpp
+++ b/src/backend/jit/llvm/llvmjit_wrap.cpp
@@ -20,7 +20,9 @@ extern "C"
 #include <llvm/IR/Function.h>
 
 #include "jit/llvmjit.h"
+#if LLVM_VERSION_MAJOR < 22
 #include "jit/llvmjit_backport.h"
+#endif
 
 #ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
 #include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
-- 
2.52.0

Reply via email to