Skip to content

Commit 00db1aa

Browse files
committed
[clang-repl] Detect __emutls_get_address availability at configure time
Detect if __emutls_get_address is available in the compiler runtime at configure time via check_cxx_source_compiles (CLANG_HAVE_EMUTLS_GET_ADDRESS) and guard its extern "C" declaration and reference in IncrementalExecutor.cpp. This prevents link errors in environments/runtimes where __emutls_get_address is not available. TAG=agy CONV=dd895981-3d14-4e98-a942-6aedc8fbfd77
1 parent c548c6a commit 00db1aa

6 files changed

Lines changed: 21 additions & 7 deletions

File tree

clang/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ if( CLANG_HAVE_DLFCN_H )
220220
cmake_pop_check_state()
221221
endif()
222222

223+
include(CheckCXXSourceCompiles)
224+
check_cxx_source_compiles("
225+
extern \"C\" void *__emutls_get_address(void *);
226+
int main() {
227+
return __emutls_get_address((void *)0) != (void *)0;
228+
}
229+
" CLANG_HAVE_EMUTLS_GET_ADDRESS)
230+
223231
set(CLANG_RESOURCE_DIR "" CACHE STRING
224232
"Relative directory from the Clang binary to its resource files.")
225233

clang/include/clang/Config/config.h.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,7 @@
9595
/* Enable the experimental new constant interpreter by default */
9696
#cmakedefine01 CLANG_USE_EXPERIMENTAL_CONST_INTERP
9797

98+
/* Define if __emutls_get_address is available in the compiler runtime */
99+
#cmakedefine01 CLANG_HAVE_EMUTLS_GET_ADDRESS
100+
98101
#endif

clang/lib/Interpreter/IncrementalExecutor.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endif // __EMSCRIPTEN__
1818

1919
#include "clang/Basic/TargetInfo.h"
20+
#include "clang/Config/config.h"
2021
#include "clang/Driver/Compilation.h"
2122
#include "clang/Driver/Driver.h"
2223
#include "clang/Driver/ToolChain.h"
@@ -70,13 +71,10 @@
7071
// in a static archive and nothing else references it, it is never linked in and
7172
// ORC's process-symbol lookup cannot resolve it. Referencing it here
7273
// force-links the archive member so it is present regardless of how the host
73-
// provides it. Excluded where an emulated-TLS runtime is not guaranteed on the
74-
// link line, so the reference would fail to link: non-Unix (MSVC has no such
75-
// runtime), Emscripten (the wasm executor below does not use this JIT path),
76-
// and AIX / z/OS (whose runtimes may not provide the symbol). On those hosts
77-
// thread_locals instead rely on process-symbol lookup, unchanged from before.
78-
#if defined(LLVM_ON_UNIX) && !defined(__EMSCRIPTEN__) && !defined(_AIX) && \
79-
!defined(__MVS__) && !defined(__FreeBSD__)
74+
// provides it. Defined if available at configure time
75+
// (CLANG_HAVE_EMUTLS_GET_ADDRESS). When unavailable, thread_locals instead rely
76+
// on process-symbol lookup.
77+
#if CLANG_HAVE_EMUTLS_GET_ADDRESS
8078
extern "C" void *__emutls_get_address(void *);
8179
static void *getEmuTLSGetAddressPtr() {
8280
return reinterpret_cast<void *>(&__emutls_get_address);

llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ write_cmake_config("Config") {
3232
"CLANG_SYSTEMZ_DEFAULT_ARCH=z10",
3333
"PPC_LINUX_DEFAULT_IEEELONGDOUBLE=",
3434
"CLANG_USE_EXPERIMENTAL_CONST_INTERP=",
35+
"CLANG_HAVE_EMUTLS_GET_ADDRESS=",
3536
]
3637

3738
if (clang_enable_static_analyzer) {

utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,7 @@ cc_library(
19481948
":ast",
19491949
":basic",
19501950
":codegen",
1951+
":config",
19511952
":driver",
19521953
":edit",
19531954
":frontend",

utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
/* Enable the experimental new constant interpreter by default */
106106
#define CLANG_USE_EXPERIMENTAL_CONST_INTERP 0
107107

108+
/* Define if __emutls_get_address is available in the compiler runtime */
109+
#define CLANG_HAVE_EMUTLS_GET_ADDRESS 0
110+
108111
/* Directly provide definitions here behind platform preprocessor definitions.
109112
* The preprocessor conditions are sufficient to handle all of the configuration
110113
* on platforms targeted by Bazel, and defining these here more faithfully

0 commit comments

Comments
 (0)