Skip to content

Commit 62d6beb

Browse files
authored
[DXIL] Add lowering for reversebits and trunc (#86909)
Add lowering of `llvm.bitreverse` and `llvm.trunc` intrinsics to DXIL ops. Fixes #86582 Fixes #86581
1 parent d357324 commit 62d6beb

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

llvm/lib/Target/DirectX/DXIL.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ def Round : DXILOpMapping<26, unary, int_round,
292292
def Floor : DXILOpMapping<27, unary, int_floor,
293293
"Returns the largest integer that is less than or equal to the input.",
294294
[llvm_halforfloat_ty, LLVMMatchType<0>]>;
295+
def Trunc : DXILOpMapping<29, unary, int_trunc,
296+
"Returns the specified value truncated to the integer component.",
297+
[llvm_halforfloat_ty, LLVMMatchType<0>]>;
298+
def Rbits : DXILOpMapping<30, unary, int_bitreverse,
299+
"Returns the specified value with its bits reversed.",
300+
[llvm_anyint_ty, LLVMMatchType<0>]>;
295301
def FMax : DXILOpMapping<35, binary, int_maxnum,
296302
"Float maximum. FMax(a,b) = a > b ? a : b">;
297303
def FMin : DXILOpMapping<36, binary, int_minnum,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
2+
3+
; Make sure dxil operation function calls for reversebits are generated for all integer types.
4+
5+
; Function Attrs: nounwind
6+
define noundef i16 @test_bitreverse_short(i16 noundef %a) {
7+
entry:
8+
; CHECK:call i16 @dx.op.unary.i16(i32 30, i16 %{{.*}})
9+
%elt.bitreverse = call i16 @llvm.bitreverse.i16(i16 %a)
10+
ret i16 %elt.bitreverse
11+
}
12+
13+
; Function Attrs: nounwind
14+
define noundef i32 @test_bitreverse_int(i32 noundef %a) {
15+
entry:
16+
; CHECK:call i32 @dx.op.unary.i32(i32 30, i32 %{{.*}})
17+
%elt.bitreverse = call i32 @llvm.bitreverse.i32(i32 %a)
18+
ret i32 %elt.bitreverse
19+
}
20+
21+
; Function Attrs: nounwind
22+
define noundef i64 @test_bitreverse_long(i64 noundef %a) {
23+
entry:
24+
; CHECK:call i64 @dx.op.unary.i64(i32 30, i64 %{{.*}})
25+
%elt.bitreverse = call i64 @llvm.bitreverse.i64(i64 %a)
26+
ret i64 %elt.bitreverse
27+
}
28+
29+
declare i16 @llvm.bitreverse.i16(i16)
30+
declare i32 @llvm.bitreverse.i32(i32)
31+
declare i64 @llvm.bitreverse.i64(i64)

llvm/test/CodeGen/DirectX/trunc.ll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
2+
3+
; Make sure dxil operation function calls for trunc are generated for float and half.
4+
5+
define noundef float @trunc_float(float noundef %a) {
6+
entry:
7+
; CHECK:call float @dx.op.unary.f32(i32 29, float %{{.*}})
8+
%elt.trunc = call float @llvm.trunc.f32(float %a)
9+
ret float %elt.trunc
10+
}
11+
12+
define noundef half @trunc_half(half noundef %a) {
13+
entry:
14+
; CHECK:call half @dx.op.unary.f16(i32 29, half %{{.*}})
15+
%elt.trunc = call half @llvm.trunc.f16(half %a)
16+
ret half %elt.trunc
17+
}
18+
19+
declare half @llvm.trunc.f16(half)
20+
declare float @llvm.trunc.f32(float)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; RUN: not opt -S -dxil-op-lower %s 2>&1 | FileCheck %s
2+
3+
; DXIL operation trunc does not support double overload type
4+
; CHECK: LLVM ERROR: Invalid Overload Type
5+
6+
define noundef double @trunc_double(double noundef %a) {
7+
entry:
8+
%elt.trunc = call double @llvm.trunc.f64(double %a)
9+
ret double %elt.trunc
10+
}

0 commit comments

Comments
 (0)