[clang] [clang] Add use-sample-profile attribute to thread-local wrapper (PR #245514)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 01:37:34 PST 2026
https://github.com/XinlongZHANG-Bob updated https://github.com/llvm/llvm-project/pull/245514
>From 1bf9b45e4bf5ad6e7cfdf9e7f103c36f3577cfd7 Mon Sep 17 00:00:00 2001
From: XinlongZHANG-Bob <zhangxinlong.bob at bytedance.com>
Date: Fri, 27 Nov 2026 15:04:14 +0800
Subject: [PATCH 1/2] [clang] Add use-sample-profile attribute to thread-local
wrapper
The TLS wrapper is generated directly and bypasses
CodeGenFunction::StartFunction, where "use-sample-profile" is normally
added. Under AutoFDO, tls wrapper do not have the attribute, so the
inliner rejects inlining functions that have "conflicting attributes"
(the isEqual<UseSampleProfileAttr> inline-compatibility rule in
llvm/include/llvm/IR/Attributes.td).
Add the attribute in `SetLLVMFunctionAttributesForDefinition`, rather than
`StartFunction` so it can be inlined into sample-profile-compiled callers.
reproduce with source code `repro.cc`:
```
extern thread_local int tls;
extern "C" int ReadNode() {
return tls;
}
```
and profile data `sample.prof`:
```
ReadNode:100000:100000
1: 100000
```
```
mkdir -p build
clang++ -O2 -fprofile-sample-use=sample.prof -S -emit-llvm repro.cc \
-Rpass=inline -Rpass-missed=inline -o build/sample.ll
clang++ -O2 -S -emit-llvm repro.cc \
-Rpass=inline -Rpass-missed=inline -o build/plain.ll
```
---
clang/lib/CodeGen/CodeGenFunction.cpp | 3 ---
clang/lib/CodeGen/CodeGenModule.cpp | 8 +++++++
.../use-sample-profile-tls-wrapper.cpp | 23 +++++++++++++++++++
3 files changed, 31 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CodeGenCXX/use-sample-profile-tls-wrapper.cpp
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index d9c886259e4cfd..6525e5139e8fe3 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1008,9 +1008,6 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
if (CGM.getCodeGenOpts().ProfileSampleAccurate)
Fn->addFnAttr("profile-sample-accurate");
- if (!CGM.getCodeGenOpts().SampleProfileFile.empty())
- Fn->addFnAttr("use-sample-profile");
-
if (D && D->hasAttr<CFICanonicalJumpTableAttr>())
Fn->addFnAttr("cfi-canonical-jump-table");
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 390ca6dbba4616..605277ab2ed5e4 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3258,6 +3258,14 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
B.addAttribute("stack-probe-size",
std::to_string(CodeGenOpts.StackProbeSize));
+ // Set the attribute here instead of CodeGenFunction::StartFunction. Some
+ // synthetic functions (e.g. Itanium TLS wrapper) bypass StartFunction,
+ // can also get it.
+ // Inliner pass requires that the caller and callee have the same sample
+ // profile attribute according to CompatRule in Attributes.td.
+ if (!CodeGenOpts.SampleProfileFile.empty())
+ B.addAttribute("use-sample-profile");
+
if (!CodeGenUtils::hasUnwindExceptions(LangOpts))
B.addAttribute(llvm::Attribute::NoUnwind);
diff --git a/clang/test/CodeGenCXX/use-sample-profile-tls-wrapper.cpp b/clang/test/CodeGenCXX/use-sample-profile-tls-wrapper.cpp
new file mode 100644
index 00000000000000..0e520874079260
--- /dev/null
+++ b/clang/test/CodeGenCXX/use-sample-profile-tls-wrapper.cpp
@@ -0,0 +1,23 @@
+// Test that the Itanium thread-local wrapper function carries the
+// "use-sample-profile" attribute when sample profile is enabled, so that it is
+// inline-compatible with the function that reads the thread_local variable.
+
+// REQUIRES: x86-registered-target
+
+// RUN: printf 'ReadNode:100000:100000\n 1: 100000\n' > %t.prof
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++14 -O2 -disable-llvm-passes \
+// RUN: -fprofile-sample-use=%t.prof %s -emit-llvm -o - | FileCheck %s
+
+extern thread_local int tls;
+
+extern "C" int ReadNode() {
+ return tls;
+}
+
+// The caller gets "use-sample-profile" from CodeGenFunction::StartFunction,
+// and the thread-local wrapper must carry the same attribute, otherwise
+// inlining into ReadNode fails with "conflicting attributes".
+// CHECK: define{{.*}} @ReadNode{{.*}} #[[READNODE_ATTR:[0-9]+]]
+// CHECK: define{{.*}} @_ZTW3tls{{.*}} #[[WRAPPER_ATTR:[0-9]+]]
+// CHECK: attributes #[[READNODE_ATTR]] = {{.*}} "use-sample-profile"
+// CHECK: attributes #[[WRAPPER_ATTR]] = {{.*}} "use-sample-profile"
>From aaa44fb58927bb7f0110099be79277a320d81fc6 Mon Sep 17 00:00:00 2001
From: XinlongZHANG-Bob <zhangxinlong.bob at bytedance.com>
Date: Fri, 27 Nov 2026 17:36:08 +0800
Subject: [PATCH 2/2] [clang][CodeGen] Apply definition attributes to MSVC ctor
closures and __stmts__
Synthetic functions emitted outside EmitGlobalFunctionDefinition never called
SetLLVMFunctionAttributesForDefinition, so they missed attributes such as
use-sample-profile. Since the inliner requires this attribute to be identical
between caller and callee, these wrappers could otherwise fail to inline with
"conflicting attributes".
- MicrosoftCXXABI::getAddrOfCXXCtorClosure: set definition attributes on the
default and copy constructor closures (??_F / ??_O).
- CodeGenModule::EmitTopLevelStmt: set internal-function attributes on the
generated __stmts__N function.
assist by deepseek
---
clang/lib/CodeGen/CodeGenModule.cpp | 2 ++
clang/lib/CodeGen/MicrosoftCXXABI.cpp | 2 ++
.../use-sample-profile-msvc-ctor-closure.cpp | 31 +++++++++++++++++++
.../use-sample-profile-top-level-stmt.cpp | 17 ++++++++++
4 files changed, 52 insertions(+)
create mode 100644 clang/test/CodeGenCXX/use-sample-profile-msvc-ctor-closure.cpp
create mode 100644 clang/test/CodeGenCXX/use-sample-profile-top-level-stmt.cpp
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 605277ab2ed5e4..6ee6d06de9c809 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -8081,6 +8081,8 @@ void CodeGenModule::EmitTopLevelStmt(const TopLevelStmtDecl *D) {
llvm::Function *Fn = llvm::Function::Create(
FnTy, llvm::GlobalValue::InternalLinkage, Name, &getModule());
+ SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
+
CurCGF.reset(new CodeGenFunction(*this));
GlobalTopLevelStmtBlockInFlight.second = D;
CurCGF->StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp
index e7aac1530637ce..83d6097c706e60 100644
--- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp
+++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp
@@ -4185,6 +4185,8 @@ MicrosoftCXXABI::getAddrOfCXXCtorClosure(const CXXConstructorDecl *CD,
ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
bool IsCopy = CT == Ctor_CopyingClosure;
+ CGM.SetLLVMFunctionAttributesForDefinition(CD, ThunkFn);
+
// Start codegen.
CodeGenFunction CGF(CGM);
CGF.CurGD = GlobalDecl(CD, Ctor_Complete);
diff --git a/clang/test/CodeGenCXX/use-sample-profile-msvc-ctor-closure.cpp b/clang/test/CodeGenCXX/use-sample-profile-msvc-ctor-closure.cpp
new file mode 100644
index 00000000000000..e04caa2a9c4860
--- /dev/null
+++ b/clang/test/CodeGenCXX/use-sample-profile-msvc-ctor-closure.cpp
@@ -0,0 +1,31 @@
+// Test that the MSVC constructor closures carry the "use-sample-profile"
+// attribute when sample profile is enabled, so that they are inline-compatible
+// with the constructors they forward to.
+
+// REQUIRES: x86-registered-target
+
+// RUN: printf 'trigger:100000:100000\n 1: 100000\n' > %t.prof
+// RUN: %clang_cc1 -triple i686-pc-windows-msvc -std=c++14 -O2 -disable-llvm-passes \
+// RUN: -fcxx-exceptions -fms-extensions -fprofile-sample-use=%t.prof %s \
+// RUN: -emit-llvm -o - | FileCheck %s
+
+// Default constructor closure (??_F...): an exported default constructor with
+// default arguments gets a wrapper that fills in the default arguments.
+struct DefaultClosure {
+ __declspec(dllexport) DefaultClosure(int n = 42) {}
+};
+
+// Copy constructor closure (??_O...): throwing an exception whose type has a
+// copy constructor with an extra default parameter requires the copy-ctor
+// closure entry point.
+struct CopyClosure {
+ CopyClosure(const CopyClosure &, int n = 42);
+};
+void trigger(CopyClosure &c) {
+ throw c;
+}
+
+// CHECK: define{{.*}}@"??_FDefaultClosure@@QAEXXZ"{{.*}} #[[DEFAULT_ATTR:[0-9]+]]
+// CHECK: define{{.*}}@"??_OCopyClosure@@QAEXABU0@@Z"{{.*}} #[[COPY_ATTR:[0-9]+]]
+// CHECK: attributes #[[DEFAULT_ATTR]] = {{.*}} "use-sample-profile"
+// CHECK: attributes #[[COPY_ATTR]] = {{.*}} "use-sample-profile"
diff --git a/clang/test/CodeGenCXX/use-sample-profile-top-level-stmt.cpp b/clang/test/CodeGenCXX/use-sample-profile-top-level-stmt.cpp
new file mode 100644
index 00000000000000..b34ca8c9d40fa7
--- /dev/null
+++ b/clang/test/CodeGenCXX/use-sample-profile-top-level-stmt.cpp
@@ -0,0 +1,17 @@
+// Test that the incremental-extensions top-level statement function carries
+// the "use-sample-profile" attribute when sample profile is enabled, so that
+// any functions called from it are inline-compatible.
+
+// REQUIRES: x86-registered-target
+
+// RUN: printf 'helper:100000:100000\n 1: 100000\n' > %t.prof
+// RUN: %clang_cc1 -std=c++20 -fincremental-extensions -triple x86_64-linux-gnu \
+// RUN: -O2 -disable-llvm-passes -fprofile-sample-use=%t.prof %s \
+// RUN: -emit-llvm -o - | FileCheck %s
+
+static int helper() { return 42; }
+int n;
+n = helper();
+
+// CHECK: define internal void @__stmts__0{{.*}} #[[STMT_ATTR:[0-9]+]]
+// CHECK: attributes #[[STMT_ATTR]] = {{.*}} "use-sample-profile"
More information about the cfe-commits
mailing list