From patchwork Thu Mar 9 08:29:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 66682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp168943wrd; Thu, 9 Mar 2023 00:30:00 -0800 (PST) X-Google-Smtp-Source: AK7set8rj/O1qdKulcHWs2lrNnh904ShaInwTRZdsZvmuBpRllDo8WQsSmTCzxH7g9HFLICBboMZ X-Received: by 2002:a17:906:6d98:b0:8b1:7e1e:7756 with SMTP id h24-20020a1709066d9800b008b17e1e7756mr19768821ejt.73.1678350600053; Thu, 09 Mar 2023 00:30:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678350600; cv=none; d=google.com; s=arc-20160816; b=whCMXWl5NGE3+6uXtH5XCaIptAlfcSV7Lzfj3bnFfWWGvTg8h2KmUXRpme7E6qZq7i 5jjsbmESN0K/KyHEnLFHA4CmWwhO6vnu/NIifuATtaCrIA/lqG3HjJw9jCYPvgEyOTSH K0nNRyR0KzvXL7nFpvbXauDmebhMm5SAucdEhMoHbHPR2zkl+OdDCq4yobUfQFwe+Gw0 gFOsanDwmdOu9wnbsNaM6rv1nsRAu3idM5oNuHmz+mi1qsyGMm8yqzGjmPUxEx+MWAle zgbbuvplCPcQmWjphBioSYFYCPrXeKin6MY4eejBZMQXTYt5I9DOHPxGy1CybhH1C8ci GSow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=1eyGadaxc9/7KTO/C9iLsTqUl5U2Kao4Qbu+6ali6cA=; b=qVcD6+8O5BGg1+GHqoqHH5rwO5CzEDWHe1+gSJq9+CQA0XvyvSDkkpoS7TL1XFklcG uBkhf5GfMEsCXSfr81gdS/mlBpvNl4yPi/fcAw4h9MlDl0qS/MB9U5gaIFJwYJLXNquA 85XxDcKJhV7u8emTGIRirw7ZqBtAdLTQeYPwbecSouiRs5cXS15o65wHTZ9s9gc6U6c2 tF0QyYOfRXIYNe2CKV2pu+DLtd9NZjyfn/UcMjuVngLLitlrNz+JMmPHOBmTX1WrrsFc 4A44x1fl6Lt/QXg1nDlQ8DNDTH/7R4CT0VVbZ0JmrHjmZM8uX6X4h1G+GOycGv736+5z 560g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=SwdOxYSy; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ho11-20020a1709070e8b00b008d399f4f54asi55989ejc.750.2023.03.09.00.29.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 00:30:00 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=SwdOxYSy; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D44133858298 for ; Thu, 9 Mar 2023 08:29:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D44133858298 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678350598; bh=1eyGadaxc9/7KTO/C9iLsTqUl5U2Kao4Qbu+6ali6cA=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=SwdOxYSyW5q5DinJU+bJFBWR0m7lWzuKLLAY4sOmBlmDOO91umCEcGVkZpGUaHpzP qZIMt/yB0SZWqPJCas/163nO0AIU4A5Ax+OC21eYVrgFmnWbOxmox35gxVv2tg0ZfW AaUVRKsDmLfcF1Uge0Hhlgokg1+Fva7KsP458rqM= 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 78040385B51B for ; Thu, 9 Mar 2023 08:29:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 78040385B51B 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-620-J0D03j5EPu2g_j_0WVWh5g-1; Thu, 09 Mar 2023 03:29:07 -0500 X-MC-Unique: J0D03j5EPu2g_j_0WVWh5g-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ACF86185A78B; Thu, 9 Mar 2023 08:29:06 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6E0BE2026D4B; Thu, 9 Mar 2023 08:29:06 +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 3298T3PP695675 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 9 Mar 2023 09:29:04 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3298T38w695674; Thu, 9 Mar 2023 09:29:03 +0100 Date: Thu, 9 Mar 2023 09:29:02 +0100 To: Richard Biener , Aldy Hernandez Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] range-op-float: Fix up reverse binary operations [PR109008] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759878158906370600?= X-GMAIL-MSGID: =?utf-8?q?1759878158906370600?= Hi! The following testcase is reduced from miscompilation of scipy package. If we have say lhs = [1., 1.] - [1., 1.] and want to compute the range of lhs from it, we correctly determine it is [0., 0.] (if computations are exact, we generally don't try to round them further in frange_arithmetic). In the testcase it is about a reverse operation, [1., 1.] = op1 + [1., 1.] and we want to compute range of op1 from that. Right now we just perform the inverse operation (there are some corner cases about NaN and infinities handling) and so arrive to range [0., 0.] as well, and because it is a singleton, optimize return eps; to return 0. That is incorrect though, for the reverse ops we need to take into account also rounding, the right exact range is [-0x1.0p-54, 0x1.0p-53] in this case when rounding to nearest, i.e. all numbers which added to 1. with round to nearest still produce 1. The problem isn't solely on singleton ranges, and isn't solely on results around zero. We basically need to consider also values where the result is up to 0.5ulp away from the lhs range boundaries in each direction. The following patch fixes it by extending the lhs range for the reverse operations by 1ulp in each direction. The PR contains a pseudo-random test generator I've used to generate 300000 tests of + and - and then used the same test with * and / instead of + and - together with a hack to print the discovered ranges by the patch in a form that another test could then verify the range is conservatively correct and how far it is from a minimal range. I believe the results are good enough for now, though plan to look incrementally into trying to do something better on the -XXX_MAX or XXX_MAX boundaries (where I think frange_nextafter will use -inf or +inf) and also try to increase the range just by 0.5ulp rather than 1ulp if !flag_rounding_math. But dunno if either of those will be doable and will pass the testing, so I think it is worth committing this fix first. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-03-09 Jakub Jelinek Richard Biener PR tree-optimization/109008 * range-op-float.cc (float_widen_lhs_range): New function. (foperator_plus::op1_range, foperator_minus::op1_range, foperator_minus::op2_range, foperator_mult::op1_range, foperator_div::op1_range, foperator_div::op2_range): Use it. * gcc.c-torture/execute/ieee/pr109008.c: New test. Jakub --- gcc/range-op-float.cc.jj 2023-03-08 12:33:44.641043477 +0100 +++ gcc/range-op-float.cc 2023-03-08 13:13:09.015341002 +0100 @@ -2199,6 +2199,33 @@ zero_to_inf_range (REAL_VALUE_TYPE &lb, } } +/* Extend the LHS range by 1ulp in each direction. For op1_range + or op2_range of binary operations just computing the inverse + operation on ranges isn't sufficient. Consider e.g. + [1., 1.] = op1 + [1., 1.]. op1's range is not [0., 0.], but + [-0x1.0p-54, 0x1.0p-53] (when not -frounding-math), any value for + which adding 1. to it results in 1. after rounding to nearest. + So, for op1_range/op2_range extend the lhs range by 1ulp in each + direction. See PR109008 for more details. */ + +static frange +float_widen_lhs_range (tree type, const frange &lhs) +{ + frange ret = lhs; + if (lhs.known_isnan ()) + return ret; + REAL_VALUE_TYPE lb = lhs.lower_bound (); + REAL_VALUE_TYPE ub = lhs.upper_bound (); + if (real_isfinite (&lb)) + frange_nextafter (TYPE_MODE (type), lb, dconstninf); + if (real_isfinite (&ub)) + frange_nextafter (TYPE_MODE (type), ub, dconstinf); + ret.set (type, lb, ub); + ret.clear_nan (); + ret.union_ (lhs); + return ret; +} + class foperator_plus : public range_operator_float { using range_operator_float::op1_range; @@ -2214,8 +2241,9 @@ public: range_op_handler minus (MINUS_EXPR, type); if (!minus) return false; - return float_binary_op_range_finish (minus.fold_range (r, type, lhs, op2), - r, type, lhs); + frange wlhs = float_widen_lhs_range (type, lhs); + return float_binary_op_range_finish (minus.fold_range (r, type, wlhs, op2), + r, type, wlhs); } virtual bool op2_range (frange &r, tree type, const frange &lhs, @@ -2260,9 +2288,10 @@ public: { if (lhs.undefined_p ()) return false; - return float_binary_op_range_finish (fop_plus.fold_range (r, type, lhs, + frange wlhs = float_widen_lhs_range (type, lhs); + return float_binary_op_range_finish (fop_plus.fold_range (r, type, wlhs, op2), - r, type, lhs); + r, type, wlhs); } virtual bool op2_range (frange &r, tree type, const frange &lhs, @@ -2271,8 +2300,9 @@ public: { if (lhs.undefined_p ()) return false; - return float_binary_op_range_finish (fold_range (r, type, op1, lhs), - r, type, lhs); + frange wlhs = float_widen_lhs_range (type, lhs); + return float_binary_op_range_finish (fold_range (r, type, op1, wlhs), + r, type, wlhs); } private: void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, bool &maybe_nan, @@ -2338,13 +2368,14 @@ public: range_op_handler rdiv (RDIV_EXPR, type); if (!rdiv) return false; - bool ret = rdiv.fold_range (r, type, lhs, op2); + frange wlhs = float_widen_lhs_range (type, lhs); + bool ret = rdiv.fold_range (r, type, wlhs, op2); if (ret == false) return false; - if (lhs.known_isnan () || op2.known_isnan () || op2.undefined_p ()) - return float_binary_op_range_finish (ret, r, type, lhs); - const REAL_VALUE_TYPE &lhs_lb = lhs.lower_bound (); - const REAL_VALUE_TYPE &lhs_ub = lhs.upper_bound (); + if (wlhs.known_isnan () || op2.known_isnan () || op2.undefined_p ()) + return float_binary_op_range_finish (ret, r, type, wlhs); + const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound (); + const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound (); const REAL_VALUE_TYPE &op2_lb = op2.lower_bound (); const REAL_VALUE_TYPE &op2_ub = op2.upper_bound (); if ((contains_zero_p (lhs_lb, lhs_ub) && contains_zero_p (op2_lb, op2_ub)) @@ -2363,7 +2394,7 @@ public: // or if lhs must be zero and op2 doesn't include zero, it would be // UNDEFINED, while rdiv.fold_range computes a zero or singleton INF // range. Those are supersets of UNDEFINED, so let's keep that way. - return float_binary_op_range_finish (ret, r, type, lhs); + return float_binary_op_range_finish (ret, r, type, wlhs); } virtual bool op2_range (frange &r, tree type, const frange &lhs, @@ -2490,13 +2521,14 @@ public: { if (lhs.undefined_p ()) return false; - bool ret = fop_mult.fold_range (r, type, lhs, op2); + frange wlhs = float_widen_lhs_range (type, lhs); + bool ret = fop_mult.fold_range (r, type, wlhs, op2); if (!ret) return ret; - if (lhs.known_isnan () || op2.known_isnan () || op2.undefined_p ()) - return float_binary_op_range_finish (ret, r, type, lhs); - const REAL_VALUE_TYPE &lhs_lb = lhs.lower_bound (); - const REAL_VALUE_TYPE &lhs_ub = lhs.upper_bound (); + if (wlhs.known_isnan () || op2.known_isnan () || op2.undefined_p ()) + return float_binary_op_range_finish (ret, r, type, wlhs); + const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound (); + const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound (); const REAL_VALUE_TYPE &op2_lb = op2.lower_bound (); const REAL_VALUE_TYPE &op2_ub = op2.upper_bound (); if ((contains_zero_p (lhs_lb, lhs_ub) @@ -2512,7 +2544,7 @@ public: zero_to_inf_range (lb, ub, signbit_known); r.set (type, lb, ub); } - return float_binary_op_range_finish (ret, r, type, lhs); + return float_binary_op_range_finish (ret, r, type, wlhs); } virtual bool op2_range (frange &r, tree type, const frange &lhs, @@ -2521,13 +2553,14 @@ public: { if (lhs.undefined_p ()) return false; - bool ret = fold_range (r, type, op1, lhs); + frange wlhs = float_widen_lhs_range (type, lhs); + bool ret = fold_range (r, type, op1, wlhs); if (!ret) return ret; - if (lhs.known_isnan () || op1.known_isnan () || op1.undefined_p ()) - return float_binary_op_range_finish (ret, r, type, lhs, true); - const REAL_VALUE_TYPE &lhs_lb = lhs.lower_bound (); - const REAL_VALUE_TYPE &lhs_ub = lhs.upper_bound (); + if (wlhs.known_isnan () || op1.known_isnan () || op1.undefined_p ()) + return float_binary_op_range_finish (ret, r, type, wlhs, true); + const REAL_VALUE_TYPE &lhs_lb = wlhs.lower_bound (); + const REAL_VALUE_TYPE &lhs_ub = wlhs.upper_bound (); const REAL_VALUE_TYPE &op1_lb = op1.lower_bound (); const REAL_VALUE_TYPE &op1_ub = op1.upper_bound (); if ((contains_zero_p (lhs_lb, lhs_ub) && contains_zero_p (op1_lb, op1_ub)) @@ -2542,7 +2575,7 @@ public: zero_to_inf_range (lb, ub, signbit_known); r.set (type, lb, ub); } - return float_binary_op_range_finish (ret, r, type, lhs, true); + return float_binary_op_range_finish (ret, r, type, wlhs, true); } private: void rv_fold (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, bool &maybe_nan, --- gcc/testsuite/gcc.c-torture/execute/ieee/pr109008.c.jj 2023-03-08 21:30:19.158618157 +0100 +++ gcc/testsuite/gcc.c-torture/execute/ieee/pr109008.c 2023-03-08 21:29:49.899039474 +0100 @@ -0,0 +1,18 @@ +/* PR tree-optimization/109008 */ + +__attribute__((noipa)) double +foo (double eps) +{ + double d = 1. + eps; + if (d == 1.) + return eps; + return 0.0; +} + +int +main () +{ + if (foo (__DBL_EPSILON__ / 8.0) == 0.0) + __builtin_abort (); + return 0; +}