From patchwork Mon Dec 5 10:29:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 61471 Return-Path: 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 E7C3C382EF15 for ; Mon, 5 Dec 2022 10:29:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E7C3C382EF15 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670236191; bh=eaAbc5kYvJOxvAMC0jkuIanHJQJ1RFdMHYS90kcNV7w=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=jCVVwuB1FyJkhwMp2LIMY79tzIgRNvijPhg3fwd7exx9F5WOGomEyy7U3QrYdy7ur WITLWvdsd9AdfzzxE0dv56IjJO7nVdp6WVvdT5wpwQKZ/7VVGKMrDcRXNR544ZBGgs lrB5ODAWIc/F/v8QZ/tC4koixC6ogQlnPJxoPKQM= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 2AEF6382FCB9 for ; Mon, 5 Dec 2022 10:29:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2AEF6382FCB9 Received: from mimecast-mx02.redhat.com (mimecast-mx02.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 us-mta-219-PZD2URW_OK2N22hNrCi6wg-1; Mon, 05 Dec 2022 05:29:21 -0500 X-MC-Unique: PZD2URW_OK2N22hNrCi6wg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0C180101A54E; Mon, 5 Dec 2022 10:29:21 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.114]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 54F81C1908B; Mon, 5 Dec 2022 10:29:20 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2B5ATDNF3411356 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 5 Dec 2022 11:29:14 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2B5ATCSG3411355; Mon, 5 Dec 2022 11:29:12 +0100 Date: Mon, 5 Dec 2022 11:29:12 +0100 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] match.pd: Don't fold nan < x etc. for -ftrapping-math [PR106805] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! As reported in the PR, the following pr106805.c testcase is miscompiled with the default -ftrapping-math, because we fold all the comparisons into constants and don't raise any exceptions. The match.pd pattern handles just simple comparisons, from those EQ/NE are quiet and don't raise exceptions on anything but sNaN, while GT/GE/LT/LE are signaling and do raise exceptions even on qNaN. fold_relational_const handles this IMHO correctly: /* Handle the cases where either operand is a NaN. */ if (real_isnan (c0) || real_isnan (c1)) { switch (code) { case EQ_EXPR: case ORDERED_EXPR: result = 0; break; case NE_EXPR: case UNORDERED_EXPR: case UNLT_EXPR: case UNLE_EXPR: case UNGT_EXPR: case UNGE_EXPR: case UNEQ_EXPR: result = 1; break; case LT_EXPR: case LE_EXPR: case GT_EXPR: case GE_EXPR: case LTGT_EXPR: if (flag_trapping_math) return NULL_TREE; result = 0; break; default: gcc_unreachable (); } return constant_boolean_node (result, type); } by folding the signaling comparisons only if -fno-trapping-math. The following patch does the same in match.pd. Unfortunately the pr106805.c testcase still fails, but no longer because of match.pd, but on the trunk because of the still unresolved ranger problems (same issue as for fold-overflow-1.c etc.) and on 12 branch (and presumably trunk too) somewhere during expansion the comparisons are also expanded into constants (which is ok for -fno-trapping-math, but not ok with that). Though, I think the patch is a small step in the direction, so I'd like to commit this patch without the gcc.dg/pr106805.c testcase for now. Bootstrapped/regtested on x86_64-linux and i686-linux, appart from the +FAIL: gcc.dg/pr106805.c execution test nothing else appears. Ok for trunk without the last testcase? 2022-12-05 Jakub Jelinek PR middle-end/106805 * match.pd (cmp @0 REAL_CST@1): Don't optimize x cmp NaN or NaN cmp x to false/true for cmp >/>=/ QNAN; - /* { dg-final { scan-tree-dump "nonfinite_1 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_1 = \\(float\\)" "original" } } */ } { volatile int nonfinite_2; nonfinite_2 = (float) x >= QNAN; - /* { dg-final { scan-tree-dump "nonfinite_2 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_2 = \\(float\\)" "original" } } */ } { volatile int nonfinite_3; nonfinite_3 = (float) x < QNAN; - /* { dg-final { scan-tree-dump "nonfinite_3 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_3 = \\(float\\)" "original" } } */ } { volatile int nonfinite_4; nonfinite_4 = (float) x <= QNAN; - /* { dg-final { scan-tree-dump "nonfinite_4 = 0" "original" } } */ + /* { dg-final { scan-tree-dump "nonfinite_4 = \\(float\\)" "original" } } */ } { --- gcc/testsuite/c-c++-common/pr57371-5.c.jj 2022-12-02 13:34:14.397714696 +0100 +++ gcc/testsuite/c-c++-common/pr57371-5.c 2022-12-02 13:35:05.915949599 +0100 @@ -0,0 +1,47 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fno-signaling-nans -fno-trapping-math -fdump-tree-original" } */ + +/* We can not get rid of comparison in tests below because of + pending NaN exceptions. + + TODO: avoid under -fno-trapping-math. */ + +#define QNAN __builtin_nanf ("0") + +void nonfinite(unsigned short x) { + { + volatile int nonfinite_1; + nonfinite_1 = (float) x > QNAN; + /* { dg-final { scan-tree-dump "nonfinite_1 = 0" "original" } } */ + } + + { + volatile int nonfinite_2; + nonfinite_2 = (float) x >= QNAN; + /* { dg-final { scan-tree-dump "nonfinite_2 = 0" "original" } } */ + } + + { + volatile int nonfinite_3; + nonfinite_3 = (float) x < QNAN; + /* { dg-final { scan-tree-dump "nonfinite_3 = 0" "original" } } */ + } + + { + volatile int nonfinite_4; + nonfinite_4 = (float) x <= QNAN; + /* { dg-final { scan-tree-dump "nonfinite_4 = 0" "original" } } */ + } + + { + volatile int nonfinite_11; + nonfinite_11 = (float) x == QNAN; + /* { dg-final { scan-tree-dump "nonfinite_11 = 0" "original" } } */ + } + + { + volatile int nonfinite_12; + nonfinite_12 = (float) x != QNAN; + /* { dg-final { scan-tree-dump "nonfinite_12 = 1" "original" } } */ + } +} --- gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x.jj 2020-01-14 20:02:47.175603962 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-6.x 2022-12-05 10:44:10.037871848 +0100 @@ -1,3 +1,4 @@ +lappend additional_flags "-fno-trapping-math" # The ARM VxWorks kernel uses an external floating-point library in # which routines like __ledf2 are just aliases for __cmpdf2. These # routines therefore don't handle NaNs correctly. --- gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c.jj 2022-12-05 10:44:21.406705781 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.c 2022-12-05 10:44:57.237182398 +0100 @@ -0,0 +1,31 @@ + +const double dnan = 1.0/0.0 - 1.0/0.0; +double x = 1.0; + +extern void link_error (void); +extern void abort (void); + +main () +{ +#if ! defined (__vax__) && ! defined (_CRAY) + /* NaN is an IEEE unordered operand. All these test should be false. */ + if (dnan == dnan) + link_error (); + if (dnan != x) + x = 1.0; + else + link_error (); + + if (dnan == x) + link_error (); +#endif + exit (0); +} + +#ifndef __OPTIMIZE__ +void link_error (void) +{ + abort (); +} +#endif + --- gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x.jj 2022-12-05 10:44:30.338575309 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-9.x 2022-12-05 10:44:32.991536565 +0100 @@ -0,0 +1,16 @@ +# The ARM VxWorks kernel uses an external floating-point library in +# which routines like __ledf2 are just aliases for __cmpdf2. These +# routines therefore don't handle NaNs correctly. +if [istarget "arm*-*-vxworks*"] { + set torture_eval_before_execute { + global compiler_conditional_xfail_data + set compiler_conditional_xfail_data { + "The ARM kernel uses a flawed floating-point library." + { "*-*-*" } + { "-O0" } + { "-mrtp" } + } + } +} + +return 0 --- gcc/testsuite/gcc.dg/pr106805.c.jj 2022-12-02 13:02:06.813354371 +0100 +++ gcc/testsuite/gcc.dg/pr106805.c 2022-12-02 13:11:42.844792390 +0100 @@ -0,0 +1,29 @@ +/* PR middle-end/106805 */ +/* { dg-do run } */ +/* { dg-options "-O2 -ftrapping-math" } */ +/* { dg-add-options ieee } */ +/* { dg-require-effective-target fenv_exceptions_double } */ + +#include +#include + +static inline int +foo (double x, double y) +{ + return x >= y && x <= y; +} + +int +main () +{ + double x = __builtin_nan (""); + volatile int r; + r = foo (__builtin_nan (""), 1.0); + if (!fetestexcept (FE_INVALID)) + abort (); + feclearexcept (FE_ALL_EXCEPT); + r = foo (x, __builtin_inf ()); + if (!fetestexcept (FE_INVALID)) + abort (); + return 0; +}