-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][HLSL] Add radians intrinsic #110802
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-hlsl @llvm/pr-subscribers-clang Author: Adam Yang (adam-yang) Changespartially fixes #99151 Changes
Related PRsFull diff: https://github.com/llvm/llvm-project/pull/110802.diff 7 Files Affected:
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 8090119e512fbb..09b130e0def0d7 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4788,6 +4788,12 @@ def HLSLStep: LangBuiltin<"HLSL_LANG"> {
let Prototype = "void(...)";
}
+def HLSLRadians : LangBuiltin<"HLSL_LANG"> {
+ let Spellings = ["__builtin_hlsl_elementwise_radians"];
+ let Attributes = [NoThrow, Const];
+ let Prototype = "void(...)";
+}
+
// Builtins for XRay.
def XRayCustomEvent : Builtin {
let Spellings = ["__xray_customevent"];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index d739597de4c855..6c24c66a81e73c 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18852,6 +18852,14 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
retType, CGM.getHLSLRuntime().getSignIntrinsic(),
ArrayRef<Value *>{Op0}, nullptr, "hlsl.sign");
}
+ case Builtin::BI__builtin_hlsl_elementwise_radians: {
+ Value *Op0 = EmitScalarExpr(E->getArg(0));
+ if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+ llvm_unreachable("radians operand must have a float representation");
+ return Builder.CreateIntrinsic(
+ /*ReturnType=*/Op0->getType(), CGM.getHLSLRuntime().getRadiansIntrinsic(),
+ ArrayRef<Value *>{Op0}, nullptr, "hlsl.radians");
+ }
}
return nullptr;
}
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index a8aabca7348ffb..c807a3216ec9ee 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -82,6 +82,7 @@ class CGHLSLRuntime {
GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
GENERATE_HLSL_INTRINSIC_FUNCTION(Sign, sign)
GENERATE_HLSL_INTRINSIC_FUNCTION(Step, step)
+ GENERATE_HLSL_INTRINSIC_FUNCTION(Radians, radians)
GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
GENERATE_HLSL_INTRINSIC_FUNCTION(FDot, fdot)
GENERATE_HLSL_INTRINSIC_FUNCTION(SDot, sdot)
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 810a16d75f0228..81b297723da3f5 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -2086,5 +2086,35 @@ _HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
int3 sign(double3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_sign)
int4 sign(double4);
+
+//===----------------------------------------------------------------------===//
+// radians builtins
+//===----------------------------------------------------------------------===//
+
+/// \fn T radians(T Val)
+/// \brief Converts the specified value from degrees to radians.
+
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+half radians(half);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+half2 radians(half2);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+half3 radians(half3);
+_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+half4 radians(half4);
+
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+float radians(float);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+float2 radians(float2);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+float3 radians(float3);
+_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_radians)
+float4 radians(float4);
+
} // namespace hlsl
#endif //_HLSL_HLSL_INTRINSICS_H_
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 43cc6c81ae5cb0..c0326cf280cf3f 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1861,6 +1861,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
return true;
break;
}
+ case Builtin::BI__builtin_hlsl_elementwise_radians:
case Builtin::BI__builtin_hlsl_elementwise_rsqrt:
case Builtin::BI__builtin_hlsl_elementwise_frac: {
if (CheckFloatOrHalfRepresentations(&SemaRef, TheCall))
diff --git a/clang/test/CodeGenHLSL/builtins/radians.hlsl b/clang/test/CodeGenHLSL/builtins/radians.hlsl
new file mode 100644
index 00000000000000..774300525dbf02
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/radians.hlsl
@@ -0,0 +1,66 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
+// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
+// RUN: --check-prefixes=CHECK,NATIVE_HALF \
+// RUN: -DTARGET=dx -DFNATTRS=noundef
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
+// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF \
+// RUN: -DTARGET=dx -DFNATTRS=noundef
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN: spirv-unknown-vulkan-compute %s -fnative-half-type \
+// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
+// RUN: --check-prefixes=CHECK,NATIVE_HALF \
+// RUN: -DTARGET=spv -DFNATTRS="spir_func noundef"
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN: spirv-unknown-vulkan-compute %s -emit-llvm -disable-llvm-passes \
+// RUN: -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF \
+// RUN: -DTARGET=spv -DFNATTRS="spir_func noundef"
+
+
+// NATIVE_HALF: define [[FNATTRS]] half @
+// NATIVE_HALF: %{{.*}} = call half @llvm.[[TARGET]].radians.f16(
+// NATIVE_HALF: ret half %{{.*}}
+// NO_HALF: define [[FNATTRS]] float @
+// NO_HALF: %{{.*}} = call float @llvm.[[TARGET]].radians.f32(
+// NO_HALF: ret float %{{.*}}
+half test_radians_half(half p0) { return radians(p0); }
+// NATIVE_HALF: define [[FNATTRS]] <2 x half> @
+// NATIVE_HALF: %{{.*}} = call <2 x half> @llvm.[[TARGET]].radians.v2f16
+// NATIVE_HALF: ret <2 x half> %{{.*}}
+// NO_HALF: define [[FNATTRS]] <2 x float> @
+// NO_HALF: %{{.*}} = call <2 x float> @llvm.[[TARGET]].radians.v2f32(
+// NO_HALF: ret <2 x float> %{{.*}}
+half2 test_radians_half2(half2 p0) { return radians(p0); }
+// NATIVE_HALF: define [[FNATTRS]] <3 x half> @
+// NATIVE_HALF: %{{.*}} = call <3 x half> @llvm.[[TARGET]].radians.v3f16
+// NATIVE_HALF: ret <3 x half> %{{.*}}
+// NO_HALF: define [[FNATTRS]] <3 x float> @
+// NO_HALF: %{{.*}} = call <3 x float> @llvm.[[TARGET]].radians.v3f32(
+// NO_HALF: ret <3 x float> %{{.*}}
+half3 test_radians_half3(half3 p0) { return radians(p0); }
+// NATIVE_HALF: define [[FNATTRS]] <4 x half> @
+// NATIVE_HALF: %{{.*}} = call <4 x half> @llvm.[[TARGET]].radians.v4f16
+// NATIVE_HALF: ret <4 x half> %{{.*}}
+// NO_HALF: define [[FNATTRS]] <4 x float> @
+// NO_HALF: %{{.*}} = call <4 x float> @llvm.[[TARGET]].radians.v4f32(
+// NO_HALF: ret <4 x float> %{{.*}}
+half4 test_radians_half4(half4 p0) { return radians(p0); }
+
+// CHECK: define [[FNATTRS]] float @
+// CHECK: %{{.*}} = call float @llvm.[[TARGET]].radians.f32(
+// CHECK: ret float %{{.*}}
+float test_radians_float(float p0) { return radians(p0); }
+// CHECK: define [[FNATTRS]] <2 x float> @
+// CHECK: %{{.*}} = call <2 x float> @llvm.[[TARGET]].radians.v2f32
+// CHECK: ret <2 x float> %{{.*}}
+float2 test_radians_float2(float2 p0) { return radians(p0); }
+// CHECK: define [[FNATTRS]] <3 x float> @
+// CHECK: %{{.*}} = call <3 x float> @llvm.[[TARGET]].radians.v3f32
+// CHECK: ret <3 x float> %{{.*}}
+float3 test_radians_float3(float3 p0) { return radians(p0); }
+// CHECK: define [[FNATTRS]] <4 x float> @
+// CHECK: %{{.*}} = call <4 x float> @llvm.[[TARGET]].radians.v4f32
+// CHECK: ret <4 x float> %{{.*}}
+float4 test_radians_float4(float4 p0) { return radians(p0); }
+
diff --git a/clang/test/SemaHLSL/BuiltIns/radians-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/radians-errors.hlsl
new file mode 100644
index 00000000000000..1d315c486d7250
--- /dev/null
+++ b/clang/test/SemaHLSL/BuiltIns/radians-errors.hlsl
@@ -0,0 +1,27 @@
+// 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
+
+float test_too_few_arg() {
+ return __builtin_hlsl_elementwise_radians();
+ // expected-error@-1 {{too few arguments to function call, expected 1, have 0}}
+}
+
+float2 test_too_many_arg(float2 p0) {
+ return __builtin_hlsl_elementwise_radians(p0, p0);
+ // expected-error@-1 {{too many arguments to function call, expected 1, have 2}}
+}
+
+float builtin_bool_to_float_type_promotion(bool p1) {
+ return __builtin_hlsl_elementwise_radians(p1);
+ // expected-error@-1 {passing 'bool' to parameter of incompatible type 'float'}}
+}
+
+float builtin_radians_int_to_float_promotion(int p1) {
+ return __builtin_hlsl_elementwise_radians(p1);
+ // expected-error@-1 {{passing 'int' to parameter of incompatible type 'float'}}
+}
+
+float2 builtin_radians_int2_to_float2_promotion(int2 p1) {
+ return __builtin_hlsl_elementwise_radians(p1);
+ // 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)}}
+}
+
|
clang/lib/CodeGen/CGBuiltin.cpp
Outdated
@@ -18852,6 +18852,14 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: { | |||
retType, CGM.getHLSLRuntime().getSignIntrinsic(), | |||
ArrayRef<Value *>{Op0}, nullptr, "hlsl.sign"); | |||
} | |||
case Builtin::BI__builtin_hlsl_elementwise_radians: { | |||
Value *Op0 = EmitScalarExpr(E->getArg(0)); | |||
if (!E->getArg(0)->getType()->hasFloatingRepresentation()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 1864 of SemaHLSL.cpp
you have a check for this, Turn this into an assert.
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please run clang-format
8776e60
to
7585305
Compare
partially fixes #99151 ### Changes * Added int_spv_radians intrinsic in IntrinsicsSPIRV.td * Added lowering for int_spv_radians in SPIRVInstructionSelector.cpp * Added DXIL backend test case ### Related PRs * [[clang][HLSL] Add radians intrinsic #110802](#110802) * [[DXIL] Add radians intrinsic #110616](#110616)
makes progress on #99151 ### Changes - Added int_dx_radians intrinsic in IntrinsicsDirectX.td - Added expansion for int_dx_radians in DXILIntrinsicExpansion.cpp` - Added DXIL backend test case ### Related PRs * [[clang][HLSL] Add radians intrinsic #110802](#110802) * [[SPIRV] Add radians intrinsic #110800](#110800)
…ed in sema; Added radians to half and float only test
7585305
to
98d935f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but I didn't look too closely at the tests.
@farzonl This is also ready for merging if there's no further feedback. |
@adam-yang Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
partially fixes #99151
Changes
radians
clang builtinradians
clang builtin withhlsl_intrinsics.h
radians
toCheckHLSLBuiltinFunctionCall
inSemaChecking.cpp
radians
toEmitHLSLBuiltinExpr
inCGBuiltin.cpp
clang/test/CodeGenHLSL/builtins/radians.hlsl
clang/test/SemaHLSL/BuiltIns/radians-errors.hlsl
Related PRs