[llvm] [AsmPrinter] Record direct calls through the GOT in the call graph section (PR #230730)

Prabhu Rajasekaran via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 10 10:44:27 PDT 2026


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

>From e628f9f86f9a2765a285b764595ce5e11d7d7944 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Sat, 10 Oct 2026 10:44:15 -0700
Subject: [PATCH] [AsmPrinter] Record direct calls through the GOT in the call
 graph section

Direct calls to callees that are reached through the GOT (`-fno-plt`, or
callees with the `nonlazybind` attribute, which rustc applies by default
on x86_64 Linux) are lowered to calls through a memory operand whose
displacement references the callee, e.g. `call *callee at GOTPCREL(%rip)`
on X86. The callee operand of such a call instruction is a register, so
`handleCallsiteForCallgraph` treated them as indirect calls and, since
they carry no `!callee_type` metadata, dropped them from the
`.llvm.callgraph` section altogether, losing the call graph edge.

A call through the GOT entry of a function can only reach that
function, so record such calls as direct calls to the referenced
function (or external symbol) instead. Tail calls through the GOT
(`jmp *callee at GOTPCREL(%rip)`) are handled the same way.

Assisted-by: Claude
---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    | 43 +++++++++------
 .../X86/call-graph-section-got-direct-call.ll | 52 +++++++++++++++++++
 2 files changed, 80 insertions(+), 15 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/call-graph-section-got-direct-call.ll

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index ffc351890a8b5..abd8e0d4cce66 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2001,27 +2001,40 @@ static StringRef getMIMnemonic(const MachineInstr &MI, MCStreamer &Streamer) {
   return Name;
 }
 
+/// Returns the operand that references the callee of \p MI if \p MI is a
+/// direct call, or null if \p MI is an indirect call.
+static const MachineOperand *getDirectCalleeOperand(const TargetInstrInfo &TII,
+                                                    const MachineInstr &MI) {
+  const MachineOperand &CalleeOperand = TII.getCalleeOperand(MI);
+  if (CalleeOperand.isGlobal() || CalleeOperand.isSymbol())
+    return &CalleeOperand;
+  // Direct calls to callees that are reached through the GOT (e.g., `-fno-plt`
+  // or `nonlazybind` callees) are lowered to calls through a memory operand
+  // whose displacement references the callee, e.g.,
+  // `call *callee at GOTPCREL(%rip)` on X86. A call through the GOT entry of a
+  // function can only reach that function, so treat such calls as direct calls.
+  if (!MI.mayLoad())
+    return nullptr;
+  for (const MachineOperand &MO : MI.operands()) {
+    if (MO.isSymbol() ||
+        (MO.isGlobal() && MO.getGlobal()->getValueType()->isFunctionTy()))
+      return &MO;
+  }
+  return nullptr;
+}
+
 void AsmPrinter::handleCallsiteForCallgraph(
     FunctionCallGraphInfo &FuncCGInfo,
     const MachineFunction::CallSiteInfoMap &CallSitesInfoMap,
     const MachineInstr &MI) {
   assert(MI.isCall() && "This method is meant for call instructions only.");
-  const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
-  const MachineOperand &CalleeOperand = TII->getCalleeOperand(MI);
-  if (CalleeOperand.isGlobal() || CalleeOperand.isSymbol()) {
+  const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo();
+  if (const MachineOperand *CalleeOperand = getDirectCalleeOperand(TII, MI)) {
     // 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.");
-    }
+    MCSymbol *CalleeSymbol =
+        CalleeOperand->isGlobal()
+            ? getSymbol(CalleeOperand->getGlobal())
+            : GetExternalSymbolSymbol(CalleeOperand->getSymbolName());
     FuncCGInfo.DirectCallees.insert(CalleeSymbol);
     return; // Early exit after handling the direct call instruction.
   }
diff --git a/llvm/test/CodeGen/X86/call-graph-section-got-direct-call.ll b/llvm/test/CodeGen/X86/call-graph-section-got-direct-call.ll
new file mode 100644
index 0000000000000..05bc882b93596
--- /dev/null
+++ b/llvm/test/CodeGen/X86/call-graph-section-got-direct-call.ll
@@ -0,0 +1,52 @@
+;; Tests that direct calls that go through the GOT (e.g. `nonlazybind` callees or
+;; `-fno-plt`) are recorded as direct callees in the .llvm.callgraph section
+;; instead of being dropped as indirect calls without type identifiers.
+
+; RUN: llc -mtriple=x86_64-unknown-linux -relocation-model=pic --call-graph-section -o - < %s | FileCheck %s
+
+declare !callgraph !0 void @callee() #0
+
+declare !callgraph !0 void @tail_callee() #0
+
+define void @caller() !callgraph !0 {
+entry:
+  call void @callee()
+  ret void
+}
+
+define void @tail_caller() !callgraph !0 {
+entry:
+  tail call void @tail_callee()
+  ret void
+}
+
+attributes #0 = { nonlazybind }
+
+!0 = !{!"_ZTSFvE"}
+
+; CHECK-LABEL: caller:
+; CHECK: callq *callee at GOTPCREL(%rip)
+; CHECK: .section .llvm.callgraph,"o", at llvm_call_graph,.text
+;; Version
+; CHECK-NEXT: .byte 0
+;; Flags -- potential indirect target with direct callees
+; CHECK-NEXT: .byte 3
+;; Function Entry PC
+; CHECK-NEXT: .quad caller
+;; Function type ID
+; CHECK-NEXT: .quad 6588678392271548388
+;; Number of unique direct callees
+; CHECK-NEXT: .byte 1
+;; Direct callee reached through the GOT
+; CHECK-NEXT: .quad callee
+
+; CHECK-LABEL: tail_caller:
+; CHECK: jmpq *tail_callee at GOTPCREL(%rip)
+; CHECK: .section .llvm.callgraph,"o", at llvm_call_graph,.text
+; CHECK-NEXT: .byte 0
+; CHECK-NEXT: .byte 3
+; CHECK-NEXT: .quad tail_caller
+; CHECK-NEXT: .quad 6588678392271548388
+; CHECK-NEXT: .byte 1
+;; Direct tail callee reached through the GOT
+; CHECK-NEXT: .quad tail_callee



More information about the llvm-commits mailing list