[llvm] [VPlan] Extract reverse operation for reverse accesses (PR #146525)
Mel Chen via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 1 06:53:28 PST 2025
================
@@ -2866,28 +2867,42 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
TypeInfo.inferScalarType(MaxEVL), DebugLoc::getUnknown());
Builder.setInsertPoint(Header, Header->getFirstNonPhi());
- VPValue *PrevEVL = Builder.createScalarPhi(
- {MaxEVL, &EVL}, DebugLoc::getUnknown(), "prev.evl");
-
- for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
- vp_depth_first_deep(Plan.getVectorLoopRegion()->getEntry()))) {
- for (VPRecipeBase &R : *VPBB) {
- VPValue *V1, *V2;
- if (!match(&R,
- m_VPInstruction<VPInstruction::FirstOrderRecurrenceSplice>(
- m_VPValue(V1), m_VPValue(V2))))
- continue;
+ PrevEVL = Builder.createScalarPhi({MaxEVL, &EVL}, DebugLoc::getUnknown(),
+ "prev.evl");
+ }
+
+ // Transform the recipes must be converted to vector predication intrinsics
+ // even if they do not use header mask.
+ for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
+ vp_depth_first_deep(Plan.getVectorLoopRegion()->getEntry()))) {
+ for (VPRecipeBase &R : *VPBB) {
+ VPWidenIntrinsicRecipe *NewRecipe = nullptr;
+ VPValue *V1, *V2;
+ if (match(&R, m_VPInstruction<VPInstruction::FirstOrderRecurrenceSplice>(
+ m_VPValue(V1), m_VPValue(V2)))) {
VPValue *Imm = Plan.getOrAddLiveIn(
ConstantInt::getSigned(Type::getInt32Ty(Plan.getContext()), -1));
- VPWidenIntrinsicRecipe *VPSplice = new VPWidenIntrinsicRecipe(
+ NewRecipe = new VPWidenIntrinsicRecipe(
Intrinsic::experimental_vp_splice,
{V1, V2, Imm, Plan.getTrue(), PrevEVL, &EVL},
TypeInfo.inferScalarType(R.getVPSingleValue()), {}, {},
R.getDebugLoc());
- VPSplice->insertBefore(&R);
- R.getVPSingleValue()->replaceAllUsesWith(VPSplice);
- ToErase.push_back(&R);
}
+
+ // TODO: Only convert reverse to vp.reverse if it uses the result of
+ // vp.load, or defines the stored value of vp.store.
----------------
Mel-Chen wrote:
> Unconditionally replacing all the reverses to vp.reverses here means that optimizeMaskToEVL is no longer correct:
>
> ```c++
> if (match(&CurRecipe,
> m_MaskedLoad(m_VPValue(EndPtr), m_RemoveMask(HeaderMask, Mask))) &&
> match(EndPtr, m_VecEndPtr(m_VPValue(Addr), m_Specific(&Plan->getVF()))) &&
> cast<VPWidenLoadRecipe>(CurRecipe).isReverse())
> return new VPWidenLoadEVLRecipe(cast<VPWidenLoadRecipe>(CurRecipe),
> AdjustEndPtr(EndPtr), EVL, Mask);
> ```
>
> Previously if we had a masked load like
>
> ```
> headerMask: 11110000
> VPWidenLoadRecipe, reverse=true: xxxxabcd
> ```
>
> We would now generate
>
> ```
> VPWidenLoadEVLRecipe, EVL=4, reverse=true: abcdxxxx
> ```
>
> I.e. the elements aren't shifted. The test diff in this PR happens to be fine because we then unconditionally replace all reverses with vp.reverse.
Yes, there will be a brief moment where the semantics are temporarily incorrect when EVL lowering.
>
> I'd really like to avoid unconditionally replacing recipes as it makes the EVL transformation error prone and hard to follow, and this would undo what #155394 fixed. We shouldn't need to transform any recipes asides from those that use the IV step.
>
> Can we do the reverse -> vp.reverse transform in optimizeMaskToEVLRecipes alongside the load/store transforms instead?
>
> The issues you mentioned in [#146525 (comment)](https://github.com/llvm/llvm-project/pull/146525#discussion_r2502220707) can be fixed when we go to implement the permutation elimination by rewriting the transform in terms of slides instead of reverses.
We could convert reverse accesses into `Splice(VPWidenLoadEVLRecipe(VecEndPtr(ptr, evl)), poison, -evl)` inside `optimizeMaskToEVLRecipes`, and rely on the regular reverse rather than vp.reverse. The only concern is that, if we introduce simplification rules that can eliminate the reverse, there will be a temporary performance regression because the reverse access might not be lowered into VPWidenLoadEVLRecipe/VPWidenStoreEVLRecipe. However, correctness should not be affected.
If a short-term performance loss is acceptable before we allow the third operand of splice to take a variable, then I think it’s reasonable to perform this transformation directly in optimizeMaskToEVLRecipes.
> > Unconditionally replacing all the reverses to vp.reverses here means that optimizeMaskToEVL is no longer correct:
>
> I think that's kind-of already the case, right? Until all recipes are converted, the intermediate VPlan may be partially incorrect.
>
No strong opinion. I can accept the temporary semantic mismatch, though of course it would be better if we could avoid it altogether.
> For both approaches correctness boils down to whether the reverse is tied to the load/store currently. I would like to avoid correctness to depend on the exact position of the reverse. We could move the vp.reverse introduction after EVL load/store recipe creation, then asserting that the only operand/user are EVL load/store recipes.
>
> This makes me wonder if it would be easier to avoid adding them up front, but only 'materialize' the reverse operations after EVL recipes have been introduced (then we can convert `VPWidenLoadR ,reverse = true- > vp.reverse(VPWidenLoadEVLR...) ` atomically.
>
> And then just separately materialize reverse() for plain VPWidenLoad/StoreR separately, possibly for now just in `convertToConcreteRecipes`.
I hope we can extract the reverse operations earlier. This would not only make it easier to eliminate redundant reverses, but also give us the opportunity to convert remaining reverse accesses into strided accesses with a -1 stride. Mask reverses might indeed be more suitable to extract later in `executePlan`, since the cost model currently does not account for the cost of mask reverses. Doing it this way avoids affecting the middle-stage cost estimations.
Hmm, maybe I should reopen https://github.com/llvm/llvm-project/pull/123923 and do some modification.
https://github.com/llvm/llvm-project/pull/146525
More information about the llvm-commits
mailing list