[RISCV][SLP] SLP-vectorized loop is 2.6x slower than scalar on spacemit-x100
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
__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];
}
}
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
Tagging @alexey-bataev @bababuck @lukel97 for awareness.
I'll take a look and report back if I see anything else, thanks for posting! Also fyi, Luke has a cost patch in progress related to this, but I don't think that fully explains this.
Thanks @bababuck
@lukel97 tagging you for help (particularly with x100 knowledge!). Everything checks out from what I can tell. Even if we look at making the scalar instructions cost 1/2, I think we'll still end up with a net 0 cost, so something is missing.
I recall reading that the X100 vector unit behaves more as in-order which would be penalized heavily in this small loop. Could this be the result of something like that at the micro-architectural level?
One experiment we could run is adding a bunch of unrelated scalar code to the loop and see if the vector is still performing much worse.
What is of interest is the fact that using two vector loads is cheaper, that transformation should be do-able in the SLP.
@llvm/issue-subscribers-backend-risc-v
Author: Karthika Devi C (kartcq)
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
__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];
}
}
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