From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id E9D8D385558F for ; Mon, 21 Nov 2022 06:12:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E9D8D385558F 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-ej1-x629.google.com with SMTP id n20so26273679ejh.0 for ; Sun, 20 Nov 2022 22:12:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=UpaIab+MuCvBCQ3q+yIbMjd8nvHW4ch7+UPO6ysJHNg=; b=caAAbxoZw/N6cywRvNgsjZT4w/926BzWkbzGbCX3ASna7SwWZdkn8yGHhv5+c71f5X G1WnW49npKIOj5vI/fdRN/sYHy60Y6gNoyMDbBKMXwOxZX3NJAzl4MikXuGzmnDY9AHw bJAsx9I0c/4ajV/RD59eNyikVUIZ0aT3drBuuYMRB2QQDtta5VoG85IkYSZoD7BtpseT e25DfH8gylaKNQi+VP4g1QqwErf0lY8ozLtRPZQP3JGQiJTvYAL6Y3LxWOJ6IWLHSaXE fTT6nqs6ZGJ38cj6aT60gHKoar4NhZvLwb+Xsriv21rlEfXCah64vHciNh5xJQtHS85l nBOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=UpaIab+MuCvBCQ3q+yIbMjd8nvHW4ch7+UPO6ysJHNg=; b=pnAJ+x3q4BOFdQ2lTOtpZGYaZMlW3EFMo0Vr/STakU7iEJt+kH+1kUAslxyyemIs3z btIiLMLOOhsr9W25+tfHbbwd26wON3FW6IlN4XKvcPvATsnNW72Hz39DhznouqBWGfES +kUsfzm+6TGBBJh/sFHyCn4JFd28dX5nGydSX6V55FYQPw8w7ecYPIIdMNiFfptKvaqb yT0vUiQQxr5RNGZk1rgzQGtVWJ3/86LeHuVjYaOKNDEzKHwhb34bfrsDRXAfAiqJjVWF h1piqM2GWa0DqgDywMzWykfp0mtHxWviQzZBIv6RHhO/dmrFTLduMhC5ZLr+QBupZhxy zxlw== X-Gm-Message-State: ANoB5pkZLzyI0Vl9bKYYOmw7OvR4lkzMzZbMWpgIEtCmeu/t+6aPKt7Q LkfWELBRomz59IosmeZZOgBjcbXZpbDhE0P4tew= X-Google-Smtp-Source: AA0mqf4AYumP9oasyKm2trijYZi/WOn+TLMVzxMlw2Ozg8sIt6ln9LYeeOpmNeUJvWzlPiWb69mscC0hepuwQgissCA= X-Received: by 2002:a17:906:e083:b0:78d:c155:56b2 with SMTP id gh3-20020a170906e08300b0078dc15556b2mr5558330ejb.291.1669011178239; Sun, 20 Nov 2022 22:12:58 -0800 (PST) MIME-Version: 1.0 References: <20221119062532.75190-1-hongyu.wang@intel.com> In-Reply-To: From: Hongyu Wang Date: Mon, 21 Nov 2022 14:12:47 +0800 Message-ID: Subject: Re: [PATCH] i386: Only enable small loop unrolling in backend [PR 107602] To: Hongtao Liu Cc: "Liu, Hongtao" , "Wang, Hongyu" , "gcc-patches@gcc.gnu.org" , "richard.guenther@gmail.com" , "ubizjak@gmail.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,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,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: > It's not necessarily right. unroll_factor will be set as 1 when > -fno-unroll-loops, which is exactly -fno-unroll-loops means. Not that exactly, -fno-unroll-loops previously will prevent the pass from running, and on the current trunk the pass still runs. Actually I think the implementation on trunk is a bit tricky since we use a target hook without return value to execute the pass. Also the logic !OPTION_SET_P looks quite like a workaround. IMHO this is also not good for maintenance. Hongtao Liu via Gcc-patches =E4=BA=8E2022=E5=B9= =B411=E6=9C=8821=E6=97=A5=E5=91=A8=E4=B8=80 09:33=E5=86=99=E9=81=93=EF=BC= =9A > > On Mon, Nov 21, 2022 at 9:01 AM Liu, Hongtao via Gcc-patches > wrote: > > > > > > > > > -----Original Message----- > > > From: Wang, Hongyu > > > Sent: Saturday, November 19, 2022 2:26 PM > > > To: gcc-patches@gcc.gnu.org > > > Cc: richard.guenther@gmail.com; ubizjak@gmail.com; Liu, Hongtao > > > > > > Subject: [PATCH] i386: Only enable small loop unrolling in backend [P= R 107602] > > > > > > Hi, > > > > > > Followed by the discussion in pr107602, -munroll-only-small-loops Doe= s not > > PR107692? > > > turns on/off -funroll-loops, and current check in pass_rtl_unroll_loo= ps::gate > > > would cause -funroll-loops do not take effect. Revert the change abou= t > It's not necessarily right. unroll_factor will be set as 1 when > -fno-unroll-loops, which is exactly -fno-unroll-loops means. > > > targetm.loop_unroll_adjust and apply the backend option change to str= ictly > > > follow the rule that -funroll-loops takes full control of loop unroll= ing, and > > > munroll-only-small-loops just change its behavior to unroll small siz= e loops. > > > > > > Bootstrapped and regtested on x86-64-pc-linux-gnu. > > > > > > Ok for trunk? > > > > > > gcc/ChangeLog: > > > > > > PR target/107602 > > > * common/config/i386/i386-common.cc (ix86_optimization_table): > > > Enable loop unroll O2, disable -fweb and -frename-registers > > > by default. > > > * config/i386/i386-options.cc > > > (ix86_override_options_after_change): > > > Disable small loop unroll when funroll-loops enabled, reset > > > cunroll_grow_size when it is not explicitly enabled. > > > (ix86_option_override_internal): Call > > > ix86_override_options_after_change instead of calling > > > ix86_recompute_optlev_based_flags and ix86_default_align > > > separately. > > > * config/i386/i386.cc (ix86_loop_unroll_adjust): Adjust unroll > > > factor if -munroll-only-small-loops enabled. > > > * loop-init.cc (pass_rtl_unroll_loops::gate): Do not enable > > > loop unrolling for -O2-speed. > > > (pass_rtl_unroll_loops::execute): Rmove > > > targetm.loop_unroll_adjust check. > > > > > > gcc/testsuite/ChangeLog: > > > > > > PR target/107602 > > > * gcc.target/i386/pr86270.c: Add -fno-unroll-loops. > > > * gcc.target/i386/pr93002.c: Likewise. > > > --- > > > gcc/common/config/i386/i386-common.cc | 8 ++++++ > > > gcc/config/i386/i386-options.cc | 34 ++++++++++++++++++++++-= -- > > > gcc/config/i386/i386.cc | 18 ++++--------- > > > gcc/loop-init.cc | 11 +++----- > > > gcc/testsuite/gcc.target/i386/pr86270.c | 2 +- > > > gcc/testsuite/gcc.target/i386/pr93002.c | 2 +- > > > 6 files changed, 49 insertions(+), 26 deletions(-) > > > > > > diff --git a/gcc/common/config/i386/i386-common.cc > > > b/gcc/common/config/i386/i386-common.cc > > > index 6ce2a588adc..660a977b68b 100644 > > > --- a/gcc/common/config/i386/i386-common.cc > > > +++ b/gcc/common/config/i386/i386-common.cc > > > @@ -1808,7 +1808,15 @@ static const struct default_options > > > ix86_option_optimization_table[] =3D > > > /* The STC algorithm produces the smallest code at -Os, for x86.= */ > > > { OPT_LEVELS_2_PLUS, OPT_freorder_blocks_algorithm_, NULL, > > > REORDER_BLOCKS_ALGORITHM_STC }, > > > + > > > + /* Turn on -funroll-loops with -munroll-only-small-loops to enab= le small > > > + loop unrolling at -O2. */ > > > + { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_funroll_loops, NULL, 1 }, > > > { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_munroll_only_small_loops, NU= LL, > > > 1 }, > > > + /* Turns off -frename-registers and -fweb which are enabled by > > > + funroll-loops. */ > > > + { OPT_LEVELS_ALL, OPT_frename_registers, NULL, 0 }, > > > + { OPT_LEVELS_ALL, OPT_fweb, NULL, 0 }, > > > /* Turn off -fschedule-insns by default. It tends to make the > > > problem with not enough registers even worse. */ > > > { OPT_LEVELS_ALL, OPT_fschedule_insns, NULL, 0 }, diff --git > > > a/gcc/config/i386/i386-options.cc b/gcc/config/i386/i386-options.cc i= ndex > > > e5c77f3a84d..bc1d36e36a8 100644 > > > --- a/gcc/config/i386/i386-options.cc > > > +++ b/gcc/config/i386/i386-options.cc > > > @@ -1838,8 +1838,37 @@ ix86_recompute_optlev_based_flags (struct > > > gcc_options *opts, void ix86_override_options_after_change (void) = { > > > + /* Default align_* from the processor table. */ > > > ix86_default_align (&global_options); > > > + > > > ix86_recompute_optlev_based_flags (&global_options, &global_option= s_set); > > > + > > > + /* Disable unrolling small loops when there's explicit > > > + -f{,no}unroll-loop. */ > > > + if ((OPTION_SET_P (flag_unroll_loops)) > > > + || (OPTION_SET_P (flag_unroll_all_loops) > > > + && flag_unroll_all_loops)) > > > + { > > > + if (!OPTION_SET_P (ix86_unroll_only_small_loops)) > > > + ix86_unroll_only_small_loops =3D 0; > > > + /* Re-enable -frename-registers and -fweb if funroll-loops > > > + enabled. */ > > > + if (!OPTION_SET_P (flag_web)) > > > + flag_web =3D flag_unroll_loops; > > > + if (!OPTION_SET_P (flag_rename_registers)) > > > + flag_rename_registers =3D flag_unroll_loops; > > > + /* -fcunroll-grow-size default follws -f[no]-unroll-loops. */ > > > + if (!OPTION_SET_P (flag_cunroll_grow_size)) > > > + flag_cunroll_grow_size =3D flag_unroll_loops > > > + || flag_peel_loops > > > + || optimize >=3D 3; > > > + } > > > + else > > > + { > > > + if (!OPTION_SET_P (flag_cunroll_grow_size)) > > > + flag_cunroll_grow_size =3D flag_peel_loops || optimize >=3D 3; > > > + } > > > + > > > } > > > > > > /* Clear stack slot assignments remembered from previous functions. > > > @@ -2351,7 +2380,7 @@ ix86_option_override_internal (bool main_args_p= , > > > > > > set_ix86_tune_features (opts, ix86_tune, opts->x_ix86_dump_tunes); > > > > > > - ix86_recompute_optlev_based_flags (opts, opts_set); > > > + ix86_override_options_after_change (); > > > > > > ix86_tune_cost =3D processor_cost_table[ix86_tune]; > > > /* TODO: ix86_cost should be chosen at instruction or function gra= nuality > > > @@ -2382,9 +2411,6 @@ ix86_option_override_internal (bool main_args_p= , > > > || TARGET_64BIT_P (opts->x_ix86_isa_flags)) > > > opts->x_ix86_regparm =3D REGPARM_MAX; > > > > > > - /* Default align_* from the processor table. */ > > > - ix86_default_align (opts); > > > - > > > /* Provide default for -mbranch-cost=3D value. */ > > > SET_OPTION_IF_UNSET (opts, opts_set, ix86_branch_cost, > > > ix86_tune_cost->branch_cost); > > > diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index > > > 292b32c5e99..10f8b4e91a5 100644 > > > --- a/gcc/config/i386/i386.cc > > > +++ b/gcc/config/i386/i386.cc > > > @@ -23829,20 +23829,12 @@ ix86_loop_unroll_adjust (unsigned nunroll, > > > class loop *loop) > > > > > > /* Unroll small size loop when unroll factor is not explicitly > > > specified. */ > > > - if (!(flag_unroll_loops > > > - || flag_unroll_all_loops > > > - || loop->unroll)) > > > + if (ix86_unroll_only_small_loops && !loop->unroll) > > > { > > > - nunroll =3D 1; > > > - > > > - /* Any explicit -f{no-}unroll-{all-}loops turns off > > > - -munroll-only-small-loops. */ > > > - if (ix86_unroll_only_small_loops > > > - && !OPTION_SET_P (flag_unroll_loops) > > > - && loop->ninsns <=3D ix86_cost->small_unroll_ninsns) > > > - nunroll =3D ix86_cost->small_unroll_factor; > > > - > > > - return nunroll; > > > + if (loop->ninsns <=3D ix86_cost->small_unroll_ninsns) > > > + return MIN (nunroll, ix86_cost->small_unroll_factor); > > > + else > > > + return 1; > > > } > > > > > > if (!TARGET_ADJUST_UNROLL) > > > diff --git a/gcc/loop-init.cc b/gcc/loop-init.cc index 9789efa1e11..f= 03c077cae1 > > > 100644 > > > --- a/gcc/loop-init.cc > > > +++ b/gcc/loop-init.cc > > > @@ -565,12 +565,10 @@ public: > > > {} > > > > > > /* opt_pass methods: */ > > > - bool gate (function *fun) final override > > > + bool gate (function *) final override > > > { > > > - return (flag_unroll_loops || flag_unroll_all_loops || cfun->ha= s_unroll > > > - || (targetm.loop_unroll_adjust > > > - && optimize >=3D 2 > > > - && optimize_function_for_speed_p (fun))); > > > + return (flag_unroll_loops || flag_unroll_all_loops > > > + || cfun->has_unroll); > > > } > > > > > > unsigned int execute (function *) final override; @@ -586,8 +584,7= @@ > > > pass_rtl_unroll_loops::execute (function *fun) > > > if (dump_file) > > > df_dump (dump_file); > > > > > > - if (flag_unroll_loops > > > - || targetm.loop_unroll_adjust) > > > + if (flag_unroll_loops) > > > flags |=3D UAP_UNROLL; > > > if (flag_unroll_all_loops) > > > flags |=3D UAP_UNROLL_ALL; > > > diff --git a/gcc/testsuite/gcc.target/i386/pr86270.c > > > b/gcc/testsuite/gcc.target/i386/pr86270.c > > > index cbc9fbb0450..98b012caf23 100644 > > > --- a/gcc/testsuite/gcc.target/i386/pr86270.c > > > +++ b/gcc/testsuite/gcc.target/i386/pr86270.c > > > @@ -1,5 +1,5 @@ > > > /* { dg-do compile } */ > > > -/* { dg-options "-O2 -mno-unroll-only-small-loops" } */ > > > +/* { dg-options "-O2 -fno-unroll-loops" } */ > > > > > > int *a; > > > long len; > > > diff --git a/gcc/testsuite/gcc.target/i386/pr93002.c > > > b/gcc/testsuite/gcc.target/i386/pr93002.c > > > index f75a847f75d..7e2d869e17b 100644 > > > --- a/gcc/testsuite/gcc.target/i386/pr93002.c > > > +++ b/gcc/testsuite/gcc.target/i386/pr93002.c > > > @@ -1,6 +1,6 @@ > > > /* PR target/93002 */ > > > /* { dg-do compile } */ > > > -/* { dg-options "-O2 -mno-unroll-only-small-loops" } */ > > > +/* { dg-options "-O2 -fno-unroll-loops" } */ > > > /* { dg-final { scan-assembler-not "cmp\[^\n\r]*-1" } } */ > > > > > > volatile int sink; > > > -- > > > 2.18.1 > > > > > -- > BR, > Hongtao