Skip to content

Commit 7c7b72b

Browse files
committed
Emit constrained atan2 intrinsic for clang builtin
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294 - `Builtins.td` - Add f16 support for libm atan2 builtin - `CGBuiltin.cpp` - Emit constraint atan2 intrinsic for clang builtin Part of Implement the atan2 HLSL Function llvm#70096.
1 parent 66bbbf2 commit 7c7b72b

File tree

8 files changed

+49
-29
lines changed

8 files changed

+49
-29
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ def FminimumNumF16F128 : Builtin, F16F128MathTemplate {
227227
let Prototype = "T(T, T)";
228228
}
229229

230-
def Atan2F128 : Builtin {
231-
let Spellings = ["__builtin_atan2f128"];
230+
def Atan2F16F128 : Builtin, F16F128MathTemplate {
231+
let Spellings = ["__builtin_atan2"];
232232
let Attributes = [FunctionWithBuiltinPrefix, NoThrow, ConstIgnoringErrnoAndExceptions];
233-
let Prototype = "__float128(__float128, __float128)";
233+
let Prototype = "T(T, T)";
234234
}
235235

236236
def CopysignF16 : Builtin {

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,17 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
27242724
return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(
27252725
*this, E, Intrinsic::atan, Intrinsic::experimental_constrained_atan));
27262726

2727+
case Builtin::BIatan2:
2728+
case Builtin::BIatan2f:
2729+
case Builtin::BIatan2l:
2730+
case Builtin::BI__builtin_atan2:
2731+
case Builtin::BI__builtin_atan2f:
2732+
case Builtin::BI__builtin_atan2f16:
2733+
case Builtin::BI__builtin_atan2l:
2734+
case Builtin::BI__builtin_atan2f128:
2735+
return RValue::get(emitBinaryMaybeConstrainedFPBuiltin(
2736+
*this, E, Intrinsic::atan2, Intrinsic::experimental_constrained_atan2));
2737+
27272738
case Builtin::BIceil:
27282739
case Builtin::BIceilf:
27292740
case Builtin::BIceill:

clang/test/CodeGen/X86/math-builtins.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
4545

4646
__builtin_atan2(f,f); __builtin_atan2f(f,f) ; __builtin_atan2l(f, f); __builtin_atan2f128(f,f);
4747

48-
// NO__ERRNO: declare double @atan2(double noundef, double noundef) [[READNONE:#[0-9]+]]
49-
// NO__ERRNO: declare float @atan2f(float noundef, float noundef) [[READNONE]]
50-
// NO__ERRNO: declare x86_fp80 @atan2l(x86_fp80 noundef, x86_fp80 noundef) [[READNONE]]
51-
// NO__ERRNO: declare fp128 @atan2f128(fp128 noundef, fp128 noundef) [[READNONE]]
48+
// NO__ERRNO: declare double @llvm.atan2.f64(double, double) [[READNONE_INTRINSIC:#[0-9]+]]
49+
// NO__ERRNO: declare float @llvm.atan2.f32(float, float) [[READNONE_INTRINSIC]]
50+
// NO__ERRNO: declare x86_fp80 @llvm.atan2.f80(x86_fp80, x86_fp80) [[READNONE_INTRINSIC]]
51+
// NO__ERRNO: declare fp128 @llvm.atan2.f128(fp128, fp128) [[READNONE_INTRINSIC]]
5252
// HAS_ERRNO: declare double @atan2(double noundef, double noundef) [[NOT_READNONE]]
5353
// HAS_ERRNO: declare float @atan2f(float noundef, float noundef) [[NOT_READNONE]]
5454
// HAS_ERRNO: declare x86_fp80 @atan2l(x86_fp80 noundef, x86_fp80 noundef) [[NOT_READNONE]]
5555
// HAS_ERRNO: declare fp128 @atan2f128(fp128 noundef, fp128 noundef) [[NOT_READNONE]]
5656

5757
__builtin_copysign(f,f); __builtin_copysignf(f,f); __builtin_copysignl(f,f); __builtin_copysignf128(f,f);
5858

59-
// NO__ERRNO: declare double @llvm.copysign.f64(double, double) [[READNONE_INTRINSIC:#[0-9]+]]
59+
// NO__ERRNO: declare double @llvm.copysign.f64(double, double) [[READNONE_INTRINSIC]]
6060
// NO__ERRNO: declare float @llvm.copysign.f32(float, float) [[READNONE_INTRINSIC]]
6161
// NO__ERRNO: declare x86_fp80 @llvm.copysign.f80(x86_fp80, x86_fp80) [[READNONE_INTRINSIC]]
6262
// NO__ERRNO: declare fp128 @llvm.copysign.f128(fp128, fp128) [[READNONE_INTRINSIC]]
@@ -179,7 +179,7 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
179179

180180
__builtin_acosh(f); __builtin_acoshf(f); __builtin_acoshl(f); __builtin_acoshf128(f);
181181

182-
// NO__ERRNO: declare double @acosh(double noundef) [[READNONE]]
182+
// NO__ERRNO: declare double @acosh(double noundef) [[READNONE:#[0-9]+]]
183183
// NO__ERRNO: declare float @acoshf(float noundef) [[READNONE]]
184184
// NO__ERRNO: declare x86_fp80 @acoshl(x86_fp80 noundef) [[READNONE]]
185185
// NO__ERRNO: declare fp128 @acoshf128(fp128 noundef) [[READNONE]]
@@ -721,10 +721,10 @@ __builtin_trunc(f); __builtin_truncf(f); __builtin_truncl(f); __builtin
721721
// HAS_ERRNO: declare fp128 @llvm.trunc.f128(fp128) [[READNONE_INTRINSIC]]
722722
};
723723

724-
// NO__ERRNO: attributes [[READNONE]] = { {{.*}}memory(none){{.*}} }
725724
// NO__ERRNO: attributes [[READNONE_INTRINSIC]] = { {{.*}}memory(none){{.*}} }
726725
// NO__ERRNO: attributes [[NOT_READNONE]] = { nounwind {{.*}} }
727726
// NO__ERRNO: attributes [[PURE]] = { {{.*}}memory(read){{.*}} }
727+
// NO__ERRNO: attributes [[READNONE]] = { {{.*}}memory(none){{.*}} }
728728

729729
// HAS_ERRNO: attributes [[NOT_READNONE]] = { nounwind {{.*}} }
730730
// HAS_ERRNO: attributes [[READNONE_INTRINSIC]] = { {{.*}}memory(none){{.*}} }

clang/test/CodeGen/constrained-math-builtins.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ __builtin_atan(f); __builtin_atanf(f); __builtin_atanl(f); __builti
5757
// CHECK: call x86_fp80 @llvm.experimental.constrained.atan.f80(x86_fp80 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
5858
// CHECK: call fp128 @llvm.experimental.constrained.atan.f128(fp128 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
5959

60+
__builtin_atan2(f,f); __builtin_atan2f(f,f); __builtin_atan2l(f,f); __builtin_atan2f128(f,f);
61+
62+
// CHECK: call double @llvm.experimental.constrained.atan2.f64(double %{{.*}}, double %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
63+
// CHECK: call float @llvm.experimental.constrained.atan2.f32(float %{{.*}}, float %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
64+
// CHECK: call x86_fp80 @llvm.experimental.constrained.atan2.f80(x86_fp80 %{{.*}}, x86_fp80 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
65+
// CHECK: call fp128 @llvm.experimental.constrained.atan2.f128(fp128 %{{.*}}, fp128 %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
66+
6067
__builtin_ceil(f); __builtin_ceilf(f); __builtin_ceill(f); __builtin_ceilf128(f);
6168

6269
// CHECK: call double @llvm.experimental.constrained.ceil.f64(double %{{.*}}, metadata !"fpexcept.strict")

clang/test/CodeGen/libcalls.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ void test_builtins(double d, float f, long double ld) {
9595
double atan2_ = atan2(d, 2);
9696
long double atan2l_ = atan2l(ld, ld);
9797
float atan2f_ = atan2f(f, f);
98-
// CHECK-NO: declare double @atan2(double noundef, double noundef) [[NUW_RN:#[0-9]+]]
99-
// CHECK-NO: declare x86_fp80 @atan2l(x86_fp80 noundef, x86_fp80 noundef) [[NUW_RN]]
100-
// CHECK-NO: declare float @atan2f(float noundef, float noundef) [[NUW_RN]]
98+
// CHECK-NO: declare double @llvm.atan2.f64(double, double) [[NUW_RNI]]
99+
// CHECK-NO: declare x86_fp80 @llvm.atan2.f80(x86_fp80, x86_fp80) [[NUW_RNI]]
100+
// CHECK-NO: declare float @llvm.atan2.f32(float, float) [[NUW_RNI]]
101101
// CHECK-YES: declare double @atan2(double noundef, double noundef) [[NUW]]
102102
// CHECK-YES: declare x86_fp80 @atan2l(x86_fp80 noundef, x86_fp80 noundef) [[NUW]]
103103
// CHECK-YES: declare float @atan2f(float noundef, float noundef) [[NUW]]
@@ -124,5 +124,4 @@ void test_builtins(double d, float f, long double ld) {
124124
}
125125

126126
// CHECK-YES: attributes [[NUW]] = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+x87" }
127-
// CHECK-NO-DAG: attributes [[NUW_RN]] = { nounwind willreturn memory(none){{.*}} }
128127
// CHECK-NO-DAG: attributes [[NUW_RNI]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

clang/test/CodeGen/math-libcalls.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
2323

2424
atan2(f,f); atan2f(f,f) ; atan2l(f, f);
2525

26-
// NO__ERRNO: declare double @atan2(double noundef, double noundef) [[READNONE:#[0-9]+]]
27-
// NO__ERRNO: declare float @atan2f(float noundef, float noundef) [[READNONE]]
28-
// NO__ERRNO: declare x86_fp80 @atan2l(x86_fp80 noundef, x86_fp80 noundef) [[READNONE]]
26+
// NO__ERRNO: declare double @llvm.atan2.f64(double, double) [[READNONE_INTRINSIC:#[0-9]+]]
27+
// NO__ERRNO: declare float @llvm.atan2.f32(float, float) [[READNONE_INTRINSIC]]
28+
// NO__ERRNO: declare x86_fp80 @llvm.atan2.f80(x86_fp80, x86_fp80) [[READNONE_INTRINSIC]]
2929
// HAS_ERRNO: declare double @atan2(double noundef, double noundef) [[NOT_READNONE]]
3030
// HAS_ERRNO: declare float @atan2f(float noundef, float noundef) [[NOT_READNONE]]
3131
// HAS_ERRNO: declare x86_fp80 @atan2l(x86_fp80 noundef, x86_fp80 noundef) [[NOT_READNONE]]
32-
// HAS_MAYTRAP: declare double @atan2(double noundef, double noundef) [[NOT_READNONE:#[0-9]+]]
33-
// HAS_MAYTRAP: declare float @atan2f(float noundef, float noundef) [[NOT_READNONE]]
34-
// HAS_MAYTRAP: declare x86_fp80 @atan2l(x86_fp80 noundef, x86_fp80 noundef) [[NOT_READNONE]]
32+
// HAS_MAYTRAP: declare double @llvm.experimental.constrained.atan2.f64(
33+
// HAS_MAYTRAP: declare float @llvm.experimental.constrained.atan2.f32(
34+
// HAS_MAYTRAP: declare x86_fp80 @llvm.experimental.constrained.atan2.f80(
3535

3636
copysign(f,f); copysignf(f,f);copysignl(f,f);
3737

38-
// NO__ERRNO: declare double @llvm.copysign.f64(double, double) [[READNONE_INTRINSIC:#[0-9]+]]
38+
// NO__ERRNO: declare double @llvm.copysign.f64(double, double) [[READNONE_INTRINSIC]]
3939
// NO__ERRNO: declare float @llvm.copysign.f32(float, float) [[READNONE_INTRINSIC]]
4040
// NO__ERRNO: declare x86_fp80 @llvm.copysign.f80(x86_fp80, x86_fp80) [[READNONE_INTRINSIC]]
4141
// HAS_ERRNO: declare double @llvm.copysign.f64(double, double) [[READNONE_INTRINSIC:#[0-9]+]]
@@ -65,13 +65,13 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
6565
// HAS_ERRNO: declare double @frexp(double noundef, ptr noundef) [[NOT_READNONE]]
6666
// HAS_ERRNO: declare float @frexpf(float noundef, ptr noundef) [[NOT_READNONE]]
6767
// HAS_ERRNO: declare x86_fp80 @frexpl(x86_fp80 noundef, ptr noundef) [[NOT_READNONE]]
68-
// HAS_MAYTRAP: declare double @frexp(double noundef, ptr noundef) [[NOT_READNONE]]
68+
// HAS_MAYTRAP: declare double @frexp(double noundef, ptr noundef) [[NOT_READNONE:#[0-9]+]]
6969
// HAS_MAYTRAP: declare float @frexpf(float noundef, ptr noundef) [[NOT_READNONE]]
7070
// HAS_MAYTRAP: declare x86_fp80 @frexpl(x86_fp80 noundef, ptr noundef) [[NOT_READNONE]]
7171

7272
ldexp(f,f); ldexpf(f,f); ldexpl(f,f);
7373

74-
// NO__ERRNO: declare double @ldexp(double noundef, i32 noundef) [[READNONE]]
74+
// NO__ERRNO: declare double @ldexp(double noundef, i32 noundef) [[READNONE:#[0-9]+]]
7575
// NO__ERRNO: declare float @ldexpf(float noundef, i32 noundef) [[READNONE]]
7676
// NO__ERRNO: declare x86_fp80 @ldexpl(x86_fp80 noundef, i32 noundef) [[READNONE]]
7777
// HAS_ERRNO: declare double @ldexp(double noundef, i32 noundef) [[NOT_READNONE]]
@@ -719,9 +719,9 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
719719
// HAS_ERRNO: declare x86_fp80 @llvm.trunc.f80(x86_fp80) [[READNONE_INTRINSIC]]
720720
};
721721

722-
// NO__ERRNO: attributes [[READNONE]] = { {{.*}}memory(none){{.*}} }
723722
// NO__ERRNO: attributes [[READNONE_INTRINSIC]] = { {{.*}}memory(none){{.*}} }
724723
// NO__ERRNO: attributes [[NOT_READNONE]] = { nounwind {{.*}} }
724+
// NO__ERRNO: attributes [[READNONE]] = { {{.*}}memory(none){{.*}} }
725725
// NO__ERRNO: attributes [[READONLY]] = { {{.*}}memory(read){{.*}} }
726726

727727
// HAS_ERRNO: attributes [[NOT_READNONE]] = { nounwind {{.*}} }

clang/test/CodeGenCXX/builtin-calling-conv.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// RUN: %clang_cc1 -triple x86_64-linux-pc -DREDECL -emit-llvm %s -o - | FileCheck %s -check-prefix LINUX
2-
// RUN: %clang_cc1 -triple spir-unknown-unknown -DREDECL -DSPIR -emit-llvm %s -o - | FileCheck %s -check-prefix SPIR
3-
// RUN: %clang_cc1 -triple x86_64-linux-pc -emit-llvm %s -o - | FileCheck %s -check-prefix LINUX
4-
// RUN: %clang_cc1 -triple spir-unknown-unknown -DSPIR -emit-llvm %s -o - | FileCheck %s -check-prefix SPIR
5-
// RUN: %clang_cc1 -triple i386-windows-pc -fdefault-calling-conv=stdcall -emit-llvm %s -o - | FileCheck %s -check-prefix WIN32
1+
// RUN: %clang_cc1 -triple x86_64-linux-pc -DREDECL -emit-llvm -fmath-errno %s -o - | FileCheck %s -check-prefix LINUX
2+
// RUN: %clang_cc1 -triple spir-unknown-unknown -DREDECL -DSPIR -emit-llvm -fmath-errno %s -o - | FileCheck %s -check-prefix SPIR
3+
// RUN: %clang_cc1 -triple x86_64-linux-pc -emit-llvm -fmath-errno %s -o - | FileCheck %s -check-prefix LINUX
4+
// RUN: %clang_cc1 -triple spir-unknown-unknown -DSPIR -emit-llvm -fmath-errno %s -o - | FileCheck %s -check-prefix SPIR
5+
// RUN: %clang_cc1 -triple i386-windows-pc -fdefault-calling-conv=stdcall -emit-llvm -fmath-errno %s -o - | FileCheck %s -check-prefix WIN32
66

77
#ifdef REDECL
88
namespace std {

clang/test/CodeGenOpenCL/builtins-f16.cl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ void test_half_builtins(half h0, half h1, half h2, int i0) {
1515
// CHECK: call half @llvm.atan.f16(half %h0)
1616
res = __builtin_atanf16(h0);
1717

18+
// CHECK: call half @llvm.atan2.f16(half %h0, half %h1)
19+
res = __builtin_atan2f16(h0, h1);
20+
1821
// CHECK: call half @llvm.copysign.f16(half %h0, half %h1)
1922
res = __builtin_copysignf16(h0, h1);
2023

0 commit comments

Comments
 (0)