From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb2b.google.com (mail-yb1-xb2b.google.com [IPv6:2607:f8b0:4864:20::b2b]) by sourceware.org (Postfix) with ESMTPS id 665773858C5E for ; Fri, 10 Mar 2023 02:25:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 665773858C5E 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-yb1-xb2b.google.com with SMTP id g27so191725ybe.3 for ; Thu, 09 Mar 2023 18:25:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678415121; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=lxvU0f+Euos5fRTx6G7w8bD/z++I0ZmzNlpONZZT3HM=; b=btPbzwFPnACcMiWNMnuRuMGuPQWIPjttmRGjDPLYDUafRFxbiZHeH5lB2PqRK9eZ8Q 5oJuGh6gmW71JMt7xKQvOvXh1qT5/ASuqL0YAeZ+Y5OKM/L7QoSINOadDO0vWMZvgRCy ehaP5DlTGH+/81GAFGN7MJ3vVKmtuejaXp04e8PW6F4TMnjtVvFZKWjuAqNd6DXXkuJY dtHO9X88CoI8o7JIeerCil78a0xDiHbLeh56ynsnya2OH9fhsmWm0TQNP0E+pAaHgJTN yXICroda++JbLiEgRTNrw1c0eKK9S734u5Qu1sDl1IEZI60rrrXpEBjqT8km6O6PGI0n BGag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678415121; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lxvU0f+Euos5fRTx6G7w8bD/z++I0ZmzNlpONZZT3HM=; b=fPecHTD7qY6hfbprk/hUoF6hWo8NxtnT8PMMD7TrBVtc0KJfrzNTArMCfnuCGErYTO bEYaMjP3bifYzBjxJMuFonvx+x+EOQOhIk8KRgx8BpBwB2rX99YWv3Ih+ZGXsKDSCWrd b/BjJgmT35rL0PAdliqSbTx3zmfuKTtCRhjzztDOOma/O48tFx1DzpV4CRyTOoGvnsgN Bvbs0Q7RRhc5iueSSZItu3GJlnNkxEbX5GiR6BfAOkLWgeJqJY2bQla6zt/ly7sHJ3Eh FYW+6uo1sYAI/hPsRzI7YT8OLSYC/TtG08chu6zrM0/gJs+r6+98dfi4P39mCaOVdV4J i/AQ== X-Gm-Message-State: AO0yUKWHmpZ5v+5bpqghuVpgjU3qPS/m8zr4rHWESPS4IEuf1/LrS5eB 1RNUm7pEVqnijBiThntIoF7mVq4T0+jjoIuzLJQ= X-Google-Smtp-Source: AK7set/8rRr/BC6ZcavkTm6z6r5T8XfFlRnvXunJuPUBLbBhvboFo28aKRl8HmdRqaM89UITks3H76ap+gpFC38fsO0= X-Received: by 2002:a25:f211:0:b0:a5f:0:bf12 with SMTP id i17-20020a25f211000000b00a5f0000bf12mr11802221ybe.13.1678415121718; Thu, 09 Mar 2023 18:25:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Hongtao Liu Date: Fri, 10 Mar 2023 10:30:19 +0800 Message-ID: Subject: Re: [PATCH]middle-end: don't form FMAs when multiplication is not single use. [PR108583] To: Tamar Christina Cc: gcc-patches@gcc.gnu.org, nd@arm.com, rguenther@suse.de, jlaw@ventanamicro.com, richard.sandiford@arm.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.3 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,SPF_HELO_NONE,SPF_PASS,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 List-Id: On Fri, Mar 10, 2023 at 3:37=E2=80=AFAM Tamar Christina via Gcc-patches wrote: > > Hi All, > > The testcase > > typedef unsigned int vec __attribute__((vector_size(32))); > vec > f3 (vec a, vec b, vec c) > { > vec d =3D a * b; > return d + ((c + d) >> 1); > } > > shows a case where we don't want to form an FMA due to the MUL not being = single > use. In this case to form an FMA we have to redo the MUL as well as we n= o > longer have it to share. > > As such making an FMA here would be a de-optimization. > > Bootstrapped Regtested on aarch64-none-linux-gnu and no issues. > > Ok for master? > > Thanks, > Tamar > > gcc/ChangeLog: > > PR target/108583 > * tree-ssa-math-opts.cc (convert_mult_to_fma): Inhibit FMA in cas= e not > single use. > > gcc/testsuite/ChangeLog: > > PR target/108583 > * gcc.dg/mla_1.c: New test. > > Co-Authored-By: Richard Sandiford > > --- inline copy of patch -- > diff --git a/gcc/testsuite/gcc.dg/mla_1.c b/gcc/testsuite/gcc.dg/mla_1.c > new file mode 100644 > index 0000000000000000000000000000000000000000..a92ecf248116d89b1bc4207a9= 07ea5ed95728a28 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/mla_1.c > @@ -0,0 +1,40 @@ > +/* { dg-do compile } */ > +/* { dg-require-effective-target vect_int } */ > +/* { dg-options "-O2 -msve-vector-bits=3D256 -march=3Darmv8.2-a+sve -fdu= mp-tree-optimized" } */ > + > +unsigned int > +f1 (unsigned int a, unsigned int b, unsigned int c) { > + unsigned int d =3D a * b; > + return d + ((c + d) >> 1); > +} > + > +unsigned int > +g1 (unsigned int a, unsigned int b, unsigned int c) { > + return a * b + c; > +} > + > +__Uint32x4_t > +f2 (__Uint32x4_t a, __Uint32x4_t b, __Uint32x4_t c) { > + __Uint32x4_t d =3D a * b; > + return d + ((c + d) >> 1); > +} > + > +__Uint32x4_t > +g2 (__Uint32x4_t a, __Uint32x4_t b, __Uint32x4_t c) { > + return a * b + c; > +} > + > +typedef unsigned int vec __attribute__((vector_size(32))); vec > +f3 (vec a, vec b, vec c) > +{ > + vec d =3D a * b; > + return d + ((c + d) >> 1); > +} > + > +vec > +g3 (vec a, vec b, vec c) > +{ > + return a * b + c; > +} > + > +/* { dg-final { scan-tree-dump-times {\.FMA } 1 "optimized" { target aar= ch64*-*-* } } } */ > diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc > index 5ab5b944a573ad24ce8427aff24fc5215bf05dac..26ed91d58fa4709a67c903ad4= 46d267a3113c172 100644 > --- a/gcc/tree-ssa-math-opts.cc > +++ b/gcc/tree-ssa-math-opts.cc > @@ -3346,6 +3346,20 @@ convert_mult_to_fma (gimple *mul_stmt, tree op1, t= ree op2, > param_avoid_fma_max_bits)); > bool defer =3D check_defer; > bool seen_negate_p =3D false; > + > + /* There is no numerical difference between fused and unfused integer = FMAs, > + and the assumption below that FMA is as cheap as addition is unlike= ly > + to be true, especially if the multiplication occurs multiple times = on > + the same chain. E.g., for something like: > + > + (((a * b) + c) >> 1) + (a * b) > + > + we do not want to duplicate the a * b into two additions, not least > + because the result is not a natural FMA chain. */ > + if (ANY_INTEGRAL_TYPE_P (type) > + && !has_single_use (mul_result)) What about floating point? > + return false; > + > /* Make sure that the multiplication statement becomes dead after > the transformation, thus that all uses are transformed to FMAs. > This means we assume that an FMA operation has the same cost > > > > > -- --=20 BR, Hongtao