Skip to content

[RISCV] Support Strict FP arithmetic Op when only have Zvfhmin #68867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class VectorLegalizer {
/// type.
void PromoteSETCC(SDNode *Node, SmallVectorImpl<SDValue> &Results);

void PromoteSTRICT(SDNode *Node, SmallVectorImpl<SDValue> &Results);

public:
VectorLegalizer(SelectionDAG& dag) :
DAG(dag), TLI(dag.getTargetLoweringInfo()) {}
Expand Down Expand Up @@ -636,6 +638,47 @@ void VectorLegalizer::PromoteSETCC(SDNode *Node,
Results.push_back(Res);
}

void VectorLegalizer::PromoteSTRICT(SDNode *Node,
SmallVectorImpl<SDValue> &Results) {
MVT VecVT = Node->getOperand(1).getSimpleValueType();
MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);

assert(VecVT.isFloatingPoint());

SDLoc DL(Node);
SmallVector<SDValue, 5> Operands(Node->getNumOperands());
SmallVector<SDValue, 2> Chains;

for (unsigned j = 1; j != Node->getNumOperands(); ++j)
if (Node->getOperand(j).getValueType().isVector() &&
!(ISD::isVPOpcode(Node->getOpcode()) &&
ISD::getVPMaskIdx(Node->getOpcode()) == j)) // Skip mask operand.
{
// promote the vector operand.
SDValue Ext =
DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {NewVecVT, MVT::Other},
{Node->getOperand(0), Node->getOperand(j)});
Operands[j] = Ext.getValue(0);
Chains.push_back(Ext.getValue(1));
} else
Operands[j] = Node->getOperand(j); // Skip no vector operand.

SDVTList VTs = DAG.getVTList(NewVecVT, Node->getValueType(1));

Operands[0] = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);

SDValue Res =
DAG.getNode(Node->getOpcode(), DL, VTs, Operands, Node->getFlags());

SDValue Round =
DAG.getNode(ISD::STRICT_FP_ROUND, DL, {VecVT, MVT::Other},
{Res.getValue(1), Res.getValue(0),
DAG.getIntPtrConstant(0, DL, /*isTarget=*/true)});

Results.push_back(Round.getValue(0));
Results.push_back(Round.getValue(1));
}

void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
// For a few operations there is a specific concept for promotion based on
// the operand's type.
Expand Down Expand Up @@ -676,6 +719,14 @@ void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
// Promote the operation by extending the operand.
PromoteSETCC(Node, Results);
return;
case ISD::STRICT_FADD:
case ISD::STRICT_FSUB:
case ISD::STRICT_FMUL:
case ISD::STRICT_FDIV:
case ISD::STRICT_FSQRT:
case ISD::STRICT_FMA:
PromoteSTRICT(Node, Results);
return;
case ISD::FP_ROUND:
case ISD::FP_EXTEND:
// These operations are used to do promotion so they can't be promoted
Expand Down
52 changes: 46 additions & 6 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,12 +828,13 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,

// TODO: support more ops.
static const unsigned ZvfhminPromoteOps[] = {
ISD::FMINNUM, ISD::FMAXNUM, ISD::FADD, ISD::FSUB,
ISD::FMUL, ISD::FMA, ISD::FDIV, ISD::FSQRT,
ISD::FABS, ISD::FNEG, ISD::FCOPYSIGN, ISD::FCEIL,
ISD::FFLOOR, ISD::FROUND, ISD::FROUNDEVEN, ISD::FRINT,
ISD::FNEARBYINT, ISD::IS_FPCLASS, ISD::SETCC, ISD::FMAXIMUM,
ISD::FMINIMUM};
ISD::FMINNUM, ISD::FMAXNUM, ISD::FADD, ISD::FSUB,
ISD::FMUL, ISD::FMA, ISD::FDIV, ISD::FSQRT,
ISD::FABS, ISD::FNEG, ISD::FCOPYSIGN, ISD::FCEIL,
ISD::FFLOOR, ISD::FROUND, ISD::FROUNDEVEN, ISD::FRINT,
ISD::FNEARBYINT, ISD::IS_FPCLASS, ISD::SETCC, ISD::FMAXIMUM,
ISD::FMINIMUM, ISD::STRICT_FADD, ISD::STRICT_FSUB, ISD::STRICT_FMUL,
ISD::STRICT_FDIV, ISD::STRICT_FSQRT, ISD::STRICT_FMA};

// TODO: support more vp ops.
static const unsigned ZvfhminPromoteVPOps[] = {
Expand Down Expand Up @@ -5482,6 +5483,41 @@ static SDValue SplitVectorReductionOp(SDValue Op, SelectionDAG &DAG) {
{ResLo, Hi, MaskHi, EVLHi}, Op->getFlags());
}

static SDValue SplitStrictFPVectorOp(SDValue Op, SelectionDAG &DAG) {

assert(Op->isStrictFPOpcode());

auto [LoVT, HiVT] = DAG.GetSplitDestVTs(Op->getValueType(0));

SDVTList LoVTs = DAG.getVTList(LoVT, Op->getValueType(1));
SDVTList HiVTs = DAG.getVTList(HiVT, Op->getValueType(1));

SDLoc DL(Op);

SmallVector<SDValue, 4> LoOperands(Op.getNumOperands());
SmallVector<SDValue, 4> HiOperands(Op.getNumOperands());

for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
if (!Op.getOperand(j).getValueType().isVector()) {
LoOperands[j] = Op.getOperand(j);
HiOperands[j] = Op.getOperand(j);
continue;
}
std::tie(LoOperands[j], HiOperands[j]) =
DAG.SplitVector(Op.getOperand(j), DL);
}

SDValue LoRes =
DAG.getNode(Op.getOpcode(), DL, LoVTs, LoOperands, Op->getFlags());
HiOperands[0] = LoRes.getValue(1);
SDValue HiRes =
DAG.getNode(Op.getOpcode(), DL, HiVTs, HiOperands, Op->getFlags());

SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, Op->getValueType(0),
LoRes.getValue(0), HiRes.getValue(0));
return DAG.getMergeValues({V, HiRes.getValue(1)}, DL);
}

SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
SelectionDAG &DAG) const {
switch (Op.getOpcode()) {
Expand Down Expand Up @@ -6256,6 +6292,10 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
case ISD::STRICT_FDIV:
case ISD::STRICT_FSQRT:
case ISD::STRICT_FMA:
if (Op.getValueType() == MVT::nxv32f16 &&
(Subtarget.hasVInstructionsF16Minimal() &&
!Subtarget.hasVInstructionsF16()))
return SplitStrictFPVectorOp(Op, DAG);
return lowerToScalableOp(Op, DAG);
case ISD::STRICT_FSETCC:
case ISD::STRICT_FSETCCS:
Expand Down
Loading