From patchwork Sat Nov 19 06:25:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hongyu Wang X-Patchwork-Id: 23220 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp579961wrr; Fri, 18 Nov 2022 22:26:32 -0800 (PST) X-Google-Smtp-Source: AA0mqf43EU3xo2a7it1hCCn5aXyR2vVz/Er7FsrfpplZEFJRcO7JghF8dC4tWP8HisdehNbQU6tl X-Received: by 2002:a17:907:b686:b0:78d:f796:c00d with SMTP id vm6-20020a170907b68600b0078df796c00dmr8417522ejc.251.1668839192228; Fri, 18 Nov 2022 22:26:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668839192; cv=none; d=google.com; s=arc-20160816; b=0gaCFn8N976RHhnBdCPmW6pW+MZYLwVsCxlxVk8BG4MVTBAsbNDvJWW+qzaNiVfHdg RBxVBNmdbmdBB6nYgtk0k+YLN15maRhJ8kWjYQ0grExFrKHixJ2uemRosXwrZLheVubP ciMC3gR3dryp/tN9h1Tlo5PN/TMIxFlh2do/zjUxWzfQ67/V4/QjTfBC7OQ1ARagWY9u J1UUUofz/a40UPwYeS1Je4wqzz7klOV+GVJuNiiQR7IBSxyFYkRj/PInCHjNqj+GGQ0M 23fH6lvGFBIk6hY9RSKV9UXO3FdFxJEkWdyroFdg+HY2l4+i76rtGVBW+BhnhrSn2Q05 PVbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id:date :subject:cc:to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=PZRZ3/VEF1SHRSwW38n7J74gHB3JYaS8sN5c/sai9gQ=; b=rS/nqusjKfkDW2KwYyfIc/5ef+xQH5IG44ITBdVFgC6wsbR6hI85W1sASGfr1h2PXn ZrIXyY/QGrCt5DBIZsZNAA2Rd2eeq/LNWraApX6vMVS8P0mNPO13Jj+olu2X7uNm9Hev vrp1V7WGW7zg9v0GtMj54VkBwLwr0AjuNfrajHbiIKAlGHRRpRKQ6tUix6mJNVaaDaPX QZsmhcWBoFH2emojPAsMTDFN0ISxZ83nGbpwJQHWOxX6lxRpVBwAK0CxGYKZbZeZCoRs DNP9KFQqDUZIMO5Mr64OnLM2/7YBbk71Y0gj6tertx4/kUK+PmDagwO34efm0kb2dEuh eFUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="I3SDc/fH"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id du13-20020a17090772cd00b007ae26c33a6fsi4823154ejc.685.2022.11.18.22.26.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 22:26:32 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="I3SDc/fH"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0CEBE385840A for ; Sat, 19 Nov 2022 06:26:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0CEBE385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668839191; bh=PZRZ3/VEF1SHRSwW38n7J74gHB3JYaS8sN5c/sai9gQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=I3SDc/fHcZ4IPyVJIU6WpXhntHTVZx79tl/kuP89d6eFX4YsI36IZc3LcaAP0NbJn EZkqi1SCSTeQsGIEuakfTYJpfLqWYo5hDvbNY8mtVLP1kAjDlQ20McS2CHacwtV9bP 5ExLkFq9ydUXR0gd93qJ0ug05k5yWnnSGbxhQJUk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by sourceware.org (Postfix) with ESMTPS id D2FC43858C52 for ; Sat, 19 Nov 2022 06:25:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D2FC43858C52 X-IronPort-AV: E=McAfee;i="6500,9779,10535"; a="310927896" X-IronPort-AV: E=Sophos;i="5.96,175,1665471600"; d="scan'208";a="310927896" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Nov 2022 22:25:35 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10535"; a="709277606" X-IronPort-AV: E=Sophos;i="5.96,175,1665471600"; d="scan'208";a="709277606" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmsmga004.fm.intel.com with ESMTP; 18 Nov 2022 22:25:33 -0800 Received: from shliclel320.sh.intel.com (shliclel320.sh.intel.com [10.239.240.127]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 85F2D10054F8; Sat, 19 Nov 2022 14:25:32 +0800 (CST) To: gcc-patches@gcc.gnu.org Cc: richard.guenther@gmail.com, ubizjak@gmail.com, hongtao.liu@intel.com Subject: [PATCH] i386: Only enable small loop unrolling in backend [PR 107602] Date: Sat, 19 Nov 2022 14:25:32 +0800 Message-Id: <20221119062532.75190-1-hongyu.wang@intel.com> X-Mailer: git-send-email 2.18.1 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_SHORT, SPF_HELO_NONE, SPF_SOFTFAIL, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Hongyu Wang via Gcc-patches From: Hongyu Wang Reply-To: Hongyu Wang Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749904724672158691?= X-GMAIL-MSGID: =?utf-8?q?1749904724672158691?= Hi, Followed by the discussion in pr107602, -munroll-only-small-loops Does not 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 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;