Skip to content

Commit c35cab6

Browse files
bjopemahesh-attarde
authored andcommitted
[DebugInfo] Handle followup loop metadata in updateLoopMetadataDebugLocations (llvm#157557)
Inliner/IROutliner/CodeExtractor all uses the updateLoopMetadataDebugLocations helper in order to modify debug location related to loop metadata. However, the helper has only been updating DILocation nodes found as operands to the first level of the MD_loop metadata. There could however be more DILocations as part of the various kinds of followup metadata. A typical example would be llvm.loop metadata like this !6 = distinct !{!6, !7, !8, !9, !10, !11} !7 = !DILocation(line: 6, column: 3, scope: !3) !8 = !DILocation(line: 7, column: 22, scope: !3) !11 = !{!"llvm.loop.distribute.followup_all", !7, !8, ..., !14} !14 = !{!"llvm.loop.vectorize.followup_all", !7, !8, ...} Instead of just updating !7 and !8 in !6, this patch make sure that we now recursively update the DILocations in !11 and !14 as well. Fixes llvm#141568
1 parent f4cf7ec commit c35cab6

File tree

2 files changed

+118
-3
lines changed

2 files changed

+118
-3
lines changed

llvm/lib/IR/DebugInfo.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,38 @@ bool DebugInfoFinder::addScope(DIScope *Scope) {
375375
return true;
376376
}
377377

378+
/// Recursively handle DILocations in followup metadata etc.
379+
///
380+
/// TODO: If for example a followup loop metadata would refence itself this
381+
/// function would go into infinite recursion. We do not expect such cycles in
382+
/// the loop metadata (except for the self-referencing first element
383+
/// "LoopID"). However, we could at least handle such situations more gracefully
384+
/// somehow (e.g. by keeping track of visited nodes and dropping metadata).
385+
static Metadata *updateLoopMetadataDebugLocationsRecursive(
386+
Metadata *MetadataIn, function_ref<Metadata *(Metadata *)> Updater) {
387+
const MDTuple *M = dyn_cast_or_null<MDTuple>(MetadataIn);
388+
// The loop metadata options should start with a MDString.
389+
if (!M || M->getNumOperands() < 1 || !isa<MDString>(M->getOperand(0)))
390+
return MetadataIn;
391+
392+
bool Updated = false;
393+
SmallVector<Metadata *, 4> MDs{M->getOperand(0)};
394+
for (Metadata *MD : llvm::drop_begin(M->operands())) {
395+
if (!MD) {
396+
MDs.push_back(nullptr);
397+
continue;
398+
}
399+
Metadata *NewMD =
400+
Updater(updateLoopMetadataDebugLocationsRecursive(MD, Updater));
401+
if (NewMD)
402+
MDs.push_back(NewMD);
403+
Updated |= NewMD != MD;
404+
}
405+
406+
assert(!M->isDistinct() && "M should not be distinct.");
407+
return Updated ? MDNode::get(M->getContext(), MDs) : MetadataIn;
408+
}
409+
378410
static MDNode *updateLoopMetadataDebugLocationsImpl(
379411
MDNode *OrigLoopID, function_ref<Metadata *(Metadata *)> Updater) {
380412
assert(OrigLoopID && OrigLoopID->getNumOperands() > 0 &&
@@ -385,11 +417,11 @@ static MDNode *updateLoopMetadataDebugLocationsImpl(
385417
// Save space for the self-referential LoopID.
386418
SmallVector<Metadata *, 4> MDs = {nullptr};
387419

388-
for (unsigned i = 1; i < OrigLoopID->getNumOperands(); ++i) {
389-
Metadata *MD = OrigLoopID->getOperand(i);
420+
for (Metadata *MD : llvm::drop_begin(OrigLoopID->operands())) {
390421
if (!MD)
391422
MDs.push_back(nullptr);
392-
else if (Metadata *NewMD = Updater(MD))
423+
else if (Metadata *NewMD = Updater(
424+
updateLoopMetadataDebugLocationsRecursive(MD, Updater)))
393425
MDs.push_back(NewMD);
394426
}
395427

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=inline -S | FileCheck %s
3+
4+
; When inlining we need to update DILocation recursively for the followup
5+
; metadata when updating llvm.loop metadata.
6+
7+
define void @a() !dbg !3 {
8+
; CHECK-LABEL: define void @a(
9+
; CHECK-SAME: ) !dbg [[DBG3:![0-9]+]] {
10+
; CHECK-NEXT: [[ENTRY:.*:]]
11+
; CHECK-NEXT: br label %[[FOR_BODY:.*]]
12+
; CHECK: [[FOR_BODY]]:
13+
; CHECK-NEXT: br label %[[FOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
14+
;
15+
entry:
16+
br label %for.body
17+
18+
for.body: ; preds = %for.body, %entry
19+
br label %for.body, !llvm.loop !6
20+
}
21+
22+
define void @f() !dbg !17 {
23+
; CHECK-LABEL: define void @f(
24+
; CHECK-SAME: ) !dbg [[DBG17:![0-9]+]] {
25+
; CHECK-NEXT: [[ENTRY:.*:]]
26+
; CHECK-NEXT: br label %[[A_EXIT:.*]]
27+
; CHECK: [[A_EXIT]]:
28+
; CHECK-NEXT: br label %[[A_EXIT]], !llvm.loop [[LOOP18:![0-9]+]]
29+
; CHECK: [[A_EXIT1:.*:]]
30+
; CHECK-NEXT: ret void
31+
;
32+
entry:
33+
call void @a(), !dbg !18
34+
ret void
35+
}
36+
37+
!llvm.dbg.cu = !{!0}
38+
!llvm.module.flags = !{!2}
39+
40+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, splitDebugInlining: false, nameTableKind: None)
41+
!1 = !DIFile(filename: "foo.c", directory: "/")
42+
!2 = !{i32 2, !"Debug Info Version", i32 3}
43+
!3 = distinct !DISubprogram(name: "a", scope: !1, file: !1, line: 3, type: !4, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
44+
!4 = !DISubroutineType(types: !5)
45+
!5 = !{}
46+
!6 = distinct !{!6, !7, !8, !9, !10, !11}
47+
!7 = !DILocation(line: 6, column: 3, scope: !3)
48+
!8 = !DILocation(line: 7, column: 22, scope: !3)
49+
!9 = !{!"llvm.loop.mustprogress"}
50+
!10 = !{!"llvm.loop.distribute.enable", i1 true}
51+
!11 = !{!"llvm.loop.distribute.followup_all", !7, !8, !9, !12, !13, !14}
52+
!12 = !{!"llvm.loop.vectorize.width", i32 8}
53+
!13 = !{!"llvm.loop.vectorize.enable", i1 true}
54+
!14 = !{!"llvm.loop.vectorize.followup_all", !7, !8, !9, !15, !16}
55+
!15 = !{!"llvm.loop.isvectorized"}
56+
!16 = !{!"llvm.loop.unroll.count", i32 1}
57+
!17 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 9, type: !4, scopeLine: 9, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
58+
!18 = !DILocation(line: 9, column: 12, scope: !17)
59+
;.
60+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, splitDebugInlining: false, nameTableKind: None)
61+
; CHECK: [[META1]] = !DIFile(filename: "{{.*}}foo.c", directory: {{.*}})
62+
; CHECK: [[DBG3]] = distinct !DISubprogram(name: "a", scope: [[META1]], file: [[META1]], line: 3, type: [[META4:![0-9]+]], scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]])
63+
; CHECK: [[META4]] = !DISubroutineType(types: [[META5:![0-9]+]])
64+
; CHECK: [[META5]] = !{}
65+
; CHECK: [[LOOP6]] = distinct !{[[LOOP6]], [[META7:![0-9]+]], [[META8:![0-9]+]], [[META9:![0-9]+]], [[META10:![0-9]+]], [[META11:![0-9]+]]}
66+
; CHECK: [[META7]] = !DILocation(line: 6, column: 3, scope: [[DBG3]])
67+
; CHECK: [[META8]] = !DILocation(line: 7, column: 22, scope: [[DBG3]])
68+
; CHECK: [[META9]] = !{!"llvm.loop.mustprogress"}
69+
; CHECK: [[META10]] = !{!"llvm.loop.distribute.enable", i1 true}
70+
; CHECK: [[META11]] = !{!"llvm.loop.distribute.followup_all", [[META7]], [[META8]], [[META9]], [[META12:![0-9]+]], [[META13:![0-9]+]], [[META14:![0-9]+]]}
71+
; CHECK: [[META12]] = !{!"llvm.loop.vectorize.width", i32 8}
72+
; CHECK: [[META13]] = !{!"llvm.loop.vectorize.enable", i1 true}
73+
; CHECK: [[META14]] = !{!"llvm.loop.vectorize.followup_all", [[META7]], [[META8]], [[META9]], [[META15:![0-9]+]], [[META16:![0-9]+]]}
74+
; CHECK: [[META15]] = !{!"llvm.loop.isvectorized"}
75+
; CHECK: [[META16]] = !{!"llvm.loop.unroll.count", i32 1}
76+
; CHECK: [[DBG17]] = distinct !DISubprogram(name: "f", scope: [[META1]], file: [[META1]], line: 9, type: [[META4]], scopeLine: 9, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]])
77+
; CHECK: [[LOOP18]] = distinct !{[[LOOP18]], [[META19:![0-9]+]], [[META21:![0-9]+]], [[META9]], [[META10]], [[META22:![0-9]+]]}
78+
; CHECK: [[META19]] = !DILocation(line: 6, column: 3, scope: [[DBG3]], inlinedAt: [[META20:![0-9]+]])
79+
; CHECK: [[META20]] = distinct !DILocation(line: 9, column: 12, scope: [[DBG17]])
80+
; CHECK: [[META21]] = !DILocation(line: 7, column: 22, scope: [[DBG3]], inlinedAt: [[META20]])
81+
; CHECK: [[META22]] = !{!"llvm.loop.distribute.followup_all", [[META19]], [[META21]], [[META9]], [[META12]], [[META13]], [[META23:![0-9]+]]}
82+
; CHECK: [[META23]] = !{!"llvm.loop.vectorize.followup_all", [[META19]], [[META21]], [[META9]], [[META15]], [[META16]]}
83+
;.

0 commit comments

Comments
 (0)