https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128920

            Bug ID: 128920
           Summary: wrong code: static initializer of a
                    scalar_storage_order big-endian _Float16 is emitted as
                    0 (assemble_real flips in SImode, emits 2 bytes)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

int printf(const char *, ...);
#pragma scalar_storage_order big-endian
struct S { _Float16 x; };
#pragma scalar_storage_order default
struct S g = { 1.5f16 };
int main(void) {
  printf("%d\n", g.x == 1.5f16);
}

$ gcc -O0 t.c && ./a.out
t.s: Assembler messages:
t.s:9: Warning: value 0x00000000003e0000 truncated to 0x0000000000000000
0

Expected 1. Wrong at all levels (-O0 to -O3). The assembly has

g:
        .value  4063232

which is 0x3e0000; the assembler keeps only 0x0000.

With a static object the load is folded from the initializer at -O1 and above,
so the output depends on the level:

int printf(const char *, ...);
#pragma scalar_storage_order big-endian
struct S { _Float16 x; _Float16 y; };
#pragma scalar_storage_order default
static struct S g = { 1.5f16, 1.5f16 };
int main(void) {
  printf("%d\n", g.x == 1.5f16);
}

$ gcc -O0 t.c && ./a.out
0
$ gcc -O1 t.c && ./a.out
1

Cause: assemble_real (varasm.cc):

  unsigned int chunk_nunits = MIN (nunits, units_per);
  if (reverse)
    elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
  else
    elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
  assemble_integer (elt, chunk_nunits, align, 1);

(and the same in the loop below it). For HFmode/BFmode, nunits = 2: the value
0x3e00 is byte-swapped as a 4-byte word (0x003e0000), but only chunk_nunits = 2
bytes are emitted, so the two significant bytes are cut. Flipping in the
integer mode of chunk_nunits * BITS_PER_UNIT (int_mode_for_size (...).require
()) should fix it. The reverse path is from r6-4623-gee45a32dae2 (SSO support);
the chunk_nunits size is from r10-3247-g743a16d57a4. __bf16 and _Complex
_Float16 fields fail in the same way. A run-time store writes the right bytes
(3e 00); only static initializers are wrong. Other targets with HFmode
(aarch64, arm) are probably affected too (not tested).

Fails in all releases we tested back to 12.5 (the first release with x86
_Float16), so there is no good version to bisect from.

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.

gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release
  • [Bug middle-end/128920] New: wro... jeffchen006 at gmail dot com via Gcc-bugs

Reply via email to