https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100804
Bug ID: 100804 Summary: storage order swapped with specific opt Product: gcc Version: 11.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: george.thopas at gmail dot com Target Milestone: --- /* Target: x86_64-pc-linux-gnu gcc versie 10.2.0 (Gentoo 10.2.0-r4 p5) failing: $ gcc -Wall -Wextra -O2 test.c && ./a.out Aborted passing: $ gcc -Wall -Wextra -O3 test.c && ./a.out $ gcc -Wall -Wextra -O1 test.c && ./a.out $ gcc -Wall -Wextra -O0 test.c && ./a.out no fallout with -fno-strict-aliasing -fwrapv problem goes away with adding either -fno-code-hoisting, -fno-guess-branch-probability, The example includes an unused string structure which seems to affect the problem. The odd thing is that it is never used in this code Removing the untaken else path makes the problem go away too Thanks */ #include <stddef.h> #include <stdio.h> #include <stdlib.h> #define BE __attribute__((scalar_storage_order("big-endian"))) struct _str { char str[10]; }; typedef struct _str t_str; struct _le { char is_val; union { int val; t_str str; } data; }; typedef struct _le t_le; union _be_data { int val; t_str str; } BE; typedef union _be_data t_be_data; struct _be { char is_val; t_be_data data; } BE; typedef struct _be t_be; void validate(t_be * in) { t_le chk = { is_val:1, data: { val:0x12345678 } }; t_le out = { }; out.is_val = in->is_val; if (out.is_val) out.data.val = in->data.val; else out.data.str = in->data.str; if (out.data.val != chk.data.val) abort(); } int main(void) { t_be val = { is_val:1, data: { val:0x12345678 } }; validate(&val); return 0; }