Skip to content

Commit 2674248

Browse files
committed
[DebugInfo][DWARF] Add heapallocsite information
LLVM currently stores heapallocsite information in CodeView debuginfo, but not in DWARF debuginfo. heapallocsite information is useful for profiling and memory analysis; plumb it into DWARF as an LLVM-specific extension.
1 parent 5475834 commit 2674248

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ HANDLE_DW_AT(0x3e09, LLVM_ptrauth_authenticates_null_values, 0, LLVM)
624624
HANDLE_DW_AT(0x3e0a, LLVM_ptrauth_authentication_mode, 0, LLVM)
625625
HANDLE_DW_AT(0x3e0b, LLVM_num_extra_inhabitants, 0, LLVM)
626626
HANDLE_DW_AT(0x3e0c, LLVM_stmt_sequence, 0, LLVM)
627+
HANDLE_DW_AT(0x3e0d, LLVM_heapallocsite, 0, LLVM)
627628

628629
// Apple extensions.
629630

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,12 +1289,10 @@ DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const {
12891289
}
12901290
}
12911291

1292-
DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
1293-
const DISubprogram *CalleeSP,
1294-
bool IsTail,
1295-
const MCSymbol *PCAddr,
1296-
const MCSymbol *CallAddr,
1297-
unsigned CallReg) {
1292+
DIE &DwarfCompileUnit::constructCallSiteEntryDIE(
1293+
DIE &ScopeDIE, const DISubprogram *CalleeSP, bool IsTail,
1294+
const MCSymbol *PCAddr, const MCSymbol *CallAddr, unsigned CallReg,
1295+
DIType *AllocSiteTy) {
12981296
// Insert a call site entry DIE within ScopeDIE.
12991297
DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site),
13001298
ScopeDIE, nullptr);
@@ -1303,7 +1301,7 @@ DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
13031301
// Indirect call.
13041302
addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target),
13051303
MachineLocation(CallReg));
1306-
} else {
1304+
} else if (CalleeSP) {
13071305
DIE *CalleeDIE = getOrCreateSubprogramDIE(CalleeSP);
13081306
assert(CalleeDIE && "Could not create DIE for call site entry origin");
13091307
if (AddLinkageNamesToDeclCallOriginsForTuning(DD) &&
@@ -1348,6 +1346,10 @@ DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE,
13481346
getDwarf5OrGNUAttr(dwarf::DW_AT_call_return_pc), PCAddr);
13491347
}
13501348

1349+
if (AllocSiteTy) {
1350+
addType(CallSiteDIE, AllocSiteTy, dwarf::DW_AT_LLVM_heapallocsite);
1351+
}
1352+
13511353
return CallSiteDIE;
13521354
}
13531355

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ class DwarfCompileUnit final : public DwarfUnit {
285285
/// the \p CallReg is set to 0.
286286
DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP,
287287
bool IsTail, const MCSymbol *PCAddr,
288-
const MCSymbol *CallAddr, unsigned CallReg);
288+
const MCSymbol *CallAddr, unsigned CallReg,
289+
DIType *AllocSiteTy);
289290
/// Construct call site parameter DIEs for the \p CallSiteDIE. The \p Params
290291
/// were collected by the \ref collectCallSiteParameters.
291292
/// Note: The order of parameters does not matter, since debuggers recognize

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -930,28 +930,29 @@ void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
930930
if (MI.hasDelaySlot() && !delaySlotSupported(*&MI))
931931
return;
932932

933+
DIType *AllocSiteTy = dyn_cast_or_null<DIType>(MI.getHeapAllocMarker());
934+
933935
// If this is a direct call, find the callee's subprogram.
934936
// In the case of an indirect call find the register that holds
935937
// the callee.
936938
const MachineOperand &CalleeOp = TII->getCalleeOperand(MI);
937-
if (!CalleeOp.isGlobal() &&
938-
(!CalleeOp.isReg() || !CalleeOp.getReg().isPhysical()))
939-
continue;
940939

941940
unsigned CallReg = 0;
942941
const DISubprogram *CalleeSP = nullptr;
943942
const Function *CalleeDecl = nullptr;
944-
if (CalleeOp.isReg()) {
945-
CallReg = CalleeOp.getReg();
946-
if (!CallReg)
947-
continue;
948-
} else {
943+
if (CalleeOp.isReg() && CalleeOp.getReg().isPhysical()) {
944+
CallReg = CalleeOp.getReg(); // might be zero
945+
} else if (CalleeOp.isGlobal()) {
949946
CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal());
950-
if (!CalleeDecl || !CalleeDecl->getSubprogram())
951-
continue;
952-
CalleeSP = CalleeDecl->getSubprogram();
947+
if (CalleeDecl)
948+
CalleeSP = CalleeDecl->getSubprogram(); // might be nullptr
953949
}
954950

951+
// Omit DIE if we can't tell where the call goes *and* we don't want to
952+
// add metadata to it.
953+
if (CalleeSP == nullptr && CallReg == 0 && AllocSiteTy == nullptr)
954+
continue;
955+
955956
// TODO: Omit call site entries for runtime calls (objc_msgSend, etc).
956957

957958
bool IsTail = TII->isTailCall(MI);
@@ -986,7 +987,7 @@ void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
986987
<< (IsTail ? " [IsTail]" : "") << "\n");
987988

988989
DIE &CallSiteDIE = CU.constructCallSiteEntryDIE(
989-
ScopeDIE, CalleeSP, IsTail, PCAddr, CallAddr, CallReg);
990+
ScopeDIE, CalleeSP, IsTail, PCAddr, CallAddr, CallReg, AllocSiteTy);
990991

991992
// Optionally emit call-site-param debug info.
992993
if (emitDebugEntryValues()) {

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static bool isODRAttribute(uint16_t Attr) {
109109
case dwarf::DW_AT_specification:
110110
case dwarf::DW_AT_abstract_origin:
111111
case dwarf::DW_AT_import:
112+
case dwarf::DW_AT_LLVM_heapallocsite:
112113
return true;
113114
}
114115
llvm_unreachable("Improper attribute.");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: llc -O3 -o %t -filetype=obj %s
2+
; RUN: llvm-dwarfdump %t | FileCheck %s
3+
4+
; based on clang++ output for `int *alloc_int() { return new int; }`
5+
6+
7+
target triple = "x86_64-unknown-linux-gnu"
8+
9+
define dso_local ptr @alloc_int() !dbg !3 {
10+
; CHECK: DW_TAG_subprogram
11+
entry:
12+
%call = call ptr @alloc(i64 noundef 4), !heapallocsite !7
13+
; CHECK: DW_TAG_GNU_call_site
14+
; CHECK: DW_AT_LLVM_heapallocsite ([[ALLOCSITE:.*]])
15+
ret ptr %call
16+
}
17+
18+
; CHECK: {{.*}}[[ALLOCSITE]]: DW_TAG_base_type
19+
; CHECK: DW_AT_name ("int")
20+
21+
declare dso_local ptr @alloc(i64 noundef)
22+
23+
!llvm.dbg.cu = !{!0}
24+
!llvm.module.flags = !{!2}
25+
26+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, emissionKind: FullDebug)
27+
!1 = !DIFile(filename: "a.cpp", directory: "/")
28+
!2 = !{i32 2, !"Debug Info Version", i32 3}
29+
!3 = distinct !DISubprogram(name: "alloc_int", scope: !1, file: !1, line: 1, type: !4, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition, unit: !0)
30+
!4 = !DISubroutineType(types: !5)
31+
!5 = !{!6}
32+
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
33+
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)

0 commit comments

Comments
 (0)