-Wstrict-aliasing=3 non-diagnosed union type punning under -O2
0 reputation · 24 Oct 2021, 14:34 UTC
When profiling C performance bottlenecks involving type-punned pointers, compiler strict-aliasing assumptions under -O2/-O3 can silently restructure loads and stores, potentially obscuring whether measured cycles-instructions reflect genuine hotspots or aliasing-driven optimizations. GCC and Clang assume pointers of incompatible types do not alias per C99 6.5/7, enabling transformations such as redundant load elimination that alter instruction counts and cache behavior. The -Wstrict-aliasing warning levels (1–3) provide partial diagnostics, but level 3 frequently misses union-based punning patterns that C99 6.5.2.3/3 explicitly permits, creating a diagnostic gap between compiler assumptions and source intent. UBSan strict-aliasing instrumentation reports violations at runtime with approximately 2–3× overhead and does not cover code compiled without sanitization, such as system allocators. The -fno-strict-aliasing flag disables these assumptions globally, costing 5–15% performance in pointer-heavy code (e.g., network packet parsing) but lacks a standard per-function or per-pointer disable mechanism. Which profiling approach reliably isolates true hotspots without perturbing aliasing behavior or incurring prohibitive overhead? How does -fno-strict-aliasing interact with link-time optimization across GCC 13 and Clang 17? Does restrict qualify on destination pointers recover auto-vectorization while preserving intended aliasing assumptions?