llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Prabhu Rajasekaran (Prabhuk)

<details>
<summary>Changes</summary>

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


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


2 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+6-3) 
- (added) clang/test/CodeGen/call-graph-section-single-type-id.c (+21) 


``````````diff
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) {
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/230838
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to