https://gcc.gnu.org/g:fa20d1603db32449ffe51e54849f6d67eb7c7965
commit r17-5915-gfa20d1603db32449ffe51e54849f6d67eb7c7965 Author: Zhongyao Chen <[email protected]> Date: Tue Nov 3 20:29:18 2026 +0800 RISC-V: Support per-LMUL vector permute cost Turn the scalar permute_cost in common_vector_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. In riscv_builtin_vectorization_cost, return the baseline cost for LMUL 1. In costs::adjust_stmt_cost, look up the per-LMUL permute cost for vec_perm. 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. 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): Look up per-LMUL permute_cost for vec_perm. * config/riscv/riscv.cc (rvv_vls_vector_cost, rvv_vla_vector_cost) (xt_c9501_vls_vector_cost): Update permute_cost. (riscv_builtin_vectorization_cost): Return permute_cost[LMUL_1]. Signed-off-by: Zhongyao Chen <[email protected]> Diff: --- gcc/config/riscv/riscv-protos.h | 4 ++-- gcc/config/riscv/riscv-vector-costs.cc | 19 +++++++++++++++---- gcc/config/riscv/riscv.cc | 8 ++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h index 8641eec98a2e..78d6c5ca0d65 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 924a0330103a..70194ec70c32 100644 --- a/gcc/config/riscv/riscv-vector-costs.cc +++ b/gcc/config/riscv/riscv-vector-costs.cc @@ -1523,12 +1523,23 @@ 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. */ + which affects performance regardless of loop structure. + Permutes use dedicated per-LMUL cost tables instead of linear + scaling. */ if (VECTOR_TYPE_P (vectype)) { - unsigned lmul_factor = get_lmul_cost_scaling (TYPE_MODE (vectype)); - if (lmul_factor > 1) - stmt_cost *= lmul_factor; + machine_mode mode = TYPE_MODE (vectype); + if (kind == vec_perm) + { + enum vlmul_type vlmul + = is_frac_vlmul_p (mode) ? LMUL_1 : get_vlmul (mode); + if (riscv_vla_mode_p (mode)) + stmt_cost = costs->vla->permute_cost[vlmul]; + else + stmt_cost = costs->vls->permute_cost[vlmul]; + } + else + stmt_cost *= get_lmul_cost_scaling (mode); } return stmt_cost; diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index c827aee918ab..cd4b2bb76c2d 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 */ @@ -14639,7 +14639,7 @@ 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 common_costs->permute_cost[LMUL_1]; case vec_promote_demote: return fp ? common_costs->fp_stmt_cost : common_costs->int_stmt_cost;
