[llvm] Enable using threads on z/OS (PR #171847)

Sean Perry via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 07:25:16 PST 2025


https://github.com/perry-ca created https://github.com/llvm/llvm-project/pull/171847

z/OS 3.1 enables TLS support (limited to compile time constant initializers).  To enable building with thread support, we need to update the code to handle the difference in definition of pthread_t.  It is a struct on z/OS, not an integer.  The existing code assumes that pthread_t is an integer.  This usually happens when checking to see if pthread_t is null or not. 

In Parallel.cpp, there was a variable `Backoff` defined as TLS.  The initializer for this requires C++ initialization which isn't supported on z/OS.  The variable isn't actually used (see declaration of local var with same name inside the loop) so deleting it solved the build failure this was causing.

>From fdd65dddc3a378665933437e09298112e6c1d393 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Wed, 3 Dec 2025 10:57:25 -0500
Subject: [PATCH 1/3] use correct type for thread id

---
 llvm/include/llvm/Support/thread.h  | 14 ++++++++++++--
 llvm/lib/Support/Unix/Threading.inc | 12 ++++++++++--
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Support/thread.h b/llvm/include/llvm/Support/thread.h
index 51873e7d529bf..aad82e485ab57 100644
--- a/llvm/include/llvm/Support/thread.h
+++ b/llvm/include/llvm/Support/thread.h
@@ -51,7 +51,11 @@ class thread {
 public:
 #ifdef LLVM_ON_UNIX
   using native_handle_type = pthread_t;
+#ifdef __MVS__
+  using id = unsigned long long;
+#else
   using id = pthread_t;
+#endif
   using start_routine_type = void *(*)(void *);
 
   template <typename CalleeTuple> static void *ThreadProxy(void *Ptr) {
@@ -97,7 +101,13 @@ class thread {
     return *this;
   }
 
-  bool joinable() const noexcept { return Thread != native_handle_type(); }
+  bool is_null() const noexcept {
+    return get_id() == 0;
+  }
+
+  bool joinable() const noexcept {
+    return !is_null();
+  }
 
   inline id get_id() const noexcept;
 
@@ -133,7 +143,7 @@ thread::thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
 
   Thread = llvm_execute_on_thread_impl(ThreadProxy<CalleeTuple>, Callee.get(),
                                        StackSizeInBytes);
-  if (Thread != native_handle_type())
+  if (!is_null())
     Callee.release();
 }
 
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index f016ed6937524..cceffcb34c547 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -119,9 +119,17 @@ void llvm_thread_join_impl(pthread_t Thread) {
   }
 }
 
-pthread_t llvm_thread_get_id_impl(pthread_t Thread) { return Thread; }
+llvm::thread::id llvm_thread_get_id_impl(pthread_t Thread) {
+#ifdef __MVS__
+  return Thread.__;
+#else
+  return Thread;
+#endif
+}
 
-pthread_t llvm_thread_get_current_id_impl() { return ::pthread_self(); }
+llvm::thread::id llvm_thread_get_current_id_impl() {
+  return llvm_thread_get_id_impl(::pthread_self());
+}
 
 } // namespace llvm
 

>From 82de84df3c2640fab99742cbb37edaa43078b22f Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Wed, 3 Dec 2025 21:06:35 +0000
Subject: [PATCH 2/3] Remove unused variable blocking z/OS build & one more
 place that retrieves thread id

---
 llvm/lib/Support/Parallel.cpp       | 2 --
 llvm/lib/Support/Unix/Threading.inc | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp
index ab220b8f2ceba..97c16b0eb333a 100644
--- a/llvm/lib/Support/Parallel.cpp
+++ b/llvm/lib/Support/Parallel.cpp
@@ -129,8 +129,6 @@ class ThreadPoolExecutor : public Executor {
     // first successful tryAcquire() in a process. This guarantees forward
     // progress without requiring a dedicated "always-on" thread here.
 
-    static thread_local std::unique_ptr<ExponentialBackoff> Backoff;
-
     while (true) {
       if (TheJobserver) {
         // Jobserver-mode scheduling:
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index cceffcb34c547..2865ef3b17efa 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -156,6 +156,8 @@ uint64_t llvm::get_threadid() {
   return uint64_t(syscall(__NR_gettid));
 #elif defined(_AIX)
   return uint64_t(thread_self());
+#elif defined(__MVS__)
+  return llvm_thread_get_id_impl(pthread_self());
 #else
   return uint64_t(pthread_self());
 #endif

>From 6c69a623bfc84b16f6f6cd8f417458ea48096a38 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Thu, 11 Dec 2025 10:12:40 -0500
Subject: [PATCH 3/3] enable using threads on z/OS

---
 llvm/CMakeLists.txt | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 908580f791f36..58c3c598c49b4 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -609,12 +609,7 @@ option(LLVM_ENABLE_LIBEDIT "Use libedit if available." ON)
 
 option(LLVM_ENABLE_LIBPFM "Use libpfm for performance counters if available." ON)
 
-# On z/OS, threads cannot be used because TLS is not supported.
-if (CMAKE_SYSTEM_NAME MATCHES "OS390")
-  option(LLVM_ENABLE_THREADS "Use threads if available." OFF)
-else()
-  option(LLVM_ENABLE_THREADS "Use threads if available." ON)
-endif()
+option(LLVM_ENABLE_THREADS "Use threads if available." ON)
 
 set(LLVM_ENABLE_ICU "OFF" CACHE STRING "Use ICU for text encoding conversion support if available. Can be ON, OFF, or FORCE_ON")
 



More information about the llvm-commits mailing list