https://github.com/pranavk created 
https://github.com/llvm/llvm-project/pull/217700

Guard the extern "C" declaration and reference to __emutls_get_address in 
IncrementalExecutor.cpp behind a new CMake option 
CLANG_ENABLE_EMUTLS_GET_ADDRESS, which is OFF by default.

This prevents link errors in environments/runtimes where __emutls_get_address 
is not available.

Replace all other guards with the new cmake option.

>From 88e9770f9005c58f23f97c85f56e66c4b973ee2e Mon Sep 17 00:00:00 2001
From: Pranav Kant <[email protected]>
Date: Thu, 20 Aug 2026 10:03:36 -0700
Subject: [PATCH] [clang-repl] Add CLANG_ENABLE_EMUTLS_GET_ADDRESS cmake option

Guard the extern "C" declaration and reference to __emutls_get_address
in IncrementalExecutor.cpp behind a new CMake option
CLANG_ENABLE_EMUTLS_GET_ADDRESS, which is OFF by default.

This prevents link errors in environments/runtimes where
__emutls_get_address is not available.

Replace all other guards with the new cmake option.
---
 clang/CMakeLists.txt                                  |  3 +++
 clang/include/clang/Config/config.h.cmake             |  3 +++
 clang/lib/Interpreter/IncrementalExecutor.cpp         | 11 ++++-------
 .../gn/secondary/clang/include/clang/Config/BUILD.gn  |  1 +
 .../clang/include/clang/Config/config.h               |  3 +++
 5 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 89f584c5dceaa..e35f08467cde7 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -503,6 +503,9 @@ option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf 
fuzzer." OFF)
 option(CLANG_USE_EXPERIMENTAL_CONST_INTERP
   "Use the new experimental constant interpreter for compile-time evaluation." 
OFF)
 
+option(CLANG_ENABLE_EMUTLS_GET_ADDRESS
+  "Enable linking __emutls_get_address into IncrementalExecutor for emulated 
TLS." OFF)
+
 if (DEFINED CLANG_ENABLE_ARCMT)
   set(CLANG_ENABLE_OBJC_REWRITER ${CLANG_ENABLE_ARCMT})
   message(DEPRECATION "'CLANG_ENABLE_ARCMT' is deprecated as ARCMigrate has 
been removed from Clang. Please use 'CLANG_ENABLE_OBJC_REWRITER' instead to 
enable or disable the Objective-C rewriter.")
diff --git a/clang/include/clang/Config/config.h.cmake 
b/clang/include/clang/Config/config.h.cmake
index 11b4096726f67..1c62e6fca8824 100644
--- a/clang/include/clang/Config/config.h.cmake
+++ b/clang/include/clang/Config/config.h.cmake
@@ -95,4 +95,7 @@
 /* Enable the experimental new constant interpreter by default */
 #cmakedefine01 CLANG_USE_EXPERIMENTAL_CONST_INTERP
 
+/* Enable emutls get address workaround in IncrementalExecutor */
+#cmakedefine01 CLANG_ENABLE_EMUTLS_GET_ADDRESS
+
 #endif
diff --git a/clang/lib/Interpreter/IncrementalExecutor.cpp 
b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 6d337e7848699..b07c8f93fd355 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -17,6 +17,7 @@
 #endif // __EMSCRIPTEN__
 
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Config/config.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/ToolChain.h"
@@ -70,13 +71,9 @@
 // in a static archive and nothing else references it, it is never linked in 
and
 // ORC's process-symbol lookup cannot resolve it. Referencing it here
 // force-links the archive member so it is present regardless of how the host
-// provides it. Excluded where an emulated-TLS runtime is not guaranteed on the
-// link line, so the reference would fail to link: non-Unix (MSVC has no such
-// runtime), Emscripten (the wasm executor below does not use this JIT path),
-// and AIX / z/OS (whose runtimes may not provide the symbol). On those hosts
-// thread_locals instead rely on process-symbol lookup, unchanged from before.
-#if defined(LLVM_ON_UNIX) && !defined(__EMSCRIPTEN__) && !defined(_AIX) &&     
\
-    !defined(__MVS__) && !defined(__FreeBSD__)
+// provides it. Controlled by the CLANG_ENABLE_EMUTLS_GET_ADDRESS cmake option.
+// When disabled, thread_locals instead rely on process-symbol lookup.
+#if CLANG_ENABLE_EMUTLS_GET_ADDRESS
 extern "C" void *__emutls_get_address(void *);
 static void *getEmuTLSGetAddressPtr() {
   return reinterpret_cast<void *>(&__emutls_get_address);
diff --git a/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn 
b/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
index f8196d1b248e5..83fd6d6a0f66f 100644
--- a/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
+++ b/llvm/utils/gn/secondary/clang/include/clang/Config/BUILD.gn
@@ -32,6 +32,7 @@ write_cmake_config("Config") {
     "CLANG_SYSTEMZ_DEFAULT_ARCH=z10",
     "PPC_LINUX_DEFAULT_IEEELONGDOUBLE=",
     "CLANG_USE_EXPERIMENTAL_CONST_INTERP=",
+    "CLANG_ENABLE_EMUTLS_GET_ADDRESS=",
   ]
 
   if (clang_enable_static_analyzer) {
diff --git 
a/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h 
b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
index 7f7308f338966..03ef275c4ba39 100644
--- a/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
+++ b/utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
@@ -105,6 +105,9 @@
 /* Enable the experimental new constant interpreter by default */
 #define CLANG_USE_EXPERIMENTAL_CONST_INTERP 0
 
+/* Enable emutls get address workaround in IncrementalExecutor */
+#define CLANG_ENABLE_EMUTLS_GET_ADDRESS 0
+
 /* Directly provide definitions here behind platform preprocessor definitions.
  * The preprocessor conditions are sufficient to handle all of the 
configuration
  * on platforms targeted by Bazel, and defining these here more faithfully

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to