From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x112a.google.com (mail-yw1-x112a.google.com [IPv6:2607:f8b0:4864:20::112a]) by sourceware.org (Postfix) with ESMTPS id 3CAAD3858D35 for ; Mon, 21 Nov 2022 01:32:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3CAAD3858D35 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-yw1-x112a.google.com with SMTP id 00721157ae682-39115d17f3dso94273427b3.1 for ; Sun, 20 Nov 2022 17:32:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=DWipJP1542aHQkm2Cj0FNA0zkGgPOFm0i9+VoutwHFI=; b=J2RFuMpzDAKEzlWw1ZRidOH9nBXuoEBJjsBTJq4Zg3Wh1/Fu0Udy5ZAN33w6xwMr6O aFpgXce17nJDs5EM5v3VklEb0hxcoMC+VU0HkIFf64Ufm+eTI/oZWJom3RGrBsWn8Pab Uhka/dlWxVQAV/ZzEY7hxBiHLnGfP5FM2yziZfdBeEfS5Dy9XoSzQxiAJ9DbAtORsLAE Jnvnxi3jV03G21DoXzCG1NDDbF2B9TWXg36TXLNvk5Fk2nj3pY8MoE8ERxxF1z4Het8y sSAW4AqJ3XMg5L/giuj+5i5gbBmhoCPAxnfv5ljiM+NfQeDi9weir44fpDuObY47/ice 2c+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=DWipJP1542aHQkm2Cj0FNA0zkGgPOFm0i9+VoutwHFI=; b=ViLYIKDag/etArenqzoOSwXqDL3X7BsQly6oj8AbrXmPmshahQaDiOsVhPdcexNWWY zfYMFGU/K0J1rXRqG1+hXxzjXFf1J5BijKE/XzIRW6Hfj8n+Qe80W24l4zPgpuhnMK7U itFsbWmehKId9G32mvV7o07EW1PilD5vbMKTTopjFxLQzdDuCZiqmX4EcXESznnRWWZP z2FS5yNtBGAWWwfUcxLpxT4DigkMUau5Ro77G4BxPtadf7LbFGB/icfP60e5hdLrmihW kJxq7hMCaEbvYU6G7Xbd7noB6Jrfy//E+WnnfXX3TxLi/Fmk31Unw2wDleCyUFAd3poC jtJg== X-Gm-Message-State: ANoB5plUB6wIrv72WmmzFX8y/4UXA14mxur5NNMNbbUTXRZ/9IEit86O aLQMMHkO+SXsqBNNqqRpzD/uTS86H1g3uDHn6zw= X-Google-Smtp-Source: AA0mqf7p1Ddon/SEFFolbvYpt200sbtS2D00Di+z2XMD6R2uY/xt3XKtO7fnHus8xFFLkLSnZCPgzaPNbiXgRAbnZNQ= X-Received: by 2002:a0d:ddcd:0:b0:378:a018:4b04 with SMTP id g196-20020a0dddcd000000b00378a0184b04mr15073977ywe.186.1668994352521; Sun, 20 Nov 2022 17:32:32 -0800 (PST) MIME-Version: 1.0 References: <20221119062532.75190-1-hongyu.wang@intel.com> In-Reply-To: From: Hongtao Liu Date: Mon, 21 Nov 2022 09:35:41 +0800 Message-ID: Subject: Re: [PATCH] i386: Only enable small loop unrolling in backend [PR 107602] To: "Liu, Hongtao" Cc: "Wang, Hongyu" , "gcc-patches@gcc.gnu.org" , "richard.guenther@gmail.com" , "ubizjak@gmail.com" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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: 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 [PR 107602] > > > > Hi, > > > > Followed by the discussion in pr107602, -munroll-only-small-loops Does not > PR107692? > > turns on/off -funroll-loops, and current check in pass_rtl_unroll_loops::gate > > would cause -funroll-loops do not take effect. Revert the change about 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 strictly > > follow the rule that -funroll-loops takes full control of loop unrolling, and > > munroll-only-small-loops just change its behavior to unroll small size 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[] = > > /* 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 enable 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, NULL, > > 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 index > > 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_options_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 = 0; > > + /* Re-enable -frename-registers and -fweb if funroll-loops > > + enabled. */ > > + if (!OPTION_SET_P (flag_web)) > > + flag_web = flag_unroll_loops; > > + if (!OPTION_SET_P (flag_rename_registers)) > > + flag_rename_registers = flag_unroll_loops; > > + /* -fcunroll-grow-size default follws -f[no]-unroll-loops. */ > > + if (!OPTION_SET_P (flag_cunroll_grow_size)) > > + flag_cunroll_grow_size = flag_unroll_loops > > + || flag_peel_loops > > + || optimize >= 3; > > + } > > + else > > + { > > + if (!OPTION_SET_P (flag_cunroll_grow_size)) > > + flag_cunroll_grow_size = flag_peel_loops || optimize >= 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 = processor_cost_table[ix86_tune]; > > /* TODO: ix86_cost should be chosen at instruction or function granuality > > @@ -2382,9 +2411,6 @@ ix86_option_override_internal (bool main_args_p, > > || TARGET_64BIT_P (opts->x_ix86_isa_flags)) > > opts->x_ix86_regparm = REGPARM_MAX; > > > > - /* Default align_* from the processor table. */ > > - ix86_default_align (opts); > > - > > /* Provide default for -mbranch-cost= 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 = 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 <= ix86_cost->small_unroll_ninsns) > > - nunroll = ix86_cost->small_unroll_factor; > > - > > - return nunroll; > > + if (loop->ninsns <= 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..f03c077cae1 > > 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->has_unroll > > - || (targetm.loop_unroll_adjust > > - && optimize >= 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 |= UAP_UNROLL; > > if (flag_unroll_all_loops) > > flags |= 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