Skip to content

Commit 435da4e

Browse files
authored
[RISCV] Promote SETCC and VP_SETCC of f16 vectors when only have zvfhmin (#66866)
This patch implements the promotion of fp16 vectors SETCC and VP_SETCC when we only have zvfhmin but no zvfh.
1 parent e794b66 commit 435da4e

File tree

6 files changed

+4837
-1568
lines changed

6 files changed

+4837
-1568
lines changed

llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ class VectorLegalizer {
173173
/// result is truncated back to the original scalar type.
174174
void PromoteReduction(SDNode *Node, SmallVectorImpl<SDValue> &Results);
175175

176+
/// Implements vector setcc operation promotion.
177+
///
178+
/// All vector operands are promoted to a vector type with larger element
179+
/// type.
180+
void PromoteSETCC(SDNode *Node, SmallVectorImpl<SDValue> &Results);
181+
176182
public:
177183
VectorLegalizer(SelectionDAG& dag) :
178184
DAG(dag), TLI(dag.getTargetLoweringInfo()) {}
@@ -603,6 +609,31 @@ void VectorLegalizer::PromoteReduction(SDNode *Node,
603609
Results.push_back(Res);
604610
}
605611

612+
void VectorLegalizer::PromoteSETCC(SDNode *Node,
613+
SmallVectorImpl<SDValue> &Results) {
614+
MVT VecVT = Node->getOperand(0).getSimpleValueType();
615+
MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
616+
617+
unsigned ExtOp = VecVT.isFloatingPoint() ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
618+
619+
SDLoc DL(Node);
620+
SmallVector<SDValue, 5> Operands(Node->getNumOperands());
621+
622+
Operands[0] = DAG.getNode(ExtOp, DL, NewVecVT, Node->getOperand(0));
623+
Operands[1] = DAG.getNode(ExtOp, DL, NewVecVT, Node->getOperand(1));
624+
Operands[2] = Node->getOperand(2);
625+
626+
if (Node->getOpcode() == ISD::VP_SETCC) {
627+
Operands[3] = Node->getOperand(3); // mask
628+
Operands[4] = Node->getOperand(4); // evl
629+
}
630+
631+
SDValue Res = DAG.getNode(Node->getOpcode(), DL, Node->getSimpleValueType(0),
632+
Operands, Node->getFlags());
633+
634+
Results.push_back(Res);
635+
}
636+
606637
void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
607638
// For a few operations there is a specific concept for promotion based on
608639
// the operand's type.
@@ -638,6 +669,11 @@ void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
638669
// Promote the operation by extending the operand.
639670
PromoteReduction(Node, Results);
640671
return;
672+
case ISD::VP_SETCC:
673+
case ISD::SETCC:
674+
// Promote the operation by extending the operand.
675+
PromoteSETCC(Node, Results);
676+
return;
641677
case ISD::FP_ROUND:
642678
case ISD::FP_EXTEND:
643679
// These operations are used to do promotion so they can't be promoted

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,11 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
822822

823823
// TODO: support more ops.
824824
static const unsigned ZvfhminPromoteOps[] = {
825-
ISD::FMINNUM, ISD::FMAXNUM, ISD::FADD, ISD::FSUB,
826-
ISD::FMUL, ISD::FMA, ISD::FDIV, ISD::FSQRT,
827-
ISD::FABS, ISD::FNEG, ISD::FCOPYSIGN, ISD::FCEIL,
828-
ISD::FFLOOR, ISD::FROUND, ISD::FROUNDEVEN, ISD::FRINT,
829-
ISD::FNEARBYINT, ISD::IS_FPCLASS, ISD::SPLAT_VECTOR};
825+
ISD::FMINNUM, ISD::FMAXNUM, ISD::FADD, ISD::FSUB,
826+
ISD::FMUL, ISD::FMA, ISD::FDIV, ISD::FSQRT,
827+
ISD::FABS, ISD::FNEG, ISD::FCOPYSIGN, ISD::FCEIL,
828+
ISD::FFLOOR, ISD::FROUND, ISD::FROUNDEVEN, ISD::FRINT,
829+
ISD::FNEARBYINT, ISD::IS_FPCLASS, ISD::SPLAT_VECTOR, ISD::SETCC};
830830

831831
// TODO: support more vp ops.
832832
static const unsigned ZvfhminPromoteVPOps[] = {
@@ -837,7 +837,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
837837
ISD::VP_FMINNUM, ISD::VP_FMAXNUM, ISD::VP_FCEIL,
838838
ISD::VP_FFLOOR, ISD::VP_FROUND, ISD::VP_FROUNDEVEN,
839839
ISD::VP_FCOPYSIGN, ISD::VP_FROUNDTOZERO, ISD::VP_FRINT,
840-
ISD::VP_FNEARBYINT};
840+
ISD::VP_FNEARBYINT, ISD::VP_SETCC};
841841

842842
// Sets common operation actions on RVV floating-point vector types.
843843
const auto SetCommonVFPActions = [&](MVT VT) {
@@ -5392,6 +5392,11 @@ static SDValue SplitVPOp(SDValue Op, SelectionDAG &DAG) {
53925392
DAG.SplitEVL(Op.getOperand(j), Op.getValueType(), DL);
53935393
continue;
53945394
}
5395+
if (!Op.getOperand(j).getValueType().isVector()) {
5396+
LoOperands[j] = Op.getOperand(j);
5397+
HiOperands[j] = Op.getOperand(j);
5398+
continue;
5399+
}
53955400
std::tie(LoOperands[j], HiOperands[j]) =
53965401
DAG.SplitVector(Op.getOperand(j), DL);
53975402
}
@@ -6079,6 +6084,11 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
60796084
return DAG.getSetCC(DL, VT, RHS, LHS, CCVal);
60806085
}
60816086

6087+
if (Op.getOperand(0).getSimpleValueType() == MVT::nxv32f16 &&
6088+
(Subtarget.hasVInstructionsF16Minimal() &&
6089+
!Subtarget.hasVInstructionsF16()))
6090+
return SplitVectorOp(Op, DAG);
6091+
60826092
return lowerFixedLengthVectorSetccToRVV(Op, DAG);
60836093
}
60846094
case ISD::ADD:
@@ -6246,6 +6256,10 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
62466256
case ISD::VP_FP_TO_UINT:
62476257
return lowerVPFPIntConvOp(Op, DAG);
62486258
case ISD::VP_SETCC:
6259+
if (Op.getOperand(0).getSimpleValueType() == MVT::nxv32f16 &&
6260+
(Subtarget.hasVInstructionsF16Minimal() &&
6261+
!Subtarget.hasVInstructionsF16()))
6262+
return SplitVPOp(Op, DAG);
62496263
if (Op.getOperand(0).getSimpleValueType().getVectorElementType() == MVT::i1)
62506264
return lowerVPSetCCMaskOp(Op, DAG);
62516265
[[fallthrough]];

0 commit comments

Comments
 (0)