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 enable-buffer-initialization=true 25// RUN: %{compile} | %{run} | FileCheck %s 26// 27// Do the same run, but now with direct IR generation and vectorization. 28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true 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 direct IR generation and VLA vectorization. 32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %} 33 34// Reduction in this file _are_ supported by the AArch64 SVE backend 35 36#SparseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : compressed)}> 37#CSR = #sparse_tensor.encoding<{map = (d0, d1) -> (d0 : dense, d1 : compressed)}> 38#CSC = #sparse_tensor.encoding<{ 39 map = (d0, d1) -> (d1 : dense, d0 : compressed) 40}> 41 42// 43// Traits for tensor operations. 44// 45#trait_matmul = { 46 indexing_maps = [ 47 affine_map<(i,j,k) -> (i,k)>, // A 48 affine_map<(i,j,k) -> (k,j)>, // B 49 affine_map<(i,j,k) -> (i,j)> // C (out) 50 ], 51 iterator_types = ["parallel", "parallel", "reduction"], 52 doc = "C(i,j) = SUM_k A(i,k) * B(k,j)" 53} 54 55module { 56 func.func @min_plus_csrcsr(%arga: tensor<?x?xf64, #CSR>, 57 %argb: tensor<?x?xf64, #CSR>) -> tensor<?x?xf64, #CSR> { 58 %c0 = arith.constant 0 : index 59 %c1 = arith.constant 1 : index 60 %maxf = arith.constant 1.0e999 : f64 61 %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #CSR> 62 %d1 = tensor.dim %argb, %c1 : tensor<?x?xf64, #CSR> 63 %xm = tensor.empty(%d0, %d1) : tensor<?x?xf64, #CSR> 64 %0 = linalg.generic #trait_matmul 65 ins(%arga, %argb: tensor<?x?xf64, #CSR>, tensor<?x?xf64, #CSR>) 66 outs(%xm: tensor<?x?xf64, #CSR>) { 67 ^bb(%a: f64, %b: f64, %output: f64): 68 %1 = sparse_tensor.binary %a, %b : f64, f64 to f64 69 overlap = { 70 ^bb0(%x: f64, %y: f64): 71 %3 = arith.addf %x, %y : f64 72 sparse_tensor.yield %3 : f64 73 } 74 left={} 75 right={} 76 %2 = sparse_tensor.reduce %1, %output, %maxf : f64 { 77 ^bb0(%x: f64, %y: f64): 78 %cmp = arith.cmpf "olt", %x, %y : f64 79 %3 = arith.select %cmp, %x, %y : f64 80 sparse_tensor.yield %3 : f64 81 } 82 linalg.yield %2 : f64 83 } -> tensor<?x?xf64, #CSR> 84 return %0 : tensor<?x?xf64, #CSR> 85 } 86 87 func.func @min_plus_csrcsc(%arga: tensor<?x?xf64, #CSR>, 88 %argb: tensor<?x?xf64, #CSC>) -> tensor<?x?xf64, #CSR> { 89 %c0 = arith.constant 0 : index 90 %c1 = arith.constant 1 : index 91 %maxf = arith.constant 1.0e999 : f64 92 %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #CSR> 93 %d1 = tensor.dim %argb, %c1 : tensor<?x?xf64, #CSC> 94 %xm = tensor.empty(%d0, %d1) : tensor<?x?xf64, #CSR> 95 %0 = linalg.generic #trait_matmul 96 ins(%arga, %argb: tensor<?x?xf64, #CSR>, tensor<?x?xf64, #CSC>) 97 outs(%xm: tensor<?x?xf64, #CSR>) { 98 ^bb(%a: f64, %b: f64, %output: f64): 99 %1 = sparse_tensor.binary %a, %b : f64, f64 to f64 100 overlap = { 101 ^bb0(%x: f64, %y: f64): 102 %3 = arith.addf %x, %y : f64 103 sparse_tensor.yield %3 : f64 104 } 105 left={} 106 right={} 107 %2 = sparse_tensor.reduce %1, %output, %maxf : f64 { 108 ^bb0(%x: f64, %y: f64): 109 %cmp = arith.cmpf "olt", %x, %y : f64 110 %3 = arith.select %cmp, %x, %y : f64 111 sparse_tensor.yield %3 : f64 112 } 113 linalg.yield %2 : f64 114 } -> tensor<?x?xf64, #CSR> 115 return %0 : tensor<?x?xf64, #CSR> 116 } 117 118 // Driver method to call and verify vector kernels. 119 func.func @main() { 120 %c0 = arith.constant 0 : index 121 122 // Setup sparse matrices. 123 %m1 = arith.constant sparse< 124 [ [0,0], [0,1], [1,0], [2,2], [2,3], [2,4], [3,0], [3,2], [3,3] ], 125 [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] 126 > : tensor<4x5xf64> 127 %m2 = arith.constant sparse< 128 [ [0,0], [1,3], [2,0], [2,3], [3,1], [4,1] ], 129 [6.0, 5.0, 4.0, 3.0, 2.0, 11.0 ] 130 > : tensor<5x4xf64> 131 %sm1 = sparse_tensor.convert %m1 : tensor<4x5xf64> to tensor<?x?xf64, #CSR> 132 %sm2r = sparse_tensor.convert %m2 : tensor<5x4xf64> to tensor<?x?xf64, #CSR> 133 %sm2c = sparse_tensor.convert %m2 : tensor<5x4xf64> to tensor<?x?xf64, #CSC> 134 135 // Call sparse matrix kernels. 136 %5 = call @min_plus_csrcsr(%sm1, %sm2r) 137 : (tensor<?x?xf64, #CSR>, tensor<?x?xf64, #CSR>) -> tensor<?x?xf64, #CSR> 138 %6 = call @min_plus_csrcsc(%sm1, %sm2c) 139 : (tensor<?x?xf64, #CSR>, tensor<?x?xf64, #CSC>) -> tensor<?x?xf64, #CSR> 140 141 // 142 // Verify the results. 143 // 144 // CHECK: ---- Sparse Tensor ---- 145 // CHECK-NEXT: nse = 9 146 // CHECK-NEXT: dim = ( 4, 5 ) 147 // CHECK-NEXT: lvl = ( 4, 5 ) 148 // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 ) 149 // CHECK-NEXT: crd[1] : ( 0, 1, 0, 2, 3, 4, 0, 2, 3 ) 150 // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) 151 // CHECK-NEXT: ---- 152 // CHECK: ---- Sparse Tensor ---- 153 // CHECK-NEXT: nse = 6 154 // CHECK-NEXT: dim = ( 5, 4 ) 155 // CHECK-NEXT: lvl = ( 5, 4 ) 156 // CHECK-NEXT: pos[1] : ( 0, 1, 2, 4, 5, 6 ) 157 // CHECK-NEXT: crd[1] : ( 0, 3, 0, 3, 1, 1 ) 158 // CHECK-NEXT: values : ( 6, 5, 4, 3, 2, 11 ) 159 // CHECK-NEXT: ---- 160 // CHECK: ---- Sparse Tensor ---- 161 // CHECK-NEXT: nse = 9 162 // CHECK-NEXT: dim = ( 4, 4 ) 163 // CHECK-NEXT: lvl = ( 4, 4 ) 164 // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 ) 165 // CHECK-NEXT: crd[1] : ( 0, 3, 0, 0, 1, 3, 0, 1, 3 ) 166 // CHECK-NEXT: values : ( 7, 7, 9, 8, 7, 7, 12, 11, 11 ) 167 // CHECK-NEXT: ---- 168 // CHECK: ---- Sparse Tensor ---- 169 // CHECK-NEXT: nse = 9 170 // CHECK-NEXT: dim = ( 4, 4 ) 171 // CHECK-NEXT: lvl = ( 4, 4 ) 172 // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 ) 173 // CHECK-NEXT: crd[1] : ( 0, 3, 0, 0, 1, 3, 0, 1, 3 ) 174 // CHECK-NEXT: values : ( 7, 7, 9, 8, 7, 7, 12, 11, 11 ) 175 // CHECK-NEXT: ---- 176 // 177 sparse_tensor.print %sm1 : tensor<?x?xf64, #CSR> 178 sparse_tensor.print %sm2r : tensor<?x?xf64, #CSR> 179 sparse_tensor.print %5 : tensor<?x?xf64, #CSR> 180 sparse_tensor.print %6 : tensor<?x?xf64, #CSR> 181 182 // Release the resources. 183 bufferization.dealloc_tensor %sm1 : tensor<?x?xf64, #CSR> 184 bufferization.dealloc_tensor %sm2r : tensor<?x?xf64, #CSR> 185 bufferization.dealloc_tensor %sm2c : tensor<?x?xf64, #CSC> 186 bufferization.dealloc_tensor %5 : tensor<?x?xf64, #CSR> 187 bufferization.dealloc_tensor %6 : tensor<?x?xf64, #CSR> 188 return 189 } 190} 191