Skip to content

Commit dadd5eb

Browse files
committed
[DebugInfo] Update DebugInfoFinder to take retainedNodes into account
Since https://reviews.llvm.org/D144004, DISubprogram's retainedNodes field is used to track DIImportedEntities, in addition to local variables and labels. However, the corresponding update for DebugInfoFinder, to make it visit DISubprogram's retainedNodes, was missing. This is the fix for it. This change is separated from llvm#119001 to simplify it.
1 parent a07cc18 commit dadd5eb

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

llvm/include/llvm/IR/DebugInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class DebugInfoFinder {
110110
void processInstruction(const Module &M, const Instruction &I);
111111

112112
/// Process a DILocalVariable.
113-
void processVariable(const Module &M, const DILocalVariable *DVI);
113+
void processVariable(DILocalVariable *DVI);
114114
/// Process debug info location.
115115
void processLocation(const Module &M, const DILocation *Loc);
116116
/// Process a DbgRecord (e.g, treat a DbgVariableRecord like a
@@ -127,6 +127,7 @@ class DebugInfoFinder {
127127
void processCompileUnit(DICompileUnit *CU);
128128
void processScope(DIScope *Scope);
129129
void processType(DIType *DT);
130+
void processImportedEntity(DIImportedEntity *Import);
130131
bool addCompileUnit(DICompileUnit *CU);
131132
bool addGlobalVariable(DIGlobalVariableExpression *DIG);
132133
bool addScope(DIScope *Scope);

llvm/lib/IR/DebugInfo.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,14 @@ void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
242242
else
243243
processSubprogram(cast<DISubprogram>(RT));
244244
for (auto *Import : CU->getImportedEntities()) {
245-
auto *Entity = Import->getEntity();
246-
if (auto *T = dyn_cast<DIType>(Entity))
247-
processType(T);
248-
else if (auto *SP = dyn_cast<DISubprogram>(Entity))
249-
processSubprogram(SP);
250-
else if (auto *NS = dyn_cast<DINamespace>(Entity))
251-
processScope(NS->getScope());
252-
else if (auto *M = dyn_cast<DIModule>(Entity))
253-
processScope(M->getScope());
245+
processImportedEntity(Import);
254246
}
255247
}
256248

257249
void DebugInfoFinder::processInstruction(const Module &M,
258250
const Instruction &I) {
259251
if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
260-
processVariable(M, DVI->getVariable());
252+
processVariable(DVI->getVariable());
261253

262254
if (auto DbgLoc = I.getDebugLoc())
263255
processLocation(M, DbgLoc.get());
@@ -275,7 +267,7 @@ void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
275267

276268
void DebugInfoFinder::processDbgRecord(const Module &M, const DbgRecord &DR) {
277269
if (const DbgVariableRecord *DVR = dyn_cast<const DbgVariableRecord>(&DR))
278-
processVariable(M, DVR->getVariable());
270+
processVariable(DVR->getVariable());
279271
processLocation(M, DR.getDebugLoc().get());
280272
}
281273

@@ -303,6 +295,18 @@ void DebugInfoFinder::processType(DIType *DT) {
303295
}
304296
}
305297

298+
void DebugInfoFinder::processImportedEntity(DIImportedEntity *Import) {
299+
auto *Entity = Import->getEntity();
300+
if (auto *T = dyn_cast<DIType>(Entity))
301+
processType(T);
302+
else if (auto *SP = dyn_cast<DISubprogram>(Entity))
303+
processSubprogram(SP);
304+
else if (auto *NS = dyn_cast<DINamespace>(Entity))
305+
processScope(NS->getScope());
306+
else if (auto *M = dyn_cast<DIModule>(Entity))
307+
processScope(M->getScope());
308+
}
309+
306310
void DebugInfoFinder::processScope(DIScope *Scope) {
307311
if (!Scope)
308312
return;
@@ -350,10 +354,15 @@ void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
350354
processType(TVal->getType());
351355
}
352356
}
357+
358+
for (auto *N : SP->getRetainedNodes())
359+
if (auto *Var = dyn_cast_or_null<DILocalVariable>(N))
360+
processVariable(Var);
361+
else if (auto *Import = dyn_cast_or_null<DIImportedEntity>(N))
362+
processImportedEntity(Import);
353363
}
354364

355-
void DebugInfoFinder::processVariable(const Module &M,
356-
const DILocalVariable *DV) {
365+
void DebugInfoFinder::processVariable(DILocalVariable *DV) {
357366
if (!NodesSeen.insert(DV).second)
358367
return;
359368
processScope(DV->getScope());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
; RUN: opt -passes='print<module-debuginfo>' -disable-output 2>&1 < %s \
2+
; RUN: | FileCheck %s
3+
4+
; This is to track DebugInfoFinder's ability to find the debug info metadata,
5+
; in particular, properly visit DISubprogram's retainedNodes.
6+
7+
; CHECK: Compile unit: DW_LANG_C_plus_plus from /somewhere/source.cpp
8+
; CHECK: Subprogram: foo from /somewhere/source.cpp:1 ('_Z3foov')
9+
; CHECK: Subprogram: bar from /somewhere/source.cpp:5
10+
; CHECK: Type: T from /somewhere/source.cpp:2 DW_TAG_structure_type
11+
12+
%struct.T = type { i32 }
13+
14+
; Function Attrs: mustprogress noinline nounwind optnone ssp uwtable(sync)
15+
define noundef i32 @_Z3foov() !dbg !7 {
16+
entry:
17+
ret i32 0
18+
}
19+
20+
!llvm.dbg.cu = !{!0}
21+
!llvm.module.flags = !{!2, !3, !4, !5}
22+
!llvm.ident = !{!6}
23+
24+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 21.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
25+
!1 = !DIFile(filename: "source.cpp", directory: "/somewhere")
26+
!2 = !{i32 7, !"Dwarf Version", i32 5}
27+
!3 = !{i32 2, !"Debug Info Version", i32 3}
28+
!4 = !{i32 1, !"wchar_size", i32 4}
29+
!5 = !{i32 8, !"PIC Level", i32 2}
30+
!6 = !{!"clang version 21.0.0git"}
31+
!7 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 1, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !8)
32+
!8 = !{!9}
33+
!9 = !DILocalVariable(name: "v", scope: !7, file: !1, line: 8, type: !10)
34+
!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "T", scope: !7, file: !1, line: 2, size: 32, flags: DIFlagTypePassByValue, elements: !11)
35+
!11 = !{!12, !14}
36+
!12 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !10, file: !1, line: 3, baseType: !13, size: 32)
37+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
38+
!14 = !DISubprogram(name: "bar", scope: !10, file: !1, line: 5, scopeLine: 5, flags: DIFlagPrototyped | DIFlagStaticMember, spFlags: DISPFlagLocalToUnit)

0 commit comments

Comments
 (0)