Skip to content

Commit c7954ca

Browse files
authored
Recommit "[RISCV] Refine cost on Min/Max reduction (#79402)" (#86480)
This is recommitted as the test and fix for llvm.vector.reduce.fmaximum/fminimum are covered in #80553 and #80697
1 parent c4df57d commit c7954ca

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
@@ -1058,13 +1058,42 @@ RISCVTTIImpl::getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
10581058
}
10591059

10601060
// IR Reduction is composed by two vmv and one rvv reduction instruction.
1061-
InstructionCost BaseCost = 2;
1062-
1063-
if (CostKind == TTI::TCK_CodeSize)
1064-
return (LT.first - 1) + BaseCost;
1065-
1066-
unsigned VL = getEstimatedVLFor(Ty);
1067-
return (LT.first - 1) + BaseCost + Log2_32_Ceil(VL);
1061+
unsigned SplitOp;
1062+
SmallVector<unsigned, 3> Opcodes;
1063+
switch (IID) {
1064+
default:
1065+
llvm_unreachable("Unsupported intrinsic");
1066+
case Intrinsic::smax:
1067+
SplitOp = RISCV::VMAX_VV;
1068+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMAX_VS, RISCV::VMV_X_S};
1069+
break;
1070+
case Intrinsic::smin:
1071+
SplitOp = RISCV::VMIN_VV;
1072+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMIN_VS, RISCV::VMV_X_S};
1073+
break;
1074+
case Intrinsic::umax:
1075+
SplitOp = RISCV::VMAXU_VV;
1076+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMAXU_VS, RISCV::VMV_X_S};
1077+
break;
1078+
case Intrinsic::umin:
1079+
SplitOp = RISCV::VMINU_VV;
1080+
Opcodes = {RISCV::VMV_S_X, RISCV::VREDMINU_VS, RISCV::VMV_X_S};
1081+
break;
1082+
case Intrinsic::maxnum:
1083+
SplitOp = RISCV::VFMAX_VV;
1084+
Opcodes = {RISCV::VFMV_S_F, RISCV::VFREDMAX_VS, RISCV::VFMV_F_S};
1085+
break;
1086+
case Intrinsic::minnum:
1087+
SplitOp = RISCV::VFMIN_VV;
1088+
Opcodes = {RISCV::VFMV_S_F, RISCV::VFREDMIN_VS, RISCV::VFMV_F_S};
1089+
break;
1090+
}
1091+
// Add a cost for data larger than LMUL8
1092+
InstructionCost SplitCost =
1093+
(LT.first > 1) ? (LT.first - 1) *
1094+
getRISCVInstructionCost(SplitOp, LT.second, CostKind)
1095+
: 0;
1096+
return SplitCost + getRISCVInstructionCost(Opcodes, LT.second, CostKind);
10681097
}
10691098

10701099
InstructionCost

0 commit comments

Comments
 (0)