llvm-project/mlir/test/Dialect/SparseTensor/convert_sparse2sparse.mlir
bixia1 0d60000257 [mlir][sparse] Reorganize tests for the sparse_tensor.convert operator.
Rename conversion_sparse2dense.mlir and conversion_sparse2sparse.mlir to
convert_sparse2dense.mlir/sparse2sparse.mlir.

Add convert_dense2sparse.mlir. Move the sparse_tensor.convert operator tests
out of conversion.mlir.

Reviewed By: aartbik

Differential Revision: https://reviews.llvm.org/D135922
2022-10-14 14:38:18 -07:00

128 lines
6.8 KiB
MLIR

// First use with `kViaCOO` for sparse2sparse conversion (the old way).
// RUN: mlir-opt %s --sparse-tensor-conversion="s2s-strategy=1" \
// RUN: --canonicalize --cse | FileCheck %s -check-prefix=CHECK-COO
//
// Now again with `kAuto` (the new default).
// RUN: mlir-opt %s --sparse-tensor-conversion="s2s-strategy=0" \
// RUN: --canonicalize --cse | FileCheck %s -check-prefixes=CHECK-AUTO,CHECK
#SparseVector64 = #sparse_tensor.encoding<{
dimLevelType = ["compressed"],
pointerBitWidth = 64,
indexBitWidth = 64
}>
#SparseVector32 = #sparse_tensor.encoding<{
dimLevelType = ["compressed"],
pointerBitWidth = 32,
indexBitWidth = 32
}>
#SparseVector = #sparse_tensor.encoding<{
dimLevelType = ["compressed"]
}>
// CHECK-LABEL: func @sparse_nop_convert(
// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) -> !llvm.ptr<i8>
// CHECK: return %[[A]] : !llvm.ptr<i8>
func.func @sparse_nop_convert(%arg0: tensor<64xf32, #SparseVector>) -> tensor<64xf32, #SparseVector> {
%0 = sparse_tensor.convert %arg0 : tensor<64xf32, #SparseVector> to tensor<64xf32, #SparseVector>
return %0 : tensor<64xf32, #SparseVector>
}
// CHECK-LABEL: func @sparse_hidden_nop_cast(
// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>) -> !llvm.ptr<i8>
// CHECK: return %[[A]] : !llvm.ptr<i8>
func.func @sparse_hidden_nop_cast(%arg0: tensor<32xf32, #SparseVector>) -> tensor<?xf32, #SparseVector> {
%0 = sparse_tensor.convert %arg0 : tensor<32xf32, #SparseVector> to tensor<?xf32, #SparseVector>
return %0 : tensor<?xf32, #SparseVector>
}
// CHECK-LABEL: func @sparse_convert_1d_ss(
// CHECK-SAME: %[[A:.*]]: !llvm.ptr<i8>)
// CHECK-DAG: %[[SparseToSparse:.*]] = arith.constant 3 : i32
// CHECK-DAG: %[[P:.*]] = memref.alloca() : memref<1xi8>
// CHECK-DAG: %[[Q:.*]] = memref.alloca() : memref<1xindex>
// CHECK-DAG: %[[R:.*]] = memref.alloca() : memref<1xindex>
// CHECK-DAG: %[[X:.*]] = memref.cast %[[P]] : memref<1xi8> to memref<?xi8>
// CHECK-DAG: %[[Y:.*]] = memref.cast %[[Q]] : memref<1xindex> to memref<?xindex>
// CHECK-DAG: %[[Z:.*]] = memref.cast %[[R]] : memref<1xindex> to memref<?xindex>
// CHECK: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %[[SparseToSparse]], %[[A]])
// CHECK: return %[[T]] : !llvm.ptr<i8>
func.func @sparse_convert_1d_ss(%arg0: tensor<?xf32, #SparseVector64>) -> tensor<?xf32, #SparseVector32> {
%0 = sparse_tensor.convert %arg0 : tensor<?xf32, #SparseVector64> to tensor<?xf32, #SparseVector32>
return %0 : tensor<?xf32, #SparseVector32>
}
// CHECK-COO-LABEL: func @sparse_convert(
// CHECK-COO-SAME: %[[A:.*]]: !llvm.ptr<i8>)
// CHECK-COO-DAG: %[[ToCOO:.*]] = arith.constant 5 : i32
// CHECK-COO-DAG: %[[FromCOO:.*]] = arith.constant 2 : i32
// CHECK-COO-DAG: %[[P:.*]] = memref.alloca() : memref<1xi8>
// CHECK-COO-DAG: %[[Q:.*]] = memref.alloca() : memref<1xindex>
// CHECK-COO-DAG: %[[R:.*]] = memref.alloca() : memref<1xindex>
// CHECK-COO-DAG: %[[X:.*]] = memref.cast %[[P]] : memref<1xi8> to memref<?xi8>
// CHECK-COO-DAG: %[[Y:.*]] = memref.cast %[[Q]] : memref<1xindex> to memref<?xindex>
// CHECK-COO-DAG: %[[Z:.*]] = memref.cast %[[R]] : memref<1xindex> to memref<?xindex>
// CHECK-COO: %[[C:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %[[ToCOO]], %[[A]])
// CHECK-COO: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %[[FromCOO]], %[[C]])
// CHECK-COO: call @delSparseTensorCOOF32(%[[C]])
// CHECK-COO: return %[[T]] : !llvm.ptr<i8>
// CHECK-AUTO-LABEL: func @sparse_convert(
// CHECK-AUTO-SAME: %[[A:.*]]: !llvm.ptr<i8>)
// CHECK-AUTO-DAG: %[[SparseToSparse:.*]] = arith.constant 3 : i32
// CHECK-AUTO-DAG: %[[P:.*]] = memref.alloca() : memref<1xi8>
// CHECK-AUTO-DAG: %[[Q:.*]] = memref.alloca() : memref<1xindex>
// CHECK-AUTO-DAG: %[[R:.*]] = memref.alloca() : memref<1xindex>
// CHECK-AUTO-DAG: %[[X:.*]] = memref.cast %[[P]] : memref<1xi8> to memref<?xi8>
// CHECK-AUTO-DAG: %[[Y:.*]] = memref.cast %[[Q]] : memref<1xindex> to memref<?xindex>
// CHECK-AUTO-DAG: %[[Z:.*]] = memref.cast %[[R]] : memref<1xindex> to memref<?xindex>
// CHECK-AUTO: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %[[SparseToSparse]], %[[A]])
// CHECK-AUTO: return %[[T]] : !llvm.ptr<i8>
func.func @sparse_convert(%arg0: tensor<?xf32, #SparseVector64>) -> tensor<?xf32, #SparseVector32> {
%0 = sparse_tensor.convert %arg0 : tensor<?xf32, #SparseVector64> to tensor<?xf32, #SparseVector32>
return %0 : tensor<?xf32, #SparseVector32>
}
#SparseSingleton64 = #sparse_tensor.encoding<{
dimLevelType = ["singleton"],
pointerBitWidth = 64,
indexBitWidth = 64
}>
#SparseSingleton32 = #sparse_tensor.encoding<{
dimLevelType = ["singleton"],
pointerBitWidth = 32,
indexBitWidth = 32
}>
// CHECK-COO-LABEL: func @sparse_convert_singleton(
// CHECK-COO-SAME: %[[A:.*]]: !llvm.ptr<i8>)
// CHECK-COO-DAG: %[[ToCOO:.*]] = arith.constant 5 : i32
// CHECK-COO-DAG: %[[FromCOO:.*]] = arith.constant 2 : i32
// CHECK-COO-DAG: %[[P:.*]] = memref.alloca() : memref<1xi8>
// CHECK-COO-DAG: %[[Q:.*]] = memref.alloca() : memref<1xindex>
// CHECK-COO-DAG: %[[R:.*]] = memref.alloca() : memref<1xindex>
// CHECK-COO-DAG: %[[X:.*]] = memref.cast %[[P]] : memref<1xi8> to memref<?xi8>
// CHECK-COO-DAG: %[[Y:.*]] = memref.cast %[[Q]] : memref<1xindex> to memref<?xindex>
// CHECK-COO-DAG: %[[Z:.*]] = memref.cast %[[R]] : memref<1xindex> to memref<?xindex>
// CHECK-COO: %[[C:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %[[ToCOO]], %[[A]])
// CHECK-COO: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %[[FromCOO]], %[[C]])
// CHECK-COO: call @delSparseTensorCOOF32(%[[C]])
// CHECK-COO: return %[[T]] : !llvm.ptr<i8>
// CHECK-AUTO-LABEL: func @sparse_convert_singleton(
// CHECK-AUTO-SAME: %[[A:.*]]: !llvm.ptr<i8>)
// CHECK-AUTO-DAG: %[[SparseToSparse:.*]] = arith.constant 3 : i32
// CHECK-AUTO-DAG: %[[P:.*]] = memref.alloca() : memref<1xi8>
// CHECK-AUTO-DAG: %[[Q:.*]] = memref.alloca() : memref<1xindex>
// CHECK-AUTO-DAG: %[[R:.*]] = memref.alloca() : memref<1xindex>
// CHECK-AUTO-DAG: %[[X:.*]] = memref.cast %[[P]] : memref<1xi8> to memref<?xi8>
// CHECK-AUTO-DAG: %[[Y:.*]] = memref.cast %[[Q]] : memref<1xindex> to memref<?xindex>
// CHECK-AUTO-DAG: %[[Z:.*]] = memref.cast %[[R]] : memref<1xindex> to memref<?xindex>
// CHECK-AUTO: %[[T:.*]] = call @newSparseTensor(%[[X]], %[[Y]], %[[Z]], %{{.*}}, %{{.*}}, %{{.*}}, %[[SparseToSparse]], %[[A]])
// CHECK-AUTO: return %[[T]] : !llvm.ptr<i8>
func.func @sparse_convert_singleton(%arg0: tensor<?xf32, #SparseSingleton64>) -> tensor<?xf32, #SparseSingleton32> {
%0 = sparse_tensor.convert %arg0 : tensor<?xf32, #SparseSingleton64> to tensor<?xf32, #SparseSingleton32>
return %0 : tensor<?xf32, #SparseSingleton32>
}