Skip to content

Commit 2800448

Browse files
authored
[RISCV] Refine cost on Min/Max reduction (#79402)
This patch is split off from #77342, and follows #79103 - Correct for CodeSize cost that 1 instruction is not included. 3 is from {VMV.S, ReductionOp, VMV.X} - Add SplitCost which chains a series of VMAX/VMIN/... which scales with LMUL. - Use MVT to estimate VL.
1 parent cb46c61 commit 2800448

File tree

5 files changed

+266
-237
lines changed

5 files changed

+266
-237
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

+36-7
Original file line numberDiff line numberDiff line change
@@ -947,13 +947,42 @@ RISCVTTIImpl::getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
947947
}
948948

949949
// IR Reduction is composed by two vmv and one rvv reduction instruction.
950-
InstructionCost BaseCost = 2;
951-
952-
if (CostKind == TTI::TCK_CodeSize)
953-
return (LT.first - 1) + BaseCost;
954-
955-
unsigned VL = getEstimatedVLFor(Ty);
956-
return (LT.first - 1) + BaseCost + Log2_32_Ceil(VL);
950+
unsigned SplitOp;
951+
SmallVector<unsigned, 3> Opcodes;
952+
switch (IID) {
953+
default:
954+
llvm_unreachable("Unsupported intrinsic");
955+
case Intrinsic::smax:
956+
SplitOp = RISCV::VMAX_VV;
957+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMAX_VS, RISCV::VMV_X_S};
958+
break;
959+
case Intrinsic::smin:
960+
SplitOp = RISCV::VMIN_VV;
961+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMIN_VS, RISCV::VMV_X_S};
962+
break;
963+
case Intrinsic::umax:
964+
SplitOp = RISCV::VMAXU_VV;
965+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMAXU_VS, RISCV::VMV_X_S};
966+
break;
967+
case Intrinsic::umin:
968+
SplitOp = RISCV::VMINU_VV;
969+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMINU_VS, RISCV::VMV_X_S};
970+
break;
971+
case Intrinsic::maxnum:
972+
SplitOp = RISCV::VFMAX_VV;
973+
Opcodes = {RISCV::VFMV_S_F, RISCV::VFREDMAX_VS, RISCV::VFMV_F_S};
974+
break;
975+
case Intrinsic::minnum:
976+
SplitOp = RISCV::VFMIN_VV;
977+
Opcodes = {RISCV::VFMV_S_F, RISCV::VFREDMIN_VS, RISCV::VFMV_F_S};
978+
break;
979+
}
980+
// Add a cost for data larger than LMUL8
981+
InstructionCost SplitCost =
982+
(LT.first > 1) ? (LT.first - 1) *
983+
getRISCVInstructionCost(SplitOp, LT.second, CostKind)
984+
: 0;
985+
return SplitCost + getRISCVInstructionCost(Opcodes, LT.second, CostKind);
957986
}
958987

959988
InstructionCost

0 commit comments

Comments
 (0)