[llvm] [RISCV][CostModel] Estimate cost of Extract/InsertElement with non-constant index when vector instructions are not available (PR #67334)

Sergey Kachkov via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 26 06:08:44 PDT 2023


================
@@ -1456,6 +1456,24 @@ InstructionCost RISCVTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
       Opcode != Instruction::InsertElement)
     return BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1);
 
+  // Extract/InsertElement with non-constant index is very costly without
+  // V instructions; estimate cost of loads/stores sequence via the stack:
+  // ExtractElement cost: store vector to stack, load scalar;
+  // InsertElement cost: store vector to stack, store scalar, load vector.
+  if (Index == -1U && !ST->hasVInstructions()) {
+    auto *VecTy = cast<VectorType>(Val);
+    auto *ElemTy = VecTy->getElementType();
+    auto NumElems = VecTy->getElementCount().getKnownMinValue();
----------------
skachkov-sc wrote:

Actually, I've realized that scalable vectors types can't be lowered with scalars (backend fails with "Don't know how to legalize this scalable vector type" error), and currently the reported cost of these operations is Invalid. So probably it's better to return Invalid cost instead of numeric value to show that it can't be lowered (added some test cases with vscale vectors)

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


More information about the llvm-commits mailing list