Skip to content

Commit be88a59

Browse files
committed
Added radians to clang
1 parent 4980f21 commit be88a59

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,12 @@ def HLSLStep: LangBuiltin<"HLSL_LANG"> {
47884788
let Prototype = "void(...)";
47894789
}
47904790

4791+
def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
4792+
let Spellings = ["__builtin_hlsl_elementwise_radians"];
4793+
let Attributes = [NoThrow, Const];
4794+
let Prototype = "void(...)";
4795+
}
4796+
47914797
// Builtins for XRay.
47924798
def XRayCustomEvent : Builtin {
47934799
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18852,6 +18852,14 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1885218852
retType, CGM.getHLSLRuntime().getSignIntrinsic(),
1885318853
ArrayRef<Value *>{Op0}, nullptr, "hlsl.sign");
1885418854
}
18855+
case Builtin::BI__builtin_hlsl_elementwise_radians: {
18856+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18857+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18858+
llvm_unreachable("radians operand must have a float representation");
18859+
return Builder.CreateIntrinsic(
18860+
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getRadiansIntrinsic(),
18861+
ArrayRef<Value *>{Op0}, nullptr, "hlsl.radians");
18862+
}
1885518863
}
1885618864
return nullptr;
1885718865
}

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class CGHLSLRuntime {
8282
GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
8383
GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
8484
GENERATE_HLSL_INTRINSIC_FUNCTION(Step, step)
85+
GENERATE_HLSL_INTRINSIC_FUNCTION(Radians, radians)
8586
GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
8687
GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
8788
GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,5 +2086,35 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
20862086
int3 sign(double3);
20872087
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
20882088
int4 sign(double4);
2089+
2090+
//===----------------------------------------------------------------------===//
2091+
// radians builtins
2092+
//===----------------------------------------------------------------------===//
2093+
2094+
/// \fn T radians(T Val)
2095+
/// \brief Converts the specified value from degrees to radians.
2096+
2097+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2098+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2099+
half radians(half);
2100+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2101+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2102+
half2 radians(half2);
2103+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2104+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2105+
half3 radians(half3);
2106+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2107+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2108+
half4 radians(half4);
2109+
2110+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2111+
float radians(float);
2112+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2113+
float2 radians(float2);
2114+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2115+
float3 radians(float3);
2116+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2117+
float4 radians(float4);
2118+
20892119
} // namespace hlsl
20902120
#endif //_HLSL_HLSL_INTRINSICS_H_

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
18611861
return true;
18621862
break;
18631863
}
1864+
case Builtin::BI__builtin_hlsl_elementwise_radians:
18641865
case Builtin::BI__builtin_hlsl_elementwise_rsqrt:
18651866
case Builtin::BI__builtin_hlsl_elementwise_frac: {
18661867
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
4+
// RUN: --check-prefixes=CHECK,NATIVE_HALF
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
7+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF
8+
9+
// NATIVE_HALF: define noundef half @
10+
// NATIVE_HALF: %{{.*}} = call half @llvm.dx.radians.f16(
11+
// NATIVE_HALF: ret half %{{.*}}
12+
// NO_HALF: define noundef float @"?test_radians_half@@YA$halff@$halff@@Z"(
13+
// NO_HALF: %{{.*}} = call float @llvm.dx.radians.f32(
14+
// NO_HALF: ret float %{{.*}}
15+
half test_radians_half(half p0) { return radians(p0); }
16+
// NATIVE_HALF: define noundef <2 x half> @
17+
// NATIVE_HALF: %{{.*}} = call <2 x half> @llvm.dx.radians.v2f16
18+
// NATIVE_HALF: ret <2 x half> %{{.*}}
19+
// NO_HALF: define noundef <2 x float> @
20+
// NO_HALF: %{{.*}} = call <2 x float> @llvm.dx.radians.v2f32(
21+
// NO_HALF: ret <2 x float> %{{.*}}
22+
half2 test_radians_half2(half2 p0) { return radians(p0); }
23+
// NATIVE_HALF: define noundef <3 x half> @
24+
// NATIVE_HALF: %{{.*}} = call <3 x half> @llvm.dx.radians.v3f16
25+
// NATIVE_HALF: ret <3 x half> %{{.*}}
26+
// NO_HALF: define noundef <3 x float> @
27+
// NO_HALF: %{{.*}} = call <3 x float> @llvm.dx.radians.v3f32(
28+
// NO_HALF: ret <3 x float> %{{.*}}
29+
half3 test_radians_half3(half3 p0) { return radians(p0); }
30+
// NATIVE_HALF: define noundef <4 x half> @
31+
// NATIVE_HALF: %{{.*}} = call <4 x half> @llvm.dx.radians.v4f16
32+
// NATIVE_HALF: ret <4 x half> %{{.*}}
33+
// NO_HALF: define noundef <4 x float> @
34+
// NO_HALF: %{{.*}} = call <4 x float> @llvm.dx.radians.v4f32(
35+
// NO_HALF: ret <4 x float> %{{.*}}
36+
half4 test_radians_half4(half4 p0) { return radians(p0); }
37+
38+
// CHECK: define noundef float @
39+
// CHECK: %{{.*}} = call float @llvm.dx.radians.f32(
40+
// CHECK: ret float %{{.*}}
41+
float test_radians_float(float p0) { return radians(p0); }
42+
// CHECK: define noundef <2 x float> @
43+
// CHECK: %{{.*}} = call <2 x float> @llvm.dx.radians.v2f32
44+
// CHECK: ret <2 x float> %{{.*}}
45+
float2 test_radians_float2(float2 p0) { return radians(p0); }
46+
// CHECK: define noundef <3 x float> @
47+
// CHECK: %{{.*}} = call <3 x float> @llvm.dx.radians.v3f32
48+
// CHECK: ret <3 x float> %{{.*}}
49+
float3 test_radians_float3(float3 p0) { return radians(p0); }
50+
// CHECK: define noundef <4 x float> @
51+
// CHECK: %{{.*}} = call <4 x float> @llvm.dx.radians.v4f32
52+
// CHECK: ret <4 x float> %{{.*}}
53+
float4 test_radians_float4(float4 p0) { return radians(p0); }
54+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected
2+
3+
float test_too_few_arg() {
4+
return __builtin_hlsl_elementwise_radians();
5+
// expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
6+
}
7+
8+
float2 test_too_many_arg(float2 p0) {
9+
return __builtin_hlsl_elementwise_radians(p0, p0);
10+
// expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
11+
}
12+
13+
float builtin_bool_to_float_type_promotion(bool p1) {
14+
return __builtin_hlsl_elementwise_radians(p1);
15+
// expected-error@-1 {passing 'bool' to parameter of incompatible type 'float'}}
16+
}
17+
18+
float builtin_radians_int_to_float_promotion(int p1) {
19+
return __builtin_hlsl_elementwise_radians(p1);
20+
// expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
21+
}
22+
23+
float2 builtin_radians_int2_to_float2_promotion(int2 p1) {
24+
return __builtin_hlsl_elementwise_radians(p1);
25+
// expected-error@-1 {{passing 'int2' (aka 'vector<int, 2>') to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(float)))) float' (vector of 2 'float' values)}}
26+
}
27+

0 commit comments

Comments
 (0)