[llvm] [DXIL] Add lowering for `reversebits` and `trunc` (PR #86909)
Helena Kotas via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 27 22:14:15 PDT 2024
https://github.com/hekota created https://github.com/llvm/llvm-project/pull/86909
Add lowering of `llvm.bitreverse` and `llvm.trunc` intrinsics to DXIL ops.
Completes #86582
Completes #86581
>From c0bc821a5886e735a5151192caa351f504f0e98e Mon Sep 17 00:00:00 2001
From: Helena Kotas <hekotas at microsoft.com>
Date: Wed, 27 Mar 2024 13:14:29 -0700
Subject: [PATCH 1/3] [DXIL] Add lowering for reversebits
---
llvm/lib/Target/DirectX/DXIL.td | 3 +++
llvm/test/CodeGen/DirectX/reversebits.ll | 27 ++++++++++++++++++++++++
2 files changed, 30 insertions(+)
create mode 100644 llvm/test/CodeGen/DirectX/reversebits.ll
diff --git a/llvm/lib/Target/DirectX/DXIL.td b/llvm/lib/Target/DirectX/DXIL.td
index 1fd6f3ed044ecd..61643a7a5988c6 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.",
diff --git a/llvm/test/CodeGen/DirectX/reversebits.ll b/llvm/test/CodeGen/DirectX/reversebits.ll
new file mode 100644
index 00000000000000..63315d09c7ea49
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/reversebits.ll
@@ -0,0 +1,27 @@
+; 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
+}
>From 9ecf8783633ed4377217bf948c0dc2bcbd5d7d6d Mon Sep 17 00:00:00 2001
From: Helena Kotas <hekotas at microsoft.com>
Date: Wed, 27 Mar 2024 17:04:16 -0700
Subject: [PATCH 2/3] [DXIL] Add lowering for trunc
---
llvm/lib/Target/DirectX/DXIL.td | 3 +++
llvm/test/CodeGen/DirectX/trunc.ll | 17 +++++++++++++++++
llvm/test/CodeGen/DirectX/trunc_error.ll | 10 ++++++++++
3 files changed, 30 insertions(+)
create mode 100644 llvm/test/CodeGen/DirectX/trunc.ll
create mode 100644 llvm/test/CodeGen/DirectX/trunc_error.ll
diff --git a/llvm/lib/Target/DirectX/DXIL.td b/llvm/lib/Target/DirectX/DXIL.td
index 61643a7a5988c6..03743a81433ff4 100644
--- a/llvm/lib/Target/DirectX/DXIL.td
+++ b/llvm/lib/Target/DirectX/DXIL.td
@@ -295,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/trunc.ll b/llvm/test/CodeGen/DirectX/trunc.ll
new file mode 100644
index 00000000000000..2e72f123b81e5f
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/trunc.ll
@@ -0,0 +1,17 @@
+; 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
+}
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
+}
>From 20edc78b2f8ec1cfb805136f5916ea2b8cc975e8 Mon Sep 17 00:00:00 2001
From: Helena Kotas <hekotas at microsoft.com>
Date: Wed, 27 Mar 2024 22:09:20 -0700
Subject: [PATCH 3/3] Add llvm decls
---
llvm/test/CodeGen/DirectX/reversebits.ll | 4 ++++
llvm/test/CodeGen/DirectX/trunc.ll | 3 +++
2 files changed, 7 insertions(+)
diff --git a/llvm/test/CodeGen/DirectX/reversebits.ll b/llvm/test/CodeGen/DirectX/reversebits.ll
index 63315d09c7ea49..6641355e2dea71 100644
--- a/llvm/test/CodeGen/DirectX/reversebits.ll
+++ b/llvm/test/CodeGen/DirectX/reversebits.ll
@@ -25,3 +25,7 @@ entry:
%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
index 2e72f123b81e5f..1d7bd6030ceaa0 100644
--- a/llvm/test/CodeGen/DirectX/trunc.ll
+++ b/llvm/test/CodeGen/DirectX/trunc.ll
@@ -15,3 +15,6 @@ entry:
%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)
More information about the llvm-commits
mailing list