From patchwork Tue Feb 11 18:43:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulrich Weigand X-Patchwork-Id: 1236431 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-519363-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=de.ibm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=klpe8Y02; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48HBW93K0Sz9sP7 for ; Wed, 12 Feb 2020 05:43:49 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:date:from:cc:mime-version:content-type :content-transfer-encoding:message-id; q=dns; s=default; b=k1QQF bPBKK6Vc4GK52MO+4NapHU1Iidf6+AYZaiAetag7whxaH4fqOvgxTVjGiGdcws0j AeYI/rSYQwze1uYKok5Cn7DuGkSgch/35P82Eii8oxZJGkLJBbcLZRAI6f7xyM/T 2ONCaYqCm2U54x3D2gp0wRyo80F61sj0xBeqrs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:to:date:from:cc:mime-version:content-type :content-transfer-encoding:message-id; s=default; bh=1C6S9oigx61 X9hKYv2TXaa+XxLI=; b=klpe8Y024s0ASRD1jxKcJBdkHJDL+OfKCyOv9/p+mGC a8kGYuJeDrTiWmllm3vDO50057PSY7ZWb+qe9v5l5dHEzPNyQJf1zopolsZTRJdv u8S00CK3CUmKL5ljtMUPUK9rwTjpW2gvTrSWyISLs00NmT2yWIsFmQf6omTJkqMk = Received: (qmail 110427 invoked by alias); 11 Feb 2020 18:43:41 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 109321 invoked by uid 89); 11 Feb 2020 18:43:41 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-14.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0b-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.158.5) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 11 Feb 2020 18:43:39 +0000 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 01BIebmk143511 for ; Tue, 11 Feb 2020 13:43:37 -0500 Received: from e06smtp04.uk.ibm.com (e06smtp04.uk.ibm.com [195.75.94.100]) by mx0b-001b2d01.pphosted.com with ESMTP id 2y1ufmnm8g-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 11 Feb 2020 13:43:37 -0500 Received: from localhost by e06smtp04.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 11 Feb 2020 18:43:35 -0000 Received: from b06avi18626390.portsmouth.uk.ibm.com (9.149.26.192) by e06smtp04.uk.ibm.com (192.168.101.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 11 Feb 2020 18:43:33 -0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 01BIgcI832833868 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 11 Feb 2020 18:42:38 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6C40511C04A; Tue, 11 Feb 2020 18:43:32 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 504DE11C04C; Tue, 11 Feb 2020 18:43:32 +0000 (GMT) Received: from oc3748833570.ibm.com (unknown [9.152.214.26]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 11 Feb 2020 18:43:32 +0000 (GMT) Received: by oc3748833570.ibm.com (Postfix, from userid 1000) id 0968DD80313; Tue, 11 Feb 2020 19:43:31 +0100 (CET) Subject: [PATCH v2] Fix -ffast-math flags handling inconsistencies To: gcc-patches@gcc.gnu.org Date: Tue, 11 Feb 2020 19:43:31 +0100 (CET) From: "Ulrich Weigand" Cc: joseph@codesourcery.com, richard.guenther@gmail.com, segher@kernel.crashing.org MIME-Version: 1.0 x-cbid: 20021118-0016-0000-0000-000002E5EBFD X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 20021118-0017-0000-0000-00003348E36F Message-Id: <20200211184332.0968DD80313@oc3748833570.ibm.com> Hello, this second version of the patch incorporates all changes discussed in the original mail thread starting here: https://gcc.gnu.org/ml/gcc-patches/2020-01/msg02097.html The patch now implements the following set of changes: 1. If a component flag of -ffast-math (or -funsafe-math-optimizations) is explicitly set (or reset) on the command line, this should override any implicit change due to -f(no-)fast-math, no matter in which order the flags come on the command line. This change affects all flags. 2. Any component flag modified from its default by -ffast-math should be reset to the default by -fno-fast-math. This was previously not done for the following flags: -fcx-limited-range -fexcess-precision= 3. Once -ffinite-math-only is true, the -f(no-)signaling-nans flag has no meaning (if we have no NaNs at all, it does not matter whether there is a difference between quiet and signaling NaNs). Therefore, it does not make sense for -ffast-math to imply -fno-signaling-nans. This is also a documentation change. 4. -ffast-math is documented to imply -fno-rounding-math, however the latter setting is the default anyway; therefore it does not make sense to try to modify it from its default setting. 5. The __FAST_MATH__ preprocessor macro should be defined if and only if all the component flags of -ffast-math are set to the value that is documented as the effect of -ffast-math. The following flags were currently *not* so tested: -fcx-limited-range -fassociative-math -freciprocal-math -frounding-math (Note that we should still *test* for -fno-rounding-math here even though it is not set as per 4. -ffast-math -frounding-math should not set the __FAST_MATH__ macro.) This is also a documentation change. Tested on s390x-ibm-linux. OK for mainline? Bye, Ulrich gcc/ChangeLog: * doc/invoke.texi (-ffast-math): Remove mention of -fno-signaling-nans. Clarify conditions when __FAST_MATH__ preprocessor macro is defined. * opts.c (common_handle_option): Pass OPTS_SET to set_fast_math_flags and set_unsafe_math_optimizations_flags. (set_fast_math_flags): Add OPTS_SET argument, and use it to avoid setting flags already explicitly set on the command line. In the !set case, also reset x_flag_cx_limited_range and x_flag_excess_precision. Never reset x_flag_signaling_nans or x_flag_rounding_math. (set_unsafe_math_optimizations_flags): Add OPTS_SET argument, and use it to avoid setting flags already explicitly set on the command line. (fast_math_flags_set_p): Also test x_flag_cx_limited_range, x_flag_associative_math, x_flag_reciprocal_math, and x_flag_rounding_math. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 2cd8d7ec5ff..662fded21ea 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -11441,10 +11441,10 @@ is unpredictable. @opindex ffast-math Sets the options @option{-fno-math-errno}, @option{-funsafe-math-optimizations}, @option{-ffinite-math-only}, @option{-fno-rounding-math}, -@option{-fno-signaling-nans}, @option{-fcx-limited-range} and -@option{-fexcess-precision=fast}. +@option{-fcx-limited-range} and @option{-fexcess-precision=fast}. -This option causes the preprocessor macro @code{__FAST_MATH__} to be defined. +Whenever all these options listed above are set to those values, +the preprocessor macro @code{__FAST_MATH__} will be defined. This option is not turned on by any @option{-O} option besides @option{-Ofast} since it can result in incorrect output for programs diff --git a/gcc/opts.c b/gcc/opts.c index 7affeb41a96..5a2ae9631ab 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -192,10 +192,12 @@ static void set_debug_level (enum debug_info_type type, int extended, const char *arg, struct gcc_options *opts, struct gcc_options *opts_set, location_t loc); -static void set_fast_math_flags (struct gcc_options *opts, int set); +static void set_fast_math_flags (struct gcc_options *opts, + struct gcc_options *opts_set, int set); static void decode_d_option (const char *arg, struct gcc_options *opts, location_t loc, diagnostic_context *dc); static void set_unsafe_math_optimizations_flags (struct gcc_options *opts, + struct gcc_options *opts_set, int set); static void enable_warning_as_error (const char *arg, int value, unsigned int lang_mask, @@ -2447,11 +2449,11 @@ common_handle_option (struct gcc_options *opts, break; case OPT_ffast_math: - set_fast_math_flags (opts, value); + set_fast_math_flags (opts, opts_set, value); break; case OPT_funsafe_math_optimizations: - set_unsafe_math_optimizations_flags (opts, value); + set_unsafe_math_optimizations_flags (opts, opts_set, value); break; case OPT_ffixed_: @@ -2839,44 +2841,44 @@ set_Wstrict_aliasing (struct gcc_options *opts, int onoff) /* The following routines are useful in setting all the flags that -ffast-math and -fno-fast-math imply. */ static void -set_fast_math_flags (struct gcc_options *opts, int set) +set_fast_math_flags (struct gcc_options *opts, + struct gcc_options *opts_set, int set) { - if (!opts->frontend_set_flag_unsafe_math_optimizations) + if (!opts->frontend_set_flag_unsafe_math_optimizations + && !opts_set->x_flag_unsafe_math_optimizations) { opts->x_flag_unsafe_math_optimizations = set; - set_unsafe_math_optimizations_flags (opts, set); + set_unsafe_math_optimizations_flags (opts, opts_set, set); } if (!opts->frontend_set_flag_finite_math_only) - opts->x_flag_finite_math_only = set; + SET_OPTION_IF_UNSET (opts, opts_set, flag_finite_math_only, set); if (!opts->frontend_set_flag_errno_math) - opts->x_flag_errno_math = !set; - if (set) - { - if (opts->frontend_set_flag_excess_precision == EXCESS_PRECISION_DEFAULT) - opts->x_flag_excess_precision - = set ? EXCESS_PRECISION_FAST : EXCESS_PRECISION_DEFAULT; - if (!opts->frontend_set_flag_signaling_nans) - opts->x_flag_signaling_nans = 0; - if (!opts->frontend_set_flag_rounding_math) - opts->x_flag_rounding_math = 0; - if (!opts->frontend_set_flag_cx_limited_range) - opts->x_flag_cx_limited_range = 1; - } + SET_OPTION_IF_UNSET (opts, opts_set, flag_errno_math, !set); + if (!opts->frontend_set_flag_cx_limited_range) + SET_OPTION_IF_UNSET (opts, opts_set, flag_cx_limited_range, set); + if (!opts->frontend_set_flag_excess_precision) + SET_OPTION_IF_UNSET (opts, opts_set, flag_excess_precision, + set ? EXCESS_PRECISION_FAST + : EXCESS_PRECISION_DEFAULT); + + // -ffast-math should also reset -frounding-math, but since this + // is off by default, there's nothing to do for now. } /* When -funsafe-math-optimizations is set the following flags are set as well. */ static void -set_unsafe_math_optimizations_flags (struct gcc_options *opts, int set) +set_unsafe_math_optimizations_flags (struct gcc_options *opts, + struct gcc_options *opts_set, int set) { if (!opts->frontend_set_flag_trapping_math) - opts->x_flag_trapping_math = !set; + SET_OPTION_IF_UNSET (opts, opts_set, flag_trapping_math, !set); if (!opts->frontend_set_flag_signed_zeros) - opts->x_flag_signed_zeros = !set; + SET_OPTION_IF_UNSET (opts, opts_set, flag_signed_zeros, !set); if (!opts->frontend_set_flag_associative_math) - opts->x_flag_associative_math = set; + SET_OPTION_IF_UNSET (opts, opts_set, flag_associative_math, set); if (!opts->frontend_set_flag_reciprocal_math) - opts->x_flag_reciprocal_math = set; + SET_OPTION_IF_UNSET (opts, opts_set, flag_reciprocal_math, set); } /* Return true iff flags in OPTS are set as if -ffast-math. */ @@ -2884,10 +2886,14 @@ bool fast_math_flags_set_p (const struct gcc_options *opts) { return (!opts->x_flag_trapping_math + && !opts->x_flag_signed_zeros + && opts->x_flag_associative_math + && opts->x_flag_reciprocal_math && opts->x_flag_unsafe_math_optimizations && opts->x_flag_finite_math_only - && !opts->x_flag_signed_zeros && !opts->x_flag_errno_math + && !opts->x_flag_rounding_math + && opts->x_flag_cx_limited_range && opts->x_flag_excess_precision == EXCESS_PRECISION_FAST); }