xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/block_majors.mlir (revision eb206e9ea84eff0a0596fed2de8316d924f946d1)
1//--------------------------------------------------------------------------------------------------
2// WHEN CREATING A NEW TEST, PLEASE JUST COPY & PASTE WITHOUT EDITS.
3//
4// Set-up that's shared across all tests in this directory. In principle, this
5// config could be moved to lit.local.cfg. However, there are downstream users that
6// do not use these LIT config files. Hence why this is kept inline.
7//
8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true
9// DEFINE: %{sparsifier_opts_sve} = enable-arm-sve=true %{sparsifier_opts}
10// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}"
11// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}"
12// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils
13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils
14// DEFINE: %{run_opts} = -e main -entry-point-result=void
15// DEFINE: %{run} = mlir-runner %{run_opts} %{run_libs}
16// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs_sve}
17//
18// DEFINE: %{env} =
19//--------------------------------------------------------------------------------------------------
20
21// RUN: %{compile} | %{run} | FileCheck %s
22//
23// Do the same run, but now with direct IR generation.
24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false
25// RUN: %{compile} | %{run} | FileCheck %s
26//
27// Do the same run, but now with direct IR generation and vectorization.
28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true vl=2 reassociate-fp-reductions=true enable-index-optimizations=true
29// RUN: %{compile} | %{run} | FileCheck %s
30
31#BSR_row_rowmajor = #sparse_tensor.encoding<{
32  map = (i, j) ->
33    ( i floordiv 3 : dense
34    , j floordiv 4 : compressed
35    , i mod 3 : dense
36    , j mod 4 : dense
37    )
38}>
39
40#BSR_row_colmajor = #sparse_tensor.encoding<{
41  map = (i, j) ->
42    ( i floordiv 3 : dense
43    , j floordiv 4 : compressed
44    , j mod 4 : dense
45    , i mod 3 : dense
46    )
47}>
48
49#BSR_col_rowmajor = #sparse_tensor.encoding<{
50  map = (i, j) ->
51    ( j floordiv 4 : dense
52    , i floordiv 3 : compressed
53    , i mod 3 : dense
54    , j mod 4 : dense
55    )
56}>
57
58#BSR_col_colmajor = #sparse_tensor.encoding<{
59  map = (i, j) ->
60    ( j floordiv 4 : dense
61    , i floordiv 3 : compressed
62    , j mod 4 : dense
63    , i mod 3 : dense
64    )
65}>
66
67//
68// Example 3x4 block storage of a 6x16 matrix:
69//
70//  +---------+---------+---------+---------+
71//  | 1 2 . . | . . . . | . . . . | . . . . |
72//  | . . . . | . . . . | . . . . | . . . . |
73//  | . . . 3 | . . . . | . . . . | . . . . |
74//  +---------+---------+---------+---------+
75//  | . . . . | . . . . | 4 5 . . | . . . . |
76//  | . . . . | . . . . | . . . . | . . . . |
77//  | . . . . | . . . . | . . 6 7 | . . . . |
78//  +---------+---------+---------+---------+
79//
80// Storage for CSR block storage. Note that this essentially
81// provides CSR storage of 2x4 blocks with either row-major
82// or column-major storage within each 3x4 block of elements.
83//
84//    positions[1]   : 0 1 2
85//    coordinates[1] : 0 2
86//    values         : 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
87//                     4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7 [row-major]
88//
89//                     1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3,
90//                     4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7 [col-major]
91//
92// Storage for CSC block storage. Note that this essentially
93// provides CSC storage of 4x2 blocks with either row-major
94// or column-major storage within each 3x4 block of elements.
95//
96//    positions[1]   : 0 1 1 2 2
97//    coordinates[1] : 0 1
98//    values         : 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
99//                     4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7 [row-major]
100//
101//                     1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3,
102//                     4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7 [col-major]
103//
104module {
105
106
107  //
108  // CHECK: ---- Sparse Tensor ----
109  // CHECK-NEXT: nse = 24
110  // CHECK-NEXT: dim = ( 6, 16 )
111  // CHECK-NEXT: lvl = ( 2, 4, 3, 4 )
112  // CHECK-NEXT: pos[1] : ( 0, 1, 2 )
113  // CHECK-NEXT: crd[1] : ( 0, 2 )
114  // CHECK-NEXT: values : ( 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7 )
115  // CHECK-NEXT: ----
116  //
117  func.func @foo1() {
118    // Build.
119    %c0 = arith.constant 0   : index
120    %f0 = arith.constant 0.0 : f64
121    %m = arith.constant sparse<
122        [ [0, 0], [0, 1], [2, 3], [3, 8], [3, 9], [5, 10], [5, 11] ],
123        [ 1., 2., 3., 4., 5., 6., 7.]
124    > : tensor<6x16xf64>
125    %s1 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_row_rowmajor>
126    // Test.
127    sparse_tensor.print %s1 : tensor<?x?xf64, #BSR_row_rowmajor>
128    // Release.
129    bufferization.dealloc_tensor %s1: tensor<?x?xf64, #BSR_row_rowmajor>
130    return
131  }
132
133  //
134  // CHECK-NEXT: ---- Sparse Tensor ----
135  // CHECK-NEXT: nse = 24
136  // CHECK-NEXT: dim = ( 6, 16 )
137  // CHECK-NEXT: lvl = ( 2, 4, 4, 3 )
138  // CHECK-NEXT: pos[1] : ( 0, 1, 2 )
139  // CHECK-NEXT: crd[1] : ( 0, 2 )
140  // CHECK-NEXT: values : ( 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7 )
141  // CHECK-NEXT: ----
142  //
143  func.func @foo2() {
144    // Build.
145    %c0 = arith.constant 0   : index
146    %f0 = arith.constant 0.0 : f64
147    %m = arith.constant sparse<
148        [ [0, 0], [0, 1], [2, 3], [3, 8], [3, 9], [5, 10], [5, 11] ],
149        [ 1., 2., 3., 4., 5., 6., 7.]
150    > : tensor<6x16xf64>
151    %s2 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_row_colmajor>
152    // Test.
153    sparse_tensor.print %s2 : tensor<?x?xf64, #BSR_row_colmajor>
154    // Release.
155    bufferization.dealloc_tensor %s2: tensor<?x?xf64, #BSR_row_colmajor>
156    return
157  }
158
159  //
160  // CHECK-NEXT: ---- Sparse Tensor ----
161  // CHECK-NEXT: nse = 24
162  // CHECK-NEXT: dim = ( 6, 16 )
163  // CHECK-NEXT: lvl = ( 4, 2, 3, 4 )
164  // CHECK-NEXT: pos[1] : ( 0, 1, 1, 2, 2 )
165  // CHECK-NEXT: crd[1] : ( 0, 1 )
166  // CHECK-NEXT: values : ( 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7 )
167  // CHECK-NEXT: ----
168  //
169  func.func @foo3() {
170    // Build.
171    %c0 = arith.constant 0   : index
172    %f0 = arith.constant 0.0 : f64
173    %m = arith.constant sparse<
174        [ [0, 0], [0, 1], [2, 3], [3, 8], [3, 9], [5, 10], [5, 11] ],
175        [ 1., 2., 3., 4., 5., 6., 7.]
176    > : tensor<6x16xf64>
177    %s3 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_col_rowmajor>
178    // Test.
179    sparse_tensor.print %s3 : tensor<?x?xf64, #BSR_col_rowmajor>
180    // Release.
181    bufferization.dealloc_tensor %s3: tensor<?x?xf64, #BSR_col_rowmajor>
182    return
183  }
184
185  //
186  // CHECK-NEXT: ---- Sparse Tensor ----
187  // CHECK-NEXT: nse = 24
188  // CHECK-NEXT: dim = ( 6, 16 )
189  // CHECK-NEXT: lvl = ( 4, 2, 4, 3 )
190  // CHECK-NEXT: pos[1] : ( 0, 1, 1, 2, 2 )
191  // CHECK-NEXT: crd[1] : ( 0, 1 )
192  // CHECK-NEXT: values : ( 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7 )
193  // CHECK-NEXT: ----
194  //
195  func.func @foo4() {
196    // Build.
197    %c0 = arith.constant 0   : index
198    %f0 = arith.constant 0.0 : f64
199    %m = arith.constant sparse<
200        [ [0, 0], [0, 1], [2, 3], [3, 8], [3, 9], [5, 10], [5, 11] ],
201        [ 1., 2., 3., 4., 5., 6., 7.]
202    > : tensor<6x16xf64>
203    %s4 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_col_colmajor>
204    // Test.
205    sparse_tensor.print %s4 : tensor<?x?xf64, #BSR_col_colmajor>
206    // Release.
207    bufferization.dealloc_tensor %s4: tensor<?x?xf64, #BSR_col_colmajor>
208    return
209  }
210
211  func.func @main() {
212    call @foo1() : () -> ()
213    call @foo2() : () -> ()
214    call @foo3() : () -> ()
215    call @foo4() : () -> ()
216    return
217  }
218}
219