public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2
@ 2023-04-06 11:32 tomas.pecka at cesnet dot cz
  2023-04-06 11:52 ` [Bug tree-optimization/109434] [12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: tomas.pecka at cesnet dot cz @ 2023-04-06 11:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109434
           Summary: std::optional weird -Wmaybe-unitialized and behaviour
                    with -O2
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tomas.pecka at cesnet dot cz
  Target Milestone: ---

Created attachment 54816
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54816&action=edit
reproducer

Hello,

I've seen a compile warning on a code (see attachment) that looks bogus. 

  In file included from optional.cpp:2:
  In member function ‘constexpr bool std::_Optional_base_impl<_Tp,
_Dp>::_M_is_engaged() const [with _Tp = int; _Dp = std::_Optional_base<int,
true, true>]’,
      inlined from ‘constexpr bool std::optional<_Tp>::has_value() const [with
_Tp = int]’ at /usr/include/c++/12.2.1/optional:988:35,
      inlined from ‘int main()’ at optional.cpp:21:63:
  /usr/include/c++/12.2.1/optional:471:58: warning: ‘*(unsigned
char*)((char*)&optInt +
offsetof(std::optional<int>,std::optional<int>::<unnamed>.std::_Optional_base<int,
true, true>::_M_payload.std::_Optional_payload<int, true, true,
true>::<unnamed>.std::_Optional_payload_base<int>::_M_engaged))’ may be used
uninitialized [-Wmaybe-uninitialized]
    471 |       { return static_cast<const _Dp*>(this)->_M_payload._M_engaged;
}
        |                                                          ^~~~~~~~~~
  optional.cpp: In function ‘int main()’:
  optional.cpp:15:24: note: ‘*(unsigned char*)((char*)&optInt +
offsetof(std::optional<int>,std::optional<int>::<unnamed>.std::_Optional_base<int,
true,   true>::_M_payload.std::_Optional_payload<int, true, true,
true>::<unnamed>.std::_Optional_payload_base<int>::_M_engaged))’ was declared
here
     15 |     std::optional<int> optInt;
        |                        ^~~~~~

The warning is present only when compiling with -Wall -O2. When I run the
executable, I see unexpected weird output, e.g.

  catch 56
  val=72704.000000

which look like some uninitialized variable is really there. When run under
valgrind, I see reports about unitialized variables.

Code seems to be working correctly when compiled with -O1 or lower and the
executable seem to be behaving expectedly as well.
Compiling and executing under clang++ works as well regardless of the
optimization level. 
I don't have older versions of g++ (< 12) but compiling the attached code on
godbolt with g++ 11 and lower does not trigger any warning.

Interesting part is that declaring optDbl as std::optional<int> makes the code
behave correctly.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12/13 Regression] std::optional weird -Wmaybe-unitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
@ 2023-04-06 11:52 ` pinskia at gcc dot gnu.org
  2023-04-08 14:31 ` law at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-06 11:52 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-04-06
      Known to fail|                            |12.1.0
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |needs-bisection,
                   |                            |needs-reduction
            Summary|std::optional weird         |[12/13 Regression]
                   |-Wmaybe-unitialized and     |std::optional weird
                   |behaviour with -O2          |-Wmaybe-unitialized and
                   |                            |behaviour with -O2
   Target Milestone|---                         |12.3
      Known to work|                            |10.1.0, 11.1.0

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
DSE2 removes the original store:

  optInt = {};
  optInt = foo ();


  Deleted dead store: optInt = {};

Even though foo can throw ...

Confirmed.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12/13 Regression] std::optional weird -Wmaybe-unitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
  2023-04-06 11:52 ` [Bug tree-optimization/109434] [12/13 Regression] " pinskia at gcc dot gnu.org
@ 2023-04-08 14:31 ` law at gcc dot gnu.org
  2023-04-11 12:41 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: law at gcc dot gnu.org @ 2023-04-08 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |law at gcc dot gnu.org
           Priority|P3                          |P2

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12/13 Regression] std::optional weird -Wmaybe-unitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
  2023-04-06 11:52 ` [Bug tree-optimization/109434] [12/13 Regression] " pinskia at gcc dot gnu.org
  2023-04-08 14:31 ` law at gcc dot gnu.org
@ 2023-04-11 12:41 ` rguenth at gcc dot gnu.org
  2023-04-11 12:55 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-11 12:41 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Hmm, we did fix such an issue already .. let me take a look.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12/13 Regression] std::optional weird -Wmaybe-unitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
                   ` (2 preceding siblings ...)
  2023-04-11 12:41 ` rguenth at gcc dot gnu.org
@ 2023-04-11 12:55 ` rguenth at gcc dot gnu.org
  2023-04-12  6:49 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-11 12:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
So the issue is that clear_bytes_written_by doesn't handle exceptions properly
and that's thru initialize_ao_ref_for_dse.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12/13 Regression] std::optional weird -Wmaybe-unitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
                   ` (3 preceding siblings ...)
  2023-04-11 12:55 ` rguenth at gcc dot gnu.org
@ 2023-04-12  6:49 ` cvs-commit at gcc dot gnu.org
  2023-04-12  6:52 ` [Bug tree-optimization/109434] [12 " rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-04-12  6:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:6e3e708dbadaae7b504af7fc4410015624793f02

commit r13-7143-g6e3e708dbadaae7b504af7fc4410015624793f02
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Apr 11 15:06:59 2023 +0200

    tree-optimization/109434 - bogus DSE of throwing call LHS

    The byte tracking of call LHS didn't properly handle possibly
    throwing calls correctly which cases bogus DSE and in turn, for the
    testcase a bogus uninit diagnostic and (unreliable) wrong-code.

            PR tree-optimization/109434
            * tree-ssa-dse.cc (initialize_ao_ref_for_dse): Properly
            handle possibly throwing calls when processing the LHS
            and may-defs are not OK.

            * g++.dg/opt/pr109434.C: New testcase.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-unitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
                   ` (4 preceding siblings ...)
  2023-04-12  6:49 ` cvs-commit at gcc dot gnu.org
@ 2023-04-12  6:52 ` rguenth at gcc dot gnu.org
  2023-04-13  7:44 ` [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-uninitialized " tomas.pecka at cesnet dot cz
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-12  6:52 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |13.0
            Summary|[12/13 Regression]          |[12 Regression]
                   |std::optional weird         |std::optional weird
                   |-Wmaybe-unitialized and     |-Wmaybe-unitialized and
                   |behaviour with -O2          |behaviour with -O2
      Known to fail|                            |12.2.0

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed on trunk sofar.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-uninitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
                   ` (5 preceding siblings ...)
  2023-04-12  6:52 ` [Bug tree-optimization/109434] [12 " rguenth at gcc dot gnu.org
@ 2023-04-13  7:44 ` tomas.pecka at cesnet dot cz
  2023-04-17 10:45 ` cvs-commit at gcc dot gnu.org
  2023-04-17 10:47 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: tomas.pecka at cesnet dot cz @ 2023-04-13  7:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Tomáš Pecka <tomas.pecka at cesnet dot cz> ---
I can confirm that the bug now does not appear in our application code that I
simplified into attached code. Tested on commit df7f55cb2ae.
Thank you!

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-uninitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
                   ` (6 preceding siblings ...)
  2023-04-13  7:44 ` [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-uninitialized " tomas.pecka at cesnet dot cz
@ 2023-04-17 10:45 ` cvs-commit at gcc dot gnu.org
  2023-04-17 10:47 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-04-17 10:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Richard Biener
<rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:3d2210dafd872e8470e2a6ae5eea74d2669bc055

commit r12-9415-g3d2210dafd872e8470e2a6ae5eea74d2669bc055
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Apr 11 15:06:59 2023 +0200

    tree-optimization/109434 - bogus DSE of throwing call LHS

    The byte tracking of call LHS didn't properly handle possibly
    throwing calls correctly which cases bogus DSE and in turn, for the
    testcase a bogus uninit diagnostic and (unreliable) wrong-code.

            PR tree-optimization/109434
            * tree-ssa-dse.cc (initialize_ao_ref_for_dse): Properly
            handle possibly throwing calls when processing the LHS
            and may-defs are not OK.  Add mode to initialize a may-def.
            (dse_optimize_stmt): Query may-defs.

            * g++.dg/opt/pr109434.C: New testcase.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-uninitialized and behaviour with -O2
  2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
                   ` (7 preceding siblings ...)
  2023-04-17 10:45 ` cvs-commit at gcc dot gnu.org
@ 2023-04-17 10:47 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-17 10:47 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
      Known to work|                            |12.2.1
         Resolution|---                         |FIXED

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-04-17 10:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 11:32 [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2 tomas.pecka at cesnet dot cz
2023-04-06 11:52 ` [Bug tree-optimization/109434] [12/13 Regression] " pinskia at gcc dot gnu.org
2023-04-08 14:31 ` law at gcc dot gnu.org
2023-04-11 12:41 ` rguenth at gcc dot gnu.org
2023-04-11 12:55 ` rguenth at gcc dot gnu.org
2023-04-12  6:49 ` cvs-commit at gcc dot gnu.org
2023-04-12  6:52 ` [Bug tree-optimization/109434] [12 " rguenth at gcc dot gnu.org
2023-04-13  7:44 ` [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-uninitialized " tomas.pecka at cesnet dot cz
2023-04-17 10:45 ` cvs-commit at gcc dot gnu.org
2023-04-17 10:47 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).