https://github.com/Prabhuk created
https://github.com/llvm/llvm-project/pull/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
>From 1e4099a108e91f68993bc9992ff479a9042030c4 Mon Sep 17 00:00:00 2001
From: prabhukr <[email protected]>
Date: Sat, 10 Oct 2026 12:23:16 -0700
Subject: [PATCH] [clang][CodeGen] Attach a single callgraph type identifier to
function definitions
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
---
clang/lib/CodeGen/CodeGenModule.cpp | 9 +++++---
.../call-graph-section-single-type-id.c | 21 +++++++++++++++++++
2 files changed, 27 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CodeGen/call-graph-section-single-type-id.c
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp
b/clang/lib/CodeGen/CodeGenModule.cpp
index 62ef11a697b1c..3c6e09effe418 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3664,10 +3664,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..93a421fb084c6
--- /dev/null
+++ b/clang/test/CodeGen/call-graph-section-single-type-id.c
@@ -0,0 +1,21 @@
+// 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 \
+// RUN: -emit-llvm -o - %s | FileCheck %s
+
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc
-fexperimental-call-graph-section \
+// RUN: -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) {
+}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits