public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong
@ 2022-04-28  1:25 guihaoc at gcc dot gnu.org
  2022-04-28  1:28 ` [Bug tree-optimization/105414] " pinskia at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: guihaoc at gcc dot gnu.org @ 2022-04-28  1:25 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105414
           Summary: constant folding for fmin/max(snan, snan) is wrong
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: guihaoc at gcc dot gnu.org
  Target Milestone: ---

gcc -O0 -fsignaling-nans -D_WANT_SNAN -lm -o fmin fmin.c && ./fmin
(snan, snan), fmin: nan
gcc -O3 -fsignaling-nans -D_WANT_SNAN -lm -o fmin fmin.c && ./fmin
(snan, snan), fmin: snan

The fmin(SNaN, SNaN) got different result with O0 and O3. The result should be
nan(QNaN) according to C standard. 

The problem might be at match.pd. fmin(a,a) can't be folded to a when a is
SNaN. I propose following patch to fix it.

diff --git a/gcc/match.pd b/gcc/match.pd
index cad61848daa..2c2efda158b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3093,7 +3093,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (for minmax (min max FMIN_ALL FMAX_ALL)
  (simplify
   (minmax @0 @0)
-  @0))
+  (if(!HONOR_SNANS (@0) || !TREE_REAL_CST (@0).signalling)
+  @0)))
 /* min(max(x,y),y) -> y.  */
 (simplify
  (min:c (max:c @0 @1) @1)

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
@ 2022-04-28  1:28 ` pinskia at gcc dot gnu.org
  2022-04-28  1:31 ` guihaoc at gcc dot gnu.org
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-04-28  1:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
What target is this on?

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
  2022-04-28  1:28 ` [Bug tree-optimization/105414] " pinskia at gcc dot gnu.org
@ 2022-04-28  1:31 ` guihaoc at gcc dot gnu.org
  2022-04-28  3:07 ` guihaoc at gcc dot gnu.org
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: guihaoc at gcc dot gnu.org @ 2022-04-28  1:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from HaoChen Gui <guihaoc at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> What target is this on?

I tested it on ppc64le. But I think it should be on all targets?

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
  2022-04-28  1:28 ` [Bug tree-optimization/105414] " pinskia at gcc dot gnu.org
  2022-04-28  1:31 ` guihaoc at gcc dot gnu.org
@ 2022-04-28  3:07 ` guihaoc at gcc dot gnu.org
  2022-04-28  7:07 ` rguenth at gcc dot gnu.org
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: guihaoc at gcc dot gnu.org @ 2022-04-28  3:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from HaoChen Gui <guihaoc at gcc dot gnu.org> ---
For fmin/max behavior, I referred the this ticket. 
https://sourceware.org/bugzilla/show_bug.cgi?id=20947

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-04-28  3:07 ` guihaoc at gcc dot gnu.org
@ 2022-04-28  7:07 ` rguenth at gcc dot gnu.org
  2022-04-28  7:51 ` jakub at gcc dot gnu.org
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-28  7:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-04-28
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think you want

 (if (!tree_expr_maybe_signaling_nan_p (@0))
...

instead.

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-04-28  7:07 ` rguenth at gcc dot gnu.org
@ 2022-04-28  7:51 ` jakub at gcc dot gnu.org
  2022-04-28  8:10 ` guihaoc at gcc dot gnu.org
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-04-28  7:51 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Yeah, TREE_REAL_CST can't be used on @0 if it isn't a REAL_CST which nothing
guarantees.

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2022-04-28  7:51 ` jakub at gcc dot gnu.org
@ 2022-04-28  8:10 ` guihaoc at gcc dot gnu.org
  2022-04-28  8:18 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: guihaoc at gcc dot gnu.org @ 2022-04-28  8:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from HaoChen Gui <guihaoc at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #4)
> I think you want
> 
>  (if (!tree_expr_maybe_signaling_nan_p (@0))
> ...
> 
> instead.

Thanks so much for comments. Do we have a way to return a NaN directly in
match.pd when both arguments are sNaN?

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2022-04-28  8:10 ` guihaoc at gcc dot gnu.org
@ 2022-04-28  8:18 ` jakub at gcc dot gnu.org
  2022-04-29 10:02 ` guihaoc at gcc dot gnu.org
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-04-28  8:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Sure, but you don't want to do that at least if flag_trapping_math.
Otherwise, the predicate would be tree_expr_signaling_nan_p and real_nan
function with "", 1 as the middle 2 arguments can create it.  But note that
nothing in match.pd does that right now, so I don't think we should do it in
this case either.

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2022-04-28  8:18 ` jakub at gcc dot gnu.org
@ 2022-04-29 10:02 ` guihaoc at gcc dot gnu.org
  2022-04-29 10:06 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: guihaoc at gcc dot gnu.org @ 2022-04-29 10:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from HaoChen Gui <guihaoc at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #7)
> Sure, but you don't want to do that at least if flag_trapping_math.
> Otherwise, the predicate would be tree_expr_signaling_nan_p and real_nan
> function with "", 1 as the middle 2 arguments can create it.  But note that
> nothing in match.pd does that right now, so I don't think we should do it in
> this case either.

If either of arguments is sNaN, fmin/max should return a qNaN. So I really want
to create a pattern in match.pd to do this. It needs to create a qNaN and
return it.  I can't find an existing example in match.pd. Seems the return
value should be relevant to the arguments?

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2022-04-29 10:02 ` guihaoc at gcc dot gnu.org
@ 2022-04-29 10:06 ` jakub at gcc dot gnu.org
  2022-04-29 12:21 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-04-29 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to HaoChen Gui from comment #8)
> (In reply to Jakub Jelinek from comment #7)
> > Sure, but you don't want to do that at least if flag_trapping_math.
> > Otherwise, the predicate would be tree_expr_signaling_nan_p and real_nan
> > function with "", 1 as the middle 2 arguments can create it.  But note that
> > nothing in match.pd does that right now, so I don't think we should do it in
> > this case either.
> 
> If either of arguments is sNaN, fmin/max should return a qNaN. So I really
> want to create a pattern in match.pd to do this. It needs to create a qNaN

A sNaN should primarily raise an exception or raise a signal when used.
So better not to fold it.
Given that we don't fold anything else with sNaN, starting from sNaN + whatever
etc., just folding it for something so rare as fmin/fmax would be weird.

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2022-04-29 10:06 ` jakub at gcc dot gnu.org
@ 2022-04-29 12:21 ` rguenth at gcc dot gnu.org
  2022-05-11  1:32 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-29 12:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #9)
> (In reply to HaoChen Gui from comment #8)
> > (In reply to Jakub Jelinek from comment #7)
> > > Sure, but you don't want to do that at least if flag_trapping_math.
> > > Otherwise, the predicate would be tree_expr_signaling_nan_p and real_nan
> > > function with "", 1 as the middle 2 arguments can create it.  But note that
> > > nothing in match.pd does that right now, so I don't think we should do it in
> > > this case either.
> > 
> > If either of arguments is sNaN, fmin/max should return a qNaN. So I really
> > want to create a pattern in match.pd to do this. It needs to create a qNaN
> 
> A sNaN should primarily raise an exception or raise a signal when used.
> So better not to fold it.
> Given that we don't fold anything else with sNaN, starting from sNaN +
> whatever
> etc., just folding it for something so rare as fmin/fmax would be weird.

Agreed btw.  Iff then such propagation / simplification should belong in
a pass like forwprop or value-numbering which can do this in a cheaper way
than adding patterns for all kinds of operations with NaNs.  One might also
say that we should diagnose any arithmetic with known NaNs ...

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2022-04-29 12:21 ` rguenth at gcc dot gnu.org
@ 2022-05-11  1:32 ` cvs-commit at gcc dot gnu.org
  2022-05-12  2:54 ` guihaoc at gcc dot gnu.org
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-11  1:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by HaoChen Gui <guihaoc@gcc.gnu.org>:

https://gcc.gnu.org/g:344e425340e3c8e4539b43bf8f661e02c5a5b9a0

commit r13-280-g344e425340e3c8e4539b43bf8f661e02c5a5b9a0
Author: Haochen Gui <guihaoc@gcc.gnu.org>
Date:   Mon May 9 17:34:23 2022 +0800

    This patch skips constant folding for fmin/max when either argument is
sNaN. According to C standard, fmin(sNaN, sNaNï¼= qNaN, fmin(sNaN, NaN) =
qNaN.

    gcc/
            PR target/105414
            * match.pd (minmax): Skip constant folding for fmin/fmax when both
            arguments are sNaN or one is sNaN and another is NaN.

    gcc/testsuite/
            PR target/105414
            * gcc.dg/pr105414.c: New.

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (10 preceding siblings ...)
  2022-05-11  1:32 ` cvs-commit at gcc dot gnu.org
@ 2022-05-12  2:54 ` guihaoc at gcc dot gnu.org
  2022-09-28 10:05 ` cvs-commit at gcc dot gnu.org
  2022-11-07  4:48 ` pinskia at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: guihaoc at gcc dot gnu.org @ 2022-05-12  2:54 UTC (permalink / raw)
  To: gcc-bugs

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

HaoChen Gui <guihaoc at gcc dot gnu.org> changed:

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

--- Comment #12 from HaoChen Gui <guihaoc at gcc dot gnu.org> ---
Fixed.

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (11 preceding siblings ...)
  2022-05-12  2:54 ` guihaoc at gcc dot gnu.org
@ 2022-09-28 10:05 ` cvs-commit at gcc dot gnu.org
  2022-11-07  4:48 ` pinskia at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-09-28 10:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #13 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Xi Ruoyao <xry111@gcc.gnu.org>:

https://gcc.gnu.org/g:b48d7ff3570fa0ebe7790275cf020d8885120338

commit r13-2914-gb48d7ff3570fa0ebe7790275cf020d8885120338
Author: Xi Ruoyao <xry111@xry111.site>
Date:   Sat Sep 24 20:47:22 2022 +0800

    LoongArch: Use UNSPEC for fmin/fmax RTL pattern [PR105414]

    I made a mistake defining fmin/fmax RTL patterns in r13-2085: I used
    smin and smax in the definition mistakenly.  This causes the optimizer
    to perform constant folding as if fmin/fmax was "really" smin/smax
    operations even with -fsignaling-nans.  Then pr105414.c fails.

    We don't have fmin/fmax RTL codes for now (PR107013) so we can only use
    an UNSPEC for fmin and fmax patterns.

    gcc/ChangeLog:

            PR tree-optimization/105414
            * config/loongarch/loongarch.md (UNSPEC_FMAX): New unspec.
            (UNSPEC_FMIN): Likewise.
            (fmax<mode>3): Use UNSPEC_FMAX instead of smax.
            (fmin<mode>3): Use UNSPEC_FMIN instead of smin.

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

* [Bug tree-optimization/105414] constant folding for fmin/max(snan, snan) is wrong
  2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
                   ` (12 preceding siblings ...)
  2022-09-28 10:05 ` cvs-commit at gcc dot gnu.org
@ 2022-11-07  4:48 ` pinskia at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-07  4:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0

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

end of thread, other threads:[~2022-11-07  4:48 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-28  1:25 [Bug tree-optimization/105414] New: constant folding for fmin/max(snan, snan) is wrong guihaoc at gcc dot gnu.org
2022-04-28  1:28 ` [Bug tree-optimization/105414] " pinskia at gcc dot gnu.org
2022-04-28  1:31 ` guihaoc at gcc dot gnu.org
2022-04-28  3:07 ` guihaoc at gcc dot gnu.org
2022-04-28  7:07 ` rguenth at gcc dot gnu.org
2022-04-28  7:51 ` jakub at gcc dot gnu.org
2022-04-28  8:10 ` guihaoc at gcc dot gnu.org
2022-04-28  8:18 ` jakub at gcc dot gnu.org
2022-04-29 10:02 ` guihaoc at gcc dot gnu.org
2022-04-29 10:06 ` jakub at gcc dot gnu.org
2022-04-29 12:21 ` rguenth at gcc dot gnu.org
2022-05-11  1:32 ` cvs-commit at gcc dot gnu.org
2022-05-12  2:54 ` guihaoc at gcc dot gnu.org
2022-09-28 10:05 ` cvs-commit at gcc dot gnu.org
2022-11-07  4:48 ` pinskia 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).