https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128268
Bug ID: 128268
Summary: wrong code at -O1 and above: a struct copy stores a
nested scalar_storage_order("big-endian") member in
native byte order
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: kyuwoncho18 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
#include <stdio.h>
typedef struct __attribute__((scalar_storage_order("big-endian"))) { unsigned
x; } BE;
typedef struct { BE b; } Outer;
__attribute__((noinline)) void set_outer(Outer *p) {
Outer t = { { 0x11223344u } };
*p = t;
}
__attribute__((noinline)) unsigned get_outer(Outer *p) { return p->b.x; }
int main(void) { Outer s; set_outer(&s); printf("%08x\n", get_outer(&s));
return 0; }
godbolt: https://godbolt.org/z/e3Y9c8x9a
b.x is only written and read through the big-endian BE, and *p = t copies a
whole Outer, so the program must print 11223344, as it does at -O0 and with -O1
-fno-tree-sra. At -O1, -O2, -O3 and -Os it prints 44332211.