xref: /llvm-project/mlir/test/Dialect/SparseTensor/sparse_pack.mlir (revision ced2fc7819d5ddea616ec330f18e08ff284c1868)
1f82bee13SPeiming Liu// RUN: mlir-opt %s --canonicalize --sparse-tensor-codegen -cse --canonicalize | FileCheck %s
2a41672e1SPeiming Liu
3a41672e1SPeiming Liu#COO = #sparse_tensor.encoding<{
43dc62112SYinying Li  map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton),
584cd51bbSwren romano  crdWidth=32
6a41672e1SPeiming Liu}>
7a41672e1SPeiming Liu
8a41672e1SPeiming Liu// CHECK-LABEL:   func.func @sparse_pack(
9a41672e1SPeiming Liu// CHECK-SAME:      %[[VAL_0:.*]]: tensor<6xf64>,
10de560888SPeiming Liu// CHECK-SAME:      %[[VAL_1:.*]]: tensor<2xindex>,
11de560888SPeiming Liu// CHECK-SAME:      %[[VAL_2:.*]]: tensor<6x2xi32>)
12f7b8b005SPeiming Liu// CHECK-DAG:       %[[VAL_3:.*]] = arith.constant 2 : index
13f7b8b005SPeiming Liu// CHECK-DAG:       %[[VAL_4:.*]] = arith.constant 100 : index
14f7b8b005SPeiming Liu// CHECK-DAG:       %[[VAL_5:.*]] = arith.constant 1 : index
15*ced2fc78SChristopher Bate// CHECK-DAG:       %[[VAL_6:.*]] = bufferization.to_memref %[[VAL_1]] : tensor<2xindex> to memref<2xindex>
16f7b8b005SPeiming Liu// CHECK-DAG:       %[[VAL_7:.*]] = memref.cast %[[VAL_6]] : memref<2xindex> to memref<?xindex>
17*ced2fc78SChristopher Bate// CHECK-DAG:       %[[VAL_8:.*]] = bufferization.to_memref %[[VAL_2]] : tensor<6x2xi32> to memref<6x2xi32>
18f7b8b005SPeiming Liu// CHECK-DAG:       %[[VAL_9:.*]] = memref.collapse_shape %[[VAL_8]] {{\[\[}}0, 1]] : memref<6x2xi32> into memref<12xi32>
19f7b8b005SPeiming Liu// CHECK-DAG:       %[[VAL_10:.*]] = memref.cast %[[VAL_9]] : memref<12xi32> to memref<?xi32>
20*ced2fc78SChristopher Bate// CHECK-DAG:       %[[VAL_11:.*]] = bufferization.to_memref %[[VAL_0]] : tensor<6xf64> to memref<6xf64>
21f7b8b005SPeiming Liu// CHECK-DAG:       %[[VAL_12:.*]] = memref.cast %[[VAL_11]] : memref<6xf64> to memref<?xf64>
22f7b8b005SPeiming Liu// CHECK:           %[[VAL_13:.*]] = sparse_tensor.storage_specifier.init
23f7b8b005SPeiming Liu// CHECK:           %[[VAL_14:.*]] = sparse_tensor.storage_specifier.set %[[VAL_13]]  lvl_sz at 0 with %[[VAL_4]]
24f7b8b005SPeiming Liu// CHECK:           %[[VAL_15:.*]] = sparse_tensor.storage_specifier.set %[[VAL_14]]  pos_mem_sz at 0 with %[[VAL_3]]
25f7b8b005SPeiming Liu// CHECK:           %[[VAL_16:.*]] = tensor.extract %[[VAL_1]]{{\[}}%[[VAL_5]]] : tensor<2xindex>
26f7b8b005SPeiming Liu// CHECK:           %[[VAL_17:.*]] = arith.muli %[[VAL_16]], %[[VAL_3]] : index
27b2e6b735SPeiming Liu// CHECK:           %[[VAL_18:.*]] = sparse_tensor.storage_specifier.set %[[VAL_15]]  crd_mem_sz at 0 with %[[VAL_17]]
28f7b8b005SPeiming Liu// CHECK:           %[[VAL_19:.*]] = sparse_tensor.storage_specifier.set %[[VAL_18]]  lvl_sz at 1 with %[[VAL_4]]
29de560888SPeiming Liu// CHECK:           %[[VAL_20:.*]] = sparse_tensor.storage_specifier.set %[[VAL_19]]  val_mem_sz with %[[VAL_16]]
30f7b8b005SPeiming Liu// CHECK:           return %[[VAL_7]], %[[VAL_10]], %[[VAL_12]], %[[VAL_20]]
31a41672e1SPeiming Liu// CHECK:         }
32de560888SPeiming Liufunc.func @sparse_pack(%values: tensor<6xf64>, %pos:tensor<2xindex>, %coordinates: tensor<6x2xi32>)
33a41672e1SPeiming Liu                    -> tensor<100x100xf64, #COO> {
34fc9f1d49SPeiming Liu  %0 = sparse_tensor.assemble (%pos, %coordinates), %values
35fc9f1d49SPeiming Liu     : (tensor<2xindex>, tensor<6x2xi32>), tensor<6xf64> to tensor<100x100xf64, #COO>
36a41672e1SPeiming Liu  return %0 : tensor<100x100xf64, #COO>
37a41672e1SPeiming Liu}
38dc6427d6SPeiming Liu
39dc6427d6SPeiming Liu// CHECK-LABEL:   func.func @sparse_unpack(
40dc6427d6SPeiming Liu// CHECK-SAME:      %[[VAL_0:.*]]: memref<?xindex>,
41dc6427d6SPeiming Liu// CHECK-SAME:      %[[VAL_1:.*]]: memref<?xi32>,
42dc6427d6SPeiming Liu// CHECK-SAME:      %[[VAL_2:.*]]: memref<?xf64>,
43c5a67e16SYinying Li// CHECK-SAME:      %[[VAL_3:.*]]: !sparse_tensor.storage_specifier<#sparse{{[0-9]*}}>,
44b2e6b735SPeiming Liu// CHECK-SAME:      %[[VAL_4:.*]]: tensor<6xf64>,
45b2e6b735SPeiming Liu// CHECK-SAME:      %[[VAL_5:.*]]: tensor<2xindex>,
46b2e6b735SPeiming Liu// CHECK-SAME:      %[[VAL_6:.*]]: tensor<6x2xi32>) -> (tensor<6xf64>, tensor<2xindex>, tensor<6x2xi32>) {
47b2e6b735SPeiming Liu// CHECK:           %[[VAL_7:.*]] = sparse_tensor.storage_specifier.get %[[VAL_3]]  pos_mem_sz at 0
48*ced2fc78SChristopher Bate// CHECK:           %[[VAL_8:.*]] = bufferization.to_memref %[[VAL_5]] : tensor<2xindex> to memref<2xindex>
49b2e6b735SPeiming Liu// CHECK:           %[[VAL_9:.*]] = memref.subview %[[VAL_8]][0] {{\[}}%[[VAL_7]]] [1] : memref<2xindex> to memref<?xindex>
50b2e6b735SPeiming Liu// CHECK:           %[[VAL_10:.*]] = memref.subview %[[VAL_0]][0] {{\[}}%[[VAL_7]]] [1] : memref<?xindex> to memref<?xindex>
51b2e6b735SPeiming Liu// CHECK:           memref.copy %[[VAL_10]], %[[VAL_9]] : memref<?xindex> to memref<?xindex>
52b2e6b735SPeiming Liu// CHECK:           %[[VAL_11:.*]] = sparse_tensor.storage_specifier.get %[[VAL_3]]  crd_mem_sz at 0
53*ced2fc78SChristopher Bate// CHECK:           %[[VAL_12:.*]] = bufferization.to_memref %[[VAL_6]] : tensor<6x2xi32> to memref<6x2xi32>
54b2e6b735SPeiming Liu// CHECK:           %[[VAL_13:.*]] = memref.collapse_shape %[[VAL_12]] {{\[\[}}0, 1]] : memref<6x2xi32> into memref<12xi32>
55b2e6b735SPeiming Liu// CHECK:           %[[VAL_14:.*]] = memref.subview %[[VAL_13]][0] {{\[}}%[[VAL_11]]] [1] : memref<12xi32> to memref<?xi32>
56b2e6b735SPeiming Liu// CHECK:           %[[VAL_15:.*]] = memref.subview %[[VAL_1]][0] {{\[}}%[[VAL_11]]] [1] : memref<?xi32> to memref<?xi32>
57b2e6b735SPeiming Liu// CHECK:           memref.copy %[[VAL_15]], %[[VAL_14]] : memref<?xi32> to memref<?xi32>
58b2e6b735SPeiming Liu// CHECK:           %[[VAL_16:.*]] = sparse_tensor.storage_specifier.get %[[VAL_3]]  val_mem_sz
59*ced2fc78SChristopher Bate// CHECK:           %[[VAL_17:.*]] = bufferization.to_memref %[[VAL_4]] : tensor<6xf64> to memref<6xf64>
60b2e6b735SPeiming Liu// CHECK:           %[[VAL_18:.*]] = memref.subview %[[VAL_17]][0] {{\[}}%[[VAL_16]]] [1] : memref<6xf64> to memref<?xf64>
61b2e6b735SPeiming Liu// CHECK:           %[[VAL_19:.*]] = memref.subview %[[VAL_2]][0] {{\[}}%[[VAL_16]]] [1] : memref<?xf64> to memref<?xf64>
62b2e6b735SPeiming Liu// CHECK:           memref.copy %[[VAL_19]], %[[VAL_18]] : memref<?xf64> to memref<?xf64>
63fc9f1d49SPeiming Liu// CHECK-DAG:       %[[VAL_20:.*]] = bufferization.to_tensor %[[VAL_17]] : memref<6xf64>
64fc9f1d49SPeiming Liu// CHECK-DAG:       %[[VAL_21:.*]] = bufferization.to_tensor %[[VAL_8]] : memref<2xindex>
65fc9f1d49SPeiming Liu// CHECK-DAG:       %[[VAL_22:.*]] = bufferization.to_tensor %[[VAL_12]] : memref<6x2xi32>
66b2e6b735SPeiming Liu// CHECK:           return %[[VAL_20]], %[[VAL_21]], %[[VAL_22]] : tensor<6xf64>, tensor<2xindex>, tensor<6x2xi32>
6781cb70e4SPeiming Liu// CHECK:         }
68b2e6b735SPeiming Liufunc.func @sparse_unpack(%sp : tensor<100x100xf64, #COO>,
69b2e6b735SPeiming Liu                         %od : tensor<6xf64>,
70b2e6b735SPeiming Liu                         %op : tensor<2xindex>,
71b2e6b735SPeiming Liu                         %oi : tensor<6x2xi32>)
72b2e6b735SPeiming Liu                       -> (tensor<6xf64>, tensor<2xindex>, tensor<6x2xi32>) {
73fc9f1d49SPeiming Liu  %rp, %ri, %rd, %dl, %pl, %il = sparse_tensor.disassemble %sp : tensor<100x100xf64, #COO>
74fc9f1d49SPeiming Liu                                 out_lvls(%op, %oi : tensor<2xindex>, tensor<6x2xi32>)
75fc9f1d49SPeiming Liu                                 out_vals(%od : tensor<6xf64>)
76fc9f1d49SPeiming Liu                                 -> (tensor<2xindex>, tensor<6x2xi32>), tensor<6xf64>, (index, index), index
77b2e6b735SPeiming Liu  return %rd, %rp, %ri : tensor<6xf64>, tensor<2xindex>, tensor<6x2xi32>
78dc6427d6SPeiming Liu}
79