From patchwork Thu Aug 31 12:23:44 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: 137287 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp204355vqu; Thu, 31 Aug 2023 05:24:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHm/g6KHsV+Fmi4rRXL052mSknJmBkiB4IJiH2O+DUXdiXWhf05fDayM9QXzV+JT3djCBER X-Received: by 2002:a17:906:3089:b0:9a4:88af:b77 with SMTP id 9-20020a170906308900b009a488af0b77mr4076687ejv.60.1693484675125; Thu, 31 Aug 2023 05:24:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693484675; cv=none; d=google.com; s=arc-20160816; b=E7lDLAtXOdr+6oMxnb0Zurw/dzkk4GBdGYhE7vikDDVeRY5wgoTKfCh9Xlxa26/XOS iJ4cKzi7aZIKv6WQVxbIejE7x47vERVpa5oj9fVLX8/orDgRbkVGybr8sIh98iOmghnp wHSLS/WySZRU1r3ZcL1QKmphyo8PUgLJUiDBNRoerXoyQ3spsKZfGfWs7PZB0JkDyohK d+el6/4mVcmgM4bqiIlIFjwNw20TFy6S6234lMZqrm4emasSJmeyvSw7EP+74tE6MKvD NxPhDMpVrwMtgB23mJBmrq8p9IyzHz+bbX0lm+X66JSYoZAZTsUvKihECyKk7JgOypVl xTag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id :content-transfer-encoding:mime-version:message-id:date:subject:to :from:dmarc-filter:delivered-to; bh=xwMHEsa8Wpr4+UrPRvImOLBIouDqyBTtBdutmRSAsAg=; fh=arl273cIQBNH1P6XLxHQvF0scgitfd773vOV+bwQx3o=; b=yraT+HqISHd9IubUANvdpma9OhQB07P0c5YMVIevVbEtdUET9+OlIrs2scv+P74Avp +kwrg8CGVHH251aSN7f8Z1OGJr1Rpl0EpT/fXc0RrUKr56K/3+X/Wx4JFbeDUvL8rKgh TFxskKffHyaJ+5xXydzHynMZqFwu4XsAhdibhNzTU76XuLrCsvsAhiTbi7lprLVqOF2v vL41exeHuJBLy//JtoMpr1OmCX6N/vZlfF7ot1GQsMw8YD6EyZTIgQSwfd9tp8b3u168 Nb7un/GozjzfBm/IYbB5zPW14pPAEI0bh1wQAb7Wrqm8wRZd9HTZs64tISNw5TyQoJPX UJGQ== 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 server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id p26-20020a170906b21a00b0099279210460si941204ejz.643.2023.08.31.05.24.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 05:24:35 -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 768B43857B98 for ; Thu, 31 Aug 2023 12:24:24 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbgjp3.qq.com (smtpbgjp3.qq.com [54.92.39.34]) by sourceware.org (Postfix) with ESMTPS id 5A5423858D28 for ; Thu, 31 Aug 2023 12:23:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A5423858D28 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: bizesmtp63t1693484626t81rbku3 Received: from rios-cad122.hadoop.rioslab.org ( [58.60.1.26]) by bizesmtp.qq.com (ESMTP) with id ; Thu, 31 Aug 2023 20:23:45 +0800 (CST) X-QQ-SSF: 01400000000000G0V000000A0000000 X-QQ-FEAT: 90EFqYDyPxCjX+IfNCrQR8b18kqod6vAxhxUwb29eYeoAvEXw85twaGSG0Ihp WdpGUhyvdbOxSXHXiP9AS6D3CDg4QQFJSFQzcCo8hJiMM8haiUbrBX11KB0HEB9Qu1h7NUE 3ZqDAY1uVoKG/zEjmIPW0MZxr6QNVteE2tOSD1sJAldhbGiuGhpRTIh04eYzoL4+6yiRcVh rEWzcGlraSDrGz/lZKcX7A8lP4dJhxZ4lmVIBCTboxFZUgWyQDcF1rr/ooCG38SFKHlnIKo 6vuNFCWRTj8lV95Ur4/MRxFLc7EHpJTBkW5UQyVnBrBvg6GCrTA+Gu1X7x6ZCe/L3Bm0Tfi iAFJOrSp/JBYiYIuQhEKxdjRO2/5HQ6YTlMO6Usw+zDh63gClnKlhwk+ZtZx4+H/4HEcJWS 9Av00+kDeSjVzvpfJw/0/j786P0cogMz X-QQ-GoodBg: 2 X-BIZMAIL-ID: 4470499800587718170 From: Juzhe-Zhong To: gcc-patches@gcc.gnu.org Subject: [PATCH] RISC-V: Add Vector cost model framework for RVV Date: Thu, 31 Aug 2023 20:23:44 +0800 Message-Id: <20230831122344.2460947-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:qybglogicsvrgz:qybglogicsvrgz7a-one-0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kito.cheng@sifive.com, kito.cheng@gmail.com, Juzhe-Zhong Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775747386783241017 X-GMAIL-MSGID: 1775747386783241017 Hi, currently RVV vectorization only support picking LMUL according to compile option --param=riscv-autovec-lmul= which is no ideal. Compiler should be able to pick optimal LMUL/vectorization factor to vectorize the loop according to the loop_vec_info and SSA-based register pressure analysis. Now, I figure out current GCC cost model provide the approach that we can choose LMUL/vectorization factor by adjusting the COST. This patch is just add the minimum COST model framework which is still applying the default cost model (No vector codes changed from before). Regression all pased and no difference. gcc/ChangeLog: * config.gcc: Add vector cost model framework for RVV. * config/riscv/riscv.cc (riscv_vectorize_create_costs): Ditto. (TARGET_VECTORIZE_CREATE_COSTS): Ditto. * config/riscv/t-riscv: Ditto. * config/riscv/riscv-vector-costs.cc: New file. * config/riscv/riscv-vector-costs.h: New file. --- gcc/config.gcc | 2 +- gcc/config/riscv/riscv-vector-costs.cc | 66 ++++++++++++++++++++++++++ gcc/config/riscv/riscv-vector-costs.h | 44 +++++++++++++++++ gcc/config/riscv/riscv.cc | 15 ++++++ gcc/config/riscv/t-riscv | 8 ++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 gcc/config/riscv/riscv-vector-costs.cc create mode 100644 gcc/config/riscv/riscv-vector-costs.h diff --git a/gcc/config.gcc b/gcc/config.gcc index 415e0e1ebc5..0ba1a7f494c 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -530,7 +530,7 @@ pru-*-*) ;; riscv*) cpu_type=riscv - extra_objs="riscv-builtins.o riscv-c.o riscv-sr.o riscv-shorten-memrefs.o riscv-selftests.o riscv-v.o riscv-vsetvl.o" + extra_objs="riscv-builtins.o riscv-c.o riscv-sr.o riscv-shorten-memrefs.o riscv-selftests.o riscv-v.o riscv-vsetvl.o riscv-vector-costs.o" extra_objs="${extra_objs} riscv-vector-builtins.o riscv-vector-builtins-shapes.o riscv-vector-builtins-bases.o" extra_objs="${extra_objs} thead.o" d_target_objs="riscv-d.o" diff --git a/gcc/config/riscv/riscv-vector-costs.cc b/gcc/config/riscv/riscv-vector-costs.cc new file mode 100644 index 00000000000..1a5e13d5eb3 --- /dev/null +++ b/gcc/config/riscv/riscv-vector-costs.cc @@ -0,0 +1,66 @@ +/* Cost model implementation for RISC-V 'V' Extension for GNU compiler. + Copyright (C) 2023-2023 Free Software Foundation, Inc. + Contributed by Juzhe Zhong (juzhe.zhong@rivai.ai), RiVAI Technologies Ltd. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#define IN_TARGET_CODE 1 + +#define INCLUDE_STRING +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "target.h" +#include "function.h" +#include "tree.h" +#include "basic-block.h" +#include "rtl.h" +#include "gimple.h" +#include "targhooks.h" +#include "cfgloop.h" +#include "fold-const.h" +#include "tm_p.h" +#include "tree-vectorizer.h" + +/* This file should be included last. */ +#include "riscv-vector-costs.h" + +namespace riscv_vector { + +costs::costs (vec_info *vinfo, bool costing_for_scalar) + : vector_costs (vinfo, costing_for_scalar) +{} + +unsigned +costs::add_stmt_cost (int count, vect_cost_for_stmt kind, + stmt_vec_info stmt_info, slp_tree, tree vectype, + int misalign, vect_cost_model_location where) +{ + /* TODO: Use default STMT cost model. + We will support more accurate STMT cost model later. */ + int stmt_cost = default_builtin_vectorization_cost (kind, vectype, misalign); + return record_stmt_cost (stmt_info, where, count * stmt_cost); +} + +void +costs::finish_cost (const vector_costs *scalar_costs) +{ + vector_costs::finish_cost (scalar_costs); +} + +} // namespace riscv_vector diff --git a/gcc/config/riscv/riscv-vector-costs.h b/gcc/config/riscv/riscv-vector-costs.h new file mode 100644 index 00000000000..57b1be01048 --- /dev/null +++ b/gcc/config/riscv/riscv-vector-costs.h @@ -0,0 +1,44 @@ +/* Cost model declaration of RISC-V 'V' Extension for GNU compiler. + Copyright (C) 2023-2023 Free Software Foundation, Inc. + Contributed by Juzhe Zhong (juzhe.zhong@rivai.ai), RiVAI Technologies Ltd. + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GCC is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GCC; see the file COPYING3. If not see + . */ + +#ifndef GCC_RISCV_VECTOR_COST_H +#define GCC_RISCV_VECTOR_COST_H + +namespace riscv_vector { + +/* rvv-specific vector costs. */ +class costs : public vector_costs +{ + using vector_costs::vector_costs; + +public: + costs (vec_info *, bool); + +private: + unsigned int add_stmt_cost (int count, vect_cost_for_stmt kind, + stmt_vec_info stmt_info, slp_tree node, + tree vectype, int misalign, + vect_cost_model_location where) override; + void finish_cost (const vector_costs *) override; +}; + +} // namespace riscv_vector + +#endif // GCC_RISCV_VECTOR_COST_H diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 5dc303f89c7..5df9c9d6919 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -74,6 +74,7 @@ along with GCC; see the file COPYING3. If not see /* This file should be included last. */ #include "target-def.h" +#include "riscv-vector-costs.h" /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */ #define UNSPEC_ADDRESS_P(X) \ @@ -9059,6 +9060,17 @@ riscv_frame_pointer_required (void) return riscv_save_frame_pointer && !crtl->is_leaf; } +/* Implement targetm.vectorize.create_costs. */ + +static vector_costs * +riscv_vectorize_create_costs (vec_info *vinfo, bool costing_for_scalar) +{ + if (TARGET_VECTOR) + return new riscv_vector::costs (vinfo, costing_for_scalar); + /* Default vector costs. */ + return new vector_costs (vinfo, costing_for_scalar); +} + /* Initialize the GCC target structure. */ #undef TARGET_ASM_ALIGNED_HI_OP #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" @@ -9366,6 +9378,9 @@ riscv_frame_pointer_required (void) #undef TARGET_FRAME_POINTER_REQUIRED #define TARGET_FRAME_POINTER_REQUIRED riscv_frame_pointer_required +#undef TARGET_VECTORIZE_CREATE_COSTS +#define TARGET_VECTORIZE_CREATE_COSTS riscv_vectorize_create_costs + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-riscv.h" diff --git a/gcc/config/riscv/t-riscv b/gcc/config/riscv/t-riscv index f3ce66ccdd4..b1f80d1d87c 100644 --- a/gcc/config/riscv/t-riscv +++ b/gcc/config/riscv/t-riscv @@ -67,6 +67,14 @@ riscv-vsetvl.o: $(srcdir)/config/riscv/riscv-vsetvl.cc \ $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \ $(srcdir)/config/riscv/riscv-vsetvl.cc +riscv-vector-costs.o: $(srcdir)/config/riscv/riscv-vector-costs.cc \ + $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TARGET_H) $(FUNCTION_H) \ + $(TREE_H) basic-block.h $(RTL_H) gimple.h targhooks.h cfgloop.h \ + fold-const.h $(TM_P_H) tree-vectorizer.h \ + $(srcdir)/config/riscv/riscv-vector-costs.h + $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \ + $(srcdir)/config/riscv/riscv-vector-costs.cc + riscv-d.o: $(srcdir)/config/riscv/riscv-d.cc \ $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(COMPILE) $<