https://gcc.gnu.org/g:32dc4bc481a0f3b94bf2129e908e15917f75b45f
commit r17-3821-g32dc4bc481a0f3b94bf2129e908e15917f75b45f Author: Wang Yaduo <[email protected]> Date: Tue Sep 1 09:32:27 2026 -0600 [PATCH v2] RISC-V: Fix scalar floating-point costs Scalar IL costing receives a null vectype, causing floating-point statements to use the integer cost. Recover the scalar type and restrict LMUL adjustments to vector types. gcc/ChangeLog: * config/riscv/riscv-vector-costs.cc (costs::adjust_stmt_cost): Restrict LMUL scaling to vector types. (costs::add_stmt_cost): Recover the scalar type for scalar costing. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/autovec/scalar-fp-cost.c: New test. Diff: --- gcc/config/riscv/riscv-vector-costs.cc | 11 ++++++++++- gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gcc/config/riscv/riscv-vector-costs.cc b/gcc/config/riscv/riscv-vector-costs.cc index bfb8a0ca63b0..dfb80bee6791 100644 --- a/gcc/config/riscv/riscv-vector-costs.cc +++ b/gcc/config/riscv/riscv-vector-costs.cc @@ -1569,7 +1569,7 @@ 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 (vectype) + if (VECTOR_TYPE_P (vectype)) { unsigned lmul_factor = get_lmul_cost_scaling (TYPE_MODE (vectype)); if (lmul_factor > 1) @@ -1584,6 +1584,15 @@ costs::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) { + /* VECTYPE is null when costing scalar IL. Recover the scalar type to + distinguish floating-point statements from integer statements. */ + if (m_cost_type == SCALAR_COST && stmt_info) + { + gcc_assert (!vectype); + if (tree lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info))) + vectype = TREE_TYPE (lhs); + } + int stmt_cost = targetm.vectorize.builtin_vectorization_cost (kind, vectype, misalign); diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c new file mode 100644 index 000000000000..d729dd69d02f --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/scalar-fp-cost.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -march=rv64gcv -mabi=lp64d -mtune=xt-c9501fdvt -fdump-tree-vect-details" } */ + +void +foo (float *restrict dst, const float *restrict x, + const float *restrict y, long n) +{ + for (long i = 0; i < n; ++i) + dst[i] = x[i] + y[i]; +} + +/* { dg-final { scan-tree-dump {_[0-9]+ \+ _[0-9]+ .*scalar_stmt costs 2 in body} "vect" } } */
