Skip to content

Commit 4a6d31f

Browse files
committed
[LV] Pass resume phi to fixReductionScalarResumeWhenVectorizing (NFC).
fixReductionScalarResumeWhenVectorizingEpilog updates the resume phis in the scalar preheader. Instead of looking at all recipes in the middle block and finding their resume-phi users we can iterate over all resume phis in the scalar preheader directly. This slightly simplifies the code and removes the need to look for the resume phi. Also slightly simplifies #141860.
1 parent 4d50b40 commit 4a6d31f

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7218,12 +7218,15 @@ static Value *getStartValueFromReductionResult(VPInstruction *RdxResult) {
72187218
return StartVPV->getLiveInIRValue();
72197219
}
72207220

7221-
// If \p R is a Compute{Reduction,AnyOf,FindLastIV}Result when vectorizing the
7221+
// If \p EpiResumePhiR is resume VPPhi for a reduction when vectorizing the
72227222
// epilog loop, fix the reduction's scalar PHI node by adding the incoming value
72237223
// from the main vector loop.
72247224
static void fixReductionScalarResumeWhenVectorizingEpilog(
7225-
VPRecipeBase *R, VPTransformState &State, BasicBlock *BypassBlock) {
7226-
auto *EpiRedResult = dyn_cast<VPInstruction>(R);
7225+
VPPhi *EpiResumePhiR, VPTransformState &State, BasicBlock *BypassBlock) {
7226+
// Get the VPInstruction computing the reduction result in the middle block.
7227+
// The first operand may not be from the middle block if it is not connected
7228+
// to the scalar preheader. In that case, there's nothing to fix.
7229+
auto *EpiRedResult = dyn_cast<VPInstruction>(EpiResumePhiR->getOperand(0));
72277230
if (!EpiRedResult ||
72287231
(EpiRedResult->getOpcode() != VPInstruction::ComputeAnyOfResult &&
72297232
EpiRedResult->getOpcode() != VPInstruction::ComputeReductionResult &&
@@ -7274,12 +7277,7 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72747277
// When fixing reductions in the epilogue loop we should already have
72757278
// created a bc.merge.rdx Phi after the main vector body. Ensure that we carry
72767279
// over the incoming values correctly.
7277-
using namespace VPlanPatternMatch;
7278-
assert(count_if(EpiRedResult->users(), IsaPred<VPPhi>) == 1 &&
7279-
"ResumePhi must have a single user");
7280-
auto *EpiResumePhiVPI =
7281-
cast<VPInstruction>(*find_if(EpiRedResult->users(), IsaPred<VPPhi>));
7282-
auto *EpiResumePhi = cast<PHINode>(State.get(EpiResumePhiVPI, true));
7280+
auto *EpiResumePhi = cast<PHINode>(State.get(EpiResumePhiR, true));
72837281
EpiResumePhi->setIncomingValueForBlock(
72847282
BypassBlock, MainResumePhi->getIncomingValueForBlock(BypassBlock));
72857283
}
@@ -7388,17 +7386,13 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73887386
}
73897387
}
73907388
VPBasicBlock *ScalarPH = BestVPlan.getScalarPreheader();
7391-
ArrayRef<VPBlockBase *> ScalarPreds = ScalarPH->getPredecessors();
7392-
if (!ScalarPreds.empty()) {
7389+
if (ScalarPH->getNumPredecessors() > 0) {
73937390
// If ScalarPH has predecessors, we may need to update its reduction
7394-
// resume values. If there is a middle block, it must be the first
7395-
// predecessor. Note that the first predecessor may not be the middle
7396-
// block, if the middle block doesn't branch to the scalar preheader. In
7397-
// that case, fixReductionScalarResumeWhenVectorizingEpilog will be a
7398-
// no-op.
7399-
auto *MiddleVPBB = cast<VPBasicBlock>(ScalarPreds[0]);
7400-
for (VPRecipeBase &R : *MiddleVPBB)
7401-
fixReductionScalarResumeWhenVectorizingEpilog(&R, State, BypassBlock);
7391+
// resume values.
7392+
for (VPRecipeBase &R : ScalarPH->phis()) {
7393+
fixReductionScalarResumeWhenVectorizingEpilog(cast<VPPhi>(&R), State,
7394+
BypassBlock);
7395+
}
74027396
}
74037397
}
74047398

0 commit comments

Comments
 (0)