File riscv.patch of Package postgresql18

Index: postgresql-18.0/config/llvm.m4
===================================================================
--- postgresql-18.0.orig/config/llvm.m4
+++ postgresql-18.0/config/llvm.m4
@@ -76,6 +76,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
       engine) pgac_components="$pgac_components $pgac_component";;
       debuginfodwarf) pgac_components="$pgac_components $pgac_component";;
       orcjit) pgac_components="$pgac_components $pgac_component";;
+      jitlink) pgac_components="$pgac_components $pgac_component";;
       passes) pgac_components="$pgac_components $pgac_component";;
       native) pgac_components="$pgac_components $pgac_component";;
       perfjitevents) pgac_components="$pgac_components $pgac_component";;
Index: postgresql-18.0/configure
===================================================================
--- postgresql-18.0.orig/configure
+++ postgresql-18.0/configure
@@ -5215,6 +5215,7 @@ fi
       engine) pgac_components="$pgac_components $pgac_component";;
       debuginfodwarf) pgac_components="$pgac_components $pgac_component";;
       orcjit) pgac_components="$pgac_components $pgac_component";;
+      jitlink) pgac_components="$pgac_components $pgac_component";;
       passes) pgac_components="$pgac_components $pgac_component";;
       native) pgac_components="$pgac_components $pgac_component";;
       perfjitevents) pgac_components="$pgac_components $pgac_component";;
Index: postgresql-18.0/src/backend/jit/llvm/llvmjit.c
===================================================================
--- postgresql-18.0.orig/src/backend/jit/llvm/llvmjit.c
+++ postgresql-18.0/src/backend/jit/llvm/llvmjit.c
@@ -315,6 +315,11 @@ llvm_release_context(JitContext *context
 LLVMModuleRef
 llvm_mutable_module(LLVMJitContext *context)
 {
+#ifdef __riscv
+	const char *abiname;
+	const char *target_abi = "target-abi";
+	LLVMMetadataRef abi_metadata;
+#endif
 	llvm_assert_in_fatal_section();
 
 	/*
@@ -327,6 +332,40 @@ llvm_mutable_module(LLVMJitContext *cont
 		context->module = LLVMModuleCreateWithNameInContext("pg", llvm_context);
 		LLVMSetTarget(context->module, llvm_triple);
 		LLVMSetDataLayout(context->module, llvm_layout);
+#ifdef __riscv
+#if __riscv_xlen == 64
+#ifdef __riscv_float_abi_double
+		abiname = "lp64d";
+#elif defined(__riscv_float_abi_single)
+		abiname = "lp64f";
+#else
+		abiname = "lp64";
+#endif
+#elif __riscv_xlen == 32
+#ifdef __riscv_float_abi_double
+		abiname = "ilp32d";
+#elif defined(__riscv_float_abi_single)
+		abiname = "ilp32f";
+#else
+		abiname = "ilp32";
+#endif
+#else
+		elog(ERROR, "unsupported riscv xlen %d", __riscv_xlen);
+#endif
+		/*
+		 * set this manually to avoid llvm defaulting to soft float and
+		 * resulting in linker error: `can't link double-float modules
+		 * with soft-float modules`
+		 * we could set this for TargetMachine via MCOptions, but there
+		 * is no C API for it
+		 * ref: https://github.com/llvm/llvm-project/blob/afa520ab34803c82587ea6759bfd352579f741b4/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp#L90
+		 */
+		abi_metadata = LLVMMDStringInContext2(
+			LLVMGetModuleContext(context->module),
+			abiname, strlen(abiname));
+		LLVMAddModuleFlag(context->module, LLVMModuleFlagBehaviorOverride,
+			target_abi, strlen(target_abi), abi_metadata);
+#endif
 	}
 
 	return context->module;
@@ -854,6 +893,8 @@ llvm_session_initialize(void)
 	char	   *cpu = NULL;
 	char       *host_features = NULL;
 	char	   *features = NULL;
+	LLVMRelocMode reloc = LLVMRelocDefault;
+	LLVMCodeModel codemodel = LLVMCodeModelJITDefault;
 	LLVMTargetMachineRef opt0_tm;
 	LLVMTargetMachineRef opt3_tm;
 
@@ -908,16 +949,21 @@ llvm_session_initialize(void)
 		sprintf(features, "%s%s", host_features, no_vector);
 	}
 
+#ifdef __riscv
+	reloc = LLVMRelocPIC;
+	codemodel = LLVMCodeModelLarge;
+#endif
+
 	opt0_tm =
 		LLVMCreateTargetMachine(llvm_targetref, llvm_triple, cpu, features,
 								LLVMCodeGenLevelNone,
-								LLVMRelocDefault,
-								LLVMCodeModelJITDefault);
+								reloc,
+								codemodel);
 	opt3_tm =
 		LLVMCreateTargetMachine(llvm_targetref, llvm_triple, cpu, features,
 								LLVMCodeGenLevelAggressive,
-								LLVMRelocDefault,
-								LLVMCodeModelJITDefault);
+								reloc,
+								codemodel);
 
 	LLVMDisposeMessage(cpu);
 	cpu = NULL;
@@ -1212,6 +1258,10 @@ llvm_log_jit_error(void *ctx, LLVMErrorR
 static LLVMOrcObjectLayerRef
 llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple)
 {
+#if defined(USE_LLVM_JITLINK)
+	LLVMOrcObjectLayerRef objlayer =
+		LLVMOrcCreateJITLinkObjectLinkingLayer(ES);
+#else
 #ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
 	LLVMOrcObjectLayerRef objlayer =
 		LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(ES);
@@ -1229,6 +1279,7 @@ llvm_create_object_layer(void *Ctx, LLVM
 		LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(objlayer, l);
 	}
 #endif
+#endif
 
 #if defined(HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER) && HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER
 	if (jit_profiling_support)
Index: postgresql-18.0/src/backend/jit/llvm/llvmjit_wrap.cpp
===================================================================
--- postgresql-18.0.orig/src/backend/jit/llvm/llvmjit_wrap.cpp
+++ postgresql-18.0/src/backend/jit/llvm/llvmjit_wrap.cpp
@@ -28,6 +28,9 @@ extern "C"
 #include "jit/SectionMemoryManager.h"
 #include <llvm/Support/CBindingWrapping.h>
 #endif
+#ifdef USE_LLVM_JITLINK
+#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
+#endif
 
 
 /*
@@ -46,9 +49,28 @@ LLVMGetFunctionType(LLVMValueRef r)
 	return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getFunctionType());
 }
 
-#ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
+#if defined(USE_LLVM_JITLINK) || defined(USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER)
 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ExecutionSession, LLVMOrcExecutionSessionRef)
-DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ObjectLayer, LLVMOrcObjectLayerRef);
+DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ObjectLayer, LLVMOrcObjectLayerRef)
+#endif
+
+#if defined(USE_LLVM_JITLINK)
+
+LLVMOrcObjectLayerRef
+LLVMOrcCreateJITLinkObjectLinkingLayer(LLVMOrcExecutionSessionRef ES)
+{
+	auto expected_mm = llvm::jitlink::InProcessMemoryManager::Create();
+
+	/* XXX how should this error be reported? */
+	if (!expected_mm)
+		elog(ERROR,
+			 "could not create JITLink memory manager: %s",
+			 llvm::toString(expected_mm.takeError()).c_str());
+
+	return wrap(new llvm::orc::ObjectLinkingLayer(*unwrap(ES), std::move(*expected_mm)));
+}
+
+#elif defined(USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER)
 
 LLVMOrcObjectLayerRef
 LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef ES)
Index: postgresql-18.0/src/include/jit/llvmjit.h
===================================================================
--- postgresql-18.0.orig/src/include/jit/llvmjit.h
+++ postgresql-18.0/src/include/jit/llvmjit.h
@@ -24,6 +24,11 @@
 #include <llvm-c/OrcEE.h>
 #endif
 
+#if defined(__riscv) && LLVM_VERSION_MAJOR >= 15
+#include <llvm-c/Orc.h>
+#define USE_LLVM_JITLINK
+/* else use legacy RTDyld */
+#endif
 
 /*
  * File needs to be includable by both C and C++ code, and include other
@@ -144,6 +149,10 @@ extern LLVMTypeRef LLVMGetFunctionType(L
 extern LLVMOrcObjectLayerRef LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef ES);
 #endif
 
+#ifdef USE_LLVM_JITLINK
+extern LLVMOrcObjectLayerRef LLVMOrcCreateJITLinkObjectLinkingLayer(LLVMOrcExecutionSessionRef ES);
+#endif
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
openSUSE Build Service is sponsored by