1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: mlir-cpu-runner -e entry -entry-point-result=void \ 3// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 4// RUN: FileCheck %s 5 6#SparseVector = #sparse_tensor.encoding<{ 7 dimLevelType = ["compressed"] 8}> 9 10#SparseMatrix = #sparse_tensor.encoding<{ 11 dimLevelType = ["compressed", "compressed"] 12}> 13 14#trait_1d = { 15 indexing_maps = [ 16 affine_map<(i) -> (i)>, // a 17 affine_map<(i) -> (i)> // x (out) 18 ], 19 iterator_types = ["parallel"], 20 doc = "X(i) = a(i) op i" 21} 22 23#trait_2d = { 24 indexing_maps = [ 25 affine_map<(i,j) -> (i,j)>, // A 26 affine_map<(i,j) -> (i,j)> // X (out) 27 ], 28 iterator_types = ["parallel", "parallel"], 29 doc = "X(i,j) = A(i,j) op i op j" 30} 31 32// 33// Test with indices. Note that a lot of results are actually 34// dense, but this is done to stress test all the operations. 35// 36module { 37 38 // 39 // Kernel that uses index in the index notation (conjunction). 40 // 41 func.func @sparse_index_1d_conj(%arga: tensor<8xi64, #SparseVector>) 42 -> tensor<8xi64, #SparseVector> { 43 %init = bufferization.alloc_tensor() : tensor<8xi64, #SparseVector> 44 %r = linalg.generic #trait_1d 45 ins(%arga: tensor<8xi64, #SparseVector>) 46 outs(%init: tensor<8xi64, #SparseVector>) { 47 ^bb(%a: i64, %x: i64): 48 %i = linalg.index 0 : index 49 %ii = arith.index_cast %i : index to i64 50 %m1 = arith.muli %a, %ii : i64 51 linalg.yield %m1 : i64 52 } -> tensor<8xi64, #SparseVector> 53 return %r : tensor<8xi64, #SparseVector> 54 } 55 56 // 57 // Kernel that uses index in the index notation (disjunction). 58 // 59 func.func @sparse_index_1d_disj(%arga: tensor<8xi64, #SparseVector>) 60 -> tensor<8xi64, #SparseVector> { 61 %init = bufferization.alloc_tensor() : tensor<8xi64, #SparseVector> 62 %r = linalg.generic #trait_1d 63 ins(%arga: tensor<8xi64, #SparseVector>) 64 outs(%init: tensor<8xi64, #SparseVector>) { 65 ^bb(%a: i64, %x: i64): 66 %i = linalg.index 0 : index 67 %ii = arith.index_cast %i : index to i64 68 %m1 = arith.addi %a, %ii : i64 69 linalg.yield %m1 : i64 70 } -> tensor<8xi64, #SparseVector> 71 return %r : tensor<8xi64, #SparseVector> 72 } 73 74 // 75 // Kernel that uses indices in the index notation (conjunction). 76 // 77 func.func @sparse_index_2d_conj(%arga: tensor<3x4xi64, #SparseMatrix>) 78 -> tensor<3x4xi64, #SparseMatrix> { 79 %init = bufferization.alloc_tensor() : tensor<3x4xi64, #SparseMatrix> 80 %r = linalg.generic #trait_2d 81 ins(%arga: tensor<3x4xi64, #SparseMatrix>) 82 outs(%init: tensor<3x4xi64, #SparseMatrix>) { 83 ^bb(%a: i64, %x: i64): 84 %i = linalg.index 0 : index 85 %j = linalg.index 1 : index 86 %ii = arith.index_cast %i : index to i64 87 %jj = arith.index_cast %j : index to i64 88 %m1 = arith.muli %ii, %a : i64 89 %m2 = arith.muli %jj, %m1 : i64 90 linalg.yield %m2 : i64 91 } -> tensor<3x4xi64, #SparseMatrix> 92 return %r : tensor<3x4xi64, #SparseMatrix> 93 } 94 95 // 96 // Kernel that uses indices in the index notation (disjunction). 97 // 98 func.func @sparse_index_2d_disj(%arga: tensor<3x4xi64, #SparseMatrix>) 99 -> tensor<3x4xi64, #SparseMatrix> { 100 %init = bufferization.alloc_tensor() : tensor<3x4xi64, #SparseMatrix> 101 %r = linalg.generic #trait_2d 102 ins(%arga: tensor<3x4xi64, #SparseMatrix>) 103 outs(%init: tensor<3x4xi64, #SparseMatrix>) { 104 ^bb(%a: i64, %x: i64): 105 %i = linalg.index 0 : index 106 %j = linalg.index 1 : index 107 %ii = arith.index_cast %i : index to i64 108 %jj = arith.index_cast %j : index to i64 109 %m1 = arith.addi %ii, %a : i64 110 %m2 = arith.addi %jj, %m1 : i64 111 linalg.yield %m2 : i64 112 } -> tensor<3x4xi64, #SparseMatrix> 113 return %r : tensor<3x4xi64, #SparseMatrix> 114 } 115 116 func.func @add_outer_2d(%arg0: tensor<2x3xf32, #SparseMatrix>) 117 -> tensor<2x3xf32, #SparseMatrix> { 118 %0 = bufferization.alloc_tensor() : tensor<2x3xf32, #SparseMatrix> 119 %1 = linalg.generic #trait_2d 120 ins(%arg0 : tensor<2x3xf32, #SparseMatrix>) 121 outs(%0 : tensor<2x3xf32, #SparseMatrix>) { 122 ^bb0(%arg1: f32, %arg2: f32): 123 %2 = linalg.index 0 : index 124 %3 = arith.index_cast %2 : index to i64 125 %4 = arith.uitofp %3 : i64 to f32 126 %5 = arith.addf %arg1, %4 : f32 127 linalg.yield %5 : f32 128 } -> tensor<2x3xf32, #SparseMatrix> 129 return %1 : tensor<2x3xf32, #SparseMatrix> 130 } 131 132 // 133 // Main driver. 134 // 135 func.func @entry() { 136 %c0 = arith.constant 0 : index 137 %du = arith.constant -1 : i64 138 %df = arith.constant -1.0 : f32 139 140 // Setup input sparse vector. 141 %v1 = arith.constant sparse<[[2], [4]], [ 10, 20]> : tensor<8xi64> 142 %sv = sparse_tensor.convert %v1 : tensor<8xi64> to tensor<8xi64, #SparseVector> 143 144 // Setup input "sparse" vector. 145 %v2 = arith.constant dense<[ 1, 2, 4, 8, 16, 32, 64, 128 ]> : tensor<8xi64> 146 %dv = sparse_tensor.convert %v2 : tensor<8xi64> to tensor<8xi64, #SparseVector> 147 148 // Setup input sparse matrix. 149 %m1 = arith.constant sparse<[[1,1], [2,3]], [10, 20]> : tensor<3x4xi64> 150 %sm = sparse_tensor.convert %m1 : tensor<3x4xi64> to tensor<3x4xi64, #SparseMatrix> 151 152 // Setup input "sparse" matrix. 153 %m2 = arith.constant dense <[ [ 1, 1, 1, 1 ], 154 [ 1, 2, 1, 1 ], 155 [ 1, 1, 3, 4 ] ]> : tensor<3x4xi64> 156 %dm = sparse_tensor.convert %m2 : tensor<3x4xi64> to tensor<3x4xi64, #SparseMatrix> 157 158 // Setup input sparse f32 matrix. 159 %mf32 = arith.constant sparse<[[0,1], [1,2]], [10.0, 41.0]> : tensor<2x3xf32> 160 %sf32 = sparse_tensor.convert %mf32 : tensor<2x3xf32> to tensor<2x3xf32, #SparseMatrix> 161 162 // Call the kernels. 163 %0 = call @sparse_index_1d_conj(%sv) : (tensor<8xi64, #SparseVector>) 164 -> tensor<8xi64, #SparseVector> 165 %1 = call @sparse_index_1d_disj(%sv) : (tensor<8xi64, #SparseVector>) 166 -> tensor<8xi64, #SparseVector> 167 %2 = call @sparse_index_1d_conj(%dv) : (tensor<8xi64, #SparseVector>) 168 -> tensor<8xi64, #SparseVector> 169 %3 = call @sparse_index_1d_disj(%dv) : (tensor<8xi64, #SparseVector>) 170 -> tensor<8xi64, #SparseVector> 171 %4 = call @sparse_index_2d_conj(%sm) : (tensor<3x4xi64, #SparseMatrix>) 172 -> tensor<3x4xi64, #SparseMatrix> 173 %5 = call @sparse_index_2d_disj(%sm) : (tensor<3x4xi64, #SparseMatrix>) 174 -> tensor<3x4xi64, #SparseMatrix> 175 %6 = call @sparse_index_2d_conj(%dm) : (tensor<3x4xi64, #SparseMatrix>) 176 -> tensor<3x4xi64, #SparseMatrix> 177 %7 = call @sparse_index_2d_disj(%dm) : (tensor<3x4xi64, #SparseMatrix>) 178 -> tensor<3x4xi64, #SparseMatrix> 179 180 // 181 // Verify result. 182 // 183 // CHECK: ( 20, 80, -1, -1, -1, -1, -1, -1 ) 184 // CHECK-NEXT: ( 0, 1, 12, 3, 24, 5, 6, 7 ) 185 // CHECK-NEXT: ( 0, 2, 8, 24, 64, 160, 384, 896 ) 186 // CHECK-NEXT: ( 1, 3, 6, 11, 20, 37, 70, 135 ) 187 // CHECK-NEXT: ( 10, 120, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ) 188 // CHECK-NEXT: ( 0, 1, 2, 3, 1, 12, 3, 4, 2, 3, 4, 25 ) 189 // CHECK-NEXT: ( 0, 0, 0, 0, 0, 2, 2, 3, 0, 2, 12, 24 ) 190 // CHECK-NEXT: ( 1, 2, 3, 4, 2, 4, 4, 5, 3, 4, 7, 9 ) 191 // 192 %8 = sparse_tensor.values %0 : tensor<8xi64, #SparseVector> to memref<?xi64> 193 %9 = sparse_tensor.values %1 : tensor<8xi64, #SparseVector> to memref<?xi64> 194 %10 = sparse_tensor.values %2 : tensor<8xi64, #SparseVector> to memref<?xi64> 195 %11 = sparse_tensor.values %3 : tensor<8xi64, #SparseVector> to memref<?xi64> 196 %12 = sparse_tensor.values %4 : tensor<3x4xi64, #SparseMatrix> to memref<?xi64> 197 %13 = sparse_tensor.values %5 : tensor<3x4xi64, #SparseMatrix> to memref<?xi64> 198 %14 = sparse_tensor.values %6 : tensor<3x4xi64, #SparseMatrix> to memref<?xi64> 199 %15 = sparse_tensor.values %7 : tensor<3x4xi64, #SparseMatrix> to memref<?xi64> 200 %16 = vector.transfer_read %8[%c0], %du: memref<?xi64>, vector<8xi64> 201 %17 = vector.transfer_read %9[%c0], %du: memref<?xi64>, vector<8xi64> 202 %18 = vector.transfer_read %10[%c0], %du: memref<?xi64>, vector<8xi64> 203 %19 = vector.transfer_read %11[%c0], %du: memref<?xi64>, vector<8xi64> 204 %20 = vector.transfer_read %12[%c0], %du: memref<?xi64>, vector<12xi64> 205 %21 = vector.transfer_read %13[%c0], %du: memref<?xi64>, vector<12xi64> 206 %22 = vector.transfer_read %14[%c0], %du: memref<?xi64>, vector<12xi64> 207 %23 = vector.transfer_read %15[%c0], %du: memref<?xi64>, vector<12xi64> 208 vector.print %16 : vector<8xi64> 209 vector.print %17 : vector<8xi64> 210 vector.print %18 : vector<8xi64> 211 vector.print %19 : vector<8xi64> 212 vector.print %20 : vector<12xi64> 213 vector.print %21 : vector<12xi64> 214 vector.print %22 : vector<12xi64> 215 vector.print %23 : vector<12xi64> 216 217 // Release resources. 218 bufferization.dealloc_tensor %sv : tensor<8xi64, #SparseVector> 219 bufferization.dealloc_tensor %dv : tensor<8xi64, #SparseVector> 220 bufferization.dealloc_tensor %0 : tensor<8xi64, #SparseVector> 221 bufferization.dealloc_tensor %1 : tensor<8xi64, #SparseVector> 222 bufferization.dealloc_tensor %2 : tensor<8xi64, #SparseVector> 223 bufferization.dealloc_tensor %3 : tensor<8xi64, #SparseVector> 224 bufferization.dealloc_tensor %sm : tensor<3x4xi64, #SparseMatrix> 225 bufferization.dealloc_tensor %dm : tensor<3x4xi64, #SparseMatrix> 226 bufferization.dealloc_tensor %4 : tensor<3x4xi64, #SparseMatrix> 227 bufferization.dealloc_tensor %5 : tensor<3x4xi64, #SparseMatrix> 228 bufferization.dealloc_tensor %6 : tensor<3x4xi64, #SparseMatrix> 229 bufferization.dealloc_tensor %7 : tensor<3x4xi64, #SparseMatrix> 230 231 // 232 // Call the f32 kernel, verify the result, release the resources. 233 // 234 // CHECK-NEXT: ( 0, 10, 0, 1, 1, 42 ) 235 // 236 %100 = call @add_outer_2d(%sf32) : (tensor<2x3xf32, #SparseMatrix>) 237 -> tensor<2x3xf32, #SparseMatrix> 238 %101 = sparse_tensor.values %100 : tensor<2x3xf32, #SparseMatrix> to memref<?xf32> 239 %102 = vector.transfer_read %101[%c0], %df: memref<?xf32>, vector<6xf32> 240 vector.print %102 : vector<6xf32> 241 bufferization.dealloc_tensor %sf32 : tensor<2x3xf32, #SparseMatrix> 242 bufferization.dealloc_tensor %100 : tensor<2x3xf32, #SparseMatrix> 243 244 return 245 } 246} 247