diff --git a/libc/src/__support/StringUtil/CMakeLists.txt b/libc/src/__support/StringUtil/CMakeLists.txt --- a/libc/src/__support/StringUtil/CMakeLists.txt +++ b/libc/src/__support/StringUtil/CMakeLists.txt @@ -44,6 +44,7 @@ DEPENDS .message_mapper .platform_errors + libc.src.__support.common libc.src.__support.CPP.span libc.src.__support.CPP.string_view libc.src.__support.CPP.stringstream @@ -60,6 +61,7 @@ .message_mapper .platform_signals libc.include.signal + libc.src.__support.common libc.src.__support.CPP.span libc.src.__support.CPP.string_view libc.src.__support.CPP.stringstream diff --git a/libc/src/__support/StringUtil/error_to_string.cpp b/libc/src/__support/StringUtil/error_to_string.cpp --- a/libc/src/__support/StringUtil/error_to_string.cpp +++ b/libc/src/__support/StringUtil/error_to_string.cpp @@ -14,6 +14,7 @@ #include "src/__support/CPP/stringstream.h" #include "src/__support/StringUtil/message_mapper.h" #include "src/__support/integer_to_string.h" +#include "src/__support/macros/attributes.h" #include @@ -31,7 +32,7 @@ // This is to hold error strings that have to be custom built. It may be // rewritten on every call to strerror (or other error to string function). constexpr size_t ERR_BUFFER_SIZE = max_buff_size(); -thread_local char error_buffer[ERR_BUFFER_SIZE]; +LIBC_THREAD_LOCAL char error_buffer[ERR_BUFFER_SIZE]; constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_ERRORS); diff --git a/libc/src/__support/StringUtil/signal_to_string.cpp b/libc/src/__support/StringUtil/signal_to_string.cpp --- a/libc/src/__support/StringUtil/signal_to_string.cpp +++ b/libc/src/__support/StringUtil/signal_to_string.cpp @@ -14,6 +14,7 @@ #include "src/__support/CPP/stringstream.h" #include "src/__support/StringUtil/message_mapper.h" #include "src/__support/integer_to_string.h" +#include "src/__support/macros/attributes.h" #include #include @@ -32,7 +33,7 @@ // This is to hold signal strings that have to be custom built. It may be // rewritten on every call to strsignal (or other signal to string function). constexpr size_t SIG_BUFFER_SIZE = max_buff_size(); -thread_local char signal_buffer[SIG_BUFFER_SIZE]; +LIBC_THREAD_LOCAL char signal_buffer[SIG_BUFFER_SIZE]; constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_SIGNALS); diff --git a/libc/src/__support/macros/attributes.h b/libc/src/__support/macros/attributes.h --- a/libc/src/__support/macros/attributes.h +++ b/libc/src/__support/macros/attributes.h @@ -17,8 +17,16 @@ #ifndef LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H #define LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H +#include "properties/architectures.h" + #define LIBC_INLINE inline #define LIBC_INLINE_ASM __asm__ __volatile__ #define LIBC_UNUSED __attribute__((unused)) +#ifdef LIBC_TARGET_ARCH_IS_GPU +#define LIBC_THREAD_LOCAL +#else +#define LIBC_THREAD_LOCAL thread_local +#endif + #endif // LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H diff --git a/libc/src/__support/threads/CMakeLists.txt b/libc/src/__support/threads/CMakeLists.txt --- a/libc/src/__support/threads/CMakeLists.txt +++ b/libc/src/__support/threads/CMakeLists.txt @@ -48,6 +48,7 @@ DEPENDS .mutex .${LIBC_TARGET_OS}.thread + libc.src.__support.common libc.src.__support.fixedvector libc.src.__support.CPP.array libc.src.__support.CPP.optional diff --git a/libc/src/__support/threads/thread.h b/libc/src/__support/threads/thread.h --- a/libc/src/__support/threads/thread.h +++ b/libc/src/__support/threads/thread.h @@ -13,6 +13,7 @@ #include "src/__support/CPP/optional.h" #include "src/__support/CPP/string_view.h" #include "src/__support/CPP/stringstream.h" +#include "src/__support/macros/attributes.h" #include "src/__support/macros/properties/architectures.h" #include // for exec_pagesize. @@ -225,7 +226,7 @@ int get_name(cpp::StringStream &name) const; }; -extern thread_local Thread self; +extern LIBC_THREAD_LOCAL Thread self; // Platforms should implement this function. [[noreturn]] void thread_exit(ThreadReturnValue retval, ThreadStyle style); diff --git a/libc/src/__support/threads/thread.cpp b/libc/src/__support/threads/thread.cpp --- a/libc/src/__support/threads/thread.cpp +++ b/libc/src/__support/threads/thread.cpp @@ -12,10 +12,11 @@ #include "src/__support/CPP/array.h" #include "src/__support/CPP/optional.h" #include "src/__support/fixedvector.h" +#include "src/__support/macros/attributes.h" namespace __llvm_libc { -thread_local Thread self; +LIBC_THREAD_LOCAL Thread self; namespace { @@ -99,7 +100,7 @@ : active(true), payload(p), dtor(d) {} }; -static thread_local cpp::array tss_values; +static LIBC_THREAD_LOCAL cpp::array tss_values; } // anonymous namespace @@ -128,7 +129,7 @@ } }; -static thread_local ThreadAtExitCallbackMgr atexit_callback_mgr; +static LIBC_THREAD_LOCAL ThreadAtExitCallbackMgr atexit_callback_mgr; // The function __cxa_thread_atexit is provided by C++ runtimes like libcxxabi. // It is used by thread local object runtime to register destructor calls. To diff --git a/libc/src/errno/CMakeLists.txt b/libc/src/errno/CMakeLists.txt --- a/libc/src/errno/CMakeLists.txt +++ b/libc/src/errno/CMakeLists.txt @@ -6,4 +6,5 @@ libc_errno.h # Include this DEPENDS libc.include.errno + libc.src.__support.common ) diff --git a/libc/src/errno/libc_errno.h b/libc/src/errno/libc_errno.h --- a/libc/src/errno/libc_errno.h +++ b/libc/src/errno/libc_errno.h @@ -9,6 +9,7 @@ #ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H #define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H +#include "src/__support/macros/attributes.h" #include "src/__support/macros/properties/architectures.h" #include @@ -42,7 +43,7 @@ extern "C" ErrnoConsumer __llvmlibc_internal_errno; #else // LIBC_TARGET_ARCH_IS_GPU extern "C" { -extern thread_local int __llvmlibc_internal_errno; +extern LIBC_THREAD_LOCAL int __llvmlibc_internal_errno; } // extern "C" #endif diff --git a/libc/src/errno/libc_errno.cpp b/libc/src/errno/libc_errno.cpp --- a/libc/src/errno/libc_errno.cpp +++ b/libc/src/errno/libc_errno.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "src/__support/macros/attributes.h" #include "src/__support/macros/properties/architectures.h" namespace __llvm_libc { @@ -29,13 +30,13 @@ #ifdef LIBC_TARGET_ARCH_IS_GPU ErrnoConsumer __llvmlibc_errno; #else -thread_local int __llvmlibc_errno; +LIBC_THREAD_LOCAL int __llvmlibc_errno; #endif // LIBC_TARGET_ARCH_IS_GPU #else #ifdef LIBC_TARGET_ARCH_IS_GPU ErrnoConsumer __llvmlibc_internal_errno; #else -thread_local int __llvmlibc_internal_errno; +LIBC_THREAD_LOCAL int __llvmlibc_internal_errno; #endif // LIBC_TARGET_ARCH_IS_GPU #endif } // extern "C" diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt --- a/libc/src/stdlib/CMakeLists.txt +++ b/libc/src/stdlib/CMakeLists.txt @@ -218,6 +218,8 @@ rand_util.cpp HDRS rand_util.h + DEPENDS + libc.src.__support.common ) add_entrypoint_object( diff --git a/libc/src/stdlib/rand_util.h b/libc/src/stdlib/rand_util.h --- a/libc/src/stdlib/rand_util.h +++ b/libc/src/stdlib/rand_util.h @@ -9,9 +9,11 @@ #ifndef LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H #define LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H +#include "src/__support/macros/attributes.h" + namespace __llvm_libc { -extern thread_local unsigned long rand_next; +extern LIBC_THREAD_LOCAL unsigned long rand_next; } // namespace __llvm_libc diff --git a/libc/src/stdlib/rand_util.cpp b/libc/src/stdlib/rand_util.cpp --- a/libc/src/stdlib/rand_util.cpp +++ b/libc/src/stdlib/rand_util.cpp @@ -7,9 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/stdlib/rand_util.h" +#include "src/__support/macros/attributes.h" namespace __llvm_libc { -thread_local unsigned long rand_next; +LIBC_THREAD_LOCAL unsigned long rand_next; } // namespace __llvm_libc diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -93,7 +93,10 @@ libc_support_library( name = "__support_macros_attributes", hdrs = ["src/__support/macros/attributes.h"], - deps = [":libc_root"], + deps = [ + ":__support_macros_properties_architectures", + ":libc_root" + ], ) libc_support_library( @@ -827,6 +830,7 @@ hdrs = ["src/errno/libc_errno.h"], deps = [ ":__support_common", + ":__support_macros_attributes", ":__support_macros_properties_architectures", ], )