5595e64
From 0edaa982336823d4d7af8f10b91579fe0099ef3d Mon Sep 17 00:00:00 2001
5595e64
From: Tom Stellard <tstellar@redhat.com>
5595e64
Date: Tue, 20 Apr 2021 20:14:21 -0700
5595e64
Subject: [PATCH] jit: Workaround potential datalayout mismatch on s390x
5595e64
5595e64
LLVM's s390x target uses a different datalayout for z13 and newer processors.
5595e64
If llvmjit_types.bc is compiled to target a processor older than z13, and
5595e64
then the JIT runs on a z13 or newer processor, then there will be a mismatch
5595e64
in datalayouts between llvmjit_types.bc and the JIT engine.  This mismatch
5595e64
causes the JIT to fail at runtime.
5595e64
---
5595e64
 src/backend/jit/llvm/llvmjit.c | 46 ++++++++++++++++++++++++++++++++--
5595e64
 1 file changed, 44 insertions(+), 2 deletions(-)
5595e64
5595e64
diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
5595e64
index 98a27f08bf..05b6438ba8 100644
5595e64
--- a/src/backend/jit/llvm/llvmjit.c
5595e64
+++ b/src/backend/jit/llvm/llvmjit.c
5595e64
@@ -776,6 +776,35 @@ llvm_compile_module(LLVMJitContext *context)
5595e64
 			 errhidecontext(true)));
5595e64
 }
5595e64
 
5595e64
+/*
5595e64
+ * For the systemz target, LLVM uses a different datalayout for z13 and newer
5595e64
+ * CPUs than it does for older CPUs.  This can cause a mismatch in datalayouts
5595e64
+ * in the case where the llvm_types_module is compiled with a pre-z13 CPU
5595e64
+ * and the JIT is running on z13 or newer.
5595e64
+ * See computeDataLayout() function in
5595e64
+ * llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp for information on the
5595e64
+ * datalayout differences.
5595e64
+ */
5595e64
+static bool
5595e64
+needs_systemz_workaround(void)
5595e64
+{
5595e64
+	bool ret = false;
5595e64
+	LLVMContextRef llvm_context;
5595e64
+	LLVMTypeRef vec_type;
5595e64
+	LLVMTargetDataRef llvm_layoutref;
5595e64
+	if (strncmp(LLVMGetTargetName(llvm_targetref), "systemz", strlen("systemz")))
5595e64
+	{
5595e64
+		return false;
5595e64
+	}
5595e64
+
5595e64
+	llvm_context = LLVMGetModuleContext(llvm_types_module);
5595e64
+	vec_type = LLVMVectorType(LLVMIntTypeInContext(llvm_context, 32), 4);
5595e64
+	llvm_layoutref = LLVMCreateTargetData(llvm_layout);
5595e64
+	ret = (LLVMABIAlignmentOfType(llvm_layoutref, vec_type) == 16);
5595e64
+	LLVMDisposeTargetData(llvm_layoutref);
5595e64
+	return ret;
5595e64
+}
5595e64
+
5595e64
 /*
5595e64
  * Per session initialization.
5595e64
  */
5595e64
@@ -785,6 +814,7 @@ llvm_session_initialize(void)
5595e64
 	MemoryContext oldcontext;
5595e64
 	char	   *error = NULL;
5595e64
 	char	   *cpu = NULL;
5595e64
+	char       *host_features = NULL;
5595e64
 	char	   *features = NULL;
5595e64
 	LLVMTargetMachineRef opt0_tm;
5595e64
 	LLVMTargetMachineRef opt3_tm;
5595e64
@@ -816,10 +846,17 @@ llvm_session_initialize(void)
5595e64
 	 * features not all CPUs have (weird, huh).
5595e64
 	 */
5595e64
 	cpu = LLVMGetHostCPUName();
5595e64
-	features = LLVMGetHostCPUFeatures();
5595e64
+	features = host_features = LLVMGetHostCPUFeatures();
5595e64
 	elog(DEBUG2, "LLVMJIT detected CPU \"%s\", with features \"%s\"",
5595e64
 		 cpu, features);
5595e64
 
5595e64
+	if (needs_systemz_workaround())
5595e64
+	{
5595e64
+		const char *no_vector =",-vector";
5595e64
+		features = malloc(sizeof(char) * (strlen(host_features) + strlen(no_vector) + 1));
5595e64
+		sprintf(features, "%s%s", host_features, no_vector);
5595e64
+	}
5595e64
+
5595e64
 	opt0_tm =
5595e64
 		LLVMCreateTargetMachine(llvm_targetref, llvm_triple, cpu, features,
5595e64
 								LLVMCodeGenLevelNone,
5595e64
@@ -833,8 +870,13 @@ llvm_session_initialize(void)
5595e64
 
5595e64
 	LLVMDisposeMessage(cpu);
5595e64
 	cpu = NULL;
5595e64
-	LLVMDisposeMessage(features);
5595e64
+	if (features != host_features)
5595e64
+	{
5595e64
+		free(features);
5595e64
+	}
5595e64
 	features = NULL;
5595e64
+	LLVMDisposeMessage(host_features);
5595e64
+	host_features = NULL;
5595e64
 
5595e64
 	/* force symbols in main binary to be loaded */
5595e64
 	LLVMLoadLibraryPermanently(NULL);
5595e64
-- 
5595e64
2.27.0
5595e64