Author: Chuanqi Xu
Date: 2026-03-16T09:05:33Z
New Revision: 87f1a2be7505a9f2a2e5179cde36957655ef8d5b

URL: 
https://github.com/llvm/llvm-project/commit/87f1a2be7505a9f2a2e5179cde36957655ef8d5b
DIFF: 
https://github.com/llvm/llvm-project/commit/87f1a2be7505a9f2a2e5179cde36957655ef8d5b.diff

LOG: [C++20] [Modules] Don't add discardable variables to module initializers 
(#186752)

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

The root cause of the problem is, we shouldn't add the inline variable
(which is discardable in linker's point of view) to the module's
initializers.

I verified with GCC's generated code to make the behavior consistent.

This is also a small optimization by the way.

Added: 
    clang/test/Modules/pr170099.cppm

Modified: 
    clang/lib/Sema/SemaDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 054b664ca0a8b..101d5085b980b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -15138,7 +15138,10 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl 
*var) {
 
   // If this variable must be emitted, add it as an initializer for the current
   // module.
-  if (Context.DeclMustBeEmitted(var) && !ModuleScopes.empty())
+  if (Context.DeclMustBeEmitted(var) && !ModuleScopes.empty() &&
+      (ModuleScopes.back().Module->isHeaderLikeModule() ||
+       // For named modules, we may only emit non discardable variables.
+       !isDiscardableGVALinkage(Context.GetGVALinkageForVariable(var))))
     Context.addModuleInitializer(ModuleScopes.back().Module, var);
 
   // Build the bindings if this is a structured binding declaration.

diff  --git a/clang/test/Modules/pr170099.cppm 
b/clang/test/Modules/pr170099.cppm
new file mode 100644
index 0000000000000..ca3b4c10fe729
--- /dev/null
+++ b/clang/test/Modules/pr170099.cppm
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++26 -O3 -emit-llvm %s -o 
- | FileCheck %s
+module;
+
+struct A {};
+
+struct B {
+       int x;
+       A   a;
+       constexpr B(char *) { x = int(); }
+       ~B();
+};
+
+struct C {
+       B b = "";
+} inline c{};
+
+export module foo;
+
+// Just to make sure it won't crash
+// CHECK: @_ZGIW3foo


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

Reply via email to