[llvm] [LV] Transform to handle exits in the scalar loop (PR #148626)
Graham Hunter via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 5 08:06:14 PST 2025
================
@@ -3610,6 +3616,147 @@ 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();
----------------
huntergr-arm wrote:
It was the preheader generated by the old createVectorizedLoopSkeleton() code, as the check blocks were generated a bit later. At the time I wrote this much of the logic for creating check blocks was still in the old LoopVectorize code instead of properly being created in vplan. I think that's changed now, so I'll see if I can add it properly.
https://github.com/llvm/llvm-project/pull/148626
More information about the llvm-commits
mailing list