]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
jit: No backport::SectionMemoryManager for LLVM 22.
authorThomas Munro <tmunro@postgresql.org>
Fri, 3 Apr 2026 01:48:54 +0000 (14:48 +1300)
committerThomas Munro <tmunro@postgresql.org>
Fri, 3 Apr 2026 02:01:56 +0000 (15:01 +1300)
LLVM 22 has the fix that we copied into our tree in commit 9044fc1d and
a new function to reach it[1][2], so we only need to use our copy for
Aarch64 + LLVM < 22.  The only change to the final version that our copy
didn't get is a new LLVM_ABI macro, but that isn't appropriate for us.
Our copy is hopefully now frozen and would only need maintenance if bugs
are found in the upstream code.

Non-Aarch64 systems now also use the new API with LLVM 22.  It allocates
all sections with one contiguous mmap() instead of one per
section.  We could have done that earlier, but commit 9044fc1d wanted to
limit the blast radius to the affected systems.  We might as well
benefit from that small improvement everywhere now that it is available
out of the box.

We can't delete our copy until LLVM 22 is our minimum supported version,
or we switch to the newer JITLink API for at least Aarch64.

[1] https://github.com/llvm/llvm-project/pull/71968
[2] https://github.com/llvm/llvm-project/pull/174307

Backpatch-through: 14
Discussion: https://postgr.es/m/CA%2BhUKGJTumad75o8Zao-LFseEbt%3DenbUFCM7LZVV%3Dc8yg2i7dg%40mail.gmail.com

src/backend/jit/llvm/SectionMemoryManager.cpp
src/backend/jit/llvm/llvmjit.c
src/include/jit/SectionMemoryManager.h
src/include/jit/llvmjit_backport.h

index c4fbf15a961c2614debd0fa89ec855123d84f491..f04e4e4231ea3d35329acfe2ecf65fc8c04d699b 100644 (file)
@@ -1,18 +1,11 @@
 /*
- * This file is from https://github.com/llvm/llvm-project/pull/71968
- * with minor modifications to avoid name clash and work with older
- * LLVM versions.  The llvm::backport::SectionMemoryManager class is a
- * drop-in replacement for llvm::SectionMemoryManager, for use with
- * llvm::RuntimeDyld.  It fixes a memory layout bug on large memory
- * ARM systems (see pull request for details).  If the LLVM project
- * eventually commits the change, we may need to resynchronize our
- * copy with any further modifications, but they would be unlikely to
- * backport it into the LLVM versions that we target so we would still
- * need this copy.
+ * This file is from LLVM 22 (originally pull request #71968), with minor
+ * modifications to avoid name clash and work with older LLVM versions.  It
+ * replaces llvm::SectionMemoryManager, and is injected into llvm::RuntimeDyld
+ * to fix a memory layout bug on large memory ARM systems on LLVM < 22.
  *
- * In the future we will switch to using JITLink instead of
- * RuntimeDyld where possible, and later remove this code (.cpp, .h,
- * .LICENSE) after all LLVM versions that we target allow it.
+ * We can remove this code (.cpp, .h, .LICENSE) once LLVM 22 is our minimum
+ * supported version or we've switched to JITLink for at least Aarch64.
  *
  * This file is a modified copy of a part of the LLVM source code that
  * we would normally access from the LLVM library.  It is therefore
index c2d8578f9c21b00238f7e0627e36ee899fc23fc4..c96e2d87ac6ba696c824f49e4429853798365290 100644 (file)
@@ -1321,7 +1321,10 @@ llvm_log_jit_error(void *ctx, LLVMErrorRef error)
 static LLVMOrcObjectLayerRef
 llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple)
 {
-#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
+#if LLVM_VERSION_MAJOR >= 22
+       LLVMOrcObjectLayerRef objlayer =
+               LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManagerReserveAlloc(ES, true);
+#elif defined(USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER)
        LLVMOrcObjectLayerRef objlayer =
                LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(ES);
 #else
index 93cf97715706377e20c8cc084de4e221bad81e1e..aac78b5bd7e6957e0b362cd111ad57eda17d09ea 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * This is a copy LLVM source code modified by the PostgreSQL project.
+ * This is a copy of LLVM source code modified by the PostgreSQL project.
  * See SectionMemoryManager.cpp for notes on provenance and license.
  */
 
index 92874f7998ca648cd6c4cd0b6b3de2159df2a013..04851c9b68ad9be01edc68a1e987672a8da8ebca 100644 (file)
@@ -8,7 +8,7 @@
 #include <llvm/Config/llvm-config.h>
 
 /*
- * LLVM's RuntimeDyld can produce code that crashes on larger memory ARM
+ * Pre-LLVM 22 RuntimeDyld can produce code that crashes on large memory ARM
  * systems, because llvm::SectionMemoryManager allocates multiple pieces of
  * memory that can be placed too far apart for the generated code.  See
  * src/backend/jit/llvm/SectionMemoryManager.cpp for the patched replacement
@@ -18,7 +18,7 @@
  * We have adjusted it to compile against a range of LLVM versions, but not
  * further back than 12 for now.
  */
-#if defined(__aarch64__) && LLVM_VERSION_MAJOR > 11
+#if defined(__aarch64__) && LLVM_VERSION_MAJOR > 11 && LLVM_VERSION_MAJOR < 22
 #define USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
 #endif