Skip to content

Commit 08f0b8a

Browse files
committed
Added radians to clang
1 parent e379b4b commit 08f0b8a

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
@@ -4818,6 +4818,12 @@ def HLSLStep: LangBuiltin<"HLSL_LANG"> {
48184818
let Prototype = "void(...)";
48194819
}
48204820

4821+
def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
4822+
let Spellings = ["__builtin_hlsl_elementwise_radians"];
4823+
let Attributes = [NoThrow, Const];
4824+
let Prototype = "void(...)";
4825+
}
4826+
48214827
// Builtins for XRay.
48224828
def XRayCustomEvent : Builtin {
48234829
let Spellings = ["__xray_customevent"];

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18877,6 +18877,14 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1887718877
retType, CGM.getHLSLRuntime().getSignIntrinsic(),
1887818878
ArrayRef<Value *>{Op0}, nullptr, "hlsl.sign");
1887918879
}
18880+
case Builtin::BI__builtin_hlsl_elementwise_radians: {
18881+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18882+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
18883+
llvm_unreachable("radians operand must have a float representation");
18884+
return Builder.CreateIntrinsic(
18885+
/*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getRadiansIntrinsic(),
18886+
ArrayRef<Value *>{Op0}, nullptr, "hlsl.radians");
18887+
}
1888018888
}
1888118889
return nullptr;
1888218890
}

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
@@ -2116,5 +2116,35 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
21162116
int3 sign(double3);
21172117
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
21182118
int4 sign(double4);
2119+
2120+
//===----------------------------------------------------------------------===//
2121+
// radians builtins
2122+
//===----------------------------------------------------------------------===//
2123+
2124+
/// \fn T radians(T Val)
2125+
/// \brief Converts the specified value from degrees to radians.
2126+
2127+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2128+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2129+
half radians(half);
2130+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2131+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2132+
half2 radians(half2);
2133+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2134+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2135+
half3 radians(half3);
2136+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2137+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2138+
half4 radians(half4);
2139+
2140+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2141+
float radians(float);
2142+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2143+
float2 radians(float2);
2144+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2145+
float3 radians(float3);
2146+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2147+
float4 radians(float4);
2148+
21192149
} // namespace hlsl
21202150
#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)