Skip to content

Commit 6433333

Browse files
committed
[mlir][arith] Add arith.shrsi support to WIE
This includes LIT tests over the generated ops and runtime tests. Reviewed By: antiagainst Differential Revision: https://reviews.llvm.org/D137965
1 parent 940d3e0 commit 6433333

5 files changed

Lines changed: 267 additions & 2 deletions

File tree

‎mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,69 @@ struct ConvertShRUI final : OpConversionPattern<arith::ShRUIOp> {
781781
}
782782
};
783783

784+
//===----------------------------------------------------------------------===//
785+
// ConvertShRSI
786+
//===----------------------------------------------------------------------===//
787+
788+
struct ConvertShRSI final : OpConversionPattern<arith::ShRSIOp> {
789+
using OpConversionPattern::OpConversionPattern;
790+
791+
LogicalResult
792+
matchAndRewrite(arith::ShRSIOp op, OpAdaptor adaptor,
793+
ConversionPatternRewriter &rewriter) const override {
794+
Location loc = op->getLoc();
795+
796+
Type oldTy = op.getType();
797+
auto newTy =
798+
getTypeConverter()->convertType(oldTy).dyn_cast_or_null<VectorType>();
799+
if (!newTy)
800+
return rewriter.notifyMatchFailure(
801+
loc, llvm::formatv("unsupported type: {0}", op.getType()));
802+
803+
Value lhsElem1 = extractLastDimSlice(rewriter, loc, adaptor.getLhs(), 1);
804+
Value rhsElem0 = extractLastDimSlice(rewriter, loc, adaptor.getRhs(), 0);
805+
806+
Type narrowTy = rhsElem0.getType();
807+
int64_t origBitwidth = newTy.getElementTypeBitWidth() * 2;
808+
809+
// Rewrite this as an bitwise or of `arith.shrui` and sign extension bits.
810+
// Perform as many ops over the narrow integer type as possible and let the
811+
// other emulation patterns convert the rest.
812+
Value elemZero =
813+
createScalarOrSplatConstant(rewriter, loc, narrowTy, 0);
814+
Value signBit = rewriter.create<arith::CmpIOp>(
815+
loc, arith::CmpIPredicate::slt, lhsElem1, elemZero);
816+
signBit = dropTrailingX1Dim(rewriter, loc, signBit);
817+
818+
// Create a bit pattern of either all ones or all zeros. Then shift it left
819+
// to calculate the sign extension bits created by shifting the original
820+
// sign bit right.
821+
Value allSign = rewriter.create<arith::ExtSIOp>(loc, oldTy, signBit);
822+
Value maxShift =
823+
createScalarOrSplatConstant(rewriter, loc, narrowTy, origBitwidth);
824+
Value numNonSignExtBits =
825+
rewriter.create<arith::SubIOp>(loc, maxShift, rhsElem0);
826+
numNonSignExtBits = dropTrailingX1Dim(rewriter, loc, numNonSignExtBits);
827+
numNonSignExtBits =
828+
rewriter.create<arith::ExtUIOp>(loc, oldTy, numNonSignExtBits);
829+
Value signBits =
830+
rewriter.create<arith::ShLIOp>(loc, allSign, numNonSignExtBits);
831+
832+
// Use original arguments to create the right shift.
833+
Value shrui = rewriter.create<arith::ShRUIOp>(loc, op.getLhs(), op.getRhs());
834+
Value shrsi = rewriter.create<arith::OrIOp>(loc, shrui, signBits);
835+
836+
// Handle shifting by zero. This is necessary when the `signBits` shift is
837+
// invalid.
838+
Value isNoop = rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq,
839+
rhsElem0, elemZero);
840+
isNoop = dropTrailingX1Dim(rewriter, loc, isNoop);
841+
rewriter.replaceOpWithNewOp<arith::SelectOp>(op, isNoop, op.getLhs(), shrsi);
842+
843+
return success();
844+
}
845+
};
846+
784847
//===----------------------------------------------------------------------===//
785848
// ConvertTruncI
786849
//===----------------------------------------------------------------------===//
@@ -799,7 +862,8 @@ struct ConvertTruncI final : OpConversionPattern<arith::TruncIOp> {
799862
loc, llvm::formatv("unsupported truncation result type: {0}",
800863
op.getType()));
801864

802-
// Discard the high half of the input. Truncate the low half, if necessary.
865+
// Discard the high half of the input. Truncate the low half, if
866+
// necessary.
803867
Value extracted = extractLastDimSlice(rewriter, loc, adaptor.getIn(), 0);
804868
extracted = dropTrailingX1Dim(rewriter, loc, extracted);
805869
Value truncated =
@@ -940,7 +1004,7 @@ void arith::populateArithWideIntEmulationPatterns(
9401004
// Misc ops.
9411005
ConvertConstant, ConvertCmpI, ConvertSelect, ConvertVectorPrint,
9421006
// Binary ops.
943-
ConvertAddI, ConvertMulI, ConvertShLI, ConvertShRUI,
1007+
ConvertAddI, ConvertMulI, ConvertShLI, ConvertShRSI, ConvertShRUI,
9441008
// Bitwise binary ops.
9451009
ConvertBitwiseBinary<arith::AndIOp>, ConvertBitwiseBinary<arith::OrIOp>,
9461010
ConvertBitwiseBinary<arith::XOrIOp>,

‎mlir/test/Dialect/Arith/emulate-wide-int.mlir‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,45 @@ func.func @shrui_vector(%a : vector<3xi64>, %b : vector<3xi64>) -> vector<3xi64>
587587
return %m : vector<3xi64>
588588
}
589589

590+
// CHECK-LABEL: func.func @shrsi_scalar
591+
// CHECK-SAME: ([[ARG0:%.+]]: vector<2xi32>, [[ARG1:%.+]]: vector<2xi32>) -> vector<2xi32>
592+
// CHECK-NEXT: [[HIGH0:%.+]] = vector.extract [[ARG0]][1] : vector<2xi32>
593+
// CHECK-NEXT: [[LOW1:%.+]] = vector.extract [[ARG1]][0] : vector<2xi32>
594+
// CHECK-NEXT: [[CST0:%.+]] = arith.constant 0 : i32
595+
// CHECK-NEXT: [[NEG:%.+]] = arith.cmpi slt, [[HIGH0]], [[CST0]] : i32
596+
// CHECK-NEXT: [[NEGEXT:%.+]] = arith.extsi [[NEG]] : i1 to i32
597+
// CHECK: [[CST64:%.+]] = arith.constant 64 : i32
598+
// CHECK-NEXT: [[SIGNBITS:%.+]] = arith.subi [[CST64]], [[LOW1]] : i32
599+
// CHECK: arith.shli
600+
// CHECK: arith.shrui
601+
// CHECK: arith.shli
602+
// CHECK: arith.shli
603+
// CHECK: arith.shrui
604+
// CHECK: arith.shrui
605+
// CHECK: arith.shli
606+
// CHECK: arith.shrui
607+
// CHECK: return {{%.+}} : vector<2xi32>
608+
func.func @shrsi_scalar(%a : i64, %b : i64) -> i64 {
609+
%c = arith.shrsi %a, %b : i64
610+
return %c : i64
611+
}
612+
613+
// CHECK-LABEL: func.func @shrsi_vector
614+
// CHECK-SAME: ({{%.+}}: vector<3x2xi32>, {{%.+}}: vector<3x2xi32>) -> vector<3x2xi32>
615+
// CHECK: arith.shli {{%.+}}, {{%.+}} : vector<3x1xi32>
616+
// CHECK: arith.shrui {{%.+}}, {{%.+}} : vector<3x1xi32>
617+
// CHECK: arith.shli {{%.+}}, {{%.+}} : vector<3x1xi32>
618+
// CHECK: arith.shli {{%.+}}, {{%.+}} : vector<3x1xi32>
619+
// CHECK: arith.shrui {{%.+}}, {{%.+}} : vector<3x1xi32>
620+
// CHECK: arith.shrui {{%.+}}, {{%.+}} : vector<3x1xi32>
621+
// CHECK: arith.shli {{%.+}}, {{%.+}} : vector<3x1xi32>
622+
// CHECK: arith.shrui {{%.+}}, {{%.+}} : vector<3x1xi32>
623+
// CHECK: return {{%.+}} : vector<3x2xi32>
624+
func.func @shrsi_vector(%a : vector<3xi64>, %b : vector<3xi64>) -> vector<3xi64> {
625+
%m = arith.shrsi %a, %b : vector<3xi64>
626+
return %m : vector<3xi64>
627+
}
628+
590629
// CHECK-LABEL: func @andi_scalar_a_b
591630
// CHECK-SAME: ([[ARG0:%.+]]: vector<2xi32>, [[ARG1:%.+]]: vector<2xi32>) -> vector<2xi32>
592631
// CHECK-NEXT: [[LOW0:%.+]] = vector.extract [[ARG0]][0] : vector<2xi32>

‎mlir/test/Integration/Dialect/Arith/CPU/test-wide-int-emulation-compare-results-i16.mlir‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,53 @@ func.func @test_shli() -> () {
203203
return
204204
}
205205

206+
//===----------------------------------------------------------------------===//
207+
// Test arith.shrsi
208+
//===----------------------------------------------------------------------===//
209+
210+
// Ops in this function will be emulated using i8 ops.
211+
func.func @emulate_shrsi(%lhs : i16, %rhs : i16) -> (i16) {
212+
%res = arith.shrsi %lhs, %rhs : i16
213+
return %res : i16
214+
}
215+
216+
// Performs both wide and emulated `arith.shrsi`, and checks that the results
217+
// match.
218+
func.func @check_shrsi(%lhs : i16, %rhs : i16) -> () {
219+
%wide = arith.shrsi %lhs, %rhs : i16
220+
%emulated = func.call @emulate_shrsi(%lhs, %rhs) : (i16, i16) -> (i16)
221+
func.call @check_results(%lhs, %rhs, %wide, %emulated) : (i16, i16, i16, i16) -> ()
222+
return
223+
}
224+
225+
// Checks that `arith.shrus` is emulated properly by sampling the input space.
226+
// Checks all valid shift amounts for i16: 0 to 15.
227+
// In total, this test function checks 100 * 16 = 1.6k input pairs.
228+
func.func @test_shrsi() -> () {
229+
%idx0 = arith.constant 0 : index
230+
%idx1 = arith.constant 1 : index
231+
%idx16 = arith.constant 16 : index
232+
%idx100 = arith.constant 100 : index
233+
234+
%cst0 = arith.constant 0 : i16
235+
%cst1 = arith.constant 1 : i16
236+
237+
scf.for %lhs_idx = %idx0 to %idx100 step %idx1 iter_args(%lhs = %cst0) -> (i16) {
238+
%arg_lhs = func.call @xhash(%lhs) : (i16) -> (i16)
239+
240+
scf.for %rhs_idx = %idx0 to %idx16 step %idx1 iter_args(%rhs = %cst0) -> (i16) {
241+
func.call @check_shrsi(%arg_lhs, %rhs) : (i16, i16) -> ()
242+
%rhs_next = arith.addi %rhs, %cst1 : i16
243+
scf.yield %rhs_next : i16
244+
}
245+
246+
%lhs_next = arith.addi %lhs, %cst1 : i16
247+
scf.yield %lhs_next : i16
248+
}
249+
250+
return
251+
}
252+
206253
//===----------------------------------------------------------------------===//
207254
// Test arith.shrui
208255
//===----------------------------------------------------------------------===//
@@ -258,6 +305,7 @@ func.func @entry() {
258305
func.call @test_addi() : () -> ()
259306
func.call @test_muli() : () -> ()
260307
func.call @test_shli() : () -> ()
308+
func.call @test_shrsi() : () -> ()
261309
func.call @test_shrui() : () -> ()
262310
return
263311
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Check that the wide integer `arith.shrsi` emulation produces the same result as wide
2+
// `arith.shrsi`. Emulate i16 ops with i8 ops.
3+
4+
// RUN: mlir-opt %s --convert-scf-to-cf --convert-cf-to-llvm --convert-vector-to-llvm \
5+
// RUN: --convert-func-to-llvm --convert-arith-to-llvm | \
6+
// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
7+
// RUN: --shared-libs=%mlir_lib_dir/libmlir_c_runner_utils%shlibext | \
8+
// RUN: FileCheck %s --match-full-lines
9+
10+
// RUN: mlir-opt %s --test-arith-emulate-wide-int="widest-int-supported=8" \
11+
// RUN: --convert-scf-to-cf --convert-cf-to-llvm --convert-vector-to-llvm \
12+
// RUN: --convert-func-to-llvm --convert-arith-to-llvm | \
13+
// RUN: mlir-cpu-runner -e entry -entry-point-result=void \
14+
// RUN: --shared-libs=%mlir_lib_dir/libmlir_c_runner_utils%shlibext | \
15+
// RUN: FileCheck %s --match-full-lines
16+
17+
// Ops in this function *only* will be emulated using i8 types.
18+
func.func @emulate_shrsi(%lhs : i16, %rhs : i16) -> (i16) {
19+
%res = arith.shrsi %lhs, %rhs : i16
20+
return %res : i16
21+
}
22+
23+
func.func @check_shrsi(%lhs : i16, %rhs : i16) -> () {
24+
%res = func.call @emulate_shrsi(%lhs, %rhs) : (i16, i16) -> (i16)
25+
vector.print %res : i16
26+
return
27+
}
28+
29+
func.func @entry() {
30+
%cst0 = arith.constant 0 : i16
31+
%cst1 = arith.constant 1 : i16
32+
%cst2 = arith.constant 2 : i16
33+
%cst7 = arith.constant 7 : i16
34+
%cst8 = arith.constant 8 : i16
35+
%cst9 = arith.constant 9 : i16
36+
%cst15 = arith.constant 15 : i16
37+
38+
%cst_n1 = arith.constant -1 : i16
39+
40+
%cst1337 = arith.constant 1337 : i16
41+
%cst_n1337 = arith.constant -1337 : i16
42+
43+
%cst_i16_min = arith.constant -32768 : i16
44+
45+
// CHECK: -32768
46+
// CHECK-NEXT: -16384
47+
// CHECK-NEXT: -8192
48+
// CHECK-NEXT: -256
49+
// CHECK-NEXT: -128
50+
// CHECK-NEXT: -64
51+
// CHECK-NEXT: -1
52+
func.call @check_shrsi(%cst_i16_min, %cst0) : (i16, i16) -> ()
53+
func.call @check_shrsi(%cst_i16_min, %cst1) : (i16, i16) -> ()
54+
func.call @check_shrsi(%cst_i16_min, %cst2) : (i16, i16) -> ()
55+
func.call @check_shrsi(%cst_i16_min, %cst7) : (i16, i16) -> ()
56+
func.call @check_shrsi(%cst_i16_min, %cst8) : (i16, i16) -> ()
57+
func.call @check_shrsi(%cst_i16_min, %cst9) : (i16, i16) -> ()
58+
func.call @check_shrsi(%cst_i16_min, %cst15) : (i16, i16) -> ()
59+
60+
// CHECK-NEXT: 0
61+
// CHECK-NEXT: 0
62+
// CHECK-NEXT: 0
63+
// CHECK-NEXT: 1
64+
// CHECK-NEXT: -1
65+
// CHECK-NEXT: -1
66+
func.call @check_shrsi(%cst0, %cst0) : (i16, i16) -> ()
67+
func.call @check_shrsi(%cst0, %cst1) : (i16, i16) -> ()
68+
func.call @check_shrsi(%cst1, %cst1) : (i16, i16) -> ()
69+
func.call @check_shrsi(%cst1, %cst0) : (i16, i16) -> ()
70+
func.call @check_shrsi(%cst_n1, %cst1) : (i16, i16) -> ()
71+
func.call @check_shrsi(%cst_n1, %cst15) : (i16, i16) -> ()
72+
73+
// CHECK-NEXT: 1337
74+
// CHECK-NEXT: 334
75+
// CHECK-NEXT: 10
76+
// CHECK-NEXT: 5
77+
// CHECK-NEXT: 2
78+
// CHECK-NEXT: 0
79+
func.call @check_shrsi(%cst1337, %cst0) : (i16, i16) -> ()
80+
func.call @check_shrsi(%cst1337, %cst2) : (i16, i16) -> ()
81+
func.call @check_shrsi(%cst1337, %cst7) : (i16, i16) -> ()
82+
func.call @check_shrsi(%cst1337, %cst8) : (i16, i16) -> ()
83+
func.call @check_shrsi(%cst1337, %cst9) : (i16, i16) -> ()
84+
func.call @check_shrsi(%cst1337, %cst15) : (i16, i16) -> ()
85+
86+
// CHECK-NEXT: -1337
87+
// CHECK-NEXT: -335
88+
// CHECK-NEXT: -11
89+
// CHECK-NEXT: -6
90+
// CHECK-NEXT: -3
91+
// CHECK-NEXT: -1
92+
func.call @check_shrsi(%cst_n1337, %cst0) : (i16, i16) -> ()
93+
func.call @check_shrsi(%cst_n1337, %cst2) : (i16, i16) -> ()
94+
func.call @check_shrsi(%cst_n1337, %cst7) : (i16, i16) -> ()
95+
func.call @check_shrsi(%cst_n1337, %cst8) : (i16, i16) -> ()
96+
func.call @check_shrsi(%cst_n1337, %cst9) : (i16, i16) -> ()
97+
func.call @check_shrsi(%cst_n1337, %cst15) : (i16, i16) -> ()
98+
99+
return
100+
}

‎mlir/test/Integration/Dialect/Arith/CPU/test-wide-int-emulation-shrui-i16.mlir‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func.func @entry() {
3838
%cst_n1 = arith.constant -1 : i16
3939

4040
%cst1337 = arith.constant 1337 : i16
41+
%cst_n1337 = arith.constant -1337 : i16
4142

4243
%cst_i16_min = arith.constant -32768 : i16
4344

@@ -67,6 +68,19 @@ func.func @entry() {
6768
func.call @check_shrui(%cst1337, %cst9) : (i16, i16) -> ()
6869
func.call @check_shrui(%cst1337, %cst15) : (i16, i16) -> ()
6970

71+
// CHECK-NEXT: -1337
72+
// CHECK-NEXT: 16049
73+
// CHECK-NEXT: 501
74+
// CHECK-NEXT: 250
75+
// CHECK-NEXT: 125
76+
// CHECK-NEXT: 1
77+
func.call @check_shrui(%cst_n1337, %cst0) : (i16, i16) -> ()
78+
func.call @check_shrui(%cst_n1337, %cst2) : (i16, i16) -> ()
79+
func.call @check_shrui(%cst_n1337, %cst7) : (i16, i16) -> ()
80+
func.call @check_shrui(%cst_n1337, %cst8) : (i16, i16) -> ()
81+
func.call @check_shrui(%cst_n1337, %cst9) : (i16, i16) -> ()
82+
func.call @check_shrui(%cst_n1337, %cst15) : (i16, i16) -> ()
83+
7084
// CHECK-NEXT: 16384
7185
// CHECK-NEXT: 8192
7286
// CHECK-NEXT: 256

0 commit comments

Comments
 (0)