Skip to content

[VP][RISCV] Introduce vp.lrint/llrint and RISC-V support. #82627

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 3 commits into from
Feb 26, 2024
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
98 changes: 98 additions & 0 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16021,6 +16021,8 @@ functions would, but without setting errno. If the rounded value is
too large to be stored in the result type, the return value is a
non-deterministic value (equivalent to `freeze poison`).

.. _int_lrint:

'``llvm.lrint.*``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -16066,6 +16068,8 @@ would, but without setting errno. If the rounded value is too large to
be stored in the result type, the return value is a non-deterministic
value (equivalent to `freeze poison`).

.. _int_llrint:

'``llvm.llrint.*``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -23382,6 +23386,100 @@ Examples:
%t = call <4 x float> @llvm.trunc.v4f32(<4 x float> %a)
%also.r = select <4 x i1> %mask, <4 x float> %t, <4 x float> poison

.. _int_vp_lrint:

'``llvm.vp.lrint.*``' Intrinsics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""
This is an overloaded intrinsic.

::

declare <16 x i32> @llvm.vp.lrint.v16i32.v16f32(<16 x float> <op>, <16 x i1> <mask>, i32 <vector_length>)
declare <vscale x 4 x i32> @llvm.vp.lrint.nxv4i32.nxv4f32(<vscale x 4 x float> <op>, <vscale x 4 x i1> <mask>, i32 <vector_length>)
declare <256 x i64> @llvm.vp.lrint.v256i64.v256f64(<256 x double> <op>, <256 x i1> <mask>, i32 <vector_length>)

Overview:
"""""""""

Predicated lrint of a vector of floating-point values.


Arguments:
""""""""""

The result is an integer vector and the first operand is a vector of :ref:`floating-point <t_floating>`
type with the same number of elements as the result vector type. The second
operand is the vector mask and has the same number of elements as the result
vector type. The third operand is the explicit vector length of the operation.

Semantics:
""""""""""

The '``llvm.vp.lrint``' intrinsic performs lrint (:ref:`lrint <int_lrint>`) of
the first vector operand on each enabled lane. The result on disabled lanes is a
:ref:`poison value <poisonvalues>`.

Examples:
"""""""""

.. code-block:: llvm

%r = call <4 x i32> @llvm.vp.lrint.v4i32.v4f32(<4 x float> %a, <4 x i1> %mask, i32 %evl)
;; For all lanes below %evl, %r is lane-wise equivalent to %also.r

%t = call <4 x i32> @llvm.lrint.v4f32(<4 x float> %a)
%also.r = select <4 x i1> %mask, <4 x i32> %t, <4 x i32> poison

.. _int_vp_llrint:

'``llvm.vp.llrint.*``' Intrinsics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""
This is an overloaded intrinsic.

::

declare <16 x i32> @llvm.vp.llrint.v16i32.v16f32(<16 x float> <op>, <16 x i1> <mask>, i32 <vector_length>)
declare <vscale x 4 x i32> @llvm.vp.llrint.nxv4i32.nxv4f32(<vscale x 4 x float> <op>, <vscale x 4 x i1> <mask>, i32 <vector_length>)
declare <256 x i64> @llvm.vp.llrint.v256i64.v256f64(<256 x double> <op>, <256 x i1> <mask>, i32 <vector_length>)

Overview:
"""""""""

Predicated llrint of a vector of floating-point values.


Arguments:
""""""""""
The result is an integer vector and the first operand is a vector of :ref:`floating-point <t_floating>`
type with the same number of elements as the result vector type. The second
operand is the vector mask and has the same number of elements as the result
vector type. The third operand is the explicit vector length of the operation.

Semantics:
""""""""""

The '``llvm.vp.llrint``' intrinsic performs lrint (:ref:`llrint <int_llrint>`) of
the first vector operand on each enabled lane. The result on disabled lanes is a
:ref:`poison value <poisonvalues>`.

Examples:
"""""""""

.. code-block:: llvm

%r = call <4 x i32> @llvm.vp.llrint.v4i32.v4f32(<4 x float> %a, <4 x i1> %mask, i32 %evl)
;; For all lanes below %evl, %r is lane-wise equivalent to %also.r

%t = call <4 x i32> @llvm.llrint.v4f32(<4 x float> %a)
%also.r = select <4 x i1> %mask, <4 x i32> %t, <4 x i32> poison


.. _int_vp_bitreverse:

'``llvm.vp.bitreverse.*``' Intrinsics
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/IR/Intrinsics.td
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,14 @@ let IntrProperties = [IntrNoMem, IntrNoSync, IntrWillReturn] in {
[ LLVMMatchType<0>,
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>,
llvm_i32_ty]>;
def int_vp_lrint : DefaultAttrsIntrinsic<[ llvm_anyvector_ty ],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a Verifier.cpp change to check the element counts like we do for the non-VP versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

[ llvm_anyvector_ty,
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>,
llvm_i32_ty]>;
def int_vp_llrint : DefaultAttrsIntrinsic<[ llvm_anyvector_ty ],
[ llvm_anyvector_ty,
LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>,
llvm_i32_ty]>;

// Casts
def int_vp_trunc : DefaultAttrsIntrinsic<[ llvm_anyvector_ty ],
Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm/IR/VPIntrinsics.def
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,18 @@ VP_PROPERTY_FUNCTIONAL_INTRINSIC(nearbyint)
VP_PROPERTY_FUNCTIONAL_SDOPC(FNEARBYINT)
END_REGISTER_VP(vp_nearbyint, VP_FNEARBYINT)

// llvm.vp.lrint(x,mask,vlen)
BEGIN_REGISTER_VP(vp_lrint, 1, 2, VP_LRINT, 0)
VP_PROPERTY_FUNCTIONAL_INTRINSIC(lrint)
VP_PROPERTY_FUNCTIONAL_SDOPC(LRINT)
END_REGISTER_VP(vp_lrint, VP_LRINT)

// llvm.vp.llrint(x,mask,vlen)
BEGIN_REGISTER_VP(vp_llrint, 1, 2, VP_LLRINT, 0)
VP_PROPERTY_FUNCTIONAL_INTRINSIC(llrint)
VP_PROPERTY_FUNCTIONAL_SDOPC(LLRINT)
END_REGISTER_VP(vp_llrint, VP_LLRINT)

///// } Floating-Point Arithmetic

///// Type Casts {
Expand Down
14 changes: 13 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,9 @@ void DAGTypeLegalizer::SplitVectorResult(SDNode *N, unsigned ResNo) {
case ISD::FRINT:
case ISD::VP_FRINT:
case ISD::LRINT:
case ISD::VP_LRINT:
case ISD::LLRINT:
case ISD::VP_LLRINT:
case ISD::FROUND:
case ISD::VP_FROUND:
case ISD::FROUNDEVEN:
Expand Down Expand Up @@ -4263,6 +4265,8 @@ void DAGTypeLegalizer::WidenVectorResult(SDNode *N, unsigned ResNo) {

case ISD::LRINT:
case ISD::LLRINT:
case ISD::VP_LRINT:
case ISD::VP_LLRINT:
Res = WidenVecRes_XRINT(N);
break;

Expand Down Expand Up @@ -4869,7 +4873,15 @@ SDValue DAGTypeLegalizer::WidenVecRes_XRINT(SDNode *N) {
if (WidenNumElts != SrcVT.getVectorElementCount())
return DAG.UnrollVectorOp(N, WidenNumElts.getKnownMinValue());

return DAG.getNode(N->getOpcode(), dl, WidenVT, Src);
if (N->getNumOperands() == 1)
return DAG.getNode(N->getOpcode(), dl, WidenVT, Src);

assert(N->getNumOperands() == 3 && "Unexpected number of operands!");
assert(N->isVPOpcode() && "Expected VP opcode");

SDValue Mask =
GetWidenedMask(N->getOperand(1), WidenVT.getVectorElementCount());
return DAG.getNode(N->getOpcode(), dl, WidenVT, Src, Mask, N->getOperand(2));
}

SDValue DAGTypeLegalizer::WidenVecRes_Convert_StrictFP(SDNode *N) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/IR/IntrinsicInst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ Function *VPIntrinsic::getDeclarationForParams(Module *M, Intrinsic::ID VPID,
case Intrinsic::vp_fpext:
case Intrinsic::vp_ptrtoint:
case Intrinsic::vp_inttoptr:
case Intrinsic::vp_lrint:
case Intrinsic::vp_llrint:
VPFunc =
Intrinsic::getDeclaration(M, VPID, {ReturnType, Params[0]->getType()});
break;
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6183,9 +6183,11 @@ void Verifier::visitVPIntrinsic(VPIntrinsic &VPI) {
break;
case Intrinsic::vp_fptoui:
case Intrinsic::vp_fptosi:
case Intrinsic::vp_lrint:
case Intrinsic::vp_llrint:
Check(
RetTy->isIntOrIntVectorTy() && ValTy->isFPOrFPVectorTy(),
"llvm.vp.fptoui or llvm.vp.fptosi intrinsic first argument element "
"llvm.vp.fptoui, llvm.vp.fptosi, llvm.vp.lrint or llvm.vp.llrint" "intrinsic first argument element "
"type must be floating-point and result element type must be integer",
*VPCast);
break;
Expand Down
10 changes: 9 additions & 1 deletion llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,8 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
ISD::VP_FCEIL, ISD::VP_FFLOOR, ISD::VP_FROUND,
ISD::VP_FROUNDEVEN, ISD::VP_FCOPYSIGN, ISD::VP_FROUNDTOZERO,
ISD::VP_FRINT, ISD::VP_FNEARBYINT, ISD::VP_IS_FPCLASS,
ISD::VP_FMINIMUM, ISD::VP_FMAXIMUM, ISD::EXPERIMENTAL_VP_REVERSE,
ISD::VP_FMINIMUM, ISD::VP_FMAXIMUM, ISD::VP_LRINT,
ISD::VP_LLRINT, ISD::EXPERIMENTAL_VP_REVERSE,
ISD::EXPERIMENTAL_VP_SPLICE};

static const unsigned IntegerVecReduceOps[] = {
Expand Down Expand Up @@ -5811,6 +5812,11 @@ static unsigned getRISCVVLOp(SDValue Op) {
case ISD::FMAXNUM:
case ISD::VP_FMAXNUM:
return RISCVISD::VFMAX_VL;
case ISD::LRINT:
case ISD::VP_LRINT:
case ISD::LLRINT:
case ISD::VP_LLRINT:
return RISCVISD::VFCVT_X_F_VL;
}
// clang-format on
#undef OP_CASE
Expand Down Expand Up @@ -6801,6 +6807,8 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
case ISD::VP_USUBSAT:
case ISD::VP_SADDSAT:
case ISD::VP_SSUBSAT:
case ISD::VP_LRINT:
case ISD::VP_LLRINT:
return lowerVPOp(Op, DAG);
case ISD::VP_AND:
case ISD::VP_OR:
Expand Down
Loading