From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id D18D13858000 for ; Mon, 31 Jan 2022 14:57:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D18D13858000 Received: by mail-ej1-x62f.google.com with SMTP id j2so43436709ejk.6 for ; Mon, 31 Jan 2022 06:57:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7CNw5YD4qUaL4PuPsWE71XqS60sd1wGG3xsFSir/WCg=; b=jDV9NJGNgETlsXTAlpqW0ToD68tRJYjDeHmsNPK0GxaEKXa/5OV6UsdEqAZp7YMy7l fchL6fUaoH1eTGpCeiJZsmWXicm45HYnwdkWKvkWy6fGCeqEwLAWr6s/AT7TQvHcL7+p 77exzro2XYROAmXockJyskm7/VQYQ4aEeiN2pM7Q7bXkoZikml5FiPrr6U3RaFrzg66V X2vIdcKLmXrX2gR5jUPtGrfuwZuCwJ+VK1Dx9EaXa1QpZQ6dNV4AOEIy+KfrOXniauYQ N00DA/YuZB0Htoqip+yAL4Qz/JB3liBDuSACusYrzPlDznZgNiZ+zbqsQvjEKBPn0fLM U16g== X-Gm-Message-State: AOAM532IRwB1Eap0kQmvdCtuCo7pYoXdtO5O+2/opGOXQMmLvRNYOIkE 6PbHDGcQKNc5+KOKkpbvioR5U9xOSl5vhQ6IBr8= X-Google-Smtp-Source: ABdhPJzU/gzk0WpX0CupVzvMUq2FPmBohzvnnQhqKWGjvUOKzJkFJTj7EJb547ssf/QQcaD+are0QPTsamHH48eVOjE= X-Received: by 2002:a17:907:9689:: with SMTP id hd9mr8642242ejc.240.1643641077594; Mon, 31 Jan 2022 06:57:57 -0800 (PST) MIME-Version: 1.0 References: <8b49a9906a0d1019bd877bf526f71ab5321550fe.camel@mengyan1223.wang> In-Reply-To: From: Richard Biener Date: Mon, 31 Jan 2022 15:57:46 +0100 Message-ID: Subject: Re: [PATCH] fold-const: do not fold 'inf/inf' with -ftrapping-math [PR95115] To: Xi Ruoyao Cc: "Joseph S. Myers" , GCC Patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Jan 2022 14:58:00 -0000 On Mon, Jan 31, 2022 at 11:07 AM Xi Ruoyao wrote: > > On Mon, 2022-01-31 at 10:35 +0100, Richard Biener wrote: > > On Sun, Jan 30, 2022 at 7:39 PM Xi Ruoyao via Gcc-patches > > wrote: > > > > > > 'inf/inf' should raise an invalid operation exception at runtime. > > > So it > > > should not be folded during compilation unless -fno-trapping-math is > > > used. > > > > I wonder if it would make sense to handle it similar to > > > > inexact = real_arithmetic (&value, code, &d1, &d2); > > real_convert (&result, mode, &value); > > > > /* Don't constant fold this floating point operation if > > the result has overflowed and flag_trapping_math. */ > > if (flag_trapping_math > > && MODE_HAS_INFINITIES (mode) > > && REAL_VALUE_ISINF (result) > > && !REAL_VALUE_ISINF (d1) > > && !REAL_VALUE_ISINF (d2)) > > return NULL_TREE; > > > thus whenever we fold to NaN but flag_trapping_math is set, > > do not fold? > > Like this? > > inexact = real_arithmetic (&value, code, &d1, &d2); > real_convert (&result, mode, &value); > > + /* Don't constant fold this floating point operation if > + both operands are not NaN but the result is NaN, and > + flag_trapping_math. Such operations should raise an > + invalid operation exception. */ > + if (flag_trapping_math > + && MODE_HAS_NANS (mode) > + && REAL_VALUE_ISNAN (result) > + && !REAL_VALUE_ISNAN (d1) > + && !REAL_VALUE_ISNAN (d2)) > + return NULL_TREE; > + > /* Don't constant fold this floating point operation if > the result has overflowed and flag_trapping_math. */ > if (flag_trapping_math > > > We already exclude NaN operands earlier > > (but unconditionally return NaN even for NaN + 1. though > > that should raise invalid as well, no?) > > qNaN + 1 (or even qNaN + qNaN) should produce a qNaN but should not > raise any exception. OTOH sNaN + 1 should produce a qNaN and raise an > invalid operation exception. Ah, I see. So yes, like that. Joseph, do you agree? Thanks, Richard. > For sNaN operands we already ruled out the > const folding earlier: > > > /* Don't perform operation if we honor signaling NaNs and > > either operand is a signaling NaN. */ > > if (HONOR_SNANS (mode) > > && (REAL_VALUE_ISSIGNALING_NAN (d1) > > || REAL_VALUE_ISSIGNALING_NAN (d2))) > > return NULL_TREE; > > > I suppose Joseph should know best what the correct semantics > > are here. > > > > Thanks, > > Richard. > > > > > gcc/ > > > PR middle-end/95115 > > > * fold-const.cc (const_binop): Do not fold "inf/inf". > > > > > > gcc/testsuite > > > * gcc.dg/pr95115.c: New test. > > > --- > > > gcc/fold-const.cc | 8 ++++++++ > > > gcc/testsuite/gcc.dg/pr95115.c | 25 +++++++++++++++++++++++++ > > > 2 files changed, 33 insertions(+) > > > create mode 100644 gcc/testsuite/gcc.dg/pr95115.c > > > > > > diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc > > > index fd9c6352d4f..0e99d5a2e3d 100644 > > > --- a/gcc/fold-const.cc > > > +++ b/gcc/fold-const.cc > > > @@ -1283,6 +1283,14 @@ const_binop (enum tree_code code, tree arg1, > > > tree arg2) > > > && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode))) > > > return NULL_TREE; > > > > > > + /* Don't perform "inf/inf", because it would raise an invalid > > > + operation exception (IEEE 754 section 7.2 clause d). */ > > > + if (code == RDIV_EXPR > > > + && flag_trapping_math > > > + && REAL_VALUE_ISINF (d1) > > > + && REAL_VALUE_ISINF (d2)) > > > + return NULL_TREE; > > > + > > > /* If either operand is a NaN, just return it. Otherwise, > > > set up > > > for floating-point trap; we return an overflow. */ > > > if (REAL_VALUE_ISNAN (d1)) > > > diff --git a/gcc/testsuite/gcc.dg/pr95115.c > > > b/gcc/testsuite/gcc.dg/pr95115.c > > > new file mode 100644 > > > index 00000000000..46a95dfb698 > > > --- /dev/null > > > +++ b/gcc/testsuite/gcc.dg/pr95115.c > > > @@ -0,0 +1,25 @@ > > > +/* { dg-do run } */ > > > +/* { dg-options "-O2 -ftrapping-math" } */ > > > +/* { dg-add-options ieee } */ > > > +/* { dg-require-effective-target fenv_exceptions } */ > > > + > > > +#include > > > +#include > > > + > > > +double > > > +x (void) > > > +{ > > > + double d = __builtin_inf (); > > > + return d / d; > > > +} > > > + > > > +int > > > +main (void) > > > +{ > > > + double r = x (); > > > + if (!__builtin_isnan (r)) > > > + abort (); > > > + if (!fetestexcept (FE_INVALID)) > > > + abort (); > > > + exit (0); > > > +} > > > -- > > > 2.35.1 > > > > > > > > -- > Xi Ruoyao > School of Aerospace Science and Technology, Xidian University