Skip to content

Commit 8d04383

Browse files
committed
!fixup address latest comments, thanks!
1 parent 65d0288 commit 8d04383

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7859,18 +7859,16 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
78597859

78607860
ILV.printDebugTracesAtEnd();
78617861

7862-
// 4. Adjust branch weight of the branch in the middle block if it exists.
7863-
if (ExitVPBB) {
7864-
auto *MiddleTerm =
7865-
cast<BranchInst>(State.CFG.VPBB2IRBB[ExitVPBB]->getTerminator());
7866-
if (MiddleTerm->isConditional() &&
7867-
hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())) {
7868-
// Assume that `Count % VectorTripCount` is equally distributed.
7869-
unsigned TripCount = BestVPlan.getUF() * State.VF.getKnownMinValue();
7870-
assert(TripCount > 0 && "trip count should not be zero");
7871-
const uint32_t Weights[] = {1, TripCount - 1};
7872-
setBranchWeights(*MiddleTerm, Weights, /*IsExpected=*/false);
7873-
}
7862+
// 4. Adjust branch weight of the branch in the middle block.
7863+
auto *MiddleTerm =
7864+
cast<BranchInst>(State.CFG.VPBB2IRBB[ExitVPBB]->getTerminator());
7865+
if (MiddleTerm->isConditional() &&
7866+
hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())) {
7867+
// Assume that `Count % VectorTripCount` is equally distributed.
7868+
unsigned TripCount = BestVPlan.getUF() * State.VF.getKnownMinValue();
7869+
assert(TripCount > 0 && "trip count should not be zero");
7870+
const uint32_t Weights[] = {1, TripCount - 1};
7871+
setBranchWeights(*MiddleTerm, Weights, /*IsExpected=*/false);
78747872
}
78757873

78767874
return State.ExpandedSCEVs;

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,14 @@ class VPBlockBase {
621621
/// Remove all the successors of this block.
622622
void clearSuccessors() { Successors.clear(); }
623623

624+
/// Swap successors of the block. The block must have exactly 2 successors.
625+
// TODO: This should be part of introducing conditional branch recipes rather
626+
// than being independent.
627+
void swapSuccessors() {
628+
assert(Successors.size() == 2 && "must have 2 successors to swap");
629+
std::swap(Successors[0], Successors[1]);
630+
}
631+
624632
/// The method which generates the output IR that correspond to this
625633
/// VPBlockBase, thereby "executing" the VPlan.
626634
virtual void execute(VPTransformState *State) = 0;

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,21 +1898,22 @@ void VPlanTransforms::handleUncountableEarlyExit(
18981898
VPBasicBlock *NewMiddle = new VPBasicBlock("middle.split");
18991899
VPBlockUtils::insertOnEdge(LoopRegion, MiddleVPBB, NewMiddle);
19001900
VPBlockUtils::connectBlocks(NewMiddle, VPEarlyExitBlock);
1901-
std::swap(NewMiddle->getSuccessors()[0], NewMiddle->getSuccessors()[1]);
1901+
NewMiddle->swapSuccessors();
19021902

19031903
VPBuilder MiddleBuilder(NewMiddle);
19041904
MiddleBuilder.createNaryOp(VPInstruction::BranchOnCond, {IsEarlyExitTaken});
19051905

19061906
// Replace the condition controlling the non-early exit from the vector loop
19071907
// with one exiting if either the original condition of the vector latch is
19081908
// true or the early exit has been taken.
1909-
auto *LatchExitingBranch =
1910-
dyn_cast<VPInstruction>(LatchVPBB->getTerminator());
1909+
auto *LatchExitingBranch = cast<VPInstruction>(LatchVPBB->getTerminator());
1910+
assert(LatchExitingBranch->getOpcode() == VPInstruction::BranchOnCount &&
1911+
"Unexpected terminator");
19111912
auto *IsLatchExitTaken =
19121913
Builder.createICmp(CmpInst::ICMP_EQ, LatchExitingBranch->getOperand(0),
19131914
LatchExitingBranch->getOperand(1));
1914-
auto *AnyExiting = Builder.createNaryOp(Instruction::Or,
1915-
{IsEarlyExitTaken, IsLatchExitTaken});
1916-
Builder.createNaryOp(VPInstruction::BranchOnCond, AnyExiting);
1915+
auto *AnyExitTaken = Builder.createNaryOp(
1916+
Instruction::Or, {IsEarlyExitTaken, IsLatchExitTaken});
1917+
Builder.createNaryOp(VPInstruction::BranchOnCond, AnyExitTaken);
19171918
LatchExitingBranch->eraseFromParent();
19181919
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct VPlanTransforms {
128128
/// UncountableExitingBlock by
129129
/// * updating the condition exiting the vector loop to include the early
130130
/// exit conditions
131-
/// * splitting the original middle block to branch to the early exit blocks
131+
/// * splitting the original middle block to branch to the early exit block
132132
/// if taken.
133133
static void handleUncountableEarlyExit(VPlan &Plan, ScalarEvolution &SE,
134134
Loop *OrigLoop,

0 commit comments

Comments
 (0)