From cac92d0786db7686cf449ff1535d8da558eec206 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
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

