1// RUN: mlir-opt %s --sparse-compiler | \
2// RUN: mlir-cpu-runner \
3// RUN:  -e entry -entry-point-result=void  \
4// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
5// RUN: FileCheck %s
6
7#DCSR  = #sparse_tensor.encoding<{
8  dimLevelType = [ "compressed", "compressed" ],
9  pointerBitWidth = 8,
10  indexBitWidth = 8
11}>
12
13#DCSC  = #sparse_tensor.encoding<{
14  dimLevelType = [ "compressed", "compressed" ],
15  dimOrdering = affine_map<(i,j) -> (j,i)>,
16  pointerBitWidth = 64,
17  indexBitWidth = 64
18}>
19
20#CSC  = #sparse_tensor.encoding<{
21  dimLevelType = [ "dense", "compressed" ],
22  dimOrdering = affine_map<(i,j) -> (j,i)>,
23  pointerBitWidth = 16,
24  indexBitWidth = 32
25}>
26
27//
28// Integration test that tests conversions between sparse tensors,
29// where the pointer and index sizes in the overhead storage change
30// in addition to layout.
31//
32module {
33
34  //
35  // Helper method to print values and indices arrays. The transfer actually
36  // reads more than required to verify size of buffer as well.
37  //
38  func.func @dumpf64(%arg0: memref<?xf64>) {
39    %c = arith.constant 0 : index
40    %d = arith.constant -1.0 : f64
41    %0 = vector.transfer_read %arg0[%c], %d: memref<?xf64>, vector<8xf64>
42    vector.print %0 : vector<8xf64>
43    return
44  }
45  func.func @dumpi08(%arg0: memref<?xi8>) {
46    %c = arith.constant 0 : index
47    %d = arith.constant -1 : i8
48    %0 = vector.transfer_read %arg0[%c], %d: memref<?xi8>, vector<8xi8>
49    vector.print %0 : vector<8xi8>
50    return
51  }
52  func.func @dumpi32(%arg0: memref<?xi32>) {
53    %c = arith.constant 0 : index
54    %d = arith.constant -1 : i32
55    %0 = vector.transfer_read %arg0[%c], %d: memref<?xi32>, vector<8xi32>
56    vector.print %0 : vector<8xi32>
57    return
58  }
59  func.func @dumpi64(%arg0: memref<?xi64>) {
60    %c = arith.constant 0 : index
61    %d = arith.constant -1 : i64
62    %0 = vector.transfer_read %arg0[%c], %d: memref<?xi64>, vector<8xi64>
63    vector.print %0 : vector<8xi64>
64    return
65  }
66
67  func.func @entry() {
68    %c1 = arith.constant 1 : index
69    %t1 = arith.constant sparse<
70      [ [0,0], [0,1], [0,63], [1,0], [1,1], [31,0], [31,63] ],
71        [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]> : tensor<32x64xf64>
72    %t2 = tensor.cast %t1 : tensor<32x64xf64> to tensor<?x?xf64>
73
74    // Dense to sparse.
75    %1 = sparse_tensor.convert %t1 : tensor<32x64xf64> to tensor<32x64xf64, #DCSR>
76    %2 = sparse_tensor.convert %t1 : tensor<32x64xf64> to tensor<32x64xf64, #DCSC>
77    %3 = sparse_tensor.convert %t1 : tensor<32x64xf64> to tensor<32x64xf64, #CSC>
78
79    // Sparse to sparse.
80    %4 = sparse_tensor.convert %1 : tensor<32x64xf64, #DCSR> to tensor<32x64xf64, #DCSC>
81    %5 = sparse_tensor.convert %2 : tensor<32x64xf64, #DCSC> to tensor<32x64xf64, #DCSR>
82    %6 = sparse_tensor.convert %3 : tensor<32x64xf64, #CSC>  to tensor<32x64xf64, #DCSR>
83
84    //
85    // All proper row-/column-wise?
86    //
87    // CHECK:      ( 1, 2, 3, 4, 5, 6, 7, -1 )
88    // CHECK-NEXT: ( 1, 4, 6, 2, 5, 3, 7, -1 )
89    // CHECK-NEXT: ( 1, 4, 6, 2, 5, 3, 7, -1 )
90    // CHECK-NEXT: ( 1, 4, 6, 2, 5, 3, 7, -1 )
91    // CHECK-NEXT: ( 1, 2, 3, 4, 5, 6, 7, -1 )
92    // CHECK-NEXT: ( 1, 2, 3, 4, 5, 6, 7, -1 )
93    //
94    %m1 = sparse_tensor.values %1 : tensor<32x64xf64, #DCSR> to memref<?xf64>
95    %m2 = sparse_tensor.values %2 : tensor<32x64xf64, #DCSC> to memref<?xf64>
96    %m3 = sparse_tensor.values %3 : tensor<32x64xf64, #CSC>  to memref<?xf64>
97    %m4 = sparse_tensor.values %4 : tensor<32x64xf64, #DCSC> to memref<?xf64>
98    %m5 = sparse_tensor.values %5 : tensor<32x64xf64, #DCSR> to memref<?xf64>
99    %m6 = sparse_tensor.values %6 : tensor<32x64xf64, #DCSR> to memref<?xf64>
100    call @dumpf64(%m1) : (memref<?xf64>) -> ()
101    call @dumpf64(%m2) : (memref<?xf64>) -> ()
102    call @dumpf64(%m3) : (memref<?xf64>) -> ()
103    call @dumpf64(%m4) : (memref<?xf64>) -> ()
104    call @dumpf64(%m5) : (memref<?xf64>) -> ()
105    call @dumpf64(%m6) : (memref<?xf64>) -> ()
106
107    //
108    // Sanity check on indices.
109    //
110    // CHECK-NEXT: ( 0, 1, 63, 0, 1, 0, 63, -1 )
111    // CHECK-NEXT: ( 0, 1, 31, 0, 1, 0, 31, -1 )
112    // CHECK-NEXT: ( 0, 1, 31, 0, 1, 0, 31, -1 )
113    // CHECK-NEXT: ( 0, 1, 31, 0, 1, 0, 31, -1 )
114    // CHECK-NEXT: ( 0, 1, 63, 0, 1, 0, 63, -1 )
115    // CHECK-NEXT: ( 0, 1, 63, 0, 1, 0, 63, -1 )
116    //
117    %i1 = sparse_tensor.indices %1, %c1 : tensor<32x64xf64, #DCSR> to memref<?xi8>
118    %i2 = sparse_tensor.indices %2, %c1 : tensor<32x64xf64, #DCSC> to memref<?xi64>
119    %i3 = sparse_tensor.indices %3, %c1 : tensor<32x64xf64, #CSC>  to memref<?xi32>
120    %i4 = sparse_tensor.indices %4, %c1 : tensor<32x64xf64, #DCSC> to memref<?xi64>
121    %i5 = sparse_tensor.indices %5, %c1 : tensor<32x64xf64, #DCSR> to memref<?xi8>
122    %i6 = sparse_tensor.indices %6, %c1 : tensor<32x64xf64, #DCSR> to memref<?xi8>
123    call @dumpi08(%i1) : (memref<?xi8>)  -> ()
124    call @dumpi64(%i2) : (memref<?xi64>) -> ()
125    call @dumpi32(%i3) : (memref<?xi32>) -> ()
126    call @dumpi64(%i4) : (memref<?xi64>) -> ()
127    call @dumpi08(%i5) : (memref<?xi08>) -> ()
128    call @dumpi08(%i6) : (memref<?xi08>) -> ()
129
130    // Release the resources.
131    bufferization.dealloc_tensor %1 : tensor<32x64xf64, #DCSR>
132    bufferization.dealloc_tensor %2 : tensor<32x64xf64, #DCSC>
133    bufferization.dealloc_tensor %3 : tensor<32x64xf64, #CSC>
134    bufferization.dealloc_tensor %4 : tensor<32x64xf64, #DCSC>
135    bufferization.dealloc_tensor %5 : tensor<32x64xf64, #DCSR>
136    bufferization.dealloc_tensor %6 : tensor<32x64xf64, #DCSR>
137
138    return
139  }
140}
141