Skip to content

Commit 7781e10

Browse files
authored
[clang] Lower non-builtin sincos[f|l] calls to llvm.sincos.* when -fno-math-errno is set (#121763)
This will allow vectorizing these calls (after a few more patches). This should not change the codegen for targets that enable the use of AA during the codegen (in `TargetSubtargetInfo::useAA()`). This includes targets such as AArch64. This notably does not include x86 but can be worked around by passing `-mllvm -combiner-global-alias-analysis=true` to clang. Follow up to #114086.
1 parent 160da73 commit 7781e10

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -3377,11 +3377,16 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
33773377
return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(
33783378
*this, E, Intrinsic::sinh, Intrinsic::experimental_constrained_sinh));
33793379

3380+
case Builtin::BIsincos:
3381+
case Builtin::BIsincosf:
3382+
case Builtin::BIsincosl:
33803383
case Builtin::BI__builtin_sincos:
33813384
case Builtin::BI__builtin_sincosf:
33823385
case Builtin::BI__builtin_sincosf16:
33833386
case Builtin::BI__builtin_sincosl:
33843387
case Builtin::BI__builtin_sincosf128:
3388+
if (Builder.getIsFPConstrained())
3389+
break; // TODO: Emit constrained sincos intrinsic once one exists.
33853390
emitSincosBuiltin(*this, E, Intrinsic::sincos);
33863391
return RValue::get(nullptr);
33873392

clang/test/CodeGen/AArch64/sincos.c

+46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// RUN: %clang_cc1 -triple=aarch64-gnu-linux -emit-llvm -O1 %s -o - | FileCheck --check-prefix=NO-MATH-ERRNO %s
22
// RUN: %clang_cc1 -triple=aarch64-gnu-linux -emit-llvm -fmath-errno %s -o - | FileCheck --check-prefix=MATH-ERRNO %s
33

4+
void sincos(double, double*, double*);
5+
void sincosf(float, float*, float*);
6+
void sincosl(long double, long double*, long double*);
7+
48
// NO-MATH-ERRNO-LABEL: @sincos_f32
59
// NO-MATH-ERRNO: [[SINCOS:%.*]] = tail call { float, float } @llvm.sincos.f32(float {{.*}})
610
// NO-MATH-ERRNO-NEXT: [[SIN:%.*]] = extractvalue { float, float } [[SINCOS]], 0
@@ -12,6 +16,20 @@
1216
// MATH-ERRNO: call void @sincosf(
1317
//
1418
void sincos_f32(float x, float* fp0, float* fp1) {
19+
sincosf(x, fp0, fp1);
20+
}
21+
22+
// NO-MATH-ERRNO-LABEL: @sincos_builtin_f32
23+
// NO-MATH-ERRNO: [[SINCOS:%.*]] = tail call { float, float } @llvm.sincos.f32(float {{.*}})
24+
// NO-MATH-ERRNO-NEXT: [[SIN:%.*]] = extractvalue { float, float } [[SINCOS]], 0
25+
// NO-MATH-ERRNO-NEXT: [[COS:%.*]] = extractvalue { float, float } [[SINCOS]], 1
26+
// NO-MATH-ERRNO-NEXT: store float [[SIN]], ptr {{.*}}, align 4, !alias.scope [[SINCOS_ALIAS_SCOPE:![0-9]+]]
27+
// NO-MATH-ERRNO-NEXT: store float [[COS]], ptr {{.*}}, align 4, !noalias [[SINCOS_ALIAS_SCOPE]]
28+
//
29+
// MATH-ERRNO-LABEL: @sincos_builtin_f32
30+
// MATH-ERRNO: call void @sincosf(
31+
//
32+
void sincos_builtin_f32(float x, float* fp0, float* fp1) {
1533
__builtin_sincosf(x, fp0, fp1);
1634
}
1735

@@ -26,6 +44,20 @@ void sincos_f32(float x, float* fp0, float* fp1) {
2644
// MATH-ERRNO: call void @sincos(
2745
//
2846
void sincos_f64(double x, double* dp0, double* dp1) {
47+
sincos(x, dp0, dp1);
48+
}
49+
50+
// NO-MATH-ERRNO-LABEL: @sincos_builtin_f64
51+
// NO-MATH-ERRNO: [[SINCOS:%.*]] = tail call { double, double } @llvm.sincos.f64(double {{.*}})
52+
// NO-MATH-ERRNO-NEXT: [[SIN:%.*]] = extractvalue { double, double } [[SINCOS]], 0
53+
// NO-MATH-ERRNO-NEXT: [[COS:%.*]] = extractvalue { double, double } [[SINCOS]], 1
54+
// NO-MATH-ERRNO-NEXT: store double [[SIN]], ptr {{.*}}, align 8, !alias.scope [[SINCOS_ALIAS_SCOPE:![0-9]+]]
55+
// NO-MATH-ERRNO-NEXT: store double [[COS]], ptr {{.*}}, align 8, !noalias [[SINCOS_ALIAS_SCOPE]]
56+
//
57+
// MATH-ERRNO-LABEL: @sincos_builtin_f64
58+
// MATH-ERRNO: call void @sincos(
59+
//
60+
void sincos_builtin_f64(double x, double* dp0, double* dp1) {
2961
__builtin_sincos(x, dp0, dp1);
3062
}
3163

@@ -40,5 +72,19 @@ void sincos_f64(double x, double* dp0, double* dp1) {
4072
// MATH-ERRNO: call void @sincosl(
4173
//
4274
void sincos_f128(long double x, long double* ldp0, long double* ldp1) {
75+
sincosl(x, ldp0, ldp1);
76+
}
77+
78+
// NO-MATH-ERRNO-LABEL: @sincos_builtin_f128
79+
// NO-MATH-ERRNO: [[SINCOS:%.*]] = tail call { fp128, fp128 } @llvm.sincos.f128(fp128 {{.*}})
80+
// NO-MATH-ERRNO-NEXT: [[SIN:%.*]] = extractvalue { fp128, fp128 } [[SINCOS]], 0
81+
// NO-MATH-ERRNO-NEXT: [[COS:%.*]] = extractvalue { fp128, fp128 } [[SINCOS]], 1
82+
// NO-MATH-ERRNO-NEXT: store fp128 [[SIN]], ptr {{.*}}, align 16, !alias.scope [[SINCOS_ALIAS_SCOPE:![0-9]+]]
83+
// NO-MATH-ERRNO-NEXT: store fp128 [[COS]], ptr {{.*}}, align 16, !noalias [[SINCOS_ALIAS_SCOPE]]
84+
//
85+
// MATH-ERRNO-LABEL: @sincos_builtin_f128
86+
// MATH-ERRNO: call void @sincosl(
87+
//
88+
void sincos_builtin_f128(long double x, long double* ldp0, long double* ldp1) {
4389
__builtin_sincosl(x, ldp0, ldp1);
4490
}

clang/test/CodeGen/math-libcalls.c

+11
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,17 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
660660
// HAS_MAYTRAP: declare float @llvm.experimental.constrained.sinh.f32(
661661
// HAS_MAYTRAP: declare x86_fp80 @llvm.experimental.constrained.sinh.f80(
662662

663+
sincos(f, d, d); sincosf(f, fp, fp); sincosl(f, l, l);
664+
665+
// NO__ERRNO: declare { double, double } @llvm.sincos.f64(double) [[READNONE_INTRINSIC]]
666+
// NO__ERRNO: declare { float, float } @llvm.sincos.f32(float) [[READNONE_INTRINSIC]]
667+
// NO__ERRNO: declare { x86_fp80, x86_fp80 } @llvm.sincos.f80(x86_fp80) [[READNONE_INTRINSIC]]
668+
// HAS_ERRNO: declare void @sincos(double noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
669+
// HAS_ERRNO: declare void @sincosf(float noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
670+
// HAS_ERRNO: declare void @sincosl(x86_fp80 noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
671+
// HAS_MAYTRAP: declare void @sincos(double noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
672+
// HAS_MAYTRAP: declare void @sincosf(float noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
673+
// HAS_MAYTRAP: declare void @sincosl(x86_fp80 noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
663674

664675
sqrt(f); sqrtf(f); sqrtl(f);
665676

0 commit comments

Comments
 (0)