public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Uros Bizjak <ubizjak@gmail.com>
To: Hongyu Wang <hongyu.wang@intel.com>
Cc: gcc-patches@gcc.gnu.org, richard.guenther@gmail.com,
	hongtao.liu@intel.com
Subject: Re: [PATCH] i386: Enable small loop unrolling for O2
Date: Wed, 26 Oct 2022 08:56:27 +0200	[thread overview]
Message-ID: <CAFULd4a_PJY_t-=FVTtvcacgCu+azWsfX-8rAqFudy93bHXreQ@mail.gmail.com> (raw)
In-Reply-To: <20221026055248.94100-1-hongyu.wang@intel.com>

On Wed, Oct 26, 2022 at 7:53 AM Hongyu Wang <hongyu.wang@intel.com> wrote:
>
> Hi,
>
> Inspired by rs6000 and s390 port changes, this patch
> enables loop unrolling for small size loop at O2 by default.
> The default behavior is to unroll loop with unknown trip-count and
> less than 4 insns by 1 time.
>
> This improves 548.exchange2 by 3.5% on icelake and 6% on zen3 with
> 1.2% codesize increment. For other benchmarks the variants are minor
> and overall codesize increased by 0.2%.
>
> The kernel image size increased by 0.06%, and no impact on eembc.

Does this setting benefit all targets?  IIRC, in the past all
benchmarks also enabled -funroll-loops, so it looks to me that
unrolling small loops by default is a good compromise.

The patch is technically OK, but as a tuning default, I would leave
the final approval to HJ.

Thanks,
Uros.

>
> Bootstrapped & regrtested on x86_64-pc-linux-gnu.
>
> Ok for trunk?
>
> gcc/ChangeLog:
>
>         * common/config/i386/i386-common.cc (ix86_optimization_table):
>         Enable loop unroll and small loop unroll at O2 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.
>         * config/i386/i386.opt: Add -munroll-only-small-loops,
>         -param=x86-small-unroll-ninsns= for loop insn limit,
>         -param=x86-small-unroll-factor= for unroll factor.
>         * doc/invoke.texi: Document -munroll-only-small-loops,
>         x86-small-unroll-ninsns and x86-small-unroll-factor.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/i386/pr86270.c: Add -mno-unroll-only-small-loops.
>         * gcc.target/i386/pr93002.c: Likewise.
> ---
>  gcc/common/config/i386/i386-common.cc   |  6 ++++
>  gcc/config/i386/i386-options.cc         | 40 ++++++++++++++++++++++---
>  gcc/config/i386/i386.cc                 | 13 ++++++++
>  gcc/config/i386/i386.opt                | 13 ++++++++
>  gcc/doc/invoke.texi                     | 14 +++++++++
>  gcc/testsuite/gcc.target/i386/pr86270.c |  2 +-
>  gcc/testsuite/gcc.target/i386/pr93002.c |  2 +-
>  7 files changed, 84 insertions(+), 6 deletions(-)
>
> diff --git a/gcc/common/config/i386/i386-common.cc b/gcc/common/config/i386/i386-common.cc
> index d6a68dc9b1d..0e580b39d14 100644
> --- a/gcc/common/config/i386/i386-common.cc
> +++ b/gcc/common/config/i386/i386-common.cc
> @@ -1686,6 +1686,12 @@ 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 },
> +    { 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 acb2291e70f..6ea347c32e1 100644
> --- a/gcc/config/i386/i386-options.cc
> +++ b/gcc/config/i386/i386-options.cc
> @@ -1819,8 +1819,43 @@ 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;
> +      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;
> +      /* Disables loop unrolling if -mno-unroll-only-small-loops is
> +        explicitly set and -funroll-loops is not enabled.  */
> +      if (OPTION_SET_P (ix86_unroll_only_small_loops)
> +         && !ix86_unroll_only_small_loops
> +         && !(OPTION_SET_P (flag_unroll_loops)
> +              || OPTION_SET_P (flag_unroll_all_loops)))
> +       flag_unroll_loops = flag_unroll_all_loops = 0;
> +    }
> +
>  }
>
>  /* Clear stack slot assignments remembered from previous functions.
> @@ -2332,7 +2367,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
> @@ -2363,9 +2398,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 480db35f6cd..75829a5d0f4 100644
> --- a/gcc/config/i386/i386.cc
> +++ b/gcc/config/i386/i386.cc
> @@ -23820,6 +23820,19 @@ ix86_loop_unroll_adjust (unsigned nunroll, class loop *loop)
>    unsigned i;
>    unsigned mem_count = 0;
>
> +  /* Unroll small size loop when unroll factor is not explicitly
> +     specified.  */
> +  if (ix86_unroll_only_small_loops && !loop->unroll)
> +    {
> +      int small_unroll = 0;
> +      if (loop->ninsns <= (unsigned) ix86_small_unroll_ninsns)
> +       small_unroll = MIN ((unsigned) ix86_small_unroll_factor,
> +                           nunroll);
> +      else
> +       small_unroll = 1;
> +      return small_unroll;
> +    }
> +
>    if (!TARGET_ADJUST_UNROLL)
>       return nunroll;
>
> diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
> index 0dbaacb57ed..a724c73c0c4 100644
> --- a/gcc/config/i386/i386.opt
> +++ b/gcc/config/i386/i386.opt
> @@ -1214,3 +1214,16 @@ Do not use GOT to access external symbols.
>  -param=x86-stlf-window-ninsns=
>  Target Joined UInteger Var(x86_stlf_window_ninsns) Init(64) Param
>  Instructions number above which STFL stall penalty can be compensated.
> +
> +munroll-only-small-loops
> +Target Var(ix86_unroll_only_small_loops) Init(0) Save
> +Enable conservative small loop unrolling.
> +
> +-param=x86-small-unroll-ninsns=
> +Target Joined UInteger Var(ix86_small_unroll_ninsns) Init(4) Param
> +Insturctions number limit for loop to be unrolled under
> +-munroll-only-small-loops.
> +
> +-param=x86-small-unroll-factor=
> +Target Joined UInteger Var(ix86_small_unroll_factor) Init(2) Param
> +Unroll factor for -munroll-only-small-loops.
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index cd4d3c1d72c..b6fa79eccc3 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -15779,6 +15779,14 @@ The following choices of @var{name} are available on i386 and x86_64 targets:
>  @item x86-stlf-window-ninsns
>  Instructions number above which STFL stall penalty can be compensated.
>
> +@item x86-small-unroll-ninsns
> +If -munroll-only-small-loops is enabled, only unroll loops with instruction
> +count less than this parameter. The default value is 4.
> +
> +@item x86-small-unroll-factor
> +If -munroll-only-small-loops is enabled, reset the unroll factor with this
> +value. The default value is 2 which means the loop will be unrolled once.
> +
>  @end table
>
>  @end table
> @@ -25186,6 +25194,12 @@ environments where no dynamic link is performed, like firmwares, OS
>  kernels, executables linked with @option{-static} or @option{-static-pie}.
>  @option{-mdirect-extern-access} is not compatible with @option{-fPIC} or
>  @option{-fpic}.
> +
> +@item -munroll-only-small-loops
> +@itemx -mno-unroll-only-small-loops
> +@opindex munroll-only-small-loops
> +Controls conservative small loop unrolling. It is default enbaled by
> +O2, and unrolls loop with less than 4 insns by 1 time.
>  @end table
>
>  @node M32C Options
> diff --git a/gcc/testsuite/gcc.target/i386/pr86270.c b/gcc/testsuite/gcc.target/i386/pr86270.c
> index 81841ef5bd7..cbc9fbb0450 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" } */
> +/* { dg-options "-O2 -mno-unroll-only-small-loops" } */
>
>  int *a;
>  long len;
> diff --git a/gcc/testsuite/gcc.target/i386/pr93002.c b/gcc/testsuite/gcc.target/i386/pr93002.c
> index 0248fcc00a5..f75a847f75d 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" } */
> +/* { dg-options "-O2 -mno-unroll-only-small-loops" } */
>  /* { dg-final { scan-assembler-not "cmp\[^\n\r]*-1" } } */
>
>  volatile int sink;
> --
> 2.18.1
>

  reply	other threads:[~2022-10-26  6:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26  5:52 Hongyu Wang
2022-10-26  6:56 ` Uros Bizjak [this message]
2022-10-26  8:13   ` Hongyu Wang
2022-10-28  7:33 ` Richard Biener
2022-10-28  8:03   ` Hongyu Wang
2022-10-28  8:41     ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFULd4a_PJY_t-=FVTtvcacgCu+azWsfX-8rAqFudy93bHXreQ@mail.gmail.com' \
    --to=ubizjak@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hongtao.liu@intel.com \
    --cc=hongyu.wang@intel.com \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).