[llvm] [SLP] More accurately cost RISCV scalar splats (PR #213104)

Ryan Buchner via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 30 13:50:01 PDT 2026


================
@@ -2561,6 +2572,12 @@ InstructionCost RISCVTTIImpl::getVectorInstrCost(
     return BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1,
                                      VIC);
 
+  // Scalar splat operand can be folded for vector ops that support splatting
+  // the scalar operand, so the explicit insertelement is free in this context.
+  if (Opcode == Instruction::InsertElement &&
+      VIC == TTI::VectorInstrContext::SplatOp)
----------------
bababuck wrote:

No. The semantics of the `SplatOp` context hint is that it should only be used for costing a splat operand sequence that feeds into an instruction that `canSplatOperand`. So we only use `SplatOp` in cases such as:
```
%tmp0 = insertelement <4 x i32> poison, %scalar, 0
%tmp1 = shufflevector <4 x i32> %tmp0, <4 x i32> poison, <4 x i32> {0, 0, 0, 0}
%tmp2 = add <4 x i32> %vector, %tmp1
```
These cases emit a single instruction such as `vadd.vx` in RISCV.

In a follow up MR, I plan to add a `VectorInstContext` hint for `ScalarSplat` for cases where:
```
%tmp0 = insertelement <4 x i32> poison, %scalar, 0
%tmp1 = shufflevector <4 x i32> %tmp0, <4 x i32> poison, <4 x i32> {0, 0, 0, 0}
%tmp2 = non-splattable-op <4 x i32> %vector, %tmp1
```
in which case we would generate:
`vmv.v.x` - ` vNSP.vv` sequence

For the `ScalarSplat` hint in RISCV we would still cost the `insert` but not the `shuffle`.

https://github.com/llvm/llvm-project/pull/213104


More information about the llvm-commits mailing list