https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129280
Bug ID: 129280
Summary: [14/15/16/17 Regression] wrong code at -O3: loop
interchange restarts a reduction when an IV wraps
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: jeffchen006 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
Tested with gcc version 17.0.0 20261203 (experimental), r17-7143-g592f605117e,
x86_64-pc-linux-gnu.
int printf(const char *, ...);
int b[100][8], a[8];
int main(void)
{
for (int j = 0; j < 100; j++)
for (int i = 0; i < 8; i++)
b[j][i] = 1;
for (int i = 0; i < 8; i++) {
int s = 0;
unsigned char k = 0;
for (int j = 0; j < 100; j++) {
s += b[j][i] * k + 1;
k += 128;
}
a[i] = s;
}
printf("%d\n", a[0]);
return 0;
}
$ gcc -O2 t.c && ./a.out
6500
$ gcc -O3 t.c && ./a.out
130
-fno-loop-interchange fixes it; -O1 -floop-interchange also gives 130.
Bisected to r8-5164-gfbdec14e80e, which added gimple-loop-interchange.cc; its
parent is fine. For a reduction with a constant initial value,
undo_simple_reduction makes a memory reduction of the form s = (iv != iv_init)
? a[i] : init, with m_inductions[0] as iv. Here m_inductions[0] is k = {0, +,
128} in 8 bits, which is 0 again at every even j, so s restarts there. Only j =
98, 99 are left: 1 + 129 = 130. The test needs an IV that never comes back to
its initial value.
---
Notes:
- r17-7143: -O2 6500, -O3 130, -O3 -fno-loop-interchange 6500, -O1
-floop-interchange 130. Releases: 7.5 6500; 8.5..16.2 130 at -O3.
- UB review: k += 128 converts int 256 to unsigned char (modulo, defined); s
max 6500. Sanitizers: gcc ASan+UBSan, clang ASan+UBSan, MSan, TySan -O0/-O2
clean, 6500.
- Test is reduced-min.c (`b[j][i] * k + 1`; BATCH6 ยง4 had `* (k + 1)`, same
6500/130).
- Dup refresh: PR129138 (mine, same pass, other fault); nothing new on
interchange.