Message ID | 20221025210140.125230-1-aldyh@redhat.com |
---|---|
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 15CD9385741E for <patchwork@sourceware.org>; Tue, 25 Oct 2022 21:03:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15CD9385741E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666731782; bh=FetIeloKHqwNqz3V/j8hFGzj25NeBbo1Q1+Q1VBysWg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=F4rwchGXbgQ793m+mEjyOUhLt9E7S5vrb9HsIZ8v71JY22HgA+XKJUcT2S9GsgpXN RjDDEFPV1Su4Hu70PMzohOsPoHU4e2c/v8vv5ph4p1eAGClHcfqJOt7olClvcrbTUV St0ye+xl3qL4cl4rJbdVsp+DjZaL2JzbHfpjZkIc= 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 562AC3857421 for <gcc-patches@gcc.gnu.org>; Tue, 25 Oct 2022 21:02:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 562AC3857421 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-290-tY0kK5ugOh2sU2JZKPSkUQ-1; Tue, 25 Oct 2022 17:02:29 -0400 X-MC-Unique: tY0kK5ugOh2sU2JZKPSkUQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6DC963C01D8F; Tue, 25 Oct 2022 21:02:24 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.78]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C50692166B2A; Tue, 25 Oct 2022 21:02:16 +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 29PL2DTg125263 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 25 Oct 2022 23:02:13 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 29PL2DZ5125262; Tue, 25 Oct 2022 23:02:13 +0200 To: Richard Biener <richard.guenther@gmail.com> Subject: [PATCH] [PR tree-optimization/107394] Canonicalize global franges as they are read back. Date: Tue, 25 Oct 2022 23:01:40 +0200 Message-Id: <20221025210140.125230-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 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_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Aldy Hernandez via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Aldy Hernandez <aldyh@redhat.com> Cc: Jakub Jelinek <jakub@redhat.com>, GCC patches <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 |
[PR,tree-optimization/107394] Canonicalize global franges as they are read back.
|
|
Commit Message
Aldy Hernandez
Oct. 25, 2022, 9:01 p.m. UTC
[Richi/Jakub/FP experts, does this sound like the right solution, or am I missing some subtle IPA/inlining issue?] The problem here is that we're inlining a global range with NANs into a function that has been tagged with __attribute__((optimize ("-ffinite-math-only"))). As the global range is copied from SSA_NAME_RANGE_INFO, its NAN bits are copied, which then cause frange::verify_range() to fail a sanity check making sure no NANs creep in when !HONOR_NANS. I think what we should do is nuke the NAN bits as we're restoring the global range. For that matter, if we use the frange constructor, everything except that NAN sign will be done automatically, including dropping INFs to the min/max representable range when appropriate. PR tree-optimization/107394 gcc/ChangeLog: * value-range-storage.cc (frange_storage_slot::get_frange): Use frange constructor. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr107394.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/pr107394.c | 22 ++++++++++++++++++++++ gcc/value-range-storage.cc | 18 +++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr107394.c
Comments
Updated tested patch. Aldy On Tue, Oct 25, 2022 at 11:02 PM Aldy Hernandez <aldyh@redhat.com> wrote: > > [Richi/Jakub/FP experts, does this sound like the right solution, or am I > missing some subtle IPA/inlining issue?] > > The problem here is that we're inlining a global range with NANs into > a function that has been tagged with __attribute__((optimize > ("-ffinite-math-only"))). As the global range is copied from > SSA_NAME_RANGE_INFO, its NAN bits are copied, which then cause > frange::verify_range() to fail a sanity check making sure no NANs > creep in when !HONOR_NANS. > > I think what we should do is nuke the NAN bits as we're restoring the > global range. For that matter, if we use the frange constructor, > everything except that NAN sign will be done automatically, including > dropping INFs to the min/max representable range when appropriate. > > PR tree-optimization/107394 > > gcc/ChangeLog: > > * value-range-storage.cc (frange_storage_slot::get_frange): Use > frange constructor. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/pr107394.c: New test. > --- > gcc/testsuite/gcc.dg/tree-ssa/pr107394.c | 22 ++++++++++++++++++++++ > gcc/value-range-storage.cc | 18 +++++++----------- > 2 files changed, 29 insertions(+), 11 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr107394.c > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr107394.c b/gcc/testsuite/gcc.dg/tree-ssa/pr107394.c > new file mode 100644 > index 00000000000..0e1e5ac40ce > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr107394.c > @@ -0,0 +1,22 @@ > +// { dg-do compile } > +// { dg-options "-O2" } > + > +static double > +quux (double x) > +{ > + return __builtin_fabs (x); > +} > + > +__attribute__ ((flatten, optimize ("-ffinite-math-only"))) static int > +bar (int *p) > +{ > + *p = quux (0.0); > + > + return 0; > +} > + > +void > +foo (int *p) > +{ > + (void) bar (p); > +} > diff --git a/gcc/value-range-storage.cc b/gcc/value-range-storage.cc > index 6e054622830..b660102e064 100644 > --- a/gcc/value-range-storage.cc > +++ b/gcc/value-range-storage.cc > @@ -261,17 +261,13 @@ frange_storage_slot::get_frange (frange &r, tree type) const > { > gcc_checking_assert (r.supports_type_p (type)); > > - r.set_undefined (); > - r.m_kind = m_kind; > - r.m_type = type; > - r.m_min = m_min; > - r.m_max = m_max; > - r.m_pos_nan = m_pos_nan; > - r.m_neg_nan = m_neg_nan; > - r.normalize_kind (); > - > - if (flag_checking) > - r.verify_range (); > + // Use the constructor because it will canonicalize the range. > + r = frange (type, m_min, m_max, m_kind); > + > + // The constructor will set the NAN bits for HONOR_NANS, but we must > + // make sure to set the NAN sign if known. > + if (HONOR_NANS (type) && (m_pos_nan ^ m_neg_nan) == 1) > + r.update_nan (m_neg_nan); > } > > bool > -- > 2.37.3 >
On 10/25/22 15:01, Aldy Hernandez via Gcc-patches wrote: > [Richi/Jakub/FP experts, does this sound like the right solution, or am I > missing some subtle IPA/inlining issue?] > > The problem here is that we're inlining a global range with NANs into > a function that has been tagged with __attribute__((optimize > ("-ffinite-math-only"))). As the global range is copied from > SSA_NAME_RANGE_INFO, its NAN bits are copied, which then cause > frange::verify_range() to fail a sanity check making sure no NANs > creep in when !HONOR_NANS. > > I think what we should do is nuke the NAN bits as we're restoring the > global range. For that matter, if we use the frange constructor, > everything except that NAN sign will be done automatically, including > dropping INFs to the min/max representable range when appropriate. > > PR tree-optimization/107394 > > gcc/ChangeLog: > > * value-range-storage.cc (frange_storage_slot::get_frange): Use > frange constructor. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/pr107394.c: New test. The other approach would be to disabling inlining in this case due to an unsafe attribute mismatch, but we're not currently doing much sanity checking in this space and it might be a huge can of worms. I'm inclined to ACK, but give Jakub and Richi until Monday to chime in first. jeff
On Fri, Oct 28, 2022 at 12:45 AM Jeff Law <jeffreyalaw@gmail.com> wrote: > > > On 10/25/22 15:01, Aldy Hernandez via Gcc-patches wrote: > > [Richi/Jakub/FP experts, does this sound like the right solution, or am I > > missing some subtle IPA/inlining issue?] > > > > The problem here is that we're inlining a global range with NANs into > > a function that has been tagged with __attribute__((optimize > > ("-ffinite-math-only"))). As the global range is copied from > > SSA_NAME_RANGE_INFO, its NAN bits are copied, which then cause > > frange::verify_range() to fail a sanity check making sure no NANs > > creep in when !HONOR_NANS. > > > > I think what we should do is nuke the NAN bits as we're restoring the > > global range. For that matter, if we use the frange constructor, > > everything except that NAN sign will be done automatically, including > > dropping INFs to the min/max representable range when appropriate. > > > > PR tree-optimization/107394 > > > > gcc/ChangeLog: > > > > * value-range-storage.cc (frange_storage_slot::get_frange): Use > > frange constructor. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/tree-ssa/pr107394.c: New test. > > The other approach would be to disabling inlining in this case due to an > unsafe attribute mismatch, but we're not currently doing much sanity > checking in this space and it might be a huge can of worms. I'm > inclined to ACK, but give Jakub and Richi until Monday to chime in first. We are actually quite careful in this regard but maybe our reasoning is wrong. We are allowing inlining of -fno-finite-math-only into -ffinite-math-only code but not the other way around. On the actual patch I think that ranges with Inf/NaNs should be always treated as "valid", the optimization to trim them with certain options is optimization and thus optional. So IMHO having verify_range ICE on NaNs isn't correct? That said, the patch is in line with what we do elsewhere at the moment, so I guess OK. Richard. > > jeff >
On Fri, Oct 28, 2022 at 8:48 AM Richard Biener <richard.guenther@gmail.com> wrote: > > On Fri, Oct 28, 2022 at 12:45 AM Jeff Law <jeffreyalaw@gmail.com> wrote: > > > > > > On 10/25/22 15:01, Aldy Hernandez via Gcc-patches wrote: > > > [Richi/Jakub/FP experts, does this sound like the right solution, or am I > > > missing some subtle IPA/inlining issue?] > > > > > > The problem here is that we're inlining a global range with NANs into > > > a function that has been tagged with __attribute__((optimize > > > ("-ffinite-math-only"))). As the global range is copied from > > > SSA_NAME_RANGE_INFO, its NAN bits are copied, which then cause > > > frange::verify_range() to fail a sanity check making sure no NANs > > > creep in when !HONOR_NANS. > > > > > > I think what we should do is nuke the NAN bits as we're restoring the > > > global range. For that matter, if we use the frange constructor, > > > everything except that NAN sign will be done automatically, including > > > dropping INFs to the min/max representable range when appropriate. > > > > > > PR tree-optimization/107394 > > > > > > gcc/ChangeLog: > > > > > > * value-range-storage.cc (frange_storage_slot::get_frange): Use > > > frange constructor. > > > > > > gcc/testsuite/ChangeLog: > > > > > > * gcc.dg/tree-ssa/pr107394.c: New test. > > > > The other approach would be to disabling inlining in this case due to an > > unsafe attribute mismatch, but we're not currently doing much sanity > > checking in this space and it might be a huge can of worms. I'm > > inclined to ACK, but give Jakub and Richi until Monday to chime in first. > > We are actually quite careful in this regard but maybe our reasoning > is wrong. We are allowing inlining of -fno-finite-math-only into > -ffinite-math-only code but not the other way around. > > On the actual patch I think that ranges with Inf/NaNs should be always > treated as "valid", the optimization to trim them with certain options > is optimization and thus optional. So IMHO having verify_range ICE > on NaNs isn't correct? Just to make a point here - in functions with -ffinite-math-only in effect volatile double x = __builtin_nan(""); will still have a literal NaN in the IL and that's not invalid GIMPLE. You cannot assume that no NaNs appear with -ffinite-math-only, you just don't need to specially are about preserving them. > That said, the patch is in line with what we do elsewhere at the moment, > so I guess OK. > > Richard. > > > > > jeff > >
On Fri, Oct 28, 2022, 08:49 Richard Biener <richard.guenther@gmail.com> wrote: > On Fri, Oct 28, 2022 at 12:45 AM Jeff Law <jeffreyalaw@gmail.com> wrote: > > > > > > On 10/25/22 15:01, Aldy Hernandez via Gcc-patches wrote: > > > [Richi/Jakub/FP experts, does this sound like the right solution, or > am I > > > missing some subtle IPA/inlining issue?] > > > > > > The problem here is that we're inlining a global range with NANs into > > > a function that has been tagged with __attribute__((optimize > > > ("-ffinite-math-only"))). As the global range is copied from > > > SSA_NAME_RANGE_INFO, its NAN bits are copied, which then cause > > > frange::verify_range() to fail a sanity check making sure no NANs > > > creep in when !HONOR_NANS. > > > > > > I think what we should do is nuke the NAN bits as we're restoring the > > > global range. For that matter, if we use the frange constructor, > > > everything except that NAN sign will be done automatically, including > > > dropping INFs to the min/max representable range when appropriate. > > > > > > PR tree-optimization/107394 > > > > > > gcc/ChangeLog: > > > > > > * value-range-storage.cc (frange_storage_slot::get_frange): Use > > > frange constructor. > > > > > > gcc/testsuite/ChangeLog: > > > > > > * gcc.dg/tree-ssa/pr107394.c: New test. > > > > The other approach would be to disabling inlining in this case due to an > > unsafe attribute mismatch, but we're not currently doing much sanity > > checking in this space and it might be a huge can of worms. I'm > > inclined to ACK, but give Jakub and Richi until Monday to chime in first. > > We are actually quite careful in this regard but maybe our reasoning > is wrong. We are allowing inlining of -fno-finite-math-only into > -ffinite-math-only code but not the other way around. > > On the actual patch I think that ranges with Inf/NaNs should be always > treated as "valid", the optimization to trim them with certain options > is optimization and thus optional. So IMHO having verify_range ICE > on NaNs isn't correct? > That was my gut feeling as well, but the assert has caught real issues such as this one. Also, in your example down thread, we would drop the explicit NAN to UNDEFINED if expressed as a range (as agreed earlier this cycle). So we won't ICE...since a range with NAN will never get built. The assert is there to keep NANs from sneaking in. However, if you still think it's incorrect I'm happy to remove it. > That said, the patch is in line with what we do elsewhere at the moment, > so I guess OK. > Thanks. Aldy > Richard. > > > > > jeff > > > >
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr107394.c b/gcc/testsuite/gcc.dg/tree-ssa/pr107394.c new file mode 100644 index 00000000000..0e1e5ac40ce --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr107394.c @@ -0,0 +1,22 @@ +// { dg-do compile } +// { dg-options "-O2" } + +static double +quux (double x) +{ + return __builtin_fabs (x); +} + +__attribute__ ((flatten, optimize ("-ffinite-math-only"))) static int +bar (int *p) +{ + *p = quux (0.0); + + return 0; +} + +void +foo (int *p) +{ + (void) bar (p); +} diff --git a/gcc/value-range-storage.cc b/gcc/value-range-storage.cc index 6e054622830..b660102e064 100644 --- a/gcc/value-range-storage.cc +++ b/gcc/value-range-storage.cc @@ -261,17 +261,13 @@ frange_storage_slot::get_frange (frange &r, tree type) const { gcc_checking_assert (r.supports_type_p (type)); - r.set_undefined (); - r.m_kind = m_kind; - r.m_type = type; - r.m_min = m_min; - r.m_max = m_max; - r.m_pos_nan = m_pos_nan; - r.m_neg_nan = m_neg_nan; - r.normalize_kind (); - - if (flag_checking) - r.verify_range (); + // Use the constructor because it will canonicalize the range. + r = frange (type, m_min, m_max, m_kind); + + // The constructor will set the NAN bits for HONOR_NANS, but we must + // make sure to set the NAN sign if known. + if (HONOR_NANS (type) && (m_pos_nan ^ m_neg_nan) == 1) + r.update_nan (m_neg_nan); } bool