[llvm] [LV] Transform to handle exits in the scalar loop (PR #148626)
Benjamin Maxwell via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 12 06:36:56 PST 2025
================
@@ -3514,6 +3524,151 @@ void VPlanTransforms::handleUncountableEarlyExit(VPBasicBlock *EarlyExitingVPBB,
LatchExitingBranch->eraseFromParent();
}
+bool VPlanTransforms::handleUncountableExitsInScalarLoop(VPlan &Plan) {
+ assert(!Plan.hasScalarVFOnly() &&
+ "Cannot transform uncountable exits in scalar loop");
+
+ // We can abandon a vplan entirely if we return false here, so we shouldn't
+ // crash if some earlier assumptions on scalar IR don't hold for the vplan
+ // version of the loop.
+ VPCanonicalIVPHIRecipe *IV = Plan.getVectorLoopRegion()->getCanonicalIV();
+ VPInstruction *IVUpdate = dyn_cast<VPInstruction>(IV->getBackedgeValue());
+ if (!IVUpdate)
+ return false;
+
+ SmallVector<VPRecipeBase *, 2> GEPs;
+ SmallVector<VPRecipeBase *, 8> ConditionRecipes;
+
+ std::optional<VPValue *> Cond =
+ vputils::getRecipesForUncountableExit(Plan, ConditionRecipes, GEPs);
+ if (!Cond)
+ return false;
+
+ // Check GEPs to see if we can link them to the canonical IV.
+ using namespace llvm::VPlanPatternMatch;
+ for (auto *GEP : GEPs)
+ if (!match(GEP,
+ m_GetElementPtr(m_LiveIn(),
+ m_ScalarIVSteps(m_Specific(IV), m_SpecificInt(1),
+ m_Specific(&Plan.getVF())))))
+ return false;
+
+ // Clone the condition recipes into the preheader
+ SmallDenseMap<VPRecipeBase *, VPRecipeBase *, 8> CloneMap;
+ VPBasicBlock *VectorPH = Plan.getVectorPreheader();
+ for (VPRecipeBase *R : reverse(ConditionRecipes)) {
+ VPRecipeBase *Clone = R->clone();
+ VectorPH->appendRecipe(Clone);
+ CloneMap[R] = Clone;
+ }
+
+ // Remap the cloned recipes to use the corresponding operands.
+ for (VPRecipeBase *R : ConditionRecipes) {
+ auto *Clone = CloneMap.at(R);
+ for (unsigned I = 0; I < R->getNumOperands(); ++I)
+ if (VPRecipeBase *OpR =
+ CloneMap.lookup(R->getOperand(I)->getDefiningRecipe()))
+ Clone->setOperand(I, OpR->getVPSingleValue());
+ }
+
+ // Adjust preheader GEPs to match the value they would have for the first
+ // iteration of the vector body.
+ for (auto *GEP : GEPs)
+ CloneMap.at(GEP)->setOperand(1, IV->getStartValue());
+
+ // Split vector preheader to form a new bypass block.
+ VPBasicBlock *NewPH = VectorPH->splitAt(VectorPH->end());
+ VPBasicBlock *ScalarPH = Plan.getScalarPreheader();
+
+ // Create bypass block branch.
+ VPRecipeBase *Uncountable = (*Cond)->getDefiningRecipe();
+ VPRecipeBase *PHUncountable = CloneMap.at(Uncountable);
+ VPBuilder PHBuilder(VectorPH, VectorPH->end());
+ VPValue *PHAnyOf = PHBuilder.createNaryOp(
+ VPInstruction::AnyOf, {PHUncountable->getVPSingleValue()});
+ PHBuilder.createNaryOp(VPInstruction::BranchOnCond, {PHAnyOf},
+ PHUncountable->getDebugLoc());
+ VectorPH->clearSuccessors();
+ NewPH->clearPredecessors();
+ VPBlockUtils::connectBlocks(VectorPH, ScalarPH);
+ VPBlockUtils::connectBlocks(VectorPH, NewPH);
+
+ // Modify plan so that other check blocks (e.g. SCEVs) can be attached to
+ // the correct block.
+ Plan.setEarlyExitPreheader(VectorPH);
+
+ // Fix up the resume phi in scalar preheader -- we might not have reached
+ // the calculated maximum vector tripcount, so just use the next value of IV.
+ VPBasicBlock *MiddleBlock = Plan.getMiddleBlock();
+ VPValue *VecTC = &Plan.getVectorTripCount();
+ for (VPRecipeBase &PHI : ScalarPH->phis()) {
+ VPPhi *ResumePHI = dyn_cast<VPPhi>(&PHI);
+ VPValue *EntryVal = nullptr;
+ for (unsigned I = 0; I < ResumePHI->getNumIncoming(); ++I) {
+ const VPBasicBlock *Block = ResumePHI->getIncomingBlock(I);
+ if (Block == Plan.getEntry()) {
+ EntryVal = ResumePHI->getIncomingValue(I);
+ } else if (Block == MiddleBlock) {
+ VPValue *V = ResumePHI->getIncomingValue(I);
+ if (V == VecTC) {
+ ResumePHI->setOperand(I, IVUpdate);
+ } else {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
----------------
MacDue wrote:
nit: I think the flow here can be simplified to:
```suggestion
for (unsigned I = 0; I < ResumePHI->getNumIncoming(); ++I) {
const VPBasicBlock *Block = ResumePHI->getIncomingBlock(I);
VPValue* V = ResumePHI->getIncomingValue(I);
if (Block == Plan.getEntry()) {
EntryVal = V;
} else if (Block == MiddleBlock && V == VecTC) {
ResumePHI->setOperand(I, IVUpdate);
} else {
return false;
}
}
```
https://github.com/llvm/llvm-project/pull/148626
More information about the llvm-commits
mailing list