Skip to content
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

[clang] Add tanf16 builtin and support for tan constrained intrinsic #93314

Merged
merged 3 commits into from
May 29, 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
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,11 @@ def SqrtF16F128 : Builtin, F16F128MathTemplate {
let Prototype = "T(T)";
}

def TanF128 : Builtin {
let Spellings = ["__builtin_tanf128"];
def TanF16F128 : Builtin, F16F128MathTemplate {
let Spellings = ["__builtin_tan"];
let Attributes = [FunctionWithBuiltinPrefix, NoThrow,
ConstIgnoringErrnoAndExceptions];
let Prototype = "__float128(__float128)";
let Prototype = "T(T)";
}

def TanhF128 : Builtin {
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2922,6 +2922,18 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
SetSqrtFPAccuracy(Call);
return RValue::get(Call);
}

case Builtin::BItan:
case Builtin::BItanf:
case Builtin::BItanl:
case Builtin::BI__builtin_tan:
case Builtin::BI__builtin_tanf:
case Builtin::BI__builtin_tanf16:
case Builtin::BI__builtin_tanl:
case Builtin::BI__builtin_tanf128:
return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(
*this, E, Intrinsic::tan, Intrinsic::experimental_constrained_tan));

case Builtin::BItrunc:
case Builtin::BItruncf:
case Builtin::BItruncl:
Expand Down
8 changes: 4 additions & 4 deletions clang/test/CodeGen/X86/math-builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,10 @@ __builtin_sqrt(f); __builtin_sqrtf(f); __builtin_sqrtl(f); __builtin_

__builtin_tan(f); __builtin_tanf(f); __builtin_tanl(f); __builtin_tanf128(f);

// NO__ERRNO: declare double @tan(double noundef) [[READNONE]]
// NO__ERRNO: declare float @tanf(float noundef) [[READNONE]]
// NO__ERRNO: declare x86_fp80 @tanl(x86_fp80 noundef) [[READNONE]]
// NO__ERRNO: declare fp128 @tanf128(fp128 noundef) [[READNONE]]
// NO__ERRNO: declare double @llvm.tan.f64(double) [[READNONE_INTRINSIC]]
// NO__ERRNO: declare float @llvm.tan.f32(float) [[READNONE_INTRINSIC]]
// NO__ERRNO: declare x86_fp80 @llvm.tan.f80(x86_fp80) [[READNONE_INTRINSIC]]
// NO__ERRNO: declare fp128 @llvm.tan.f128(fp128) [[READNONE_INTRINSIC]]
// HAS_ERRNO: declare double @tan(double noundef) [[NOT_READNONE]]
// HAS_ERRNO: declare float @tanf(float noundef) [[NOT_READNONE]]
// HAS_ERRNO: declare x86_fp80 @tanl(x86_fp80 noundef) [[NOT_READNONE]]
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CodeGen/constrained-math-builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c, _
// CHECK: call x86_fp80 @llvm.experimental.constrained.sqrt.f80(x86_fp80 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
// CHECK: call fp128 @llvm.experimental.constrained.sqrt.f128(fp128 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")

__builtin_tan(f); __builtin_tanf(f); __builtin_tanl(f); __builtin_tanf128(f);

// CHECK: call double @llvm.experimental.constrained.tan.f64(double %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
// CHECK: call float @llvm.experimental.constrained.tan.f32(float %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
// CHECK: call x86_fp80 @llvm.experimental.constrained.tan.f80(x86_fp80 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
// CHECK: call fp128 @llvm.experimental.constrained.tan.f128(fp128 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")


__builtin_trunc(f); __builtin_truncf(f); __builtin_truncl(f); __builtin_truncf128(f);

// CHECK: call double @llvm.experimental.constrained.trunc.f64(double %{{.*}}, metadata !"fpexcept.strict")
Expand Down Expand Up @@ -315,6 +323,11 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c, _
// CHECK: declare x86_fp80 @llvm.experimental.constrained.sqrt.f80(x86_fp80, metadata, metadata)
// CHECK: declare fp128 @llvm.experimental.constrained.sqrt.f128(fp128, metadata, metadata)

// CHECK: declare double @llvm.experimental.constrained.tan.f64(double, metadata, metadata)
// CHECK: declare float @llvm.experimental.constrained.tan.f32(float, metadata, metadata)
// CHECK: declare x86_fp80 @llvm.experimental.constrained.tan.f80(x86_fp80, metadata, metadata)
// CHECK: declare fp128 @llvm.experimental.constrained.tan.f128(fp128, metadata, metadata)

// CHECK: declare double @llvm.experimental.constrained.trunc.f64(double, metadata)
// CHECK: declare float @llvm.experimental.constrained.trunc.f32(float, metadata)
// CHECK: declare x86_fp80 @llvm.experimental.constrained.trunc.f80(x86_fp80, metadata)
Expand Down
12 changes: 6 additions & 6 deletions clang/test/CodeGen/math-libcalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,15 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {

tan(f); tanf(f); tanl(f);

// NO__ERRNO: declare double @tan(double noundef) [[READNONE]]
// NO__ERRNO: declare float @tanf(float noundef) [[READNONE]]
// NO__ERRNO: declare x86_fp80 @tanl(x86_fp80 noundef) [[READNONE]]
// NO__ERRNO: declare double @llvm.tan.f64(double) [[READNONE_INTRINSIC]]
// NO__ERRNO: declare float @llvm.tan.f32(float) [[READNONE_INTRINSIC]]
// NO__ERRNO: declare x86_fp80 @llvm.tan.f80(x86_fp80) [[READNONE_INTRINSIC]]
// HAS_ERRNO: declare double @tan(double noundef) [[NOT_READNONE]]
// HAS_ERRNO: declare float @tanf(float noundef) [[NOT_READNONE]]
// HAS_ERRNO: declare x86_fp80 @tanl(x86_fp80 noundef) [[NOT_READNONE]]
// HAS_MAYTRAP: declare double @tan(double noundef) [[NOT_READNONE]]
// HAS_MAYTRAP: declare float @tanf(float noundef) [[NOT_READNONE]]
// HAS_MAYTRAP: declare x86_fp80 @tanl(x86_fp80 noundef) [[NOT_READNONE]]
// HAS_MAYTRAP: declare double @llvm.experimental.constrained.tan.f64(
// HAS_MAYTRAP: declare float @llvm.experimental.constrained.tan.f32(
// HAS_MAYTRAP: declare x86_fp80 @llvm.experimental.constrained.tan.f80(

tanh(f); tanhf(f); tanhl(f);

Expand Down
3 changes: 3 additions & 0 deletions clang/test/CodeGenOpenCL/builtins-f16.cl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ void test_half_builtins(half h0, half h1, half h2, int i0) {
// CHECK: call half @llvm.sqrt.f16(half %h0)
res = __builtin_sqrtf16(h0);

// CHECK: call half @llvm.tan.f16(half %h0)
res = __builtin_tanf16(h0);

// CHECK: call half @llvm.trunc.f16(half %h0)
res = __builtin_truncf16(h0);

Expand Down
36 changes: 36 additions & 0 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26169,6 +26169,42 @@ same values as the libm ``cos`` functions would, and handles error
conditions in the same way.


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

Syntax:
"""""""

::

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

Overview:
"""""""""

The '``llvm.experimental.constrained.tan``' intrinsic returns the 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 tangent of the specified operand, returning the
same values as the libm ``tan`` functions would, and handles error
conditions in the same way.


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

Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/CodeGen/ISDOpcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ enum NodeType {
STRICT_FLDEXP,
STRICT_FSIN,
STRICT_FCOS,
STRICT_FTAN,
STRICT_FEXP,
STRICT_FEXP2,
STRICT_FLOG,
Expand Down Expand Up @@ -934,6 +935,7 @@ enum NodeType {
FCBRT,
FSIN,
FCOS,
FTAN,
FPOW,
FPOWI,
/// FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/ConstrainedOps.def
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ 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(sqrt, 1, 1, experimental_constrained_sqrt, FSQRT)
DAG_FUNCTION(tan, 1, 1, experimental_constrained_tan, FTAN)
DAG_FUNCTION(trunc, 1, 0, experimental_constrained_trunc, FTRUNC)

// This is definition for fmuladd intrinsic function, that is converted into
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/Intrinsics.td
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,10 @@ let IntrProperties = [IntrInaccessibleMemOnly, IntrWillReturn, IntrStrictFP] in
[ LLVMMatchType<0>,
llvm_metadata_ty,
llvm_metadata_ty ]>;
def int_experimental_constrained_tan : 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
8 changes: 8 additions & 0 deletions llvm/test/Assembler/fp-intrinsics-attr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ define void @func(double %a, double %b, double %c, i32 %i) strictfp {
metadata !"round.dynamic",
metadata !"fpexcept.strict")

%tan = call double @llvm.experimental.constrained.tan.f64(
double %a,
metadata !"round.dynamic",
metadata !"fpexcept.strict")

%pow = call double @llvm.experimental.constrained.pow.f64(
double %a, double %b,
metadata !"round.dynamic",
Expand Down Expand Up @@ -244,6 +249,9 @@ declare double @llvm.experimental.constrained.sin.f64(double, metadata, metadata
declare double @llvm.experimental.constrained.cos.f64(double, metadata, metadata)
; CHECK: @llvm.experimental.constrained.cos.f64({{.*}}) #[[ATTR1]]

declare double @llvm.experimental.constrained.tan.f64(double, metadata, metadata)
; CHECK: @llvm.experimental.constrained.tan.f64({{.*}}) #[[ATTR1]]

declare double @llvm.experimental.constrained.pow.f64(double, double, metadata, metadata)
; CHECK: @llvm.experimental.constrained.pow.f64({{.*}}) #[[ATTR1]]

Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Feature/fp-intrinsics.ll
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ entry:
ret double %result
}

; Verify that tan(42.0) isn't simplified when the rounding mode is unknown.
; CHECK-LABEL: ftan
; CHECK: call double @llvm.experimental.constrained.tan
define double @ftan() #0 {
entry:
%result = call double @llvm.experimental.constrained.tan.f64(double 42.0,
metadata !"round.dynamic",
metadata !"fpexcept.strict") #0
ret double %result
}

; Verify that exp(42.0) isn't simplified when the rounding mode is unknown.
; CHECK-LABEL: f10
; CHECK: call double @llvm.experimental.constrained.exp
Expand Down
Loading