Turn the permute_cost into an array indexed by LMUL (1, 2, 4, 8)
to model non-linear permute scaling, defaulting to quadratic scaling
({1, 4, 16, 64}) for generic cores.

Regtested on rv64gcv, showed regressions in 4 files:
  - gcc.target/riscv/rvv/autovec/partial/slp-{1,3,5}.c: Prefers vlseg8
    because generic segment cost is too cheap (1).
  - gcc.target/riscv/rvv/autovec/vls/bswap16-0.c: bswap16 is costed as
    vec_perm, so quadratic scaling avoids large LMUL.

I leave testcases untouched for now.
  - For slp-{1,3,5}.c, will introduce a segment cost table in a separate
    patch.
  - For bswap16-0.c, currently we can't tell if it's a cheaper perm.
    Robin is working on vect; once that allows us to do fine-grained
    permute costing, this can be refined into dedicated cost tables for
    gather/slide/compress.  After that this can be fixed.

To Bohan: please check if the updated xt_c9501_vls_vector_cost numbers
look reasonable to you.

gcc/ChangeLog:

        * config/riscv/riscv-protos.h (struct common_vector_cost): Change
        permute_cost to array.
        * config/riscv/riscv-vector-costs.cc (costs::adjust_stmt_cost): Skip
        linear LMUL scaling for vec_perm.
        * config/riscv/riscv.cc (rvv_vls_vector_cost): Update permute_cost.
        (rvv_vla_vector_cost): Likewise.
        (xt_c9501_vls_vector_cost): Likewise.
        (riscv_builtin_vectorization_cost): Query permute_cost by LMUL.

Signed-off-by: Zhongyao Chen <[email protected]>
---
 gcc/config/riscv/riscv-protos.h        |  4 ++--
 gcc/config/riscv/riscv-vector-costs.cc |  6 ++++--
 gcc/config/riscv/riscv.cc              | 10 ++++++----
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
index c0fb2839780..e24b474d591 100644
--- a/gcc/config/riscv/riscv-protos.h
+++ b/gcc/config/riscv/riscv-protos.h
@@ -270,8 +270,8 @@ struct common_vector_cost
   /* Cost of a scalar-to-vector operation.  */
   const int scalar_to_vec_cost;
 
-  /* Cost of a permute operation.  */
-  const int permute_cost;
+  /* Cost of a permute operation for LMUL 1, 2, 4, 8.  */
+  const int permute_cost[4];
 
   /* Cost of an aligned vector load.  */
   const int align_load_cost;
diff --git a/gcc/config/riscv/riscv-vector-costs.cc 
b/gcc/config/riscv/riscv-vector-costs.cc
index 924a0330103..9eeeaff05c0 100644
--- a/gcc/config/riscv/riscv-vector-costs.cc
+++ b/gcc/config/riscv/riscv-vector-costs.cc
@@ -1523,8 +1523,10 @@ costs::adjust_stmt_cost (enum vect_cost_for_stmt kind, 
loop_vec_info loop,
 
   /* Apply LMUL cost scaling uniformly to all vector operations.
      Larger LMUL values have higher latency and register pressure,
-     which affects performance regardless of loop structure.  */
-  if (VECTOR_TYPE_P (vectype))
+     which affects performance regardless of loop structure.
+     Permutes have already looked up their per-LMUL cost table in
+     builtin_vectorization_cost.  */
+  if (VECTOR_TYPE_P (vectype) && kind != vec_perm)
     {
       unsigned lmul_factor = get_lmul_cost_scaling (TYPE_MODE (vectype));
       if (lmul_factor > 1)
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index bed695ccae4..17fcdb6f764 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -406,7 +406,7 @@ static const common_vector_cost rvv_vls_vector_cost = {
   1, /* segment_permute (8) */
   1, /* vec_to_scalar_cost  */
   1, /* scalar_to_vec_cost  */
-  1, /* permute_cost  */
+  {1, 4, 16, 64}, /* permute_cost.  */
   1, /* align_load_cost  */
   1, /* align_store_cost  */
   2, /* unalign_load_cost  */
@@ -439,7 +439,7 @@ static const scalable_vector_cost rvv_vla_vector_cost = {
     1, /* segment_permute (8) */
     1, /* vec_to_scalar_cost  */
     1, /* scalar_to_vec_cost  */
-    1, /* permute_cost  */
+    {1, 4, 16, 64}, /* permute_cost.  */
     1, /* align_load_cost  */
     1, /* align_store_cost  */
     2, /* unalign_load_cost  */
@@ -751,7 +751,7 @@ static const common_vector_cost xt_c9501_vls_vector_cost = {
   12, /* segment_permute (8) */
   2, /* vec_to_scalar_cost  */
   2, /* scalar_to_vec_cost  */
-  4, /* permute_cost  */
+  {4, 16, 64, 256}, /* permute_cost.  */
   6, /* align_load_cost  */
   1, /* align_store_cost  */
   8, /* unalign_load_cost  */
@@ -14557,7 +14557,9 @@ riscv_builtin_vectorization_cost (enum 
vect_cost_for_stmt type_of_cost,
       return costs->cond_not_taken_branch_cost;
 
     case vec_perm:
-      return common_costs->permute_cost;
+      return is_frac_vlmul_p (TYPE_MODE (vectype))
+       ? common_costs->permute_cost[LMUL_1]
+       : common_costs->permute_cost[get_vlmul (TYPE_MODE (vectype))];
 
     case vec_promote_demote:
       return fp ? common_costs->fp_stmt_cost : common_costs->int_stmt_cost;
-- 
2.43.0


Reply via email to