Skip to content

Commit 857c25b

Browse files
committed
[DebugInfo][LoopUnroll] Preserve DebugLocs on optimized cond branches
This patch fixes a simple error where as part of loop unrolling we optimize conditional loop-exiting branches into unconditional branches when we know that they will or won't exit the loop, but does not propagate the source location of the original branch to the new one.
1 parent 32719c4 commit 857c25b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,8 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
883883
DeadSucc->removePredecessor(Src, /* KeepOneInputPHIs */ true);
884884

885885
// Replace the conditional branch with an unconditional one.
886-
BranchInst::Create(Dest, Term->getIterator());
886+
auto *BI = BranchInst::Create(Dest, Term->getIterator());
887+
BI->setDebugLoc(Term->getDebugLoc());
887888
Term->eraseFromParent();
888889

889890
DTUpdates.emplace_back(DominatorTree::Delete, Src, DeadSucc);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
2+
; RUN: opt < %s -passes=loop-unroll -S | FileCheck %s
3+
; RUN: opt < %s -passes=loop-unroll-full -S | FileCheck %s
4+
5+
;; Loop Unrolling should preserve the DebugLocs of conditional branches that get
6+
;; optimized into unconditional branches.
7+
8+
define i32 @_ZeqRK4int3S1_() {
9+
; CHECK-LABEL: define i32 @_ZeqRK4int3S1_() {
10+
; CHECK-NEXT: [[ENTRY:.*:]]
11+
; CHECK-NEXT: br label %[[FOR_BODY:.*]]
12+
; CHECK: [[FOR_COND:.*]]:
13+
; CHECK-NEXT: br label %[[CLEANUP:.*]], !dbg [[DBG3:![0-9]+]]
14+
; CHECK: [[FOR_BODY]]:
15+
; CHECK-NEXT: br i1 false, label %[[FOR_COND]], label %[[CLEANUP]]
16+
; CHECK: [[CLEANUP]]:
17+
; CHECK-NEXT: ret i32 0
18+
;
19+
entry:
20+
br label %for.body
21+
22+
for.cond: ; preds = %for.body
23+
br i1 false, label %cleanup, label %for.body, !dbg !3
24+
25+
for.body: ; preds = %for.cond, %entry
26+
br i1 false, label %for.cond, label %cleanup
27+
28+
cleanup: ; preds = %for.body, %for.cond
29+
ret i32 0
30+
}
31+
32+
!llvm.dbg.cu = !{!0}
33+
!llvm.module.flags = !{!2}
34+
35+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 20.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug)
36+
!1 = !DIFile(filename: "test.cpp", directory: "/tmp")
37+
!2 = !{i32 2, !"Debug Info Version", i32 3}
38+
!3 = !DILocation(line: 304, column: 2, scope: !4)
39+
!4 = distinct !DILexicalBlock(scope: !5, file: !1, line: 304, column: 2)
40+
!5 = distinct !DISubprogram(name: "operator==", linkageName: "_ZeqRK4int3S1_", scope: !1, file: !1, line: 302, type: !6, scopeLine: 303, spFlags: DISPFlagDefinition, unit: !0)
41+
!6 = distinct !DISubroutineType(types: !7)
42+
!7 = !{}
43+
;.
44+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug)
45+
; CHECK: [[META1]] = !DIFile(filename: "test.cpp", directory: {{.*}})
46+
; CHECK: [[META2:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
47+
; CHECK: [[DBG3]] = !DILocation(line: 304, column: 2, scope: [[META4:![0-9]+]])
48+
; CHECK: [[META4]] = distinct !DILexicalBlock(scope: [[META5:![0-9]+]], file: [[META1]], line: 304, column: 2)
49+
; CHECK: [[META5]] = distinct !DISubprogram(name: "operator==", linkageName: "_ZeqRK4int3S1_", scope: [[META1]], file: [[META1]], line: 302, type: [[META6:![0-9]+]], scopeLine: 303, spFlags: DISPFlagDefinition, unit: [[META0]])
50+
; CHECK: [[META6]] = distinct !DISubroutineType(types: [[META7:![0-9]+]])
51+
; CHECK: [[META7]] = !{}
52+
;.

0 commit comments

Comments
 (0)