public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector
@ 2023-06-08 20:48 lakin at vividtheory dot com
  2023-06-08 21:00 ` [Bug target/110182] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: lakin at vividtheory dot com @ 2023-06-08 20:48 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110182
           Summary: GCC generates incorrect results for simple Eigen Casts
                    / Subtractions at -O2 or above for a 3 dimensional
                    vector
           Product: gcc
           Version: 13.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lakin at vividtheory dot com
  Target Milestone: ---

Created attachment 55286
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55286&action=edit
The minimal reproducing example

I am attaching a simple example program that shows how gcc is getting an
incorrect result for a simple program which is attempting to represent a 1D
matrix of doubles as two floating point matrices, one for the closest value as
a float and one for the remainder.  

gcc 11 gets correct results at -O1 and -O2, but not -O3. gcc 12/13 get
incorrect results at -O2 and -O3, but not at -O1.

I've attached the source file below, and am more than happy to provide more
information / intermediate outputs. However, one can reproduce this even at
godbolt: https://godbolt.org/z/j7P5of7xo 

More info here:
https://gist.github.com/lakinwecker/9ef9dbde94c018a33f4c33822c6d93ad

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

* [Bug target/110182] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
@ 2023-06-08 21:00 ` pinskia at gcc dot gnu.org
  2023-06-08 21:01 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-08 21:00 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |target
            Summary|GCC generates incorrect     |Vector(2) cast from double
                   |results for simple Eigen    |to float and subtraction
                   |Casts / Subtractions at -O2 |seems to produce incorrect
                   |or above for a 3            |results at -O2 and above
                   |dimensional vector          |

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Looks like a target issue:
The gimple level looks like this in GCC 12 for low:
  vect__178.810_465 = (vector(2) float) _165;
  vect__202.814_467 = _165 - _165;
  vect__203.815_466 = (vector(2) float) vect__202.814_467;
  _172 = SR.808_30 * 6.371e+6;
  MEM[(struct plain_array *)&high] ={v} {CLOBBER};
  MEM <vector(2) float> [(float &)&high] = vect__178.810_465;
  _184 = (float) _172;
  MEM[(float &)&high + 8] = _184;
  MEM[(struct plain_array *)&low] ={v} {CLOBBER};
  MEM <vector(2) float> [(float &)&low] = vect__203.815_466;


While for GCC 11 we have the casting as scalar:
  _173 = (float) _298;
  MEM[(float &)&high] = _173;
  _296 = BIT_FIELD_REF <_160, 64, 64>;
  _177 = (float) _296;
  MEM[(float &)&high + 4] = _177;
  _179 = (float) _167;
  MEM[(float &)&high + 8] = _179;
  MEM[(struct plain_array *)&low] ={v} {CLOBBER};
  _192 = (double) _173;
  _197 = _298 - _192;
  _198 = (float) _197;
  MEM[(float &)&low] = _198;
  _224 = (double) _177;
  _229 = _296 - _224;
  _230 = (float) _229;
  MEM[(float &)&low + 4] = _230;
  _208 = (double) _179;
  _213 = _167 - _208;
  _214 = (float) _213;
  MEM[(float &)&low + 8] = _214;

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

* [Bug target/110182] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
  2023-06-08 21:00 ` [Bug target/110182] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above pinskia at gcc dot gnu.org
@ 2023-06-08 21:01 ` pinskia at gcc dot gnu.org
  2023-06-08 21:03 ` [Bug target/110182] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-08 21:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>but not -O3.

Oh that is because the vectorizer is not enabled at -O2 in GCC 11 but is for
GCC 12+.

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

* [Bug target/110182] [11/12/13/14 Regression] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
  2023-06-08 21:00 ` [Bug target/110182] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above pinskia at gcc dot gnu.org
  2023-06-08 21:01 ` pinskia at gcc dot gnu.org
@ 2023-06-08 21:03 ` pinskia at gcc dot gnu.org
  2023-06-08 21:17 ` [Bug tree-optimization/110182] " pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-08 21:03 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
      Known to fail|                            |11.1.0
            Summary|Vector(2) cast from double  |[11/12/13/14 Regression]
                   |to float and subtraction    |Vector(2) cast from double
                   |seems to produce incorrect  |to float and subtraction
                   |results at -O2 and above    |seems to produce incorrect
                   |                            |results at -O2 and above
      Known to work|                            |10.4.0
             Target|                            |x86_64-linux-gnu
   Target Milestone|---                         |11.5

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

* [Bug tree-optimization/110182] [11/12/13/14 Regression] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
                   ` (2 preceding siblings ...)
  2023-06-08 21:03 ` [Bug target/110182] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
@ 2023-06-08 21:17 ` pinskia at gcc dot gnu.org
  2023-06-09  7:16 ` [Bug tree-optimization/110182] [11/12/13/14 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-08 21:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|target                      |tree-optimization
   Last reconfirmed|                            |2023-06-08
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase:
```
typedef float vec2float __attribute__((vector_size(2*sizeof(float))));
typedef double vec2double __attribute__((vector_size(2*sizeof(double))));

vec2double f(vec2double a)
{
        auto t0 = a[0];
        auto t1 = a[1];
        float b0 = t0;
        float b1 = t1;
        double c0 = b0;
        double c1 = b1;
        auto r0 = t0 - c0;
        auto r1 = t1 - c1;
        return (vec2double){r0, r1};
}

double f1(double a)
{
        auto t0 = a;
        float b0 = t0;
        double c0 = b0;
        auto r0 = t0 - c0;
        return r0;
}
```

f should produce a similar results as f1 but does not.

The problem is not SLP directly, it is doing an ok job.
It produces:
  _12 = a_1(D);
  vect_b0_4.4_13 = (vector(2) float) _12;
  vect_c0_6.5_14 = (vector(2) double) vect_b0_4.4_13;
  vect_r0_8.6_15 = _12 - vect_c0_6.5_14;

Which is correct.
But then FRE comes along and does:
  vect_r0_8.6_15 = a_1(D) - a_1(D);


Because the code in match.pd that handles
/* Handle cases of two conversions in a row.  */

Messes up and uses TYPE_PRECISION for vector types but that stores the number
of elements in the vector .....

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

* [Bug tree-optimization/110182] [11/12/13/14 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
                   ` (3 preceding siblings ...)
  2023-06-08 21:17 ` [Bug tree-optimization/110182] " pinskia at gcc dot gnu.org
@ 2023-06-09  7:16 ` rguenth at gcc dot gnu.org
  2023-06-09  9:08 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-09  7:16 UTC (permalink / raw)
  To: gcc-bugs

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

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 #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Let me fix it.

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

* [Bug tree-optimization/110182] [11/12/13/14 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
                   ` (4 preceding siblings ...)
  2023-06-09  7:16 ` [Bug tree-optimization/110182] [11/12/13/14 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results rguenth at gcc dot gnu.org
@ 2023-06-09  9:08 ` rguenth at gcc dot gnu.org
  2023-06-09 10:10 ` [Bug tree-optimization/110182] [11/12 " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-09  9:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
commit 3e12669a0eb968cfcbe9242b382fd8020935edf8 (origin/master, origin/HEAD)
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Jun 9 09:29:09 2023 +0200

    middle-end/110182 - TYPE_PRECISION on VECTOR_TYPE causes wrong-code

    When folding two conversions in a row we use TYPE_PRECISION but
    that's invalid for VECTOR_TYPE.  The following fixes this by
    using element_precision instead.

            * match.pd (two conversions in a row): Use element_precision
            to DTRT for VECTOR_TYPE.

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

* [Bug tree-optimization/110182] [11/12 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
                   ` (5 preceding siblings ...)
  2023-06-09  9:08 ` rguenth at gcc dot gnu.org
@ 2023-06-09 10:10 ` rguenth at gcc dot gnu.org
  2023-06-23 11:30 ` cvs-commit at gcc dot gnu.org
  2023-12-15 12:22 ` [Bug tree-optimization/110182] [11 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-09 10:10 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[11/12/13/14 Regression]    |[11/12 Regression]
                   |Vector(2) cast from double  |Vector(2) cast from double
                   |to float and back and       |to float and back and
                   |subtraction seems to        |subtraction seems to
                   |produce incorrect results   |produce incorrect results
      Known to work|                            |13.1.1, 14.0
      Known to fail|                            |13.1.0

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed on trunk and GCC 13 branch sofar.  Not my day writing commit logs though
...

commit b6118b8155a679ced926e8ff900e0ed969cd23a7 (HEAD -> gcc-13,
origin/releases/gcc-13)
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Jun 9 09:29:09 2023 +0200

    middle-end/110182 - TYPE_PRECISION on VECTOR_TYPE causes wrong-code

    When folding two conversions in a row we use TYPE_PRECISION but
    that's invalid for VECTOR_TYPE.  The following fixes this by
    using element_precision instead.

            middle-end/110182
            * match.pd (two conversions in a row): Use element_precision
            to DTRT for VECTOR_TYPE.

    (cherry picked from commit 3e12669a0eb968cfcbe9242b382fd8020935edf8)

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

* [Bug tree-optimization/110182] [11/12 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
                   ` (6 preceding siblings ...)
  2023-06-09 10:10 ` [Bug tree-optimization/110182] [11/12 " rguenth at gcc dot gnu.org
@ 2023-06-23 11:30 ` cvs-commit at gcc dot gnu.org
  2023-12-15 12:22 ` [Bug tree-optimization/110182] [11 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-06-23 11:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- 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:f06f1b4102b1c6965ad6b1da0094d6de5c3a2940

commit r12-9722-gf06f1b4102b1c6965ad6b1da0094d6de5c3a2940
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Jun 9 09:29:09 2023 +0200

    middle-end/110182 - TYPE_PRECISION on VECTOR_TYPE causes wrong-code

    When folding two conversions in a row we use TYPE_PRECISION but
    that's invalid for VECTOR_TYPE.  The following fixes this by
    using element_precision instead.

            PR middle-end/110182
            * match.pd (two conversions in a row): Use element_precision
            to DTRT for VECTOR_TYPE.

    (cherry picked from commit 3e12669a0eb968cfcbe9242b382fd8020935edf8)

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

* [Bug tree-optimization/110182] [11 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results
  2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
                   ` (7 preceding siblings ...)
  2023-06-23 11:30 ` cvs-commit at gcc dot gnu.org
@ 2023-12-15 12:22 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-15 12:22 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |11.4.0
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED
      Known to work|                            |11.4.1

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
commit 40aa486160620d4270c9d6e777722c49823f552c
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Jun 9 09:29:09 2023 +0200

    middle-end/110182 - TYPE_PRECISION on VECTOR_TYPE causes wrong-code

    When folding two conversions in a row we use TYPE_PRECISION but
    that's invalid for VECTOR_TYPE.  The following fixes this by
    using element_precision instead.

            * match.pd (two conversions in a row): Use element_precision
            to DTRT for VECTOR_TYPE.

    (cherry picked from commit 3e12669a0eb968cfcbe9242b382fd8020935edf8)

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

end of thread, other threads:[~2023-12-15 12:22 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-08 20:48 [Bug c++/110182] New: GCC generates incorrect results for simple Eigen Casts / Subtractions at -O2 or above for a 3 dimensional vector lakin at vividtheory dot com
2023-06-08 21:00 ` [Bug target/110182] Vector(2) cast from double to float and subtraction seems to produce incorrect results at -O2 and above pinskia at gcc dot gnu.org
2023-06-08 21:01 ` pinskia at gcc dot gnu.org
2023-06-08 21:03 ` [Bug target/110182] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
2023-06-08 21:17 ` [Bug tree-optimization/110182] " pinskia at gcc dot gnu.org
2023-06-09  7:16 ` [Bug tree-optimization/110182] [11/12/13/14 Regression] Vector(2) cast from double to float and back and subtraction seems to produce incorrect results rguenth at gcc dot gnu.org
2023-06-09  9:08 ` rguenth at gcc dot gnu.org
2023-06-09 10:10 ` [Bug tree-optimization/110182] [11/12 " rguenth at gcc dot gnu.org
2023-06-23 11:30 ` cvs-commit at gcc dot gnu.org
2023-12-15 12:22 ` [Bug tree-optimization/110182] [11 " 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).