From 93bd96a1afb37c0f68f42bef0e1a3d85a60c8da3 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 10 Oct 2024 13:31:56 +1300
Subject: 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.
---
 src/backend/jit/llvm/llvmjit.c        | 39 ++++++++++++++++++++++++---
 src/backend/jit/llvm/llvmjit_wrap.cpp |  2 ++
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 8d009dd5cf7..044ae50078c 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;
 }
 
@@ -1225,6 +1243,19 @@ llvm_create_jit_instance(LLVMTargetMachineRef tm)
 		elog(ERROR, "failed to create lljit instance: %s",
 			 llvm_error_message(error));
 
+#if LLVM_VERSION_MAJOR >= 22
+	if (jit_debugging_support)
+	{
+		LLVMOrcLLJITEnableDebugSupport(lljit);
+	}
+
+	if (jit_profiling_support)
+	{
+    // This requires llvm with https://github.com/bonnefoa/llvm-project/tree/jitlink-perf
+		LLVMOrcLLJITEnablePerfSupport(lljit);
+	}
+#endif
+
 	LLVMOrcExecutionSessionSetErrorReporter(LLVMOrcLLJITGetExecutionSession(lljit),
 											llvm_log_jit_error, NULL);
 
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

