Skip to content

Commit 67a108c

Browse files
committed
[SLU][profcheck] Use the original branch weigths in buildPartialInvariantUnswitchConditionalBranch
1 parent 9914c83 commit 67a108c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static void buildPartialUnswitchConditionalBranch(
331331
static void buildPartialInvariantUnswitchConditionalBranch(
332332
BasicBlock &BB, ArrayRef<Value *> ToDuplicate, bool Direction,
333333
BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc, Loop &L,
334-
MemorySSAUpdater *MSSAU) {
334+
MemorySSAUpdater *MSSAU, const BranchInst &OriginalBranch) {
335335
ValueToValueMapTy VMap;
336336
for (auto *Val : reverse(ToDuplicate)) {
337337
Instruction *Inst = cast<Instruction>(Val);
@@ -371,8 +371,17 @@ static void buildPartialInvariantUnswitchConditionalBranch(
371371
IRBuilder<> IRB(&BB);
372372
IRB.SetCurrentDebugLocation(DebugLoc::getCompilerGenerated());
373373
Value *Cond = VMap[ToDuplicate[0]];
374-
IRB.CreateCondBr(Cond, Direction ? &UnswitchedSucc : &NormalSucc,
375-
Direction ? &NormalSucc : &UnswitchedSucc);
374+
auto *ProfData =
375+
!ProfcheckDisableMetadataFixes &&
376+
ToDuplicate[0] == skipTrivialSelect(OriginalBranch.getCondition())
377+
? OriginalBranch.getMetadata(LLVMContext::MD_prof)
378+
: nullptr;
379+
auto *BR =
380+
IRB.CreateCondBr(Cond, Direction ? &UnswitchedSucc : &NormalSucc,
381+
Direction ? &NormalSucc : &UnswitchedSucc, ProfData);
382+
if (!ProfData)
383+
setExplicitlyUnknownBranchWeightsIfProfiled(
384+
*BR, *BR->getParent()->getParent(), DEBUG_TYPE);
376385
}
377386

378387
/// Rewrite the PHI nodes in an unswitched loop exit basic block.
@@ -2509,7 +2518,7 @@ static void unswitchNontrivialInvariants(
25092518
// the branch in the split block.
25102519
if (PartiallyInvariant)
25112520
buildPartialInvariantUnswitchConditionalBranch(
2512-
*SplitBB, Invariants, Direction, *ClonedPH, *LoopPH, L, MSSAU);
2521+
*SplitBB, Invariants, Direction, *ClonedPH, *LoopPH, L, MSSAU, *BI);
25132522
else {
25142523
buildPartialUnswitchConditionalBranch(
25152524
*SplitBB, Invariants, Direction, *ClonedPH, *LoopPH,

llvm/test/Transforms/SimpleLoopUnswitch/partial-unswitch.ll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
declare void @clobber()
55

6-
define i32 @partial_unswitch_true_successor(ptr %ptr, i32 %N) {
6+
define i32 @partial_unswitch_true_successor(ptr %ptr, i32 %N) !prof !0 {
77
; CHECK-LABEL: @partial_unswitch_true_successor(
88
; CHECK-NEXT: entry:
99
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[PTR:%.*]], align 4
@@ -50,7 +50,7 @@ loop.header:
5050
%iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ]
5151
%lv = load i32, ptr %ptr
5252
%sc = icmp eq i32 %lv, 100
53-
br i1 %sc, label %noclobber, label %clobber
53+
br i1 %sc, label %noclobber, label %clobber, !prof !1
5454

5555
noclobber:
5656
br label %loop.latch
@@ -62,7 +62,7 @@ clobber:
6262
loop.latch:
6363
%c = icmp ult i32 %iv, %N
6464
%iv.next = add i32 %iv, 1
65-
br i1 %c, label %loop.header, label %exit
65+
br i1 %c, label %loop.header, label %exit, !prof !2
6666

6767
exit:
6868
ret i32 10
@@ -1456,6 +1456,10 @@ exit:
14561456
ret i32 10
14571457
}
14581458

1459+
!0 = !{!"function_entry_count", i32 10}
1460+
!1 = !{!"branch_weights", i32 1000, i32 1}
1461+
!2 = !{!"branch_weights", i32 100, i32 3}
1462+
14591463
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[UNSWITCH_PARTIAL_DISABLE:![0-9]+]]}
14601464
; CHECK: [[UNSWITCH_PARTIAL_DISABLE]] = !{!"llvm.loop.unswitch.partial.disable"}
14611465
; CHECK: [[LOOP2]] = distinct !{[[LOOP2]], [[UNSWITCH_PARTIAL_DISABLE]]}

0 commit comments

Comments
 (0)