[llvm] [LV] Support strided load with a stride of -1 (PR #128718)

Mel Chen via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 2 07:00:29 PDT 2025


================
@@ -2126,8 +2126,13 @@ void VPVectorPointerRecipe::execute(VPTransformState &State) {
   Value *Ptr = State.get(getOperand(0), VPLane(0));
 
   Value *Increment = createStepForVF(Builder, IndexTy, State.VF, CurrentPart);
+  // TODO: Support non-unit-reverse strided accesses.
+  Value *Index =
+      Strided
+          ? Builder.CreateMul(Increment, ConstantInt::getSigned(IndexTy, -1))
----------------
Mel-Chen wrote:

Let me explain why we need a vector-pointer here instead of a vector-end-pointer.

In the current implementation of reverse loads (`CM_WidenReverse`), the vectorizer emits a regular load followed by a reverse.
For example, to load VF elements in reverse starting from index n (say `VF = 4`), we want to access `[n, n-1, n-2, n-3]`.
With load + reverse, we actually load `[n-3, n-2, n-1, n]`, and then reverse the vector:
```
%L  = load [n-3, n-2, n-1, n]
%RL = reverse %L
```
So in this case, the vector-end-pointer should get the pointer that points to `n-3`.

However, when using a strided load with a negative stride to implement reverse loads, it behaves more like a regular consecutive load in terms of pointer computation:
```
%RL = strided.load n, -1
```
That’s why I chose to extend vector-pointer rather than rely on vector-end-pointer.
That said, I now think it would be better to explicitly pass in the stride to vector-pointer, rather than using a `bool Strided` flag.

Alternatively, would you prefer introducing a new pointer recipe specifically for strided cases where the stride is not 1?

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


More information about the llvm-commits mailing list