On Fri, Jan 9, 2026 at 2:33 AM Anthonin Bonnefoy
<[email protected]> wrote:
> On Tue, Jan 6, 2026 at 1:09 AM Thomas Munro <[email protected]> wrote:
> > It sounds like we might need to wait for or write a similar
> > function for perf support.
>
> Yeah, there's no C api provided by LLVM yet. I've just opened a
> tentative PR[1] to add it.

Thanks Anthonin!  The configure script needs the library for the
"orcdebugging" component (Meson must be adding all components
automatically), and we should check the return value.  I also figured
we should set the error report handler as soon as possible, just in
case it wants to tell us something.  v4 attached.

jit_profiling_support=1 doesn't fail nicely on a system without perf,
but I don't think it's the fault of your patch.  I posted some details
to your PR.  Let's see if it can land in time...  I think there are
just a couple of days left, so it might just be too late for 22.

I suppose we should only use this for ELF systems initially, given
that debugging support is missing for Macs.
From 74b49aced5230932c5232614a30ad6eb81f07661 Mon Sep 17 00:00:00 2001
From: Thomas Munro <[email protected]>
Date: Thu, 10 Oct 2024 13:31:56 +1300
Subject: [PATCH v4 1/3] 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 If Anthonin's patch for LLVMOrcLLJITEnablePerfSupport() is committed
in time for LLVM 22 we'll have to decide how to gate the use of JITLink

Co-authored-by: Thomas Munro <[email protected]>
Co-authored-by: Anthonin Bonnefoy <[email protected]>
Discussion: https://postgr.es/m/CA%2BhUKGJBJx4fDGLv8zUtmsmg16Swry7DJbMr2_GNZcd6sgE0rg%40mail.gmail.com
---
 config/llvm.m4                        |  1 +
 configure                             |  1 +
 src/backend/jit/llvm/llvmjit.c        | 45 ++++++++++++++++++++++++---
 src/backend/jit/llvm/llvmjit_wrap.cpp |  2 ++
 4 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/config/llvm.m4 b/config/llvm.m4
index 5d4f14cb900..b1ef4e90bdb 100644
--- a/config/llvm.m4
+++ b/config/llvm.m4
@@ -75,6 +75,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
     case $pgac_component in
       engine) pgac_components="$pgac_components $pgac_component";;
       debuginfodwarf) pgac_components="$pgac_components $pgac_component";;
+      orcdebugging) pgac_components="$pgac_components $pgac_component";;
       orcjit) pgac_components="$pgac_components $pgac_component";;
       passes) pgac_components="$pgac_components $pgac_component";;
       native) pgac_components="$pgac_components $pgac_component";;
diff --git a/configure b/configure
index 02e4ec7890f..cba5a9061ff 100755
--- a/configure
+++ b/configure
@@ -5041,6 +5041,7 @@ fi
     case $pgac_component in
       engine) pgac_components="$pgac_components $pgac_component";;
       debuginfodwarf) pgac_components="$pgac_components $pgac_component";;
+      orcdebugging) pgac_components="$pgac_components $pgac_component";;
       orcjit) pgac_components="$pgac_components $pgac_component";;
       passes) pgac_components="$pgac_components $pgac_component";;
       native) pgac_components="$pgac_components $pgac_component";;
diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 49e76153f9a..ffd04bbc988 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -24,6 +24,9 @@
 #include <llvm-c/Orc.h>
 #include <llvm-c/OrcEE.h>
 #include <llvm-c/LLJIT.h>
+#if LLVM_VERSION_MAJOR >= 22
+#include <llvm-c/LLJITUtils.h>
+#endif
 #include <llvm-c/Support.h>
 #include <llvm-c/Target.h>
 #if LLVM_VERSION_MAJOR < 17
@@ -34,7 +37,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"
@@ -1136,6 +1141,7 @@ llvm_resolve_symbols(LLVMOrcDefinitionGeneratorRef GeneratorObj, void *Ctx,
 
 		LLVMOrcRetainSymbolStringPoolEntry(LookupSet[i].Name);
 		symbols[i].Name = LookupSet[i].Name;
+
 		symbols[i].Sym.Address = llvm_resolve_symbol(name, NULL);
 		symbols[i].Sym.Flags.GenericFlags = LLVMJITSymbolGenericFlagsExported;
 	}
@@ -1172,12 +1178,22 @@ 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));
+
+#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 +1211,8 @@ llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *T
 			LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(objlayer, l);
 	}
 
+#endif
+
 	return objlayer;
 }
 
@@ -1228,6 +1246,25 @@ llvm_create_jit_instance(LLVMTargetMachineRef tm)
 	LLVMOrcExecutionSessionSetErrorReporter(LLVMOrcLLJITGetExecutionSession(lljit),
 											llvm_log_jit_error, NULL);
 
+#if LLVM_VERSION_MAJOR >= 22
+	if (jit_debugging_support)
+	{
+		error = LLVMOrcLLJITEnableDebugSupport(lljit);
+		if (error)
+			elog(ERROR, "failed to enable JIT debugging support: %s",
+				 llvm_error_message(error));
+	}
+
+	if (jit_profiling_support)
+	{
+		// This requires llvm with https://github.com/bonnefoa/llvm-project/tree/jitlink-perf
+		error = LLVMOrcLLJITEnablePerfSupport(lljit);
+		if (error)
+			elog(ERROR, "failed to enable JIT perf support: %s",
+				 llvm_error_message(error));
+	}
+#endif
+
 	/*
 	 * Symbol resolution support for symbols in the postgres binary /
 	 * libraries already loaded.
diff --git a/src/backend/jit/llvm/llvmjit_wrap.cpp b/src/backend/jit/llvm/llvmjit_wrap.cpp
index 9cba4b96e45..6a7d63444f8 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.51.0

Reply via email to