Skip to content

Commit daeee56

Browse files
author
Siva Chandra Reddy
committed
[libc] Add macro LIBC_THREAD_LOCAL.
It resolves to thread_local on all platform except for the GPUs on which it resolves to nothing. The use of thread_local in the source code has been replaced with the new macro. Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D151486
1 parent df1b2be commit daeee56

File tree

14 files changed

+39
-12
lines changed

14 files changed

+39
-12
lines changed

Diff for: libc/src/__support/StringUtil/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_object_library(
4444
DEPENDS
4545
.message_mapper
4646
.platform_errors
47+
libc.src.__support.common
4748
libc.src.__support.CPP.span
4849
libc.src.__support.CPP.string_view
4950
libc.src.__support.CPP.stringstream
@@ -60,6 +61,7 @@ add_object_library(
6061
.message_mapper
6162
.platform_signals
6263
libc.include.signal
64+
libc.src.__support.common
6365
libc.src.__support.CPP.span
6466
libc.src.__support.CPP.string_view
6567
libc.src.__support.CPP.stringstream

Diff for: libc/src/__support/StringUtil/error_to_string.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "src/__support/CPP/stringstream.h"
1515
#include "src/__support/StringUtil/message_mapper.h"
1616
#include "src/__support/integer_to_string.h"
17+
#include "src/__support/macros/attributes.h"
1718

1819
#include <stddef.h>
1920

@@ -31,7 +32,7 @@ constexpr size_t max_buff_size() {
3132
// This is to hold error strings that have to be custom built. It may be
3233
// rewritten on every call to strerror (or other error to string function).
3334
constexpr size_t ERR_BUFFER_SIZE = max_buff_size();
34-
thread_local char error_buffer[ERR_BUFFER_SIZE];
35+
LIBC_THREAD_LOCAL char error_buffer[ERR_BUFFER_SIZE];
3536

3637
constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_ERRORS);
3738

Diff for: libc/src/__support/StringUtil/signal_to_string.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "src/__support/CPP/stringstream.h"
1515
#include "src/__support/StringUtil/message_mapper.h"
1616
#include "src/__support/integer_to_string.h"
17+
#include "src/__support/macros/attributes.h"
1718

1819
#include <signal.h>
1920
#include <stddef.h>
@@ -32,7 +33,7 @@ constexpr size_t max_buff_size() {
3233
// This is to hold signal strings that have to be custom built. It may be
3334
// rewritten on every call to strsignal (or other signal to string function).
3435
constexpr size_t SIG_BUFFER_SIZE = max_buff_size();
35-
thread_local char signal_buffer[SIG_BUFFER_SIZE];
36+
LIBC_THREAD_LOCAL char signal_buffer[SIG_BUFFER_SIZE];
3637

3738
constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_SIGNALS);
3839

Diff for: libc/src/__support/macros/attributes.h

+8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@
1717
#ifndef LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H
1818
#define LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H
1919

20+
#include "properties/architectures.h"
21+
2022
#define LIBC_INLINE inline
2123
#define LIBC_INLINE_ASM __asm__ __volatile__
2224
#define LIBC_UNUSED __attribute__((unused))
2325

26+
#ifdef LIBC_TARGET_ARCH_IS_GPU
27+
#define LIBC_THREAD_LOCAL
28+
#else
29+
#define LIBC_THREAD_LOCAL thread_local
30+
#endif
31+
2432
#endif // LLVM_LIBC_SUPPORT_MACROS_ATTRIBUTES_H

Diff for: libc/src/__support/threads/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.thread)
4848
DEPENDS
4949
.mutex
5050
.${LIBC_TARGET_OS}.thread
51+
libc.src.__support.common
5152
libc.src.__support.fixedvector
5253
libc.src.__support.CPP.array
5354
libc.src.__support.CPP.optional

Diff for: libc/src/__support/threads/thread.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
#include "src/__support/CPP/array.h"
1313
#include "src/__support/CPP/optional.h"
1414
#include "src/__support/fixedvector.h"
15+
#include "src/__support/macros/attributes.h"
1516

1617
namespace __llvm_libc {
1718

18-
thread_local Thread self;
19+
LIBC_THREAD_LOCAL Thread self;
1920

2021
namespace {
2122

@@ -99,7 +100,7 @@ struct TSSValueUnit {
99100
: active(true), payload(p), dtor(d) {}
100101
};
101102

102-
static thread_local cpp::array<TSSValueUnit, TSS_KEY_COUNT> tss_values;
103+
static LIBC_THREAD_LOCAL cpp::array<TSSValueUnit, TSS_KEY_COUNT> tss_values;
103104

104105
} // anonymous namespace
105106

@@ -128,7 +129,7 @@ class ThreadAtExitCallbackMgr {
128129
}
129130
};
130131

131-
static thread_local ThreadAtExitCallbackMgr atexit_callback_mgr;
132+
static LIBC_THREAD_LOCAL ThreadAtExitCallbackMgr atexit_callback_mgr;
132133

133134
// The function __cxa_thread_atexit is provided by C++ runtimes like libcxxabi.
134135
// It is used by thread local object runtime to register destructor calls. To

Diff for: libc/src/__support/threads/thread.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "src/__support/CPP/optional.h"
1414
#include "src/__support/CPP/string_view.h"
1515
#include "src/__support/CPP/stringstream.h"
16+
#include "src/__support/macros/attributes.h"
1617
#include "src/__support/macros/properties/architectures.h"
1718

1819
#include <linux/param.h> // for exec_pagesize.
@@ -225,7 +226,7 @@ struct Thread {
225226
int get_name(cpp::StringStream &name) const;
226227
};
227228

228-
extern thread_local Thread self;
229+
extern LIBC_THREAD_LOCAL Thread self;
229230

230231
// Platforms should implement this function.
231232
[[noreturn]] void thread_exit(ThreadReturnValue retval, ThreadStyle style);

Diff for: libc/src/errno/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ add_entrypoint_object(
66
libc_errno.h # Include this
77
DEPENDS
88
libc.include.errno
9+
libc.src.__support.common
910
)

Diff for: libc/src/errno/libc_errno.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/macros/attributes.h"
910
#include "src/__support/macros/properties/architectures.h"
1011

1112
namespace __llvm_libc {
@@ -29,13 +30,13 @@ extern "C" {
2930
#ifdef LIBC_TARGET_ARCH_IS_GPU
3031
ErrnoConsumer __llvmlibc_errno;
3132
#else
32-
thread_local int __llvmlibc_errno;
33+
LIBC_THREAD_LOCAL int __llvmlibc_errno;
3334
#endif // LIBC_TARGET_ARCH_IS_GPU
3435
#else
3536
#ifdef LIBC_TARGET_ARCH_IS_GPU
3637
ErrnoConsumer __llvmlibc_internal_errno;
3738
#else
38-
thread_local int __llvmlibc_internal_errno;
39+
LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
3940
#endif // LIBC_TARGET_ARCH_IS_GPU
4041
#endif
4142
} // extern "C"

Diff for: libc/src/errno/libc_errno.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
1010
#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
1111

12+
#include "src/__support/macros/attributes.h"
1213
#include "src/__support/macros/properties/architectures.h"
1314

1415
#include <errno.h>
@@ -42,7 +43,7 @@ namespace __llvm_libc {
4243
extern "C" ErrnoConsumer __llvmlibc_internal_errno;
4344
#else // LIBC_TARGET_ARCH_IS_GPU
4445
extern "C" {
45-
extern thread_local int __llvmlibc_internal_errno;
46+
extern LIBC_THREAD_LOCAL int __llvmlibc_internal_errno;
4647
} // extern "C"
4748
#endif
4849

Diff for: libc/src/stdlib/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ add_object_library(
218218
rand_util.cpp
219219
HDRS
220220
rand_util.h
221+
DEPENDS
222+
libc.src.__support.common
221223
)
222224

223225
add_entrypoint_object(

Diff for: libc/src/stdlib/rand_util.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdlib/rand_util.h"
10+
#include "src/__support/macros/attributes.h"
1011

1112
namespace __llvm_libc {
1213

13-
thread_local unsigned long rand_next;
14+
LIBC_THREAD_LOCAL unsigned long rand_next;
1415

1516
} // namespace __llvm_libc

Diff for: libc/src/stdlib/rand_util.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#ifndef LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
1010
#define LLVM_LIBC_SRC_STDLIB_RAND_UTIL_H
1111

12+
#include "src/__support/macros/attributes.h"
13+
1214
namespace __llvm_libc {
1315

14-
extern thread_local unsigned long rand_next;
16+
extern LIBC_THREAD_LOCAL unsigned long rand_next;
1517

1618
} // namespace __llvm_libc
1719

Diff for: utils/bazel/llvm-project-overlay/libc/BUILD.bazel

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ libc_support_library(
9393
libc_support_library(
9494
name = "__support_macros_attributes",
9595
hdrs = ["src/__support/macros/attributes.h"],
96-
deps = [":libc_root"],
96+
deps = [
97+
":__support_macros_properties_architectures",
98+
":libc_root"
99+
],
97100
)
98101

99102
libc_support_library(
@@ -827,6 +830,7 @@ libc_function(
827830
hdrs = ["src/errno/libc_errno.h"],
828831
deps = [
829832
":__support_common",
833+
":__support_macros_attributes",
830834
":__support_macros_properties_architectures",
831835
],
832836
)

0 commit comments

Comments
 (0)