1// RUN: mlir-opt %s -split-input-file | mlir-opt | FileCheck %s 2 3#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}> 4 5// CHECK-LABEL: func @sparse_new( 6// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 7// CHECK: %[[T:.*]] = sparse_tensor.new %[[A]] : !llvm.ptr<i8> to tensor<128xf64, #{{.*}}> 8// CHECK: return %[[T]] : tensor<128xf64, #{{.*}}> 9func.func @sparse_new(%arg0: !llvm.ptr<i8>) -> tensor<128xf64, #SparseVector> { 10 %0 = sparse_tensor.new %arg0 : !llvm.ptr<i8> to tensor<128xf64, #SparseVector> 11 return %0 : tensor<128xf64, #SparseVector> 12} 13 14// ----- 15 16#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}> 17 18// CHECK-LABEL: func @sparse_init() 19// CHECK-DAG: %[[C16:.*]] = arith.constant 16 : index 20// CHECK-DAG: %[[C32:.*]] = arith.constant 32 : index 21// CHECK: %[[T:.*]] = sparse_tensor.init[%[[C16]], %[[C32]]] : tensor<?x32xf64, #{{.*}}> 22// CHECK: return %[[T]] : tensor<?x32xf64, #{{.*}}> 23func.func @sparse_init() -> tensor<?x32xf64, #SparseMatrix> { 24 %d1 = arith.constant 16 : index 25 %d2 = arith.constant 32 : index 26 %0 = sparse_tensor.init [%d1, %d2] : tensor<?x32xf64, #SparseMatrix> 27 return %0 : tensor<?x32xf64, #SparseMatrix> 28} 29 30// ----- 31 32#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}> 33 34// CHECK-LABEL: func @sparse_release( 35// CHECK-SAME: %[[A:.*]]: tensor<128xf64, #{{.*}}> 36// CHECK: sparse_tensor.release %[[A]] : tensor<128xf64, #{{.*}}> 37// CHECK: return 38func.func @sparse_release(%arg0: tensor<128xf64, #SparseVector>) { 39 sparse_tensor.release %arg0 : tensor<128xf64, #SparseVector> 40 return 41} 42 43// ----- 44 45#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}> 46 47// CHECK-LABEL: func @sparse_convert_1d_to_sparse( 48// CHECK-SAME: %[[A:.*]]: tensor<64xf32>) 49// CHECK: %[[T:.*]] = sparse_tensor.convert %[[A]] : tensor<64xf32> to tensor<64xf32, #{{.*}}> 50// CHECK: return %[[T]] : tensor<64xf32, #{{.*}}> 51func.func @sparse_convert_1d_to_sparse(%arg0: tensor<64xf32>) -> tensor<64xf32, #SparseVector> { 52 %0 = sparse_tensor.convert %arg0 : tensor<64xf32> to tensor<64xf32, #SparseVector> 53 return %0 : tensor<64xf32, #SparseVector> 54} 55 56// ----- 57 58#SparseTensor = #sparse_tensor.encoding<{ dimLevelType = [ "dense", "dense", "compressed" ] }> 59 60// CHECK-LABEL: func @sparse_convert_3d_from_sparse( 61// CHECK-SAME: %[[A:.*]]: tensor<8x8x8xf64, #{{.*}}>) 62// CHECK: %[[T:.*]] = sparse_tensor.convert %[[A]] : tensor<8x8x8xf64, #{{.*}}> to tensor<8x8x8xf64> 63// CHECK: return %[[T]] : tensor<8x8x8xf64> 64func.func @sparse_convert_3d_from_sparse(%arg0: tensor<8x8x8xf64, #SparseTensor>) -> tensor<8x8x8xf64> { 65 %0 = sparse_tensor.convert %arg0 : tensor<8x8x8xf64, #SparseTensor> to tensor<8x8x8xf64> 66 return %0 : tensor<8x8x8xf64> 67} 68 69// ----- 70 71#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}> 72 73// CHECK-LABEL: func @sparse_pointers( 74// CHECK-SAME: %[[A:.*]]: tensor<128xf64, #{{.*}}>) 75// CHECK: %[[C:.*]] = arith.constant 0 : index 76// CHECK: %[[T:.*]] = sparse_tensor.pointers %[[A]], %[[C]] : tensor<128xf64, #{{.*}}> to memref<?xindex> 77// CHECK: return %[[T]] : memref<?xindex> 78func.func @sparse_pointers(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> { 79 %c = arith.constant 0 : index 80 %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf64, #SparseVector> to memref<?xindex> 81 return %0 : memref<?xindex> 82} 83 84// ----- 85 86#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}> 87 88// CHECK-LABEL: func @sparse_indices( 89// CHECK-SAME: %[[A:.*]]: tensor<128xf64, #{{.*}}>) 90// CHECK: %[[C:.*]] = arith.constant 0 : index 91// CHECK: %[[T:.*]] = sparse_tensor.indices %[[A]], %[[C]] : tensor<128xf64, #{{.*}}> to memref<?xindex> 92// CHECK: return %[[T]] : memref<?xindex> 93func.func @sparse_indices(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> { 94 %c = arith.constant 0 : index 95 %0 = sparse_tensor.indices %arg0, %c : tensor<128xf64, #SparseVector> to memref<?xindex> 96 return %0 : memref<?xindex> 97} 98 99// ----- 100 101#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}> 102 103// CHECK-LABEL: func @sparse_values( 104// CHECK-SAME: %[[A:.*]]: tensor<128xf64, #{{.*}}>) 105// CHECK: %[[T:.*]] = sparse_tensor.values %[[A]] : tensor<128xf64, #{{.*}}> to memref<?xf64> 106// CHECK: return %[[T]] : memref<?xf64> 107func.func @sparse_values(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xf64> { 108 %0 = sparse_tensor.values %arg0 : tensor<128xf64, #SparseVector> to memref<?xf64> 109 return %0 : memref<?xf64> 110} 111 112// ----- 113 114#DenseMatrix = #sparse_tensor.encoding<{dimLevelType = ["dense","dense"]}> 115 116// CHECK-LABEL: func @sparse_load( 117// CHECK-SAME: %[[A:.*]]: tensor<16x32xf64, #{{.*}}>) 118// CHECK: %[[T:.*]] = sparse_tensor.load %[[A]] : tensor<16x32xf64, #{{.*}}> 119// CHECK: return %[[T]] : tensor<16x32xf64, #{{.*}}> 120func.func @sparse_load(%arg0: tensor<16x32xf64, #DenseMatrix>) -> tensor<16x32xf64, #DenseMatrix> { 121 %0 = sparse_tensor.load %arg0 : tensor<16x32xf64, #DenseMatrix> 122 return %0 : tensor<16x32xf64, #DenseMatrix> 123} 124 125// ----- 126 127#DenseMatrix = #sparse_tensor.encoding<{dimLevelType = ["dense","dense"]}> 128 129// CHECK-LABEL: func @sparse_load_ins( 130// CHECK-SAME: %[[A:.*]]: tensor<16x32xf64, #{{.*}}>) 131// CHECK: %[[T:.*]] = sparse_tensor.load %[[A]] hasInserts : tensor<16x32xf64, #{{.*}}> 132// CHECK: return %[[T]] : tensor<16x32xf64, #{{.*}}> 133func.func @sparse_load_ins(%arg0: tensor<16x32xf64, #DenseMatrix>) -> tensor<16x32xf64, #DenseMatrix> { 134 %0 = sparse_tensor.load %arg0 hasInserts : tensor<16x32xf64, #DenseMatrix> 135 return %0 : tensor<16x32xf64, #DenseMatrix> 136} 137 138// ----- 139 140#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}> 141 142// CHECK-LABEL: func @sparse_insert( 143// CHECK-SAME: %[[A:.*]]: tensor<128xf64, #sparse_tensor.encoding<{{.*}}>>, 144// CHECK-SAME: %[[B:.*]]: memref<?xindex>, 145// CHECK-SAME: %[[C:.*]]: f64) { 146// CHECK: sparse_tensor.lex_insert %[[A]], %[[B]], %[[C]] : tensor<128xf64, #{{.*}}>, memref<?xindex>, f64 147// CHECK: return 148func.func @sparse_insert(%arg0: tensor<128xf64, #SparseVector>, %arg1: memref<?xindex>, %arg2: f64) { 149 sparse_tensor.lex_insert %arg0, %arg1, %arg2 : tensor<128xf64, #SparseVector>, memref<?xindex>, f64 150 return 151} 152 153// ----- 154 155#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}> 156 157// CHECK-LABEL: func @sparse_expansion( 158// CHECK-SAME: %[[A:.*]]: tensor<8x8xf64, #sparse_tensor.encoding<{{.*}}>>) 159// CHECK: sparse_tensor.expand %[[A]] 160// CHECK: return 161func.func @sparse_expansion(%arg0: tensor<8x8xf64, #SparseMatrix>) { 162 %values, %filled, %added, %count = sparse_tensor.expand %arg0 163 : tensor<8x8xf64, #SparseMatrix> to memref<?xf64>, memref<?xi1>, memref<?xindex>, index 164 return 165} 166 167// ----- 168 169#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}> 170 171// CHECK-LABEL: func @sparse_compression( 172// CHECK-SAME: %[[A:.*]]: tensor<8x8xf64, #sparse_tensor.encoding<{{.*}}>>, 173// CHECK: sparse_tensor.compress %[[A]] 174// CHECK: return 175func.func @sparse_compression(%arg0: tensor<8x8xf64, #SparseMatrix>, 176 %arg1: memref<?xindex>, %arg2: memref<?xf64>, %arg3: memref<?xi1>, 177 %arg4: memref<?xindex>, %arg5: index) { 178 sparse_tensor.compress %arg0, %arg1, %arg2, %arg3, %arg4, %arg5 179 : tensor<8x8xf64, #SparseMatrix>, memref<?xindex>, memref<?xf64>, memref<?xi1>, memref<?xindex>, index 180 return 181} 182 183// ----- 184 185#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}> 186 187// CHECK-LABEL: func @sparse_out( 188// CHECK-SAME: %[[A:.*]]: tensor<?x?xf64, #sparse_tensor.encoding<{{.*}}>>, 189// CHECK-SAME: %[[B:.*]]: !llvm.ptr<i8>) 190// CHECK: sparse_tensor.out %[[A]], %[[B]] : tensor<?x?xf64, #sparse_tensor.encoding<{{.*}}>>, !llvm.ptr<i8> 191// CHECK: return 192func.func @sparse_out(%arg0: tensor<?x?xf64, #SparseMatrix>, %arg1: !llvm.ptr<i8>) { 193 sparse_tensor.out %arg0, %arg1 : tensor<?x?xf64, #SparseMatrix>, !llvm.ptr<i8> 194 return 195} 196 197// ----- 198 199#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}> 200 201// CHECK-LABEL: func @sparse_binary( 202// CHECK-SAME: %[[A:.*]]: f64, %[[B:.*]]: i64) -> f64 { 203// CHECK: %[[Z:.*]] = arith.constant 0.000000e+00 : f64 204// CHECK: %[[C1:.*]] = sparse_tensor.binary %[[A]], %[[B]] : f64, i64 to f64 205// CHECK: overlap = { 206// CHECK: ^bb0(%[[A1:.*]]: f64, %[[B1:.*]]: i64): 207// CHECK: sparse_tensor.yield %[[A1]] : f64 208// CHECK: } 209// CHECK: left = identity 210// CHECK: right = { 211// CHECK: ^bb0(%[[A2:.*]]: i64): 212// CHECK: sparse_tensor.yield %[[Z]] : f64 213// CHECK: } 214// CHECK: return %[[C1]] : f64 215// CHECK: } 216func.func @sparse_binary(%arg0: f64, %arg1: i64) -> f64 { 217 %cf0 = arith.constant 0.0 : f64 218 %r = sparse_tensor.binary %arg0, %arg1 : f64, i64 to f64 219 overlap={ 220 ^bb0(%x: f64, %y: i64): 221 sparse_tensor.yield %x : f64 222 } 223 left=identity 224 right={ 225 ^bb0(%y: i64): 226 sparse_tensor.yield %cf0 : f64 227 } 228 return %r : f64 229} 230 231// ----- 232 233#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}> 234 235// CHECK-LABEL: func @sparse_unary( 236// CHECK-SAME: %[[A:.*]]: f64) -> f64 { 237// CHECK: %[[C1:.*]] = sparse_tensor.unary %[[A]] : f64 to f64 238// CHECK: present = { 239// CHECK: ^bb0(%[[A1:.*]]: f64): 240// CHECK: sparse_tensor.yield %[[A1]] : f64 241// CHECK: } 242// CHECK: absent = { 243// CHECK: %[[R:.*]] = arith.constant -1.000000e+00 : f64 244// CHECK: sparse_tensor.yield %[[R]] : f64 245// CHECK: } 246// CHECK: return %[[C1]] : f64 247// CHECK: } 248func.func @sparse_unary(%arg0: f64) -> f64 { 249 %r = sparse_tensor.unary %arg0 : f64 to f64 250 present={ 251 ^bb0(%x: f64): 252 sparse_tensor.yield %x : f64 253 } absent={ 254 ^bb0: 255 %cf1 = arith.constant -1.0 : f64 256 sparse_tensor.yield %cf1 : f64 257 } 258 return %r : f64 259} 260 261// ----- 262 263#SparseMatrix = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}> 264 265// CHECK-LABEL: func @sparse_unary( 266// CHECK-SAME: %[[A:.*]]: f64) -> i64 { 267// CHECK: %[[C1:.*]] = sparse_tensor.unary %[[A]] : f64 to i64 268// CHECK: present = { 269// CHECK: ^bb0(%[[A1:.*]]: f64): 270// CHECK: %[[R:.*]] = arith.fptosi %[[A1]] : f64 to i64 271// CHECK: sparse_tensor.yield %[[R]] : i64 272// CHECK: } 273// CHECK: absent = { 274// CHECK: } 275// CHECK: return %[[C1]] : i64 276// CHECK: } 277func.func @sparse_unary(%arg0: f64) -> i64 { 278 %r = sparse_tensor.unary %arg0 : f64 to i64 279 present={ 280 ^bb0(%x: f64): 281 %ret = arith.fptosi %x : f64 to i64 282 sparse_tensor.yield %ret : i64 283 } 284 absent={} 285 return %r : i64 286} 287