From 41cd60000b91c121e1286c194284bffec770081b Mon Sep 17 00:00:00 2001 From: Honza Horak Date: Thu, 22 Apr 2021 20:36:42 +0200 Subject: [PATCH] Work-around fix jit failure on s390x Consulted upstream in https://www.postgresql.org/message-id/20210420225228.qr4x6zv3hqjorh5t%40alap3.anarazel.de This problem could in theory happen on any architecture depending on which specific CPU variant is being used. We just only see this problem in Fedora on the s390x builder. Related: #1940964 --- postgresql-datalayout-mismatch-on-s390.patch | 99 ++++++++++++++++++++ postgresql.spec | 15 ++- 2 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 postgresql-datalayout-mismatch-on-s390.patch diff --git a/postgresql-datalayout-mismatch-on-s390.patch b/postgresql-datalayout-mismatch-on-s390.patch new file mode 100644 index 0000000..eef0378 --- /dev/null +++ b/postgresql-datalayout-mismatch-on-s390.patch @@ -0,0 +1,99 @@ +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 + diff --git a/postgresql.spec b/postgresql.spec index f2400e5..3384d82 100644 --- a/postgresql.spec +++ b/postgresql.spec @@ -32,9 +32,7 @@ %{!?beta:%global beta 0} %{!?test:%global test 1} -# Disable temporarily to be able to build the package -# tracked in RHBZ#1940964 -%{!?llvmjit:%global llvmjit 0} +%{!?llvmjit:%global llvmjit 1} %{!?upgrade:%global upgrade 1} %{!?plpython3:%global plpython3 1} %{!?pltcl:%global pltcl 1} @@ -62,7 +60,7 @@ Summary: PostgreSQL client programs Name: postgresql %global majorversion 13 Version: %{majorversion}.2 -Release: 5%{?dist} +Release: 6%{?dist} # The PostgreSQL license is very similar to other MIT licenses, but the OSI # recognizes it as an independent license, so we do as well. @@ -109,6 +107,9 @@ Patch2: postgresql-logging.patch Patch5: postgresql-var-run-socket.patch Patch8: postgresql-external-libpq.patch Patch9: postgresql-server-pg_config.patch +# Upstream bug #16971: https://www.postgresql.org/message-id/16971-5d004d34742a3d35%40postgresql.org +# rhbz#1940964 +Patch10: postgresql-datalayout-mismatch-on-s390.patch BuildRequires: make BuildRequires: gcc @@ -377,6 +378,7 @@ goal of accelerating analytics queries. %patch5 -p1 %patch8 -p1 %patch9 -p1 +%patch10 -p1 # We used to run autoconf here, but there's no longer any real need to, # since Postgres ships with a reasonably modern configure script. @@ -1125,6 +1127,11 @@ make -C postgresql-setup-%{setup_version} check %changelog +* Thu Apr 22 2021 Honza Horak - 13.2-6 +- Fix jit failure on s390x + Thanks to Tom Stellard + Related: #1940964 + * Tue Apr 20 2021 Honza Horak - 13.2-5 - Add macro for llvmjit settings