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 direct IR generation and 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 direct IR generation and VLA vectorization. 32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %} 33 34#SparseVector = #sparse_tensor.encoding<{ 35 map = (d0) -> (d0 : compressed) 36}> 37 38#SparseMatrix = #sparse_tensor.encoding<{ 39 map = (d0, d1) -> (d0 : compressed, d1 : compressed) 40}> 41 42#trait_1d = { 43 indexing_maps = [ 44 affine_map<(i) -> (i)>, // a 45 affine_map<(i) -> (i)> // x (out) 46 ], 47 iterator_types = ["parallel"], 48 doc = "X(i) = a(i) op i" 49} 50 51#trait_2d = { 52 indexing_maps = [ 53 affine_map<(i,j) -> (i,j)>, // A 54 affine_map<(i,j) -> (i,j)> // X (out) 55 ], 56 iterator_types = ["parallel", "parallel"], 57 doc = "X(i,j) = A(i,j) op i op j" 58} 59 60// 61// Test with indices and sparse inputs. All outputs are dense. 62// 63module { 64 65 // 66 // Kernel that uses index in the index notation (conjunction). 67 // 68 func.func @sparse_index_1d_conj(%arga: tensor<8xi64, #SparseVector>, 69 %out: tensor<8xi64>) -> tensor<8xi64> { 70 %r = linalg.generic #trait_1d 71 ins(%arga: tensor<8xi64, #SparseVector>) 72 outs(%out: tensor<8xi64>) { 73 ^bb(%a: i64, %x: i64): 74 %i = linalg.index 0 : index 75 %ii = arith.index_cast %i : index to i64 76 %m1 = arith.muli %a, %ii : i64 77 linalg.yield %m1 : i64 78 } -> tensor<8xi64> 79 return %r : tensor<8xi64> 80 } 81 82 // 83 // Kernel that uses index in the index notation (disjunction). 84 // 85 func.func @sparse_index_1d_disj(%arga: tensor<8xi64, #SparseVector>, 86 %out: tensor<8xi64>) -> tensor<8xi64> { 87 %r = linalg.generic #trait_1d 88 ins(%arga: tensor<8xi64, #SparseVector>) 89 outs(%out: tensor<8xi64>) { 90 ^bb(%a: i64, %x: i64): 91 %i = linalg.index 0 : index 92 %ii = arith.index_cast %i : index to i64 93 %m1 = arith.addi %a, %ii : i64 94 linalg.yield %m1 : i64 95 } -> tensor<8xi64> 96 return %r : tensor<8xi64> 97 } 98 99 // 100 // Kernel that uses indices in the index notation (conjunction). 101 // 102 func.func @sparse_index_2d_conj(%arga: tensor<3x4xi64, #SparseMatrix>, 103 %out: tensor<3x4xi64>) -> tensor<3x4xi64> { 104 %r = linalg.generic #trait_2d 105 ins(%arga: tensor<3x4xi64, #SparseMatrix>) 106 outs(%out: tensor<3x4xi64>) { 107 ^bb(%a: i64, %x: i64): 108 %i = linalg.index 0 : index 109 %j = linalg.index 1 : index 110 %ii = arith.index_cast %i : index to i64 111 %jj = arith.index_cast %j : index to i64 112 %m1 = arith.muli %ii, %a : i64 113 %m2 = arith.muli %jj, %m1 : i64 114 linalg.yield %m2 : i64 115 } -> tensor<3x4xi64> 116 return %r : tensor<3x4xi64> 117 } 118 119 // 120 // Kernel that uses indices in the index notation (disjunction). 121 // 122 func.func @sparse_index_2d_disj(%arga: tensor<3x4xi64, #SparseMatrix>, 123 %out: tensor<3x4xi64>) -> tensor<3x4xi64> { 124 %r = linalg.generic #trait_2d 125 ins(%arga: tensor<3x4xi64, #SparseMatrix>) 126 outs(%out: tensor<3x4xi64>) { 127 ^bb(%a: i64, %x: i64): 128 %i = linalg.index 0 : index 129 %j = linalg.index 1 : index 130 %ii = arith.index_cast %i : index to i64 131 %jj = arith.index_cast %j : index to i64 132 %m1 = arith.addi %ii, %a : i64 133 %m2 = arith.addi %jj, %m1 : i64 134 linalg.yield %m2 : i64 135 } -> tensor<3x4xi64> 136 return %r : tensor<3x4xi64> 137 } 138 139 // 140 // Main driver. 141 // 142 func.func @main() { 143 %c0 = arith.constant 0 : index 144 %du = arith.constant -1 : i64 145 146 // Setup input sparse vector. 147 %v1 = arith.constant sparse<[[2], [4]], [ 10, 20]> : tensor<8xi64> 148 %sv = sparse_tensor.convert %v1 : tensor<8xi64> to tensor<8xi64, #SparseVector> 149 150 // Setup input "sparse" vector. 151 %v2 = arith.constant dense<[ 1, 2, 4, 8, 16, 32, 64, 128 ]> : tensor<8xi64> 152 %dv = sparse_tensor.convert %v2 : tensor<8xi64> to tensor<8xi64, #SparseVector> 153 154 // Setup input sparse matrix. 155 %m1 = arith.constant sparse<[[1,1], [2,3]], [10, 20]> : tensor<3x4xi64> 156 %sm = sparse_tensor.convert %m1 : tensor<3x4xi64> to tensor<3x4xi64, #SparseMatrix> 157 158 // Setup input "sparse" matrix. 159 %m2 = arith.constant dense <[ [ 1, 1, 1, 1 ], 160 [ 1, 2, 1, 1 ], 161 [ 1, 1, 3, 4 ] ]> : tensor<3x4xi64> 162 %dm = sparse_tensor.convert %m2 : tensor<3x4xi64> to tensor<3x4xi64, #SparseMatrix> 163 164 // Setup out tensors. 165 // Note: Constants bufferize to read-only buffers. 166 %init_8 = tensor.empty() : tensor<8xi64> 167 %init_3_4 = tensor.empty() : tensor<3x4xi64> 168 169 // Call the kernels. 170 %0 = call @sparse_index_1d_conj(%sv, %init_8) : (tensor<8xi64, #SparseVector>, tensor<8xi64>) -> tensor<8xi64> 171 %1 = call @sparse_index_1d_disj(%sv, %init_8) : (tensor<8xi64, #SparseVector>, tensor<8xi64>) -> tensor<8xi64> 172 %2 = call @sparse_index_1d_conj(%dv, %init_8) : (tensor<8xi64, #SparseVector>, tensor<8xi64>) -> tensor<8xi64> 173 %3 = call @sparse_index_1d_disj(%dv, %init_8) : (tensor<8xi64, #SparseVector>, tensor<8xi64>) -> tensor<8xi64> 174 %4 = call @sparse_index_2d_conj(%sm, %init_3_4) : (tensor<3x4xi64, #SparseMatrix>, tensor<3x4xi64>) -> tensor<3x4xi64> 175 %5 = call @sparse_index_2d_disj(%sm, %init_3_4) : (tensor<3x4xi64, #SparseMatrix>, tensor<3x4xi64>) -> tensor<3x4xi64> 176 %6 = call @sparse_index_2d_conj(%dm, %init_3_4) : (tensor<3x4xi64, #SparseMatrix>, tensor<3x4xi64>) -> tensor<3x4xi64> 177 %7 = call @sparse_index_2d_disj(%dm, %init_3_4) : (tensor<3x4xi64, #SparseMatrix>, tensor<3x4xi64>) -> tensor<3x4xi64> 178 179 // 180 // Verify result. 181 // 182 // CHECK: ( 0, 0, 20, 0, 80, 0, 0, 0 ) 183 // CHECK-NEXT: ( 0, 1, 12, 3, 24, 5, 6, 7 ) 184 // CHECK-NEXT: ( 0, 2, 8, 24, 64, 160, 384, 896 ) 185 // CHECK-NEXT: ( 1, 3, 6, 11, 20, 37, 70, 135 ) 186 // CHECK-NEXT: ( ( 0, 0, 0, 0 ), ( 0, 10, 0, 0 ), ( 0, 0, 0, 120 ) ) 187 // CHECK-NEXT: ( ( 0, 1, 2, 3 ), ( 1, 12, 3, 4 ), ( 2, 3, 4, 25 ) ) 188 // CHECK-NEXT: ( ( 0, 0, 0, 0 ), ( 0, 2, 2, 3 ), ( 0, 2, 12, 24 ) ) 189 // CHECK-NEXT: ( ( 1, 2, 3, 4 ), ( 2, 4, 4, 5 ), ( 3, 4, 7, 9 ) ) 190 // 191 %vv0 = vector.transfer_read %0[%c0], %du: tensor<8xi64>, vector<8xi64> 192 %vv1 = vector.transfer_read %1[%c0], %du: tensor<8xi64>, vector<8xi64> 193 %vv2 = vector.transfer_read %2[%c0], %du: tensor<8xi64>, vector<8xi64> 194 %vv3 = vector.transfer_read %3[%c0], %du: tensor<8xi64>, vector<8xi64> 195 %vv4 = vector.transfer_read %4[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64> 196 %vv5 = vector.transfer_read %5[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64> 197 %vv6 = vector.transfer_read %6[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64> 198 %vv7 = vector.transfer_read %7[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64> 199 vector.print %vv0 : vector<8xi64> 200 vector.print %vv1 : vector<8xi64> 201 vector.print %vv2 : vector<8xi64> 202 vector.print %vv3 : vector<8xi64> 203 vector.print %vv4 : vector<3x4xi64> 204 vector.print %vv5 : vector<3x4xi64> 205 vector.print %vv6 : vector<3x4xi64> 206 vector.print %vv7 : vector<3x4xi64> 207 208 // Release resources. 209 bufferization.dealloc_tensor %sv : tensor<8xi64, #SparseVector> 210 bufferization.dealloc_tensor %dv : tensor<8xi64, #SparseVector> 211 bufferization.dealloc_tensor %sm : tensor<3x4xi64, #SparseMatrix> 212 bufferization.dealloc_tensor %dm : tensor<3x4xi64, #SparseMatrix> 213 bufferization.dealloc_tensor %0 : tensor<8xi64> 214 bufferization.dealloc_tensor %1 : tensor<8xi64> 215 bufferization.dealloc_tensor %2 : tensor<8xi64> 216 bufferization.dealloc_tensor %3 : tensor<8xi64> 217 bufferization.dealloc_tensor %4 : tensor<3x4xi64> 218 bufferization.dealloc_tensor %5 : tensor<3x4xi64> 219 bufferization.dealloc_tensor %6 : tensor<3x4xi64> 220 bufferization.dealloc_tensor %7 : tensor<3x4xi64> 221 222 return 223 } 224} 225