| Issue |
242934
|
| Summary |
[LAA] `|Dist| > MaxBTC * MaxStride` reports NoDep for a partial byte overlap that SVE `-Os` then vectorizes wrong
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
A 16-iteration copy of unaligned `i32`s, 61 bytes apart, has a true dependence that LoopAccessAnalysis drops. At `-Os` on AArch64 SVE, LoopVectorize then emits one masked `<vscale x 4 x i32>` load of every lane and one masked store 61 bytes later. When the hardware vector length is at least 64 bytes (`vscale >= 4`), one chunk covers all 16 lanes. The load of iteration 15 reads the original bytes before the store of iteration 0.
Iteration 0 stores bytes `[61, 65)`. Iteration 15 loads bytes `[60, 64)`. Those ranges overlap on `[61, 64)`. Fill `a[i] = (i*17+7) mod 256`. Then the correct last stored word at offset 121 is bytes `3,7,24,41`. The vector loop writes `3,20,24,41`. First differing byte is index 122: got 20, expected 7.
`|Dist| > MaxBTC * Step` is `61 > 15 * 4`. That shows the *starts* of the accesses never land on the same byte. It does not show the 4-byte windows are disjoint. `61 % 4 != 0`, and `61 < 15*4 + 4`.
```c
#include <stdint.h>
#include <stdio.h>
typedef uint32_t __attribute__((may_alias, aligned(1))) u32a;
enum { N = 16, DIST = 61, SPAN = DIST + N * 4 };
__attribute__((noinline)) static void copy(uint8_t *a) {
for (unsigned i = 0; i < N; i++)
*(u32a *)(a + DIST + i * 4u) = *(u32a *)(a + i * 4u);
}
__attribute__((optnone, noinline)) static void ref(uint8_t *a) {
for (unsigned i = 0; i < N; i++)
*(u32a *)(a + DIST + i * 4u) = *(u32a *)(a + i * 4u);
}
int main(void) {
uint8_t g[SPAN], e[SPAN];
for (unsigned i = 0; i < SPAN; i++)
g[i] = e[i] = (uint8_t)(i * 17u + 7u);
copy(g);
ref(e);
for (unsigned i = 0; i < SPAN; i++)
if (g[i] != e[i]) {
printf("BAD at %u got=%u exp=%u\n", i, g[i], e[i]);
return 1;
}
printf("OK\n");
return 0;
}
```
```console
$ clang --target=aarch64-linux-gnu -march=armv9-a+sve -static -Os t.c
$ qemu-aarch64 -cpu max,sve-default-vector-length=64 ./a.out
BAD at 122 got=20 exp=7
$ clang ... -O0 ... && qemu-aarch64 ... # OK
```
`-O2`/`-O3` on the same target emit NEON `ldp`/`stur` of 16-byte chunks and match. `-Oz` at VL=64 matches. `-Os` with VL=16/32 matches. The bug needs the SVE tail-folded width that covers the whole trip count, which `-Os` picks when VL is 64 bytes or more.
Versions (qemu-aarch64, `-march=armv9-a+sve`, clang 15 used `armv8-a+sve`):
- clang-15.0.6 `-Os` VL=64: OK
- clang-16.0.4 / 17.0.6 / 19–24 `-Os` VL=64: fails (`BAD at 122 got=20 exp=7`)
- clang trunk snapshot `58561153d5fc` (2026-11-15): `-Os` VL=64/128 fails (`BAD at 122 got=20 exp=7`); `-Os` VL=16/32, `-O0`, `-O2`, `-Oz`, `-Os -fno-vectorize`: OK
- clang-24 `-O0`/`-O1`/`-O2`/`-O3` VL=64: OK; `-Os` VL=16/32: OK; `-Os` VL=64/128/256: fails; `-Oz` VL=64: OK; `-Os -fno-vectorize`: OK
- clang-24 `-mllvm -opt-bisect-limit=403` `-Os` VL=64: OK (stops before `loop-vectorize` on `copy`); limit 404: fails
- clang-18: not installed
- clang-24 x86-64 / gcc x86-64 / clang-24 RVV / clang-24 s390x: OK
Where: `isSafeDependenceDistance` (`LoopAccessAnalysis.cpp` lines 2861–2902 at `c74b358f44c3`, main of 2026-11-16) returns true when `|Dist| > MaxBTC * MaxStride` and takes no access size. The caller (lines 3129–3132) returns `Dependence::NoDep` on that result and never reaches the later check (line 3270):
```cpp
uint64_t MinDistanceNeeded = MaxStride * (MinNumIter - 1) + TypeByteSize;
```
For this loop that quantity is `4 * 15 + 4 = 64`. Distance 61 is below 64, so the later check would reject the dependence. `TypeByteSize` is already in the caller's tuple. The in-tree test `llvm/test/Analysis/LoopAccessAnalysis/is-safe-dep-distance-partial-overlap.ll` (`@partial_overlap_pos_dist`, trip 16, byte distance 61) still expects "Memory dependences are safe" under a FIXME from PR #230773.
Related: PR #230773 (merged 2026-10-10) added that NFC test and left the dependence reported as safe; there is no GitHub issue for the runtime SVE miscompile. PR #230860 is a different MaxBTC wrap hole. PR #242898 (NFC, merged 2026-11-15) adds more LAA FIXME tests but does not change `isSafeDependenceDistance`; the FIXME test above is unchanged on main. Not a duplicate of any of these.
Possible fix (not tested): at the `NoDep` return, do not call the distance safe unless `|Dist| >= MaxBTC * Step + TypeByteSize`, or unless `Dist` is a multiple of `TypeByteSize`.
Godbolt (asm only): https://godbolt.org/z/o37fPzK73
`-Os` shows SVE `whilelo` / `ld1w` / `st1w`; `-O0` does not (re-checked on CE clang trunk `cbfac21cd384`). CE aarch64 execute is not VL-controllable here (a CE `-Os` run printed `OK`), so the runtime mismatch `BAD at 122 got=20 exp=7` still requires local `qemu-aarch64 -cpu max,sve-default-vector-length=64` as in `repro.sh` / `versions.txt`.
cc @fhahn @david-arm
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs