Skip to content

Commit

Permalink
[mlir][spirv] Allow converting from index type in unsigned ops
Browse files Browse the repository at this point in the history
`index` type is converted to `i32` in SPIR-V. This is fine to
support for all signed/unsigned ops.

Reviewed By: hanchung

Differential Revision: https://reviews.llvm.org/D124451
  • Loading branch information
antiagainst committed Apr 27, 2022
1 parent a6b355d commit 38e802a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mlir/lib/Conversion/SPIRVCommon/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ElementwiseOpPattern final : public OpConversionPattern<Op> {
if (!dstType)
return failure();
if (SPIRVOp::template hasTrait<OpTrait::spirv::UnsignedOp>() &&
dstType != op.getType()) {
!op.getType().isIndex() && dstType != op.getType()) {
return op.emitError(
"bitwidth emulation is not implemented yet on unsigned op");
}
Expand Down
34 changes: 32 additions & 2 deletions mlir/test/Conversion/ArithmeticToSPIRV/arithmetic-to-spirv.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func.func @int32_scalar(%lhs: i32, %rhs: i32) {
return
}

// CHECK-LABEL: @scalar_srem
// CHECK-LABEL: @int32_scalar_srem
// CHECK-SAME: (%[[LHS:.+]]: i32, %[[RHS:.+]]: i32)
func.func @scalar_srem(%lhs: i32, %rhs: i32) {
func.func @int32_scalar_srem(%lhs: i32, %rhs: i32) {
// CHECK: %[[LABS:.+]] = spv.GLSL.SAbs %[[LHS]] : i32
// CHECK: %[[RABS:.+]] = spv.GLSL.SAbs %[[RHS]] : i32
// CHECK: %[[ABS:.+]] = spv.UMod %[[LABS]], %[[RABS]] : i32
Expand All @@ -40,6 +40,36 @@ func.func @scalar_srem(%lhs: i32, %rhs: i32) {
return
}

// CHECK-LABEL: @index_scalar
func.func @index_scalar(%lhs: index, %rhs: index) {
// CHECK: spv.IAdd %{{.*}}, %{{.*}}: i32
%0 = arith.addi %lhs, %rhs: index
// CHECK: spv.ISub %{{.*}}, %{{.*}}: i32
%1 = arith.subi %lhs, %rhs: index
// CHECK: spv.IMul %{{.*}}, %{{.*}}: i32
%2 = arith.muli %lhs, %rhs: index
// CHECK: spv.SDiv %{{.*}}, %{{.*}}: i32
%3 = arith.divsi %lhs, %rhs: index
// CHECK: spv.UDiv %{{.*}}, %{{.*}}: i32
%4 = arith.divui %lhs, %rhs: index
// CHECK: spv.UMod %{{.*}}, %{{.*}}: i32
%5 = arith.remui %lhs, %rhs: index
return
}

// CHECK-LABEL: @index_scalar_srem
// CHECK-SAME: (%[[LHS:.+]]: i32, %[[RHS:.+]]: i32)
func.func @index_scalar_srem(%lhs: index, %rhs: index) {
// CHECK: %[[LABS:.+]] = spv.GLSL.SAbs %[[LHS]] : i32
// CHECK: %[[RABS:.+]] = spv.GLSL.SAbs %[[RHS]] : i32
// CHECK: %[[ABS:.+]] = spv.UMod %[[LABS]], %[[RABS]] : i32
// CHECK: %[[POS:.+]] = spv.IEqual %[[LHS]], %[[LABS]] : i32
// CHECK: %[[NEG:.+]] = spv.SNegate %[[ABS]] : i32
// CHECK: %{{.+}} = spv.Select %[[POS]], %[[ABS]], %[[NEG]] : i1, i32
%0 = arith.remsi %lhs, %rhs: index
return
}

// Check float unary operation conversions.
// CHECK-LABEL: @float32_unary_scalar
func.func @float32_unary_scalar(%arg0: f32) {
Expand Down

0 comments on commit 38e802a

Please sign in to comment.