Skip to content
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

Merged
merged 4 commits into from
Mar 28, 2024

Conversation

hekota
Copy link
Member

@hekota hekota commented Mar 28, 2024

Add lowering of llvm.bitreverse and llvm.trunc intrinsics to DXIL ops.

Fixes #86582
Fixes #86581

Copy link

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 @ followed by their GitHub username.

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.

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 28, 2024

@llvm/pr-subscribers-backend-directx

Author: Helena Kotas (hekota)

Changes

Add lowering of llvm.bitreverse and llvm.trunc intrinsics to DXIL ops.

Completes #86582
Completes #86581


Full diff: https://github.com/llvm/llvm-project/pull/86909.diff

4 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXIL.td (+6)
  • (added) llvm/test/CodeGen/DirectX/reversebits.ll (+31)
  • (added) llvm/test/CodeGen/DirectX/trunc.ll (+20)
  • (added) llvm/test/CodeGen/DirectX/trunc_error.ll (+10)
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
+}

Comment on lines 289 to 290
"Returns the specified value with its bits reversed.",
[llvm_anyint_ty, LLVMMatchType<0>]>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"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.

Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Member

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?

Copy link
Contributor

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.

@farzonl
Copy link
Member

farzonl commented Mar 28, 2024

might be good to have a reverversebits error case so we know dxil doesn't get generated for floats.

@damyanp
Copy link
Contributor

damyanp commented Mar 28, 2024

I think you need to update the PR description so it says Fixes #86582 or Closes #86582. Completes #86582 doesn't seem to get picked up. https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests

@hekota
Copy link
Member Author

hekota commented Mar 28, 2024

might be good to have a reverversebits error case so we know dxil doesn't get generated for floats.

I assumed we are only adding lowering error cases when the llvm intrinsic supports more types than DXIL. For example in the cos_error.ll there is error case for the double overload, but not for any integer overloads because llvm.cos does not support integers. The llvm.bitreverse intrinsics only has only integer overloads and opt fails to even load the code with llvm.bitreverse.f32, let alone reach the dxil-op-lower pass.

@farzonl
Copy link
Member

farzonl commented Mar 28, 2024

might be good to have a reverversebits error case so we know dxil doesn't get generated for floats.

I assumed we are only adding lowering error cases when the llvm intrinsic supports more types than DXIL. For example in the cos_error.ll there is error case for the double overload, but not for any integer overloads because llvm.cos does not support integers. The llvm.bitreverse intrinsics only has only integer overloads and opt fails to even load the code with llvm.bitreverse.f32, let alone reach the dxil-op-lower pass.

Thanks for checking I wasn't sure.

@farzonl farzonl merged commit 62d6beb into llvm:main Mar 28, 2024
2 of 3 checks passed
Copy link

@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
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!

@hekota hekota deleted the hlsl-reversebits-trunc-lowering branch March 28, 2024 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DXIL] implement reversebits lowering [DXIL] implement trunc lowering
6 participants