https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126374
Bug ID: 126374
Summary: Incorrect codegen with -O2 -fno-tree-sra, struct
containing union
Product: gcc
Version: 12.5.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: carlosgalvezp at gmail dot com
Target Milestone: ---
Created attachment 65117
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65117&action=edit
Reduced example
Hi,
We have discovered incorrect codegen on GCC 8.3.0 aarch64-linux-gnu when
disabling SRA. The problem disappears on GCC 10+, but it's unclear if it's
"just luck", the code has UB, or the bug was actually fixed. I have tried to
search in the bug portal without success, thus I wanted to check with you guys,
I hope it's OK even if it's an older compiler. I just want to understand what
the problem is.
Please find attached a reduced example. The code is compiled with:
aarch64-linux-gnu-g++ -std=c++14 -O2 -fno-tree-sra repro.cpp
If we *remove* the -fno-tree-sra flag (enable SRA), the codegen is correct.
We need to add -fno-tree-sra to resolve a different bug.
This problem only happens on aarch64, not on x86.
I don't believe there is UB in the code, but I'm happy to be corrected
otherwise. The code behaves correctly otherwise on x86 GCC, Clang and MSVC
trunk, with -O0/-O3, -Wall, -Wextra, and ASAN/UBSAN.
I have also verified that adding either of the following flags also fixes the
codegen:
-fno-strict-aliasing
-fno-ipa-sra
-fno-partial-inlining
-fno-peephole2
I suspect the issue is copying the Optional object (which contains a union)
across multiple functions, at some point the actual value is lost when SRA
is disabled.
Let me know if I can provide anything else, thanks!
PS: I tried to set the correct GCC version in the tool but it didn't let me...