From patchwork Tue Sep 21 11:41:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 45212 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 CDFB43858022 for ; Tue, 21 Sep 2021 11:41:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 4B9253858C3A for ; Tue, 21 Sep 2021 11:41:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4B9253858C3A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=+xhP7KLrklxKDuSN9zJnSLdhr/oSUv8W1IHQ+MrdZg4=; b=jI6xWHXsHb/ybrnU2KGHEy7wGo rPs4jQp47asnoL9bTBt6UeQJ4gK89QFNArHMEfxJQi8zV2wShscOTOcTpJjkQ1lUEVVjQ+tmSnA4r RzWH+1iofe+MmlK77t9uWdvP4YCYbhrL3/2ZC72tsO4Q6ReBvZR5Mo4A14/YbwXfARMPdGMKW9WYU jr3qLTGE/5J7sgTJUhLECWQj7eNXJuBNoTGSxKmM/D1vbQqS9JdanONiOB1u07QAqRm3XB9JRZIXf pco9zFsTa6tDOUzwadhMH6lDZ3RBPX7hmFnrULbZS2mt0pdCWMelzd+SbY3pg6iGwh9kGmZBG/RhP ejZVjJzA==; Received: from [185.62.158.67] (port=57627 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mSe95-0007Jx-Ix for gcc-patches@gcc.gnu.org; Tue, 21 Sep 2021 07:41:07 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [RFC/PATCH] C++ constexpr vs. floating point exceptions. Date: Tue, 21 Sep 2021 12:41:06 +0100 Message-ID: <005b01d7aedd$91545eb0$b3fd1c10$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adeu3KuEslH2PuzMTAmOzF2gAiyhFg== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" I was wondering if I may ask the C++ language experts for their opinion on whether (potential) floating point exceptions/traps can be ignored in constant expressions; this is related to PR c++/96862. I think my question boils down to whether (or not) the following is valid C++: constexpr float my_inf = 7.0 / 0.0; [This is currently an error with "-O2", but OK with "-O2 -ffast-math"!] There's a long history of g++'s semantics being accidentally tied to the middle-end's constant folding, such that the current status quo is that some middle-end bugs can't be fixed without breaking C++, and vice versa. I'm hoping that the patch below (following Jakub's lead with rounding math) might be a next step to improving things, provided that my understanding of the desired/correct behaviour of the C++ front-end is correct. This patch has been tested on x86_64-pc-linux-gnu with a "make bootstrap" and "make -k check" with no new failures after tweaking two checks in g++.dg/ubsan/pr63956.C. With this change the middle-end can become more strict about respecting flag_trapping_math without affecting C++'s behavior. Ideally, what the front-end considers valid should be independent of whether the user specified -fno-trapping-math (or -ffast-math) to the middle-end. Thoughts? Ok for mainline? 2021-09-21 Roger Sayle gcc/cp/ChangeLog * constexpr.c (cxx_eval_outermost_const_expr): Temporarily disable the middle-end from honoring floating point exceptions/traps while folding "manifestly constant" expressions. gcc/testsuite/ChangeLog * g++.dg/ubsan/pr63956.C: Update to (always) allow floating point division in constexpr (if both operands are constexpr). Roger diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 8a5dd06..ddea132 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -7276,6 +7276,13 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, /* Turn off -frounding-math for manifestly constant evaluation. */ warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval); + /* For manifestly constant evaluation, trapping (floating point) + exceptions don't prevent evaluation at compile-time, so temporarily + turn off -fsignaling-nans, -ftrapping-math and -ftrapv. */ + warning_sentinel sn (flag_signaling_nans, ctx.manifestly_const_eval); + warning_sentinel tm (flag_trapping_math, ctx.manifestly_const_eval); + warning_sentinel tv (flag_trapv, ctx.manifestly_const_eval); + tree type = initialized_type (t); tree r = t; bool is_consteval = false; diff --git a/gcc/testsuite/g++.dg/ubsan/pr63956.C b/gcc/testsuite/g++.dg/ubsan/pr63956.C index 3a1596e..126ed1d 100644 --- a/gcc/testsuite/g++.dg/ubsan/pr63956.C +++ b/gcc/testsuite/g++.dg/ubsan/pr63956.C @@ -69,12 +69,12 @@ constexpr float fn4 (float a, float b) { if (b != 2.0) - a = a / b; // { dg-error "is not a constant expression" } + a = a / b; return a; } constexpr float l1 = fn4 (5.0, 3.0); -constexpr float l2 = fn4 (7.0, 0.0); // { dg-message "in .constexpr. expansion" } +constexpr float l2 = fn4 (7.0, 0.0); constexpr int fn5 (const int *a, int b)