Message ID | 20220204230727.GP2646553@tucnak |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D59B3858C2D for <patchwork@sourceware.org>; Fri, 4 Feb 2022 23:08:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D59B3858C2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644016106; bh=9YFAQOEo+q277k4OMpXauuern7EKkkX2jrKSD1cDAlg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=yFGGFDk4xU4JKnePXvECxCmWBY/i5pZ+PTe32SheQPnirU92x2xuD2mYWkDVX+FT7 9O0FuOj/pamBBcilhdla9lrUMPPIwaw56MSTtzdNCuXHaxzKBGcUcaas22webMRnVv hP5Biu6K5YsKmXbj9SQmFg1QyLUOd8hSMcR9ep8Y= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AEACB3858D20 for <gcc-patches@gcc.gnu.org>; Fri, 4 Feb 2022 23:07:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AEACB3858D20 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-620-px-aSVI3Ppuwx8keA2w6tg-1; Fri, 04 Feb 2022 18:07:33 -0500 X-MC-Unique: px-aSVI3Ppuwx8keA2w6tg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5178B100CCC1; Fri, 4 Feb 2022 23:07:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.125]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E0035E2C3; Fri, 4 Feb 2022 23:07:31 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 214N7SYl2027492 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 5 Feb 2022 00:07:29 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 214N7RrX2027491; Sat, 5 Feb 2022 00:07:27 +0100 Date: Sat, 5 Feb 2022 00:07:27 +0100 To: Richard Biener <rguenther@suse.de>, Jeff Law <jeffreyalaw@gmail.com> Subject: [PATCH] match.pd: Fix x * 0.0 -> 0.0 folding [PR104389] Message-ID: <20220204230727.GP2646553@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Jakub Jelinek <jakub@redhat.com> Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
match.pd: Fix x * 0.0 -> 0.0 folding [PR104389]
|
|
Commit Message
Jakub Jelinek
Feb. 4, 2022, 11:07 p.m. UTC
Hi! The recent PR95115 change to punt in const_binop on folding operation with non-NaN operands into NaN if flag_trapping_math broke the following testcase, because the x * 0.0 simplification punts just if x maybe a NaN (because NaN * 0.0 is NaN not 0.0) or if one of the operands could be negative zero. But Inf * 0.0 or -Inf * 0.0 is also NaN, not 0.0, so when NaNs are honored we need to punt for possible infinities too. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and 11/10 where the PR95115 change has been unfortunately backported to as well? 2022-02-04 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/104389 (x * 0 -> 0): Punt if x maybe infinite and NaNs are honored. * gcc.dg/pr104389.c: New test. Jakub
Comments
> Am 05.02.2022 um 00:08 schrieb Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org>: > > Hi! > > The recent PR95115 change to punt in const_binop on folding operation > with non-NaN operands into NaN if flag_trapping_math broke the following > testcase, because the x * 0.0 simplification punts just if > x maybe a NaN (because NaN * 0.0 is NaN not 0.0) or if one of the operands > could be negative zero. But Inf * 0.0 or -Inf * 0.0 is also NaN, not > 0.0, so when NaNs are honored we need to punt for possible infinities too. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk > and 11/10 where the PR95115 change has been unfortunately backported to > as well? Ok > 2022-02-04 Jakub Jelinek <jakub@redhat.com> > > PR tree-optimization/104389 > (x * 0 -> 0): Punt if x maybe infinite and NaNs are honored. > > * gcc.dg/pr104389.c: New test. > > --- gcc/match.pd.jj 2022-02-04 14:36:55.393599880 +0100 > +++ gcc/match.pd 2022-02-04 20:30:48.548213594 +0100 > @@ -256,10 +256,12 @@ (define_operator_list SYNC_FETCH_AND_AND > /* Maybe fold x * 0 to 0. The expressions aren't the same > when x is NaN, since x * 0 is also NaN. Nor are they the > same in modes with signed zeros, since multiplying a > - negative value by 0 gives -0, not +0. */ > + negative value by 0 gives -0, not +0. Nor when x is +-Inf, > + since x * 0 is NaN. */ > (simplify > (mult @0 real_zerop@1) > (if (!tree_expr_maybe_nan_p (@0) > + && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0)) > && !tree_expr_maybe_real_minus_zero_p (@0) > && !tree_expr_maybe_real_minus_zero_p (@1)) > @1)) > --- gcc/testsuite/gcc.dg/pr104389.c.jj 2022-02-04 20:37:40.579537142 +0100 > +++ gcc/testsuite/gcc.dg/pr104389.c 2022-02-04 20:37:20.787809803 +0100 > @@ -0,0 +1,26 @@ > +/* PR tree-optimization/104389 */ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > +/* { dg-add-options ieee } */ > +/* { dg-require-effective-target inf } */ > + > +__attribute__((noipa)) double > +foo (void) > +{ > + double a = __builtin_huge_val (); > + return a * 0.0; > +} > + > +__attribute__((noipa)) long double > +bar (void) > +{ > + return __builtin_huge_vall () * 0.0L; > +} > + > +int > +main () > +{ > + if (!__builtin_isnan (foo ()) || !__builtin_isnanl (bar ())) > + __builtin_abort (); > + return 0; > +} > > Jakub >
On Sat, 2022-02-05 at 08:21 +0100, Richard Biener via Gcc-patches wrote: > > > > Am 05.02.2022 um 00:08 schrieb Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org>: > > > > Hi! > > > > The recent PR95115 change to punt in const_binop on folding operation > > with non-NaN operands into NaN if flag_trapping_math broke the following > > testcase, because the x * 0.0 simplification punts just if > > x maybe a NaN (because NaN * 0.0 is NaN not 0.0) or if one of the operands > > could be negative zero. But Inf * 0.0 or -Inf * 0.0 is also NaN, not > > 0.0, so when NaNs are honored we need to punt for possible infinities too. Sorry for the trouble, but some warning here: even with this patch applied, Python would still need to replace inf * 0.0 with nan("") or something. Now with folding for inf * 0.0 disabled, the multiplication will be evaluated at runtime and raise FE_INVALID, which is likely unwanted by Python. However, raising FE_INVALID is the correct behavior no matter if we like or dislike it... > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk > > and 11/10 where the PR95115 change has been unfortunately backported to > > as well? As there may be many packages (unfortunately) using the nasty "inf * 0.0" pattern to get NaN, maybe we should just revert the PR95115 change for 11/10, apply PR104389 patch for trunk, and announce the behavior change in gcc-12/changes.html?
> Am 05.02.2022 um 09:21 schrieb Xi Ruoyao <xry111@mengyan1223.wang>: > > On Sat, 2022-02-05 at 08:21 +0100, Richard Biener via Gcc-patches wrote: >> >> >>>> Am 05.02.2022 um 00:08 schrieb Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org>: >>> >>> Hi! >>> >>> The recent PR95115 change to punt in const_binop on folding operation >>> with non-NaN operands into NaN if flag_trapping_math broke the following >>> testcase, because the x * 0.0 simplification punts just if >>> x maybe a NaN (because NaN * 0.0 is NaN not 0.0) or if one of the operands >>> could be negative zero. But Inf * 0.0 or -Inf * 0.0 is also NaN, not >>> 0.0, so when NaNs are honored we need to punt for possible infinities too. > > Sorry for the trouble, but some warning here: even with this patch > applied, Python would still need to replace inf * 0.0 with nan("") or > something. Now with folding for inf * 0.0 disabled, the multiplication > will be evaluated at runtime and raise FE_INVALID, which is likely > unwanted by Python. However, raising FE_INVALID is the correct behavior > no matter if we like or dislike it... > >>> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk >>> and 11/10 where the PR95115 change has been unfortunately backported to >>> as well? > > As there may be many packages (unfortunately) using the nasty "inf * > 0.0" pattern to get NaN, maybe we should just revert the PR95115 change > for 11/10, apply PR104389 patch for trunk, and announce the behavior > change in gcc-12/changes.html? That probably makes sense. We could also revisit turning on -fno -trapping-math by default . > -- > Xi Ruoyao <xry111@mengyan1223.wang> > School of Aerospace Science and Technology, Xidian University
On Sat, Feb 05, 2022 at 08:21:26AM +0100, Richard Biener wrote: > > Am 05.02.2022 um 00:08 schrieb Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org>: > > The recent PR95115 change to punt in const_binop on folding operation > > with non-NaN operands into NaN if flag_trapping_math broke the following > > testcase, because the x * 0.0 simplification punts just if > > x maybe a NaN (because NaN * 0.0 is NaN not 0.0) or if one of the operands > > could be negative zero. But Inf * 0.0 or -Inf * 0.0 is also NaN, not > > 0.0, so when NaNs are honored we need to punt for possible infinities too. > > > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk > > and 11/10 where the PR95115 change has been unfortunately backported to > > as well? > > Ok Thanks, committed now. Turns out gcc 10/11 don't suffer from this bug (though reversion of the PR95115 change for 10/11 is probably a good idea anyway), this bug has been introduced in r12-1393-g5b02ed4b87685c0f7c5da9b46cde3ce56fcfd457 where we used there if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)) before. Looking at that commit, there was also: (simplify (minus @0 @0) - (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type)) + (if (!FLOAT_TYPE_P (type) || !tree_expr_maybe_nan_p (@0)) { build_zero_cst (type); })) Inf - Inf or (-Inf) - (-Inf) is NaN, not 0.0, but that got fixed in r12-5529-g6ea5fb3cc7f3cc9b731d72183c66c23543876f5a Jakub
--- gcc/match.pd.jj 2022-02-04 14:36:55.393599880 +0100 +++ gcc/match.pd 2022-02-04 20:30:48.548213594 +0100 @@ -256,10 +256,12 @@ (define_operator_list SYNC_FETCH_AND_AND /* Maybe fold x * 0 to 0. The expressions aren't the same when x is NaN, since x * 0 is also NaN. Nor are they the same in modes with signed zeros, since multiplying a - negative value by 0 gives -0, not +0. */ + negative value by 0 gives -0, not +0. Nor when x is +-Inf, + since x * 0 is NaN. */ (simplify (mult @0 real_zerop@1) (if (!tree_expr_maybe_nan_p (@0) + && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0)) && !tree_expr_maybe_real_minus_zero_p (@0) && !tree_expr_maybe_real_minus_zero_p (@1)) @1)) --- gcc/testsuite/gcc.dg/pr104389.c.jj 2022-02-04 20:37:40.579537142 +0100 +++ gcc/testsuite/gcc.dg/pr104389.c 2022-02-04 20:37:20.787809803 +0100 @@ -0,0 +1,26 @@ +/* PR tree-optimization/104389 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ +/* { dg-add-options ieee } */ +/* { dg-require-effective-target inf } */ + +__attribute__((noipa)) double +foo (void) +{ + double a = __builtin_huge_val (); + return a * 0.0; +} + +__attribute__((noipa)) long double +bar (void) +{ + return __builtin_huge_vall () * 0.0L; +} + +int +main () +{ + if (!__builtin_isnan (foo ()) || !__builtin_isnanl (bar ())) + __builtin_abort (); + return 0; +}