Issue 234136
Summary [RISCV][SLP] SLP-vectorized loop is 2.6x slower than scalar on spacemit-x100
Labels performance, llvm:SLPVectorizer
Assignees
Reporter kartcq
    On `-mcpu=spacemit-x100` , SLP vectorizes the loop below and the result runs **2.6x slower** than `-fno-slp-vectorize`, even though it
executes fewer instructions. The SLP cost model reports a solid win (`cost -8`).

Original kernel is TSVC **s116**. Shown here with stride 4, which makes the SLP tree exactly 4 wide with no scalar tail.
Note - stride 5 behaves the same way.

## Reproducer

```c
__attribute__((noinline))
void v006(int n, int *restrict a)
{
    for (int i = 0; i < n - 4; i += 4) {
        a[i]     = a[i + 1] * a[i];
        a[i + 1] = a[i + 2] * a[i + 1];
        a[i + 2] = a[i + 3] * a[i + 2];
        a[i + 3] = a[i + 4] * a[i + 3];
    }
}
```

```sh
clang -target riscv64-unknown-linux-gnu -mcpu=spacemit-x100 -mabi=lp64d -Ofast -S kernel.c
clang -target riscv64-unknown-linux-gnu -mcpu=spacemit-x100 -mabi=lp64d -Ofast -S kernel.c -fno-slp-vectorize
```

Godbolt link: https://godbolt.org/z/9xMWqbjME

## Measured on SpacemiT K3 (X100)

`n=1024`, 2M calls, checksums identical.

| build | ns/call | |
|---|---|---|
| `-fno-slp-vectorize` | 589 | 1.00x |
| default (SLP on) | 1518 | **2.6x slower** |

## Why it is might be slow

The vectorized body is a single serial dependency chain, `vle32.v` → `vslide1up.vx` → `vmul.vv` → `vse32.v`, and the loop is not
unrolled, so each iteration pays the full chain latency. 

Replacing the `vslide1up.vx` with a second `vle32.v` (so the shuffle is gone and the chain is shorter) recovers part of the loss but is still 2.2x slower
than scalar, so the shuffle alone does not explain it.

## Cost model

`-mllvm -debug-_only_=SLP`:

```
SLP:     VectorCost = 1
SLP:     ScalarCost = 4
SLP: Adding cost -6 for bundle Idx: 0, n=4 [store i32 %mul, ...]
SLP:     VectorCost = 1
SLP:     ScalarCost = 4
SLP: Adding cost -6 for bundle Idx: 1, n=4 [%mul = mul nsw i32 %1, %3, ...]
SLP: Adding cost 6 for bundle Idx: 2, n=4 [%1 = phi i32 ...]
SLP:     VectorCost = 1
SLP:     ScalarCost = 4
SLP: Adding cost -4 for bundle Idx: 3, n=4 [%3 = load i32, ...]
SLP: Extract Cost = 2.

remark: Stores SLP vectorized with cost -8 and with tree size 4
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to