Author: Fabian Meumertzheim
Date: 2026-11-10T17:48:51+08:00
New Revision: ccc134e95c26472d145a4560a222e92a33dd51cc

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

LOG: [clang][Modules] Don't home functions excluded from explicit instantiation 
(#240594)

With `-fmodules-codegen`, inline functions and implicit template
instantiations are emitted into the module's object file and users of
the module only reference them. libc++ marks most of its inline
functions as hidden and excluded from explicit instantiation, so such a
reference can't be resolved if the module's object file is linked into a
different shared library than the user. For example, a test that links
against the shared library of a module that instantiates
`std::vector<T>` fails with "undefined hidden symbol" for the members of
`std::vector<T>` that it calls.

Emit these functions in every user instead, just like `always_inline`
functions. This matches the meaning of the attribute, which already
makes every user instantiate the function despite an explicit
instantiation declaration. Before e9d85264ac replaced `always_inline`
with `exclude_from_explicit_instantiation` in libc++, its hidden
functions were never homed because of the `always_inline` check.

Assisted-by: Claude Code (Claude Opus 5.5)

Added: 
    clang/test/Modules/codegen-exclude-from-explicit-instantiation.cpp

Modified: 
    clang/lib/Serialization/ASTWriterDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ASTWriterDecl.cpp 
b/clang/lib/Serialization/ASTWriterDecl.cpp
index 5ec9b32e1b6f5d..bd8bc046f2874b 100644
--- a/clang/lib/Serialization/ASTWriterDecl.cpp
+++ b/clang/lib/Serialization/ASTWriterDecl.cpp
@@ -395,8 +395,12 @@ static bool shouldFunctionGenerateHereOnly(const 
FunctionDecl *FD) {
        Ctx.getLangOpts().BuildingPCHWithObjectFile))
     // Under -fmodules-codegen, codegen is performed for all non-internal,
     // non-always_inline functions, unless they are available elsewhere.
-    if (!FD->hasAttr<AlwaysInlineAttr>() && Linkage != GVA_Internal &&
-        Linkage != GVA_AvailableExternally)
+    // Functions excluded from explicit instantiation are emitted by every user
+    // instead, as they are usually hidden and the module's object file may be
+    // linked into a 
diff erent shared library.
+    if (!FD->hasAttr<AlwaysInlineAttr>() &&
+        !FD->hasAttr<ExcludeFromExplicitInstantiationAttr>() &&
+        Linkage != GVA_Internal && Linkage != GVA_AvailableExternally)
       return true;
 
   Module *M = FD->getOwningModule();

diff  --git 
a/clang/test/Modules/codegen-exclude-from-explicit-instantiation.cpp 
b/clang/test/Modules/codegen-exclude-from-explicit-instantiation.cpp
new file mode 100644
index 00000000000000..71d03443d036ca
--- /dev/null
+++ b/clang/test/Modules/codegen-exclude-from-explicit-instantiation.cpp
@@ -0,0 +1,43 @@
+// Test that functions excluded from explicit instantiation, such as libc++'s
+// hidden ones, are emitted by every user of a module built with
+// -fmodules-codegen instead of only in the module's object file.
+
+// RUN: rm -rf %t && mkdir %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x c++ -fmodules-codegen -fmodules 
-emit-module -fmodule-name=foo %t/foo.modulemap -o %t/foo.pcm
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %t/foo.pcm -o - | 
FileCheck %s --check-prefix=DEF
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fmodules -fmodule-file=%t/foo.pcm 
-I%t -disable-llvm-passes -emit-llvm %t/test.cpp -o - | FileCheck %s 
--check-prefix=USE
+
+// DEF-DAG: define weak_odr void @_Z11instantiatev()
+// DEF-DAG: define weak_odr void @_ZN1SIiE5homedEv(
+// DEF-DAG: define linkonce_odr hidden void @_ZN1SIiE8excludedEv(
+
+// USE-DAG: declare void @_ZN1SIiE5homedEv(
+// USE-DAG: define linkonce_odr hidden void @_ZN1SIiE8excludedEv(
+
+//--- foo.h
+template <typename T> struct S {
+  void homed() {}
+  __attribute__((visibility("hidden"), exclude_from_explicit_instantiation))
+  void excluded() {}
+};
+
+inline void instantiate() {
+  S<int> s;
+  s.homed();
+  s.excluded();
+}
+
+//--- test.cpp
+#include "foo.h"
+
+void test() {
+  S<int> s;
+  s.homed();
+  s.excluded();
+}
+
+//--- foo.modulemap
+module foo { header "foo.h" }


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

Reply via email to