[llvm-branch-commits] [llvm] [llvm][AsmPrinter] Emit call graph section (PR #87576)

Prabhu Rajasekaran via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Apr 18 19:18:37 PDT 2025


https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/87576

>From 6b67376bd5e1f21606017c83cc67f2186ba36a33 Mon Sep 17 00:00:00 2001
From: Necip Fazil Yildiran <necip at google.com>
Date: Thu, 13 Mar 2025 01:41:04 +0000
Subject: [PATCH 1/4] Updated the test as reviewers suggested.

Created using spr 1.3.6-beta.1
---
 llvm/test/CodeGen/X86/call-graph-section.ll | 66 +++++++++++++++++++
 llvm/test/CodeGen/call-graph-section.ll     | 73 ---------------------
 2 files changed, 66 insertions(+), 73 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/call-graph-section.ll
 delete mode 100644 llvm/test/CodeGen/call-graph-section.ll

diff --git a/llvm/test/CodeGen/X86/call-graph-section.ll b/llvm/test/CodeGen/X86/call-graph-section.ll
new file mode 100644
index 0000000000000..a77a2b8051ed3
--- /dev/null
+++ b/llvm/test/CodeGen/X86/call-graph-section.ll
@@ -0,0 +1,66 @@
+;; Tests that we store the type identifiers in .callgraph section of the binary.
+
+; RUN: llc --call-graph-section -filetype=obj -o - < %s | \
+; RUN: llvm-readelf -x .callgraph - | FileCheck %s
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local void @foo() #0 !type !4 {
+entry:
+  ret void
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local i32 @bar(i8 signext %a) #0 !type !5 {
+entry:
+  %a.addr = alloca i8, align 1
+  store i8 %a, ptr %a.addr, align 1
+  ret i32 0
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local ptr @baz(ptr %a) #0 !type !6 {
+entry:
+  %a.addr = alloca ptr, align 8
+  store ptr %a, ptr %a.addr, align 8
+  ret ptr null
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local void @main() #0 !type !7 {
+entry:
+  %retval = alloca i32, align 4
+  %fp_foo = alloca ptr, align 8
+  %a = alloca i8, align 1
+  %fp_bar = alloca ptr, align 8
+  %fp_baz = alloca ptr, align 8
+  store i32 0, ptr %retval, align 4
+  store ptr @foo, ptr %fp_foo, align 8
+  %0 = load ptr, ptr %fp_foo, align 8
+  call void (...) %0() [ "callee_type"(metadata !"_ZTSFvE.generalized") ]
+  store ptr @bar, ptr %fp_bar, align 8
+  %1 = load ptr, ptr %fp_bar, align 8
+  %2 = load i8, ptr %a, align 1
+  %call = call i32 %1(i8 signext %2) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
+  store ptr @baz, ptr %fp_baz, align 8
+  %3 = load ptr, ptr %fp_baz, align 8
+  %call1 = call ptr %3(ptr %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
+  call void @foo() [ "callee_type"(metadata !"_ZTSFvE.generalized") ]
+  %4 = load i8, ptr %a, align 1
+  %call2 = call i32 @bar(i8 signext %4) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
+  %call3 = call ptr @baz(ptr %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
+  ret void
+}
+
+;; Check that the numeric type id (md5 hash) for the below type ids are emitted
+;; to the callgraph section.
+
+; CHECK: Hex dump of section '.callgraph':
+
+; CHECK-DAG: 2444f731 f5eecb3e
+!4 = !{i64 0, !"_ZTSFvE.generalized"}
+; CHECK-DAG: 5486bc59 814b8e30
+!5 = !{i64 0, !"_ZTSFicE.generalized"}
+; CHECK-DAG: 7ade6814 f897fd77
+!6 = !{i64 0, !"_ZTSFPvS_E.generalized"}
+; CHECK-DAG: caaf769a 600968fa
+!7 = !{i64 0, !"_ZTSFiE.generalized"}
diff --git a/llvm/test/CodeGen/call-graph-section.ll b/llvm/test/CodeGen/call-graph-section.ll
deleted file mode 100644
index bb158d11e82c9..0000000000000
--- a/llvm/test/CodeGen/call-graph-section.ll
+++ /dev/null
@@ -1,73 +0,0 @@
-; Tests that we store the type identifiers in .callgraph section of the binary.
-
-; RUN: llc --call-graph-section -filetype=obj -o - < %s | \
-; RUN: llvm-readelf -x .callgraph - | FileCheck %s
-
-target triple = "x86_64-unknown-linux-gnu"
-
-define dso_local void @foo() #0 !type !4 {
-entry:
-  ret void
-}
-
-define dso_local i32 @bar(i8 signext %a) #0 !type !5 {
-entry:
-  %a.addr = alloca i8, align 1
-  store i8 %a, i8* %a.addr, align 1
-  ret i32 0
-}
-
-define dso_local i32* @baz(i8* %a) #0 !type !6 {
-entry:
-  %a.addr = alloca i8*, align 8
-  store i8* %a, i8** %a.addr, align 8
-  ret i32* null
-}
-
-define dso_local i32 @main() #0 !type !7 {
-entry:
-  %retval = alloca i32, align 4
-  %fp_foo = alloca void (...)*, align 8
-  %a = alloca i8, align 1
-  %fp_bar = alloca i32 (i8)*, align 8
-  %fp_baz = alloca i32* (i8*)*, align 8
-  store i32 0, i32* %retval, align 4
-  store void (...)* bitcast (void ()* @foo to void (...)*), void (...)** %fp_foo, align 8
-  %0 = load void (...)*, void (...)** %fp_foo, align 8
-  call void (...) %0() [ "callee_type"(metadata !"_ZTSFvE.generalized") ]
-  store i32 (i8)* @bar, i32 (i8)** %fp_bar, align 8
-  %1 = load i32 (i8)*, i32 (i8)** %fp_bar, align 8
-  %2 = load i8, i8* %a, align 1
-  %call = call i32 %1(i8 signext %2) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
-  store i32* (i8*)* @baz, i32* (i8*)** %fp_baz, align 8
-  %3 = load i32* (i8*)*, i32* (i8*)** %fp_baz, align 8
-  %call1 = call i32* %3(i8* %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
-  call void @foo() [ "callee_type"(metadata !"_ZTSFvE.generalized") ]
-  %4 = load i8, i8* %a, align 1
-  %call2 = call i32 @bar(i8 signext %4) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
-  %call3 = call i32* @baz(i8* %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
-  ret i32 0
-}
-
-attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
-
-!llvm.module.flags = !{!0, !1, !2}
-!llvm.ident = !{!3}
-
-; Check that the numeric type id (md5 hash) for the below type ids are emitted
-; to the callgraph section.
-
-; CHECK: Hex dump of section '.callgraph':
-
-!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 7, !"uwtable", i32 1}
-!2 = !{i32 7, !"frame-pointer", i32 2}
-!3 = !{!"clang version 13.0.0 (git at github.com:llvm/llvm-project.git 6d35f403b91c2f2c604e23763f699d580370ca96)"}
-; CHECK-DAG: 2444f731 f5eecb3e
-!4 = !{i64 0, !"_ZTSFvE.generalized"}
-; CHECK-DAG: 5486bc59 814b8e30
-!5 = !{i64 0, !"_ZTSFicE.generalized"}
-; CHECK-DAG: 7ade6814 f897fd77
-!6 = !{i64 0, !"_ZTSFPvS_E.generalized"}
-; CHECK-DAG: caaf769a 600968fa
-!7 = !{i64 0, !"_ZTSFiE.generalized"}

>From d2bb381918c3d2c196d7a27f7572b0607bbdbb70 Mon Sep 17 00:00:00 2001
From: Necip Fazil Yildiran <necip at google.com>
Date: Thu, 13 Mar 2025 02:08:32 +0000
Subject: [PATCH 2/4] Scoped enum. Simplify test.

Created using spr 1.3.6-beta.1
---
 llvm/include/llvm/CodeGen/AsmPrinter.h      |  2 +-
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp  | 11 ++++++-----
 llvm/test/CodeGen/X86/call-graph-section.ll | 22 +++------------------
 3 files changed, 10 insertions(+), 25 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h
index 1ad6d6cd18f9a..f9bf50adef5a7 100644
--- a/llvm/include/llvm/CodeGen/AsmPrinter.h
+++ b/llvm/include/llvm/CodeGen/AsmPrinter.h
@@ -184,7 +184,7 @@ class AsmPrinter : public MachineFunctionPass {
     /// Enumeration of function kinds, and their mapping to function kind values
     /// stored in call graph section entries.
     /// Must match the enum in llvm/tools/llvm-objdump/llvm-objdump.cpp.
-    enum FunctionKind {
+    enum class FunctionKind : uint64_t {
       /// Function cannot be target to indirect calls.
       NOT_INDIRECT_TARGET = 0,
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index a1b86ee368927..d86962668a056 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1706,16 +1706,17 @@ void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
   // Can be optimized to occupy 2 bits instead.
   // Emit function kind, and type id if available.
   if (!IsIndirectTarget) {
-    OutStreamer->emitInt64(FunctionInfo::FunctionKind::NOT_INDIRECT_TARGET);
+    OutStreamer->emitInt64(
+        static_cast<uint64_t>(FunctionInfo::FunctionKind::NOT_INDIRECT_TARGET));
   } else {
     const auto *TypeId = extractNumericCGTypeId(F);
     if (TypeId) {
-      OutStreamer->emitInt64(
-          FunctionInfo::FunctionKind::INDIRECT_TARGET_KNOWN_TID);
+      OutStreamer->emitInt64(static_cast<uint64_t>(
+          FunctionInfo::FunctionKind::INDIRECT_TARGET_KNOWN_TID));
       OutStreamer->emitInt64(TypeId->getZExtValue());
     } else {
-      OutStreamer->emitInt64(
-          FunctionInfo::FunctionKind::INDIRECT_TARGET_UNKNOWN_TID);
+      OutStreamer->emitInt64(static_cast<uint64_t>(
+          FunctionInfo::FunctionKind::INDIRECT_TARGET_UNKNOWN_TID));
     }
   }
 
diff --git a/llvm/test/CodeGen/X86/call-graph-section.ll b/llvm/test/CodeGen/X86/call-graph-section.ll
index a77a2b8051ed3..d0e6e1acb0c06 100644
--- a/llvm/test/CodeGen/X86/call-graph-section.ll
+++ b/llvm/test/CodeGen/X86/call-graph-section.ll
@@ -3,27 +3,11 @@
 ; RUN: llc --call-graph-section -filetype=obj -o - < %s | \
 ; RUN: llvm-readelf -x .callgraph - | FileCheck %s
 
-; Function Attrs: noinline nounwind optnone uwtable
-define dso_local void @foo() #0 !type !4 {
-entry:
-  ret void
-}
+declare !type !4 void @foo() #0
 
-; Function Attrs: noinline nounwind optnone uwtable
-define dso_local i32 @bar(i8 signext %a) #0 !type !5 {
-entry:
-  %a.addr = alloca i8, align 1
-  store i8 %a, ptr %a.addr, align 1
-  ret i32 0
-}
+declare !type !5 noundef i32 @bar(i8 signext %a) #0
 
-; Function Attrs: noinline nounwind optnone uwtable
-define dso_local ptr @baz(ptr %a) #0 !type !6 {
-entry:
-  %a.addr = alloca ptr, align 8
-  store ptr %a, ptr %a.addr, align 8
-  ret ptr null
-}
+declare !type !6 noundef ptr @baz(ptr %a) #0
 
 ; Function Attrs: noinline nounwind optnone uwtable
 define dso_local void @main() #0 !type !7 {

>From e3c95b5a08f82a8d8ab548a579756b45946d76e0 Mon Sep 17 00:00:00 2001
From: Necip Fazil Yildiran <necip at google.com>
Date: Thu, 13 Mar 2025 02:20:27 +0000
Subject: [PATCH 3/4] Remove unnecessary cast.

Created using spr 1.3.6-beta.1
---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index d86962668a056..8da914385b0d7 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1662,8 +1662,8 @@ static ConstantInt *extractNumericCGTypeId(const Function &F) {
     return nullptr;
 
   uint64_t TypeIdVal = llvm::MD5Hash(MDGeneralizedTypeId->getString());
-  Type *Int64Ty = Type::getInt64Ty(F.getContext());
-  return cast<ConstantInt>(ConstantInt::get(Int64Ty, TypeIdVal));
+  IntegerType *Int64Ty = Type::getInt64Ty(F.getContext());
+  return ConstantInt::get(Int64Ty, TypeIdVal);
 }
 
 /// Emits call graph section.

>From 678008ee3f8be81000c5f3e9b087fb8006e6421d Mon Sep 17 00:00:00 2001
From: Necip Fazil Yildiran <necip at google.com>
Date: Thu, 13 Mar 2025 18:24:53 +0000
Subject: [PATCH 4/4] Reorder IR metadata and rename temporary var names in
 test.

Created using spr 1.3.6-beta.1
---
 llvm/test/CodeGen/X86/call-graph-section.ll | 37 ++++++++++-----------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/llvm/test/CodeGen/X86/call-graph-section.ll b/llvm/test/CodeGen/X86/call-graph-section.ll
index d0e6e1acb0c06..3c295a6d262ff 100644
--- a/llvm/test/CodeGen/X86/call-graph-section.ll
+++ b/llvm/test/CodeGen/X86/call-graph-section.ll
@@ -3,14 +3,13 @@
 ; RUN: llc --call-graph-section -filetype=obj -o - < %s | \
 ; RUN: llvm-readelf -x .callgraph - | FileCheck %s
 
-declare !type !4 void @foo() #0
+declare !type !0 void @foo()
 
-declare !type !5 noundef i32 @bar(i8 signext %a) #0
+declare !type !1 noundef i32 @bar(i8 signext)
 
-declare !type !6 noundef ptr @baz(ptr %a) #0
+declare !type !2 noundef ptr @baz(ptr)
 
-; Function Attrs: noinline nounwind optnone uwtable
-define dso_local void @main() #0 !type !7 {
+define dso_local void @main() !type !3 {
 entry:
   %retval = alloca i32, align 4
   %fp_foo = alloca ptr, align 8
@@ -19,19 +18,19 @@ entry:
   %fp_baz = alloca ptr, align 8
   store i32 0, ptr %retval, align 4
   store ptr @foo, ptr %fp_foo, align 8
-  %0 = load ptr, ptr %fp_foo, align 8
-  call void (...) %0() [ "callee_type"(metadata !"_ZTSFvE.generalized") ]
+  %fp_foo_val = load ptr, ptr %fp_foo, align 8
+  call void (...) %fp_foo_val() [ "callee_type"(metadata !"_ZTSFvE.generalized") ]
   store ptr @bar, ptr %fp_bar, align 8
-  %1 = load ptr, ptr %fp_bar, align 8
-  %2 = load i8, ptr %a, align 1
-  %call = call i32 %1(i8 signext %2) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
+  %fp_bar_val = load ptr, ptr %fp_bar, align 8
+  %a_val = load i8, ptr %a, align 1
+  %call_fp_bar = call i32 %fp_bar_val(i8 signext %a_val) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
   store ptr @baz, ptr %fp_baz, align 8
-  %3 = load ptr, ptr %fp_baz, align 8
-  %call1 = call ptr %3(ptr %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
+  %fp_baz_val = load ptr, ptr %fp_baz, align 8
+  %call_fp_baz = call ptr %fp_baz_val(ptr %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
   call void @foo() [ "callee_type"(metadata !"_ZTSFvE.generalized") ]
-  %4 = load i8, ptr %a, align 1
-  %call2 = call i32 @bar(i8 signext %4) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
-  %call3 = call ptr @baz(ptr %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
+  %a_val_2 = load i8, ptr %a, align 1
+  %call_bar = call i32 @bar(i8 signext %a_val_2) [ "callee_type"(metadata !"_ZTSFicE.generalized") ]
+  %call_baz = call ptr @baz(ptr %a) [ "callee_type"(metadata !"_ZTSFPvS_E.generalized") ]
   ret void
 }
 
@@ -41,10 +40,10 @@ entry:
 ; CHECK: Hex dump of section '.callgraph':
 
 ; CHECK-DAG: 2444f731 f5eecb3e
-!4 = !{i64 0, !"_ZTSFvE.generalized"}
+!0 = !{i64 0, !"_ZTSFvE.generalized"}
 ; CHECK-DAG: 5486bc59 814b8e30
-!5 = !{i64 0, !"_ZTSFicE.generalized"}
+!1 = !{i64 0, !"_ZTSFicE.generalized"}
 ; CHECK-DAG: 7ade6814 f897fd77
-!6 = !{i64 0, !"_ZTSFPvS_E.generalized"}
+!2 = !{i64 0, !"_ZTSFPvS_E.generalized"}
 ; CHECK-DAG: caaf769a 600968fa
-!7 = !{i64 0, !"_ZTSFiE.generalized"}
+!3 = !{i64 0, !"_ZTSFiE.generalized"}



More information about the llvm-branch-commits mailing list