Skip to content

Commit

Permalink
[DXIL] Add lowering for reversebits and trunc (#86909)
Browse files Browse the repository at this point in the history
Add lowering of `llvm.bitreverse` and `llvm.trunc` intrinsics to DXIL
ops.

Fixes #86582
Fixes #86581
  • Loading branch information
hekota committed Mar 28, 2024
1 parent d357324 commit 62d6beb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions llvm/lib/Target/DirectX/DXIL.td
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ 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 Rbits : DXILOpMapping<30, unary, int_bitreverse,
"Returns the specified value with its bits reversed.",
[llvm_anyint_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,
Expand Down
31 changes: 31 additions & 0 deletions llvm/test/CodeGen/DirectX/reversebits.ll
Original file line number Diff line number Diff line change
@@ -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) {
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) {
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) {
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)
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/DirectX/trunc.ll
Original file line number Diff line number Diff line change
@@ -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) {
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) {
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)
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/DirectX/trunc_error.ll
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 62d6beb

Please sign in to comment.