public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-3593] RISC-V: Add Vector cost model framework for RVV
@ 2023-08-31 12:42 Pan Li
  0 siblings, 0 replies; only message in thread
From: Pan Li @ 2023-08-31 12:42 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4da3065a6422062b029df9660a226297802455f4

commit r14-3593-g4da3065a6422062b029df9660a226297802455f4
Author: Juzhe-Zhong <juzhe.zhong@rivai.ai>
Date:   Thu Aug 31 20:23:44 2023 +0800

    RISC-V: Add Vector cost model framework for RVV
    
    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.

Diff:
---
 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(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 415e0e1ebc57..0ba1a7f494cc 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 000000000000..1a5e13d5eb3b
--- /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
+<http://www.gnu.org/licenses/>.  */
+
+#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 000000000000..57b1be010487
--- /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
+   <http://www.gnu.org/licenses/>.  */
+
+#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 8bca8075713e..8d8f7b4f16ed 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)					\
@@ -9058,6 +9059,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"
@@ -9365,6 +9377,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 f3ce66ccdd4f..b1f80d1d87ca 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) $<

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-08-31 12:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-31 12:42 [gcc r14-3593] RISC-V: Add Vector cost model framework for RVV Pan Li

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).