| Issue |
242949
|
| Summary |
[LV] VPlan-native outer-loop path skips memory-dependence legality and miscompiles a carried RAW
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
Pass: `loop-vectorize` (VPlan-native outer-loop path). Requires the experimental opt-in `-mllvm -enable-vplan-native-path` (default `"false"` in `VectorizeOptions.td`). `#pragma clang loop vectorize(enable)` does not vectorize this outer loop without that flag.
`LoopVectorizationLegality::canVectorize` returns success for every non-innermost loop once `canVectorizeOuterLoop()` returns true, and skips the remaining legal checks that support only innermost loops — including `canVectorizeMemory`. `canVectorizeOuterLoop` checks widenable types, swifterror, operand bundles, atomics/volatiles, terminator shape, uniform branches, nested-loop latch exits, and outer inductions; it never calls `canVectorizeMemory`. The inner-loop path does.
The loop is a distance-1 RAW on the outer IV: iteration `i` reads `A[(i-1)*m+j]`, which iteration `i-1` just wrote. A VF-4 chunk of `i = {1,2,3,4}` gathers all four previous rows from the original array and scatters `+ 1`. Row 0 is never written, so row 1 is accidentally correct. Rows 2–4 are `original previous row + 1`, not `updated previous row + 1`.
```c
#include <stdio.h>
static unsigned A[10];
__attribute__((noinline)) static void carry(unsigned n, unsigned m) {
unsigned i, j;
#pragma clang loop vectorize(enable) vectorize_width(4)
for (i = 1; i < n; ++i)
for (j = 0; j < m; ++j)
A[i * m + j] = A[(i - 1u) * m + j] + 1u;
}
int main(void) {
unsigned i;
volatile unsigned n = 5, m = 2;
for (i = 0; i < 10; ++i)
A[i] = i * 17u + 11u;
carry(n, m);
/* Scalar rows: 11 28 | 12 29 | 13 30 | 14 31 | 15 32 */
printf("%u %u %u %u %u %u %u %u %u %u\n", A[0], A[1], A[2], A[3], A[4],
A[5], A[6], A[7], A[8], A[9]);
return 0;
}
```
```console
$ clang -O0 -mllvm -enable-vplan-native-path t.c && ./a.out
11 28 12 29 13 30 14 31 15 32
$ clang -O2 -mllvm -enable-vplan-native-path t.c && ./a.out
11 28 12 29 46 63 80 97 114 131
$ clang -O2 t.c && ./a.out
11 28 12 29 13 30 14 31 15 32
```
`-Rpass=loop-vectorize` with the flag reports `vectorized outer loop (vectorization width: 4, interleaved count: 1)`. After vectorize, one gather/scatter covers outer iterations 1..4. Opt-bisect on clang-24 `-O2` with the flag: limit 191 OK (`inject-tli-mappings` on `carry`); limit 192 fails (`loop-vectorize` on `carry`). `opt -passes=loop-vectorize -enable-vplan-native-path` alone is sufficient.
Where (`LoopVectorizationLegality.cpp` lines 2019–2032 at `c74b358f44c3`, main of 2026-11-16; unchanged since `cf44dd9e317e`):
```cpp
// LoopVectorizationLegality.cpp — canVectorize
if (!TheLoop->isInnermost()) {
...
if (!canVectorizeOuterLoop()) { ... return false; }
LLVM_DEBUG(dbgs() << "LV: We can vectorize this outer loop!\n");
return Result; // skips canVectorizeMemory
}
```
Versions (all failing lines are `11 28 12 29 46 63 80 97 114 131`; flag `-mllvm -enable-vplan-native-path` unless noted):
- clang 15.0.6 / 16.0.4 / 17.0.6 / 19–23: `-O0`/`-O1`/`-Oz` OK; `-O2`/`-O3`/`-Os` FAIL
- clang-24: `-O0` OK; `-O1`/`-O2`/`-O3`/`-Os` FAIL; `-Oz` OK (refuses vectorize); `-O2` without the flag OK
- clang trunk snapshot `58561153d5fc` (2026-11-15): same results (`-O1`/`-O2`/`-O3`/`-Os` FAIL with the flag; `-O0`, `-Oz`, `-O2` without the flag OK)
- clang-18: not installed
- gcc `-O0`/`-O2`: OK
- Cross (clang-24, same bad line): RVV `-O2`/`-Os`, SVE `-O2`/`-Os`, s390x z15 `-O2`/`-Os` all FAIL with the flag
Related but different: closed PR #226212 rejects non-simple outer-loop memory accesses (atomics/volatiles); it does not add dependence legality. #62065 is a performance investigation of the flag. #45303 (closed) was a bad vec.phi, not a memory dependence. Closed PR #189979 proposed removing the VPlan-native path and notes that LoopAccessAnalysis does not handle outer loops; it was not merged and the path is still reachable with the flag. No open/closed issue for this carried RAW under the VPlan-native path.
Possible fix (not tested): do not return success from `canVectorize` for a non-innermost loop until memory-dependence legality accepts the nest; if those checks still cannot handle outer loops, refuse the loop the way an inner loop with a carried conflict is refused.
Godbolt (executed): https://godbolt.org/z/je6dbP4vj
Uses the experimental flag **`-mllvm -enable-vplan-native-path`** (not an older `-vplan-native` spelling). CE clang trunk (`cbfac21cd384`, re-run 2026-11-16): `-O0` with the flag → `11 28 12 29 13 30 14 31 15 32`; `-O2` with the flag → `11 28 12 29 46 63 80 97 114 131`; `-O2` without the flag → scalar-correct line. Matches local `versions.txt`.
cc @fhahn @artagnon
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