Skip to content

Commit 3da032e

Browse files
dzhidzhoevJaddyen
authored andcommitted
[DebugInfo] Update DebugInfoFinder to take retainedNodes into account (llvm#140285)
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 f327f7d commit 3da032e

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
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: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,14 @@ void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
241241
processType(T);
242242
else
243243
processSubprogram(cast<DISubprogram>(RT));
244-
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());
254-
}
244+
for (auto *Import : CU->getImportedEntities())
245+
processImportedEntity(Import);
255246
}
256247

257248
void DebugInfoFinder::processInstruction(const Module &M,
258249
const Instruction &I) {
259250
if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
260-
processVariable(M, DVI->getVariable());
251+
processVariable(DVI->getVariable());
261252

262253
if (auto DbgLoc = I.getDebugLoc())
263254
processLocation(M, DbgLoc.get());
@@ -275,7 +266,7 @@ void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
275266

276267
void DebugInfoFinder::processDbgRecord(const Module &M, const DbgRecord &DR) {
277268
if (const DbgVariableRecord *DVR = dyn_cast<const DbgVariableRecord>(&DR))
278-
processVariable(M, DVR->getVariable());
269+
processVariable(DVR->getVariable());
279270
processLocation(M, DR.getDebugLoc().get());
280271
}
281272

@@ -303,6 +294,18 @@ void DebugInfoFinder::processType(DIType *DT) {
303294
}
304295
}
305296

297+
void DebugInfoFinder::processImportedEntity(DIImportedEntity *Import) {
298+
auto *Entity = Import->getEntity();
299+
if (auto *T = dyn_cast<DIType>(Entity))
300+
processType(T);
301+
else if (auto *SP = dyn_cast<DISubprogram>(Entity))
302+
processSubprogram(SP);
303+
else if (auto *NS = dyn_cast<DINamespace>(Entity))
304+
processScope(NS->getScope());
305+
else if (auto *M = dyn_cast<DIModule>(Entity))
306+
processScope(M->getScope());
307+
}
308+
306309
void DebugInfoFinder::processScope(DIScope *Scope) {
307310
if (!Scope)
308311
return;
@@ -350,10 +353,16 @@ void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
350353
processType(TVal->getType());
351354
}
352355
}
356+
357+
for (auto *N : SP->getRetainedNodes()) {
358+
if (auto *Var = dyn_cast_or_null<DILocalVariable>(N))
359+
processVariable(Var);
360+
else if (auto *Import = dyn_cast_or_null<DIImportedEntity>(N))
361+
processImportedEntity(Import);
362+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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: Subprogram: imported from /somewhere/source.cpp:3
11+
; CHECK: Type: T from /somewhere/source.cpp:2 DW_TAG_structure_type
12+
13+
%struct.T = type { i32 }
14+
15+
; Function Attrs: mustprogress noinline nounwind optnone ssp uwtable(sync)
16+
define noundef i32 @_Z3foov() !dbg !7 {
17+
entry:
18+
ret i32 0
19+
}
20+
21+
!llvm.dbg.cu = !{!0}
22+
!llvm.module.flags = !{!2, !3, !4, !5}
23+
!llvm.ident = !{!6}
24+
25+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 21.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
26+
!1 = !DIFile(filename: "source.cpp", directory: "/somewhere")
27+
!2 = !{i32 7, !"Dwarf Version", i32 5}
28+
!3 = !{i32 2, !"Debug Info Version", i32 3}
29+
!4 = !{i32 1, !"wchar_size", i32 4}
30+
!5 = !{i32 8, !"PIC Level", i32 2}
31+
!6 = !{!"clang version 21.0.0git"}
32+
!7 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 1, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !8)
33+
!8 = !{!9, !15}
34+
!9 = !DILocalVariable(name: "v", scope: !7, file: !1, line: 8, type: !10)
35+
!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "T", scope: !7, file: !1, line: 2, size: 32, flags: DIFlagTypePassByValue, elements: !11)
36+
!11 = !{!12, !14}
37+
!12 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !10, file: !1, line: 3, baseType: !13, size: 32)
38+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
39+
!14 = !DISubprogram(name: "bar", scope: !10, file: !1, line: 5, scopeLine: 5, flags: DIFlagPrototyped | DIFlagStaticMember, spFlags: DISPFlagLocalToUnit)
40+
!15 = !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: !7, entity: !16, file: !1, line: 7)
41+
!16 = distinct !DISubprogram(name: "imported", scope: !17, file: !1, line: 3, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
42+
!17 = !DINamespace(name: "ns", scope: null)

0 commit comments

Comments
 (0)