From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id 3970C3824584 for ; Wed, 26 Oct 2022 08:18:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3970C3824584 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-x62e.google.com with SMTP id 13so19255884ejn.3 for ; Wed, 26 Oct 2022 01:18:38 -0700 (PDT) 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=T7EGUVJwmVTMFGOfJUvKqdJqfi1S4Iv8rwlKdp6hph4=; b=H35qZ8ADA4/PV93nWDY/gJdsJJjzwkfKNq89O0hqCn7sk2TfVPalViG60XdZo4AdoY 524lNgGJVJnvoDGpH3uYxVXGz+9qhmE/mOBuV4gzSFbqVfUOvkMidMTiz2X+Sz8rfnw0 bOo7+TAK/QoVCM3FtYvWGDVIn4BRFZNe4xt40OPN+8NKg2sv2RJjVwx/5sG5MecDpn1Q MmDW8rrI/BqDW/N6Zjc8SM8CtHKTSWjzm+SW8zpysb0noi0F1elG+mJV3VjxhNesk7HW 9Nr6LjMcTTfNWOdHmrWYY4R1/CStW7Jv9bk6ieRM0szrzxV5H1zteSGTKpviitSh8ZF0 OSQQ== 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=T7EGUVJwmVTMFGOfJUvKqdJqfi1S4Iv8rwlKdp6hph4=; b=tXokS4TzsK7f7DRIqfY+ImJzSz/f0zwV52wSmDUOAEBb72+qXIhhjNzXIUOl2aurCH WmPbv1kogBoCQu77JArLELJX1kl+PyMZbunkFfCWDf4N5iORX+7tDtpCRSgf4Eu6EEez y6sbhlheXdo4jhWVTN5A2iTXGYOAggckdFJnsAXETFvFVbYSFwzGN2cFm17rvYLlG4wW eTicq3CN4bapiR4bj6FQ+BiiChvMimVIUtlLQ1+oVRMm3O8hdo06/xdnO2p4Bwr8QJuP 9pdTk/WpSToXk0A6/pFluxsKwBQnBeI0KenZalR/JA23vveIXxmTKicUhMMs5JE+Il9f MOWw== X-Gm-Message-State: ACrzQf1RdtupkEX+HUsu/YAXCVgjqxTQmjjU2beqrtTAEUVxfJoic2ny 7K38CpHCnBFvLsmhUUl+ZjJ9gVFXteOb8pjBTQjIbEl3rmh+dgn3 X-Google-Smtp-Source: AMsMyM5XXM7dyGBjlZ2L1J2uzpietnVETkzQZIcM33ALaWW0UqWMgrutR3lN9s8lLo7N6ONs6TGzjCQSUcioC9YBBiw= X-Received: by 2002:a17:907:608f:b0:78e:1b60:60e2 with SMTP id ht15-20020a170907608f00b0078e1b6060e2mr36955479ejc.382.1666772315555; Wed, 26 Oct 2022 01:18:35 -0700 (PDT) MIME-Version: 1.0 References: <20221026055248.94100-1-hongyu.wang@intel.com> In-Reply-To: From: Hongyu Wang Date: Wed, 26 Oct 2022 16:13:31 +0800 Message-ID: Subject: Re: [PATCH] i386: Enable small loop unrolling for O2 To: Uros Bizjak Cc: Hongyu Wang , hongtao.liu@intel.com, gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-6.6 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_NUMSUBJECT,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: > 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 idea to unroll small loops can be explained from the x86 micro-architecture. Modern x86 processors has multiple way instruction decoder (5uops for icelake/zen3). So for small loop with <=3D 4 instructions (usually has 3 uops with a cmp/jmp pair that can be macro-fused), the decoder would have 2 uops bubble for each iteration and the pipeline could not be fully utilized. Therefore we decide to unroll the 4 insn loop once to at least to full-fill the decoder and enhance the pipeline utilization. We are not familiar with micro architecture of other targets, we don't know whether the unrolling could benefit the instruction decoder, so the decision could be different. Uros Bizjak via Gcc-patches =E4=BA=8E2022=E5=B9= =B410=E6=9C=8826=E6=97=A5=E5=91=A8=E4=B8=89 14:57=E5=86=99=E9=81=93=EF=BC= =9A > > On Wed, Oct 26, 2022 at 7:53 AM Hongyu Wang 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=3Dx86-small-unroll-ninsns=3D for loop insn limit, > > -param=3Dx86-small-unroll-factor=3D 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[] =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 }, > > + { 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-opt= ions.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_op= tions *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 =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; > > + 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; > > + /* 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 =3D flag_unroll_all_loops =3D 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 =3D processor_cost_table[ix86_tune]; > > /* TODO: ix86_cost should be chosen at instruction or function granu= ality > > @@ -2363,9 +2398,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 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, cla= ss loop *loop) > > unsigned i; > > unsigned mem_count =3D 0; > > > > + /* Unroll small size loop when unroll factor is not explicitly > > + specified. */ > > + if (ix86_unroll_only_small_loops && !loop->unroll) > > + { > > + int small_unroll =3D 0; > > + if (loop->ninsns <=3D (unsigned) ix86_small_unroll_ninsns) > > + small_unroll =3D MIN ((unsigned) ix86_small_unroll_factor, > > + nunroll); > > + else > > + small_unroll =3D 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=3Dx86-stlf-window-ninsns=3D > > 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=3Dx86-small-unroll-ninsns=3D > > +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=3Dx86-small-unroll-factor=3D > > +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 availa= ble 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 instru= ction > > +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 on= ce. > > + > > @end table > > > > @end table > > @@ -25186,6 +25194,12 @@ environments where no dynamic link is performe= d, like firmwares, OS > > kernels, executables linked with @option{-static} or @option{-static-p= ie}. > > @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/gc= c.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/gc= c.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 > >