https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100998
Bug ID: 100998
Summary: bug in experimental GCC12 with optimization '-O1',
disappears with optimization '-O0'
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: fossum at us dot ibm.com
Target Milestone: ---
(note: m, i and k are "long int", GEMM_UNROLL_M is 256, COMPSIZE is 1, and
a, c, aa, cc are of type (float *))
Here's a snippet of our code:
===============================================
for (i = 1; i < GEMM_UNROLL_M; i *= 2){
if (m & i) {
if (((m & ~(i - 1)) - i) < 0) {
fprintf(stderr, "EEK! m = %ld, i = %ld, ((m & ~(i - 1)) - i) = %ld\n",
m, i, ((m & ~(i - 1)) - i));
fflush(stderr);
}
aa = a + ((m & ~(i - 1)) - i) * k * COMPSIZE;
cc = c + ((m & ~(i - 1)) - i) * COMPSIZE;
...
[call a function using aa and cc]
}
}
===============================================
When we run with -O0, the printout does not occur, and all is well.
When we run with -O1, we see this printout:
EEK! m = 3, i = 1, ((m & ~(i - 1)) - i) = -2
The fact that we get a negative number ends up leading to a
segfault in the called function, when we try to access the
first element of the array "aa".
I would be DELIGHTED if you could help me understand that the tested
construction ((m & ~(i - 1)) - i) is somehow illegal, but I feel like
it should NEVER return a negative value, as long as i is a power of 2,
and (m & i) is not 0.
I'm building this code with GCC12 (a version provided by my colleague
Peter Bergner, and I'm hoping he will add a comment clarifying exactly
which version of your experimental GCC12 he is using.