xref: /llvm-project/mlir/test/Dialect/SparseTensor/convert_sparse2sparse.mlir (revision ef100c228a5913c6c54bfed8e80fd265ce8beca2)
1// RUN: mlir-opt %s --stage-sparse-ops --lower-sparse-ops-to-foreach --canonicalize --cse | FileCheck %s
2
3#SparseVector64 = #sparse_tensor.encoding<{
4  map = (d0) -> (d0 : compressed),
5  posWidth = 64,
6  crdWidth = 64
7}>
8
9#SparseVector32 = #sparse_tensor.encoding<{
10  map = (d0) -> (d0 : compressed),
11  posWidth = 32,
12  crdWidth = 32
13}>
14
15#SparseVector = #sparse_tensor.encoding<{
16  map = (d0) -> (d0 : compressed)
17}>
18
19#SortedCOO2D = #sparse_tensor.encoding<{
20  map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton),
21}>
22
23#SortedCOO3D = #sparse_tensor.encoding<{
24  map = (d0, d1, d2) -> (d0 : compressed(nonunique), d1 : singleton(nonunique), d2 : singleton)
25
26}>
27
28#TsssPermuted = #sparse_tensor.encoding<{
29  map = (d0, d1, d2) -> (d2 : compressed, d0 : compressed, d1 : compressed)
30}>
31
32#COOSlice = #sparse_tensor.encoding<{
33  map = (d0 : #sparse_tensor<slice(2, 2, 1)>, d1 : #sparse_tensor<slice(12, 13, 1)>) -> (d0 : compressed(nonunique), d1 : singleton)
34}>
35
36// CHECK-LABEL:   func.func @sparse_nop_convert
37// CHECK-NEXT:       return
38func.func @sparse_nop_convert(%arg0: tensor<64xf32, #SparseVector>) -> tensor<64xf32, #SparseVector> {
39  %0 = sparse_tensor.convert %arg0 : tensor<64xf32, #SparseVector> to tensor<64xf32, #SparseVector>
40  return %0 : tensor<64xf32, #SparseVector>
41}
42
43// CHECK-LABEL:   func.func @sparse_hidden_nop_cast
44// CHECK-NEXT:      sparse_tensor.convert
45// CHECK-NEXT:      return
46func.func @sparse_hidden_nop_cast(%arg0: tensor<32xf32, #SparseVector>) -> tensor<?xf32, #SparseVector> {
47  %0 = sparse_tensor.convert %arg0 : tensor<32xf32, #SparseVector> to tensor<?xf32, #SparseVector>
48  return %0 : tensor<?xf32, #SparseVector>
49}
50
51// CHECK-LABEL:   func.func @sparse_convert_1d_ss(
52// CHECK-NEXT:      sparse_tensor.convert
53// CHECK-NEXT:      return
54func.func @sparse_convert_1d_ss(%arg0: tensor<?xf32, #SparseVector64>) -> tensor<?xf32, #SparseVector32> {
55  %0 = sparse_tensor.convert %arg0 : tensor<?xf32, #SparseVector64> to tensor<?xf32, #SparseVector32>
56  return %0 : tensor<?xf32, #SparseVector32>
57}
58
59// CHECK-LABEL:   func.func @sparse_convert(
60// CHECK-NEXT:      sparse_tensor.convert
61// CHECK-NEXT:      return
62func.func @sparse_convert(%arg0: tensor<?xf32, #SparseVector64>) -> tensor<?xf32, #SparseVector32> {
63  %0 = sparse_tensor.convert %arg0 : tensor<?xf32, #SparseVector64> to tensor<?xf32, #SparseVector32>
64  return %0 : tensor<?xf32, #SparseVector32>
65}
66
67// CHECK-LABEL:   func.func @sparse_convert_permuted
68// CHECK:           sparse_tensor.foreach
69// CHECK:             tensor.insert
70// CHECK:           sparse_tensor.load
71// CHECK:           sparse_tensor.reorder_coo
72// CHECK:           sparse_tensor.foreach
73// CHECK:             tensor.insert
74// CHECK:           sparse_tensor.load
75// CHECK:           return
76func.func @sparse_convert_permuted(%arg0: tensor<?x?x?xf32, #SortedCOO3D>) -> tensor<?x?x?xf32, #TsssPermuted> {
77  %0 = sparse_tensor.convert %arg0 : tensor<?x?x?xf32, #SortedCOO3D> to tensor<?x?x?xf32, #TsssPermuted>
78  return %0 : tensor<?x?x?xf32, #TsssPermuted>
79}
80
81// CHECK-LABEL:   func.func @sparse_convert_slice
82// CHECK:           sparse_tensor.foreach
83// CHECK:             tensor.insert
84// CHECK:           sparse_tensor.load
85// CHECK-NOT:       sparse_tensor.reorder_coo
86// CHECK:           return
87func.func @sparse_convert_slice(%arg0: tensor<2x13xi32, #COOSlice>) -> (tensor<2x13xi32, #SortedCOO2D>)  {
88  %0 = sparse_tensor.convert %arg0 : tensor<2x13xi32, #COOSlice> to tensor<2x13xi32, #SortedCOO2D>
89  return %0 : tensor<2x13xi32, #SortedCOO2D>
90}
91