Message ID  20220917082403.15737211aldyh@redhat.com 

State  New 
Headers 
ReturnPath: <gccpatchesbounces+patchwork=sourceware.org@gcc.gnu.org> XOriginalTo: patchwork@sourceware.org DeliveredTo: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B26603831DEA for <patchwork@sourceware.org>; Sat, 17 Sep 2022 08:24:52 +0000 (GMT) DKIMFilter: OpenDKIM Filter v2.11.0 sourceware.org B26603831DEA DKIMSignature: v=1; a=rsasha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663403092; bh=p/YxWrLRTx7kRMCbeW+npAOoYKbpcJ7eGs9cFktgEYo=; h=To:Subject:Date:ListId:ListUnsubscribe:ListArchive:ListPost: ListHelp:ListSubscribe:From:ReplyTo:Cc:From; b=PZRTPTn7tIFyMaoM7/ZelihhH0nO5VKui8jD/UDZK5o0RZNG7tXSHJ57J66JmHw2T rsx/r8lRLn8uP5Bi6lh+abJIBX11kqDfnGdl+Ig1H3bZdFsS+1tPYcuqTl88/SDFvw 2VHvhhjhuKYx4N6QdmBQnsMOy+cZvKEz4GcjqPOc= XOriginalTo: gccpatches@gcc.gnu.org DeliveredTo: gccpatches@gcc.gnu.org Received: from ussmtpdelivery124.mimecast.com (ussmtpdelivery124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5D901383177E for <gccpatches@gcc.gnu.org>; Sat, 17 Sep 2022 08:24:20 +0000 (GMT) DMARCFilter: OpenDMARC Filter v1.4.1 sourceware.org 5D901383177E Received: from mimecastmx02.redhat.com (mimecastmx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id usmta135lEKGaKPBOvuL1R81MSGOPQ1; Sat, 17 Sep 2022 04:24:18 0400 XMCUnique: lEKGaKPBOvuL1R81MSGOPQ1 Received: from smtp.corp.redhat.com (intmx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDHAES256SHA (256/256 bits)) (No client certificate requested) by mimecastmx02.redhat.com (Postfix) with ESMTPS id 37DCA185A78F; Sat, 17 Sep 2022 08:24:18 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C557C492CA2; Sat, 17 Sep 2022 08:24:17 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 28H8OFRA1573751 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 17 Sep 2022 10:24:15 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 28H8OFdW1573750; Sat, 17 Sep 2022 10:24:15 +0200 To: Jakub Jelinek <jakub@redhat.com>, Richard Henderson <rth@gcc.gnu.org> Subject: [PATCH] frange: flush denormals to zero for funsafemathoptimizations. Date: Sat, 17 Sep 2022 10:24:03 +0200 MessageId: <20220917082403.15737211aldyh@redhat.com> MIMEVersion: 1.0 XScannedBy: MIMEDefang 3.1 on 10.11.54.9 XMimecastSpamScore: 0 XMimecastOriginator: redhat.com ContentTransferEncoding: 8bit ContentType: text/plain; charset="USASCII"; xdefault=true XSpamStatus: No, score=11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 XSpamCheckerVersion: SpamAssassin 3.4.6 (20210409) on server2.sourceware.org XBeenThere: gccpatches@gcc.gnu.org XMailmanVersion: 2.1.29 Precedence: list ListId: Gccpatches mailing list <gccpatches.gcc.gnu.org> ListUnsubscribe: <https://gcc.gnu.org/mailman/options/gccpatches>, <mailto:gccpatchesrequest@gcc.gnu.org?subject=unsubscribe> ListArchive: <https://gcc.gnu.org/pipermail/gccpatches/> ListPost: <mailto:gccpatches@gcc.gnu.org> ListHelp: <mailto:gccpatchesrequest@gcc.gnu.org?subject=help> ListSubscribe: <https://gcc.gnu.org/mailman/listinfo/gccpatches>, <mailto:gccpatchesrequest@gcc.gnu.org?subject=subscribe> From: Aldy Hernandez via Gccpatches <gccpatches@gcc.gnu.org> ReplyTo: Aldy Hernandez <aldyh@redhat.com> Cc: GCC patches <gccpatches@gcc.gnu.org> ErrorsTo: gccpatchesbounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gccpatches" <gccpatchesbounces+patchwork=sourceware.org@gcc.gnu.org> 
Series 
frange: flush denormals to zero for funsafemathoptimizations.


Commit Message
Aldy Hernandez
Sept. 17, 2022, 8:24 a.m. UTC
Jakub has mentioned that for funsafemathoptimizations we may flush denormals to zero, in which case we need to be careful to extend the ranges to the appropriate zero. This patch does exactly that. For a range of [x, DENORMAL] we flush to [x, 0.0] and for [+DENORMAL, x] we flush to [+0.0, x]. It is unclear whether we should do this for Alpha, since I believe flushing to zero is the default, and the port requires mieee for IEEE sanity. If so, perhaps we should add a target hook so backends are free to request flushing to zero. Thoughts? gcc/ChangeLog: * valuerange.cc (frange::flush_denormals_to_zero): New. (frange::set): Call flush_denormals_to_zero. * valuerange.h (class frange): Add flush_denormals_to_zero.  gcc/valuerange.cc  24 ++++++++++++++++++++++++ gcc/valuerange.h  1 + 2 files changed, 25 insertions(+)
Comments
On Sat, Sep 17, 2022 at 10:25 AM Aldy Hernandez via Gccpatches <gccpatches@gcc.gnu.org> wrote: > > Jakub has mentioned that for funsafemathoptimizations we may flush > denormals to zero, in which case we need to be careful to extend the > ranges to the appropriate zero. This patch does exactly that. For a > range of [x, DENORMAL] we flush to [x, 0.0] and for [+DENORMAL, x] > we flush to [+0.0, x]. > > It is unclear whether we should do this for Alpha, since I believe > flushing to zero is the default, and the port requires mieee for IEEE > sanity. If so, perhaps we should add a target hook so backends are > free to request flushing to zero. > > Thoughts? I'm not sure what the intention of this is  it effectively results in more conservative ranges for funsafemathoptimizations. That is, if funsafemathoptimizations says that denormals don't exist and are all zero then doesn't that mean we can instead use the smalles nondenormal number less than (or greater than) zero here? That said, the flushing you do is also "valid" for fnounsafemathoptimizations in case we don't want to deal with denormals in range boundaries. It might also be a correctness requirement in case we don't know how targets handle denormals (IIRC even OS defaults might matter here) so we conservatively have to treat them as being flushed to zero. So ... if we want to be on the "safe" side then please always do this. At the same point you could do if (!HONOR_SIGNED_ZEROS ()) if (real_iszero (&m_max)) { if (real_iszero (&m_min)) m.min.sign = 1; m_max.sign = 1; } else if (real_iszero (&m_min)) m_min.sign = 0; so that we canonicalize a zero bound so that the sign is known for a range. Richard. > gcc/ChangeLog: > > * valuerange.cc (frange::flush_denormals_to_zero): New. > (frange::set): Call flush_denormals_to_zero. > * valuerange.h (class frange): Add flush_denormals_to_zero. >  > gcc/valuerange.cc  24 ++++++++++++++++++++++++ > gcc/valuerange.h  1 + > 2 files changed, 25 insertions(+) > > diff git a/gcc/valuerange.cc b/gcc/valuerange.cc > index 67d5d7fa90f..f285734f0e0 100644 >  a/gcc/valuerange.cc > +++ b/gcc/valuerange.cc > @@ 267,6 +267,26 @@ tree_compare (tree_code code, tree op1, tree op2) > return !integer_zerop (fold_build2 (code, integer_type_node, op1, op2)); > } > > +// Flush denormal endpoints to the appropriate 0.0. > + > +void > +frange::flush_denormals_to_zero () > +{ > + if (undefined_p ()  known_isnan ()) > + return; > + > + // Flush [x, DENORMAL] to [x, 0.0]. > + if (real_isdenormal (&m_max) && real_isneg (&m_max)) > + { > + m_max = dconst0; > + if (HONOR_SIGNED_ZEROS (m_type)) > + m_max.sign = 1; > + } > + // Flush [+DENORMAL, x] to [+0.0, x]. > + if (real_isdenormal (&m_min) && !real_isneg (&m_min)) > + m_min = dconst0; > +} > + > // Setter for franges. > > void > @@ 317,6 +337,10 @@ frange::set (tree min, tree max, value_range_kind kind) > gcc_checking_assert (tree_compare (LE_EXPR, min, max)); > > normalize_kind (); > + > + if (flag_unsafe_math_optimizations) > + flush_denormals_to_zero (); > + > if (flag_checking) > verify_range (); > } > diff git a/gcc/valuerange.h b/gcc/valuerange.h > index 3a401f3e4e2..795b1f00fdc 100644 >  a/gcc/valuerange.h > +++ b/gcc/valuerange.h > @@ 327,6 +327,7 @@ private: > bool union_nans (const frange &); > bool intersect_nans (const frange &); > bool combine_zeros (const frange &, bool union_p); > + void flush_denormals_to_zero (); > > tree m_type; > REAL_VALUE_TYPE m_min; >  > 2.37.1 >
On Mon, Sep 19, 2022 at 09:37:22AM +0200, Richard Biener wrote: > On Sat, Sep 17, 2022 at 10:25 AM Aldy Hernandez via Gccpatches > <gccpatches@gcc.gnu.org> wrote: > > > > Jakub has mentioned that for funsafemathoptimizations we may flush > > denormals to zero, in which case we need to be careful to extend the > > ranges to the appropriate zero. This patch does exactly that. For a > > range of [x, DENORMAL] we flush to [x, 0.0] and for [+DENORMAL, x] > > we flush to [+0.0, x]. > > > > It is unclear whether we should do this for Alpha, since I believe > > flushing to zero is the default, and the port requires mieee for IEEE > > sanity. If so, perhaps we should add a target hook so backends are > > free to request flushing to zero. > > > > Thoughts? > > I'm not sure what the intention of this is  it effectively results in > more conservative ranges for funsafemathoptimizations. That is, > if funsafemathoptimizations says that denormals don't exist and > are all zero then doesn't that mean we can instead use the smalles > nondenormal number less than (or greater than) zero here? Yes, with funsafemathoptimizations we will sometimes have slightly larger ranges, while when we know or assume we know that denormals will be honored we can be more precise. It is similar to the fnosignedzeros case, when we are more relaxed and say we don't care about sign of zeros and optimizations can be performed to leave the sign of zero in whatever state they like, we can't have [+0.0, xx] or [xx, 0.0] ranges and need to extend them to [0.0, xx] or [xx, +0.0]. The honor denormals case is similar, if there is the possibility of having denormals flushed to zero, [+denormal, xx] needs to be extended to [+0.0, xx] and [xx, denormal] to [xx, 0.0]. Now, I think we just should add a separate option whether denormals are honored or not, because always extending the ranges with denormals on the boundaries might prevent some useful optimizations. If we add such an option, it could default to not honoring them for funsafemathoptimizations, and say on alpha it should default to not honoring them by default always unless mieee option is used, but all of this if the user didn't use the option explicitly, in that case follow what the user said. That way if users know they set DTZ flag themselves or use libraries that do so, they can tell the compiler about it. > That said, the flushing you do is also "valid" for > fnounsafemathoptimizations > in case we don't want to deal with denormals in range boundaries. It is always valid to extend the range more, but could be harmful to optimizations. Say if we have then x > 0.0 test... Of course, without ffastmath we won't DCE various floating point operations because they can raise exceptions, but at least we can DCE the basic block after it that depends on x > 0.0. Jakub
On Mon, Sep 19, 2022 at 9:37 AM Richard Biener <richard.guenther@gmail.com> wrote: > > On Sat, Sep 17, 2022 at 10:25 AM Aldy Hernandez via Gccpatches > <gccpatches@gcc.gnu.org> wrote: > > > > Jakub has mentioned that for funsafemathoptimizations we may flush > > denormals to zero, in which case we need to be careful to extend the > > ranges to the appropriate zero. This patch does exactly that. For a > > range of [x, DENORMAL] we flush to [x, 0.0] and for [+DENORMAL, x] > > we flush to [+0.0, x]. > > > > It is unclear whether we should do this for Alpha, since I believe > > flushing to zero is the default, and the port requires mieee for IEEE > > sanity. If so, perhaps we should add a target hook so backends are > > free to request flushing to zero. > > > > Thoughts? > > I'm not sure what the intention of this is  it effectively results in > more conservative ranges for funsafemathoptimizations. That is, > if funsafemathoptimizations says that denormals don't exist and > are all zero then doesn't that mean we can instead use the smalles > nondenormal number less than (or greater than) zero here? > > That said, the flushing you do is also "valid" for > fnounsafemathoptimizations > in case we don't want to deal with denormals in range boundaries. > > It might also be a correctness requirement in case we don't know how > targets handle denormals (IIRC even OS defaults might matter here) so > we conservatively have to treat them as being flushed to zero. Actually, rth suggested we always flush to zero because we don't know what the target would do. Again, I'm happy to do whatever you agree on. I have no opinion. My main goal here is correctness. > > So ... if we want to be on the "safe" side then please always do this. > > At the same point you could do > > if (!HONOR_SIGNED_ZEROS ()) > if (real_iszero (&m_max)) > { > if (real_iszero (&m_min)) > m.min.sign = 1; > m_max.sign = 1; > } But wouldn't that set [0.0, 0.0] when encountering [+0, +0] ?? > else if (real_iszero (&m_min)) > m_min.sign = 0; Jakub actually suggested something completely different...just set +0 always for !HONOR_SIGNED_ZEROS. Aldy > > so that we canonicalize a zero bound so that the sign is known for a range. > > Richard. > > > gcc/ChangeLog: > > > > * valuerange.cc (frange::flush_denormals_to_zero): New. > > (frange::set): Call flush_denormals_to_zero. > > * valuerange.h (class frange): Add flush_denormals_to_zero. > >  > > gcc/valuerange.cc  24 ++++++++++++++++++++++++ > > gcc/valuerange.h  1 + > > 2 files changed, 25 insertions(+) > > > > diff git a/gcc/valuerange.cc b/gcc/valuerange.cc > > index 67d5d7fa90f..f285734f0e0 100644 > >  a/gcc/valuerange.cc > > +++ b/gcc/valuerange.cc > > @@ 267,6 +267,26 @@ tree_compare (tree_code code, tree op1, tree op2) > > return !integer_zerop (fold_build2 (code, integer_type_node, op1, op2)); > > } > > > > +// Flush denormal endpoints to the appropriate 0.0. > > + > > +void > > +frange::flush_denormals_to_zero () > > +{ > > + if (undefined_p ()  known_isnan ()) > > + return; > > + > > + // Flush [x, DENORMAL] to [x, 0.0]. > > + if (real_isdenormal (&m_max) && real_isneg (&m_max)) > > + { > > + m_max = dconst0; > > + if (HONOR_SIGNED_ZEROS (m_type)) > > + m_max.sign = 1; > > + } > > + // Flush [+DENORMAL, x] to [+0.0, x]. > > + if (real_isdenormal (&m_min) && !real_isneg (&m_min)) > > + m_min = dconst0; > > +} > > + > > // Setter for franges. > > > > void > > @@ 317,6 +337,10 @@ frange::set (tree min, tree max, value_range_kind kind) > > gcc_checking_assert (tree_compare (LE_EXPR, min, max)); > > > > normalize_kind (); > > + > > + if (flag_unsafe_math_optimizations) > > + flush_denormals_to_zero (); > > + > > if (flag_checking) > > verify_range (); > > } > > diff git a/gcc/valuerange.h b/gcc/valuerange.h > > index 3a401f3e4e2..795b1f00fdc 100644 > >  a/gcc/valuerange.h > > +++ b/gcc/valuerange.h > > @@ 327,6 +327,7 @@ private: > > bool union_nans (const frange &); > > bool intersect_nans (const frange &); > > bool combine_zeros (const frange &, bool union_p); > > + void flush_denormals_to_zero (); > > > > tree m_type; > > REAL_VALUE_TYPE m_min; > >  > > 2.37.1 > > >
On Mon, Sep 19, 2022 at 3:04 PM Aldy Hernandez <aldyh@redhat.com> wrote: > > On Mon, Sep 19, 2022 at 9:37 AM Richard Biener > <richard.guenther@gmail.com> wrote: > > > > On Sat, Sep 17, 2022 at 10:25 AM Aldy Hernandez via Gccpatches > > <gccpatches@gcc.gnu.org> wrote: > > > > > > Jakub has mentioned that for funsafemathoptimizations we may flush > > > denormals to zero, in which case we need to be careful to extend the > > > ranges to the appropriate zero. This patch does exactly that. For a > > > range of [x, DENORMAL] we flush to [x, 0.0] and for [+DENORMAL, x] > > > we flush to [+0.0, x]. > > > > > > It is unclear whether we should do this for Alpha, since I believe > > > flushing to zero is the default, and the port requires mieee for IEEE > > > sanity. If so, perhaps we should add a target hook so backends are > > > free to request flushing to zero. > > > > > > Thoughts? > > > > I'm not sure what the intention of this is  it effectively results in > > more conservative ranges for funsafemathoptimizations. That is, > > if funsafemathoptimizations says that denormals don't exist and > > are all zero then doesn't that mean we can instead use the smalles > > nondenormal number less than (or greater than) zero here? > > > > That said, the flushing you do is also "valid" for > > fnounsafemathoptimizations > > in case we don't want to deal with denormals in range boundaries. > > > > It might also be a correctness requirement in case we don't know how > > targets handle denormals (IIRC even OS defaults might matter here) so > > we conservatively have to treat them as being flushed to zero. > > Actually, rth suggested we always flush to zero because we don't know > what the target would do. Again, I'm happy to do whatever you agree More specifically, we don't know what the OS will do, so we either should have a flag of some kind set to TRUE by default when unsafe math optimizations, or always assume denormal flushing could happen. Again, no opinion. Up to y'all. Aldy > on. I have no opinion. My main goal here is correctness. > > > > > So ... if we want to be on the "safe" side then please always do this. > > > > At the same point you could do > > > > if (!HONOR_SIGNED_ZEROS ()) > > if (real_iszero (&m_max)) > > { > > if (real_iszero (&m_min)) > > m.min.sign = 1; > > m_max.sign = 1; > > } > > But wouldn't that set [0.0, 0.0] when encountering [+0, +0] ?? > > > else if (real_iszero (&m_min)) > > m_min.sign = 0; > > Jakub actually suggested something completely different...just set +0 > always for !HONOR_SIGNED_ZEROS. > > Aldy > > > > > so that we canonicalize a zero bound so that the sign is known for a range. > > > > Richard. > > > > > gcc/ChangeLog: > > > > > > * valuerange.cc (frange::flush_denormals_to_zero): New. > > > (frange::set): Call flush_denormals_to_zero. > > > * valuerange.h (class frange): Add flush_denormals_to_zero. > > >  > > > gcc/valuerange.cc  24 ++++++++++++++++++++++++ > > > gcc/valuerange.h  1 + > > > 2 files changed, 25 insertions(+) > > > > > > diff git a/gcc/valuerange.cc b/gcc/valuerange.cc > > > index 67d5d7fa90f..f285734f0e0 100644 > > >  a/gcc/valuerange.cc > > > +++ b/gcc/valuerange.cc > > > @@ 267,6 +267,26 @@ tree_compare (tree_code code, tree op1, tree op2) > > > return !integer_zerop (fold_build2 (code, integer_type_node, op1, op2)); > > > } > > > > > > +// Flush denormal endpoints to the appropriate 0.0. > > > + > > > +void > > > +frange::flush_denormals_to_zero () > > > +{ > > > + if (undefined_p ()  known_isnan ()) > > > + return; > > > + > > > + // Flush [x, DENORMAL] to [x, 0.0]. > > > + if (real_isdenormal (&m_max) && real_isneg (&m_max)) > > > + { > > > + m_max = dconst0; > > > + if (HONOR_SIGNED_ZEROS (m_type)) > > > + m_max.sign = 1; > > > + } > > > + // Flush [+DENORMAL, x] to [+0.0, x]. > > > + if (real_isdenormal (&m_min) && !real_isneg (&m_min)) > > > + m_min = dconst0; > > > +} > > > + > > > // Setter for franges. > > > > > > void > > > @@ 317,6 +337,10 @@ frange::set (tree min, tree max, value_range_kind kind) > > > gcc_checking_assert (tree_compare (LE_EXPR, min, max)); > > > > > > normalize_kind (); > > > + > > > + if (flag_unsafe_math_optimizations) > > > + flush_denormals_to_zero (); > > > + > > > if (flag_checking) > > > verify_range (); > > > } > > > diff git a/gcc/valuerange.h b/gcc/valuerange.h > > > index 3a401f3e4e2..795b1f00fdc 100644 > > >  a/gcc/valuerange.h > > > +++ b/gcc/valuerange.h > > > @@ 327,6 +327,7 @@ private: > > > bool union_nans (const frange &); > > > bool intersect_nans (const frange &); > > > bool combine_zeros (const frange &, bool union_p); > > > + void flush_denormals_to_zero (); > > > > > > tree m_type; > > > REAL_VALUE_TYPE m_min; > > >  > > > 2.37.1 > > > > >
On Mon, Sep 19, 2022 at 3:04 PM Aldy Hernandez <aldyh@redhat.com> wrote: > > On Mon, Sep 19, 2022 at 9:37 AM Richard Biener > <richard.guenther@gmail.com> wrote: > > > > On Sat, Sep 17, 2022 at 10:25 AM Aldy Hernandez via Gccpatches > > <gccpatches@gcc.gnu.org> wrote: > > > > > > Jakub has mentioned that for funsafemathoptimizations we may flush > > > denormals to zero, in which case we need to be careful to extend the > > > ranges to the appropriate zero. This patch does exactly that. For a > > > range of [x, DENORMAL] we flush to [x, 0.0] and for [+DENORMAL, x] > > > we flush to [+0.0, x]. > > > > > > It is unclear whether we should do this for Alpha, since I believe > > > flushing to zero is the default, and the port requires mieee for IEEE > > > sanity. If so, perhaps we should add a target hook so backends are > > > free to request flushing to zero. > > > > > > Thoughts? > > > > I'm not sure what the intention of this is  it effectively results in > > more conservative ranges for funsafemathoptimizations. That is, > > if funsafemathoptimizations says that denormals don't exist and > > are all zero then doesn't that mean we can instead use the smalles > > nondenormal number less than (or greater than) zero here? > > > > That said, the flushing you do is also "valid" for > > fnounsafemathoptimizations > > in case we don't want to deal with denormals in range boundaries. > > > > It might also be a correctness requirement in case we don't know how > > targets handle denormals (IIRC even OS defaults might matter here) so > > we conservatively have to treat them as being flushed to zero. > > Actually, rth suggested we always flush to zero because we don't know > what the target would do. Again, I'm happy to do whatever you agree > on. I have no opinion. My main goal here is correctness. Yes, I think we should do this. > > > > So ... if we want to be on the "safe" side then please always do this. > > > > At the same point you could do > > > > if (!HONOR_SIGNED_ZEROS ()) > > if (real_iszero (&m_max)) > > { > > if (real_iszero (&m_min)) > > m.min.sign = 1; > > m_max.sign = 1; > > } > > But wouldn't that set [0.0, 0.0] when encountering [+0, +0] ?? Yeah, that's my laziness not adding a special case for m_min == m_max. > > > else if (real_iszero (&m_min)) > > m_min.sign = 0; > > Jakub actually suggested something completely different...just set +0 > always for !HONOR_SIGNED_ZEROS. Hmm, but the [1, 0.] with known sign becomes [1, +0.] with unknown sign? Richard. > Aldy > > > > > so that we canonicalize a zero bound so that the sign is known for a range. > > > > Richard. > > > > > gcc/ChangeLog: > > > > > > * valuerange.cc (frange::flush_denormals_to_zero): New. > > > (frange::set): Call flush_denormals_to_zero. > > > * valuerange.h (class frange): Add flush_denormals_to_zero. > > >  > > > gcc/valuerange.cc  24 ++++++++++++++++++++++++ > > > gcc/valuerange.h  1 + > > > 2 files changed, 25 insertions(+) > > > > > > diff git a/gcc/valuerange.cc b/gcc/valuerange.cc > > > index 67d5d7fa90f..f285734f0e0 100644 > > >  a/gcc/valuerange.cc > > > +++ b/gcc/valuerange.cc > > > @@ 267,6 +267,26 @@ tree_compare (tree_code code, tree op1, tree op2) > > > return !integer_zerop (fold_build2 (code, integer_type_node, op1, op2)); > > > } > > > > > > +// Flush denormal endpoints to the appropriate 0.0. > > > + > > > +void > > > +frange::flush_denormals_to_zero () > > > +{ > > > + if (undefined_p ()  known_isnan ()) > > > + return; > > > + > > > + // Flush [x, DENORMAL] to [x, 0.0]. > > > + if (real_isdenormal (&m_max) && real_isneg (&m_max)) > > > + { > > > + m_max = dconst0; > > > + if (HONOR_SIGNED_ZEROS (m_type)) > > > + m_max.sign = 1; > > > + } > > > + // Flush [+DENORMAL, x] to [+0.0, x]. > > > + if (real_isdenormal (&m_min) && !real_isneg (&m_min)) > > > + m_min = dconst0; > > > +} > > > + > > > // Setter for franges. > > > > > > void > > > @@ 317,6 +337,10 @@ frange::set (tree min, tree max, value_range_kind kind) > > > gcc_checking_assert (tree_compare (LE_EXPR, min, max)); > > > > > > normalize_kind (); > > > + > > > + if (flag_unsafe_math_optimizations) > > > + flush_denormals_to_zero (); > > > + > > > if (flag_checking) > > > verify_range (); > > > } > > > diff git a/gcc/valuerange.h b/gcc/valuerange.h > > > index 3a401f3e4e2..795b1f00fdc 100644 > > >  a/gcc/valuerange.h > > > +++ b/gcc/valuerange.h > > > @@ 327,6 +327,7 @@ private: > > > bool union_nans (const frange &); > > > bool intersect_nans (const frange &); > > > bool combine_zeros (const frange &, bool union_p); > > > + void flush_denormals_to_zero (); > > > > > > tree m_type; > > > REAL_VALUE_TYPE m_min; > > >  > > > 2.37.1 > > > > > >
On Mon, Sep 19, 2022 at 3:45 PM Richard Biener <richard.guenther@gmail.com> wrote: > > On Mon, Sep 19, 2022 at 3:04 PM Aldy Hernandez <aldyh@redhat.com> wrote: > > > > On Mon, Sep 19, 2022 at 9:37 AM Richard Biener > > <richard.guenther@gmail.com> wrote: > > > > > > On Sat, Sep 17, 2022 at 10:25 AM Aldy Hernandez via Gccpatches > > > <gccpatches@gcc.gnu.org> wrote: > > > > > > > > Jakub has mentioned that for funsafemathoptimizations we may flush > > > > denormals to zero, in which case we need to be careful to extend the > > > > ranges to the appropriate zero. This patch does exactly that. For a > > > > range of [x, DENORMAL] we flush to [x, 0.0] and for [+DENORMAL, x] > > > > we flush to [+0.0, x]. > > > > > > > > It is unclear whether we should do this for Alpha, since I believe > > > > flushing to zero is the default, and the port requires mieee for IEEE > > > > sanity. If so, perhaps we should add a target hook so backends are > > > > free to request flushing to zero. > > > > > > > > Thoughts? > > > > > > I'm not sure what the intention of this is  it effectively results in > > > more conservative ranges for funsafemathoptimizations. That is, > > > if funsafemathoptimizations says that denormals don't exist and > > > are all zero then doesn't that mean we can instead use the smalles > > > nondenormal number less than (or greater than) zero here? > > > > > > That said, the flushing you do is also "valid" for > > > fnounsafemathoptimizations > > > in case we don't want to deal with denormals in range boundaries. > > > > > > It might also be a correctness requirement in case we don't know how > > > targets handle denormals (IIRC even OS defaults might matter here) so > > > we conservatively have to treat them as being flushed to zero. > > > > Actually, rth suggested we always flush to zero because we don't know > > what the target would do. Again, I'm happy to do whatever you agree > > on. I have no opinion. My main goal here is correctness. > > Yes, I think we should do this. Ok, I'm pushing the attached patch because it's conservatively correct everywhere. We can tweak it when y'all reach consensus. > > > > > > > So ... if we want to be on the "safe" side then please always do this. > > > > > > At the same point you could do > > > > > > if (!HONOR_SIGNED_ZEROS ()) > > > if (real_iszero (&m_max)) > > > { > > > if (real_iszero (&m_min)) > > > m.min.sign = 1; > > > m_max.sign = 1; > > > } > > > > But wouldn't that set [0.0, 0.0] when encountering [+0, +0] ?? > > Yeah, that's my laziness not adding a special case for m_min == m_max. > > > > > > else if (real_iszero (&m_min)) > > > m_min.sign = 0; > > > > Jakub actually suggested something completely different...just set +0 > > always for !HONOR_SIGNED_ZEROS. > > Hmm, but the [1, 0.] with known sign becomes [1, +0.] with unknown sign? Huh. I guess that's true. Does that happen often enough in practice to matter? I suppose we're going into uncharted territory with folks asking for no signs on zeros, but them appearing in the source code? My gut feeling is that we should take whatever signs are in the source code (similar to what we're doing for NANs), but don't introduce any additional ones. Again, no strong opinions. Feel free to add whatever adjustment you think is necessary. Aldy
On Tue, Sep 20, 2022 at 07:22:03AM +0200, Aldy Hernandez wrote: > > > Jakub actually suggested something completely different...just set +0 > > > always for !HONOR_SIGNED_ZEROS. > > > > Hmm, but the [1, 0.] with known sign becomes [1, +0.] with unknown sign? > > Huh. I guess that's true. Does that happen often enough in practice Sure, if you fnosignedzeros/ffastmath and some variable can be zero, copysign/signbit is undefined. The option basically asserts you don't care about it... Jakub
diff git a/gcc/valuerange.cc b/gcc/valuerange.cc index 67d5d7fa90f..f285734f0e0 100644  a/gcc/valuerange.cc +++ b/gcc/valuerange.cc @@ 267,6 +267,26 @@ tree_compare (tree_code code, tree op1, tree op2) return !integer_zerop (fold_build2 (code, integer_type_node, op1, op2)); } +// Flush denormal endpoints to the appropriate 0.0. + +void +frange::flush_denormals_to_zero () +{ + if (undefined_p ()  known_isnan ()) + return; + + // Flush [x, DENORMAL] to [x, 0.0]. + if (real_isdenormal (&m_max) && real_isneg (&m_max)) + { + m_max = dconst0; + if (HONOR_SIGNED_ZEROS (m_type)) + m_max.sign = 1; + } + // Flush [+DENORMAL, x] to [+0.0, x]. + if (real_isdenormal (&m_min) && !real_isneg (&m_min)) + m_min = dconst0; +} + // Setter for franges. void @@ 317,6 +337,10 @@ frange::set (tree min, tree max, value_range_kind kind) gcc_checking_assert (tree_compare (LE_EXPR, min, max)); normalize_kind (); + + if (flag_unsafe_math_optimizations) + flush_denormals_to_zero (); + if (flag_checking) verify_range (); } diff git a/gcc/valuerange.h b/gcc/valuerange.h index 3a401f3e4e2..795b1f00fdc 100644  a/gcc/valuerange.h +++ b/gcc/valuerange.h @@ 327,6 +327,7 @@ private: bool union_nans (const frange &); bool intersect_nans (const frange &); bool combine_zeros (const frange &, bool union_p); + void flush_denormals_to_zero (); tree m_type; REAL_VALUE_TYPE m_min;