| Issue |
234596
|
| Summary |
# [X86][LoopVectorize] 16x16 SAD loop ~6x slower with -march=x86-64-v3: LV vectorizes the row loop with scalarized i8 loads instead of leaving it to SLP (regression since Clang 18)
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
Pr0te1nsh4ke
|
[0001-LV-count-address-scalarization-for-scalarized-loads.patch](https://github.com/user-attachments/files/33819090/0001-LV-count-address-scalarization-for-scalarized-loads.patch)
[sad16.c](https://github.com/user-attachments/files/33819091/sad16.c)
[sad_search.c](https://github.com/user-attachments/files/33819092/sad_search.c)
## Summary
A plain C sum-of-absolute-differences loop (16x16 block, as in video motion
search) compiles to good code at `-march=x86-64` (SSE2) and with
`-mtune=skylake`. With `-march=x86-64-v3 -mtune=generic` (or `haswell`),
however, the loop vectorizer vectorizes the **row** loop across rows. Rows are
not contiguous, so every byte load is scalarized: the hot loop contains 448
`vpinsrb`. The SLP vectorizer would otherwise turn each 16-byte row into one
`vpsadbw`.
Clang 16 and 17 produced good code. Clang 18 regressed and 19 got worse. It is
still present in 23.1.0 and trunk.
## Reproducer
`sad_search.c` (attached), compiled with:
```
clang -O2 -march=x86-64-v3 -mtune=generic -S sad_search.c
```
The inner function on its own (`sad16.c`) is compiled fine. The problem only
appears once it is inlined into the search loop.
## Codegen by version (`-O2 -march=x86-64-v3 -mtune=generic`, Compiler Explorer)
| clang | vpinsrb | vpsadbw | LV remark |
|---|---|---|---|
| 12, 13 | 0 | 1 | |
| 14, 15 | 224 | 0 / 16 | |
| 16, 17.0.1 | 0 | 2 | VF 8, IC 1 (x loop not yet unrolled before LV) |
| 18.1.0 | 224 | 16 | VF 8, IC 1 (row loop, scalarized) |
| 19.1.0 – 23.1.0, trunk | 448 | 32 | VF 8, IC 2 (row loop, scalarized) |
With `-mtune=skylake`, every version from 18 to trunk gives 0 `vpinsrb` and the
LV reports "vectorization is not beneficial".
## Performance (Xeon E5-2690 v4 / Broadwell, clang 22.1.8, whole search kernel)
| build | time |
|---|---|
| `-march=x86-64` (SSE2) | 15.7 ms |
| `-march=x86-64-v3` | **85 ms** |
| `-march=x86-64-v3 -mtune=skylake` | 13.6 ms |
| `-march=x86-64-v3 -fno-vectorize` | 14.8 ms |
| `-march=x86-64-v3 -fno-vectorize -fno-slp-vectorize` | 182 ms |
## Analysis
1. **Clang 17 → 18:** in 18 the 16-iteration inner `x` loop is fully unrolled
before LV ("completely unrolled loop with 16 iterations"). In 17 it is not.
Before LV in 18 the body has 32 `load i8` and 16 `llvm.abs.i32`. The only
loop left for LV is the row loop `y`, whose loads have a row stride.
2. **LV scalarizes those strided i8 loads.** `opt-bisect` points at
`loop-vectorize`. LV's estimate is right relative to *scalar* code (85 ms
vs 182 ms). But it cannot see that SLP would vectorize the unrolled row
body much better (14 ms) if LV left the loop alone.
3. **Why tuning matters:** `X86TTIImpl::prefersVectorizedAddressing()` returns
`supportsGather()`, which is false for generic/haswell tuning on AVX2.
`LoopVectorizationCostModel::getScalarizationOverhead()` then returns early
for loads ("Some targets keep addresses scalar"), so the address part of
the scalarization cost is never counted. With `fast-gather` (skylake) it is
counted, and LV declines. Adding `+fast-gather` to a generic-tuned function
flips the decision; removing it from a skylake-tuned one flips it back. i8
gathers are never legal, so the gather path itself is not involved.
## Experimental patch
Dropping that early return (attached
`0001-LV-count-address-scalarization-for-scalarized-loads.patch`, against
22.1.8) fixes the case: LV declines, SLP emits `vpsadbw`, and the kernel goes
from 91.6 to 17.1 ms. It is not proposed as-is:
- **Effect on a 23-kernel benchmark** (x86-64-v3, generic tuning): only two
functions changed codegen, this one and one neutral (+0.2%).
- **Effect on LLVM's own tests:** 5 tests in `Transforms/LoopVectorize/X86`
change. `cost-model.ll` (corei7-avx) and `replicating-load-store-costs.ll`
stop vectorizing some loops, `fixed-order-recurrence.ll` uses IC 1 instead
of 2, and `interleave-cost.ll` and
`interleaved-accesses-hoist-load-across-store.ll` produce different IR with
the same VF and IC. Whether those changes are improvements needs maintainer
judgment.
Alternative directions: make the full-unroll decision vectorization-aware
again (what changed in 18), or have LV cost scalarized strided loads in a
loop whose body SLP could vectorize along the contiguous dimension.
## Workarounds
- `-mtune=skylake`.
- `#pragma clang loop vectorize(disable)` on the row (`y`) loop inside `sad16`.
- `-mprefer-vector-width=128` (costs ~15% elsewhere).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs