Skip to content

Commit fe88c50

Browse files
Apochensyuxuanchen1997
authored andcommitted
[DebugInfo][SimpleLoopUnswitch] Fix missing debug location updates for new terminators (#98789)
Summary: Fix #98787 . Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251388
1 parent 3040951 commit fe88c50

File tree

2 files changed

+109
-7
lines changed

2 files changed

+109
-7
lines changed

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,8 @@ static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
632632
} else {
633633
// Create a new unconditional branch that will continue the loop as a new
634634
// terminator.
635-
BranchInst::Create(ContinueBB, ParentBB);
635+
Instruction *NewBI = BranchInst::Create(ContinueBB, ParentBB);
636+
NewBI->setDebugLoc(BI.getDebugLoc());
636637
}
637638
BI.setSuccessor(LoopExitSuccIdx, UnswitchedBB);
638639
BI.setSuccessor(1 - LoopExitSuccIdx, NewPH);
@@ -666,10 +667,12 @@ static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
666667
// Finish updating dominator tree and memory ssa for full unswitch.
667668
if (FullUnswitch) {
668669
if (MSSAU) {
669-
// Remove the cloned branch instruction.
670-
ParentBB->getTerminator()->eraseFromParent();
671-
// Create unconditional branch now.
672-
BranchInst::Create(ContinueBB, ParentBB);
670+
Instruction *Term = ParentBB->getTerminator();
671+
// Remove the cloned branch instruction and create unconditional branch
672+
// now.
673+
Instruction *NewBI = BranchInst::Create(ContinueBB, ParentBB);
674+
NewBI->setDebugLoc(Term->getDebugLoc());
675+
Term->eraseFromParent();
673676
MSSAU->removeEdge(ParentBB, LoopExitBB);
674677
}
675678
DT.deleteEdge(ParentBB, LoopExitBB);
@@ -861,8 +864,11 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
861864
BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI, MSSAU);
862865
OldPH->getTerminator()->eraseFromParent();
863866

864-
// Now add the unswitched switch.
867+
// Now add the unswitched switch. This new switch instruction inherits the
868+
// debug location of the old switch, because it semantically replace the old
869+
// one.
865870
auto *NewSI = SwitchInst::Create(LoopCond, NewPH, ExitCases.size(), OldPH);
871+
NewSI->setDebugLoc(SIW->getDebugLoc());
866872
SwitchInstProfUpdateWrapper NewSIW(*NewSI);
867873

868874
// Rewrite the IR for the unswitched basic blocks. This requires two steps.
@@ -972,8 +978,9 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
972978
/*KeepOneInputPHIs*/ true);
973979
}
974980
// Now nuke the switch and replace it with a direct branch.
981+
Instruction *NewBI = BranchInst::Create(CommonSuccBB, BB);
982+
NewBI->setDebugLoc(SIW->getDebugLoc());
975983
SIW.eraseFromParent();
976-
BranchInst::Create(CommonSuccBB, BB);
977984
} else if (DefaultExitBB) {
978985
assert(SI.getNumCases() > 0 &&
979986
"If we had no cases we'd have a common successor!");
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
; RUN: opt -passes='loop(simple-loop-unswitch)' -S < %s | FileCheck %s
2+
; RUN: opt -passes='loop-mssa(simple-loop-unswitch)' -S < %s | FileCheck %s
3+
4+
; Check that SimpleLoopUnswitch's unswitchTrivialBranch() and unswitchTrivialSwitch()
5+
; propagates debug locations to the new terminators replacing the old ones.
6+
7+
define i32 @test1(ptr %var, i1 %cond1, i1 %cond2) !dbg !5 {
8+
; CHECK-LABEL: define i32 @test1(
9+
; CHECK: loop_begin:
10+
; CHECK-NEXT: br label %[[CONTINUE:.*]], !dbg [[DBG8:![0-9]+]]
11+
;
12+
entry:
13+
br label %loop_begin, !dbg !8
14+
15+
loop_begin: ; preds = %do_something, %entry
16+
br i1 %cond1, label %continue, label %loop_exit, !dbg !9
17+
18+
continue: ; preds = %loop_begin
19+
%var_val = load i32, ptr %var, align 4, !dbg !10
20+
br i1 %cond2, label %do_something, label %loop_exit, !dbg !11
21+
22+
do_something: ; preds = %continue
23+
call void @some_func(), !dbg !12
24+
br label %loop_begin, !dbg !13
25+
26+
loop_exit: ; preds = %continue, %loop_begin
27+
ret i32 0, !dbg !14
28+
}
29+
30+
define i32 @test7(i32 %cond1, i32 %x, i32 %y) !dbg !15 {
31+
; CHECK-LABEL: define i32 @test7(
32+
; CHECK-SAME: i32 [[COND1:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]])
33+
; CHECK-NEXT: entry:
34+
; CHECK-NEXT: switch i32 [[COND1]], label %[[ENTRY_SPLIT:.*]] [
35+
; CHECK-NEXT: i32 0, label %[[LOOP_EXIT:.*]]
36+
; CHECK-NEXT: i32 1, label %[[LOOP_EXIT]]
37+
; CHECK-NEXT: ], !dbg [[DBG16:![0-9]+]]
38+
; CHECK: loop_begin:
39+
; CHECK-NEXT: br label %[[LATCH:.*]], !dbg [[DBG16]]
40+
;
41+
entry:
42+
br label %loop_begin, !dbg !16
43+
44+
loop_begin: ; preds = %latch, %entry
45+
switch i32 %cond1, label %latch [
46+
i32 0, label %loop_exit
47+
i32 1, label %loop_exit
48+
], !dbg !17
49+
50+
latch: ; preds = %loop_begin
51+
call void @some_func(), !dbg !18
52+
br label %loop_begin, !dbg !19
53+
54+
loop_exit: ; preds = %loop_begin, %loop_begin
55+
%result1 = phi i32 [ %x, %loop_begin ], [ %x, %loop_begin ], !dbg !20
56+
%result2 = phi i32 [ %y, %loop_begin ], [ %y, %loop_begin ], !dbg !21
57+
%result = add i32 %result1, %result2, !dbg !22
58+
ret i32 %result, !dbg !23
59+
}
60+
61+
; Function Attrs: noreturn
62+
declare void @some_func()
63+
64+
!llvm.dbg.cu = !{!0}
65+
!llvm.debugify = !{!2, !3}
66+
!llvm.module.flags = !{!4}
67+
68+
; CHECK: [[DBG8]] = !DILocation(line: 2,
69+
70+
; CHECK: [[DBG16]] = !DILocation(line: 9,
71+
72+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
73+
!1 = !DIFile(filename: "test2.ll", directory: "/")
74+
!2 = !{i32 15}
75+
!3 = !{i32 0}
76+
!4 = !{i32 2, !"Debug Info Version", i32 3}
77+
!5 = distinct !DISubprogram(name: "test1", linkageName: "test1", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
78+
!6 = !DISubroutineType(types: !7)
79+
!7 = !{}
80+
!8 = !DILocation(line: 1, column: 1, scope: !5)
81+
!9 = !DILocation(line: 2, column: 1, scope: !5)
82+
!10 = !DILocation(line: 3, column: 1, scope: !5)
83+
!11 = !DILocation(line: 4, column: 1, scope: !5)
84+
!12 = !DILocation(line: 5, column: 1, scope: !5)
85+
!13 = !DILocation(line: 6, column: 1, scope: !5)
86+
!14 = !DILocation(line: 7, column: 1, scope: !5)
87+
!15 = distinct !DISubprogram(name: "test7", linkageName: "test7", scope: null, file: !1, line: 8, type: !6, scopeLine: 8, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
88+
!16 = !DILocation(line: 8, column: 1, scope: !15)
89+
!17 = !DILocation(line: 9, column: 1, scope: !15)
90+
!18 = !DILocation(line: 10, column: 1, scope: !15)
91+
!19 = !DILocation(line: 11, column: 1, scope: !15)
92+
!20 = !DILocation(line: 12, column: 1, scope: !15)
93+
!21 = !DILocation(line: 13, column: 1, scope: !15)
94+
!22 = !DILocation(line: 14, column: 1, scope: !15)
95+
!23 = !DILocation(line: 15, column: 1, scope: !15)

0 commit comments

Comments
 (0)