Skip to content

Commit 12346fa

Browse files
committed
[licm] clone metadata when hoisting conditional branch
1 parent f675483 commit 12346fa

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

llvm/lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ static cl::opt<std::string> PrintBranchProbFuncName(
5959
cl::desc("The option to specify the name of the function "
6060
"whose branch probability info is printed."));
6161

62+
// FIXME: Flag used for an ablation performance test, Issue #147390. Placing it
63+
// here because referencing Analysis should be feasible from anywhere. Will be
64+
// removed after the ablation test.
65+
cl::opt<bool> ProfcheckDisableMetadataFixes(
66+
"profcheck-disable-metadata-fixes", cl::Hidden, cl::init(false),
67+
cl::desc(
68+
"Disable metadata propagation fixes discovered through Issue #147390"));
69+
6270
INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob",
6371
"Branch Probability Analysis", false, true)
6472
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ using PointersAndHasReadsOutsideSet =
218218
static SmallVector<PointersAndHasReadsOutsideSet, 0>
219219
collectPromotionCandidates(MemorySSA *MSSA, AliasAnalysis *AA, Loop *L);
220220

221+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
222+
221223
namespace {
222224
struct LoopInvariantCodeMotion {
223225
bool runOnLoop(Loop *L, AAResults *AA, LoopInfo *LI, DominatorTree *DT,
@@ -857,9 +859,18 @@ class ControlFlowHoister {
857859
}
858860

859861
// Now finally clone BI.
860-
ReplaceInstWithInst(
861-
HoistTarget->getTerminator(),
862-
BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition()));
862+
auto *NewBI =
863+
BranchInst::Create(HoistTrueDest, HoistFalseDest, BI->getCondition());
864+
ReplaceInstWithInst(HoistTarget->getTerminator(), NewBI);
865+
// Copy all the metadata. In particular:
866+
// - debug info (critical to Sample-based profiling) should be the same as
867+
// the original branch, not that of HoistTarget->getTerminator(), which is
868+
// what ReplaceInstWithInst would use.
869+
// - md_prof should also come from the original branch - since the condition
870+
// was hoisted, the branch probabilities shouldn't change.
871+
if (!ProfcheckDisableMetadataFixes)
872+
NewBI->copyMetadata(*BI);
873+
863874
++NumClonedBranches;
864875

865876
assert(CurLoop->getLoopPreheader() &&
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; Test that hoisting conditional branches copies the debug and profiling info
2+
; metadata from the branch being hoisted.
3+
; RUN: opt -S -passes=licm -licm-control-flow-hoisting=1 %s -o - | FileCheck %s
4+
5+
; CHECK-LABEL: @triangle_phi
6+
define void @triangle_phi(i32 %x, ptr %p) {
7+
; CHECK-LABEL: entry:
8+
; CHECK: %cmp1 = icmp sgt i32 %x, 0
9+
; CHECK: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]], !dbg [[ORIG_DBG:![0-9]+]], !prof [[ORIG_PROF:![0-9]+]]
10+
entry:
11+
br label %loop, !dbg !5
12+
13+
; CHECK: [[IF_LICM]]:
14+
; CHECK: %add = add i32 %x, 1
15+
; CHECK: br label %[[THEN_LICM]]
16+
17+
; CHECK: [[THEN_LICM]]:
18+
; CHECK: phi i32 [ %add, %[[IF_LICM]] ], [ %x, %entry ]
19+
; CHECK: store i32 %phi, ptr %p
20+
; CHECK: %cmp2 = icmp ne i32 %phi, 0
21+
; CHECK: br label %loop
22+
23+
; CHECK-LABEL: loop:
24+
loop:
25+
%cmp1 = icmp sgt i32 %x, 0
26+
; CHECK: br i1 %cmp1, label %if, label %then, !dbg [[ORIG_DBG]], !prof [[ORIG_PROF]]
27+
br i1 %cmp1, label %if, label %then, !dbg !6, !prof !8
28+
if:
29+
%add = add i32 %x, 1
30+
br label %then
31+
32+
; CHECK-LABEL: then:
33+
then:
34+
%phi = phi i32 [ %add, %if ], [ %x, %loop ]
35+
store i32 %phi, ptr %p
36+
%cmp2 = icmp ne i32 %phi, 0
37+
; CHECK-NEXT: br i1 %cmp2, label %loop, label %end, !dbg [[OTHER_DBG:![0-9]+]], !prof [[OTHER_PROF:![0-9]+]]
38+
br i1 %cmp2, label %loop, label %end, !dbg !7, !prof !9
39+
40+
; CHECK-LABEL: end:
41+
end:
42+
ret void
43+
}
44+
45+
; CHECK-DAG: [[ORIG_DBG]] = !DILocation(line: 2, column: 22
46+
; CHECK-DAG: [[ORIG_PROF]] = !{!"branch_weights", i32 5, i32 7}
47+
; CHECK-DAG: [[OTHER_DBG]] = !DILocation(line: 3, column: 22
48+
; CHECK-DAG: [[OTHER_PROF]] = !{!"branch_weights", i32 13, i32 11}
49+
50+
51+
!llvm.module.flags = !{!2, !3}
52+
53+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
54+
!1 = !DIFile(filename: "t", directory: "/")
55+
!2 = !{i32 7, !"Dwarf Version", i32 5}
56+
!3 = !{i32 2, !"Debug Info Version", i32 3}
57+
!4 = distinct !DISubprogram(name: "triangle_phi", linkageName: "triangle_phi", scope: !1, file: !1, line: 1, unit: !0)
58+
!5 = !DILocation(line: 1, column: 22, scope: !4)
59+
!6 = !DILocation(line: 2, column: 22, scope: !4)
60+
!7 = !DILocation(line: 3, column: 22, scope: !4)
61+
!8 = !{!"branch_weights", i32 5, i32 7}
62+
!9 = !{!"branch_weights", i32 13, i32 11}

0 commit comments

Comments
 (0)