RISC-V: Fix dynamic LMUL cost model ICE

Message ID 20231109023917.3985192-1-juzhe.zhong@rivai.ai
State Unresolved
Headers
Series RISC-V: Fix dynamic LMUL cost model ICE |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

juzhe.zhong@rivai.ai Nov. 9, 2023, 2:39 a.m. UTC
  When trying to use dynamic LMUL to compile benchmark.
Notice there is a bunch ICEs.

This patch fixes those ICEs and append tests.

gcc/ChangeLog:

	* config/riscv/riscv-vector-costs.cc (costs::preferred_new_lmul_p): Fix ICE.

gcc/testsuite/ChangeLog:

	* gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c: New test.
	* gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c: New test.
	* gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c: New test.

---
 gcc/config/riscv/riscv-vector-costs.cc        | 11 +++++---
 .../costmodel/riscv/rvv/dynamic-lmul-ice-1.c  | 25 +++++++++++++++++++
 .../costmodel/riscv/rvv/dynamic-lmul-ice-2.c  | 22 ++++++++++++++++
 .../costmodel/riscv/rvv/dynamic-lmul-ice-3.c  | 14 +++++++++++
 4 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
  

Comments

Kito Cheng Nov. 9, 2023, 2:43 a.m. UTC | #1
LGTM, thanks :)

On Thu, Nov 9, 2023 at 10:39 AM Juzhe-Zhong <juzhe.zhong@rivai.ai> wrote:
>
> When trying to use dynamic LMUL to compile benchmark.
> Notice there is a bunch ICEs.
>
> This patch fixes those ICEs and append tests.
>
> gcc/ChangeLog:
>
>         * config/riscv/riscv-vector-costs.cc (costs::preferred_new_lmul_p): Fix ICE.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c: New test.
>         * gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c: New test.
>         * gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c: New test.
>
> ---
>  gcc/config/riscv/riscv-vector-costs.cc        | 11 +++++---
>  .../costmodel/riscv/rvv/dynamic-lmul-ice-1.c  | 25 +++++++++++++++++++
>  .../costmodel/riscv/rvv/dynamic-lmul-ice-2.c  | 22 ++++++++++++++++
>  .../costmodel/riscv/rvv/dynamic-lmul-ice-3.c  | 14 +++++++++++
>  4 files changed, 69 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
>  create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
>
> diff --git a/gcc/config/riscv/riscv-vector-costs.cc b/gcc/config/riscv/riscv-vector-costs.cc
> index af87388a1e4..8036c9c40d7 100644
> --- a/gcc/config/riscv/riscv-vector-costs.cc
> +++ b/gcc/config/riscv/riscv-vector-costs.cc
> @@ -231,7 +231,9 @@ compute_local_live_ranges (
>
>                      TODO: We may elide the cases that the unnecessary IMM in
>                      the future.  */
> -                 if (is_gimple_val (var) && !POINTER_TYPE_P (TREE_TYPE (var)))
> +                 if (poly_int_tree_p (var)
> +                     || (is_gimple_val (var)
> +                         && !POINTER_TYPE_P (TREE_TYPE (var))))
>                     {
>                       biggest_mode
>                         = get_biggest_mode (biggest_mode,
> @@ -416,7 +418,8 @@ static void
>  update_local_live_ranges (
>    vec_info *vinfo,
>    hash_map<basic_block, vec<stmt_point>> &program_points_per_bb,
> -  hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb)
> +  hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb,
> +  machine_mode *biggest_mode)
>  {
>    loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
>    if (!loop_vinfo)
> @@ -501,6 +504,8 @@ update_local_live_ranges (
>                            : get_store_value (gsi_stmt (si));
>               tree sel_type = build_nonstandard_integer_type (
>                 TYPE_PRECISION (TREE_TYPE (var)), 1);
> +             *biggest_mode
> +               = get_biggest_mode (*biggest_mode, TYPE_MODE (sel_type));
>               tree sel = build_decl (UNKNOWN_LOCATION, VAR_DECL,
>                                      get_identifier ("vect_perm"), sel_type);
>               pair &live_range = live_ranges->get_or_insert (sel, &existed_p);
> @@ -572,7 +577,7 @@ costs::preferred_new_lmul_p (const vector_costs *uncast_other) const
>
>    /* Update live ranges according to PHI.  */
>    update_local_live_ranges (other->m_vinfo, program_points_per_bb,
> -                           live_ranges_per_bb);
> +                           live_ranges_per_bb, &biggest_mode);
>
>    /* TODO: We calculate the maximum live vars base on current STMTS
>       sequence.  We can support live range shrink if it can give us
> diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
> new file mode 100644
> index 00000000000..4f019ccae6b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
> @@ -0,0 +1,25 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv32gcv -mabi=ilp32 -O3 -ftree-vectorize --param riscv-autovec-lmul=dynamic" } */
> +
> +int a, *b[9], c, d, e;
> +
> +static int
> +fn1 ()
> +{
> +  for (c = 6; c >= 0; c--)
> +    for (d = 0; d < 2; d++)
> +      {
> +        b[d * 2 + c] = 0;
> +        e = a > 1 ? : 0;
> +        if (e == 2)
> +          return 0;
> +      }
> +  return 0;
> +}
> +
> +int
> +main ()
> +{
> +  fn1 ();
> +  return 0;
> +}
> diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
> new file mode 100644
> index 00000000000..6fc8062f23b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
> @@ -0,0 +1,22 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv32gcv -mabi=ilp32 -Ofast -ftree-vectorize --param riscv-autovec-lmul=dynamic" } */
> +
> +typedef struct rtx_def *rtx;
> +struct replacement {
> +    rtx *where;
> +    rtx *subreg_loc;
> +    int mode;
> +};
> +static struct replacement replacements[150];
> +void move_replacements (rtx *x, rtx *y, int n_replacements)
> +{
> +  int i;
> +  for (i = 0; i < n_replacements; i++)
> +    if (replacements[i].subreg_loc == x)
> +      replacements[i].subreg_loc = y;
> +    else if (replacements[i].where == x)
> +      {
> +       replacements[i].where = y;
> +       replacements[i].subreg_loc = 0;
> +      }
> +}
> diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
> new file mode 100644
> index 00000000000..c1f698b9a68
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv32gcv -mabi=ilp32 -O2 -ftree-vectorize -flto -fno-use-linker-plugin -flto-partition=none --param riscv-autovec-lmul=dynamic" } */
> +
> +void (*foo[6][6]) (int);
> +void bar (hdR)
> +    int hdR;
> +{ }
> +void xxx ()
> +{
> +    unsigned int i, j;
> +    for (i = 0; i < 6; ++i)
> +       for (j = 0; j < 6; ++j)
> +            foo [i][j] = bar;
> +}
> --
> 2.36.3
>
  
Li, Pan2 Nov. 9, 2023, 2:49 a.m. UTC | #2
Committed, thanks Kito.

Pan

-----Original Message-----
From: Kito Cheng <kito.cheng@sifive.com> 
Sent: Thursday, November 9, 2023 10:43 AM
To: Juzhe-Zhong <juzhe.zhong@rivai.ai>
Cc: gcc-patches@gcc.gnu.org; kito.cheng@gmail.com; jeffreyalaw@gmail.com; rdapp.gcc@gmail.com
Subject: Re: [PATCH] RISC-V: Fix dynamic LMUL cost model ICE

LGTM, thanks :)

On Thu, Nov 9, 2023 at 10:39 AM Juzhe-Zhong <juzhe.zhong@rivai.ai> wrote:
>
> When trying to use dynamic LMUL to compile benchmark.
> Notice there is a bunch ICEs.
>
> This patch fixes those ICEs and append tests.
>
> gcc/ChangeLog:
>
>         * config/riscv/riscv-vector-costs.cc (costs::preferred_new_lmul_p): Fix ICE.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c: New test.
>         * gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c: New test.
>         * gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c: New test.
>
> ---
>  gcc/config/riscv/riscv-vector-costs.cc        | 11 +++++---
>  .../costmodel/riscv/rvv/dynamic-lmul-ice-1.c  | 25 +++++++++++++++++++
>  .../costmodel/riscv/rvv/dynamic-lmul-ice-2.c  | 22 ++++++++++++++++
>  .../costmodel/riscv/rvv/dynamic-lmul-ice-3.c  | 14 +++++++++++
>  4 files changed, 69 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
>  create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
>  create mode 100644 gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
>
> diff --git a/gcc/config/riscv/riscv-vector-costs.cc b/gcc/config/riscv/riscv-vector-costs.cc
> index af87388a1e4..8036c9c40d7 100644
> --- a/gcc/config/riscv/riscv-vector-costs.cc
> +++ b/gcc/config/riscv/riscv-vector-costs.cc
> @@ -231,7 +231,9 @@ compute_local_live_ranges (
>
>                      TODO: We may elide the cases that the unnecessary IMM in
>                      the future.  */
> -                 if (is_gimple_val (var) && !POINTER_TYPE_P (TREE_TYPE (var)))
> +                 if (poly_int_tree_p (var)
> +                     || (is_gimple_val (var)
> +                         && !POINTER_TYPE_P (TREE_TYPE (var))))
>                     {
>                       biggest_mode
>                         = get_biggest_mode (biggest_mode,
> @@ -416,7 +418,8 @@ static void
>  update_local_live_ranges (
>    vec_info *vinfo,
>    hash_map<basic_block, vec<stmt_point>> &program_points_per_bb,
> -  hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb)
> +  hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb,
> +  machine_mode *biggest_mode)
>  {
>    loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
>    if (!loop_vinfo)
> @@ -501,6 +504,8 @@ update_local_live_ranges (
>                            : get_store_value (gsi_stmt (si));
>               tree sel_type = build_nonstandard_integer_type (
>                 TYPE_PRECISION (TREE_TYPE (var)), 1);
> +             *biggest_mode
> +               = get_biggest_mode (*biggest_mode, TYPE_MODE (sel_type));
>               tree sel = build_decl (UNKNOWN_LOCATION, VAR_DECL,
>                                      get_identifier ("vect_perm"), sel_type);
>               pair &live_range = live_ranges->get_or_insert (sel, &existed_p);
> @@ -572,7 +577,7 @@ costs::preferred_new_lmul_p (const vector_costs *uncast_other) const
>
>    /* Update live ranges according to PHI.  */
>    update_local_live_ranges (other->m_vinfo, program_points_per_bb,
> -                           live_ranges_per_bb);
> +                           live_ranges_per_bb, &biggest_mode);
>
>    /* TODO: We calculate the maximum live vars base on current STMTS
>       sequence.  We can support live range shrink if it can give us
> diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
> new file mode 100644
> index 00000000000..4f019ccae6b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
> @@ -0,0 +1,25 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv32gcv -mabi=ilp32 -O3 -ftree-vectorize --param riscv-autovec-lmul=dynamic" } */
> +
> +int a, *b[9], c, d, e;
> +
> +static int
> +fn1 ()
> +{
> +  for (c = 6; c >= 0; c--)
> +    for (d = 0; d < 2; d++)
> +      {
> +        b[d * 2 + c] = 0;
> +        e = a > 1 ? : 0;
> +        if (e == 2)
> +          return 0;
> +      }
> +  return 0;
> +}
> +
> +int
> +main ()
> +{
> +  fn1 ();
> +  return 0;
> +}
> diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
> new file mode 100644
> index 00000000000..6fc8062f23b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
> @@ -0,0 +1,22 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv32gcv -mabi=ilp32 -Ofast -ftree-vectorize --param riscv-autovec-lmul=dynamic" } */
> +
> +typedef struct rtx_def *rtx;
> +struct replacement {
> +    rtx *where;
> +    rtx *subreg_loc;
> +    int mode;
> +};
> +static struct replacement replacements[150];
> +void move_replacements (rtx *x, rtx *y, int n_replacements)
> +{
> +  int i;
> +  for (i = 0; i < n_replacements; i++)
> +    if (replacements[i].subreg_loc == x)
> +      replacements[i].subreg_loc = y;
> +    else if (replacements[i].where == x)
> +      {
> +       replacements[i].where = y;
> +       replacements[i].subreg_loc = 0;
> +      }
> +}
> diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
> new file mode 100644
> index 00000000000..c1f698b9a68
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "-march=rv32gcv -mabi=ilp32 -O2 -ftree-vectorize -flto -fno-use-linker-plugin -flto-partition=none --param riscv-autovec-lmul=dynamic" } */
> +
> +void (*foo[6][6]) (int);
> +void bar (hdR)
> +    int hdR;
> +{ }
> +void xxx ()
> +{
> +    unsigned int i, j;
> +    for (i = 0; i < 6; ++i)
> +       for (j = 0; j < 6; ++j)
> +            foo [i][j] = bar;
> +}
> --
> 2.36.3
>
  

Patch

diff --git a/gcc/config/riscv/riscv-vector-costs.cc b/gcc/config/riscv/riscv-vector-costs.cc
index af87388a1e4..8036c9c40d7 100644
--- a/gcc/config/riscv/riscv-vector-costs.cc
+++ b/gcc/config/riscv/riscv-vector-costs.cc
@@ -231,7 +231,9 @@  compute_local_live_ranges (
 
 		     TODO: We may elide the cases that the unnecessary IMM in
 		     the future.  */
-		  if (is_gimple_val (var) && !POINTER_TYPE_P (TREE_TYPE (var)))
+		  if (poly_int_tree_p (var)
+		      || (is_gimple_val (var)
+			  && !POINTER_TYPE_P (TREE_TYPE (var))))
 		    {
 		      biggest_mode
 			= get_biggest_mode (biggest_mode,
@@ -416,7 +418,8 @@  static void
 update_local_live_ranges (
   vec_info *vinfo,
   hash_map<basic_block, vec<stmt_point>> &program_points_per_bb,
-  hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb)
+  hash_map<basic_block, hash_map<tree, pair>> &live_ranges_per_bb,
+  machine_mode *biggest_mode)
 {
   loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
   if (!loop_vinfo)
@@ -501,6 +504,8 @@  update_local_live_ranges (
 			   : get_store_value (gsi_stmt (si));
 	      tree sel_type = build_nonstandard_integer_type (
 		TYPE_PRECISION (TREE_TYPE (var)), 1);
+	      *biggest_mode
+		= get_biggest_mode (*biggest_mode, TYPE_MODE (sel_type));
 	      tree sel = build_decl (UNKNOWN_LOCATION, VAR_DECL,
 				     get_identifier ("vect_perm"), sel_type);
 	      pair &live_range = live_ranges->get_or_insert (sel, &existed_p);
@@ -572,7 +577,7 @@  costs::preferred_new_lmul_p (const vector_costs *uncast_other) const
 
   /* Update live ranges according to PHI.  */
   update_local_live_ranges (other->m_vinfo, program_points_per_bb,
-			    live_ranges_per_bb);
+			    live_ranges_per_bb, &biggest_mode);
 
   /* TODO: We calculate the maximum live vars base on current STMTS
      sequence.  We can support live range shrink if it can give us
diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
new file mode 100644
index 00000000000..4f019ccae6b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-1.c
@@ -0,0 +1,25 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv32gcv -mabi=ilp32 -O3 -ftree-vectorize --param riscv-autovec-lmul=dynamic" } */
+
+int a, *b[9], c, d, e; 
+
+static int
+fn1 ()
+{
+  for (c = 6; c >= 0; c--)
+    for (d = 0; d < 2; d++)
+      {
+        b[d * 2 + c] = 0;
+        e = a > 1 ? : 0;
+        if (e == 2) 
+          return 0;
+      }
+  return 0;
+}
+
+int
+main ()
+{
+  fn1 ();
+  return 0; 
+}
diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
new file mode 100644
index 00000000000..6fc8062f23b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-2.c
@@ -0,0 +1,22 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv32gcv -mabi=ilp32 -Ofast -ftree-vectorize --param riscv-autovec-lmul=dynamic" } */
+
+typedef struct rtx_def *rtx;
+struct replacement {
+    rtx *where;
+    rtx *subreg_loc;
+    int mode;
+};
+static struct replacement replacements[150];
+void move_replacements (rtx *x, rtx *y, int n_replacements)
+{
+  int i;
+  for (i = 0; i < n_replacements; i++)
+    if (replacements[i].subreg_loc == x)
+      replacements[i].subreg_loc = y;
+    else if (replacements[i].where == x) 
+      {
+	replacements[i].where = y;
+	replacements[i].subreg_loc = 0;
+      }
+}
diff --git a/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
new file mode 100644
index 00000000000..c1f698b9a68
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/costmodel/riscv/rvv/dynamic-lmul-ice-3.c
@@ -0,0 +1,14 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=rv32gcv -mabi=ilp32 -O2 -ftree-vectorize -flto -fno-use-linker-plugin -flto-partition=none --param riscv-autovec-lmul=dynamic" } */
+
+void (*foo[6][6]) (int);
+void bar (hdR)
+    int hdR;
+{ }
+void xxx ()
+{
+    unsigned int i, j;
+    for (i = 0; i < 6; ++i)
+	for (j = 0; j < 6; ++j)
+            foo [i][j] = bar;
+}