https://github.com/kubamracek created
https://github.com/llvm/llvm-project/pull/244536
In #238529, we started emiting and using `likely_module_local` attributes on
inline functions and template instantiations, when their definition is in a
.cpp file (as opposed to a #included header). LLVM then applies different
heuristics/thresholds to those (in principle; in practice today only a single
specific inlining cap is lifted).
We've hit a build breakage triggered by #238529 in a large production codebase,
and I believe the case is a user's code bug (illegal in C++), but given that
we've observed it "in the wild" and the fix in Clang is simple and doesn't
decrease the effectiness of the feature, I propose we avoid this problem via a
Clang tweak.
Details: If a TU defines a templated function (in a .cpp file) and other
translation units reference the function through a declaration in a header...
```
// decl-only.h
template <typename T> T crossTUTemplate(T x);
// sink.cpp
#include "decl-only.h"
template <typename T> T crossTUTemplate(T x) { /* large */ }
int sink(int v) { return crossTUTemplate(v); }
// main.cpp
#include "decl-only.h"
int use(int v) { return sink(v) + crossTUTemplate(v); }
```
... then this setup succeeds to link *only when* sink.o keeps its linkonce_odr
copy of crossTUTemplate<int>. This is (as before) entirely dependent on whether
the templated function body is inlined. We already have had this problem (link
fails) even without the `likely_module_local` attribute -- if the body was
small enough. The added `likely_module_local` attribute just makes the inlining
(and thus the link failure) also possible to happen when the body is large.
Solution: Require that the template is not even *declared* in a #include'd
header to emit the `likely_module_local` attributes.
*(I mostly claim that this is illegal C++ because the problem is easy to
reproduce even before #238529 on a small templated function body. Can someone
find the exact C++ spec clause on this?)*
Assisted-by: Claude
>From 5e7e036aadc581d7d4020c82f954d28c322cca39 Mon Sep 17 00:00:00 2001
From: Kuba Mracek <[email protected]>
Date: Fri, 20 Nov 2026 08:31:32 +0000
Subject: [PATCH] [Clang] Only emit likely_module_local when all declarations
are in the main file
---
clang/lib/CodeGen/CodeGenModule.cpp | 22 +++++--
.../likely-module-local-cross-tu-template.cpp | 60 +++++++++++++++++++
2 files changed, 77 insertions(+), 5 deletions(-)
create mode 100644
clang/test/CodeGenCXX/likely-module-local-cross-tu-template.cpp
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp
b/clang/lib/CodeGen/CodeGenModule.cpp
index f0ea493e17222..af00da9c78a81 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3365,13 +3365,25 @@ void
CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
if (CodeGenOpts.ModuleLocalHints) {
const auto *FD = dyn_cast<FunctionDecl>(D);
const SourceManager &SM = getContext().getSourceManager();
- // D->getLocation(), for a template instantiation, is the location of the
- // template's definition, not instantiation.
// TODO: Also consider template instantiations where a template argument is
// a class declared in the main file.
- bool InMainFile = SM.isInMainFile(D->getLocation());
- if (FD && (FD->isInlined() || FD->isTemplateInstantiation()) && InMainFile)
- B.addAttribute(llvm::Attribute::LikelyModuleLocal);
+ if (FD && (FD->isInlined() || FD->isTemplateInstantiation())) {
+ // Usually, check the decl directly...
+ const FunctionDecl *DeclToCheck = FD;
+ // ...but for a template instantiation, check the pattern it was
+ // instantiated from, so that we also see the template's declarations
+ // (e.g. one in a header), which aren't redeclarations of the
+ // instantiation.
+ if (const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern())
+ DeclToCheck = Pattern;
+
+ bool AllDeclsInMainFile =
+ llvm::all_of(DeclToCheck->redecls(), [&](const FunctionDecl *R) {
+ return SM.isInMainFile(R->getLocation());
+ });
+ if (AllDeclsInMainFile)
+ B.addAttribute(llvm::Attribute::LikelyModuleLocal);
+ }
}
F->addFnAttrs(B);
diff --git a/clang/test/CodeGenCXX/likely-module-local-cross-tu-template.cpp
b/clang/test/CodeGenCXX/likely-module-local-cross-tu-template.cpp
new file mode 100644
index 0000000000000..4d483bc0e3ec7
--- /dev/null
+++ b/clang/test/CodeGenCXX/likely-module-local-cross-tu-template.cpp
@@ -0,0 +1,60 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -std=c++17 -O2 -emit-llvm
-o - %t/sink.cpp | FileCheck %s --check-prefix=KEEP
+// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -std=c++17 -O2 -emit-llvm
-fno-module-local-hints -o - %t/sink.cpp | FileCheck %s --check-prefix=KEEP
+// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -std=c++17 -O2 -emit-llvm
-o - %t/main.cpp | FileCheck %s --check-prefix=MAIN
+// RUN: %clang_cc1 -triple arm64-apple-macosx14.0.0 -std=c++17 -O2
-disable-llvm-passes -emit-llvm -o - %t/sink.cpp | FileCheck %s
--check-prefix=ATTRS
+
+// KEEP: define {{.*}} @_Z4sinki(
+// KEEP: call {{.*}} @_Z15crossTUTemplateIiET_S0_(
+// KEEP: define linkonce_odr {{.*}} @_Z15crossTUTemplateIiET_S0_(
+
+// MAIN: declare {{.*}} @_Z15crossTUTemplateIiET_S0_(
+
+// Functions declared in decl-only.h aren't marked, those only declared in
+// sink.cpp are.
+// ATTRS-DAG: define linkonce_odr {{.*}} @_Z15crossTUTemplateIiET_S0_({{.*}})
#[[OFF:[0-9]+]]
+// ATTRS-DAG: define linkonce_odr {{.*}} @_Z13crossTUInlinei({{.*}})
#[[OFFINL:[0-9]+]]
+// ATTRS-DAG: define linkonce_odr {{.*}} @_ZN7HdrType6methodEv({{.*}})
#[[OFFINL]]
+// ATTRS-DAG: define linkonce_odr {{.*}} @_Z13localTemplateIiET_S0_({{.*}})
#[[ON:[0-9]+]]
+// ATTRS-DAG: define linkonce_odr {{.*}} @_Z11localInlinei({{.*}})
#[[ONINL:[0-9]+]]
+// ATTRS-DAG: attributes #[[OFF]] = { mustprogress nounwind {{.*}}}
+// ATTRS-DAG: attributes #[[OFFINL]] = { inlinehint mustprogress nounwind
{{.*}}}
+// ATTRS-DAG: attributes #[[ON]] = { likely_module_local mustprogress nounwind
{{.*}}}
+// ATTRS-DAG: attributes #[[ONINL]] = { inlinehint likely_module_local
mustprogress nounwind {{.*}}}
+
+//--- decl-only.h
+// Declared only; the definitions live in sink.cpp.
+template <typename T> T crossTUTemplate(T x);
+inline int crossTUInline(int x);
+struct HdrType {
+ int method();
+};
+int sink(int);
+int sinkMore(int);
+
+//--- sink.cpp
+#include "decl-only.h"
+void opaque(int);
+// Big enough to stay out of line without the sole-call bonus.
+template <typename T> T crossTUTemplate(T x) {
+#define S(i) opaque(x + i); x ^= x << (i % 7); opaque(x * i);
+#define S10(i) S(i##0) S(i##1) S(i##2) S(i##3) S(i##4) \
+ S(i##5) S(i##6) S(i##7) S(i##8) S(i##9)
+ S10(1) S10(2) S10(3) S10(4)
+ return x;
+}
+int sink(int v) { return crossTUTemplate(v); }
+
+inline int crossTUInline(int x) { return x + 1; }
+inline int HdrType::method() { return 2; }
+template <typename T> T localTemplate(T x) { return x + 3; }
+inline int localInline(int x) { return x + 4; }
+int sinkMore(int v) {
+ return crossTUInline(v) + HdrType().method() + localTemplate(v) +
+ localInline(v);
+}
+
+//--- main.cpp
+#include "decl-only.h"
+int use(int v) { return sink(v) + crossTUTemplate(v); }
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits