1//-------------------------------------------------------------------------------------------------- 2// WHEN CREATING A NEW TEST, PLEASE JUST COPY & PASTE WITHOUT EDITS. 3// 4// Set-up that's shared across all tests in this directory. In principle, this 5// config could be moved to lit.local.cfg. However, there are downstream users that 6// do not use these LIT config files. Hence why this is kept inline. 7// 8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true 9// DEFINE: %{sparsifier_opts_sve} = enable-arm-sve=true %{sparsifier_opts} 10// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}" 11// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}" 12// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils 13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils 14// DEFINE: %{run_opts} = -e main -entry-point-result=void 15// DEFINE: %{run} = mlir-runner %{run_opts} %{run_libs} 16// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs_sve} 17// 18// DEFINE: %{env} = 19//-------------------------------------------------------------------------------------------------- 20 21// RUN: %{compile} | %{run} | FileCheck %s 22// 23// Do the same run, but now with direct IR generation. 24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false 25// RUN: %{compile} | %{run} | FileCheck %s 26// 27// Do the same run, but now with vectorization. 28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true 29// RUN: %{compile} | %{run} | FileCheck %s 30// 31// Do the same run, but now with VLA vectorization. 32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %} 33 34!Filename = !llvm.ptr 35 36#SparseMatrix = #sparse_tensor.encoding<{ 37 map = (d0, d1) -> (d0 : compressed, d1 : compressed) 38}> 39 40#trait_sum_reduce = { 41 indexing_maps = [ 42 affine_map<(i,j) -> (i,j)>, // A 43 affine_map<(i,j) -> ()> // x (out) 44 ], 45 iterator_types = ["reduction", "reduction"], 46 doc = "x += A(i,j)" 47} 48 49module { 50 // 51 // A kernel that sum-reduces a matrix to a single scalar. 52 // 53 func.func @kernel_sum_reduce(%arga: tensor<?x?xf16, #SparseMatrix>, 54 %argx: tensor<f16>) -> tensor<f16> { 55 %0 = linalg.generic #trait_sum_reduce 56 ins(%arga: tensor<?x?xf16, #SparseMatrix>) 57 outs(%argx: tensor<f16>) { 58 ^bb(%a: f16, %x: f16): 59 %0 = arith.addf %x, %a : f16 60 linalg.yield %0 : f16 61 } -> tensor<f16> 62 return %0 : tensor<f16> 63 } 64 65 func.func private @getTensorFilename(index) -> (!Filename) 66 67 // 68 // Main driver that reads matrix from file and calls the sparse kernel. 69 // 70 func.func @main() { 71 // Setup input sparse matrix from compressed constant. 72 %d = arith.constant dense <[ 73 [ 1.1, 1.2, 0.0, 1.4 ], 74 [ 0.0, 0.0, 0.0, 0.0 ], 75 [ 3.1, 0.0, 3.3, 3.4 ] 76 ]> : tensor<3x4xf16> 77 %a = sparse_tensor.convert %d : tensor<3x4xf16> to tensor<?x?xf16, #SparseMatrix> 78 79 %d0 = arith.constant 0.0 : f16 80 // Setup memory for a single reduction scalar, 81 // initialized to zero. 82 %x = tensor.from_elements %d0 : tensor<f16> 83 84 // Call the kernel. 85 %0 = call @kernel_sum_reduce(%a, %x) 86 : (tensor<?x?xf16, #SparseMatrix>, tensor<f16>) -> tensor<f16> 87 88 // Print the result for verification. 89 // 90 // CHECK: 13.5 91 // 92 %v = tensor.extract %0[] : tensor<f16> 93 %vf = arith.extf %v: f16 to f32 94 vector.print %vf : f32 95 96 // Release the resources. 97 bufferization.dealloc_tensor %0 : tensor<f16> 98 bufferization.dealloc_tensor %a : tensor<?x?xf16, #SparseMatrix> 99 100 return 101 } 102} 103