From a4bdd2eed699938660a3a942a674359ac92381e4 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sat, 18 Dec 2021 23:09:03 +0100 Subject: main/postgresql14: fix JIT on x86 and make s390x workaround conditional --- main/postgresql14/APKBUILD | 6 +- .../jit-datalayout-mismatch-on-s390x-and-x86.patch | 111 +++++++++++++++++++++ .../jit-datalayout-mismatch-on-s390x.patch | 100 ------------------- 3 files changed, 114 insertions(+), 103 deletions(-) create mode 100644 main/postgresql14/jit-datalayout-mismatch-on-s390x-and-x86.patch delete mode 100644 main/postgresql14/jit-datalayout-mismatch-on-s390x.patch diff --git a/main/postgresql14/APKBUILD b/main/postgresql14/APKBUILD index 8cd1e19121f..26ed71d5174 100644 --- a/main/postgresql14/APKBUILD +++ b/main/postgresql14/APKBUILD @@ -3,7 +3,7 @@ # Contributor: Jakub Jirutka _pkgname=postgresql pkgver=14.1 -pkgrel=6 +pkgrel=7 _majorver=${pkgver%%.*} # Should this aport provide libpq* and libecpg*? true/false # Exactly one postgresql aport must be the default one! @@ -75,7 +75,7 @@ source="https://ftp.postgresql.org/pub/source/v$pkgver/postgresql-$pkgver.tar.bz remove-libecpg_compat.patch czech-snowball-stemmer.patch make-split-headers.patch - jit-datalayout-mismatch-on-s390x.patch + jit-datalayout-mismatch-on-s390x-and-x86.patch pg_config-add-major-version.patch libpgport-pkglibdir.patch.txt external-libpq.patch.txt @@ -522,7 +522,7 @@ c4179fcd8b71791cdc41ea7b622cf82e9bd42ac1de66999234b98a83c0c508c79c492a9301274fe8 d8eb4274a54b94bed4a2ded7ae775c5a95ca0f051b831b859ccf78bf6d2ea6fe89a9a0611771f6ad85573995a7e3af1fdf5859e20cae3267a52239f12e1b61c3 remove-libecpg_compat.patch 673707b331cd09c105d18f39e83ec39371886d5d8cf706b6ebbd30ace294fa3377c391bea1ab409c001644c90ac718e3250cbc4acd9224bc0e7ac1f86c832424 czech-snowball-stemmer.patch 5262f4944844bccc839c4441570b9eb2e0792390234ebfdb8ebb7b83380ce5f5de84b038cb03045526da202a185af9c3972c2ae1b9e0e743a95c6e84f4621cf9 make-split-headers.patch -87f470bb8d08050dd955bd6f1da291891970027e6cd001452efc0bd6a57ac46187082393cd5910c289fc6e1daaf0967415393629379c07aed4e78b8e87e5fff7 jit-datalayout-mismatch-on-s390x.patch +c24986becfd73546a062612335e53b4da34829e42a0c30e85ed7481924f13b1fd0a0e5079d680905a52afea1e99f2210cc3692c529a34a74cd6961c88cce9163 jit-datalayout-mismatch-on-s390x-and-x86.patch 7790e4e4374f7bdc6b4484ba87a5fa709d30d3cbdce61ee7bf9c5dfce40cb51c7bd54ab42f4050fb48eede08ef573624d819128e57cc8c976e01202854740308 pg_config-add-major-version.patch f8ed2b7b96fd22cd87c982151e659d82bcae10033a97f403f7847fce6daa8fc580e998cfb3813af9cb59a12f0c6bcc276397c28b1fc48321eed8c7ba5f3f92ed libpgport-pkglibdir.patch.txt 6078defb3da67e7df96665cc130d32b69eebfcaf49d92eef368ea8eea8bb311fab56064c104bc97f53da9cd925301bef696b506af33e0b66d65bc6cd41ec7499 external-libpq.patch.txt diff --git a/main/postgresql14/jit-datalayout-mismatch-on-s390x-and-x86.patch b/main/postgresql14/jit-datalayout-mismatch-on-s390x-and-x86.patch new file mode 100644 index 00000000000..807a5f1b767 --- /dev/null +++ b/main/postgresql14/jit-datalayout-mismatch-on-s390x-and-x86.patch @@ -0,0 +1,111 @@ +From: Tom Stellard +From: Jakub Jirutka +Date: Sat, 18 Dec 2021 23:09:03 +0100 +Subject: [PATCH] jit: Workaround datalayout mismatch on s390x and x86 + +This patch is based on https://src.fedoraproject.org/rpms/postgresql/blob/f35/f/postgresql-datalayout-mismatch-on-s390.patch. + +Original description: + +> LLVM's s390x target uses a different datalayout for z13 and newer processors. +> If llvmjit_types.bc is compiled to target a processor older than z13, and +> then the JIT runs on a z13 or newer processor, then there will be a mismatch +> in datalayouts between llvmjit_types.bc and the JIT engine. This mismatch +> causes the JIT to fail at runtime. + +We encountered an analogous problem even on x86 (legacy 32bit arch). +However, I didn't wanna waste my time researching what exact CPU features +are problematic on this dead architecture, so I just disabled usage of any +host specific CPU features when creating the JIT on x86. And while I was on +it, I also conditioned the s390x workaround for s390x only. -jirutka + +diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c +index 98a27f08bf..05b6438ba8 100644 +--- a/src/backend/jit/llvm/llvmjit.c ++++ b/src/backend/jit/llvm/llvmjit.c +@@ -776,7 +776,38 @@ + errhidecontext(true))); + } + ++#if defined(__s390__) || defined(__s390x__) + /* ++ * For the systemz target, LLVM uses a different datalayout for z13 and newer ++ * CPUs than it does for older CPUs. This can cause a mismatch in datalayouts ++ * in the case where the llvm_types_module is compiled with a pre-z13 CPU ++ * and the JIT is running on z13 or newer. ++ * See computeDataLayout() function in ++ * llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp for information on the ++ * datalayout differences. ++ */ ++static bool ++needs_systemz_workaround(void) ++{ ++ bool ret = false; ++ LLVMContextRef llvm_context; ++ LLVMTypeRef vec_type; ++ LLVMTargetDataRef llvm_layoutref; ++ if (strncmp(LLVMGetTargetName(llvm_targetref), "systemz", strlen("systemz"))) ++ { ++ return false; ++ } ++ ++ llvm_context = LLVMGetModuleContext(llvm_types_module); ++ vec_type = LLVMVectorType(LLVMIntTypeInContext(llvm_context, 32), 4); ++ llvm_layoutref = LLVMCreateTargetData(llvm_layout); ++ ret = (LLVMABIAlignmentOfType(llvm_layoutref, vec_type) == 16); ++ LLVMDisposeTargetData(llvm_layoutref); ++ return ret; ++} ++#endif ++ ++/* + * Per session initialization. + */ + static void +@@ -785,6 +816,7 @@ + MemoryContext oldcontext; + char *error = NULL; + char *cpu = NULL; ++ char *host_features = NULL; + char *features = NULL; + LLVMTargetMachineRef opt0_tm; + LLVMTargetMachineRef opt3_tm; +@@ -815,11 +847,22 @@ + * latter is needed because some CPU architectures default to enabling + * features not all CPUs have (weird, huh). + */ ++#if !defined(__i386__) && !defined(__i386) // XXX: quick workaround for 32-bit x86 + cpu = LLVMGetHostCPUName(); +- features = LLVMGetHostCPUFeatures(); ++ features = host_features = LLVMGetHostCPUFeatures(); + elog(DEBUG2, "LLVMJIT detected CPU \"%s\", with features \"%s\"", + cpu, features); ++#endif + ++#if defined(__s390__) || defined(__s390x__) ++ if (needs_systemz_workaround()) ++ { ++ const char *no_vector =",-vector"; ++ features = malloc(sizeof(char) * (strlen(host_features) + strlen(no_vector) + 1)); ++ sprintf(features, "%s%s", host_features, no_vector); ++ } ++#endif ++ + opt0_tm = + LLVMCreateTargetMachine(llvm_targetref, llvm_triple, cpu, features, + LLVMCodeGenLevelNone, +@@ -833,8 +876,13 @@ + + LLVMDisposeMessage(cpu); + cpu = NULL; +- LLVMDisposeMessage(features); ++ if (features != host_features) ++ { ++ free(features); ++ } + features = NULL; ++ LLVMDisposeMessage(host_features); ++ host_features = NULL; + + /* force symbols in main binary to be loaded */ + LLVMLoadLibraryPermanently(NULL); diff --git a/main/postgresql14/jit-datalayout-mismatch-on-s390x.patch b/main/postgresql14/jit-datalayout-mismatch-on-s390x.patch deleted file mode 100644 index bce5c61e159..00000000000 --- a/main/postgresql14/jit-datalayout-mismatch-on-s390x.patch +++ /dev/null @@ -1,100 +0,0 @@ -Patch-Source: https://src.fedoraproject.org/rpms/postgresql/blob/f35/f/postgresql-datalayout-mismatch-on-s390.patch -From 0edaa982336823d4d7af8f10b91579fe0099ef3d Mon Sep 17 00:00:00 2001 -From: Tom Stellard -Date: Tue, 20 Apr 2021 20:14:21 -0700 -Subject: [PATCH] jit: Workaround potential datalayout mismatch on s390x - -LLVM's s390x target uses a different datalayout for z13 and newer processors. -If llvmjit_types.bc is compiled to target a processor older than z13, and -then the JIT runs on a z13 or newer processor, then there will be a mismatch -in datalayouts between llvmjit_types.bc and the JIT engine. This mismatch -causes the JIT to fail at runtime. ---- - src/backend/jit/llvm/llvmjit.c | 46 ++++++++++++++++++++++++++++++++-- - 1 file changed, 44 insertions(+), 2 deletions(-) - -diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c -index 98a27f08bf..05b6438ba8 100644 ---- a/src/backend/jit/llvm/llvmjit.c -+++ b/src/backend/jit/llvm/llvmjit.c -@@ -776,6 +776,35 @@ llvm_compile_module(LLVMJitContext *context) - errhidecontext(true))); - } - -+/* -+ * For the systemz target, LLVM uses a different datalayout for z13 and newer -+ * CPUs than it does for older CPUs. This can cause a mismatch in datalayouts -+ * in the case where the llvm_types_module is compiled with a pre-z13 CPU -+ * and the JIT is running on z13 or newer. -+ * See computeDataLayout() function in -+ * llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp for information on the -+ * datalayout differences. -+ */ -+static bool -+needs_systemz_workaround(void) -+{ -+ bool ret = false; -+ LLVMContextRef llvm_context; -+ LLVMTypeRef vec_type; -+ LLVMTargetDataRef llvm_layoutref; -+ if (strncmp(LLVMGetTargetName(llvm_targetref), "systemz", strlen("systemz"))) -+ { -+ return false; -+ } -+ -+ llvm_context = LLVMGetModuleContext(llvm_types_module); -+ vec_type = LLVMVectorType(LLVMIntTypeInContext(llvm_context, 32), 4); -+ llvm_layoutref = LLVMCreateTargetData(llvm_layout); -+ ret = (LLVMABIAlignmentOfType(llvm_layoutref, vec_type) == 16); -+ LLVMDisposeTargetData(llvm_layoutref); -+ return ret; -+} -+ - /* - * Per session initialization. - */ -@@ -785,6 +814,7 @@ llvm_session_initialize(void) - MemoryContext oldcontext; - char *error = NULL; - char *cpu = NULL; -+ char *host_features = NULL; - char *features = NULL; - LLVMTargetMachineRef opt0_tm; - LLVMTargetMachineRef opt3_tm; -@@ -816,10 +846,17 @@ llvm_session_initialize(void) - * features not all CPUs have (weird, huh). - */ - cpu = LLVMGetHostCPUName(); -- features = LLVMGetHostCPUFeatures(); -+ features = host_features = LLVMGetHostCPUFeatures(); - elog(DEBUG2, "LLVMJIT detected CPU \"%s\", with features \"%s\"", - cpu, features); - -+ if (needs_systemz_workaround()) -+ { -+ const char *no_vector =",-vector"; -+ features = malloc(sizeof(char) * (strlen(host_features) + strlen(no_vector) + 1)); -+ sprintf(features, "%s%s", host_features, no_vector); -+ } -+ - opt0_tm = - LLVMCreateTargetMachine(llvm_targetref, llvm_triple, cpu, features, - LLVMCodeGenLevelNone, -@@ -833,8 +870,13 @@ llvm_session_initialize(void) - - LLVMDisposeMessage(cpu); - cpu = NULL; -- LLVMDisposeMessage(features); -+ if (features != host_features) -+ { -+ free(features); -+ } - features = NULL; -+ LLVMDisposeMessage(host_features); -+ host_features = NULL; - - /* force symbols in main binary to be loaded */ - LLVMLoadLibraryPermanently(NULL); --- -2.27.0 - -- cgit v1.2.3