https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129258
Bug ID: 129258
Summary: -fcompare-debug failure with fixed-point constants:
CONST_FIXED rtx dump prints uninitialized padding of
fixed_value (AVR)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: compare-debug-failure
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: lzy20000419 at outlook dot com
Target Milestone: ---
Host: x86_64-pc-linux-gnu
Target: avr
Fails with an avr cross trunk (gcc 17.0.0 20261203, 799c1184b136) and with
every tested release from 7.3.0 to 16.2.0. Target: avr (any target with
fixed-point support).
volatile _Accum vout;
volatile _Sat _Accum vsat;
_Accum mix(_Accum a, _Fract f) {
_Accum t = a * 2.0k + (_Accum)f;
t -= 0.25k;
if (t > 10.0k) t = 10.0k;
return t;
}
_Sat _Accum sat_ops(_Sat _Accum s, int i) {
s += (_Sat _Accum)(i * 0.5k);
s *= 1.5k;
if (i & 1) s -= 0.125k;
return s;
}
_Fract fr(_Fract x) {
_Fract y = x * 0.5r + 0.25r;
if (y < 0.0r) y = -y;
return y;
}
int main(void) {
_Accum acc = 0.5k;
_Sat _Accum s = 1.0k;
for (int i = 0; i < 20; i++) {
acc = mix(acc, fr((i & 3) * 0.1r));
s = sat_ops(s, i - 8);
_Accum d = acc / 1.25k;
vout = d;
vsat = s;
}
return (int)(vout > 0.0k) ^ (int)(vsat == 0.0k);
}
avr-gcc -mmcu=atmega328p -O2 -fcompare-debug -S t.c
avr-gcc: error: t.c: '-fcompare-debug' failure
Expected: success. -O0, -O1, -O2 and -Os all fail. The generated assembly is
the same with and without -g. Only the -fdump-final-insns dumps differ, and
only in some const_fixed operands, for example:
-g0: (const_fixed:SA 327681 [0x50001] 0 [0] 140724603453463
[0x7ffd00000017])
-g: (const_fixed:SA 327681 [0x50001] 0 [0] 140728898420759
[0x7ffe00000017])
The low 32 bits of the third word are the mode (0x17 is SAmode). The high 32
bits look like stack addresses and change from run to run with ASLR. With the
trunk build the -O2 command failed in 13 of 20 runs, and it succeeded in all 20
runs under setarch -R (ASLR disabled). A smaller testcase, int f (_Accum t) {
return t > 10.0k; }, fails the same way but less often (14 of 20 runs). The
constant that shows garbage is 327681, which is 10.0k plus one ulp.
Godbolt (avr gcc 16.2.0): https://godbolt.org/z/fqehh1cz8
Cause: CONST_FIXED has format "www" in rtl.def, and print_rtx prints every 'w'
operand as a raw HOST_WIDE_INT. The third word holds fixed_value::mode, a
4-byte scalar_mode_pod, followed by 4 bytes of padding.
const_fixed_from_fixed_value (emit-rtl.cc) copies the whole struct from its
by-value argument (fixed->u.fv = value), so the padding comes from a stack
temporary and is not initialized. The constants compare equal
(const_fixed_hasher uses fixed_identical), so code generation does not seem to
be affected, but every RTL dump that prints such a constant depends on stack
contents.
The problem seems to be related to the raw printing of the third word in
print_rtx, or to the copy of the padding in const_fixed_from_fixed_value. I
have not tried a fix.
Related: PR127188 is a different AVR -fcompare-debug failure. Its testcase
compiles cleanly with the same trunk build.
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.