Skip to content

Commit

Permalink
[LegalizeTypes][RISCV] Support libcalls for fpto{s,u}i of bfloat by e…
Browse files Browse the repository at this point in the history
…xtending to f32 first

As there is no direct bf16 libcall for these conversions, extend to f32
first.

This patch includes a tiny refactoring to pull out equivalent logic in
ExpandIntRes_XROUND_XRINT so it can be reused in
ExpandIntRes_FP_TO_{S,U}INT.

This patch also demonstrates incorrect codegen for RV32 without zfbfmin
for the newly enabled tests. As it doesn't introduce that incorrect
codegen (caused by the assumption that 'TypeSoftPromoteHalf' is only
used for f16 types), a fix will be added in a follow-up (D157287).

Differential Revision: https://reviews.llvm.org/D156990
  • Loading branch information
asb committed Aug 8, 2023
1 parent 5e67348 commit f7dbc85
Show file tree
Hide file tree
Showing 2 changed files with 335 additions and 28 deletions.
30 changes: 23 additions & 7 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,6 +3591,17 @@ void DAGTypeLegalizer::ExpandIntRes_GET_ROUNDING(SDNode *N, SDValue &Lo,
ReplaceValueWith(SDValue(N, 1), Chain);
}

// Helper for producing an FP_EXTEND/STRICT_FP_EXTEND of Op.
static SDValue fpExtendHelper(SDValue Op, SDValue &Chain, bool IsStrict, EVT VT,
SDLoc DL, SelectionDAG &DAG) {
if (IsStrict) {
Op = DAG.getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op});
Chain = Op.getValue(1);
return Op;
}
return DAG.getNode(ISD::FP_EXTEND, DL, VT, Op);
}

void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
SDValue &Hi) {
SDLoc dl(N);
Expand All @@ -3611,6 +3622,11 @@ void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
return;
}

if (Op.getValueType() == MVT::bf16) {
// Extend to f32 as there is no bf16 libcall.
Op = fpExtendHelper(Op, Chain, IsStrict, MVT::f32, dl, DAG);
}

RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
TargetLowering::MakeLibCallOptions CallOptions;
Expand Down Expand Up @@ -3643,6 +3659,11 @@ void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
return;
}

if (Op.getValueType() == MVT::bf16) {
// Extend to f32 as there is no bf16 libcall.
Op = fpExtendHelper(Op, Chain, IsStrict, MVT::f32, dl, DAG);
}

RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
TargetLowering::MakeLibCallOptions CallOptions;
Expand Down Expand Up @@ -3673,14 +3694,9 @@ void DAGTypeLegalizer::ExpandIntRes_XROUND_XRINT(SDNode *N, SDValue &Lo,
EVT VT = Op.getValueType();

if (VT == MVT::f16) {
VT = MVT::f32;
// Extend to f32.
if (IsStrict) {
Op = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, { VT, MVT::Other }, {Chain, Op});
Chain = Op.getValue(1);
} else {
Op = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op);
}
VT = MVT::f32;
Op = fpExtendHelper(Op, Chain, IsStrict, VT, dl, DAG);
}

RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Expand Down
Loading

0 comments on commit f7dbc85

Please sign in to comment.