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

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 08:52:22 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Sean Perry (perry-ca)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/171847.diff


4 Files Affected:

- (modified) llvm/CMakeLists.txt (+1-6) 
- (modified) llvm/include/llvm/Support/thread.h (+12-2) 
- (modified) llvm/lib/Support/Parallel.cpp (-2) 
- (modified) llvm/lib/Support/Unix/Threading.inc (+12-2) 


``````````diff
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")
 
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/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 f016ed6937524..2865ef3b17efa 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
 
@@ -148,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

``````````

</details>


https://github.com/llvm/llvm-project/pull/171847


More information about the llvm-commits mailing list