From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by sourceware.org (Postfix) with ESMTPS id 1B7B83858D1E for ; Mon, 19 Jun 2023 18:23:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B7B83858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-51a21185130so3924645a12.0 for ; Mon, 19 Jun 2023 11:23:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687199018; x=1689791018; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=/UMb+TWTM3WUxWKNdkdExaZN7aLd/18K0nDfp+h1YlY=; b=rAgQE2GQag/ZNKCFGvEAo8pa3ZdQJG1yFDY2uvW1q2HvNPuPnNP4/TG26Q1bTZBcC3 bWnSLhrs58dLa4oHk2DbxIMGbftRiM4bOpNebONBCSaTG2lv0EiGN0NS+sWbKHjKUZDp JhwZHpQdnp4omVfVHJ+IU9FAqJ08jmD3cTw7d6tvwJYnigOvWvohdBKDS9zI18dH3sxQ pTDAU9DETg9BXmfsKYeiCXqVerufnp4Wfpa6qzLu4CXPP0Vw9p76KQ8NkHjx/4fAm93c C7f8tsDOlOQn+3nzQpaG0mIjJZr4U/NtCkt0IbsyGZ53Yh7dFy+VACBYYfr2i1e+0YfT dbAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687199018; x=1689791018; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/UMb+TWTM3WUxWKNdkdExaZN7aLd/18K0nDfp+h1YlY=; b=P14/uiDlYs0Ej/69qXBD1YG/STedCLyoQ5JTagXaA0zZ4ETzqL78rnj1LzKV3CkJj0 jxrdiIe/lIoHNrR781McULq88kXZhvYOor50fVnyL8E9deziLxaO4RyrdSUjfIdnj0uV +xD0P+SuD+EOIw9btkJc0zpWgBO9vYmfi+LNZy/HEDaPQpjhiOuTI1twj9RP6SLJm767 G+DC/vLzmBQ+1+cpbGHAlRnJv7PTBRHH6ck/M6/JeKJCx27+T8GbdwvnZ3jKADj5IV2U 14Qw7CYzprz74uKflVuSY/2YaBK0Lar0be97HniTPkgj9wLbayYAb3Reg5J0gY/23VA8 gCdw== X-Gm-Message-State: AC+VfDwsacstb3fGA782DV5PaRtdMFPiKR62gS+OER+xIoWMFmwfpWeq zDBg42i8+QxciWfDxU5lZw4= X-Google-Smtp-Source: ACHHUZ6xSqCHhWS7xawz9BfZdRiETdSqAF0Xvnic2nCs+1j1Zj2RMFEIpim8M+RWFdfwwg8RMVmlQw== X-Received: by 2002:a17:907:6d03:b0:988:6193:29dc with SMTP id sa3-20020a1709076d0300b00988619329dcmr4159782ejc.7.1687199017424; Mon, 19 Jun 2023 11:23:37 -0700 (PDT) Received: from smtpclient.apple (dynamic-077-002-104-054.77.2.pool.telefonica.de. [77.2.104.54]) by smtp.gmail.com with ESMTPSA id li14-20020a170906f98e00b00988a0765e29sm37408ejb.104.2023.06.19.11.23.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 19 Jun 2023 11:23:36 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Richard Biener Mime-Version: 1.0 (1.0) Subject: Re: [PATCH] c-family: implement -ffp-contract=on Date: Mon, 19 Jun 2023 20:23:26 +0200 Message-Id: References: <2207f489-f600-2a05-0741-5733b4a6f49a@ispras.ru> Cc: Marek Polacek , Jason Merrill , gcc-patches@gcc.gnu.org In-Reply-To: <2207f489-f600-2a05-0741-5733b4a6f49a@ispras.ru> To: Alexander Monakov X-Mailer: iPhone Mail (20F66) X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPAM_BODY,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > Am 19.06.2023 um 19:03 schrieb Alexander Monakov : >=20 > =EF=BB=BF > Ping. OK for trunk? Ok if the FE maintainers do not object within 48h. Thanks, Richard=20 >> On Mon, 5 Jun 2023, Alexander Monakov wrote: >>=20 >> Ping for the front-end maintainers' input. >>=20 >>> On Mon, 22 May 2023, Richard Biener wrote: >>>=20 >>> On Thu, May 18, 2023 at 11:04=E2=80=AFPM Alexander Monakov via Gcc-patch= es >>> wrote: >>>>=20 >>>> Implement -ffp-contract=3Don for C and C++ without changing default >>>> behavior (=3Doff for -std=3DcNN, =3Dfast for C++ and -std=3DgnuNN). >>>=20 >>> The documentation changes mention the defaults are changed for >>> standard modes, I suppose you want to remove that hunk. >>>=20 >>>> gcc/c-family/ChangeLog: >>>>=20 >>>> * c-gimplify.cc (fma_supported_p): New helper. >>>> (c_gimplify_expr) [PLUS_EXPR, MINUS_EXPR]: Implement FMA >>>> contraction. >>>>=20 >>>> gcc/ChangeLog: >>>>=20 >>>> * common.opt (fp_contract_mode) [on]: Remove fallback. >>>> * config/sh/sh.md (*fmasf4): Correct flag_fp_contract_mode test.= >>>> * doc/invoke.texi (-ffp-contract): Update. >>>> * trans-mem.cc (diagnose_tm_1): Skip internal function calls. >>>> --- >>>> gcc/c-family/c-gimplify.cc | 78 ++++++++++++++++++++++++++++++++++++++ >>>> gcc/common.opt | 3 +- >>>> gcc/config/sh/sh.md | 2 +- >>>> gcc/doc/invoke.texi | 8 ++-- >>>> gcc/trans-mem.cc | 3 ++ >>>> 5 files changed, 88 insertions(+), 6 deletions(-) >>>>=20 >>>> diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc >>>> index ef5c7d919f..f7635d3b0c 100644 >>>> --- a/gcc/c-family/c-gimplify.cc >>>> +++ b/gcc/c-family/c-gimplify.cc >>>> @@ -41,6 +41,8 @@ along with GCC; see the file COPYING3. If not see >>>> #include "c-ubsan.h" >>>> #include "tree-nested.h" >>>> #include "context.h" >>>> +#include "tree-pass.h" >>>> +#include "internal-fn.h" >>>>=20 >>>> /* The gimplification pass converts the language-dependent trees >>>> (ld-trees) emitted by the parser into language-independent trees >>>> @@ -686,6 +688,14 @@ c_build_bind_expr (location_t loc, tree block, tre= e body) >>>> return bind; >>>> } >>>>=20 >>>> +/* Helper for c_gimplify_expr: test if target supports fma-like FN. *= / >>>> + >>>> +static bool >>>> +fma_supported_p (enum internal_fn fn, tree type) >>>> +{ >>>> + return direct_internal_fn_supported_p (fn, type, OPTIMIZE_FOR_BOTH);= >>>> +} >>>> + >>>> /* Gimplification of expression trees. */ >>>>=20 >>>> /* Do C-specific gimplification on *EXPR_P. PRE_P and POST_P are as in= >>>> @@ -739,6 +749,74 @@ c_gimplify_expr (tree *expr_p, gimple_seq *pre_p A= TTRIBUTE_UNUSED, >>>> break; >>>> } >>>>=20 >>>> + case PLUS_EXPR: >>>> + case MINUS_EXPR: >>>> + { >>>> + tree type =3D TREE_TYPE (*expr_p); >>>> + /* For -ffp-contract=3Don we need to attempt FMA contraction on= ly >>>> + during initial gimplification. Late contraction across stat= ement >>>> + boundaries would violate language semantics. */ >>>> + if (SCALAR_FLOAT_TYPE_P (type) >>>> + && flag_fp_contract_mode =3D=3D FP_CONTRACT_ON >>>> + && cfun && !(cfun->curr_properties & PROP_gimple_any) >>>> + && fma_supported_p (IFN_FMA, type)) >>>> + { >>>> + bool neg_mul =3D false, neg_add =3D code =3D=3D MINUS_EXPR;= >>>> + >>>> + tree *op0_p =3D &TREE_OPERAND (*expr_p, 0); >>>> + tree *op1_p =3D &TREE_OPERAND (*expr_p, 1); >>>> + >>>> + /* Look for =C2=B1(x * y) =C2=B1 z, swapping operands if ne= cessary. */ >>>> + if (TREE_CODE (*op0_p) =3D=3D NEGATE_EXPR >>>> + && TREE_CODE (TREE_OPERAND (*op0_p, 0)) =3D=3D MULT_EXP= R) >>>> + /* '*EXPR_P' is '-(x * y) =C2=B1 z'. This is fine. */; >>>> + else if (TREE_CODE (*op0_p) !=3D MULT_EXPR) >>>> + { >>>> + std::swap (op0_p, op1_p); >>>> + std::swap (neg_mul, neg_add); >>>> + } >>>> + if (TREE_CODE (*op0_p) =3D=3D NEGATE_EXPR) >>>> + { >>>> + op0_p =3D &TREE_OPERAND (*op0_p, 0); >>>> + neg_mul =3D !neg_mul; >>>> + } >>>> + if (TREE_CODE (*op0_p) !=3D MULT_EXPR) >>>> + break; >>>> + auto_vec ops (3); >>>> + ops.quick_push (TREE_OPERAND (*op0_p, 0)); >>>> + ops.quick_push (TREE_OPERAND (*op0_p, 1)); >>>> + ops.quick_push (*op1_p); >>>> + >>>> + enum internal_fn ifn =3D IFN_FMA; >>>> + if (neg_mul) >>>> + { >>>> + if (fma_supported_p (IFN_FNMA, type)) >>>> + ifn =3D IFN_FNMA; >>>> + else >>>> + ops[0] =3D build1 (NEGATE_EXPR, type, ops[0]); >>>> + } >>>> + if (neg_add) >>>> + { >>>> + enum internal_fn ifn2 =3D ifn =3D=3D IFN_FMA ? IFN_FMS := IFN_FNMS; >>>> + if (fma_supported_p (ifn2, type)) >>>> + ifn =3D ifn2; >>>> + else >>>> + ops[2] =3D build1 (NEGATE_EXPR, type, ops[2]); >>>> + } >>>> + for (auto &&op : ops) >>>> + if (gimplify_expr (&op, pre_p, post_p, is_gimple_val, fb_= rvalue) >>>> + =3D=3D GS_ERROR) >>>> + return GS_ERROR; >>>> + >>>> + gcall *call =3D gimple_build_call_internal_vec (ifn, ops); >>>> + gimple_seq_add_stmt_without_update (pre_p, call); >>>> + *expr_p =3D create_tmp_var (type); >>>> + gimple_call_set_lhs (call, *expr_p); >>>=20 >>> it would be possible to do >>>=20 >>> *expr_p =3D build_call_expr_internal (ifn, type, ops[0], ops[1]. ops[2]= ); >>> return GS_OK; >>>=20 >>> and not worry about temporary creation and gimplifying of the operands. >>> That would in theory also leave the possibility to do this during >>> genericization instead (and avoid the guard against late invocation of >>> the hook). >>>=20 >>> Otherwise it looks OK, but I'll let frontend maintainers have a chance t= o look >>> as well. >>>=20 >>> Thanks for tackling this long-standing issue. >>> Richard. >>>=20 >>>> + return GS_ALL_DONE; >>>> + } >>>> + break; >>>> + } >>>> + >>>> default:; >>>> } >>>>=20 >>>> diff --git a/gcc/common.opt b/gcc/common.opt >>>> index a28ca13385..3daec85aef 100644 >>>> --- a/gcc/common.opt >>>> +++ b/gcc/common.opt >>>> @@ -1662,9 +1662,8 @@ Name(fp_contract_mode) Type(enum fp_contract_mode= ) UnknownError(unknown floating >>>> EnumValue >>>> Enum(fp_contract_mode) String(off) Value(FP_CONTRACT_OFF) >>>>=20 >>>> -; Not implemented, fall back to conservative FP_CONTRACT_OFF. >>>> EnumValue >>>> -Enum(fp_contract_mode) String(on) Value(FP_CONTRACT_OFF) >>>> +Enum(fp_contract_mode) String(on) Value(FP_CONTRACT_ON) >>>>=20 >>>> EnumValue >>>> Enum(fp_contract_mode) String(fast) Value(FP_CONTRACT_FAST) >>>> diff --git a/gcc/config/sh/sh.md b/gcc/config/sh/sh.md >>>> index 4622dba012..5cb1795482 100644 >>>> --- a/gcc/config/sh/sh.md >>>> +++ b/gcc/config/sh/sh.md >>>> @@ -9269,7 +9269,7 @@ (define_insn_and_split "*fmasf4" >>>> (match_operand:SF 3 "arith_reg_operand" "0"))) >>>> (clobber (reg:SI FPSCR_STAT_REG)) >>>> (use (reg:SI FPSCR_MODES_REG))] >>>> - "TARGET_SH2E && flag_fp_contract_mode !=3D FP_CONTRACT_OFF" >>>> + "TARGET_SH2E && flag_fp_contract_mode =3D=3D FP_CONTRACT_FAST" >>>> "fmac %1,%2,%0" >>>> "&& can_create_pseudo_p ()" >>>> [(parallel [(set (match_dup 0) >>>> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi >>>> index b92b857602..cb1e9a1d9f 100644 >>>> --- a/gcc/doc/invoke.texi >>>> +++ b/gcc/doc/invoke.texi >>>> @@ -11983,10 +11983,12 @@ This option is enabled by default at optimiza= tion levels @option{-O1}, >>>> such as forming of fused multiply-add operations if the target has >>>> native support for them. >>>> @option{-ffp-contract=3Don} enables floating-point expression contracti= on >>>> -if allowed by the language standard. This is currently not implemente= d >>>> -and treated equal to @option{-ffp-contract=3Doff}. >>>> +if allowed by the language standard. This is implemented for C and C+= +, >>>> +where it enables contraction within one expression, but not across >>>> +different statements. >>>>=20 >>>> -The default is @option{-ffp-contract=3Dfast}. >>>> +The default is @option{-ffp-contract=3Doff} for C in a standards compl= iant mode >>>> +(@option{-std=3Dc11} or similar), @option{-ffp-contract=3Dfast} otherw= ise. >>>>=20 >>>> @opindex fomit-frame-pointer >>>> @item -fomit-frame-pointer >>>> diff --git a/gcc/trans-mem.cc b/gcc/trans-mem.cc >>>> index 4b129663e0..2174faef4c 100644 >>>> --- a/gcc/trans-mem.cc >>>> +++ b/gcc/trans-mem.cc >>>> @@ -637,6 +637,9 @@ diagnose_tm_1 (gimple_stmt_iterator *gsi, bool *han= dled_ops_p, >>>> { >>>> case GIMPLE_CALL: >>>> { >>>> + if (gimple_call_internal_p (stmt)) >>>> + break; >>>> + >>>> tree fn =3D gimple_call_fn (stmt); >>>>=20 >>>> if ((d->summary_flags & DIAG_TM_OUTER) =3D=3D 0 >>>> -- >>>> 2.39.2 >>>>=20