Skip to content

Commit b88bc26

Browse files
committed
[RISCV] Refine cost on Min/Max reduction
This patch is split off from llvm#77342, and follows llvm#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 84be954 commit b88bc26

File tree

5 files changed

+266
-237
lines changed

5 files changed

+266
-237
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -942,13 +942,42 @@ RISCVTTIImpl::getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
942942
return (LT.first - 1) + 3;
943943

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

954983
InstructionCost

0 commit comments

Comments
 (0)