Skip to content

[DebugInfo][DWARF] Add heapallocsite information #132073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/BinaryFormat/Dwarf.def
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ HANDLE_DW_AT(0x3e0a, LLVM_ptrauth_authentication_mode, 0, LLVM)
HANDLE_DW_AT(0x3e0b, LLVM_num_extra_inhabitants, 0, LLVM)
HANDLE_DW_AT(0x3e0c, LLVM_stmt_sequence, 0, LLVM)
HANDLE_DW_AT(0x3e0d, LLVM_coro_suspend_idx, 0, LLVM)
// The DWARF v6 working draft defines DW_AT_alloc_type; use this LLVM-private ID
// until that is released as an official standard.
HANDLE_DW_AT(0x3e0e, LLVM_alloc_type, 0, LLVM)

// Apple extensions.

Expand Down
15 changes: 8 additions & 7 deletions llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,12 +1292,10 @@ DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const {
}
}

DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
const DISubprogram *CalleeSP,
bool IsTail,
const MCSymbol *PCAddr,
const MCSymbol *CallAddr,
unsigned CallReg) {
DIE &DwarfCompileUnit::constructCallSiteEntryDIE(
DIE &ScopeDIE, const DISubprogram *CalleeSP, bool IsTail,
const MCSymbol *PCAddr, const MCSymbol *CallAddr, unsigned CallReg,
DIType *AllocSiteTy) {
// Insert a call site entry DIE within ScopeDIE.
DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site),
ScopeDIE, nullptr);
Expand All @@ -1306,7 +1304,7 @@ DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
// Indirect call.
addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target),
MachineLocation(CallReg));
} else {
} else if (CalleeSP) {
DIE *CalleeDIE = getOrCreateSubprogramDIE(CalleeSP);
assert(CalleeDIE && "Could not create DIE for call site entry origin");
if (AddLinkageNamesToDeclCallOriginsForTuning(DD) &&
Expand Down Expand Up @@ -1351,6 +1349,9 @@ DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
getDwarf5OrGNUAttr(dwarf::DW_AT_call_return_pc), PCAddr);
}

if (AllocSiteTy)
addType(CallSiteDIE, AllocSiteTy, dwarf::DW_AT_LLVM_alloc_type);

return CallSiteDIE;
}

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ class DwarfCompileUnit final : public DwarfUnit {
/// the \p CallReg is set to 0.
DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP,
bool IsTail, const MCSymbol *PCAddr,
const MCSymbol *CallAddr, unsigned CallReg);
const MCSymbol *CallAddr, unsigned CallReg,
DIType *AllocSiteTy);
/// Construct call site parameter DIEs for the \p CallSiteDIE. The \p Params
/// were collected by the \ref collectCallSiteParameters.
/// Note: The order of parameters does not matter, since debuggers recognize
Expand Down
22 changes: 12 additions & 10 deletions llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,8 @@ void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
if (MI.hasDelaySlot() && !delaySlotSupported(*&MI))
return;

DIType *AllocSiteTy = dyn_cast_or_null<DIType>(MI.getHeapAllocMarker());

// If this is a direct call, find the callee's subprogram.
// In the case of an indirect call find the register that holds
// the callee.
Expand All @@ -950,23 +952,23 @@ void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
PhysRegCalleeOperand =
PhysRegCalleeOperand && MCOI.OperandType == MCOI::OPERAND_REGISTER;
}
if (!CalleeOp.isGlobal() && !PhysRegCalleeOperand)
continue;

unsigned CallReg = 0;
const DISubprogram *CalleeSP = nullptr;
const Function *CalleeDecl = nullptr;
if (PhysRegCalleeOperand) {
CallReg = CalleeOp.getReg();
if (!CallReg)
continue;
} else {
CallReg = CalleeOp.getReg(); // might be zero
} else if (CalleeOp.isGlobal()) {
CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal());
if (!CalleeDecl || !CalleeDecl->getSubprogram())
continue;
CalleeSP = CalleeDecl->getSubprogram();
if (CalleeDecl)
CalleeSP = CalleeDecl->getSubprogram(); // might be nullptr
}

// Omit DIE if we can't tell where the call goes *and* we don't want to
// add metadata to it.
if (CalleeSP == nullptr && CallReg == 0 && AllocSiteTy == nullptr)
continue;

Comment on lines +967 to +971
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these conditions tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The four cases that could be tested here are basically:

  1. a call to a known target subprogram has a DW_TAG_call_site
  2. a call through a register has a DW_TAG_call_site
  3. a call with heapallocsite metadata has a DW_TAG_call_site
  4. an indirect call with complex addressing (such as a call [rax] on X86 in Intel syntax) has no DW_TAG_call_site

Case 1 is covered by tests like ./llvm/test/DebugInfo/X86/dwarf-callsite-related-attrs.ll, for example in function _Z3foov.
Case 2 is (bogusly) covered by the same file, function main (with the indirect_target call); pull request #151378 adds a proper test.
Case 3 is covered by the test I added.

Case 4 is a size optimization that only happens when LLVM gives up on emitting DWARF for complex calls. It is not currently tested, from what I can tell; pull request #151378 adds a proper test. Most ways that should end up in case 4 have been very broken at least on X86 for years. The main thing I noticed is that when LLVM is generating something like call [rax], it emits debuginfo claiming that the target address is stored in RAX because the only difference at the MIR level between call rax and call [rax] is which call opcode is used (CALL64r vs CALL64m), and the generic MIR code considers both of these to have a kRegister operand even though the X86 code treats them differently (OperandType::TYPE_R64 vs OperandType::TYPE_M). On top of that, another bug is that offsets are also ignored (#70949). Due to these bugs, we probably almost never actually hit this continue - the only way I managed to make that happen on x86 is by compiling this with -m32, to generate a call with unknown destination that has no registers involved:

static int (*const foo)() = (void*)0x123;
int bar() {
  return foo() + 1;
}

I think if we want to have nice tests for cases 2 and 4, it would make sense to fix the handling of call [rax] first, so that we can for now test that call rax has callsite info while call [rax] doesn't (until someone comes along and adds support for that, then I guess we'd have to come up with a new way to test it). (Or I guess alternatively one could make an argument that we should remove this branch entirely and accept that we're generating useless callsite tags sometimes.)

I have created PR #151378 to remove the broken DWARF for call [rax].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(And once that PR lands, I'll have to rebase this change.)

// TODO: Omit call site entries for runtime calls (objc_msgSend, etc).

bool IsTail = TII->isTailCall(MI);
Expand Down Expand Up @@ -1000,7 +1002,7 @@ void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
<< (IsTail ? " [IsTail]" : "") << "\n");

DIE &CallSiteDIE = CU.constructCallSiteEntryDIE(
ScopeDIE, CalleeSP, IsTail, PCAddr, CallAddr, CallReg);
ScopeDIE, CalleeSP, IsTail, PCAddr, CallAddr, CallReg, AllocSiteTy);

// Optionally emit call-site-param debug info.
if (emitDebugEntryValues()) {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ static bool isODRAttribute(uint16_t Attr) {
case dwarf::DW_AT_specification:
case dwarf::DW_AT_abstract_origin:
case dwarf::DW_AT_import:
case dwarf::DW_AT_LLVM_alloc_type:
return true;
}
llvm_unreachable("Improper attribute.");
Expand Down
34 changes: 34 additions & 0 deletions llvm/test/DebugInfo/X86/DW_AT_alloc_type.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; RUN: llc -O3 -o %t -filetype=obj %s
; RUN: llvm-dwarfdump %t | FileCheck %s

; based on clang++ output for `int *alloc_int() { return new int; }`


target triple = "x86_64-unknown-linux-gnu"

define dso_local ptr @alloc_int() !dbg !3 {
; CHECK: DW_TAG_subprogram
entry:
%call = call ptr @alloc(i64 noundef 4), !heapallocsite !7
; CHECK: DW_TAG_call_site
; CHECK: DW_AT_LLVM_alloc_type ([[ALLOCSITE:.*]])
ret ptr %call
}

; CHECK: {{.*}}[[ALLOCSITE]]: DW_TAG_base_type
; CHECK: DW_AT_name ("int")

declare dso_local ptr @alloc(i64 noundef)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2,!8}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, emissionKind: FullDebug)
!1 = !DIFile(filename: "a.cpp", directory: "/")
!2 = !{i32 2, !"Debug Info Version", i32 3}
!3 = distinct !DISubprogram(name: "alloc_int", scope: !1, file: !1, line: 1, type: !4, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition, unit: !0)
!4 = !DISubroutineType(types: !5)
!5 = !{!6}
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!8 = !{i32 2, !"Dwarf Version", i32 5}