https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124148
Bug ID: 124148
Summary: Potential faulty strict aliasing check ending up with
wrong dead store elimination (DSE)
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: zzyiwei at gmail dot com
Target Milestone: ---
Hi,
Here's a minimal reproducer (< 100 lines of c code) with details in its README:
https://github.com/zzyiwei/gcc-dse-repro
The general idea is to mimic the Vulkan (3D graphics API) struct chaining,
using a base pNext struct walker to go through chained extension structures.
GCC versions:
- GCC-15+ is more aggressive and is where the issue began to show up in the
Vulkan driver: https://gitlab.freedesktop.org/mesa/mesa/-/issues/13242
- For the reproducer, all GCC versions I have access to can repro (GCC-13,
GCC-14, GCC-15).
Sanity check:
- "-Wall -Wextra" doesn't report anything below -O2
- "-fwrapv -fno-aggressive-loop-optimizations" doesn't make a difference
- "-fno-strict-aliasing" can mitigate this (below mitigation 3 case), but I
think the code doesn't violate strict aliasing since the base struct types do
match each other.
The Good:
$ gcc -O1 gcc_dse.c -o gcc_dse
$ ./gcc_dse
sizeof_A=32
The Bad:
$ gcc -O2 gcc_dse.c -o gcc_dse
$ ./gcc_dse
Segmentation fault
Mitigation 1: -fno-tree-dse
$ gcc -O2 -fno-tree-dse gcc_dse.c -o gcc_dse
$ ./gcc_dse
sizeof_A=32
Mitigation 2: uncomment line 95: //__asm__ volatile("" : : "g"(a.pNext) :
"memory");
$ gcc -O2 gcc_dse.c -o gcc_dse
$ ./gcc_dse
sizeof_A=32
Mitigation 3: -fno-strict-aliasing
$ gcc -O2 -fno-strict-aliasing gcc_dse.c -o gcc_dse
$ ./gcc_dse
sizeof_A=32