-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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 lowering for reversebits
and trunc
#86909
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 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 If you have received no comments on your PR for a week, you can request a review 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-backend-directx Author: Helena Kotas (hekota) ChangesAdd lowering of Completes #86582 Full diff: https://github.com/llvm/llvm-project/pull/86909.diff 4 Files Affected:
diff --git a/llvm/lib/Target/DirectX/DXIL.td b/llvm/lib/Target/DirectX/DXIL.td
index 1fd6f3ed044ecd..03743a81433ff4 100644
--- a/llvm/lib/Target/DirectX/DXIL.td
+++ b/llvm/lib/Target/DirectX/DXIL.td
@@ -285,6 +285,9 @@ def RSqrt : DXILOpMapping<25, unary, int_dx_rsqrt,
"Returns the reciprocal of the square root of the specified value."
"rsqrt(x) = 1 / sqrt(x).",
[llvm_halforfloat_ty, LLVMMatchType<0>]>;
+def Rbits : DXILOpMapping<30, unary, int_bitreverse,
+ "Returns the specified value with its bits reversed.",
+ [llvm_anyint_ty, LLVMMatchType<0>]>;
def Round : DXILOpMapping<26, unary, int_round,
"Returns the input rounded to the nearest integer"
"within a floating-point type.",
@@ -292,6 +295,9 @@ def Round : DXILOpMapping<26, unary, int_round,
def Floor : DXILOpMapping<27, unary, int_floor,
"Returns the largest integer that is less than or equal to the input.",
[llvm_halforfloat_ty, LLVMMatchType<0>]>;
+def Trunc : DXILOpMapping<29, unary, int_trunc,
+ "Returns the specified value truncated to the integer component.",
+ [llvm_halforfloat_ty, LLVMMatchType<0>]>;
def FMax : DXILOpMapping<35, binary, int_maxnum,
"Float maximum. FMax(a,b) = a > b ? a : b">;
def FMin : DXILOpMapping<36, binary, int_minnum,
diff --git a/llvm/test/CodeGen/DirectX/reversebits.ll b/llvm/test/CodeGen/DirectX/reversebits.ll
new file mode 100644
index 00000000000000..6641355e2dea71
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/reversebits.ll
@@ -0,0 +1,31 @@
+; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
+
+; Make sure dxil operation function calls for reversebits are generated for all integer types.
+
+; Function Attrs: nounwind
+define noundef i16 @test_bitreverse_short(i16 noundef %a) #0 {
+entry:
+; CHECK:call i16 @dx.op.unary.i16(i32 30, i16 %{{.*}})
+ %elt.bitreverse = call i16 @llvm.bitreverse.i16(i16 %a)
+ ret i16 %elt.bitreverse
+}
+
+; Function Attrs: nounwind
+define noundef i32 @test_bitreverse_int(i32 noundef %a) #0 {
+entry:
+; CHECK:call i32 @dx.op.unary.i32(i32 30, i32 %{{.*}})
+ %elt.bitreverse = call i32 @llvm.bitreverse.i32(i32 %a)
+ ret i32 %elt.bitreverse
+}
+
+; Function Attrs: nounwind
+define noundef i64 @test_bitreverse_long(i64 noundef %a) #0 {
+entry:
+; CHECK:call i64 @dx.op.unary.i64(i32 30, i64 %{{.*}})
+ %elt.bitreverse = call i64 @llvm.bitreverse.i64(i64 %a)
+ ret i64 %elt.bitreverse
+}
+
+declare i16 @llvm.bitreverse.i16(i16)
+declare i32 @llvm.bitreverse.i32(i32)
+declare i64 @llvm.bitreverse.i64(i64)
diff --git a/llvm/test/CodeGen/DirectX/trunc.ll b/llvm/test/CodeGen/DirectX/trunc.ll
new file mode 100644
index 00000000000000..1d7bd6030ceaa0
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/trunc.ll
@@ -0,0 +1,20 @@
+; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
+
+; Make sure dxil operation function calls for trunc are generated for float and half.
+
+define noundef float @trunc_float(float noundef %a) #0 {
+entry:
+; CHECK:call float @dx.op.unary.f32(i32 29, float %{{.*}})
+ %elt.trunc = call float @llvm.trunc.f32(float %a)
+ ret float %elt.trunc
+}
+
+define noundef half @trunc_half(half noundef %a) #0 {
+entry:
+; CHECK:call half @dx.op.unary.f16(i32 29, half %{{.*}})
+ %elt.trunc = call half @llvm.trunc.f16(half %a)
+ ret half %elt.trunc
+}
+
+declare half @llvm.trunc.f16(half)
+declare float @llvm.trunc.f32(float)
diff --git a/llvm/test/CodeGen/DirectX/trunc_error.ll b/llvm/test/CodeGen/DirectX/trunc_error.ll
new file mode 100644
index 00000000000000..751b0b94c280df
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/trunc_error.ll
@@ -0,0 +1,10 @@
+; RUN: not opt -S -dxil-op-lower %s 2>&1 | FileCheck %s
+
+; DXIL operation trunc does not support double overload type
+; CHECK: LLVM ERROR: Invalid Overload Type
+
+define noundef double @trunc_double(double noundef %a) {
+entry:
+ %elt.trunc = call double @llvm.trunc.f64(double %a)
+ ret double %elt.trunc
+}
|
llvm/lib/Target/DirectX/DXIL.td
Outdated
"Returns the specified value with its bits reversed.", | ||
[llvm_anyint_ty, LLVMMatchType<0>]>; |
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.
"Returns the specified value with its bits reversed.", | |
[llvm_anyint_ty, LLVMMatchType<0>]>; | |
"Returns the specified value with its bits reversed.">; |
Since the inherited types of int_bitreverse
are valid for Rbits
, overriding is not necessary,
Additionally, consider adding this record after Trunc
to maintain ascending order of opcode value.
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.
@bharadwajy i'm pretty sure llvm_anyint_ty also covers int vectors. don't we need to limit to int scalars?
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.
@bharadwajy i'm pretty sure llvm_anyint_ty also covers int vectors. don't we need to limit to int scalars?
Yes. At present, such a narrower scalar type is not yet defined in DXIL.td
. Additional refinement will follow once a way to represent the narrower overload type is added.
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.
Does it make sense to do the narrower scalar type in this pr? Alternatively, should we leave this as is, as a placeholder for the future refinement?
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.
Does it make sense to do the narrower scalar type in this pr? Alternatively, should we leave this as is, as a placeholder for the future refinement?
Merging this PR in its current form and following up with a refinement in a later PR would be beneficial. That would follow the path of continued addition of DXIL Ops and tests for lowering, even if overload specification includes vector types to inform my ongoing design space exploration of a better specification mechanism, without being blocked on either front.
might be good to have a reverversebits error case so we know dxil doesn't get generated for floats. |
I think you need to update the PR description so it says |
I assumed we are only adding lowering error cases when the llvm intrinsic supports more types than DXIL. For example in the |
Thanks for checking I wasn't sure. |
@hekota 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 Please check whether problems have been caused by your change specifically, as 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. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Add lowering of
llvm.bitreverse
andllvm.trunc
intrinsics to DXIL ops.Fixes #86582
Fixes #86581