Skip to content

Commit 14815d4

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

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

llvm/lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ 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> DisableProfilingInfoCorrectPropagation(
66+
"profcheck-disable-fixes", cl::Hidden, cl::init(false),
67+
cl::desc("Temporary flag, will be used for an ablation test"));
68+
6269
INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob",
6370
"Branch Probability Analysis", false, true)
6471
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> DisableProfilingInfoCorrectPropagation;
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 (!DisableProfilingInfoCorrectPropagation)
872+
NewBI->copyMetadata(*BI);
873+
863874
++NumClonedBranches;
864875

865876
assert(CurLoop->getLoopPreheader() &&
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; Test that hoising conditional branches copies over profiling metadata
2+
; RUN: opt -S -passes=licm -licm-control-flow-hoisting=1 %s -o - | FileCheck %s
3+
4+
; CHECK-LABEL: @triangle_phi
5+
define void @triangle_phi(i32 %x, ptr %p) {
6+
; CHECK-LABEL: entry:
7+
; CHECK: %cmp1 = icmp sgt i32 %x, 0
8+
; CHECK: br i1 %cmp1, label %[[IF_LICM:.*]], label %[[THEN_LICM:.*]], !prof !0
9+
entry:
10+
br label %loop
11+
12+
; CHECK: [[IF_LICM]]:
13+
; CHECK: %add = add i32 %x, 1
14+
; CHECK: br label %[[THEN_LICM]]
15+
16+
; CHECK: [[THEN_LICM]]:
17+
; CHECK: phi i32 [ %add, %[[IF_LICM]] ], [ %x, %entry ]
18+
; CHECK: store i32 %phi, ptr %p
19+
; CHECK: %cmp2 = icmp ne i32 %phi, 0
20+
; CHECK: br label %loop
21+
22+
loop:
23+
%cmp1 = icmp sgt i32 %x, 0
24+
br i1 %cmp1, label %if, label %then, !prof !0
25+
26+
if:
27+
%add = add i32 %x, 1
28+
br label %then
29+
30+
; CHECK-LABEL: then:
31+
; CHECK: br i1 %cmp2, label %loop, label %end, !prof !1
32+
then:
33+
%phi = phi i32 [ %add, %if ], [ %x, %loop ]
34+
store i32 %phi, ptr %p
35+
%cmp2 = icmp ne i32 %phi, 0
36+
br i1 %cmp2, label %loop, label %end, !prof !1
37+
38+
; CHECK-LABEL: end:
39+
end:
40+
ret void
41+
}
42+
43+
; CHECK: !0 = !{!"branch_weights", i32 5, i32 7}
44+
; CHECK: !1 = !{!"branch_weights", i32 13, i32 11}
45+
!0 = !{!"branch_weights", i32 5, i32 7}
46+
!1 = !{!"branch_weights", i32 13, i32 11}

0 commit comments

Comments
 (0)