https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128351
Bug ID: 128351
Summary: [14/15/16/17 Regression] wrong code with early-break
vectorization when the main exit count wraps (-O3
-fno-ivopts -mavx2)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: lzy20000419 at outlook dot com
Target Milestone: ---
Created attachment 66067
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66067&action=edit
testcase
```c
__attribute__((noipa)) unsigned f(unsigned long b, unsigned long e, int lim) {
unsigned s = 0; int k = 0;
unsigned long i = b;
do { s += (unsigned)i; k++; if (k > lim) break; i -= 3; } while (i != e);
return s + (unsigned)k;
}
int main(void) {
unsigned r = f(1, 1, 1000);
__builtin_printf("%u\n", r);
return 0;
}
```
```
$ gcc -O0 t.c && ./a.out
4293467798
$ gcc -O3 -fno-ivopts -mavx2 t.c && ./a.out
4294967228
```
Correct value is 4293467798 (clang, gcc -O0, gcc -O3 without -mavx2).
`-fsanitize=undefined,address` clean.
Local recheck on Debian 14.2.0-19: still prints 4294967228. Also fails with
`-march=x86-64-v3` / `-mavx512f`,
and with `-O2 -ftree-vectorize -fno-ivopts -mavx2`. Hidden by
`-fno-tree-loop-vectorize` or `-fwrapv`.
On AArch64, `-O3 -fno-ivopts` prints 4294967286 on GCC 14.1, 14.2, 15.2, 16.1
and trunk (12.5, 13.1 and 13.4
print 4293467798). Trunk `-O3 -fno-ivopts -mavx512f` prints 4294966968, not the
AVX2 value 4294967228.
Godbolt: https://godbolt.org/z/7jYGhTevj (trunk and 13.4 `-O3 -fno-ivopts
-mavx2`, clang 23.1.0 with the same
flags, AArch64 trunk and 13.4 `-O3 -fno-ivopts`, trunk `-mavx512f`).
Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.