Skip to content

Commit 5039bf4

Browse files
authored
[DebugInfo][Inline] Propagate source locs when simplifying cond branches (#134827)
During inlining, we may opportunistically simplify conditional branches (incl. switches) to unconditional branches if, after inlining, their destination is fixed. While we do this, we should propagate any DILocation attached to the original branch to the simplified branch, which this patch enables. Found using #107279.
1 parent a00a61d commit 5039bf4

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

llvm/lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,9 @@ void PruningFunctionCloner::CloneBlock(
607607
// Constant fold to uncond branch!
608608
if (Cond) {
609609
BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
610-
VMap[OldTI] = BranchInst::Create(Dest, NewBB);
610+
auto *NewBI = BranchInst::Create(Dest, NewBB);
611+
NewBI->setDebugLoc(BI->getDebugLoc());
612+
VMap[OldTI] = NewBI;
611613
ToClone.push_back(Dest);
612614
TerminatorDone = true;
613615
}
@@ -622,7 +624,9 @@ void PruningFunctionCloner::CloneBlock(
622624
if (Cond) { // Constant fold to uncond branch!
623625
SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
624626
BasicBlock *Dest = const_cast<BasicBlock *>(Case.getCaseSuccessor());
625-
VMap[OldTI] = BranchInst::Create(Dest, NewBB);
627+
auto *NewBI = BranchInst::Create(Dest, NewBB);
628+
NewBI->setDebugLoc(SI->getDebugLoc());
629+
VMap[OldTI] = NewBI;
626630
ToClone.push_back(Dest);
627631
TerminatorDone = true;
628632
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -mtriple=x86_64-unknown-unknown -S -passes=inline | FileCheck %s
3+
4+
;; Test that when we simplify inlined conditional branch/switch instructions to
5+
;; unconditional branches, we propagate the original branch's source location to
6+
;; the simplified branch.
7+
8+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
9+
target triple = "x86_64-unknown-linux-gnu"
10+
11+
define i1 @ham() {
12+
; CHECK-LABEL: define i1 @ham() {
13+
; CHECK-NEXT: [[BB:.*:]]
14+
; CHECK-NEXT: br i1 poison, label %[[BB1_I:.*]], label %[[HOGE_EXIT:.*]]
15+
; CHECK: [[BB1_I]]:
16+
; CHECK-NEXT: br label %[[HOGE_EXIT]], !dbg [[DBG4:![0-9]+]]
17+
; CHECK: [[HOGE_EXIT]]:
18+
; CHECK-NEXT: br i1 poison, label %[[BB1_I1:.*]], label %[[QUUX_EXIT:.*]]
19+
; CHECK: [[BB1_I1]]:
20+
; CHECK-NEXT: br label %[[QUUX_EXIT]], !dbg [[DBG7:![0-9]+]]
21+
; CHECK: [[QUUX_EXIT]]:
22+
; CHECK-NEXT: ret i1 false
23+
;
24+
bb:
25+
%call1 = call fastcc i32 @hoge(i1 false)
26+
%call2 = call fastcc i32 @quux(i8 0)
27+
ret i1 false
28+
}
29+
30+
define fastcc i32 @hoge(i1 %in) {
31+
; CHECK-LABEL: define fastcc i32 @hoge(
32+
; CHECK-SAME: i1 [[IN:%.*]]) {
33+
; CHECK-NEXT: [[BB:.*:]]
34+
; CHECK-NEXT: br i1 poison, label %[[BB1:.*]], label %[[BB2:.*]]
35+
; CHECK: [[BB1]]:
36+
; CHECK-NEXT: br i1 [[IN]], label %[[BB2]], label %[[BB2]], !dbg [[DBG4]]
37+
; CHECK: [[BB2]]:
38+
; CHECK-NEXT: ret i32 0
39+
;
40+
bb:
41+
br i1 poison, label %bb1, label %bb2
42+
43+
bb1: ; preds = %bb
44+
br i1 %in, label %bb2, label %bb2, !dbg !4
45+
46+
bb2: ; preds = %bb1, %bb1, %bb
47+
ret i32 0
48+
}
49+
50+
define fastcc i32 @quux(i8 %in) {
51+
; CHECK-LABEL: define fastcc i32 @quux(
52+
; CHECK-SAME: i8 [[IN:%.*]]) {
53+
; CHECK-NEXT: [[BB:.*:]]
54+
; CHECK-NEXT: br i1 poison, label %[[BB1:.*]], label %[[BB2:.*]]
55+
; CHECK: [[BB1]]:
56+
; CHECK-NEXT: switch i8 [[IN]], label %[[BB2]] [
57+
; CHECK-NEXT: i8 0, label %[[BB2]]
58+
; CHECK-NEXT: ], !dbg [[DBG7]]
59+
; CHECK: [[BB2]]:
60+
; CHECK-NEXT: ret i32 0
61+
;
62+
bb:
63+
br i1 poison, label %bb1, label %bb2
64+
65+
bb1: ; preds = %bb
66+
switch i8 %in, label %bb2 [
67+
i8 0, label %bb2
68+
], !dbg !14
69+
70+
71+
bb2: ; preds = %bb1, %bb1, %bb
72+
ret i32 0
73+
}
74+
75+
!llvm.dbg.cu = !{!0}
76+
!llvm.module.flags = !{!3}
77+
78+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 20.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: !2, globals: !2, imports: !2, splitDebugInlining: false, nameTableKind: None)
79+
!1 = !DIFile(filename: "inline-simplified-branch-dbg-loc.cpp", directory: "/tmp")
80+
!2 = !{}
81+
!3 = !{i32 2, !"Debug Info Version", i32 3}
82+
!4 = !DILocation(line: 26, column: 11, scope: !12)
83+
!6 = !DIFile(filename: "inline-simplified-branch-dbg-loc.cpp", directory: "/tmp")
84+
!12 = distinct !DISubprogram(name: "hoge", linkageName: "hoge", scope: !6, file: !6, line: 10, type: !13, scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
85+
!13 = distinct !DISubroutineType(types: !2)
86+
!14 = !DILocation(line: 16, column: 1, scope: !15)
87+
!15 = distinct !DISubprogram(name: "quux", linkageName: "quux", scope: !6, file: !6, line: 20, type: !13, scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
88+
;.
89+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: [[META2:![0-9]+]], globals: [[META2]], imports: [[META2]], splitDebugInlining: false, nameTableKind: None)
90+
; CHECK: [[META1]] = !DIFile(filename: "inline-simplified-branch-dbg-loc.cpp", directory: {{.*}})
91+
; CHECK: [[META2]] = !{}
92+
; CHECK: [[DBG4]] = !DILocation(line: 26, column: 11, scope: [[META5:![0-9]+]])
93+
; CHECK: [[META5]] = distinct !DISubprogram(name: "hoge", linkageName: "hoge", scope: [[META1]], file: [[META1]], line: 10, type: [[META6:![0-9]+]], scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
94+
; CHECK: [[META6]] = distinct !DISubroutineType(types: [[META2]])
95+
; CHECK: [[DBG7]] = !DILocation(line: 16, column: 1, scope: [[META8:![0-9]+]])
96+
; CHECK: [[META8]] = distinct !DISubprogram(name: "quux", linkageName: "quux", scope: [[META1]], file: [[META1]], line: 20, type: [[META6]], scopeLine: 11, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
97+
;.

0 commit comments

Comments
 (0)