RISC-V: Avoid vector adjustments for scalar costing
Fixes incorrect cost calculations for scalar operations on RISC-V by preventing vectorization cost adjustments.
A bug in the RISC-V backend caused scalar operations to incur incorrect cost adjustments intended for vector operations. This occurred when recovering scalar types from vector-typed expressions, leading to misapplied scaling factors and suboptimal instruction selection. The fix ensures that vector-specific cost adjustments are skipped when computing costs for scalar code, preventing issues like the expansion of a constructor through large stack temporaries and the loss of intended vector instructions.
In Details
This commit modifies costs::add_stmt_cost in riscv-vector-costs.cc to prevent vector cost adjustments when m_cost_type == SCALAR_COST. Previously, recovering a type from a vector-typed LHS (e.g., vector(512) long int) for scalar cost computation would incorrectly trigger M8 scaling. This led to an inflated scalar cost, making SLP alternatives using 512-byte vectors appear profitable, causing an excessive 4096-byte stack temporary and loss of vslideup.vx instructions.
- RISC-V
- An open-standard instruction set architecture (ISA) based on Reduced Instruction Set Computer principles, increasingly used in embedded systems, servers, and high-performance computing.
- scalar operation
- An operation that processes a single data element at a time.
- vector operation
- An operation that processes multiple data elements simultaneously using specialized vector registers and instructions.
- SLP
- Superword-Level Parallelism. An optimization technique that identifies and vectorizes sequences of independent scalar operations that operate on different data elements.