1// RUN: mlir-opt %s --sparse-tensor-conversion --canonicalize | FileCheck %s 2 3#DenseVector = #sparse_tensor.encoding<{ 4 dimLevelType = ["dense"] 5}> 6 7#SparseVector = #sparse_tensor.encoding<{ 8 dimLevelType = ["compressed"] 9}> 10 11#SparseVector64 = #sparse_tensor.encoding<{ 12 dimLevelType = ["compressed"], 13 pointerBitWidth = 64, 14 indexBitWidth = 64 15}> 16 17#SparseVector32 = #sparse_tensor.encoding<{ 18 dimLevelType = ["compressed"], 19 pointerBitWidth = 32, 20 indexBitWidth = 32 21}> 22 23#SparseMatrix = #sparse_tensor.encoding<{ 24 dimLevelType = ["dense", "compressed"] 25}> 26 27#SparseTensor = #sparse_tensor.encoding<{ 28 dimLevelType = ["dense", "compressed", "compressed"], 29 dimOrdering = affine_map<(i,j,k) -> (k,i,j)> 30}> 31 32// CHECK-LABEL: func @sparse_dim( 33// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 34// CHECK: %[[C:.*]] = constant 0 : index 35// CHECK: %[[D:.*]] = call @sparseDimSize(%[[A]], %[[C]]) 36// CHECK: return %[[D]] : index 37func @sparse_dim(%arg0: tensor<?xf64, #SparseVector>) -> index { 38 %c = constant 0 : index 39 %0 = tensor.dim %arg0, %c : tensor<?xf64, #SparseVector> 40 return %0 : index 41} 42 43// CHECK-LABEL: func @sparse_new1d( 44// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) -> !llvm.ptr<i8> 45// CHECK-DAG: %[[U:.*]] = constant dense<1> : tensor<1xi8> 46// CHECK-DAG: %[[V:.*]] = constant dense<128> : tensor<1xi64> 47// CHECK-DAG: %[[W:.*]] = constant dense<0> : tensor<1xi64> 48// CHECK-DAG: %[[X:.*]] = tensor.cast %[[U]] : tensor<1xi8> to tensor<?xi8> 49// CHECK-DAG: %[[Y:.*]] = tensor.cast %[[V]] : tensor<1xi64> to tensor<?xi64> 50// CHECK-DAG: %[[Z:.*]] = tensor.cast %[[W]] : tensor<1xi64> to tensor<?xi64> 51// CHECK: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[A]]) 52// CHECK: return %[[T]] : !llvm.ptr<i8> 53func @sparse_new1d(%arg0: !llvm.ptr<i8>) -> tensor<128xf64, #SparseVector> { 54 %0 = sparse_tensor.new %arg0 : !llvm.ptr<i8> to tensor<128xf64, #SparseVector> 55 return %0 : tensor<128xf64, #SparseVector> 56} 57 58// CHECK-LABEL: func @sparse_new2d( 59// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) -> !llvm.ptr<i8> 60// CHECK-DAG: %[[U:.*]] = constant dense<[0, 1]> : tensor<2xi8> 61// CHECK-DAG: %[[V:.*]] = constant dense<0> : tensor<2xi64> 62// CHECK-DAG: %[[W:.*]] = constant dense<[0, 1]> : tensor<2xi64> 63// CHECK-DAG: %[[X:.*]] = tensor.cast %[[U]] : tensor<2xi8> to tensor<?xi8> 64// CHECK-DAG: %[[Y:.*]] = tensor.cast %[[V]] : tensor<2xi64> to tensor<?xi64> 65// CHECK-DAG: %[[Z:.*]] = tensor.cast %[[W]] : tensor<2xi64> to tensor<?xi64> 66// CHECK: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[A]]) 67// CHECK: return %[[T]] : !llvm.ptr<i8> 68func @sparse_new2d(%arg0: !llvm.ptr<i8>) -> tensor<?x?xf32, #SparseMatrix> { 69 %0 = sparse_tensor.new %arg0 : !llvm.ptr<i8> to tensor<?x?xf32, #SparseMatrix> 70 return %0 : tensor<?x?xf32, #SparseMatrix> 71} 72 73// CHECK-LABEL: func @sparse_new3d( 74// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) -> !llvm.ptr<i8> 75// CHECK-DAG: %[[U:.*]] = constant dense<[0, 1, 1]> : tensor<3xi8> 76// CHECK-DAG: %[[V:.*]] = constant dense<0> : tensor<3xi64> 77// CHECK-DAG: %[[W:.*]] = constant dense<[1, 2, 0]> : tensor<3xi64> 78// CHECK-DAG: %[[X:.*]] = tensor.cast %[[U]] : tensor<3xi8> to tensor<?xi8> 79// CHECK-DAG: %[[Y:.*]] = tensor.cast %[[V]] : tensor<3xi64> to tensor<?xi64> 80// CHECK-DAG: %[[Z:.*]] = tensor.cast %[[W]] : tensor<3xi64> to tensor<?xi64> 81// CHECK: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[A]]) 82// CHECK: return %[[T]] : !llvm.ptr<i8> 83func @sparse_new3d(%arg0: !llvm.ptr<i8>) -> tensor<?x?x?xf32, #SparseTensor> { 84 %0 = sparse_tensor.new %arg0 : !llvm.ptr<i8> to tensor<?x?x?xf32, #SparseTensor> 85 return %0 : tensor<?x?x?xf32, #SparseTensor> 86} 87 88// CHECK-LABEL: func @sparse_convert( 89// CHECK-SAME: %[[A:.*]]: tensor<2x4xf64>) -> !llvm.ptr<i8> 90// CHECK-DAG: %[[U:.*]] = constant dense<[0, 1]> : tensor<2xi8> 91// CHECK-DAG: %[[V:.*]] = constant dense<[2, 4]> : tensor<2xi64> 92// CHECK-DAG: %[[W:.*]] = constant dense<[0, 1]> : tensor<2xi64> 93// CHECK: %[[C:.*]] = memref.buffer_cast %arg0 : memref<2x4xf64> 94// CHECK: %[[M:.*]] = memref.cast %[[C]] : memref<2x4xf64> to memref<*xf64> 95// CHECK: %[[C:.*]] = call @getPtrF64(%[[M]]) : (memref<*xf64>) -> !llvm.ptr<i8> 96// CHECK-DAG: %[[X:.*]] = tensor.cast %[[U]] : tensor<2xi8> to tensor<?xi8> 97// CHECK-DAG: %[[Y:.*]] = tensor.cast %[[V]] : tensor<2xi64> to tensor<?xi64> 98// CHECK-DAG: %[[Z:.*]] = tensor.cast %[[W]] : tensor<2xi64> to tensor<?xi64> 99// CHECK: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[C]]) 100// CHECK: return %[[T]] : !llvm.ptr<i8> 101func @sparse_convert(%arg0: tensor<2x4xf64>) -> tensor<2x4xf64, #SparseMatrix> { 102 %0 = sparse_tensor.convert %arg0 : tensor<2x4xf64> to tensor<2x4xf64, #SparseMatrix> 103 return %0 : tensor<2x4xf64, #SparseMatrix> 104} 105 106// CHECK-LABEL: func @sparse_pointers( 107// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 108// CHECK: %[[C:.*]] = constant 0 : index 109// CHECK: %[[T:.*]] = call @sparsePointers(%[[A]], %[[C]]) : (!llvm.ptr<i8>, index) -> memref<?xindex> 110// CHECK: return %[[T]] : memref<?xindex> 111func @sparse_pointers(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> { 112 %c = constant 0 : index 113 %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf64, #SparseVector> to memref<?xindex> 114 return %0 : memref<?xindex> 115} 116 117// CHECK-LABEL: func @sparse_pointers64( 118// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 119// CHECK: %[[C:.*]] = constant 0 : index 120// CHECK: %[[T:.*]] = call @sparsePointers64(%[[A]], %[[C]]) : (!llvm.ptr<i8>, index) -> memref<?xi64> 121// CHECK: return %[[T]] : memref<?xi64> 122func @sparse_pointers64(%arg0: tensor<128xf64, #SparseVector64>) -> memref<?xi64> { 123 %c = constant 0 : index 124 %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf64, #SparseVector64> to memref<?xi64> 125 return %0 : memref<?xi64> 126} 127 128// CHECK-LABEL: func @sparse_pointers32( 129// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 130// CHECK: %[[C:.*]] = constant 0 : index 131// CHECK: %[[T:.*]] = call @sparsePointers32(%[[A]], %[[C]]) : (!llvm.ptr<i8>, index) -> memref<?xi32> 132// CHECK: return %[[T]] : memref<?xi32> 133func @sparse_pointers32(%arg0: tensor<128xf64, #SparseVector32>) -> memref<?xi32> { 134 %c = constant 0 : index 135 %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf64, #SparseVector32> to memref<?xi32> 136 return %0 : memref<?xi32> 137} 138 139// CHECK-LABEL: func @sparse_indices( 140// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 141// CHECK: %[[C:.*]] = constant 0 : index 142// CHECK: %[[T:.*]] = call @sparseIndices(%[[A]], %[[C]]) : (!llvm.ptr<i8>, index) -> memref<?xindex> 143// CHECK: return %[[T]] : memref<?xindex> 144func @sparse_indices(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> { 145 %c = constant 0 : index 146 %0 = sparse_tensor.indices %arg0, %c : tensor<128xf64, #SparseVector> to memref<?xindex> 147 return %0 : memref<?xindex> 148} 149 150// CHECK-LABEL: func @sparse_indices64( 151// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 152// CHECK: %[[C:.*]] = constant 0 : index 153// CHECK: %[[T:.*]] = call @sparseIndices64(%[[A]], %[[C]]) : (!llvm.ptr<i8>, index) -> memref<?xi64> 154// CHECK: return %[[T]] : memref<?xi64> 155func @sparse_indices64(%arg0: tensor<128xf64, #SparseVector64>) -> memref<?xi64> { 156 %c = constant 0 : index 157 %0 = sparse_tensor.indices %arg0, %c : tensor<128xf64, #SparseVector64> to memref<?xi64> 158 return %0 : memref<?xi64> 159} 160 161// CHECK-LABEL: func @sparse_indices32( 162// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 163// CHECK: %[[C:.*]] = constant 0 : index 164// CHECK: %[[T:.*]] = call @sparseIndices32(%[[A]], %[[C]]) : (!llvm.ptr<i8>, index) -> memref<?xi32> 165// CHECK: return %[[T]] : memref<?xi32> 166func @sparse_indices32(%arg0: tensor<128xf64, #SparseVector32>) -> memref<?xi32> { 167 %c = constant 0 : index 168 %0 = sparse_tensor.indices %arg0, %c : tensor<128xf64, #SparseVector32> to memref<?xi32> 169 return %0 : memref<?xi32> 170} 171 172// CHECK-LABEL: func @sparse_valuesf64( 173// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 174// CHECK: %[[T:.*]] = call @sparseValuesF64(%[[A]]) : (!llvm.ptr<i8>) -> memref<?xf64> 175// CHECK: return %[[T]] : memref<?xf64> 176func @sparse_valuesf64(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xf64> { 177 %0 = sparse_tensor.values %arg0 : tensor<128xf64, #SparseVector> to memref<?xf64> 178 return %0 : memref<?xf64> 179} 180 181// CHECK-LABEL: func @sparse_valuesf32( 182// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 183// CHECK: %[[T:.*]] = call @sparseValuesF32(%[[A]]) : (!llvm.ptr<i8>) -> memref<?xf32> 184// CHECK: return %[[T]] : memref<?xf32> 185func @sparse_valuesf32(%arg0: tensor<128xf32, #SparseVector>) -> memref<?xf32> { 186 %0 = sparse_tensor.values %arg0: tensor<128xf32, #SparseVector> to memref<?xf32> 187 return %0 : memref<?xf32> 188} 189 190// CHECK-LABEL: func @sparse_valuesi32( 191// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 192// CHECK: %[[T:.*]] = call @sparseValuesI32(%[[A]]) : (!llvm.ptr<i8>) -> memref<?xi32> 193// CHECK: return %[[T]] : memref<?xi32> 194func @sparse_valuesi32(%arg0: tensor<128xi32, #SparseVector>) -> memref<?xi32> { 195 %0 = sparse_tensor.values %arg0: tensor<128xi32, #SparseVector> to memref<?xi32> 196 return %0 : memref<?xi32> 197} 198 199// CHECK-LABEL: func @sparse_valuesi16( 200// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 201// CHECK: %[[T:.*]] = call @sparseValuesI16(%[[A]]) : (!llvm.ptr<i8>) -> memref<?xi16> 202// CHECK: return %[[T]] : memref<?xi16> 203func @sparse_valuesi16(%arg0: tensor<128xi16, #SparseVector>) -> memref<?xi16> { 204 %0 = sparse_tensor.values %arg0: tensor<128xi16, #SparseVector> to memref<?xi16> 205 return %0 : memref<?xi16> 206} 207 208// CHECK-LABEL: func @sparse_valuesi8( 209// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) 210// CHECK: %[[T:.*]] = call @sparseValuesI8(%[[A]]) : (!llvm.ptr<i8>) -> memref<?xi8> 211// CHECK: return %[[T]] : memref<?xi8> 212func @sparse_valuesi8(%arg0: tensor<128xi8, #SparseVector>) -> memref<?xi8> { 213 %0 = sparse_tensor.values %arg0: tensor<128xi8, #SparseVector> to memref<?xi8> 214 return %0 : memref<?xi8> 215} 216 217// CHECK-LABEL: func @sparse_reconstruct_1( 218// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8> 219// CHECK: return %[[A]] : !llvm.ptr<i8> 220func @sparse_reconstruct_1(%arg0: tensor<128xf32, #DenseVector> {linalg.inplaceable = true}) -> tensor<128xf32, #DenseVector> { 221 %0 = sparse_tensor.values %arg0 : tensor<128xf32, #DenseVector> to memref<?xf32> 222 %1 = sparse_tensor.tensor %0 : memref<?xf32> to tensor<128xf32, #DenseVector> 223 return %1 : tensor<128xf32, #DenseVector> 224} 225 226// CHECK-LABEL: func @sparse_reconstruct_n( 227// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8> 228// CHECK: return %[[A]] : !llvm.ptr<i8> 229func @sparse_reconstruct_n(%arg0: tensor<128xf32, #SparseVector> {linalg.inplaceable = true}) -> tensor<128xf32, #SparseVector> { 230 %c = constant 0 : index 231 %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf32, #SparseVector> to memref<?xindex> 232 %1 = sparse_tensor.indices %arg0, %c : tensor<128xf32, #SparseVector> to memref<?xindex> 233 %2 = sparse_tensor.values %arg0 : tensor<128xf32, #SparseVector> to memref<?xf32> 234 %3 = sparse_tensor.tensor %0, %1, %2 : memref<?xindex>, memref<?xindex>, memref<?xf32> to tensor<128xf32, #SparseVector> 235 return %3 : tensor<128xf32, #SparseVector> 236} 237