Skip to content

Commit

Permalink
[VPlan] Track VectorPH during skeleton creation. (NFC)
Browse files Browse the repository at this point in the history
Split off from llvm#108378.

This ensures that the logic works even if now vector region exits.
  • Loading branch information
fhahn committed Jan 2, 2025
1 parent 1fa0302 commit 207e485
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ class InnerLoopVectorizer {
AC(AC), ORE(ORE), VF(VecWidth),
MinProfitableTripCount(MinProfitableTripCount), UF(UnrollFactor),
Builder(PSE.getSE()->getContext()), Legal(LVL), Cost(CM), BFI(BFI),
PSI(PSI), RTChecks(RTChecks), Plan(Plan) {
PSI(PSI), RTChecks(RTChecks), Plan(Plan),
VectorPHVPB(Plan.getEntry()->getSingleSuccessor()) {
// Query this against the original loop and save it here because the profile
// of the original loop header may change as the transformation happens.
OptForSizeBasedOnProfile = llvm::shouldOptimizeForSize(
Expand Down Expand Up @@ -582,6 +583,11 @@ class InnerLoopVectorizer {
virtual void printDebugTracesAtStart() {}
virtual void printDebugTracesAtEnd() {}

/// Introduces a new VPIRBasicBlock for \p CheckIRBB to Plan between the
/// vector preheader and its predecessor, also connecting the new block to the
/// scalar preheader.
void introduceCheckBlockInVPlan(BasicBlock *CheckIRBB);

/// The original loop.
Loop *OrigLoop;

Expand Down Expand Up @@ -676,6 +682,10 @@ class InnerLoopVectorizer {
BasicBlock *AdditionalBypassBlock = nullptr;

VPlan &Plan;

/// The vector preheader block of \p Plan, used as target for check blocks
/// introduced during skeleton creation.
VPBlockBase *VectorPHVPB;
};

/// Encapsulate information regarding vectorization of a loop and its epilogue.
Expand Down Expand Up @@ -2443,19 +2453,15 @@ InnerLoopVectorizer::getOrCreateVectorTripCount(BasicBlock *InsertBlock) {
return VectorTripCount;
}

/// Introduces a new VPIRBasicBlock for \p CheckIRBB to \p Plan between the
/// vector preheader and its predecessor, also connecting the new block to the
/// scalar preheader.
static void introduceCheckBlockInVPlan(VPlan &Plan, BasicBlock *CheckIRBB) {
void InnerLoopVectorizer::introduceCheckBlockInVPlan(BasicBlock *CheckIRBB) {
VPBlockBase *ScalarPH = Plan.getScalarPreheader();
VPBlockBase *VectorPH = Plan.getVectorPreheader();
VPBlockBase *PreVectorPH = VectorPH->getSinglePredecessor();
VPBlockBase *PreVectorPH = VectorPHVPB->getSinglePredecessor();
if (PreVectorPH->getNumSuccessors() != 1) {
assert(PreVectorPH->getNumSuccessors() == 2 && "Expected 2 successors");
assert(PreVectorPH->getSuccessors()[0] == ScalarPH &&
"Unexpected successor");
VPIRBasicBlock *CheckVPIRBB = Plan.createVPIRBasicBlock(CheckIRBB);
VPBlockUtils::insertOnEdge(PreVectorPH, VectorPH, CheckVPIRBB);
VPBlockUtils::insertOnEdge(PreVectorPH, VectorPHVPB, CheckVPIRBB);
PreVectorPH = CheckVPIRBB;
}
VPBlockUtils::connectBlocks(PreVectorPH, ScalarPH);
Expand Down Expand Up @@ -2544,7 +2550,7 @@ void InnerLoopVectorizer::emitIterationCountCheck(BasicBlock *Bypass) {
LoopBypassBlocks.push_back(TCCheckBlock);

// TODO: Wrap LoopVectorPreHeader in VPIRBasicBlock here.
introduceCheckBlockInVPlan(Plan, TCCheckBlock);
introduceCheckBlockInVPlan(TCCheckBlock);
}

BasicBlock *InnerLoopVectorizer::emitSCEVChecks(BasicBlock *Bypass) {
Expand All @@ -2562,7 +2568,7 @@ BasicBlock *InnerLoopVectorizer::emitSCEVChecks(BasicBlock *Bypass) {
LoopBypassBlocks.push_back(SCEVCheckBlock);
AddedSafetyChecks = true;

introduceCheckBlockInVPlan(Plan, SCEVCheckBlock);
introduceCheckBlockInVPlan(SCEVCheckBlock);
return SCEVCheckBlock;
}

Expand Down Expand Up @@ -2599,7 +2605,7 @@ BasicBlock *InnerLoopVectorizer::emitMemRuntimeChecks(BasicBlock *Bypass) {

AddedSafetyChecks = true;

introduceCheckBlockInVPlan(Plan, MemCheckBlock);
introduceCheckBlockInVPlan(MemCheckBlock);
return MemCheckBlock;
}

Expand Down Expand Up @@ -7952,7 +7958,7 @@ EpilogueVectorizerMainLoop::emitIterationCountCheck(BasicBlock *Bypass,
setBranchWeights(BI, MinItersBypassWeights, /*IsExpected=*/false);
ReplaceInstWithInst(TCCheckBlock->getTerminator(), &BI);

introduceCheckBlockInVPlan(Plan, TCCheckBlock);
introduceCheckBlockInVPlan(TCCheckBlock);
return TCCheckBlock;
}

Expand Down Expand Up @@ -8092,7 +8098,7 @@ EpilogueVectorizerEpilogueLoop::emitMinimumVectorEpilogueIterCountCheck(
Plan.setEntry(NewEntry);
// OldEntry is now dead and will be cleaned up when the plan gets destroyed.

introduceCheckBlockInVPlan(Plan, Insert);
introduceCheckBlockInVPlan(Insert);
return Insert;
}

Expand Down

0 comments on commit 207e485

Please sign in to comment.