[libc] [llvm] [libc] Change LIBC_THREAD_LOCAL to be dependent on LIBC_THREAD_MODE (PR #151527)
William Huynh via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 6 03:12:45 PDT 2025
https://github.com/saturn691 updated https://github.com/llvm/llvm-project/pull/151527
>From 2e6cc206aeea71f1736982b83e67248240f23838 Mon Sep 17 00:00:00 2001
From: William Huynh <William.Huynh at arm.com>
Date: Tue, 5 Aug 2025 16:03:31 +0100
Subject: [PATCH 1/2] Try and get CI to pass
---
libc/src/__support/macros/attributes.h | 11 ++++++++++-
libc/src/__support/threads/mutex.h | 9 ---------
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/libc/src/__support/macros/attributes.h b/libc/src/__support/macros/attributes.h
index c6474673de85a..0a549ff85e6cb 100644
--- a/libc/src/__support/macros/attributes.h
+++ b/libc/src/__support/macros/attributes.h
@@ -28,7 +28,16 @@
#define LIBC_INLINE_ASM __asm__ __volatile__
#define LIBC_UNUSED __attribute__((unused))
-#ifdef LIBC_TARGET_ARCH_IS_GPU
+// Uses the platform specific specialization
+#define LIBC_THREAD_MODE_PLATFORM 0
+
+// Mutex guards nothing, used in single-threaded implementations
+#define LIBC_THREAD_MODE_SINGLE 1
+
+// Vendor provides implementation
+#define LIBC_THREAD_MODE_EXTERNAL 2
+
+#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
#define LIBC_THREAD_LOCAL
#else
#define LIBC_THREAD_LOCAL thread_local
diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h
index cbef0d00009b2..9f3489cf99836 100644
--- a/libc/src/__support/threads/mutex.h
+++ b/libc/src/__support/threads/mutex.h
@@ -12,15 +12,6 @@
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
-// Uses the platform specific specialization
-#define LIBC_THREAD_MODE_PLATFORM 0
-
-// Mutex guards nothing, used in single-threaded implementations
-#define LIBC_THREAD_MODE_SINGLE 1
-
-// Vendor provides implementation
-#define LIBC_THREAD_MODE_EXTERNAL 2
-
#if !defined(LIBC_THREAD_MODE)
#error LIBC_THREAD_MODE is undefined
#endif // LIBC_THREAD_MODE
>From b293f6fe46726c7c5a49f477d5ebab34f48a79f9 Mon Sep 17 00:00:00 2001
From: William Huynh <William.Huynh at arm.com>
Date: Wed, 6 Aug 2025 11:12:34 +0100
Subject: [PATCH 2/2] Add cmake/Modules back- thread mode is set by libcxx
---
cmake/Modules/FindLibcCommonUtils.cmake | 3 +++
libc/src/__support/macros/attributes.h | 16 ++++++++++++++++
libc/src/__support/threads/mutex.h | 13 -------------
3 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/cmake/Modules/FindLibcCommonUtils.cmake b/cmake/Modules/FindLibcCommonUtils.cmake
index 95426c51a6041..81cf74fbd0d41 100644
--- a/cmake/Modules/FindLibcCommonUtils.cmake
+++ b/cmake/Modules/FindLibcCommonUtils.cmake
@@ -12,6 +12,9 @@ if(NOT TARGET llvm-libc-common-utilities)
add_library(llvm-libc-common-utilities INTERFACE)
# TODO: Reorganize the libc shared section so that it can be included without
# adding the root "libc" directory to the include path.
+ if (NOT(LIBCXX_ENABLE_THREADS))
+ target_compile_definitions(llvm-libc-common-utilities INTERFACE LIBC_THREAD_MODE=LIBC_THREAD_MODE_SINGLE)
+ endif()
target_include_directories(llvm-libc-common-utilities INTERFACE ${libc_path})
target_compile_definitions(llvm-libc-common-utilities INTERFACE LIBC_NAMESPACE=__llvm_libc_common_utils)
target_compile_features(llvm-libc-common-utilities INTERFACE cxx_std_17)
diff --git a/libc/src/__support/macros/attributes.h b/libc/src/__support/macros/attributes.h
index 0a549ff85e6cb..4ff374b0e4fbd 100644
--- a/libc/src/__support/macros/attributes.h
+++ b/libc/src/__support/macros/attributes.h
@@ -37,6 +37,22 @@
// Vendor provides implementation
#define LIBC_THREAD_MODE_EXTERNAL 2
+// libcxx doesn't define LIBC_THREAD_MODE, unless that is passed in the command
+// line in the CMake invocation. This defaults to the original implementation
+// (before changes in https://github.com/llvm/llvm-project/pull/145358)
+#ifndef LIBC_THREAD_MODE
+#define LIBC_THREAD_MODE LIBC_THREAD_MODE_PLATFORM
+#endif // LIBC_THREAD_MODE
+
+#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
+ LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
+ LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
+#error LIBC_THREAD_MODE must be one of the following values: \
+LIBC_THREAD_MODE_PLATFORM, \
+LIBC_THREAD_MODE_SINGLE, \
+LIBC_THREAD_MODE_EXTERNAL.
+#endif
+
#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
#define LIBC_THREAD_LOCAL
#else
diff --git a/libc/src/__support/threads/mutex.h b/libc/src/__support/threads/mutex.h
index 9f3489cf99836..f64f7e7b40082 100644
--- a/libc/src/__support/threads/mutex.h
+++ b/libc/src/__support/threads/mutex.h
@@ -12,19 +12,6 @@
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
-#if !defined(LIBC_THREAD_MODE)
-#error LIBC_THREAD_MODE is undefined
-#endif // LIBC_THREAD_MODE
-
-#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
- LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
- LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
-#error LIBC_THREAD_MODE must be one of the following values: \
-LIBC_THREAD_MODE_PLATFORM, \
-LIBC_THREAD_MODE_SINGLE, \
-LIBC_THREAD_MODE_EXTERNAL.
-#endif
-
#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_PLATFORM
// Platform independent code will include this header file which pulls
More information about the llvm-commits
mailing list