[llvm] [RISCV] Fix the cost of `llvm.vector.reduce.and` (PR #119160)
Shao-Ce SUN via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 8 00:44:47 PST 2025
================
@@ -1536,15 +1536,31 @@ RISCVTTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Ty);
Type *ElementTy = Ty->getElementType();
if (ElementTy->isIntegerTy(1)) {
+ // Example sequences:
+ // vfirst.m a0, v0
+ // seqz a0, a0
+ if (LT.second == MVT::v1i1)
+ return getRISCVInstructionCost(RISCV::VFIRST_M, LT.second, CostKind) +
+ getCmpSelInstrCost(Instruction::ICmp, ElementTy, ElementTy,
+ CmpInst::ICMP_EQ, CostKind);
+
if (ISD == ISD::AND) {
// Example sequences:
- // vsetvli a0, zero, e8, mf8, ta, ma
// vmand.mm v8, v9, v8 ; needed every time type is split
// vmnot.m v8, v0
// vcpop.m a0, v8
// seqz a0, a0
- return LT.first * getRISCVInstructionCost(RISCV::VMNAND_MM, LT.second,
- CostKind) +
+
+ // Scalable VT: In nxv256i1 and larger vector elements,
+ // Fixed VT: If getFixedSizeInBits() >= (4 * getRealMinVLen()),
+ // the VMAND_MM instructions have started to be added.
+ InstructionCost NumOfVMAND = 0;
+ if (LT.second.isScalableVector() ||
+ LT.second.getFixedSizeInBits() == ST->getRealMinVLen())
+ NumOfVMAND = (LT.first > 2) ? (LT.first - 2) : 0;
+ return NumOfVMAND *
+ getRISCVInstructionCost(RISCV::VMAND_MM, LT.second, CostKind) +
+ getRISCVInstructionCost(RISCV::VMNAND_MM, LT.second, CostKind) +
getRISCVInstructionCost(RISCV::VCPOP_M, LT.second, CostKind) +
----------------
sunshaoce wrote:
This indeed makes the logic much clearer. Thank you!
https://github.com/llvm/llvm-project/pull/119160
More information about the llvm-commits
mailing list