Skip to content

Commit 7fd19ee

Browse files
committed
[clang] Add sincos builtin using llvm.sincos intrinsic
This registers `sincos[f|l]` as a clang builtin and updates GCBuiltin to emit the `llvm.sincos.*` intrinsic when `-fno-math-errno` is set.
1 parent 652988b commit 7fd19ee

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

clang/include/clang/Basic/Builtins.td

+13
Original file line numberDiff line numberDiff line change
@@ -3562,6 +3562,19 @@ def Frexp : FPMathTemplate, LibBuiltin<"math.h"> {
35623562
let AddBuiltinPrefixedAlias = 1;
35633563
}
35643564

3565+
def Sincos : FPMathTemplate, GNULibBuiltin<"math.h"> {
3566+
let Spellings = ["sincos"];
3567+
let Attributes = [NoThrow];
3568+
let Prototype = "void(T, T*, T*)";
3569+
let AddBuiltinPrefixedAlias = 1;
3570+
}
3571+
3572+
def SincosF16F128 : F16F128MathTemplate, Builtin {
3573+
let Spellings = ["__builtin_sincos"];
3574+
let Attributes = [FunctionWithBuiltinPrefix, NoThrow];
3575+
let Prototype = "void(T, T*, T*)";
3576+
}
3577+
35653578
def Ldexp : FPMathTemplate, LibBuiltin<"math.h"> {
35663579
let Spellings = ["ldexp"];
35673580
let Attributes = [NoThrow, ConstIgnoringErrnoAndExceptions];

clang/lib/CodeGen/CGBuiltin.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,38 @@ static Value *emitFrexpBuiltin(CodeGenFunction &CGF, const CallExpr *E,
722722
return CGF.Builder.CreateExtractValue(Call, 0);
723723
}
724724

725+
static void emitSincosBuiltin(CodeGenFunction &CGF, const CallExpr *E,
726+
llvm::Intrinsic::ID IntrinsicID) {
727+
llvm::Value *Val = CGF.EmitScalarExpr(E->getArg(0));
728+
llvm::Value *Dest0 = CGF.EmitScalarExpr(E->getArg(1));
729+
llvm::Value *Dest1 = CGF.EmitScalarExpr(E->getArg(2));
730+
731+
llvm::Function *F = CGF.CGM.getIntrinsic(IntrinsicID, {Val->getType()});
732+
llvm::Value *Call = CGF.Builder.CreateCall(F, Val);
733+
734+
llvm::Value *SinResult = CGF.Builder.CreateExtractValue(Call, 0);
735+
llvm::Value *CosResult = CGF.Builder.CreateExtractValue(Call, 1);
736+
737+
QualType DestPtrType = E->getArg(1)->getType()->getPointeeType();
738+
LValue SinLV = CGF.MakeNaturalAlignAddrLValue(Dest0, DestPtrType);
739+
LValue CosLV = CGF.MakeNaturalAlignAddrLValue(Dest1, DestPtrType);
740+
741+
llvm::StoreInst *StoreSin =
742+
CGF.Builder.CreateStore(SinResult, SinLV.getAddress());
743+
llvm::StoreInst *StoreCos =
744+
CGF.Builder.CreateStore(CosResult, CosLV.getAddress());
745+
746+
// Mark the two stores as non-aliasing with eachother. The order of stores
747+
// emitted by this builtin is arbitrary, enforcing a particular order will
748+
// prevent optimizations later on.
749+
llvm::MDBuilder MDHelper(CGF.getLLVMContext());
750+
MDNode *Domain = MDHelper.createAnonymousAliasScopeDomain();
751+
MDNode *AliasScope = MDHelper.createAnonymousAliasScope(Domain);
752+
MDNode *AliasScopeList = MDNode::get(Call->getContext(), AliasScope);
753+
StoreSin->setMetadata(LLVMContext::MD_alias_scope, AliasScopeList);
754+
StoreCos->setMetadata(LLVMContext::MD_noalias, AliasScopeList);
755+
}
756+
725757
/// EmitFAbs - Emit a call to @llvm.fabs().
726758
static Value *EmitFAbs(CodeGenFunction &CGF, Value *V) {
727759
Function *F = CGF.CGM.getIntrinsic(Intrinsic::fabs, V->getType());
@@ -3094,6 +3126,17 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
30943126
return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(
30953127
*this, E, Intrinsic::sinh, Intrinsic::experimental_constrained_sinh));
30963128

3129+
case Builtin::BIsincos:
3130+
case Builtin::BIsincosf:
3131+
case Builtin::BIsincosl:
3132+
case Builtin::BI__builtin_sincos:
3133+
case Builtin::BI__builtin_sincosf:
3134+
case Builtin::BI__builtin_sincosl:
3135+
case Builtin::BI__builtin_sincosf128:
3136+
case Builtin::BI__builtin_sincosf16:
3137+
emitSincosBuiltin(*this, E, Intrinsic::sincos);
3138+
return RValue::get(nullptr);
3139+
30973140
case Builtin::BIsqrt:
30983141
case Builtin::BIsqrtf:
30993142
case Builtin::BIsqrtl:

clang/test/CodeGen/AArch64/sincos.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang_cc1 -triple=aarch64-gnu-linux -emit-llvm %s -o - | FileCheck --check-prefix=NO-MATH-ERRNO %s
2+
// RUN: %clang_cc1 -triple=aarch64-gnu-linux -emit-llvm -fmath-errno %s -o - | FileCheck --check-prefix=MATH-ERRNO %s
3+
4+
void sincos(double, double*, double*);
5+
void sincosf(float, float*, float*);
6+
7+
// NO-MATH-ERRNO-LABEL: @foo
8+
// NO-MATH-ERRNO: [[SINCOS:%.*]] = call { double, double } @llvm.sincos.f64(double {{.*}})
9+
// NO-MATH-ERRNO-NEXT: [[SIN:%.*]] = extractvalue { double, double } [[SINCOS]], 0
10+
// NO-MATH-ERRNO-NEXT: [[COS:%.*]] = extractvalue { double, double } [[SINCOS]], 1
11+
// NO-MATH-ERRNO-NEXT: store double [[SIN]], ptr {{.*}}, align 8, !alias.scope [[SINCOS_ALIAS_SCOPE:![0-9]+]]
12+
// NO-MATH-ERRNO-NEXT: store double [[COS]], ptr {{.*}}, align 8, !noalias [[SINCOS_ALIAS_SCOPE]]
13+
//
14+
// MATH-ERRNO-LABEL: @foo
15+
// MATH-ERRNO: call void @sincos(
16+
//
17+
void foo(double x, double* dp0, double* dp1) {
18+
sincos(x, dp0, dp1);
19+
}
20+
21+
// NO-MATH-ERRNO-LABEL: @bar
22+
// NO-MATH-ERRNO: [[SINCOS:%.*]] = call { float, float } @llvm.sincos.f32(float {{.*}})
23+
// NO-MATH-ERRNO-NEXT: [[SIN:%.*]] = extractvalue { float, float } [[SINCOS]], 0
24+
// NO-MATH-ERRNO-NEXT: [[COS:%.*]] = extractvalue { float, float } [[SINCOS]], 1
25+
// NO-MATH-ERRNO-NEXT: store float [[SIN]], ptr {{.*}}, align 4, !alias.scope [[SINCOS_ALIAS_SCOPE:![0-9]+]]
26+
// NO-MATH-ERRNO-NEXT: store float [[COS]], ptr {{.*}}, align 4, !noalias [[SINCOS_ALIAS_SCOPE]]
27+
//
28+
// MATH-ERRNO-LABEL: @bar
29+
// MATH-ERRNO: call void @sincosf(
30+
//
31+
void bar(float x, float* fp0, float* fp1) {
32+
sincosf(x, fp0, fp1);
33+
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ void foo(double *d, float f, float *fp, long double *l, int *i, const char *c) {
3838
// NO__ERRNO-NEXT: [[FREXP_F128_0:%.+]] = extractvalue { fp128, i32 } [[FREXP_F128]], 0
3939

4040

41+
// NO__ERRNO: [[SINCOS_F64:%.+]] = call { double, double } @llvm.sincos.f64(double %{{.+}})
42+
// NO__ERRNO-NEXT: [[SINCOS_F64_0:%.+]] = extractvalue { double, double } [[SINCOS_F64]], 0
43+
// NO__ERRNO-NEXT: [[SINCOS_F64_1:%.+]] = extractvalue { double, double } [[SINCOS_F64]], 1
44+
// NO__ERRNO-NEXT: store double [[SINCOS_F64_0]], ptr %{{.+}}, align 8
45+
// NO__ERRNO-NEXT: store double [[SINCOS_F64_1]], ptr %{{.+}}, align 8
46+
47+
// NO__ERRNO: [[SINCOS_F32:%.+]] = call { float, float } @llvm.sincos.f32(float %{{.+}})
48+
// NO__ERRNO-NEXT: [[SINCOS_F32_0:%.+]] = extractvalue { float, float } [[SINCOS_F32]], 0
49+
// NO__ERRNO-NEXT: [[SINCOS_F32_1:%.+]] = extractvalue { float, float } [[SINCOS_F32]], 1
50+
// NO__ERRNO-NEXT: store float [[SINCOS_F32_0]], ptr %{{.+}}, align 4
51+
// NO__ERRNO-NEXT: store float [[SINCOS_F32_1]], ptr %{{.+}}, align 4
52+
53+
// NO__ERRNO: [[SINCOS_F80:%.+]] = call { x86_fp80, x86_fp80 } @llvm.sincos.f80(x86_fp80 %{{.+}})
54+
// NO__ERRNO-NEXT: [[SINCOS_F80_0:%.+]] = extractvalue { x86_fp80, x86_fp80 } [[SINCOS_F80]], 0
55+
// NO__ERRNO-NEXT: [[SINCOS_F80_1:%.+]] = extractvalue { x86_fp80, x86_fp80 } [[SINCOS_F80]], 1
56+
// NO__ERRNO-NEXT: store x86_fp80 [[SINCOS_F80_0]], ptr %{{.+}}, align 16
57+
// NO__ERRNO-NEXT: store x86_fp80 [[SINCOS_F80_1]], ptr %{{.+}}, align 16
58+
59+
// NO__ERRNO: [[SINCOS_F128:%.+]] = call { fp128, fp128 } @llvm.sincos.f128(fp128 %{{.+}})
60+
// NO__ERRNO-NEXT: [[SINCOS_F128_0:%.+]] = extractvalue { fp128, fp128 } [[SINCOS_F128]], 0
61+
// NO__ERRNO-NEXT: [[SINCOS_F128_1:%.+]] = extractvalue { fp128, fp128 } [[SINCOS_F128]], 1
62+
// NO__ERRNO-NEXT: store fp128 [[SINCOS_F128_0]], ptr %{{.+}}, align 16
63+
// NO__ERRNO-NEXT: store fp128 [[SINCOS_F128_1]], ptr %{{.+}}, align 16
64+
65+
4166
// HAS_ERRNO: declare double @fmod(double noundef, double noundef) [[NOT_READNONE:#[0-9]+]]
4267
// HAS_ERRNO: declare float @fmodf(float noundef, float noundef) [[NOT_READNONE]]
4368
// HAS_ERRNO: declare x86_fp80 @fmodl(x86_fp80 noundef, x86_fp80 noundef) [[NOT_READNONE]]
@@ -665,6 +690,16 @@ __builtin_sinh(f); __builtin_sinhf(f); __builtin_sinhl(f); __builtin_
665690
// HAS_ERRNO: declare x86_fp80 @sinhl(x86_fp80 noundef) [[NOT_READNONE]]
666691
// HAS_ERRNO: declare fp128 @sinhf128(fp128 noundef) [[NOT_READNONE]]
667692

693+
__builtin_sincos(f,d,d); __builtin_sincosf(f,fp,fp); __builtin_sincosl(f,l,l); __builtin_sincosf128(f,l,l);
694+
// NO__ERRNO: declare { double, double } @llvm.sincos.f64(double) [[READNONE_INTRINSIC]]
695+
// NO__ERRNO: declare { float, float } @llvm.sincos.f32(float) [[READNONE_INTRINSIC]]
696+
// NO__ERRNO: declare { x86_fp80, x86_fp80 } @llvm.sincos.f80(x86_fp80) [[READNONE_INTRINSIC]]
697+
// NO__ERRNO: declare { fp128, fp128 } @llvm.sincos.f128(fp128) [[READNONE_INTRINSIC]]
698+
// HAS_ERRNO: declare void @sincos(double noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
699+
// HAS_ERRNO: declare void @sincosf(float noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
700+
// HAS_ERRNO: declare void @sincosl(x86_fp80 noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
701+
// HAS_ERRNO: declare void @sincosf128(fp128 noundef, ptr noundef, ptr noundef) [[NOT_READNONE]]
702+
668703
__builtin_sqrt(f); __builtin_sqrtf(f); __builtin_sqrtl(f); __builtin_sqrtf128(f);
669704

670705
// NO__ERRNO: declare double @llvm.sqrt.f64(double) [[READNONE_INTRINSIC]]

clang/test/OpenMP/declare_simd_aarch64.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// REQUIRES: aarch64-registered-target
22
// -fopemp and -fopenmp-simd behavior are expected to be the same.
33

4-
// RUN: %clang_cc1 -triple aarch64-linux-gnu -target-feature +neon -fopenmp -x c -emit-llvm %s -o - -femit-all-decls | FileCheck %s --check-prefix=AARCH64
5-
// RUN: %clang_cc1 -triple aarch64-linux-gnu -target-feature +neon -fopenmp-simd -x c -emit-llvm %s -o - -femit-all-decls | FileCheck %s --check-prefix=AARCH64
4+
// RUN: %clang_cc1 -triple aarch64-linux-gnu -target-feature +neon -fmath-errno -fopenmp -x c -emit-llvm %s -o - -femit-all-decls | FileCheck %s --check-prefix=AARCH64
5+
// RUN: %clang_cc1 -triple aarch64-linux-gnu -target-feature +neon -fmath-errno -fopenmp-simd -x c -emit-llvm %s -o - -femit-all-decls | FileCheck %s --check-prefix=AARCH64
66

77
#pragma omp declare simd
88
#pragma omp declare simd simdlen(2)

0 commit comments

Comments
 (0)