[clang] [Clang] Add use-sample-profile attribute to thread-local wrapper (PR #245514)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 23:03:17 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: XinlongZHANG-Bob

<details>
<summary>Changes</summary>

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 at the wrapper creation point 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
```

---
Full diff: https://github.com/llvm/llvm-project/pull/245514.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/ItaniumCXXABI.cpp (+7) 
- (added) clang/test/CodeGenCXX/use-sample-profile-tls-wrapper.cpp (+24) 


``````````diff
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index 3fbee4dc6bb03..224dd0a4dc1fb 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -3163,6 +3163,13 @@ ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
 
   CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Wrapper, /*IsThunk=*/false);
 
+  // Under AutoFDO, the TLS wrapper is created directly and does not go
+  // through StartFunction, so it lacks "use-sample-profile". Inline pass
+  // requires the caller and callee to have the same attribute, so add it
+  // here to keep the wrapper inlineable.
+  if (!CGM.getCodeGenOpts().SampleProfileFile.empty())
+    Wrapper->addFnAttr("use-sample-profile");
+
   // Always resolve references to the wrapper at link time.
   if (!Wrapper->hasLocalLinkage())
     if (!isThreadWrapperReplaceable(VD, CGM) ||
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 0000000000000..d9eef3e1f3561
--- /dev/null
+++ b/clang/test/CodeGenCXX/use-sample-profile-tls-wrapper.cpp
@@ -0,0 +1,24 @@
+// 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.
+// CHECK: define {{.*}} @ReadNode() {{.*}} #[[READNODE_ATTR:[0-9]+]]
+// CHECK: attributes #[[READNODE_ATTR]] = {{.*}} "use-sample-profile"
+
+// The thread-local wrapper must carry the same attribute, otherwise inlining
+// into ReadNode fails with "conflicting attributes".
+// CHECK: define {{.*}} @_ZTW3tls() {{.*}} #[[WRAPPER_ATTR:[0-9]+]]
+// CHECK: attributes #[[WRAPPER_ATTR]] = {{.*}} "use-sample-profile"

``````````

</details>


https://github.com/llvm/llvm-project/pull/245514


More information about the cfe-commits mailing list