jit: Changes for LLVM 17.
authorThomas Munro
Wed, 18 Oct 2023 09:15:54 +0000 (22:15 +1300)
committerThomas Munro
Wed, 18 Oct 2023 16:14:07 +0000 (05:14 +1300)
Changes required by https://llvm.org/docs/NewPassManager.html.

Back-patch to 12, leaving the final release of 11 unchanged, consistent
with earlier decision not to back-patch LLVM 16 support either.

Author: Dmitry Dolgov <[email protected]>
Reviewed-by: Andres Freund
Reviewed-by: Thomas Munro
Discussion: https://postgr.es/m/CA%2BhUKG%2BWXznXCyTgCADd%3DHWkP9Qksa6chd7L%3DGCnZo-MBgg9Lg%40mail.gmail.com

src/backend/jit/llvm/llvmjit.c
src/backend/jit/llvm/llvmjit_wrap.cpp

index 9e7ed1c2d2530119e1a5f9dcf149811c0ab46bed..252efb68b1f734c785b5b489f7e79b15335b8c70 100644 (file)
@@ -18,6 +18,9 @@
 #include 
 #include 
 #include 
+#if LLVM_VERSION_MAJOR > 16
+#include 
+#endif
 #if LLVM_VERSION_MAJOR > 11
 #include 
 #include 
 #endif
 #include 
 #include 
+#if LLVM_VERSION_MAJOR < 17
 #include 
 #include 
 #include 
 #if LLVM_VERSION_MAJOR > 6
 #include 
 #endif
+#endif
 
 #include "jit/llvmjit.h"
 #include "jit/llvmjit_emit.h"
@@ -561,6 +566,7 @@ llvm_function_reference(LLVMJitContext *context,
 static void
 llvm_optimize_module(LLVMJitContext *context, LLVMModuleRef module)
 {
+#if LLVM_VERSION_MAJOR < 17
    LLVMPassManagerBuilderRef llvm_pmb;
    LLVMPassManagerRef llvm_mpm;
    LLVMPassManagerRef llvm_fpm;
@@ -624,6 +630,31 @@ llvm_optimize_module(LLVMJitContext *context, LLVMModuleRef module)
    LLVMDisposePassManager(llvm_mpm);
 
    LLVMPassManagerBuilderDispose(llvm_pmb);
+#else
+   LLVMPassBuilderOptionsRef options;
+   LLVMErrorRef err;
+   const char *passes;
+
+   if (context->base.flags & PGJIT_OPT3)
+       passes = "default";
+   else
+       passes = "default,mem2reg";
+
+   options = LLVMCreatePassBuilderOptions();
+
+#ifdef LLVM_PASS_DEBUG
+   LLVMPassBuilderOptionsSetDebugLogging(options, 1);
+#endif
+
+   LLVMPassBuilderOptionsSetInlinerThreshold(options, 512);
+
+   err = LLVMRunPasses(module, passes, NULL, options);
+
+   if (err)
+       elog(ERROR, "failed to JIT module: %s", llvm_error_message(err));
+
+   LLVMDisposePassBuilderOptions(options);
+#endif
 }
 
 /*
index cb896e2b6a521dc6f843b92154f4d5ba77f053c8..90a41b919123be6d7b2fe2283e08178c2fde8dee 100644 (file)
@@ -23,8 +23,14 @@ extern "C"
 
 #include 
 #include 
+#if LLVM_VERSION_MAJOR < 17
 #include 
+#endif
+#if LLVM_VERSION_MAJOR > 16
+#include 
+#else
 #include 
+#endif
 
 #include "jit/llvmjit.h"