[llvm] [AsmPrinter] Fix direct callee operand lookup in CallGraphSection (PR #218534)

Prabhu Rajasekaran via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 17:06:10 PDT 2026


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

>From 67c2aebee4e950e0cb80c4883d18cf782f34e238 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Mon, 24 Aug 2026 21:51:32 +0000
Subject: [PATCH 1/2] [AsmPrinter] Fix direct callee operand lookup in
 CallGraphSection

In handleCallsiteForCallgraph, direct callee operand was assumed to
always be at index 0 (MI.getOperand(0)). While true for x86_64, on
ARM Thumb/Thumb-2 (e.g. tBL), operand 0 and 1 represent predicate condition
codes and the target symbol is placed at operand 2.

Iterate over MI.operands() to locate the direct callee symbol operand
across all target instruction formats.

Assisted-by: Gemini
---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    | 33 ++++++++++---------
 .../ARM/call-graph-section-assembly.ll        |  1 +
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index cbd8f1932a285c3..dccff13ef94fc6b 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1999,23 +1999,24 @@ void AsmPrinter::handleCallsiteForCallgraph(
     const MachineFunction::CallSiteInfoMap &CallSitesInfoMap,
     const MachineInstr &MI) {
   assert(MI.isCall() && "This method is meant for call instructions only.");
-  const MachineOperand &CalleeOperand = MI.getOperand(0);
-  if (CalleeOperand.isGlobal() || CalleeOperand.isSymbol()) {
-    // Handle direct calls.
-    MCSymbol *CalleeSymbol = nullptr;
-    switch (CalleeOperand.getType()) {
-    case llvm::MachineOperand::MO_GlobalAddress:
-      CalleeSymbol = getSymbol(CalleeOperand.getGlobal());
-      break;
-    case llvm::MachineOperand::MO_ExternalSymbol:
-      CalleeSymbol = GetExternalSymbolSymbol(CalleeOperand.getSymbolName());
-      break;
-    default:
-      llvm_unreachable(
-          "Expected to only handle direct call instructions here.");
+  for (const MachineOperand &CalleeOperand : MI.operands()) {
+    if (CalleeOperand.isGlobal() || CalleeOperand.isSymbol()) {
+      // Handle direct calls.
+      MCSymbol *CalleeSymbol = nullptr;
+      switch (CalleeOperand.getType()) {
+      case llvm::MachineOperand::MO_GlobalAddress:
+        CalleeSymbol = getSymbol(CalleeOperand.getGlobal());
+        break;
+      case llvm::MachineOperand::MO_ExternalSymbol:
+        CalleeSymbol = GetExternalSymbolSymbol(CalleeOperand.getSymbolName());
+        break;
+      default:
+        llvm_unreachable(
+            "Expected to only handle direct call instructions here.");
+      }
+      FuncCGInfo.DirectCallees.insert(CalleeSymbol);
+      return; // Early exit after handling the direct call instruction.
     }
-    FuncCGInfo.DirectCallees.insert(CalleeSymbol);
-    return; // Early exit after handling the direct call instruction.
   }
   const auto &CallSiteInfo = CallSitesInfoMap.find(&MI);
   if (CallSiteInfo == CallSitesInfoMap.end())
diff --git a/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll b/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll
index 0292c08b040260b..280aee257b81cb4 100644
--- a/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll
+++ b/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll
@@ -5,6 +5,7 @@
 ;; Test if the .llvm.callgraph section contains unique direct callees.
 
 ; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -o - < %s | FileCheck %s
+; RUN: llc -mtriple=thumbv7m-unknown-none-eabi --call-graph-section -o - < %s | FileCheck %s
 
 declare !callgraph !0 void @direct_foo()
 declare !callgraph !1 i32 @direct_bar(i8)

>From d6ce5ef259d070aae6ebe26db08175bc6cff742c Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Tue, 25 Aug 2026 17:54:03 +0000
Subject: [PATCH 2/2] Use getCalleeOperand for identifying direct callees.

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    | 34 +++++++++----------
 .../ARM/call-graph-section-assembly.ll        |  1 +
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index dccff13ef94fc6b..9c01ee7fa0e3829 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1999,24 +1999,24 @@ void AsmPrinter::handleCallsiteForCallgraph(
     const MachineFunction::CallSiteInfoMap &CallSitesInfoMap,
     const MachineInstr &MI) {
   assert(MI.isCall() && "This method is meant for call instructions only.");
-  for (const MachineOperand &CalleeOperand : MI.operands()) {
-    if (CalleeOperand.isGlobal() || CalleeOperand.isSymbol()) {
-      // Handle direct calls.
-      MCSymbol *CalleeSymbol = nullptr;
-      switch (CalleeOperand.getType()) {
-      case llvm::MachineOperand::MO_GlobalAddress:
-        CalleeSymbol = getSymbol(CalleeOperand.getGlobal());
-        break;
-      case llvm::MachineOperand::MO_ExternalSymbol:
-        CalleeSymbol = GetExternalSymbolSymbol(CalleeOperand.getSymbolName());
-        break;
-      default:
-        llvm_unreachable(
-            "Expected to only handle direct call instructions here.");
-      }
-      FuncCGInfo.DirectCallees.insert(CalleeSymbol);
-      return; // Early exit after handling the direct call instruction.
+  const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
+  const MachineOperand &CalleeOperand = TII->getCalleeOperand(MI);
+  if (CalleeOperand.isGlobal() || CalleeOperand.isSymbol()) {
+    // Handle direct calls.
+    MCSymbol *CalleeSymbol = nullptr;
+    switch (CalleeOperand.getType()) {
+    case llvm::MachineOperand::MO_GlobalAddress:
+      CalleeSymbol = getSymbol(CalleeOperand.getGlobal());
+      break;
+    case llvm::MachineOperand::MO_ExternalSymbol:
+      CalleeSymbol = GetExternalSymbolSymbol(CalleeOperand.getSymbolName());
+      break;
+    default:
+      llvm_unreachable(
+          "Expected to only handle direct call instructions here.");
     }
+    FuncCGInfo.DirectCallees.insert(CalleeSymbol);
+    return; // Early exit after handling the direct call instruction.
   }
   const auto &CallSiteInfo = CallSitesInfoMap.find(&MI);
   if (CallSiteInfo == CallSitesInfoMap.end())
diff --git a/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll b/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll
index 280aee257b81cb4..b561f38716e27fd 100644
--- a/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll
+++ b/llvm/test/CodeGen/ARM/call-graph-section-assembly.ll
@@ -6,6 +6,7 @@
 
 ; RUN: llc -mtriple=arm-unknown-linux --call-graph-section -o - < %s | FileCheck %s
 ; RUN: llc -mtriple=thumbv7m-unknown-none-eabi --call-graph-section -o - < %s | FileCheck %s
+; RUN: llc -mtriple=thumbv6m-unknown-none-eabi --call-graph-section -o - < %s | FileCheck %s
 
 declare !callgraph !0 void @direct_foo()
 declare !callgraph !1 i32 @direct_bar(i8)



More information about the llvm-commits mailing list