[compiler-rt] [llvm] [SystemZ] Enable building compiler-rt for z/OS (PR #221226)

Sean Perry via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 08:52:12 PDT 2026


https://github.com/perry-ca updated https://github.com/llvm/llvm-project/pull/221226

>From 345fbc1e579e384390bfe1eff2b7120e0200bd5c Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Fri, 28 Aug 2026 13:08:40 -0400
Subject: [PATCH 1/4] Initial changes to build compiler-rt for z/OS

---
 compiler-rt/cmake/base-config-ix.cmake        |  3 ++
 compiler-rt/lib/builtins/CMakeLists.txt       |  6 ++++
 compiler-rt/lib/builtins/floattitf.c          |  4 +++
 compiler-rt/lib/builtins/floatuntitf.c        |  4 +++
 compiler-rt/lib/builtins/gcc_personality_v0.c |  8 +++++
 compiler-rt/lib/builtins/int_lib.h            |  2 +-
 compiler-rt/lib/builtins/int_math.h           | 12 +++++--
 .../lib/builtins/s390/stack_chk_fail.c        | 34 +++++++++++++++++++
 compiler-rt/test/builtins/Unit/lit.cfg.py     |  8 ++++-
 9 files changed, 77 insertions(+), 4 deletions(-)
 create mode 100644 compiler-rt/lib/builtins/s390/stack_chk_fail.c

diff --git a/compiler-rt/cmake/base-config-ix.cmake b/compiler-rt/cmake/base-config-ix.cmake
index 15e8be327380c..eb5cda284a314 100644
--- a/compiler-rt/cmake/base-config-ix.cmake
+++ b/compiler-rt/cmake/base-config-ix.cmake
@@ -262,6 +262,9 @@ macro(test_targets)
       test_target_arch(powerpc "" "-m32")
       test_target_arch(powerpc64 "" "-m64")
     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
+      if(ZOS)
+        add_definitions("-D_XOPEN_SOURCE=600")
+      endif()
       test_target_arch(s390x "" "")
     elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "sparc")
       test_target_arch(sparc "" "-m32")
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 12a3b068b3373..9838501d25c1a 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -1023,6 +1023,7 @@ set(sparcv9_SOURCES ${GENERIC_SOURCES} ${GENERIC_TF_SOURCES})
 set(spirv64_SOURCES ${GENERIC_SOURCES})
 
 set(s390x_SOURCES
+  s390/stack_chk_fail.c
   ${GENERIC_SOURCES}
   ${GENERIC_TF_SOURCES}
 )
@@ -1176,6 +1177,11 @@ else ()
         list(APPEND BUILTIN_CFLAGS_${arch} -fforce-enable-int128)
       endif()
 
+      if (ZOS)
+        list(APPEND BUILTIN_CFLAGS_${arch} -march=z13)
+
+      endif()
+
       add_compiler_rt_runtime(clang_rt.builtins
                               STATIC
                               ARCHS ${arch}
diff --git a/compiler-rt/lib/builtins/floattitf.c b/compiler-rt/lib/builtins/floattitf.c
index 5dffe22fdb4eb..072d1c0697ac8 100644
--- a/compiler-rt/lib/builtins/floattitf.c
+++ b/compiler-rt/lib/builtins/floattitf.c
@@ -16,6 +16,8 @@
 #include "fp_lib.h"
 #include "int_lib.h"
 
+#ifdef CRT_HAS_128BIT
+
 #if defined(CRT_HAS_TF_MODE)
 #define SRC_I128
 #define DST_QUAD
@@ -33,3 +35,5 @@
 COMPILER_RT_ABI fp_t __floattitf(ti_int a) { return __floatXiYf__(a); }
 
 #endif
+
+#endif // CRT_HAS_128BIT
diff --git a/compiler-rt/lib/builtins/floatuntitf.c b/compiler-rt/lib/builtins/floatuntitf.c
index 1c5998a40b9f7..e7dc74e25c0a7 100644
--- a/compiler-rt/lib/builtins/floatuntitf.c
+++ b/compiler-rt/lib/builtins/floatuntitf.c
@@ -16,6 +16,8 @@
 #include "fp_lib.h"
 #include "int_lib.h"
 
+#ifdef CRT_HAS_128BIT
+
 #if defined(CRT_HAS_TF_MODE)
 #define SRC_U128
 #define DST_QUAD
@@ -33,3 +35,5 @@
 COMPILER_RT_ABI fp_t __floatuntitf(tu_int a) { return __floatXiYf__(a); }
 
 #endif
+
+#endif // CRT_HAS_128BIT
diff --git a/compiler-rt/lib/builtins/gcc_personality_v0.c b/compiler-rt/lib/builtins/gcc_personality_v0.c
index 02d4e65d78d1c..d8bc6371f7a6b 100644
--- a/compiler-rt/lib/builtins/gcc_personality_v0.c
+++ b/compiler-rt/lib/builtins/gcc_personality_v0.c
@@ -230,6 +230,11 @@ COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_sj0(
 COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
     _Unwind_State state, struct _Unwind_Exception *exceptionObject,
     struct _Unwind_Context *context)
+#elif defined(__MVS__)
+// The z/OS LE ABI personality routine has a different signature.
+COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
+    int version, _Unwind_Action actions, const uint8_t *lsda,
+    struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
 #elif defined(__SEH__)
 static _Unwind_Reason_Code __gcc_personality_imp(
     int version, _Unwind_Action actions, uint64_t exceptionClass,
@@ -251,8 +256,11 @@ COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
 #endif
     return continueUnwind(exceptionObject, context);
 
+  // z/OS provides the LSDA directly to the personality routine.
+#ifndef __MVS__
   // There is nothing to do if there is no LSDA for this frame.
   const uint8_t *lsda = (uint8_t *)_Unwind_GetLanguageSpecificData(context);
+#endif
   if (lsda == (uint8_t *)0)
     return continueUnwind(exceptionObject, context);
 
diff --git a/compiler-rt/lib/builtins/int_lib.h b/compiler-rt/lib/builtins/int_lib.h
index 629c3065da6a0..c7dbd580fcf33 100644
--- a/compiler-rt/lib/builtins/int_lib.h
+++ b/compiler-rt/lib/builtins/int_lib.h
@@ -49,7 +49,7 @@
 #define SYMBOL_NAME(name) XSTR(__USER_LABEL_PREFIX__) #name
 
 #if defined(__ELF__) || defined(__MINGW32__) || defined(__wasm__) ||           \
-    defined(_AIX) || defined(__CYGWIN__)
+    defined(_AIX) || defined(__MVS__) || defined(__CYGWIN__)
 #define COMPILER_RT_ALIAS(name, aliasname) \
   COMPILER_RT_ABI __typeof(name) aliasname __attribute__((__alias__(#name)));
 #elif defined(__APPLE__)
diff --git a/compiler-rt/lib/builtins/int_math.h b/compiler-rt/lib/builtins/int_math.h
index c9c6bc83251c3..2ad069b8c14ed 100644
--- a/compiler-rt/lib/builtins/int_math.h
+++ b/compiler-rt/lib/builtins/int_math.h
@@ -23,6 +23,10 @@
 #define __has_builtin(x) 0
 #endif
 
+#if defined(__MVS__)
+#include <math.h>
+#endif
+
 #if defined(_MSC_VER) && !defined(__clang__)
 #include <math.h>
 #include <stdlib.h>
@@ -71,7 +75,9 @@
 #define crt_copysignl(x, y) __builtin_copysignl((x), (y))
 // We define __has_builtin to always return 0 for GCC versions below 10,
 // but __builtin_copysignf128 is available since version 7.
-#if __has_builtin(__builtin_copysignf128) ||                                   \
+#if defined(__MVS__)
+#define crt_copysignf128(x, y) crt_copysignl((x), (y))
+#elif __has_builtin(__builtin_copysignf128) ||                                   \
     (defined(__GNUC__) && __GNUC__ >= 7)
 #define crt_copysignf128(x, y) __builtin_copysignf128((x), (y))
 #elif __has_builtin(__builtin_copysignq)
@@ -89,7 +95,9 @@
 #define crt_fabsl(x) __builtin_fabsl((x))
 // We define __has_builtin to always return 0 for GCC versions below 10,
 // but __builtin_fabsf128 is available since version 7.
-#if __has_builtin(__builtin_fabsf128) || (defined(__GNUC__) && __GNUC__ >= 7)
+#if defined(__MVS__)
+#define crt_fabsf128(x) __builtin_fabsl((x))
+#elif __has_builtin(__builtin_fabsf128) || (defined(__GNUC__) && __GNUC__ >= 7)
 #define crt_fabsf128(x) __builtin_fabsf128((x))
 #elif __has_builtin(__builtin_fabsq)
 #define crt_fabsf128(x) __builtin_fabsq((x))
diff --git a/compiler-rt/lib/builtins/s390/stack_chk_fail.c b/compiler-rt/lib/builtins/s390/stack_chk_fail.c
new file mode 100644
index 0000000000000..bc315fa1ac7cc
--- /dev/null
+++ b/compiler-rt/lib/builtins/s390/stack_chk_fail.c
@@ -0,0 +1,34 @@
+//===-- stack_chk_fail.c - Implement __stack_chk_fail ---------------------===//
+//
+// Part of the LLVM Project,under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements __stack_chk_fail for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#ifdef __MVS__
+__attribute__((no_stack_protector)) void __stack_chk_fail(void) {
+  __asm__ volatile(
+      /* Load LAA */
+      "  llgt   6,1208\n"
+      /* Load LCA */
+      "  lg     6,88(6)\n"
+      /* Load CAA */
+      "  lg     6,8(6)\n"
+      /* Load vector */
+      "  lg     6,640(6)\n"
+      /* Load __CEL4SFCR */
+      "  lg     6,32(6)\n"
+      /* Branch to __CEL4SFCR */
+      "  basr   7,6\n"
+      /* NOPR 0 */
+      "  bcr    0,0\n"
+      :
+      :
+      : "r6", "r7");
+}
+#endif
diff --git a/compiler-rt/test/builtins/Unit/lit.cfg.py b/compiler-rt/test/builtins/Unit/lit.cfg.py
index 05f97844f5470..fcc9af37b6ef5 100644
--- a/compiler-rt/test/builtins/Unit/lit.cfg.py
+++ b/compiler-rt/test/builtins/Unit/lit.cfg.py
@@ -62,6 +62,9 @@ def get_libgcc_file_name():
 elif config.target_os == "Darwin":
     base_lib = os.path.join(config.compiler_rt_libdir, "libclang_rt.osx.a ")
     config.substitutions.append(("%librt ", base_lib + " -lSystem "))
+elif config.target_os == "OS390":
+    base_lib = os.path.join( config.compiler_rt_libdir, "libclang_rt.builtins%s.a" % config.target_suffix)
+    config.substitutions.append(("%librt ", base_lib + " "))
 elif config.target_os == "Windows":
     base_lib = os.path.join(
         config.compiler_rt_libdir, "libclang_rt.builtins%s.a" % config.target_suffix
@@ -144,7 +147,10 @@ def get_libgcc_file_name():
 )
 builtins_lit_source_dir = get_required_attr(config, "builtins_lit_source_dir")
 
-extra_link_flags = ["-nodefaultlibs"]
+if sys.platform == 'zos':
+    extra_link_flags = ["-nodefaultrtlibs"]
+else:
+    extra_link_flags = ["-nodefaultlibs"]
 
 target_cflags = [get_required_attr(config, "target_cflags")]
 target_cflags += ["-fno-builtin", "-I", builtins_source_dir]

>From bbedb16efc44426f9b13669bdd98319e5a6e5f5f Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Sat, 29 Aug 2026 00:17:35 -0400
Subject: [PATCH 2/4] more changes

---
 llvm/runtimes/CMakeLists.txt | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index 7bf7bf69fe55a..da26f29375eea 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -56,6 +56,13 @@ if(NOT LLVM_BUILD_RUNTIMES)
   set(EXTRA_ARGS EXCLUDE_FROM_ALL)
 endif()
 
+# Exclude llvm-* tools from TOOLCHAIN_TOOLS for ZOS
+if(ZOS)
+  # TODO: Re-use llvm-* tools when functional for ZOS
+  set(TOOLCHAIN_TOOLS clang)
+  list(APPEND COMMON_CMAKE_ARGS "-DCMAKE_AR=/bin/ar" "-DCMAKE_LINKER=/bin/ld" "-DCMAKE_C_FLAGS=-fstack-protector" "-DCMAKE_CXX_FLAGS=-fstack-protector")
+endif()
+
 function(check_apple_target triple builtin_or_runtime)
   set(error "\
 compiler-rt for Darwin builds for all platforms and architectures using a \

>From b4fc93067b484869ec65437cb12b17e694c9ec09 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 8 Sep 2026 11:49:35 -0400
Subject: [PATCH 3/4] unnecessary changes

---
 compiler-rt/lib/builtins/floattitf.c   | 4 ----
 compiler-rt/lib/builtins/floatuntitf.c | 4 ----
 2 files changed, 8 deletions(-)

diff --git a/compiler-rt/lib/builtins/floattitf.c b/compiler-rt/lib/builtins/floattitf.c
index 072d1c0697ac8..5dffe22fdb4eb 100644
--- a/compiler-rt/lib/builtins/floattitf.c
+++ b/compiler-rt/lib/builtins/floattitf.c
@@ -16,8 +16,6 @@
 #include "fp_lib.h"
 #include "int_lib.h"
 
-#ifdef CRT_HAS_128BIT
-
 #if defined(CRT_HAS_TF_MODE)
 #define SRC_I128
 #define DST_QUAD
@@ -35,5 +33,3 @@
 COMPILER_RT_ABI fp_t __floattitf(ti_int a) { return __floatXiYf__(a); }
 
 #endif
-
-#endif // CRT_HAS_128BIT
diff --git a/compiler-rt/lib/builtins/floatuntitf.c b/compiler-rt/lib/builtins/floatuntitf.c
index e7dc74e25c0a7..1c5998a40b9f7 100644
--- a/compiler-rt/lib/builtins/floatuntitf.c
+++ b/compiler-rt/lib/builtins/floatuntitf.c
@@ -16,8 +16,6 @@
 #include "fp_lib.h"
 #include "int_lib.h"
 
-#ifdef CRT_HAS_128BIT
-
 #if defined(CRT_HAS_TF_MODE)
 #define SRC_U128
 #define DST_QUAD
@@ -35,5 +33,3 @@
 COMPILER_RT_ABI fp_t __floatuntitf(tu_int a) { return __floatXiYf__(a); }
 
 #endif
-
-#endif // CRT_HAS_128BIT

>From 2f182e8e467b8b44e60dd8347f05f40a76e27a86 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 8 Sep 2026 11:51:41 -0400
Subject: [PATCH 4/4] unnecessary changes

---
 compiler-rt/lib/builtins/int_math.h | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/compiler-rt/lib/builtins/int_math.h b/compiler-rt/lib/builtins/int_math.h
index 2ad069b8c14ed..666e17aa5967b 100644
--- a/compiler-rt/lib/builtins/int_math.h
+++ b/compiler-rt/lib/builtins/int_math.h
@@ -23,10 +23,6 @@
 #define __has_builtin(x) 0
 #endif
 
-#if defined(__MVS__)
-#include <math.h>
-#endif
-
 #if defined(_MSC_VER) && !defined(__clang__)
 #include <math.h>
 #include <stdlib.h>



More information about the llvm-commits mailing list