Skip to content

Commit fe8c649

Browse files
authored
[mlir][linalg] Add an e2e test for linalg.matmul_transpose_a to ArmSME (#71644)
This patch adds an integration test demonstrating the first e2e example lowering a linalg.matmul to SME via vector.outerproduct. The test uses a 'linalg.matmul_transpose_a' rather than 'linalg.matmul' since the latter emits a 'vector.transfer_read' with a vector type of 'vector<[4]x1xf32>' that can't be currently lowered via generic (SVE) path, since it has leading scalable dim.
1 parent 4240b17 commit fe8c649

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// RUN: mlir-opt %s \
2+
// RUN: -transform-interpreter -test-transform-dialect-erase-schedule \
3+
// RUN: -one-shot-bufferize="bufferize-function-boundaries" -canonicalize \
4+
// RUN: -enable-arm-streaming="mode=locally enable-za" \
5+
// RUN: -convert-vector-to-arm-sme -convert-arm-sme-to-scf \
6+
// RUN: -convert-vector-to-scf -cse -arm-sve-legalize-vector-storage \
7+
// RUN: -convert-vector-to-llvm=enable-arm-sme \
8+
// RUN: -convert-vector-to-llvm=enable-arm-sve \
9+
// RUN: -cse -canonicalize -allocate-arm-sme-tiles -test-lower-to-llvm | \
10+
// RUN: %mcr_aarch64_cmd \
11+
// RUN: -e=main -entry-point-result=void \
12+
// RUN: -march=aarch64 -mattr="+sve,+sme" \
13+
// RUN: -shared-libs=%mlir_runner_utils,%mlir_c_runner_utils | \
14+
// RUN: FileCheck %s
15+
16+
func.func @matmul_transpose_a(%A : tensor<?x?xf32>, %B : tensor<?x?xf32>, %C : tensor<?x?xf32>) {
17+
%res = linalg.matmul_transpose_a ins(%A, %B: tensor<?x?xf32>, tensor<?x?xf32>)
18+
outs(%C: tensor<?x?xf32>) -> tensor<?x?xf32>
19+
%xf = tensor.cast %res : tensor<?x?xf32> to tensor<*xf32>
20+
call @printMemrefF32(%xf) : (tensor<*xf32>) -> ()
21+
return
22+
}
23+
24+
func.func @main() {
25+
%c0 = arith.constant 0 : i32
26+
%c7 = arith.constant 7 : index
27+
28+
%A = arith.constant dense<[
29+
[ 1., 2., 3., 4., 5., 6., 7.],
30+
[ 8., 9., 10., 11., 12., 13., 14.],
31+
[15., 16., 17., 18., 19., 20., 21.],
32+
[22., 23., 24., 25., 26., 27., 28.],
33+
[29., 30., 31., 32., 33., 34., 35.],
34+
[36., 37., 38., 39., 40., 41., 42.],
35+
[43., 44., 45., 46., 47., 48., 49.],
36+
[50., 51., 52., 53., 54., 55., 56.],
37+
[57., 58., 59., 60., 61., 62., 63.],
38+
[64., 65., 66., 67., 68., 69., 70.],
39+
[71., 72., 73., 74., 75., 76., 77.],
40+
[78., 79., 80., 81., 82., 83., 84.],
41+
[85., 86., 87., 88., 89., 90., 91.]
42+
]> : tensor<13x7xf32>
43+
44+
%A_dyn = tensor.cast %A : tensor<13x7xf32> to tensor<?x?xf32>
45+
46+
%C_init = bufferization.alloc_tensor(%c7, %c7) : tensor<?x?xf32>
47+
%C = linalg.fill ins(%c0 : i32) outs(%C_init : tensor<?x?xf32>) -> tensor<?x?xf32>
48+
49+
// CHECK: Unranked Memref {{.*}} rank = 2 offset = 0 sizes = [7, 7] strides = [7, 1] data =
50+
// CHECK: [32955, 33514, 34073, 34632, 35191, 35750, 36309]
51+
// CHECK: [33514, 34086, 34658, 35230, 35802, 36374, 36946]
52+
// CHECK: [34073, 34658, 35243, 35828, 36413, 36998, 37583]
53+
// CHECK: [34632, 35230, 35828, 36426, 37024, 37622, 38220]
54+
// CHECK: [35191, 35802, 36413, 37024, 37635, 38246, 38857]
55+
// CHECK: [35750, 36374, 36998, 37622, 38246, 38870, 39494]
56+
// CHECK: [36309, 36946, 37583, 38220, 38857, 39494, 40131]
57+
call @matmul_transpose_a(%A_dyn, %A_dyn, %C) : (tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) -> ()
58+
59+
return
60+
}
61+
62+
module attributes {transform.with_named_sequence} {
63+
transform.named_sequence @__transform_main(%module : !transform.any_op {transform.readonly}) {
64+
%matmul_transpose_a = transform.structured.match ops{["linalg.matmul_transpose_a"]} in %module
65+
: (!transform.any_op) -> !transform.any_op
66+
67+
// Step 1: Tile for size [4] x [4], which corresponds to SVLs x SVLs, where
68+
// SVLs is the number of 32-bit elements in a vector of SVL bits.
69+
%tiled_linalg_op, %loops:3 = transform.structured.tile_using_for %matmul_transpose_a[[4], [4], 1]
70+
: (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
71+
72+
// Step 2: Vectorize.
73+
transform.structured.vectorize %tiled_linalg_op vector_sizes [[4], [4], 1]
74+
: !transform.any_op
75+
76+
%func = transform.structured.match ops{["func.func"]} in %module
77+
: (!transform.any_op) -> !transform.any_op
78+
79+
// Step 3: Lower vector.multi_reduction to vector.contract (+ some helpful patterns).
80+
transform.apply_patterns to %func {
81+
transform.apply_patterns.vector.lower_masked_transfers
82+
transform.apply_patterns.vector.transfer_permutation_patterns
83+
transform.apply_patterns.vector.reduction_to_contract
84+
} : !transform.any_op
85+
86+
// Step 4: Lower vector.contract to vector.outerproduct.
87+
transform.apply_patterns to %func {
88+
transform.apply_patterns.vector.lower_contraction lowering_strategy = "outerproduct"
89+
transform.apply_patterns.vector.lower_masks
90+
} : !transform.any_op
91+
92+
transform.yield
93+
}
94+
}
95+
96+
func.func private @printMemrefF32(%ptr : tensor<*xf32>)

0 commit comments

Comments
 (0)