public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99536] New: unexplained warning on "uninitialized value"
@ 2021-03-11  1:36 wuz73 at hotmail dot com
  2021-03-11 13:30 ` [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: wuz73 at hotmail dot com @ 2021-03-11  1:36 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99536
           Summary: unexplained warning on "uninitialized value"
           Product: gcc
           Version: 5.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wuz73 at hotmail dot com
  Target Milestone: ---

Although the example in
http://www.cplusplus.com/reference/random/normal_distribution/ compiled fine, I
got a warning on uninitialized value in a similar code below.

#include <random>
void foo(double);
int main()
{
  std::default_random_engine generator;
  std::normal_distribution<double> norm_dist(0,1);
  for(int i=0; i<3; i++)
    foo(norm_dist(generator));
  return 0;
}

When compiled with "g++ -std=c++11 -Wall -ffast-math -O -c foo.cpp" using
gcc5.3.1 on CentOS7.2, I got:

foo.cpp:8:30: warning: ‘norm_dist.std::normal_distribution::_M_saved’ may be
used uninitialized in this function [-Wmaybe-uninitialized]
foo(norm_dist(generator));

Note that without -O, -ffast-math, or the for-loop, the code compiled fine
without any warning.

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

* [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
@ 2021-03-11 13:30 ` redi at gcc dot gnu.org
  2021-03-11 15:09 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-03-11 13:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
GCC 5 is old and no longer supported.

This is probably a jump threading bug. The _M_saved member is only ever used if
_M_saved_available says it can be used, and that is only the case after it's
been initialized.

We could probably just initialize it on construction to avoid the false
positive warning:

--- a/libstdc++-v3/include/bits/random.h
+++ b/libstdc++-v3/include/bits/random.h
@@ -2024,12 +2024,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       explicit
       normal_distribution(result_type __mean,
                          result_type __stddev = result_type(1))
-      : _M_param(__mean, __stddev), _M_saved_available(false)
+      : _M_param(__mean, __stddev)
       { }

       explicit
       normal_distribution(const param_type& __p)
-      : _M_param(__p), _M_saved_available(false)
+      : _M_param(__p)
       { }

       /**
@@ -2166,8 +2166,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                        const param_type& __p);

       param_type  _M_param;
-      result_type _M_saved;
-      bool        _M_saved_available;
+      result_type _M_saved = 0;
+      bool        _M_saved_available = false;
     };

   /**

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

* [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
  2021-03-11 13:30 ` [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution redi at gcc dot gnu.org
@ 2021-03-11 15:09 ` redi at gcc dot gnu.org
  2021-03-11 17:53 ` cvs-commit at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-03-11 15:09 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-03-11
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

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

* [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
  2021-03-11 13:30 ` [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution redi at gcc dot gnu.org
  2021-03-11 15:09 ` redi at gcc dot gnu.org
@ 2021-03-11 17:53 ` cvs-commit at gcc dot gnu.org
  2021-03-11 18:18 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-03-11 17:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:67e397660611990efd98f9e4106c1ee81f6803a4

commit r11-7627-g67e397660611990efd98f9e4106c1ee81f6803a4
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Mar 11 16:43:51 2021 +0000

    libstdc++: Initialize std::normal_distribution::_M_saved [PR 99536]

    This avoids a false positive -Wmaybe-uninitialized warning, by
    initializing _M_saved on construction.

    libstdc++-v3/ChangeLog:

            PR libstdc++/99536
            * include/bits/random.h (normal_distribution): Use
            default-initializer for _M_saved and _M_saved_available.

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

* [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
                   ` (2 preceding siblings ...)
  2021-03-11 17:53 ` cvs-commit at gcc dot gnu.org
@ 2021-03-11 18:18 ` redi at gcc dot gnu.org
  2021-03-29 20:03 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-03-11 18:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Worked around in libstdc++ on master only for now.

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

* [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
                   ` (3 preceding siblings ...)
  2021-03-11 18:18 ` redi at gcc dot gnu.org
@ 2021-03-29 20:03 ` cvs-commit at gcc dot gnu.org
  2021-03-29 21:37 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-03-29 20:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:039fc1ff2b018aaba5203c89de64ddc02c7a5c17

commit r10-9592-g039fc1ff2b018aaba5203c89de64ddc02c7a5c17
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Mar 11 16:43:51 2021 +0000

    libstdc++: Initialize std::normal_distribution::_M_saved [PR 99536]

    This avoids a false positive -Wmaybe-uninitialized warning, by
    initializing _M_saved on construction.

    libstdc++-v3/ChangeLog:

            PR libstdc++/99536
            * include/bits/random.h (normal_distribution): Use
            default-initializer for _M_saved and _M_saved_available.

    (cherry picked from commit 67e397660611990efd98f9e4106c1ee81f6803a4)

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

* [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
                   ` (4 preceding siblings ...)
  2021-03-29 20:03 ` cvs-commit at gcc dot gnu.org
@ 2021-03-29 21:37 ` redi at gcc dot gnu.org
  2021-04-07 21:16 ` [Bug tree-optimization/99536] " msebor at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-03-29 21:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
And for 10.3 as well.

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

* [Bug tree-optimization/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
                   ` (5 preceding siblings ...)
  2021-03-29 21:37 ` redi at gcc dot gnu.org
@ 2021-04-07 21:16 ` msebor at gcc dot gnu.org
  2021-04-07 21:18 ` msebor at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-07 21:16 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
           Keywords|                            |missed-optimization
      Known to work|                            |10.2.0, 11.0
          Component|c++                         |tree-optimization

--- Comment #6 from Martin Sebor <msebor at gcc dot gnu.org> ---
I can reconfirm the warning with GCC 9.  It's been suppressed in GCC 10 and 11.

GCC 10 sees the IL below (annotated with my comments).  The warning triggers
because it doesn't know how to interpret the VIEW_CONVERT_EXPR<bool>.  This
limitation has caused other false positives as well (including the one for
std::optional in pr80635.  I suggested handling VIEW_CONVERT_EXPR in the pass
in response to the patch submitted to fix that bug and I still think that that
would be a good solution, even if other optimizations are enhanced to handle it
as well.

main ()
{
  unsigned char norm_dist$24;
  double norm_dist$_M_saved;
  result_type __ret;
  result_type __x;
  result_type __y;
  result_type __r2;
  const result_type __mult;
  result_type D.47826;
  long unsigned int __res;
  int i;
  struct normal_distribution norm_dist;
  struct default_random_engine generator;
  bool _18;
  double _20;
  double _21;
  double _23;
  double _24;
  double powmult_26;
  double powmult_27;
  bool _29;
  bool _30;
  bool _31;
  double _32;
  double _33;
  double _34;
  double _36;
  unsigned int ivtmp_41;
  unsigned int ivtmp_51;

  <bb 2> [local count: 268435456]:
  MEM[(struct linear_congruential_engine *)&generator] ={v} {CLOBBER};
  generator._M_x = 1;
  MEM[(struct  &)&norm_dist] ={v} {CLOBBER};
  MEM[(struct param_type *)&norm_dist] ={v} {CLOBBER};

  <bb 3> [local count: 805306365]:
  # norm_dist$_M_saved_2 = PHI <norm_dist$_M_saved_43(D)(2),
norm_dist$_M_saved_1(9)>
  # norm_dist$24_14 = PHI <0(2), norm_dist$24_13(9)>   <<< zero when
norm_dist$_M_saved_2 is uninitialized
  # ivtmp_41 = PHI <3(2), ivtmp_51(9)>
  _18 = VIEW_CONVERT_EXPR<bool>(norm_dist$24_14);      <<< not handled by
warning code
  if (_18 != 0)
    goto <bb 11>; [50.00%]                             <<< norm_dist$_M_saved_2
= norm_dist$_M_saved_1(9)
  else
    goto <bb 8>; [50.00%]                              <<< norm_dist$_M_saved_2
is uninitialized

  <bb 11> [local count: 402653183]:
  goto <bb 6>; [100.00%]

  <bb 8> [local count: 402653183]:

  <bb 4> [local count: 3660483495]:
  _20 = std::generate_canonical<double, 53,
std::linear_congruential_engine<long unsigned int, 16807, 0, 2147483647> >
(&generator);
  _21 = _20 * 2.0e+0;
  __x_22 = _21 - 1.0e+0;
  _23 = std::generate_canonical<double, 53,
std::linear_congruential_engine<long unsigned int, 16807, 0, 2147483647> >
(&generator);
  _24 = _23 * 2.0e+0;
  __y_25 = _24 - 1.0e+0;
  powmult_26 = __x_22 * __x_22;
  powmult_27 = __y_25 * __y_25;
  __r2_28 = powmult_26 + powmult_27;
  _29 = __r2_28 <= 1.0e+0;
  _30 = __r2_28 != 0.0;
  _31 = _29 & _30;
  if (_31 != 0)
    goto <bb 5>; [11.00%]
  else
    goto <bb 10>; [89.00%]

  <bb 10> [local count: 3257830313]:
  goto <bb 4>; [100.00%]

  <bb 5> [local count: 402653182]:
  _32 = log (__r2_28);
  _33 = _32 * -2.0e+0;
  _34 = _33 / __r2_28;
  __mult_35 = sqrt (_34);
  _36 = __x_22 * __mult_35;
  __ret_37 = __y_25 * __mult_35;

  <bb 6> [local count: 805306365]:                     <<<
norm_dist$_M_saved_2(11) == norm_dist$_M_saved_1(9)
  # __ret_39 = PHI <__ret_37(5), norm_dist$_M_saved_2(11)>
  # norm_dist$_M_saved_1 = PHI <_36(5), norm_dist$_M_saved_2(11)>
  # norm_dist$24_13 = PHI <1(5), 0(11)>
  foo (__ret_39);                                      <<<
-Wmaybe-uninitialized
  ivtmp_51 = ivtmp_41 + 4294967295;
  if (ivtmp_51 == 0)
    goto <bb 7>; [25.00%]
  else
    goto <bb 9>; [75.00%]

  <bb 9> [local count: 603979774]:
  goto <bb 3>; [100.00%]

  <bb 7> [local count: 268435456]:
  generator ={v} {CLOBBER};
  norm_dist ={v} {CLOBBER};
  return 0;

}

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

* [Bug tree-optimization/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
                   ` (6 preceding siblings ...)
  2021-04-07 21:16 ` [Bug tree-optimization/99536] " msebor at gcc dot gnu.org
@ 2021-04-07 21:18 ` msebor at gcc dot gnu.org
  2021-04-20 18:52 ` cvs-commit at gcc dot gnu.org
  2022-08-06  2:57 ` wuz73 at hotmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-07 21:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Martin Sebor <msebor at gcc dot gnu.org> ---
The IL I posted in comment #6 was before the libstdc++ change.

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

* [Bug tree-optimization/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
                   ` (7 preceding siblings ...)
  2021-04-07 21:18 ` msebor at gcc dot gnu.org
@ 2021-04-20 18:52 ` cvs-commit at gcc dot gnu.org
  2022-08-06  2:57 ` wuz73 at hotmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-04-20 18:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:29dad307b5d7cfdb6626c11c8e43ebff941c950b

commit r9-9374-g29dad307b5d7cfdb6626c11c8e43ebff941c950b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Mar 11 16:43:51 2021 +0000

    libstdc++: Initialize std::normal_distribution::_M_saved [PR 99536]

    This avoids a false positive -Wmaybe-uninitialized warning, by
    initializing _M_saved on construction.

    libstdc++-v3/ChangeLog:

            PR libstdc++/99536
            * include/bits/random.h (normal_distribution): Use
            default-initializer for _M_saved and _M_saved_available.

    (cherry picked from commit 67e397660611990efd98f9e4106c1ee81f6803a4)

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

* [Bug tree-optimization/99536] unexplained warning on "uninitialized value" in std::normal_distribution
  2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
                   ` (8 preceding siblings ...)
  2021-04-20 18:52 ` cvs-commit at gcc dot gnu.org
@ 2022-08-06  2:57 ` wuz73 at hotmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: wuz73 at hotmail dot com @ 2022-08-06  2:57 UTC (permalink / raw)
  To: gcc-bugs

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

wuz73 at hotmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #9 from wuz73 at hotmail dot com ---
Great. I think this can be closed now.

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

end of thread, other threads:[~2022-08-06  2:57 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-11  1:36 [Bug c++/99536] New: unexplained warning on "uninitialized value" wuz73 at hotmail dot com
2021-03-11 13:30 ` [Bug c++/99536] unexplained warning on "uninitialized value" in std::normal_distribution redi at gcc dot gnu.org
2021-03-11 15:09 ` redi at gcc dot gnu.org
2021-03-11 17:53 ` cvs-commit at gcc dot gnu.org
2021-03-11 18:18 ` redi at gcc dot gnu.org
2021-03-29 20:03 ` cvs-commit at gcc dot gnu.org
2021-03-29 21:37 ` redi at gcc dot gnu.org
2021-04-07 21:16 ` [Bug tree-optimization/99536] " msebor at gcc dot gnu.org
2021-04-07 21:18 ` msebor at gcc dot gnu.org
2021-04-20 18:52 ` cvs-commit at gcc dot gnu.org
2022-08-06  2:57 ` wuz73 at hotmail dot com

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).