-
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
[DXIL] Add radians intrinsic #110616
[DXIL] Add radians intrinsic #110616
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-llvm-ir Author: Adam Yang (adam-yang) Changesmakes progress on #99151 Changes
Related PRsFull diff: https://github.com/llvm/llvm-project/pull/110616.diff 3 Files Affected:
diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td
index 555877e7aaf0e5..f9acddf17f8c17 100644
--- a/llvm/include/llvm/IR/IntrinsicsDirectX.td
+++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td
@@ -85,4 +85,5 @@ def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;
def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
+def int_dx_radians : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
}
diff --git a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
index 926cbe97f24fda..33d1a12bd7c75a 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -53,6 +53,7 @@ static bool isIntrinsicExpansion(Function &F) {
case Intrinsic::dx_udot:
case Intrinsic::dx_sign:
case Intrinsic::dx_step:
+ case Intrinsic::dx_radians:
return true;
}
return false;
@@ -395,6 +396,15 @@ static Value *expandStepIntrinsic(CallInst *Orig) {
return Builder.CreateSelect(Cond, Zero, One);
}
+static Value *expandRadiansIntrinsic(CallInst *Orig) {
+ Value *X = Orig->getOperand(0);
+ Type *Ty = X->getType();
+ IRBuilder<> Builder(Orig);
+ Value *OneEightyOverPi = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
+ return Builder.CreateFMul(X, OneEightyOverPi);
+}
+
+
static Intrinsic::ID getMaxForClamp(Type *ElemTy,
Intrinsic::ID ClampIntrinsic) {
if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -511,6 +521,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
break;
case Intrinsic::dx_step:
Result = expandStepIntrinsic(Orig);
+ case Intrinsic::dx_radians:
+ Result = expandRadiansIntrinsic(Orig);
+ break;
}
if (Result) {
Orig->replaceAllUsesWith(Result);
diff --git a/llvm/test/CodeGen/DirectX/radians.ll b/llvm/test/CodeGen/DirectX/radians.ll
new file mode 100644
index 00000000000000..3d7fdb3b090093
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/radians.ll
@@ -0,0 +1,56 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+declare half @llvm.dx.radians.f16(half)
+declare float @llvm.dx.radians.f32(float)
+
+declare <4 x half> @llvm.dx.radians.v4f16(<4 x half>)
+declare <4 x float> @llvm.dx.radians.v4f32(<4 x float>)
+
+define noundef half @radians_half(half noundef %a) {
+; CHECK-LABEL: define noundef half @radians_half(
+; CHECK-SAME: half noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul half [[A]], 0xH2478
+; CHECK-NEXT: ret half [[TMP0]]
+;
+entry:
+ %elt.radians = call half @llvm.dx.radians.f16(half %a)
+ ret half %elt.radians
+}
+
+define noundef float @radians_float(float noundef %a) {
+; CHECK-LABEL: define noundef float @radians_float(
+; CHECK-SAME: float noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul float [[A]], 0x3F91DF46A0000000
+; CHECK-NEXT: ret float [[TMP0]]
+;
+entry:
+ %elt.radians = call float @llvm.dx.radians.f32(float %a)
+ ret float %elt.radians
+}
+
+define noundef <4 x half> @radians_half_vector(<4 x half> noundef %a) {
+; CHECK-LABEL: define noundef <4 x half> @radians_half_vector(
+; CHECK-SAME: <4 x half> noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul <4 x half> [[A]], <half 0xH2478, half 0xH2478, half 0xH2478, half 0xH2478>
+; CHECK-NEXT: ret <4 x half> [[TMP0]]
+;
+entry:
+ %elt.radians = call <4 x half> @llvm.dx.radians.v4f16(<4 x half> %a)
+ ret <4 x half> %elt.radians
+}
+
+define noundef <4 x float> @radians_float_vector(<4 x float> noundef %a) {
+; CHECK-LABEL: define noundef <4 x float> @radians_float_vector(
+; CHECK-SAME: <4 x float> noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul <4 x float> [[A]], <float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000>
+; CHECK-NEXT: ret <4 x float> [[TMP0]]
+;
+entry:
+ %elt.radians = call <4 x float> @llvm.dx.radians.v4f32(<4 x float> %a)
+ ret <4 x float> %elt.radians
+}
|
@llvm/pr-subscribers-backend-directx Author: Adam Yang (adam-yang) Changesmakes progress on #99151 Changes
Related PRsFull diff: https://github.com/llvm/llvm-project/pull/110616.diff 3 Files Affected:
diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td
index 555877e7aaf0e5..f9acddf17f8c17 100644
--- a/llvm/include/llvm/IR/IntrinsicsDirectX.td
+++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td
@@ -85,4 +85,5 @@ def int_dx_rsqrt : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>]
def int_dx_wave_is_first_lane : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrConvergent]>;
def int_dx_sign : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i32_ty>], [llvm_any_ty], [IntrNoMem]>;
def int_dx_step : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
+def int_dx_radians : DefaultAttrsIntrinsic<[llvm_anyfloat_ty], [LLVMMatchType<0>], [IntrNoMem]>;
}
diff --git a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
index 926cbe97f24fda..33d1a12bd7c75a 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -53,6 +53,7 @@ static bool isIntrinsicExpansion(Function &F) {
case Intrinsic::dx_udot:
case Intrinsic::dx_sign:
case Intrinsic::dx_step:
+ case Intrinsic::dx_radians:
return true;
}
return false;
@@ -395,6 +396,15 @@ static Value *expandStepIntrinsic(CallInst *Orig) {
return Builder.CreateSelect(Cond, Zero, One);
}
+static Value *expandRadiansIntrinsic(CallInst *Orig) {
+ Value *X = Orig->getOperand(0);
+ Type *Ty = X->getType();
+ IRBuilder<> Builder(Orig);
+ Value *OneEightyOverPi = ConstantFP::get(Ty, llvm::numbers::pi / 180.0);
+ return Builder.CreateFMul(X, OneEightyOverPi);
+}
+
+
static Intrinsic::ID getMaxForClamp(Type *ElemTy,
Intrinsic::ID ClampIntrinsic) {
if (ClampIntrinsic == Intrinsic::dx_uclamp)
@@ -511,6 +521,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
break;
case Intrinsic::dx_step:
Result = expandStepIntrinsic(Orig);
+ case Intrinsic::dx_radians:
+ Result = expandRadiansIntrinsic(Orig);
+ break;
}
if (Result) {
Orig->replaceAllUsesWith(Result);
diff --git a/llvm/test/CodeGen/DirectX/radians.ll b/llvm/test/CodeGen/DirectX/radians.ll
new file mode 100644
index 00000000000000..3d7fdb3b090093
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/radians.ll
@@ -0,0 +1,56 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+declare half @llvm.dx.radians.f16(half)
+declare float @llvm.dx.radians.f32(float)
+
+declare <4 x half> @llvm.dx.radians.v4f16(<4 x half>)
+declare <4 x float> @llvm.dx.radians.v4f32(<4 x float>)
+
+define noundef half @radians_half(half noundef %a) {
+; CHECK-LABEL: define noundef half @radians_half(
+; CHECK-SAME: half noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul half [[A]], 0xH2478
+; CHECK-NEXT: ret half [[TMP0]]
+;
+entry:
+ %elt.radians = call half @llvm.dx.radians.f16(half %a)
+ ret half %elt.radians
+}
+
+define noundef float @radians_float(float noundef %a) {
+; CHECK-LABEL: define noundef float @radians_float(
+; CHECK-SAME: float noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul float [[A]], 0x3F91DF46A0000000
+; CHECK-NEXT: ret float [[TMP0]]
+;
+entry:
+ %elt.radians = call float @llvm.dx.radians.f32(float %a)
+ ret float %elt.radians
+}
+
+define noundef <4 x half> @radians_half_vector(<4 x half> noundef %a) {
+; CHECK-LABEL: define noundef <4 x half> @radians_half_vector(
+; CHECK-SAME: <4 x half> noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul <4 x half> [[A]], <half 0xH2478, half 0xH2478, half 0xH2478, half 0xH2478>
+; CHECK-NEXT: ret <4 x half> [[TMP0]]
+;
+entry:
+ %elt.radians = call <4 x half> @llvm.dx.radians.v4f16(<4 x half> %a)
+ ret <4 x half> %elt.radians
+}
+
+define noundef <4 x float> @radians_float_vector(<4 x float> noundef %a) {
+; CHECK-LABEL: define noundef <4 x float> @radians_float_vector(
+; CHECK-SAME: <4 x float> noundef [[A:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[TMP0:%.*]] = fmul <4 x float> [[A]], <float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000, float 0x3F91DF46A0000000>
+; CHECK-NEXT: ret <4 x float> [[TMP0]]
+;
+entry:
+ %elt.radians = call <4 x float> @llvm.dx.radians.v4f32(<4 x float> %a)
+ ret <4 x float> %elt.radians
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Please run clang-format. |
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.
Mostly looks good. You will need to rebase or your tests will break.
80c303b
to
8c07f8a
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)
@farzonl This PR is ready for checkin. |
@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 * 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)
Fix an obvious typo in these tests to get them passing, and also fix the -Wimplicit-fallthrough warning that fires when trying to build.
) Fix an obvious typo in these tests to get them passing, and also fix the -Wimplicit-fallthrough warning that fires when trying to build. Reverting #110616 was tricky because of dependencies, so I'm just doing the easy fix directly here.
makes progress on #99151
Changes
Related PRs