https://github.com/ChuanqiXu9 created 
https://github.com/llvm/llvm-project/pull/189796

Close https://github.com/llvm/llvm-project/issues/189415

The function shouldVarGenerateHereOnly should also handle thread_local 
variables, not just static variables. This fixes incorrect code generation for 
thread_local variables in named modules.

>From f286d02e3c77228dd77651c9521cb824a1bd5c4a Mon Sep 17 00:00:00 2001
From: Chuanqi Xu <[email protected]>
Date: Wed, 1 Apr 2026 11:46:34 +0800
Subject: [PATCH] [C++20] [Modules] Fix thread_local variable handling in
 modules

The function shouldVarGenerateHereOnly should also handle thread_local
variables, not just static variables. This fixes incorrect code generation
for thread_local variables in named modules.
---
 clang/lib/Serialization/ASTWriterDecl.cpp |  3 +-
 clang/test/Modules/pr189415.cppm          | 34 +++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Modules/pr189415.cppm

diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp 
b/clang/lib/Serialization/ASTWriterDecl.cpp
index 7646d5d5efe00..e415ac1e47862 100644
--- a/clang/lib/Serialization/ASTWriterDecl.cpp
+++ b/clang/lib/Serialization/ASTWriterDecl.cpp
@@ -361,7 +361,8 @@ namespace clang {
 // compilation of that unit, not by its users. (Inline variables are still
 // emitted in module users.)
 static bool shouldVarGenerateHereOnly(const VarDecl *VD) {
-  if (VD->getStorageDuration() != SD_Static)
+  if (VD->getStorageDuration() != SD_Static &&
+      VD->getStorageDuration() != SD_Thread)
     return false;
 
   if (VD->getDescribedVarTemplate())
diff --git a/clang/test/Modules/pr189415.cppm b/clang/test/Modules/pr189415.cppm
new file mode 100644
index 0000000000000..86c66124a618e
--- /dev/null
+++ b/clang/test/Modules/pr189415.cppm
@@ -0,0 +1,34 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+// 
+// RUN: %clang_cc1 -std=c++20 %t/counter.cppm -triple %itanium_abi_triple \
+// RUN:   -emit-reduced-module-interface -o %t/counter.pcm
+// RUN: %clang_cc1 -std=c++20 %t/user.cpp -triple %itanium_abi_triple 
-fprebuilt-module-path=%t \
+// RUN:   -disable-llvm-passes -emit-llvm -o - | FileCheck %s
+
+//--- counter.cppm
+export module counter;
+
+namespace counter {
+
+// Works without thread_local or with inline keyword
+thread_local int next = 1;
+
+export inline auto get_next() noexcept -> int
+{
+    return next++;
+}
+
+}
+
+//--- user.cpp
+import counter;
+
+auto user() -> int
+{
+    return counter::get_next();
+}
+
+// CHECK: @_ZN7counterW7counter4nextE = external {{.*}}thread_local 
{{.*}}global
+

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

Reply via email to