139 lines · plain
1// DEFINE: %{compile} = mlir-opt %s \2// DEFINE: -transform-interpreter -test-transform-dialect-erase-schedule \3// DEFINE: -one-shot-bufferize="bufferize-function-boundaries" -buffer-deallocation-pipeline -cse -canonicalize -convert-vector-to-scf -arm-sve-legalize-vector-storage \4// DEFINE: -convert-vector-to-llvm="enable-arm-sve" -test-lower-to-llvm -o %t5// DEFINE: %{entry_point} = matmul_f326// DEFINE: %{run} = %mcr_aarch64_cmd %t -e %{entry_point} -entry-point-result=void --march=aarch64 --mattr="+sve"\7// DEFINE: -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils8 9// RUN: %{compile}10 11// RUN: %{run} | FileCheck %s --check-prefix=F3212 13// REDEFINE: %{entry_point} = matmul_mixed_ty14// RUN: %{run} | FileCheck %s --check-prefix=MIXED15 16func.func @matmul_f32() {17 // Matrix dimensions18 %K = arith.constant 3 : index19 %M = arith.constant 5 : index20 %N = arith.constant 15 : index21 %c0_f32 = arith.constant 0.0 : f3222 23 // Allocate the matrices24 %A_alloc = bufferization.alloc_tensor(%M, %K) : tensor<?x?xf32>25 %B_alloc = bufferization.alloc_tensor(%K, %N) : tensor<?x?xf32>26 %C_alloc = bufferization.alloc_tensor(%M, %N) : tensor<?x?xf32>27 28 // Initialise the matrices29 %pi = arith.constant 3.14 : f3230 %A = linalg.fill ins(%pi : f32) outs(%A_alloc : tensor<?x?xf32>) -> tensor<?x?xf32>31 %B = linalg.fill ins(%pi : f32) outs(%B_alloc : tensor<?x?xf32>) -> tensor<?x?xf32>32 %C_in = linalg.fill ins(%c0_f32 : f32) outs(%C_alloc : tensor<?x?xf32>) -> tensor<?x?xf32>33 34 // Matmul35 %C_out = linalg.matmul ins(%A, %B: tensor<?x?xf32>, tensor<?x?xf32>) outs(%C_in: tensor<?x?xf32>) -> tensor<?x?xf32>36 37 // Print and verify the output38 // F32-LABEL: SVE: START OF TEST OUTPUT39 vector.print str "SVE: START OF TEST OUTPUT\n"40 41 // F32-NEXT: Unranked Memref {{.*}} rank = 2 offset = 0 sizes = [5, 15] strides = [15, 1] data =42 // F32-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]43 %xf = tensor.cast %C_out : tensor<?x?xf32> to tensor<*xf32>44 call @printMemrefF32(%xf) : (tensor<*xf32>) -> ()45 46 // F32-NEXT: SVE: END OF TEST OUTPUT47 vector.print str "SVE: END OF TEST OUTPUT\n"48 49 return50}51 52func.func @matmul_mixed_ty() {53 // Matrix dimensions54 %K = arith.constant 3 : index55 %M = arith.constant 5 : index56 %N = arith.constant 15 : index57 %c0_i8 = arith.constant 0 : i858 %c0_i32 = arith.constant 0 : i3259 60 // Allocate the matrices61 %A_alloc = bufferization.alloc_tensor(%M, %K) : tensor<?x?xi8>62 %B_alloc = bufferization.alloc_tensor(%K, %N) : tensor<?x?xi8>63 %C_alloc = bufferization.alloc_tensor(%M, %N) : tensor<?x?xi32>64 65 // Initialise the matrices66 %pi = arith.constant 123 : i867 %A = linalg.fill ins(%pi : i8) outs(%A_alloc : tensor<?x?xi8>) -> tensor<?x?xi8>68 %B = linalg.fill ins(%pi : i8) outs(%B_alloc : tensor<?x?xi8>) -> tensor<?x?xi8>69 %C_in = linalg.fill ins(%c0_i32 : i32) outs(%C_alloc : tensor<?x?xi32>) -> tensor<?x?xi32>70 71 // Matmul72 %C_out = linalg.matmul ins(%A, %B: tensor<?x?xi8>, tensor<?x?xi8>) outs(%C_in: tensor<?x?xi32>) -> tensor<?x?xi32>73 74 // Print and verify the output75 // MIXED-LABEL: SVE: START OF TEST OUTPUT76 vector.print str "SVE: START OF TEST OUTPUT\n"77 78 // MIXED-NEXT: Unranked Memref {{.*}} rank = 2 offset = 0 sizes = [5, 15] strides = [15, 1] data =79 // MIXED-COUNT-5: [45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387, 45387]80 %xf = tensor.cast %C_out : tensor<?x?xi32> to tensor<*xi32>81 call @printMemrefI32(%xf) : (tensor<*xi32>) -> ()82 83 // MIXED-NEXT: SVE: END OF TEST OUTPUT84 vector.print str "SVE: END OF TEST OUTPUT\n"85 86 return87}88 89module attributes {transform.with_named_sequence} {90 // A sequence that will tile and vectorise a Matmul Op91 transform.named_sequence @tile_and_vectorize_matmul(%func92 : !transform.op<"func.func"> {transform.readonly}) {93 94 // Step 0: Get a handle to the matmul Op95 %matmul = transform.structured.match ops{["linalg.matmul"]} in %func96 : (!transform.op<"func.func">) -> !transform.any_op97 98 // Step 1: Tile99 %tiled_matmul, %loops:3 = transform.structured.tile_using_for %matmul tile_sizes [2, [4], 1]100 : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)101 102 // Step 2: Vectorize103 transform.structured.vectorize %tiled_matmul vector_sizes [2, [4], 1] : !transform.any_op104 105 // Step 3: Lower vector.multi_reduction to vector.contract (+ some helpful patterns)106 transform.apply_patterns to %func {107 transform.apply_patterns.vector.reduction_to_contract108 transform.apply_patterns.vector.transfer_permutation_patterns109 transform.apply_patterns.vector.lower_masked_transfers110 transform.apply_patterns.vector.sink_ops111 } : !transform.op<"func.func">112 113 // Step 4: Lower vector.contract to vector.fma114 transform.apply_patterns to %func {115 transform.apply_patterns.vector.lower_contraction lowering_strategy = "outerproduct"116 transform.apply_patterns.vector.lower_outerproduct117 } : !transform.op<"func.func">118 119 transform.yield120 }121 122 // A sequence that goes over all functions in tis module and applies123 // "tile_and_vectorize_matmul"124 transform.named_sequence @__transform_main(%module: !transform.any_op {transform.readonly}) {125 %funcs = transform.structured.match ops{["func.func"]} in %module126 : (!transform.any_op) -> !transform.op<"func.func">127 128 transform.foreach %funcs : !transform.op<"func.func"> {129 ^bb2(%func : !transform.op<"func.func">):130 transform.include @tile_and_vectorize_matmul failures(propagate)131 (%func) : (!transform.op<"func.func">) -> ()132 }133 transform.yield134 }135}136 137func.func private @printMemrefF32(%ptr : tensor<*xf32>)138func.func private @printMemrefI32(%ptr : tensor<*xi32>)139