SectionMemoryManager-diff.txt
text/plain
--- - 2026-01-02 17:03:43.795228000 +1300
+++ src/include/jit/SectionMemoryManager.h 2026-01-02 16:52:50.352095000 +1300
@@ -1,3 +1,8 @@
+/*
+ * This is a copy of LLVM source code modified by the PostgreSQL project.
+ * See SectionMemoryManager.cpp for notes on provenance and license.
+ */
+
//===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -11,18 +16,19 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
-#define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
+#ifndef LLVM_EXECUTIONENGINE_BACKPORT_SECTIONMEMORYMANAGER_H
+#define LLVM_EXECUTIONENGINE_BACKPORT_SECTIONMEMORYMANAGER_H
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
-#include "llvm/Support/Compiler.h"
-#include "llvm/Support/Memory.h"
+#include <llvm/ADT/SmallVector.h>
+#include <llvm/ExecutionEngine/RTDyldMemoryManager.h>
+#include <llvm/Support/Alignment.h>
+#include <llvm/Support/Memory.h>
#include <cstdint>
#include <string>
#include <system_error>
namespace llvm {
+namespace backport {
/// This is a simple memory manager which implements the methods called by
/// the RuntimeDyld class to allocate memory for section-based loading of
@@ -37,7 +43,7 @@
/// in the JITed object. Permissions can be applied either by calling
/// MCJIT::finalizeObject or by calling SectionMemoryManager::finalizeMemory
/// directly. Clients of MCJIT should call MCJIT::finalizeObject.
-class LLVM_ABI SectionMemoryManager : public RTDyldMemoryManager {
+class SectionMemoryManager : public RTDyldMemoryManager {
public:
/// This enum describes the various reasons to allocate pages from
/// allocateMappedMemory.
@@ -49,7 +55,7 @@
/// Implementations of this interface are used by SectionMemoryManager to
/// request pages from the operating system.
- class LLVM_ABI MemoryMapper {
+ class MemoryMapper {
public:
/// This method attempts to allocate \p NumBytes bytes of virtual memory for
/// \p Purpose. \p NearBlock may point to an existing allocation, in which
@@ -119,9 +125,17 @@
/// Implements allocating all memory in a single block. This is required to
/// limit memory offsets to fit the ARM ABI; large memory systems may
/// otherwise allocate separate sections too far apart.
+#if LLVM_VERSION_MAJOR < 16
+ virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
+ uintptr_t RODataSize,
+ uint32_t RODataAlign,
+ uintptr_t RWDataSize,
+ uint32_t RWDataAlign) override;
+#else
void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
uintptr_t RODataSize, Align RODataAlign,
uintptr_t RWDataSize, Align RWDataAlign) override;
+#endif
/// Allocates a memory block of (at least) the given size suitable for
/// executable code.
@@ -139,7 +153,7 @@
/// a default alignment of 16 will be used.
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, StringRef SectionName,
- bool isReadOnly) override;
+ bool IsReadOnly) override;
/// Update section-specific memory permissions and other attributes.
///
@@ -168,7 +182,7 @@
// The actual block of free memory
sys::MemoryBlock Free;
// If there is a pending allocation from the same reservation right before
- // this block, store it's index in PendingMem, to be able to update the
+ // this block, store its index in PendingMem, to be able to update the
// pending region if part of this block is allocated, rather than having to
// create a new one
unsigned PendingPrefixIndex;
@@ -206,6 +220,7 @@
bool ReserveAllocation;
};
+} // end namespace backport
} // end namespace llvm
-#endif // LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
+#endif // LLVM_EXECUTIONENGINE_BACKPORT_SECTIONMEMORYMANAGER_H
--- - 2026-01-02 17:03:57.672285000 +1300
+++ src/backend/jit/llvm/SectionMemoryManager.cpp 2026-01-02 16:52:50.351938000 +1300
@@ -1,3 +1,19 @@
+/*
+ * 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.
+ *
+ * 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
+ * covered by the license at https://llvm.org/LICENSE.txt, reproduced
+ * verbatim in SectionMemoryManager.LICENSE in fulfillment of clause
+ * 4a.
+ */
+
//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -11,11 +27,16 @@
//
//===----------------------------------------------------------------------===//
-#include "llvm/ExecutionEngine/SectionMemoryManager.h"
-#include "llvm/Config/config.h"
-#include "llvm/Support/Process.h"
+#include "jit/llvmjit_backport.h"
+#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
+
+#include "jit/SectionMemoryManager.h"
+#include <llvm/Support/MathExtras.h>
+#include <llvm/Support/Process.h>
+
namespace llvm {
+namespace backport {
bool SectionMemoryManager::hasSpace(const MemoryGroup &MemGroup,
uintptr_t Size) const {
@@ -26,9 +47,21 @@
return false;
}
+#if LLVM_VERSION_MAJOR < 16
+void SectionMemoryManager::reserveAllocationSpace(uintptr_t CodeSize,
+ uint32_t CodeAlign_i,
+ uintptr_t RODataSize,
+ uint32_t RODataAlign_i,
+ uintptr_t RWDataSize,
+ uint32_t RWDataAlign_i) {
+ Align CodeAlign(CodeAlign_i);
+ Align RODataAlign(RODataAlign_i);
+ Align RWDataAlign(RWDataAlign_i);
+#else
void SectionMemoryManager::reserveAllocationSpace(
uintptr_t CodeSize, Align CodeAlign, uintptr_t RODataSize,
Align RODataAlign, uintptr_t RWDataSize, Align RWDataAlign) {
+#endif
if (CodeSize == 0 && RODataSize == 0 && RWDataSize == 0)
return;
@@ -365,4 +398,7 @@
}
}
+} // namespace backport
} // namespace llvm
+
+#endif