https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127607

            Bug ID: 127607
           Summary: Erroneous uninitialization after optimisation
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luxeveningduval at gmail dot com
  Target Milestone: ---

Created attachment 65685
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65685&action=edit
File used to demonstrate error

Unsure what component this falls under, have set to the C++ frontend component.
Feel free to change if inappropriate.

Take the file error.cpp, as attached to this bug report. Using an unmodified
GCC v16.2.0 build, `g++ -O0 -std=c++26 error.cpp -Wuninitialized` will compile
fine. The program it generates should output:

```
c(): 123
c(): 456
```

when run with any input to demonstrate the output of the `c` functor at various
stages in the program. The `c` functor derives from and holds a copy of a
lambda that references a value. Attempting to increase the optimisation on this
with `g++ -O1 -std=c++26 error.cpp -Wuninitialized` produces the
`uninitialized` warning and the generated program incorrectly outputs:

```
c(): 0
c(): 0
```

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html indicates that the
following should be equivalent to `g++ -std=c++26 -Wuninitialized -Werror -O1
error.cpp`:

`g++ -std=c++26 -Wuninitialized -Werror -fauto-inc-dec -fbranch-count-reg
-fcombine-stack-adjustments -fcompare-elim -fcprop-registers -fdce -fdefer-pop
-fdse -fforward-propagate -fguess-branch-probability -fif-conversion
-fif-conversion2 -finline-functions-called-once -fipa-modref -fipa-profile
-fipa-pure-const -fipa-reference -fipa-reference-addressable -fivopts
-fmerge-constants -fmove-loop-invariants -fmove-loop-stores
-fomit-frame-pointer -freorder-blocks -fshrink-wrap -fshrink-wrap-separate
-fsplit-wide-types -fssa-backprop -fssa-phiopt -ftree-bit-ccp -ftree-ccp
-ftree-ch -ftree-coalesce-vars -ftree-copy-prop -ftree-dce
-ftree-dominator-opts -ftree-dse -ftree-forwprop -ftree-fre -ftree-phiprop
-ftree-pta -ftree-scev-cprop -ftree-sink -ftree-slsr -ftree-sra -ftree-ter
-funit-at-a-time error.cpp`

but it compiles without error and outputs correctly. Adding `-O0` to the above
changes nothing. It is not clear what changes between these two commands. All
optimisation flags that are not `-O0` have this issue. Interestingly, Clang
v23.1.0 has no such issue with `-O1` but the same problem does arise with
`-O2`.

Reply via email to