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 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 VLA vectorization. 32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %} 33 34#SparseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : compressed)}> 35#DCSR = #sparse_tensor.encoding<{map = (d0, d1) -> (d0 : compressed, d1 : compressed)}> 36 37// 38// Traits for tensor operations. 39// 40#trait_vec = { 41 indexing_maps = [ 42 affine_map<(i) -> (i)>, // a (in) 43 affine_map<(i) -> (i)> // x (out) 44 ], 45 iterator_types = ["parallel"] 46} 47#trait_mat = { 48 indexing_maps = [ 49 affine_map<(i,j) -> (i,j)>, // A (in) 50 affine_map<(i,j) -> (i,j)> // X (out) 51 ], 52 iterator_types = ["parallel", "parallel"] 53} 54 55module { 56 // Invert the structure of a sparse vector. Present values become missing. 57 // Missing values are filled with 1 (i32). Output is sparse. 58 func.func @vector_complement_sparse(%arga: tensor<?xf64, #SparseVector>) -> tensor<?xi32, #SparseVector> { 59 %c = arith.constant 0 : index 60 %ci1 = arith.constant 1 : i32 61 %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector> 62 %xv = tensor.empty(%d) : tensor<?xi32, #SparseVector> 63 %0 = linalg.generic #trait_vec 64 ins(%arga: tensor<?xf64, #SparseVector>) 65 outs(%xv: tensor<?xi32, #SparseVector>) { 66 ^bb(%a: f64, %x: i32): 67 %1 = sparse_tensor.unary %a : f64 to i32 68 present={} 69 absent={ 70 sparse_tensor.yield %ci1 : i32 71 } 72 linalg.yield %1 : i32 73 } -> tensor<?xi32, #SparseVector> 74 return %0 : tensor<?xi32, #SparseVector> 75 } 76 77 // Invert the structure of a sparse vector, where missing values are 78 // filled with 1. For a dense output, the sparsifier initializes 79 // the buffer to all zero at all other places. 80 func.func @vector_complement_dense(%arga: tensor<?xf64, #SparseVector>) -> tensor<?xi32> { 81 %c = arith.constant 0 : index 82 %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector> 83 %xv = tensor.empty(%d) : tensor<?xi32> 84 %0 = linalg.generic #trait_vec 85 ins(%arga: tensor<?xf64, #SparseVector>) 86 outs(%xv: tensor<?xi32>) { 87 ^bb(%a: f64, %x: i32): 88 %1 = sparse_tensor.unary %a : f64 to i32 89 present={} 90 absent={ 91 %ci1 = arith.constant 1 : i32 92 sparse_tensor.yield %ci1 : i32 93 } 94 linalg.yield %1 : i32 95 } -> tensor<?xi32> 96 return %0 : tensor<?xi32> 97 } 98 99 // Negate existing values. Fill missing ones with +1. 100 func.func @vector_negation(%arga: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> { 101 %c = arith.constant 0 : index 102 %cf1 = arith.constant 1.0 : f64 103 %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector> 104 %xv = tensor.empty(%d) : tensor<?xf64, #SparseVector> 105 %0 = linalg.generic #trait_vec 106 ins(%arga: tensor<?xf64, #SparseVector>) 107 outs(%xv: tensor<?xf64, #SparseVector>) { 108 ^bb(%a: f64, %x: f64): 109 %1 = sparse_tensor.unary %a : f64 to f64 110 present={ 111 ^bb0(%x0: f64): 112 %ret = arith.negf %x0 : f64 113 sparse_tensor.yield %ret : f64 114 } 115 absent={ 116 sparse_tensor.yield %cf1 : f64 117 } 118 linalg.yield %1 : f64 119 } -> tensor<?xf64, #SparseVector> 120 return %0 : tensor<?xf64, #SparseVector> 121 } 122 123 // Performs B[i] = i * A[i]. 124 func.func @vector_magnify(%arga: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> { 125 %c = arith.constant 0 : index 126 %d = tensor.dim %arga, %c : tensor<?xf64, #SparseVector> 127 %xv = tensor.empty(%d) : tensor<?xf64, #SparseVector> 128 %0 = linalg.generic #trait_vec 129 ins(%arga: tensor<?xf64, #SparseVector>) 130 outs(%xv: tensor<?xf64, #SparseVector>) { 131 ^bb(%a: f64, %x: f64): 132 %idx = linalg.index 0 : index 133 %1 = sparse_tensor.unary %a : f64 to f64 134 present={ 135 ^bb0(%x0: f64): 136 %tmp = arith.index_cast %idx : index to i64 137 %idxf = arith.uitofp %tmp : i64 to f64 138 %ret = arith.mulf %x0, %idxf : f64 139 sparse_tensor.yield %ret : f64 140 } 141 absent={} 142 linalg.yield %1 : f64 143 } -> tensor<?xf64, #SparseVector> 144 return %0 : tensor<?xf64, #SparseVector> 145 } 146 147 // Clips values to the range [3, 7]. 148 func.func @matrix_clip(%argx: tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> { 149 %c0 = arith.constant 0 : index 150 %c1 = arith.constant 1 : index 151 %cfmin = arith.constant 3.0 : f64 152 %cfmax = arith.constant 7.0 : f64 153 %d0 = tensor.dim %argx, %c0 : tensor<?x?xf64, #DCSR> 154 %d1 = tensor.dim %argx, %c1 : tensor<?x?xf64, #DCSR> 155 %xv = tensor.empty(%d0, %d1) : tensor<?x?xf64, #DCSR> 156 %0 = linalg.generic #trait_mat 157 ins(%argx: tensor<?x?xf64, #DCSR>) 158 outs(%xv: tensor<?x?xf64, #DCSR>) { 159 ^bb(%a: f64, %x: f64): 160 %1 = sparse_tensor.unary %a: f64 to f64 161 present={ 162 ^bb0(%x0: f64): 163 %mincmp = arith.cmpf "ogt", %x0, %cfmin : f64 164 %x1 = arith.select %mincmp, %x0, %cfmin : f64 165 %maxcmp = arith.cmpf "olt", %x1, %cfmax : f64 166 %x2 = arith.select %maxcmp, %x1, %cfmax : f64 167 sparse_tensor.yield %x2 : f64 168 } 169 absent={} 170 linalg.yield %1 : f64 171 } -> tensor<?x?xf64, #DCSR> 172 return %0 : tensor<?x?xf64, #DCSR> 173 } 174 175 // Slices matrix and only keep the value of the lower-right corner of the original 176 // matrix (i.e., A[2/d0 ..][2/d1 ..]), and set other values to 99. 177 func.func @matrix_slice(%argx: tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> { 178 %c0 = arith.constant 0 : index 179 %c1 = arith.constant 1 : index 180 %d0 = tensor.dim %argx, %c0 : tensor<?x?xf64, #DCSR> 181 %d1 = tensor.dim %argx, %c1 : tensor<?x?xf64, #DCSR> 182 %xv = tensor.empty(%d0, %d1) : tensor<?x?xf64, #DCSR> 183 %0 = linalg.generic #trait_mat 184 ins(%argx: tensor<?x?xf64, #DCSR>) 185 outs(%xv: tensor<?x?xf64, #DCSR>) { 186 ^bb(%a: f64, %x: f64): 187 %row = linalg.index 0 : index 188 %col = linalg.index 1 : index 189 %1 = sparse_tensor.unary %a: f64 to f64 190 present={ 191 ^bb0(%x0: f64): 192 %v = arith.constant 99.0 : f64 193 %two = arith.constant 2 : index 194 %r = arith.muli %two, %row : index 195 %c = arith.muli %two, %col : index 196 %cmp1 = arith.cmpi "ult", %r, %d0 : index 197 %tmp = arith.select %cmp1, %v, %x0 : f64 198 %cmp2 = arith.cmpi "ult", %c, %d1 : index 199 %result = arith.select %cmp2, %v, %tmp : f64 200 sparse_tensor.yield %result : f64 201 } 202 absent={} 203 linalg.yield %1 : f64 204 } -> tensor<?x?xf64, #DCSR> 205 return %0 : tensor<?x?xf64, #DCSR> 206 } 207 208 // Driver method to call and verify vector kernels. 209 func.func @main() { 210 %cmu = arith.constant -99 : i32 211 %c0 = arith.constant 0 : index 212 213 // Setup sparse vectors. 214 %v1 = arith.constant sparse< 215 [ [0], [3], [11], [17], [20], [21], [28], [29], [31] ], 216 [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] 217 > : tensor<32xf64> 218 %sv1 = sparse_tensor.convert %v1 : tensor<32xf64> to tensor<?xf64, #SparseVector> 219 220 // Setup sparse matrices. 221 %m1 = arith.constant sparse< 222 [ [0,0], [0,1], [1,7], [2,2], [2,4], [2,7], [3,0], [3,2], [3,3] ], 223 [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ] 224 > : tensor<4x8xf64> 225 %sm1 = sparse_tensor.convert %m1 : tensor<4x8xf64> to tensor<?x?xf64, #DCSR> 226 227 // Call sparse vector kernels. 228 %0 = call @vector_complement_sparse(%sv1) 229 : (tensor<?xf64, #SparseVector>) -> tensor<?xi32, #SparseVector> 230 %1 = call @vector_negation(%sv1) 231 : (tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> 232 %2 = call @vector_magnify(%sv1) 233 : (tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> 234 235 // Call sparse matrix kernels. 236 %3 = call @matrix_clip(%sm1) 237 : (tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> 238 %4 = call @matrix_slice(%sm1) 239 : (tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> 240 241 // Call kernel with dense output. 242 %5 = call @vector_complement_dense(%sv1) : (tensor<?xf64, #SparseVector>) -> tensor<?xi32> 243 244 // 245 // Verify the results. 246 // 247 // CHECK: ---- Sparse Tensor ---- 248 // CHECK-NEXT: nse = 9 249 // CHECK-NEXT: dim = ( 32 ) 250 // CHECK-NEXT: lvl = ( 32 ) 251 // CHECK-NEXT: pos[0] : ( 0, 9 ) 252 // CHECK-NEXT: crd[0] : ( 0, 3, 11, 17, 20, 21, 28, 29, 31 ) 253 // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) 254 // CHECK-NEXT: ---- 255 // CHECK: ---- Sparse Tensor ---- 256 // CHECK-NEXT: nse = 23 257 // CHECK-NEXT: dim = ( 32 ) 258 // CHECK-NEXT: lvl = ( 32 ) 259 // CHECK-NEXT: pos[0] : ( 0, 23 ) 260 // CHECK-NEXT: crd[0] : ( 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 18, 19, 22, 23, 24, 25, 26, 27, 30 ) 261 // CHECK-NEXT: values : ( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ) 262 // CHECK-NEXT: ---- 263 // CHECK: ---- Sparse Tensor ---- 264 // CHECK-NEXT: nse = 32 265 // CHECK-NEXT: dim = ( 32 ) 266 // CHECK-NEXT: lvl = ( 32 ) 267 // CHECK-NEXT: pos[0] : ( 0, 32 ) 268 // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 ) 269 // CHECK-NEXT: values : ( -1, 1, 1, -2, 1, 1, 1, 1, 1, 1, 1, -3, 1, 1, 1, 1, 1, -4, 1, 1, -5, -6, 1, 1, 1, 1, 1, 1, -7, -8, 1, -9 ) 270 // CHECK-NEXT: ---- 271 // CHECK: ---- Sparse Tensor ---- 272 // CHECK-NEXT: nse = 9 273 // CHECK-NEXT: dim = ( 32 ) 274 // CHECK-NEXT: lvl = ( 32 ) 275 // CHECK-NEXT: pos[0] : ( 0, 9 ) 276 // CHECK-NEXT: crd[0] : ( 0, 3, 11, 17, 20, 21, 28, 29, 31 ) 277 // CHECK-NEXT: values : ( 0, 6, 33, 68, 100, 126, 196, 232, 279 ) 278 // CHECK-NEXT: ---- 279 // CHECK: ---- Sparse Tensor ---- 280 // CHECK-NEXT: nse = 9 281 // CHECK-NEXT: dim = ( 4, 8 ) 282 // CHECK-NEXT: lvl = ( 4, 8 ) 283 // CHECK-NEXT: pos[0] : ( 0, 4 ) 284 // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 ) 285 // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 ) 286 // CHECK-NEXT: crd[1] : ( 0, 1, 7, 2, 4, 7, 0, 2, 3 ) 287 // CHECK-NEXT: values : ( 3, 3, 3, 4, 5, 6, 7, 7, 7 ) 288 // CHECK-NEXT: ---- 289 // CHECK: ---- Sparse Tensor ---- 290 // CHECK-NEXT: nse = 9 291 // CHECK-NEXT: dim = ( 4, 8 ) 292 // CHECK-NEXT: lvl = ( 4, 8 ) 293 // CHECK-NEXT: pos[0] : ( 0, 4 ) 294 // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 ) 295 // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 ) 296 // CHECK-NEXT: crd[1] : ( 0, 1, 7, 2, 4, 7, 0, 2, 3 ) 297 // CHECK-NEXT: values : ( 99, 99, 99, 99, 5, 6, 99, 99, 99 ) 298 // CHECK-NEXT: ---- 299 // CHECK-NEXT: ( 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0 ) 300 // 301 sparse_tensor.print %sv1 : tensor<?xf64, #SparseVector> 302 sparse_tensor.print %0 : tensor<?xi32, #SparseVector> 303 sparse_tensor.print %1 : tensor<?xf64, #SparseVector> 304 sparse_tensor.print %2 : tensor<?xf64, #SparseVector> 305 sparse_tensor.print %3 : tensor<?x?xf64, #DCSR> 306 sparse_tensor.print %4 : tensor<?x?xf64, #DCSR> 307 %v = vector.transfer_read %5[%c0], %cmu: tensor<?xi32>, vector<32xi32> 308 vector.print %v : vector<32xi32> 309 310 // Release the resources. 311 bufferization.dealloc_tensor %sv1 : tensor<?xf64, #SparseVector> 312 bufferization.dealloc_tensor %sm1 : tensor<?x?xf64, #DCSR> 313 bufferization.dealloc_tensor %0 : tensor<?xi32, #SparseVector> 314 bufferization.dealloc_tensor %1 : tensor<?xf64, #SparseVector> 315 bufferization.dealloc_tensor %2 : tensor<?xf64, #SparseVector> 316 bufferization.dealloc_tensor %3 : tensor<?x?xf64, #DCSR> 317 bufferization.dealloc_tensor %4 : tensor<?x?xf64, #DCSR> 318 bufferization.dealloc_tensor %5 : tensor<?xi32> 319 return 320 } 321} 322