From patchwork Fri Apr 7 01:34:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "juzhe.zhong@rivai.ai" X-Patchwork-Id: 80599 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1418089vqo; Thu, 6 Apr 2023 18:34:54 -0700 (PDT) X-Google-Smtp-Source: AKy350Y8FVBfsYliq7CcPiNOx7w0eg7pTobs3vqIx9fxYuMFzvO/EDk4J2Y2FfDpl9801QegCBro X-Received: by 2002:a17:906:391:b0:946:fa68:1a91 with SMTP id b17-20020a170906039100b00946fa681a91mr818675eja.75.1680831294657; Thu, 06 Apr 2023 18:34:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680831294; cv=none; d=google.com; s=arc-20160816; b=b4KphBs/OuYXpz/IIx+IZ1BoIlvTZ7bdKaMVG1Dgjh6veLZxCc3Qyc0JUEPNAZJjca 3W7KKX5tRKwypE/neWXM+MOwWaeDevDG4PbAUEkFEEtXUjMc68gMOZv4AHg93Zsjw+eO KN3nlSTds4IIavYeHyR/tEKhzS9FoTunOt82ejEuXMg4JLy3taB10ue8gy19j4uKYk4p TBJ9ubTQ4GVyHcecoTBHpYayU4HeJidpquStI8x5zF6Zhp8bT7kwl9E5lxuggg5lB5P3 9MsaJ1wTO7UJEWdrGGIknDQX2p1kAWD6PYlVM9SsKXUsMuEv8Si0fIGMZo/5lHI06KnP dwRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dmarc-filter:delivered-to; bh=I3Q36U+4atVzp+62bcMgyXainY3vk882xlPvjaPiK5Q=; b=QR/EGq1KWVaE4DxRXAbpHeV2QeKcE+5PCAyq7bVD6OYIDQI9zaz+tah2qh5Xc0+0Lf vMP+9NpDF1hfq20JBHVmtJMfGAjd8evqNQzSffxrV/P6U2mnxT+Uu7SSzKEzfh5ut4ag gqv1UykOMnyZze2NAgZGf9hQEfid6d6Zuw4dHWqFtBg7PqrrdiUc2dZTbaD7YIWQ2qVk qM+Zo3naCFG094YWzD5MfIxqm9RU8IWPvtXg8kE3REYn9npMpCd7cMcbcmKSyKHZrqip 3QAQmcutJ0D3PVILmIyRH1jAwEmJ7DAQYdDssLBO7WPpEHz8xPx75iaMWd2E+JI2oGlW 6u2A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s13-20020a1709066c8d00b009476ce976a3si2183820ejr.130.2023.04.06.18.34.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 18:34:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1FE7D3857711 for ; Fri, 7 Apr 2023 01:34:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbguseast2.qq.com (smtpbguseast2.qq.com [54.204.34.130]) by sourceware.org (Postfix) with ESMTPS id 75D6D3858D28 for ; Fri, 7 Apr 2023 01:34:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 75D6D3858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivai.ai Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivai.ai X-QQ-mid: bizesmtp89t1680831255t4rf8hs8 Received: from rios-cad5.localdomain ( [58.60.1.11]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 07 Apr 2023 09:34:14 +0800 (CST) X-QQ-SSF: 01400000000000F0O000000A0000000 X-QQ-FEAT: k0mQ4ihyJQPM1IFYyO27+QRqU+FB3BiOadxHFof+JiWfYgm9nC4713PaUN8HA lSmIyRm7CKmyBtDujlBlN9t9tUG8pj32b0OBQMpFrrSy1jJ36/gy3joX21OK3BVQVQzRdvZ owkABUGyXnwkbUYvROzEh1XIJP6EQs6LYeCoQKTj2APtyYp9kf4DwuCUQVvsi09xGsh0VJ6 CxVrHzE1kMJgx8+598URmkX5M1+6ol+is5dC1BEf/3xo43SyMcLTuiCQPX5qQj9sP9rBJUD Z2z/EmwFAs16vOuNutpb4w2OwRSi0UueK/NS5UbJRo6RKL6aWanF3AN993oS3SoPYQiYodd JwI331DH+7j+kNBcMabXE5K4UpXS/0TxDQfenFPpVw0mNoN11ISupLrZpH+XEph+nNHdHAc X-QQ-GoodBg: 2 X-BIZMAIL-ID: 6949927638587526606 From: juzhe.zhong@rivai.ai To: gcc-patches@gcc.gnu.org Cc: kito.cheng@gmail.com, palmer@dabbelt.com, jeffreyalaw@gmail.com, Juzhe-Zhong Subject: [PATCH] RISC-V: Add local user vsetvl instruction elimination Date: Fri, 7 Apr 2023 09:34:13 +0800 Message-Id: <20230407013413.127686-1-juzhe.zhong@rivai.ai> X-Mailer: git-send-email 2.36.3 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:rivai.ai:qybglogicsvr:qybglogicsvr7 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, 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 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: , 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?1762479355686878516?= X-GMAIL-MSGID: =?utf-8?q?1762479355686878516?= From: Juzhe-Zhong This patch is to enhance optimization for auto-vectorization. Before this patch: Loop: vsetvl a5,a2... vsetvl zero,a5... vle After this patch: Loop: vsetvl a5,a2 vle gcc/ChangeLog: * config/riscv/riscv-vsetvl.cc (local_eliminate_vsetvl_insn): New function. (vector_insn_info::skip_avl_compatible_p): Ditto. (vector_insn_info::merge): Remove default value. (pass_vsetvl::compute_local_backward_infos): Ditto. (pass_vsetvl::cleanup_insns): Add local vsetvl elimination. * config/riscv/riscv-vsetvl.h: Ditto. --- gcc/config/riscv/riscv-vsetvl.cc | 71 +++++++++++++++++++++++++++++++- gcc/config/riscv/riscv-vsetvl.h | 1 + 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/gcc/config/riscv/riscv-vsetvl.cc b/gcc/config/riscv/riscv-vsetvl.cc index 7e8a5376705..b402035f7a5 100644 --- a/gcc/config/riscv/riscv-vsetvl.cc +++ b/gcc/config/riscv/riscv-vsetvl.cc @@ -1054,6 +1054,51 @@ change_vsetvl_insn (const insn_info *insn, const vector_insn_info &info) change_insn (rinsn, new_pat); } +static void +local_eliminate_vsetvl_insn (const vector_insn_info &dem) +{ + const insn_info *insn = dem.get_insn (); + if (!insn || insn->is_artificial ()) + return; + rtx_insn *rinsn = insn->rtl (); + const bb_info *bb = insn->bb (); + if (vsetvl_insn_p (rinsn)) + { + rtx vl = get_vl (rinsn); + for (insn_info *i = insn->next_nondebug_insn (); + real_insn_and_same_bb_p (i, bb); i = i->next_nondebug_insn ()) + { + if (i->is_call () || i->is_asm () + || find_access (i->defs (), VL_REGNUM) + || find_access (i->defs (), VTYPE_REGNUM)) + return; + + if (has_vtype_op (i->rtl ())) + { + if (!vsetvl_discard_result_insn_p (PREV_INSN (i->rtl ()))) + return; + rtx avl = get_avl (i->rtl ()); + if (avl != vl) + return; + set_info *def = find_access (i->uses (), REGNO (avl))->def (); + if (def->insn () != insn) + return; + + vector_insn_info new_info; + new_info.parse_insn (i); + if (!new_info.skip_avl_compatible_p (dem)) + return; + + new_info.set_avl_info (dem.get_avl_info ()); + new_info = dem.merge (new_info, LOCAL_MERGE); + change_vsetvl_insn (insn, new_info); + eliminate_insn (PREV_INSN (i->rtl ())); + return; + } + } + } +} + static bool source_equal_p (insn_info *insn1, insn_info *insn2) { @@ -1984,6 +2029,19 @@ vector_insn_info::compatible_p (const vector_insn_info &other) const return true; } +bool +vector_insn_info::skip_avl_compatible_p (const vector_insn_info &other) const +{ + gcc_assert (valid_or_dirty_p () && other.valid_or_dirty_p () + && "Can't compare invalid demanded infos"); + unsigned array_size = sizeof (incompatible_conds) / sizeof (demands_cond); + /* Bypass AVL incompatible cases. */ + for (unsigned i = 1; i < array_size; i++) + if (incompatible_conds[i].dual_incompatible_p (*this, other)) + return false; + return true; +} + bool vector_insn_info::compatible_avl_p (const vl_vtype_info &other) const { @@ -2178,7 +2236,7 @@ vector_insn_info::fuse_mask_policy (const vector_insn_info &info1, vector_insn_info vector_insn_info::merge (const vector_insn_info &merge_info, - enum merge_type type = LOCAL_MERGE) const + enum merge_type type) const { if (!vsetvl_insn_p (get_insn ()->rtl ())) gcc_assert (this->compatible_p (merge_info) @@ -2716,7 +2774,7 @@ pass_vsetvl::compute_local_backward_infos (const bb_info *bb) && !reg_available_p (insn, change)) && change.compatible_p (info)) { - info = change.merge (info); + info = change.merge (info, LOCAL_MERGE); /* Fix PR109399, we should update user vsetvl instruction if there is a change in demand fusion. */ if (vsetvl_insn_p (insn->rtl ())) @@ -3998,6 +4056,15 @@ pass_vsetvl::cleanup_insns (void) const for (insn_info *insn : bb->real_nondebug_insns ()) { rtx_insn *rinsn = insn->rtl (); + const auto &dem = m_vector_manager->vector_insn_infos[insn->uid ()]; + /* Eliminate local vsetvl: + bb 0: + vsetvl a5,a6,... + vsetvl zero,a5. + + Eliminate vsetvl in bb2 when a5 is only coming from + bb 0. */ + local_eliminate_vsetvl_insn (dem); if (vlmax_avl_insn_p (rinsn)) { diff --git a/gcc/config/riscv/riscv-vsetvl.h b/gcc/config/riscv/riscv-vsetvl.h index d05472c86a0..d7a6c14e931 100644 --- a/gcc/config/riscv/riscv-vsetvl.h +++ b/gcc/config/riscv/riscv-vsetvl.h @@ -380,6 +380,7 @@ public: void fuse_mask_policy (const vector_insn_info &, const vector_insn_info &); bool compatible_p (const vector_insn_info &) const; + bool skip_avl_compatible_p (const vector_insn_info &) const; bool compatible_avl_p (const vl_vtype_info &) const; bool compatible_avl_p (const avl_info &) const; bool compatible_vtype_p (const vl_vtype_info &) const;