Skip to content

Commit d13e223

Browse files
[MLIR][AArch64] Add integration test for lowering of vector.contract to Neon FEAT_I8MM (#144699)
1 parent 1a60c74 commit d13e223

File tree

1 file changed

+336
-0
lines changed

1 file changed

+336
-0
lines changed
Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
1+
// REQUIRES: arm-emulator
2+
3+
// DEFINE: %{compile} = mlir-opt %s \
4+
// DEFINE: --convert-vector-to-scf --convert-scf-to-cf --convert-vector-to-llvm='enable-arm-neon enable-arm-i8mm' \
5+
// DEFINE: --expand-strided-metadata --convert-to-llvm --finalize-memref-to-llvm \
6+
// DEFINE: --lower-affine --convert-arith-to-llvm --reconcile-unrealized-casts \
7+
// DEFINE: -o %t
8+
9+
// DEFINE: %{entry_point} = main
10+
11+
// DEFINE: %{run} = %mcr_aarch64_cmd %t -e %{entry_point} -entry-point-result=void --march=aarch64 --mattr="+neon,+i8mm" \
12+
// DEFINE: -shared-libs=%mlir_runner_utils,%mlir_c_runner_utils,%native_mlir_arm_runner_utils
13+
14+
// RUN: rm -f %t && %{compile} && FileCheck %s --input-file=%t -check-prefix CHECK-IR && %{run} | FileCheck %s
15+
16+
#packed_maps = [
17+
affine_map<(m, n, k) -> (m, k)>,
18+
affine_map<(m, n, k) -> (n, k)>,
19+
affine_map<(m, n, k) -> (m, n)>
20+
]
21+
22+
//
23+
// Test the lowering of `vector.contract` using the `LowerContractionToNeonI8MMPattern`
24+
//
25+
// The operation that the `vector.contract` in this test performs is matrix
26+
// multiplication with accumulate
27+
// OUT = ACC + LHS * RHS
28+
// of two 8-bit integer matrices LHS and RHS, and a 32-bit integer matrix ACC
29+
// into a 32-bit integer matrix OUT. The LHS and RHS can be sign- or zero- extended,
30+
// this test covers all the possible variants.
31+
//
32+
// Tested are calculations as well as that the relevant `ArmNeon` dialect
33+
// operations ('arm_neon.smmla`, arm_neon.ummla`, etc) are emitted.
34+
//
35+
// That pattern above handles (therefore this test prepares) input/output vectors with
36+
// specific shapes:
37+
// * LHS: vector<MxKxi8>
38+
// * RHS: vector<NxKxi8>
39+
// * ACC, OUT: vector<MxNxi32>
40+
// where the M and N are even and K is divisible by 8.
41+
// Note that the RHS is transposed.
42+
// This data layout makes it efficient to load data into SIMD
43+
// registers in the layout expected by FEAT_I8MM instructions.
44+
// Such a `vector.contract` is representative of the code we aim to generate
45+
// by vectorisation of `linalg.mmt4d`.
46+
//
47+
// In this specific test we use M == 4, N == 4, and K == 8.
48+
//
49+
50+
// Test the operation where both LHS and RHS are interpreted as signed, hence
51+
// we ultimately emit and execute the `smmla` instruction.
52+
53+
// CHECK-IR-LABEL: llvm.func @test_smmla
54+
// CHECK-IR-COUNT-4: arm_neon.intr.smmla
55+
func.func @test_smmla() {
56+
57+
%c0 = arith.constant 0 : index
58+
%c0_i32 = arith.constant 0 : i32
59+
%c0_i8 = arith.constant 0 : i8
60+
61+
// Accumulator test data
62+
%acc_cst = arith.constant dense<[[-44, 20, 44, -46],
63+
[ -8, 25, -34, 26],
64+
[-20, -36, -3, 39],
65+
[-48, -31, -25, -21]]> : vector<4x4xi32>
66+
67+
%acc_mem = memref.alloca() : memref<4x4xi32>
68+
vector.transfer_write %acc_cst, %acc_mem[%c0, %c0] : vector<4x4xi32>, memref<4x4xi32>
69+
%acc = vector.transfer_read %acc_mem[%c0, %c0], %c0_i32 {in_bounds = [true, true]} : memref<4x4xi32>, vector<4x4xi32>
70+
71+
// LHS test data
72+
%lhs_cst = arith.constant dense<[[-35, -27, -36, -31, 23, -34, -8, -33],
73+
[-20, 17, -32, -47, 37, 22, -7, -21],
74+
[ -7, -35, 20, -4, 39, 46, -23, 40],
75+
[ 40, 27, 37, 43, 38, -6, 37, 49]]> : vector<4x8xi8>
76+
77+
%lhs_mem = memref.alloca() : memref<4x8xi8>
78+
vector.transfer_write %lhs_cst, %lhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
79+
%lhs = vector.transfer_read %lhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
80+
81+
// RHS test data
82+
%rhs_cst = arith.constant dense<[[-17, -50, -1, 48, -13, 22, 39, 33],
83+
[-35, -24, 37, -32, 33, 30, -11, -17],
84+
[-28, 31, 3, -44, -15, -27, 22, 35],
85+
[-23, 39, 48, 26, -23, 32, -39, -38]]> : vector<4x8xi8>
86+
87+
%rhs_mem = memref.alloca() : memref<4x8xi8>
88+
vector.transfer_write %rhs_cst, %rhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
89+
%rhs = vector.transfer_read %rhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
90+
91+
92+
// Matrix multiplication and accumulate with transposed RHS.
93+
%0 = arith.extsi %lhs : vector<4x8xi8> to vector<4x8xi32>
94+
%1 = arith.extsi %rhs : vector<4x8xi8> to vector<4x8xi32>
95+
%2 = vector.contract {indexing_maps = #packed_maps,
96+
iterator_types = ["parallel", "parallel", "reduction"],
97+
kind = #vector.kind<add>} %0, %1, %acc
98+
: vector<4x8xi32>, vector<4x8xi32> into vector<4x4xi32>
99+
100+
// Display the result of the multiplication
101+
vector.print str "Result(SMMLA):\n"
102+
%u0 = vector.extract %2[0] : vector<4xi32> from vector<4x4xi32>
103+
%u1 = vector.extract %2[1] : vector<4xi32> from vector<4x4xi32>
104+
%u2 = vector.extract %2[2] : vector<4xi32> from vector<4x4xi32>
105+
%u3 = vector.extract %2[3] : vector<4xi32> from vector<4x4xi32>
106+
vector.print %u0 : vector<4xi32>
107+
vector.print %u1 : vector<4xi32>
108+
vector.print %u2 : vector<4xi32>
109+
vector.print %u3 : vector<4xi32>
110+
111+
return
112+
}
113+
114+
// Test the operation where both LHS and RHS are interpreted as unsigned, hence
115+
// we ultimately emit and execute the `ummla` instruction.
116+
117+
// CHECK-IR-LABEL: llvm.func @test_ummla
118+
// CHECK-IR-COUNT-4: arm_neon.intr.ummla
119+
func.func @test_ummla() {
120+
121+
%c0 = arith.constant 0 : index
122+
%c0_i32 = arith.constant 0 : i32
123+
%c0_i8 = arith.constant 0 : i8
124+
125+
// Accumulator test data
126+
%acc_cst = arith.constant dense<[[16, 16, 48, 40],
127+
[40, 24, 35, 12],
128+
[33, 24, 29, 19],
129+
[28, 13, 33, 18]]> : vector<4x4xi32>
130+
131+
%acc_mem = memref.alloca() : memref<4x4xi32>
132+
vector.transfer_write %acc_cst, %acc_mem[%c0, %c0] : vector<4x4xi32>, memref<4x4xi32>
133+
%acc = vector.transfer_read %acc_mem[%c0, %c0], %c0_i32 {in_bounds = [true, true]} : memref<4x4xi32>, vector<4x4xi32>
134+
135+
// LHS test data
136+
%lhs_cst = arith.constant dense<[[35, 42, 37, 49, 36, 36, 23, 33],
137+
[39, 34, 33, 45, 43, 10, 44, 47],
138+
[18, 35, 29, 25, 36, 33, 28, 29],
139+
[26, 49, 43, 32, 27, 16, 45, 33]]> : vector<4x8xi8>
140+
141+
%lhs_mem = memref.alloca() : memref<4x8xi8>
142+
vector.transfer_write %lhs_cst, %lhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
143+
%lhs = vector.transfer_read %lhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
144+
145+
// RHS test data
146+
%rhs_cst = arith.constant dense<[[18, 31, 37, 35, 44, 22, 37, 28],
147+
[21, 22, 49, 39, 30, 28, 35, 37],
148+
[21, 47, 39, 35, 23, 43, 24, 49],
149+
[49, 49, 40, 32, 37, 20, 47, 40]]> : vector<4x8xi8>
150+
151+
%rhs_mem = memref.alloca() : memref<4x8xi8>
152+
vector.transfer_write %rhs_cst, %rhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
153+
%rhs = vector.transfer_read %rhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
154+
155+
// Matrix multiplication and accumulate with transposed RHS.
156+
%0 = arith.extui %lhs : vector<4x8xi8> to vector<4x8xi32>
157+
%1 = arith.extui %rhs : vector<4x8xi8> to vector<4x8xi32>
158+
%2 = vector.contract {indexing_maps = #packed_maps,
159+
iterator_types = ["parallel", "parallel", "reduction"],
160+
kind = #vector.kind<add>} %0, %1, %acc
161+
: vector<4x8xi32>, vector<4x8xi32> into vector<4x4xi32>
162+
163+
// Display the result of the multiplication
164+
vector.print str "Result(UMMLA):\n"
165+
%u0 = vector.extract %2[0] : vector<4xi32> from vector<4x4xi32>
166+
%u1 = vector.extract %2[1] : vector<4xi32> from vector<4x4xi32>
167+
%u2 = vector.extract %2[2] : vector<4xi32> from vector<4x4xi32>
168+
%u3 = vector.extract %2[3] : vector<4xi32> from vector<4x4xi32>
169+
vector.print %u0 : vector<4xi32>
170+
vector.print %u1 : vector<4xi32>
171+
vector.print %u2 : vector<4xi32>
172+
vector.print %u3 : vector<4xi32>
173+
174+
return
175+
}
176+
177+
// Test the operation where LHS is interpreted as unsigned and RHS is
178+
// interpreted as signed, hence we ultimately emit and execute the `usmmla`
179+
// instruction.
180+
181+
// CHECK-IR-LABEL: llvm.func @test_usmmla
182+
// CHECK-IR-COUNT-4: arm_neon.intr.usmmla
183+
func.func @test_usmmla() {
184+
185+
%c0 = arith.constant 0 : index
186+
%c0_i32 = arith.constant 0 : i32
187+
%c0_i8 = arith.constant 0 : i8
188+
189+
// Accumulator test data
190+
%acc_cst = arith.constant dense<[[-44, 20, 44, -46],
191+
[ -8, 25, -34, 26],
192+
[-20, -36, -3, 39],
193+
[-48, -31, -25, -21]]> : vector<4x4xi32>
194+
195+
%acc_mem = memref.alloca() : memref<4x4xi32>
196+
vector.transfer_write %acc_cst, %acc_mem[%c0, %c0] : vector<4x4xi32>, memref<4x4xi32>
197+
%acc = vector.transfer_read %acc_mem[%c0, %c0], %c0_i32 {in_bounds = [true, true]} : memref<4x4xi32>, vector<4x4xi32>
198+
199+
// LHS test data
200+
%lhs_cst = arith.constant dense<[[153, 161, 24, 157, 211, 154, 52, 27],
201+
[168, 77, 136, 124, 249, 28, 13, 122],
202+
[ 97, 82, 181, 39, 53, 25, 80, 240],
203+
[184, 227, 106, 165, 126, 113, 121, 228]]> : vector<4x8xi8>
204+
205+
%lhs_mem = memref.alloca() : memref<4x8xi8>
206+
vector.transfer_write %lhs_cst, %lhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
207+
%lhs = vector.transfer_read %lhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
208+
209+
// RHS test data
210+
%rhs_cst = arith.constant dense<[[ 40, 27, 37, 43, 38, -6, 37, 49],
211+
[-17, -50, -1, 48, -13, 22, 39, 33],
212+
[-35, -24, 37, -32, 33, 30, -11, -17],
213+
[-28, 31, 3, -44, -15, -27, 22, 35]]> : vector<4x8xi8>
214+
215+
%rhs_mem = memref.alloca() : memref<4x8xi8>
216+
vector.transfer_write %rhs_cst, %rhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
217+
%rhs = vector.transfer_read %rhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
218+
219+
// Matrix multiplication and accumulate with transposed RHS.
220+
%0 = arith.extui %lhs : vector<4x8xi8> to vector<4x8xi32>
221+
%1 = arith.extsi %rhs : vector<4x8xi8> to vector<4x8xi32>
222+
%2 = vector.contract {indexing_maps = #packed_maps,
223+
iterator_types = ["parallel", "parallel", "reduction"],
224+
kind = #vector.kind<add>} %0, %1, %acc
225+
: vector<4x8xi32>, vector<4x8xi32> into vector<4x4xi32>
226+
227+
// Display the result of the multiplication
228+
vector.print str "Result(USMMLA):\n"
229+
%u0 = vector.extract %2[0] : vector<4xi32> from vector<4x4xi32>
230+
%u1 = vector.extract %2[1] : vector<4xi32> from vector<4x4xi32>
231+
%u2 = vector.extract %2[2] : vector<4xi32> from vector<4x4xi32>
232+
%u3 = vector.extract %2[3] : vector<4xi32> from vector<4x4xi32>
233+
vector.print %u0 : vector<4xi32>
234+
vector.print %u1 : vector<4xi32>
235+
vector.print %u2 : vector<4xi32>
236+
vector.print %u3 : vector<4xi32>
237+
238+
return
239+
}
240+
241+
// Test the operation where LHS is interpreted as signed and RHS is interpreted
242+
// as unsigned. In this test we ultimately emit end execute the `usmmla`
243+
// instruction with reversed operands, see `LowerContractionToNeonI8MMPattern.cpp`
244+
// for more details.
245+
246+
// CHECK-IR-LABEL: llvm.func @test_summla
247+
// CHECK-IR-COUNT-4: arm_neon.intr.usmmla
248+
func.func @test_summla() {
249+
250+
%c0 = arith.constant 0 : index
251+
%c0_i32 = arith.constant 0 : i32
252+
%c0_i8 = arith.constant 0 : i8
253+
254+
// Accumulator test data
255+
%acc_cst = arith.constant dense<[[-44, 20, 44, -46],
256+
[ -8, 25, -34, 26],
257+
[-20, -36, -3, 39],
258+
[-48, -31, -25, -21]]> : vector<4x4xi32>
259+
260+
%acc_mem = memref.alloca() : memref<4x4xi32>
261+
vector.transfer_write %acc_cst, %acc_mem[%c0, %c0] : vector<4x4xi32>, memref<4x4xi32>
262+
%acc = vector.transfer_read %acc_mem[%c0, %c0], %c0_i32 {in_bounds = [true, true]} : memref<4x4xi32>, vector<4x4xi32>
263+
264+
// LHS test data
265+
%lhs_cst = arith.constant dense<[[-35, -27, -36, -31, 23, -34, -8, -33],
266+
[-20, 17, -32, -47, 37, 22, -7, -21],
267+
[ -7, -35, 20, -4, 39, 46, -23, 40],
268+
[ 40, 27, 37, 43, 38, -6, 37, 49]]> : vector<4x8xi8>
269+
270+
%lhs_mem = memref.alloca() : memref<4x8xi8>
271+
vector.transfer_write %lhs_cst, %lhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
272+
%lhs = vector.transfer_read %lhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
273+
274+
// RHS test data
275+
%rhs_cst = arith.constant dense<[[125, 171, 138, 187, 108, 175, 82, 99],
276+
[221, 25, 164, 97, 156, 221, 218, 177],
277+
[171, 160, 219, 191, 144, 45, 161, 210],
278+
[223, 165, 123, 99, 108, 86, 37, 92]]> : vector<4x8xi8>
279+
280+
%rhs_mem = memref.alloca() : memref<4x8xi8>
281+
vector.transfer_write %rhs_cst, %rhs_mem[%c0, %c0] : vector<4x8xi8>, memref<4x8xi8>
282+
%rhs = vector.transfer_read %rhs_mem[%c0, %c0], %c0_i8 {in_bounds = [true, true]} : memref<4x8xi8>, vector<4x8xi8>
283+
284+
// Matrix multiplication and accumulate with transposed RHS.
285+
%0 = arith.extsi %lhs : vector<4x8xi8> to vector<4x8xi32>
286+
%1 = arith.extui %rhs : vector<4x8xi8> to vector<4x8xi32>
287+
%2 = vector.contract {indexing_maps = #packed_maps,
288+
iterator_types = ["parallel", "parallel", "reduction"],
289+
kind = #vector.kind<add>} %0, %1, %acc
290+
: vector<4x8xi32>, vector<4x8xi32> into vector<4x4xi32>
291+
292+
// Display the result of the multiplication
293+
vector.print str "Result(SUMMLA (i.e. USMMLA transposed)):\n"
294+
%u0 = vector.extract %2[0] : vector<4xi32> from vector<4x4xi32>
295+
%u1 = vector.extract %2[1] : vector<4xi32> from vector<4x4xi32>
296+
%u2 = vector.extract %2[2] : vector<4xi32> from vector<4x4xi32>
297+
%u3 = vector.extract %2[3] : vector<4xi32> from vector<4x4xi32>
298+
vector.print %u0 : vector<4xi32>
299+
vector.print %u1 : vector<4xi32>
300+
vector.print %u2 : vector<4xi32>
301+
vector.print %u3 : vector<4xi32>
302+
303+
return
304+
}
305+
306+
func.func @main() {
307+
// CHECK-LABEL: Result(SMMLA):
308+
// CHECK: ( -1999, 1941, 685, -2879 )
309+
// CHECK: ( -3705, 2952, 987, -685 )
310+
// CHECK: ( 2565, 4157, -1589, -357 )
311+
// CHECK: ( 2383, -2252, 32, -1365 )
312+
func.call @test_smmla() : () -> ()
313+
314+
// CHECK-LABEL: Result(UMMLA):
315+
// CHECK: ( 9183, 9513, 10460, 11314 )
316+
// CHECK: ( 9648, 9812, 10092, 12088 )
317+
// CHECK: ( 7548, 7625, 8398, 9044 )
318+
// CHECK: ( 8855, 9046, 9685, 11191 )
319+
func.call @test_ummla() : () -> ()
320+
321+
// CHECK-LABEL: Result(USMMLA):
322+
// CHECK: ( 28403, 445, -2759, -11409 )
323+
// CHECK: ( 34908, 1047, 142, -7274 )
324+
// CHECK: ( 31032, 6807, -2378, 7382 )
325+
// CHECK: ( 44217, 6396, -10930, 623 )
326+
func.call @test_usmmla() : () -> ()
327+
328+
// CHECK-LABEL: Result(SUMMLA (i.e. USMMLA transposed)):
329+
// CHECK: ( -27190, -28812, -30502, -23575 )
330+
// CHECK: ( -7613, -8386, -15938, -6521 )
331+
// CHECK: ( 9468, 18750, 9199, 5764 )
332+
// CHECK: ( 33655, 41064, 48900, 31627 )
333+
func.call @test_summla() : () -> ()
334+
335+
return
336+
}

0 commit comments

Comments
 (0)