[clang] c1498de - [clang][CodeGen] Attach a single callgraph type identifier to function definitions (#230838)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 14:56:21 PST 2026
Author: Prabhu Rajasekaran
Date: 2026-11-04T22:56:12Z
New Revision: c1498de98d839e25c4c020a5bdc4f390464c2930
URL: https://github.com/llvm/llvm-project/commit/c1498de98d839e25c4c020a5bdc4f390464c2930
DIFF: https://github.com/llvm/llvm-project/commit/c1498de98d839e25c4c020a5bdc4f390464c2930.diff
LOG: [clang][CodeGen] Attach a single callgraph type identifier to function definitions (#230838)
A function definition is annotated with its call graph section type identifier
twice, when its declaration is created (SetFunctionAttributes) and when its
body is emitted (SetLLVMFunctionAttributesForDefinition), so every
defined function carries two identical `!callgraph` attachments:
define void @foo() #0 !callgraph !3 !callgraph !3 {
Replace the attachment instead of adding a second one, so that a
function carries exactly one identifier. The `.llvm.callgraph` section is
unaffected, the AsmPrinter only uses the first identifier of a function.
Assisted-by: Claude
Added:
clang/test/CodeGen/call-graph-section-single-type-id.c
Modified:
clang/lib/CodeGen/CodeGenModule.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index e59f7811c89c1..88129acfe2dab 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3720,10 +3720,13 @@ void CodeGenModule::createIndirectFunctionTypeMD(const FunctionDecl *FD,
QT = ReconstructCallGraphPrototype(FNPT, ParamTypes);
}
- F->addMetadata(
+ // A function definition is annotated when its declaration is created and
+ // again when its body is emitted. Replace the attachment instead of adding
+ // a second one, so that a function carries exactly one type identifier.
+ F->setMetadata(
llvm::LLVMContext::MD_callgraph,
- *llvm::MDTuple::get(getLLVMContext(),
- {CreateMetadataIdentifierForCallGraphType(QT)}));
+ llvm::MDTuple::get(getLLVMContext(),
+ {CreateMetadataIdentifierForCallGraphType(QT)}));
}
}
diff --git a/clang/test/CodeGen/call-graph-section-single-type-id.c b/clang/test/CodeGen/call-graph-section-single-type-id.c
new file mode 100644
index 0000000000000..ad655ddc16a0e
--- /dev/null
+++ b/clang/test/CodeGen/call-graph-section-single-type-id.c
@@ -0,0 +1,18 @@
+/// Tests that a function definition carries a single call graph section type
+/// identifier: its declaration and its body are annotated separately, and the
+/// second annotation must replace the first one.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fexperimental-call-graph-section -emit-llvm -o - %s | FileCheck %s
+
+void foo(void);
+
+// CHECK: define {{(dso_local )?}}void @bar(){{[^!]*}}!callgraph ![[TVOID:[0-9]+]] {
+void bar(void) {
+ foo();
+}
+
+/// The declaration of foo was created and annotated for the call in bar.
+// CHECK: define {{(dso_local )?}}void @foo(){{[^!]*}}!callgraph ![[TVOID]] {
+void foo(void) {
+}
More information about the cfe-commits
mailing list