[llvm] [LV] Vectorize early exit loops with stores using masking (PR #178454)
Graham Hunter via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 4 03:36:18 PDT 2026
================
@@ -4133,17 +4134,177 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
R->eraseFromParent();
}
-void VPlanTransforms::handleUncountableEarlyExits(VPlan &Plan,
- VPBasicBlock *HeaderVPBB,
- VPBasicBlock *LatchVPBB,
- VPBasicBlock *MiddleVPBB,
- UncountableExitStyle Style) {
- struct EarlyExitInfo {
- VPBasicBlock *EarlyExitingVPBB;
- VPIRBasicBlock *EarlyExitVPBB;
- VPValue *CondToExit;
- };
+struct EarlyExitInfo {
+ VPBasicBlock *EarlyExitingVPBB;
+ VPIRBasicBlock *EarlyExitVPBB;
+ VPValue *CondToExit;
+};
+
+/// Update \p Plan to mask memory operations in the loop based on whether the
+/// early exit is taken or not.
+static bool handleUncountableExitsWithSideEffects(
+ VPlan &Plan, SmallVectorImpl<EarlyExitInfo> &Exits,
+ VPBasicBlock *HeaderVPBB, VPBasicBlock *LatchVPBB, VPBasicBlock *MiddleVPBB,
+ Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT,
+ AssumptionCache *AC, VPDominatorTree &VPDT) {
+
+ // Disconnect early exiting blocks from successors, remove branches. We
+ // currently don't support multiple uses for recipes involved in creating
+ // the uncountable exit condition.
+ for (auto &Exit : Exits) {
+ if (Exit.EarlyExitingVPBB == LatchVPBB)
+ continue;
+
+ for (VPRecipeBase &R : Exit.EarlyExitVPBB->phis())
+ cast<VPIRPhi>(&R)->removeIncomingValueFor(Exit.EarlyExitingVPBB);
+ Exit.EarlyExitingVPBB->getTerminator()->eraseFromParent();
+ VPBlockUtils::disconnectBlocks(Exit.EarlyExitingVPBB, Exit.EarlyExitVPBB);
+ }
+
+ // 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.
+ SmallVector<VPInstruction *, 2> GEPs;
+ SmallVector<VPInstruction *, 8> ConditionRecipes;
+
+ std::optional<VPValue *> Cond =
+ vputils::getRecipesForUncountableExit(ConditionRecipes, GEPs, LatchVPBB);
+ if (!Cond)
+ return false;
+
+ // Find load contributing to condition.
+ VPRecipeBase *CondLoad = nullptr;
+ for (auto *Recipe : ConditionRecipes) {
+ if (match(Recipe, m_VPInstruction<Instruction::Load>(m_VPValue()))) {
+ // TODO: Support more than one load. Needs legality updates too.
+ assert(CondLoad == nullptr && "Too many condition loads");
+ CondLoad = Recipe;
+ }
+ }
+ assert(CondLoad && "Couldn't find load");
+
+ // Ensure that we are guaranteed to be able to dereference the memory used
+ // for determining the uncountable exit for the maximum possible number of
+ // scalar iterations of the loop.
+ //
+ // TODO: Support first-faulting loads in cases where we don't know whether
+ // all possible addresses are dereferenceable.
+ {
+ SmallVector<const SCEVPredicate *, 4> Predicates;
+ VPSingleDefRecipe *Load = cast<VPSingleDefRecipe>(CondLoad);
+ VPValue *Ptr = Load->getOperand(0);
+ const SCEV *PtrSCEV = vputils::getSCEVExprForVPValue(Ptr, PSE, TheLoop);
+ const DataLayout &DL = Plan.getDataLayout();
+ VPTypeAnalysis TypeInfo(Plan);
+ APInt EltSize(
+ DL.getIndexTypeSizeInBits(TypeInfo.inferScalarType(Ptr)),
+ DL.getTypeStoreSize(TypeInfo.inferScalarType(Load)).getFixedValue());
+ if (!isDereferenceableAndAlignedInLoop(
+ PtrSCEV, cast<LoadInst>(Load->getUnderlyingInstr())->getAlign(),
+ PSE.getSE()->getConstant(EltSize), TheLoop, *PSE.getSE(), DT, AC,
+ &Predicates))
+ return false;
+ }
+ // Check GEPs to see if we can link them to a widen IV recipe with a step of
+ // 1; we're only interested in contiguous accesses for the condition load
+ // right now.
----------------
huntergr-arm wrote:
The checking logic has already started migration (the check for dereferenceability was originally in `canUncountableExitConditionLoadBeMoved`; see #185323). I can make a PR to move as much as possible to VPlan if you'd prefer.
https://github.com/llvm/llvm-project/pull/178454
More information about the llvm-commits
mailing list