Skip to content

Commit 45e2e03

Browse files
authored
[mlir][SVE] Add an e2e test for vectorization of linalg.matmul (#70372)
Adds an end-to-end test for scalable vectorization of linalg.matmul.
1 parent e4dc7d4 commit 45e2e03

File tree

1 file changed

+68
-0
lines changed
  • mlir/test/Integration/Dialect/Linalg/CPU/ArmSVE

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// RUN: mlir-opt %s -test-transform-dialect-interpreter -test-transform-dialect-erase-schedule \
2+
// RUN: -one-shot-bufferize -func-bufferize -cse -canonicalize -convert-vector-to-scf -arm-sve-legalize-vector-storage \
3+
// RUN: -convert-vector-to-llvm="enable-arm-sve" -test-lower-to-llvm | \
4+
// RUN: %mcr_aarch64_cmd -e=matmul_f32 -entry-point-result=void --march=aarch64 --mattr="+sve" -shared-libs=%mlir_runner_utils,%mlir_c_runner_utils | \
5+
// RUN: FileCheck %s
6+
7+
func.func @matmul_f32() {
8+
// Matrix dimensions
9+
%K = arith.constant 3 : index
10+
%M = arith.constant 5 : index
11+
%N = arith.constant 15 : index
12+
%c0_f32 = arith.constant 0.0 : f32
13+
14+
// Allocate the matrices
15+
%A_alloc = bufferization.alloc_tensor(%M, %K) : tensor<?x?xf32>
16+
%B_alloc = bufferization.alloc_tensor(%K, %N) : tensor<?x?xf32>
17+
%C_alloc = bufferization.alloc_tensor(%M, %N) : tensor<?x?xf32>
18+
19+
// Initialise the matrices
20+
%pi = arith.constant 3.14 : f32
21+
%A = linalg.fill ins(%pi : f32) outs(%A_alloc : tensor<?x?xf32>) -> tensor<?x?xf32>
22+
%B = linalg.fill ins(%pi : f32) outs(%B_alloc : tensor<?x?xf32>) -> tensor<?x?xf32>
23+
%C_in = linalg.fill ins(%c0_f32 : f32) outs(%C_alloc : tensor<?x?xf32>) -> tensor<?x?xf32>
24+
25+
// Matmul
26+
%C_out = linalg.matmul ins(%A, %B: tensor<?x?xf32>, tensor<?x?xf32>) outs(%C_in: tensor<?x?xf32>) -> tensor<?x?xf32>
27+
28+
// Print and verify the output
29+
// CHECK-LABEL: SVE: START OF TEST OUTPUT
30+
vector.print str "SVE: START OF TEST OUTPUT"
31+
32+
// CHECK-NEXT: Unranked Memref {{.*}} rank = 2 offset = 0 sizes = [5, 15] strides = [15, 1] data =
33+
// CHECK-COUNT-5: [29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788, 29.5788]
34+
%xf = tensor.cast %C_out : tensor<?x?xf32> to tensor<*xf32>
35+
call @printMemrefF32(%xf) : (tensor<*xf32>) -> ()
36+
37+
// CHECK-NEXT: SVE: END OF TEST OUTPUT
38+
vector.print str "SVE: END OF TEST OUTPUT"
39+
40+
return
41+
}
42+
43+
transform.sequence failures(propagate) {
44+
^bb1(%module_op: !transform.any_op):
45+
// Step 1: Tile
46+
%matmul = transform.structured.match ops{["linalg.matmul"]} in %module_op : (!transform.any_op) -> !transform.any_op
47+
%func_op = get_parent_op %matmul : (!transform.any_op) -> !transform.op<"func.func">
48+
%module_with_tiled_loops, %loops:3 = transform.structured.tile_using_for %matmul [2, [4], 1] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
49+
50+
// Step 2: Vectorize
51+
%tiled_matmul = transform.structured.match ops{["linalg.matmul"]} in %module_with_tiled_loops : (!transform.any_op) -> !transform.any_op
52+
transform.structured.vectorize %tiled_matmul vector_sizes [2, [4], 1] : !transform.any_op
53+
54+
// Step 3: Lower vector.multi_reduction to vector.contract (+ some helpful patterns)
55+
transform.apply_patterns to %func_op {
56+
transform.apply_patterns.vector.reduction_to_contract
57+
transform.apply_patterns.vector.transfer_permutation_patterns
58+
transform.apply_patterns.vector.lower_masked_transfers
59+
} : !transform.op<"func.func">
60+
61+
// Step 4: Lower vector.contract to vector.fma
62+
transform.apply_patterns to %func_op {
63+
transform.apply_patterns.vector.lower_contraction lowering_strategy = "outerproduct"
64+
transform.apply_patterns.vector.lower_outerproduct
65+
} : !transform.op<"func.func">
66+
}
67+
68+
func.func private @printMemrefF32(%ptr : tensor<*xf32>)

0 commit comments

Comments
 (0)