224 lines · plain
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, this5// config could be moved to lit.local.cfg. However, there are downstream users that6// do not use these LIT config files. Hence why this is kept inline.7//8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true9// 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_utils13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils14// DEFINE: %{run_opts} = -e main -entry-point-result=void15// 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 %s22//23// Do the same run, but now with direct IR generation.24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false25// RUN: %{compile} | %{run} | FileCheck %s26//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=true29// RUN: %{compile} | %{run} | FileCheck %s30//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)>, // a45 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)>, // A54 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 -> tensor<8xi64> {70 %out = tensor.empty() : tensor<8xi64>71 %r = linalg.generic #trait_1d72 ins(%arga: tensor<8xi64, #SparseVector>)73 outs(%out: tensor<8xi64>) {74 ^bb(%a: i64, %x: i64):75 %i = linalg.index 0 : index76 %ii = arith.index_cast %i : index to i6477 %m1 = arith.muli %a, %ii : i6478 linalg.yield %m1 : i6479 } -> tensor<8xi64>80 return %r : tensor<8xi64>81 }82 83 //84 // Kernel that uses index in the index notation (disjunction).85 //86 func.func @sparse_index_1d_disj(%arga: tensor<8xi64, #SparseVector>)87 -> tensor<8xi64> {88 %out = tensor.empty() : tensor<8xi64>89 %r = linalg.generic #trait_1d90 ins(%arga: tensor<8xi64, #SparseVector>)91 outs(%out: tensor<8xi64>) {92 ^bb(%a: i64, %x: i64):93 %i = linalg.index 0 : index94 %ii = arith.index_cast %i : index to i6495 %m1 = arith.addi %a, %ii : i6496 linalg.yield %m1 : i6497 } -> tensor<8xi64>98 return %r : tensor<8xi64>99 }100 101 //102 // Kernel that uses indices in the index notation (conjunction).103 //104 func.func @sparse_index_2d_conj(%arga: tensor<3x4xi64, #SparseMatrix>)105 -> tensor<3x4xi64> {106 %out = tensor.empty() : tensor<3x4xi64>107 %r = linalg.generic #trait_2d108 ins(%arga: tensor<3x4xi64, #SparseMatrix>)109 outs(%out: tensor<3x4xi64>) {110 ^bb(%a: i64, %x: i64):111 %i = linalg.index 0 : index112 %j = linalg.index 1 : index113 %ii = arith.index_cast %i : index to i64114 %jj = arith.index_cast %j : index to i64115 %m1 = arith.muli %ii, %a : i64116 %m2 = arith.muli %jj, %m1 : i64117 linalg.yield %m2 : i64118 } -> tensor<3x4xi64>119 return %r : tensor<3x4xi64>120 }121 122 //123 // Kernel that uses indices in the index notation (disjunction).124 //125 func.func @sparse_index_2d_disj(%arga: tensor<3x4xi64, #SparseMatrix>)126 -> tensor<3x4xi64> {127 %out = tensor.empty() : tensor<3x4xi64>128 %r = linalg.generic #trait_2d129 ins(%arga: tensor<3x4xi64, #SparseMatrix>)130 outs(%out: tensor<3x4xi64>) {131 ^bb(%a: i64, %x: i64):132 %i = linalg.index 0 : index133 %j = linalg.index 1 : index134 %ii = arith.index_cast %i : index to i64135 %jj = arith.index_cast %j : index to i64136 %m1 = arith.addi %ii, %a : i64137 %m2 = arith.addi %jj, %m1 : i64138 linalg.yield %m2 : i64139 } -> tensor<3x4xi64>140 return %r : tensor<3x4xi64>141 }142 143 //144 // Main driver.145 //146 func.func @main() {147 %c0 = arith.constant 0 : index148 %du = arith.constant -1 : i64149 150 // Setup input sparse vector.151 %v1 = arith.constant sparse<[[2], [4]], [ 10, 20]> : tensor<8xi64>152 %sv = sparse_tensor.convert %v1 : tensor<8xi64> to tensor<8xi64, #SparseVector>153 154 // Setup input "sparse" vector.155 %v2 = arith.constant dense<[ 1, 2, 4, 8, 16, 32, 64, 128 ]> : tensor<8xi64>156 %dv = sparse_tensor.convert %v2 : tensor<8xi64> to tensor<8xi64, #SparseVector>157 158 // Setup input sparse matrix.159 %m1 = arith.constant sparse<[[1,1], [2,3]], [10, 20]> : tensor<3x4xi64>160 %sm = sparse_tensor.convert %m1 : tensor<3x4xi64> to tensor<3x4xi64, #SparseMatrix>161 162 // Setup input "sparse" matrix.163 %m2 = arith.constant dense <[ [ 1, 1, 1, 1 ],164 [ 1, 2, 1, 1 ],165 [ 1, 1, 3, 4 ] ]> : tensor<3x4xi64>166 %dm = sparse_tensor.convert %m2 : tensor<3x4xi64> to tensor<3x4xi64, #SparseMatrix>167 168 // Call the kernels.169 %0 = call @sparse_index_1d_conj(%sv) : (tensor<8xi64, #SparseVector>) -> tensor<8xi64>170 %1 = call @sparse_index_1d_disj(%sv) : (tensor<8xi64, #SparseVector>) -> tensor<8xi64>171 %2 = call @sparse_index_1d_conj(%dv) : (tensor<8xi64, #SparseVector>) -> tensor<8xi64>172 %3 = call @sparse_index_1d_disj(%dv) : (tensor<8xi64, #SparseVector>) -> tensor<8xi64>173 %4 = call @sparse_index_2d_conj(%sm) : (tensor<3x4xi64, #SparseMatrix>) -> tensor<3x4xi64>174 %5 = call @sparse_index_2d_disj(%sm) : (tensor<3x4xi64, #SparseMatrix>) -> tensor<3x4xi64>175 %6 = call @sparse_index_2d_conj(%dm) : (tensor<3x4xi64, #SparseMatrix>) -> tensor<3x4xi64>176 %7 = call @sparse_index_2d_disj(%dm) : (tensor<3x4xi64, #SparseMatrix>) -> tensor<3x4xi64>177 178 //179 // Verify result.180 //181 // CHECK: ( 0, 0, 20, 0, 80, 0, 0, 0 )182 // CHECK-NEXT: ( 0, 1, 12, 3, 24, 5, 6, 7 )183 // CHECK-NEXT: ( 0, 2, 8, 24, 64, 160, 384, 896 )184 // CHECK-NEXT: ( 1, 3, 6, 11, 20, 37, 70, 135 )185 // CHECK-NEXT: ( ( 0, 0, 0, 0 ), ( 0, 10, 0, 0 ), ( 0, 0, 0, 120 ) )186 // CHECK-NEXT: ( ( 0, 1, 2, 3 ), ( 1, 12, 3, 4 ), ( 2, 3, 4, 25 ) )187 // CHECK-NEXT: ( ( 0, 0, 0, 0 ), ( 0, 2, 2, 3 ), ( 0, 2, 12, 24 ) )188 // CHECK-NEXT: ( ( 1, 2, 3, 4 ), ( 2, 4, 4, 5 ), ( 3, 4, 7, 9 ) )189 //190 %vv0 = vector.transfer_read %0[%c0], %du: tensor<8xi64>, vector<8xi64>191 %vv1 = vector.transfer_read %1[%c0], %du: tensor<8xi64>, vector<8xi64>192 %vv2 = vector.transfer_read %2[%c0], %du: tensor<8xi64>, vector<8xi64>193 %vv3 = vector.transfer_read %3[%c0], %du: tensor<8xi64>, vector<8xi64>194 %vv4 = vector.transfer_read %4[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64>195 %vv5 = vector.transfer_read %5[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64>196 %vv6 = vector.transfer_read %6[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64>197 %vv7 = vector.transfer_read %7[%c0,%c0], %du: tensor<3x4xi64>, vector<3x4xi64>198 vector.print %vv0 : vector<8xi64>199 vector.print %vv1 : vector<8xi64>200 vector.print %vv2 : vector<8xi64>201 vector.print %vv3 : vector<8xi64>202 vector.print %vv4 : vector<3x4xi64>203 vector.print %vv5 : vector<3x4xi64>204 vector.print %vv6 : vector<3x4xi64>205 vector.print %vv7 : vector<3x4xi64>206 207 // Release resources.208 bufferization.dealloc_tensor %sv : tensor<8xi64, #SparseVector>209 bufferization.dealloc_tensor %dv : tensor<8xi64, #SparseVector>210 bufferization.dealloc_tensor %sm : tensor<3x4xi64, #SparseMatrix>211 bufferization.dealloc_tensor %dm : tensor<3x4xi64, #SparseMatrix>212 bufferization.dealloc_tensor %0 : tensor<8xi64>213 bufferization.dealloc_tensor %1 : tensor<8xi64>214 bufferization.dealloc_tensor %2 : tensor<8xi64>215 bufferization.dealloc_tensor %3 : tensor<8xi64>216 bufferization.dealloc_tensor %4 : tensor<3x4xi64>217 bufferization.dealloc_tensor %5 : tensor<3x4xi64>218 bufferization.dealloc_tensor %6 : tensor<3x4xi64>219 bufferization.dealloc_tensor %7 : tensor<3x4xi64>220 221 return222 }223}224