| Issue |
241378
|
| Summary |
[BasicAA] separate_storage used across loop iterations causes GVN and vectorizer miscompiles
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
cjsrxzdyzds
|
A `separate_storage` assumption describes the values its two operands have when the assumption
executes. In cross-iteration mode (`AAQueryInfo::MayBeCrossIteration`) the two pointers of a query
may come from different iterations of a cycle, so a hint about one iteration's values says nothing
about one operand's value in one iteration and the other operand's value in another. BasicAA
nevertheless answered `NoAlias` in that mode.
This program has no undefined behaviour and should print 5. Built with clang 21.1.8 at `-O1` and
above, on x86-64 with no target flags, it prints 0, and so does clang's unoptimised IR after
LLVM main at `45403d3979a5`'s `-O1` to `-O3` pipelines:
```c
#include <stdio.h>
__attribute__((noinline)) long f(long *x, long *y, long n) {
long init = 7;
long *p = &init;
long sum = 0;
for (long i = 0; i < n; i++) {
long *a = (i & 1) ? y : x;
long *b = (i & 1) ? x : y;
__builtin_assume_separate_storage(a, b);
long before = *p;
*b = 0;
sum += before - *p;
p = a;
}
return sum;
}
int main(void) {
long x = 5, y = 9;
printf("%ld\n", f(&x, &y, 2));
return 0;
}
```
In the second iteration `p` holds the first iteration's `a`, which is `x`, and `b` is `x` as
well, so the store through `b` changes `*p`. GVN removes the second load anyway: `aliasPHI` asks
about `p`'s incoming value `a` in cross-iteration mode, and the separate-storage rule answers
`NoAlias` for `a` and `b`. LoopAccessAnalysis reaches the same rule. For a loop that stores
through `b` and loads through `a` with the same swap, it reports the dependences as safe, and
with `-march=znver4` the loop vectorizer reorders stores of later iterations before loads of
earlier ones.
Related: #223173 concerns per-iteration alias-scope metadata in DependenceAnalysis. This report concerns `separate_storage` operand bundles in BasicAA and includes a GVN miscompile.
Save the C program above as `repro.c` and run on x86-64 with clang 21.1.8:
```sh
clang -O0 repro.c -o repro-O0 && ./repro-O0
clang -O1 repro.c -o repro-O1 && ./repro-O1
```
Expected output: 5 from both programs. Actual: 5 at `-O0`, 0 at `-O1`.
The failure also reproduces with LLVM main `45403d3979a5` by passing clang's
unoptimized IR through each of the O1, O2 and O3 pipelines:
```sh
clang -O1 -Xclang -disable-llvm-passes -emit-llvm -S repro.c -o repro.ll
opt -S -passes='default<O1>' repro.ll -o repro.opt.ll
lli repro.opt.ll
```
This prints 0 instead of 5.
These examples were developed while checking a model of BasicAA against LLVM, with Claude Code assistance. The report was prepared with Claude Code and OpenAI Codex.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs