Skip to content

Commit

Permalink
[X86][CodeGen] Add base trig intrinsic lowerings (llvm#96222)
Browse files Browse the repository at this point in the history
This change is an implementation of
llvm#87367 investigation on
supporting IEEE math operations as intrinsics.
Which was discussed in this RFC:
https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

This change adds constraint intrinsics and some lowering cases for
`acos`, `asin`, `atan`, `cosh`, `sinh`, and `tanh`.
The only x86 specific change was for f80.

llvm#70079
llvm#70080
llvm#70081
llvm#70083
llvm#70084
llvm#95966
    
The x86 lowering is going to be done in three pr changes with this being
the first.
A second PR will be put up for Loop Vectorizing and then SLPVectorizer.

The constraint intrinsics is also going to be in multiple parts, but
just 2.
This part covers just the llvm specific changes, part2 will cover clang
specifc changes and legalization for backends than have special
legalization
 requirements like aarch64 and wasm.
  • Loading branch information
farzonl authored and aaryanshukla committed Jul 14, 2024
1 parent f186a09 commit c8c7271
Show file tree
Hide file tree
Showing 32 changed files with 3,288 additions and 8 deletions.
213 changes: 213 additions & 0 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26535,6 +26535,219 @@ This function returns the tangent of the specified argument, returning the
same values as the libm ``tan`` functions would, and handles error
conditions in the same way.

'``llvm.experimental.constrained.asin``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

::

declare <type>
@llvm.experimental.constrained.asin(<type> <op1>,
metadata <rounding mode>,
metadata <exception behavior>)

Overview:
"""""""""

The '``llvm.experimental.constrained.asin``' intrinsic returns the arcsine of the
first operand.

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

The first argument and the return type are floating-point numbers of the same
type.

The second and third arguments specify the rounding mode and exception
behavior as described above.

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

This function returns the arcsine of the specified operand, returning the
same values as the libm ``asin`` functions would, and handles error
conditions in the same way.


'``llvm.experimental.constrained.acos``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

::

declare <type>
@llvm.experimental.constrained.acos(<type> <op1>,
metadata <rounding mode>,
metadata <exception behavior>)

Overview:
"""""""""

The '``llvm.experimental.constrained.acos``' intrinsic returns the arccosine of the
first operand.

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

The first argument and the return type are floating-point numbers of the same
type.

The second and third arguments specify the rounding mode and exception
behavior as described above.

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

This function returns the arccosine of the specified operand, returning the
same values as the libm ``acos`` functions would, and handles error
conditions in the same way.


'``llvm.experimental.constrained.atan``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

::

declare <type>
@llvm.experimental.constrained.atan(<type> <op1>,
metadata <rounding mode>,
metadata <exception behavior>)

Overview:
"""""""""

The '``llvm.experimental.constrained.atan``' intrinsic returns the arctangent of the
first operand.

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

The first argument and the return type are floating-point numbers of the same
type.

The second and third arguments specify the rounding mode and exception
behavior as described above.

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

This function returns the arctangent of the specified operand, returning the
same values as the libm ``atan`` functions would, and handles error
conditions in the same way.

'``llvm.experimental.constrained.sinh``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

::

declare <type>
@llvm.experimental.constrained.sinh(<type> <op1>,
metadata <rounding mode>,
metadata <exception behavior>)

Overview:
"""""""""

The '``llvm.experimental.constrained.sinh``' intrinsic returns the hyperbolic sine of the
first operand.

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

The first argument and the return type are floating-point numbers of the same
type.

The second and third arguments specify the rounding mode and exception
behavior as described above.

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

This function returns the hyperbolic sine of the specified operand, returning the
same values as the libm ``sinh`` functions would, and handles error
conditions in the same way.


'``llvm.experimental.constrained.cosh``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

::

declare <type>
@llvm.experimental.constrained.cosh(<type> <op1>,
metadata <rounding mode>,
metadata <exception behavior>)

Overview:
"""""""""

The '``llvm.experimental.constrained.cosh``' intrinsic returns the hyperbolic cosine of the
first operand.

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

The first argument and the return type are floating-point numbers of the same
type.

The second and third arguments specify the rounding mode and exception
behavior as described above.

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

This function returns the hyperbolic cosine of the specified operand, returning the
same values as the libm ``cosh`` functions would, and handles error
conditions in the same way.


'``llvm.experimental.constrained.tanh``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

::

declare <type>
@llvm.experimental.constrained.tanh(<type> <op1>,
metadata <rounding mode>,
metadata <exception behavior>)

Overview:
"""""""""

The '``llvm.experimental.constrained.tanh``' intrinsic returns the hyperbolic tangent of the
first operand.

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

The first argument and the return type are floating-point numbers of the same
type.

The second and third arguments specify the rounding mode and exception
behavior as described above.

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

This function returns the hyperbolic tangent of the specified operand, returning the
same values as the libm ``tanh`` functions would, and handles error
conditions in the same way.

'``llvm.experimental.constrained.exp``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
18 changes: 18 additions & 0 deletions llvm/include/llvm/CodeGen/BasicTTIImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,24 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
case Intrinsic::tan:
ISD = ISD::FTAN;
break;
case Intrinsic::asin:
ISD = ISD::FASIN;
break;
case Intrinsic::acos:
ISD = ISD::FACOS;
break;
case Intrinsic::atan:
ISD = ISD::FATAN;
break;
case Intrinsic::sinh:
ISD = ISD::FSINH;
break;
case Intrinsic::cosh:
ISD = ISD::FCOSH;
break;
case Intrinsic::tanh:
ISD = ISD::FTANH;
break;
case Intrinsic::exp:
ISD = ISD::FEXP;
break;
Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm/CodeGen/ISDOpcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ enum NodeType {
STRICT_FSIN,
STRICT_FCOS,
STRICT_FTAN,
STRICT_FASIN,
STRICT_FACOS,
STRICT_FATAN,
STRICT_FSINH,
STRICT_FCOSH,
STRICT_FTANH,
STRICT_FEXP,
STRICT_FEXP2,
STRICT_FLOG,
Expand Down Expand Up @@ -948,6 +954,12 @@ enum NodeType {
FSIN,
FCOS,
FTAN,
FASIN,
FACOS,
FATAN,
FSINH,
FCOSH,
FTANH,
FPOW,
FPOWI,
/// FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/ConstrainedOps.def
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ CMP_INSTRUCTION(FCmp, 2, 0, experimental_constrained_fcmps, FSETCCS
// Theses are definitions for intrinsic functions, that are converted into
// constrained intrinsics.
//
DAG_FUNCTION(acos, 1, 1, experimental_constrained_acos, FACOS)
DAG_FUNCTION(asin, 1, 1, experimental_constrained_asin, FASIN)
DAG_FUNCTION(atan, 1, 1, experimental_constrained_atan, FATAN)
DAG_FUNCTION(ceil, 1, 0, experimental_constrained_ceil, FCEIL)
DAG_FUNCTION(cos, 1, 1, experimental_constrained_cos, FCOS)
DAG_FUNCTION(cosh, 1, 1, experimental_constrained_cosh, FCOSH)
DAG_FUNCTION(exp, 1, 1, experimental_constrained_exp, FEXP)
DAG_FUNCTION(exp2, 1, 1, experimental_constrained_exp2, FEXP2)
DAG_FUNCTION(floor, 1, 0, experimental_constrained_floor, FFLOOR)
Expand All @@ -94,8 +98,10 @@ DAG_FUNCTION(rint, 1, 1, experimental_constrained_rint, FRINT)
DAG_FUNCTION(round, 1, 0, experimental_constrained_round, FROUND)
DAG_FUNCTION(roundeven, 1, 0, experimental_constrained_roundeven, FROUNDEVEN)
DAG_FUNCTION(sin, 1, 1, experimental_constrained_sin, FSIN)
DAG_FUNCTION(sinh, 1, 1, experimental_constrained_sinh, FSINH)
DAG_FUNCTION(sqrt, 1, 1, experimental_constrained_sqrt, FSQRT)
DAG_FUNCTION(tan, 1, 1, experimental_constrained_tan, FTAN)
DAG_FUNCTION(tanh, 1, 1, experimental_constrained_tanh, FTANH)
DAG_FUNCTION(trunc, 1, 0, experimental_constrained_trunc, FTRUNC)

// This is definition for fmuladd intrinsic function, that is converted into
Expand Down
24 changes: 24 additions & 0 deletions llvm/include/llvm/IR/Intrinsics.td
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,18 @@ let IntrProperties = [IntrInaccessibleMemOnly, IntrWillReturn, IntrStrictFP] in
llvm_anyint_ty,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_asin : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_acos : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_atan : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_sin : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
llvm_metadata_ty,
Expand All @@ -1223,6 +1235,18 @@ let IntrProperties = [IntrInaccessibleMemOnly, IntrWillReturn, IntrStrictFP] in
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_sinh : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_cosh : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_tanh : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_pow : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
[ LLVMMatchType<0>,
LLVMMatchType<0>,
Expand Down
30 changes: 30 additions & 0 deletions llvm/include/llvm/IR/RuntimeLibcalls.def
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,36 @@ HANDLE_LIBCALL(TAN_F64, "tan")
HANDLE_LIBCALL(TAN_F80, "tanl")
HANDLE_LIBCALL(TAN_F128,"tanl")
HANDLE_LIBCALL(TAN_PPCF128, "tanl")
HANDLE_LIBCALL(SINH_F32, "sinhf")
HANDLE_LIBCALL(SINH_F64, "sinh")
HANDLE_LIBCALL(SINH_F80, "sinhl")
HANDLE_LIBCALL(SINH_F128, "sinhl")
HANDLE_LIBCALL(SINH_PPCF128, "sinhl")
HANDLE_LIBCALL(COSH_F32, "coshf")
HANDLE_LIBCALL(COSH_F64, "cosh")
HANDLE_LIBCALL(COSH_F80, "coshl")
HANDLE_LIBCALL(COSH_F128, "coshl")
HANDLE_LIBCALL(COSH_PPCF128, "coshl")
HANDLE_LIBCALL(TANH_F32, "tanhf")
HANDLE_LIBCALL(TANH_F64, "tanh")
HANDLE_LIBCALL(TANH_F80, "tanhl")
HANDLE_LIBCALL(TANH_F128,"tanhl")
HANDLE_LIBCALL(TANH_PPCF128, "tanhl")
HANDLE_LIBCALL(ASIN_F32, "asinf")
HANDLE_LIBCALL(ASIN_F64, "asin")
HANDLE_LIBCALL(ASIN_F80, "asinl")
HANDLE_LIBCALL(ASIN_F128, "asinl")
HANDLE_LIBCALL(ASIN_PPCF128, "asinl")
HANDLE_LIBCALL(ACOS_F32, "acosf")
HANDLE_LIBCALL(ACOS_F64, "acos")
HANDLE_LIBCALL(ACOS_F80, "acosl")
HANDLE_LIBCALL(ACOS_F128, "acosl")
HANDLE_LIBCALL(ACOS_PPCF128, "acosl")
HANDLE_LIBCALL(ATAN_F32, "atanf")
HANDLE_LIBCALL(ATAN_F64, "atan")
HANDLE_LIBCALL(ATAN_F80, "atanl")
HANDLE_LIBCALL(ATAN_F128,"atanl")
HANDLE_LIBCALL(ATAN_PPCF128, "atanl")
HANDLE_LIBCALL(SINCOS_F32, nullptr)
HANDLE_LIBCALL(SINCOS_F64, nullptr)
HANDLE_LIBCALL(SINCOS_F80, nullptr)
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/Target/GlobalISel/SelectionDAGCompat.td
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ def : GINodeEquiv<G_FCEIL, fceil>;
def : GINodeEquiv<G_FCOS, fcos>;
def : GINodeEquiv<G_FSIN, fsin>;
def : GINodeEquiv<G_FTAN, ftan>;
def : GINodeEquiv<G_FACOS, facos>;
def : GINodeEquiv<G_FASIN, fasin>;
def : GINodeEquiv<G_FATAN, fatan>;
def : GINodeEquiv<G_FCOSH, fcosh>;
def : GINodeEquiv<G_FSINH, fsinh>;
def : GINodeEquiv<G_FTANH, ftanh>;
def : GINodeEquiv<G_FABS, fabs>;
def : GINodeEquiv<G_FSQRT, fsqrt>;
def : GINodeEquiv<G_FFLOOR, ffloor>;
Expand Down
Loading

0 comments on commit c8c7271

Please sign in to comment.