public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization
@ 2024-02-13  0:00 noobie-iv at mail dot ru
  2024-02-13  0:05 ` [Bug tree-optimization/113896] [12 Regression] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: noobie-iv at mail dot ru @ 2024-02-13  0:00 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113896
           Summary: Assigning array elements in the wrong order after
                    floating point optimization
           Product: gcc
           Version: 12.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: noobie-iv at mail dot ru
  Target Milestone: ---

Created attachment 57404
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57404&action=edit
Test program and build_and_run script

/* file: f.cpp */
extern double a1; // 1.0
extern double a2; // 1.0

void f(double K[2], bool b)
{
    double A[] = {
        b ? a1 : a2,
        0,
        0,
        0
    };

    double sum{};
    for(double  a : A) sum += a;
    for(double& a : A) a /= sum;

    if (b) {
        K[0] = A[0]; // 1.0
        K[1] = A[1]; // 0.0
    } else {
        K[0] = A[0] + A[1];
    }
}

/* file: main.cpp */
#include <iostream>

double a1 = 1.0;
double a2 = 1.0;

void f(double K[2], bool b);

int main()
{
    double K[2]{};
    f(K, true);
    std::cout << K[0] << "\t" << K[1] << "\n";
}

Bug: Returns different results when compiled with different optimization
settings:
g++ -O2 -ffast-math f.cpp main.cpp -o good
g++ -O3 -ffast-math f.cpp main.cpp -o bad
./good outputs "1 0"
./bad outputs "0 1"

The bug is reproduced in systems:
* Fedora-37-1.7 x64 with latest gcc autoupdate (12.3.1 20230508)
* Debian-12.2.0 x64 with gcc-12.3.1 build from sources

In the gcc-12 branch:
* Last good commit: d127348d7711e148e5ddd205a8c3409b37fae64c (12.2.1 20221017)
* First bad commit: fe7d74313736b8e1c30812bc49419f419bdf1c53 (12.2.1 20221017)
* Last tested bad commit: 4ced4ca95001f1583623c801c9c3642224a2c4f0 (12.3.1
20240210)

In the gcc-13 branch - There is no bug:
* Last tested good commit: c891d8dc23e1a46ad9f3e757d09e57b500d40044 (13.2.0)
In the gcc-14 branch - There is no bug:
* Last tested good commit: cff174fabd6c980c09aee95db1d9d5c22421761f (14.0.1
20240210)

Note: Function f() is a simplified version of the
XSpline::linearCombinationFor() function from the scantailor-experimental
project:
https://github.com/ImageProcessing-ElectronicPublications/scantailor-experimental/blob/main/src/math/XSpline.cpp
Now the bug has been temporarily resolved by declaring array A volatile.

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
@ 2024-02-13  0:05 ` pinskia at gcc dot gnu.org
  2024-02-13  0:16 ` [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841 pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-13  0:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |12.4
            Summary|Assigning array elements in |[12 Regression] Assigning
                   |the wrong order after       |array elements in the wrong
                   |floating point optimization |order after floating point
                   |                            |optimization
           Keywords|                            |wrong-code
          Component|c++                         |tree-optimization
      Known to fail|                            |12.3.0
      Known to work|                            |11.4.0, 12.1.0, 12.2.0

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
  2024-02-13  0:05 ` [Bug tree-optimization/113896] [12 Regression] " pinskia at gcc dot gnu.org
@ 2024-02-13  0:16 ` pinskia at gcc dot gnu.org
  2024-02-13  8:42 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-13  0:16 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12 Regression] Assigning   |[12 Regression] Assigning
                   |array elements in the wrong |array elements in the wrong
                   |order after floating point  |order after floating point
                   |optimization                |optimization since r12-8841

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
r12-8841-gfe7d74313736b8

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
  2024-02-13  0:05 ` [Bug tree-optimization/113896] [12 Regression] " pinskia at gcc dot gnu.org
  2024-02-13  0:16 ` [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841 pinskia at gcc dot gnu.org
@ 2024-02-13  8:42 ` rguenth at gcc dot gnu.org
  2024-02-13  8:43 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-02-13  8:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu.org
           Keywords|                            |needs-bisection

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
what fixed it?

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
                   ` (2 preceding siblings ...)
  2024-02-13  8:42 ` rguenth at gcc dot gnu.org
@ 2024-02-13  8:43 ` rguenth at gcc dot gnu.org
  2024-02-13 12:36 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-02-13  8:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-02-13
           Keywords|needs-bisection             |
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
           Priority|P3                          |P2

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
Hmm, OK, it was a backport..  I'll see.

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
                   ` (3 preceding siblings ...)
  2024-02-13  8:43 ` rguenth at gcc dot gnu.org
@ 2024-02-13 12:36 ` rguenth at gcc dot gnu.org
  2024-02-13 12:42 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-02-13 12:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
I have a bit of a deja-vu here.  The SLP permute optimization phase was
rewritten for GCC 13, the following fixes the latent issue for GCC 12:

diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index af477c31aa3..b3e3d9e7009 100644
--- a/gcc/tree-vect-slp.cc
+++ b/gcc/tree-vect-slp.cc
@@ -4058,6 +4058,15 @@ vect_optimize_slp (vec_info *vinfo)
                {
                  /* Preserve the special VEC_PERM we use to shield existing
                     vector defs from the rest.  But make it a no-op.  */
+                 auto_vec<stmt_vec_info, 64> saved;
+                 saved.create (SLP_TREE_SCALAR_STMTS (old).length ());
+                 for (unsigned i = 0;
+                      i < SLP_TREE_SCALAR_STMTS (old).length (); ++i)
+                   saved.quick_push (SLP_TREE_SCALAR_STMTS (old)[i]);
+                 for (unsigned i = 0;
+                      i < SLP_TREE_SCALAR_STMTS (old).length (); ++i)
+                   SLP_TREE_SCALAR_STMTS (old)[i]
+                     = saved[SLP_TREE_LANE_PERMUTATION (old)[i].second];
                  unsigned i = 0;
                  for (std::pair<unsigned, unsigned> &p
                       : SLP_TREE_LANE_PERMUTATION (old))

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
                   ` (4 preceding siblings ...)
  2024-02-13 12:36 ` rguenth at gcc dot gnu.org
@ 2024-02-13 12:42 ` cvs-commit at gcc dot gnu.org
  2024-02-13 12:48 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-02-13 12:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from GCC 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:4a1cd5560b9b545eb848eb1d1e06d345fb606f76

commit r14-8957-g4a1cd5560b9b545eb848eb1d1e06d345fb606f76
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Feb 13 13:39:29 2024 +0100

    tree-optimization/113896 - testcase for fixed PR

    The SLP permute optimization rewrite fixed this.

            PR tree-optimization/113896
            * g++.dg/torture/pr113896.C: New testcase.

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
                   ` (5 preceding siblings ...)
  2024-02-13 12:42 ` cvs-commit at gcc dot gnu.org
@ 2024-02-13 12:48 ` cvs-commit at gcc dot gnu.org
  2024-02-14 11:43 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-02-13 12:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

https://gcc.gnu.org/g:8a93dc34e90cd8e2bb073f8fd48f671aea62d965

commit r13-8323-g8a93dc34e90cd8e2bb073f8fd48f671aea62d965
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Feb 13 13:39:29 2024 +0100

    tree-optimization/113896 - testcase for fixed PR

    The SLP permute optimization rewrite fixed this.

            PR tree-optimization/113896
            * g++.dg/torture/pr113896.C: New testcase.

    (cherry picked from commit 4a1cd5560b9b545eb848eb1d1e06d345fb606f76)

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
                   ` (6 preceding siblings ...)
  2024-02-13 12:48 ` cvs-commit at gcc dot gnu.org
@ 2024-02-14 11:43 ` cvs-commit at gcc dot gnu.org
  2024-02-14 11:43 ` rguenth at gcc dot gnu.org
  2024-02-15  3:16 ` noobie-iv at mail dot ru
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-02-14 11:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from GCC 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:2f16c53558d01135f0f78cf78a2f722b774684d7

commit r12-10155-g2f16c53558d01135f0f78cf78a2f722b774684d7
Author: Richard Biener <rguenther@suse.de>
Date:   Tue Feb 13 13:43:44 2024 +0100

    tree-optimization/113896 - reduction of permuted external vector

    The following fixes eliding of the permutation of a BB reduction
    of an existing vector which breaks materialization of live lanes
    as we fail to permute the SLP_TREE_SCALAR_STMTS vector.

            PR tree-optimization/113896
            * tree-vect-slp.cc (vect_optimize_slp): Permute
            SLP_TREE_SCALAR_STMTS when eliding a permuation in a
            VEC_PERM node we need to preserve because it wraps an
            extern vector.

            * g++.dg/torture/pr113896.C: New testcase.

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
                   ` (7 preceding siblings ...)
  2024-02-14 11:43 ` cvs-commit at gcc dot gnu.org
@ 2024-02-14 11:43 ` rguenth at gcc dot gnu.org
  2024-02-15  3:16 ` noobie-iv at mail dot ru
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-02-14 11:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

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

* [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841
  2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
                   ` (8 preceding siblings ...)
  2024-02-14 11:43 ` rguenth at gcc dot gnu.org
@ 2024-02-15  3:16 ` noobie-iv at mail dot ru
  9 siblings, 0 replies; 11+ messages in thread
From: noobie-iv at mail dot ru @ 2024-02-15  3:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from noobie-iv at mail dot ru ---
I confirm that the original bug in the scantailor-experimental project was also
fixed by commit 2f16c53558d01135f0f78cf78a2f722b774684d7.

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

end of thread, other threads:[~2024-02-15  3:16 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-13  0:00 [Bug c++/113896] New: Assigning array elements in the wrong order after floating point optimization noobie-iv at mail dot ru
2024-02-13  0:05 ` [Bug tree-optimization/113896] [12 Regression] " pinskia at gcc dot gnu.org
2024-02-13  0:16 ` [Bug tree-optimization/113896] [12 Regression] Assigning array elements in the wrong order after floating point optimization since r12-8841 pinskia at gcc dot gnu.org
2024-02-13  8:42 ` rguenth at gcc dot gnu.org
2024-02-13  8:43 ` rguenth at gcc dot gnu.org
2024-02-13 12:36 ` rguenth at gcc dot gnu.org
2024-02-13 12:42 ` cvs-commit at gcc dot gnu.org
2024-02-13 12:48 ` cvs-commit at gcc dot gnu.org
2024-02-14 11:43 ` cvs-commit at gcc dot gnu.org
2024-02-14 11:43 ` rguenth at gcc dot gnu.org
2024-02-15  3:16 ` noobie-iv at mail dot ru

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