Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9df94e2

Browse files
authoredOct 4, 2024··
[clang][HLSL] Add radians intrinsic (#110802)
partially fixes #99151 ### Changes * Implemented `radians` clang builtin * Linked `radians` clang builtin with `hlsl_intrinsics.h` * Added sema checks for `radians` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp` * Add codegen for `radians` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp` * Add codegen tests to `clang/test/CodeGenHLSL/builtins/radians.hlsl` * Add sema tests to `clang/test/SemaHLSL/BuiltIns/radians-errors.hlsl` ### Related PRs * [[DXIL] Add radians intrinsic #110616](#110616) * [[SPIRV] Add radians intrinsic #110800](#110800)
1 parent 7692d10 commit 9df94e2

File tree

8 files changed

+141
-0
lines changed

8 files changed

+141
-0
lines changed
 

‎clang/include/clang/Basic/Builtins.td

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

4827+
def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
4828+
let Spellings = ["__builtin_hlsl_elementwise_radians"];
4829+
let Attributes = [NoThrow, Const];
4830+
let Prototype = "void(...)";
4831+
}
4832+
48274833
// Builtins for XRay.
48284834
def XRayCustomEvent : Builtin {
48294835
let Spellings = ["__xray_customevent"];

‎clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18892,6 +18892,15 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
1889218892
retType, CGM.getHLSLRuntime().getSignIntrinsic(),
1889318893
ArrayRef<Value *>{Op0}, nullptr, "hlsl.sign");
1889418894
}
18895+
case Builtin::BI__builtin_hlsl_elementwise_radians: {
18896+
Value *Op0 = EmitScalarExpr(E->getArg(0));
18897+
assert(E->getArg(0)->getType()->hasFloatingRepresentation() &&
18898+
"radians operand must have a float representation");
18899+
return Builder.CreateIntrinsic(
18900+
/*ReturnType=*/Op0->getType(),
18901+
CGM.getHLSLRuntime().getRadiansIntrinsic(), ArrayRef<Value *>{Op0},
18902+
nullptr, "hlsl.radians");
18903+
}
1889518904
}
1889618905
return nullptr;
1889718906
}

‎clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class CGHLSLRuntime {
8383
GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
8484
GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
8585
GENERATE_HLSL_INTRINSIC_FUNCTION(Step, step)
86+
GENERATE_HLSL_INTRINSIC_FUNCTION(Radians, radians)
8687
GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
8788
GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
8889
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
@@ -2138,5 +2138,35 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
21382138
int3 sign(double3);
21392139
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
21402140
int4 sign(double4);
2141+
2142+
//===----------------------------------------------------------------------===//
2143+
// radians builtins
2144+
//===----------------------------------------------------------------------===//
2145+
2146+
/// \fn T radians(T Val)
2147+
/// \brief Converts the specified value from degrees to radians.
2148+
2149+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2150+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2151+
half radians(half);
2152+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2153+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2154+
half2 radians(half2);
2155+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2156+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2157+
half3 radians(half3);
2158+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
2159+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2160+
half4 radians(half4);
2161+
2162+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2163+
float radians(float);
2164+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2165+
float2 radians(float2);
2166+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2167+
float3 radians(float3);
2168+
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
2169+
float4 radians(float4);
2170+
21412171
} // namespace hlsl
21422172
#endif //_HLSL_HLSL_INTRINSICS_H_

‎clang/lib/Sema/SemaHLSL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
18961896
return true;
18971897
break;
18981898
}
1899+
case Builtin::BI__builtin_hlsl_elementwise_radians:
18991900
case Builtin::BI__builtin_hlsl_elementwise_rsqrt:
19001901
case Builtin::BI__builtin_hlsl_elementwise_frac: {
19011902
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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: -DTARGET=dx -DFNATTRS=noundef
6+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
7+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
8+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF \
9+
// RUN: -DTARGET=dx -DFNATTRS=noundef
10+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
11+
// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
12+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
13+
// RUN: --check-prefixes=CHECK,NATIVE_HALF \
14+
// RUN: -DTARGET=spv -DFNATTRS="spir_func noundef"
15+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
16+
// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
17+
// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF \
18+
// RUN: -DTARGET=spv -DFNATTRS="spir_func noundef"
19+
20+
21+
// NATIVE_HALF: define [[FNATTRS]] half @
22+
// NATIVE_HALF: %{{.*}} = call half @llvm.[[TARGET]].radians.f16(
23+
// NATIVE_HALF: ret half %{{.*}}
24+
// NO_HALF: define [[FNATTRS]] float @
25+
// NO_HALF: %{{.*}} = call float @llvm.[[TARGET]].radians.f32(
26+
// NO_HALF: ret float %{{.*}}
27+
half test_radians_half(half p0) { return radians(p0); }
28+
// NATIVE_HALF: define [[FNATTRS]] <2 x half> @
29+
// NATIVE_HALF: %{{.*}} = call <2 x half> @llvm.[[TARGET]].radians.v2f16
30+
// NATIVE_HALF: ret <2 x half> %{{.*}}
31+
// NO_HALF: define [[FNATTRS]] <2 x float> @
32+
// NO_HALF: %{{.*}} = call <2 x float> @llvm.[[TARGET]].radians.v2f32(
33+
// NO_HALF: ret <2 x float> %{{.*}}
34+
half2 test_radians_half2(half2 p0) { return radians(p0); }
35+
// NATIVE_HALF: define [[FNATTRS]] <3 x half> @
36+
// NATIVE_HALF: %{{.*}} = call <3 x half> @llvm.[[TARGET]].radians.v3f16
37+
// NATIVE_HALF: ret <3 x half> %{{.*}}
38+
// NO_HALF: define [[FNATTRS]] <3 x float> @
39+
// NO_HALF: %{{.*}} = call <3 x float> @llvm.[[TARGET]].radians.v3f32(
40+
// NO_HALF: ret <3 x float> %{{.*}}
41+
half3 test_radians_half3(half3 p0) { return radians(p0); }
42+
// NATIVE_HALF: define [[FNATTRS]] <4 x half> @
43+
// NATIVE_HALF: %{{.*}} = call <4 x half> @llvm.[[TARGET]].radians.v4f16
44+
// NATIVE_HALF: ret <4 x half> %{{.*}}
45+
// NO_HALF: define [[FNATTRS]] <4 x float> @
46+
// NO_HALF: %{{.*}} = call <4 x float> @llvm.[[TARGET]].radians.v4f32(
47+
// NO_HALF: ret <4 x float> %{{.*}}
48+
half4 test_radians_half4(half4 p0) { return radians(p0); }
49+
50+
// CHECK: define [[FNATTRS]] float @
51+
// CHECK: %{{.*}} = call float @llvm.[[TARGET]].radians.f32(
52+
// CHECK: ret float %{{.*}}
53+
float test_radians_float(float p0) { return radians(p0); }
54+
// CHECK: define [[FNATTRS]] <2 x float> @
55+
// CHECK: %{{.*}} = call <2 x float> @llvm.[[TARGET]].radians.v2f32
56+
// CHECK: ret <2 x float> %{{.*}}
57+
float2 test_radians_float2(float2 p0) { return radians(p0); }
58+
// CHECK: define [[FNATTRS]] <3 x float> @
59+
// CHECK: %{{.*}} = call <3 x float> @llvm.[[TARGET]].radians.v3f32
60+
// CHECK: ret <3 x float> %{{.*}}
61+
float3 test_radians_float3(float3 p0) { return radians(p0); }
62+
// CHECK: define [[FNATTRS]] <4 x float> @
63+
// CHECK: %{{.*}} = call <4 x float> @llvm.[[TARGET]].radians.v4f32
64+
// CHECK: ret <4 x float> %{{.*}}
65+
float4 test_radians_float4(float4 p0) { return radians(p0); }
66+

‎clang/test/SemaHLSL/BuiltIns/half-float-only-errors.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_tan
1818
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_tanh
1919
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -DTEST_FUNC=__builtin_elementwise_trunc
20+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -DTEST_FUNC=__builtin_hlsl_elementwise_radians
2021

2122
double test_double_builtin(double p0) {
2223
return TEST_FUNC(p0);
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)
Please sign in to comment.