[llvm] [VPlan] Simplify reverse(reverse(x)) -> x (PR #199057)

Mel Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 4 08:01:22 PDT 2026


Mel-Chen wrote:

> > #199510 This POC is the approach I mentioned in [#196900 (comment)](https://github.com/llvm/llvm-project/pull/196900#issuecomment-4439787534). I hope we can complete the simplification of reverse before optimizeEVLMasks. This will make it much easier for us in the future when transforming reverse accesses into strided accesses with a stride of -1.
> 
> From [#200794 (comment)](https://github.com/llvm/llvm-project/pull/200794#issuecomment-4611365817), it sounds like we now want to run permutation simplification after optimizeEVLMasks?
> 
> Although I'm not sure why transforming reverse accesses into strided accesses with a stride of -1 would be more difficult after `simplifyReverses`. Wouldn't we just be matching `(vp.reverse (vp.load end-ptr, evl), evl)`?

I think there might be a misunderstanding here. Let me break down the workflow as I envision it:
1. A transformation to eliminate redundant `vector.reverse` operations (e.g., reverse(reverse(x)) -> x).
2. `VPlanTransforms::convertToStridedAccesses`: Based on the cost, it decides whether to convert reverse access patterns into strided accesses with a stride of -1, and clamps the VF of plan.
3. (For EVL tail folding) As proposed in #199510, `optimizeMaskToEVL` will no longer depend on the reverse operation of the loaded result or stored value. Therefore, even if reverse is eliminated in step 1, it won't affect EVL lowering. At this stage, splice operations will be generated, some of which might be redundant.
4. (For EVL tail folding) A transformation to eliminate redundant splice operations, which cleans up the extra splices introduced in step 3. (e.g. splice.right(splice.left(poison, val, evl), poison, evl) -> val)

The misunderstanding likely stems from the fact that I grouped both transformations (1 and 4) under `permutation elimination` and they can be implemented in a single transformation, and just need to call it two times.

Hopefully, this clarifies my perspective. :)

https://github.com/llvm/llvm-project/pull/199057


More information about the llvm-commits mailing list