xref: /llvm-project/mlir/test/Dialect/SparseTensor/conversion.mlir (revision 4bce270157f9a81bd7e38dc589a2970a445d1e96)
1// RUN: mlir-opt %s --lower-sparse-ops-to-foreach --lower-sparse-foreach-to-scf --sparse-tensor-conversion --canonicalize --cse | FileCheck %s
2
3#SparseVector = #sparse_tensor.encoding<{
4  map = (d0) -> (d0 : compressed)
5}>
6
7#SparseVector64 = #sparse_tensor.encoding<{
8  map = (d0) -> (d0 : compressed),
9  posWidth = 64,
10  crdWidth = 64
11}>
12
13#SparseVector32 = #sparse_tensor.encoding<{
14  map = (d0) -> (d0 : compressed),
15  posWidth = 32,
16  crdWidth = 32
17}>
18
19#CSR = #sparse_tensor.encoding<{
20  map = (d0, d1) -> (d0 : dense, d1 : compressed)
21}>
22
23#CSC = #sparse_tensor.encoding<{
24  map = (d0, d1) -> (d1 : dense, d0 : compressed)
25}>
26
27#SparseTensor = #sparse_tensor.encoding<{
28  map = (d0, d1, d2) -> (d2 : dense, d0 : compressed, d1 : compressed)
29}>
30
31// CHECK-LABEL: func @sparse_nop(
32//  CHECK-SAME: %[[A:.*]]: !llvm.ptr) -> !llvm.ptr
33//       CHECK: return %[[A]] : !llvm.ptr
34func.func @sparse_nop(%arg0: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #SparseVector> {
35  return %arg0 : tensor<?xf64, #SparseVector>
36}
37
38// CHECK-LABEL: func @sparse_dim1d(
39//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
40//       CHECK: %[[C:.*]] = arith.constant 0 : index
41//       CHECK: %[[D:.*]] = call @sparseLvlSize(%[[A]], %[[C]])
42//       CHECK: return %[[D]] : index
43func.func @sparse_dim1d(%arg0: tensor<?xf64, #SparseVector>) -> index {
44  %c = arith.constant 0 : index
45  %0 = tensor.dim %arg0, %c : tensor<?xf64, #SparseVector>
46  return %0 : index
47}
48
49// Querying the size of dimension 1 should do so; i.e., it should
50// not be permuted into a query for the size of level 2 (even though
51// dimension 1 is stored as level 2).
52// CHECK-LABEL: func @sparse_dim3d(
53//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
54//       CHECK: %[[C:.*]] = arith.constant 2 : index
55//       CHECK: %[[D:.*]] = call @sparseLvlSize(%[[A]], %[[C]])
56//       CHECK: return %[[D]] : index
57func.func @sparse_dim3d(%arg0: tensor<?x?x?xf64, #SparseTensor>) -> index {
58  %c = arith.constant 1 : index
59  %0 = tensor.dim %arg0, %c : tensor<?x?x?xf64, #SparseTensor>
60  return %0 : index
61}
62
63// Querying the size of a static dimension should be folded into a
64// constant (and we should be sure to get the size of dimension 1,
65// not dimension 2 nor level 1).
66// CHECK-LABEL: func @sparse_dim3d_const(
67//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
68//       CHECK: %[[C:.*]] = arith.constant 20 : index
69//       CHECK: return %[[C]] : index
70func.func @sparse_dim3d_const(%arg0: tensor<10x20x30xf64, #SparseTensor>) -> index {
71  %c = arith.constant 1 : index
72  %0 = tensor.dim %arg0, %c : tensor<10x20x30xf64, #SparseTensor>
73  return %0 : index
74}
75
76// CHECK-LABEL: func @sparse_new1d(
77//  CHECK-SAME: %[[A:.*]]: !llvm.ptr) -> !llvm.ptr
78//   CHECK-DAG: %[[DimShape0:.*]] = memref.alloca() : memref<1xindex>
79//   CHECK-DAG: %[[DimShape:.*]] = memref.cast %[[DimShape0]] : memref<1xindex> to memref<?xindex>
80//       CHECK: %[[Reader:.*]] = call @createCheckedSparseTensorReader(%[[A]], %[[DimShape]], %{{.*}})
81//   CHECK-DAG: %[[LvlTypes0:.*]] = memref.alloca() : memref<1xi64>
82//   CHECK-DAG: %[[LvlTypes:.*]] = memref.cast %[[LvlTypes0]] : memref<1xi64> to memref<?xi64>
83//   CHECK-DAG: %[[Iota0:.*]] = memref.alloca() : memref<1xindex>
84//   CHECK-DAG: %[[Iota:.*]] = memref.cast %[[Iota0]] : memref<1xindex> to memref<?xindex>
85//       CHECK: %[[T:.*]] = call @newSparseTensor(%[[DimShape]], %[[DimShape]], %[[LvlTypes]], %[[Iota]], %[[Iota]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[Reader]])
86//       CHECK: call @delSparseTensorReader(%[[Reader]])
87//       CHECK: return %[[T]] : !llvm.ptr
88func.func @sparse_new1d(%arg0: !llvm.ptr) -> tensor<128xf64, #SparseVector> {
89  %0 = sparse_tensor.new %arg0 : !llvm.ptr to tensor<128xf64, #SparseVector>
90  return %0 : tensor<128xf64, #SparseVector>
91}
92
93// CHECK-LABEL: func @sparse_new2d(
94//  CHECK-SAME: %[[A:.*]]: !llvm.ptr) -> !llvm.ptr
95//   CHECK-DAG: %[[DimShape0:.*]] = memref.alloca() : memref<2xindex>
96//   CHECK-DAG: %[[DimShape:.*]] = memref.cast %[[DimShape0]] : memref<2xindex> to memref<?xindex>
97//       CHECK: %[[Reader:.*]] = call @createCheckedSparseTensorReader(%[[A]], %[[DimShape]], %{{.*}})
98//       CHECK: %[[DimSizes:.*]] = call @getSparseTensorReaderDimSizes(%[[Reader]])
99//   CHECK-DAG: %[[LvlTypes0:.*]] = memref.alloca() : memref<2xi64>
100//   CHECK-DAG: %[[LvlTypes:.*]] = memref.cast %[[LvlTypes0]] : memref<2xi64> to memref<?xi64>
101//   CHECK-DAG: %[[Iota0:.*]] = memref.alloca() : memref<2xindex>
102//   CHECK-DAG: %[[Iota:.*]] = memref.cast %[[Iota0]] : memref<2xindex> to memref<?xindex>
103//       CHECK: %[[T:.*]] = call @newSparseTensor(%[[DimSizes]], %[[DimSizes]], %[[LvlTypes]], %[[Iota]], %[[Iota]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[Reader]])
104//       CHECK: call @delSparseTensorReader(%[[Reader]])
105//       CHECK: return %[[T]] : !llvm.ptr
106func.func @sparse_new2d(%arg0: !llvm.ptr) -> tensor<?x?xf32, #CSR> {
107  %0 = sparse_tensor.new %arg0 : !llvm.ptr to tensor<?x?xf32, #CSR>
108  return %0 : tensor<?x?xf32, #CSR>
109}
110
111// CHECK-LABEL: func @sparse_new3d(
112//  CHECK-SAME: %[[A:.*]]: !llvm.ptr) -> !llvm.ptr
113//   CHECK-DAG: %[[DimShape0:.*]] = memref.alloca() : memref<3xindex>
114//   CHECK-DAG: %[[DimShape:.*]] = memref.cast %[[DimShape0]] : memref<3xindex> to memref<?xindex>
115//       CHECK: %[[Reader:.*]] = call @createCheckedSparseTensorReader(%[[A]], %[[DimShape]], %{{.*}})
116//       CHECK: %[[DimSizes:.*]] = call @getSparseTensorReaderDimSizes(%[[Reader]])
117//   CHECK-DAG: %[[LvlTypes0:.*]] = memref.alloca() : memref<3xi64>
118//   CHECK-DAG: %[[LvlTypes:.*]] = memref.cast %[[LvlTypes0]] : memref<3xi64> to memref<?xi64>
119//   CHECK-DAG: %[[Dim2Lvl0:.*]] = memref.alloca() : memref<3xindex>
120//   CHECK-DAG: %[[Dim2Lvl:.*]] = memref.cast %[[Dim2Lvl0]] : memref<3xindex> to memref<?xindex>
121//   CHECK-DAG: %[[Lvl2Dim0:.*]] = memref.alloca() : memref<3xindex>
122//   CHECK-DAG: %[[Lvl2Dim:.*]] = memref.cast %[[Lvl2Dim0]] : memref<3xindex> to memref<?xindex>
123//   CHECK-DAG: %[[LvlSizes0:.*]] = memref.alloca() : memref<3xindex>
124//   CHECK-DAG: %[[LvlSizes:.*]] = memref.cast %[[LvlSizes0]] : memref<3xindex> to memref<?xindex>
125//       CHECK: %[[T:.*]] = call @newSparseTensor(%[[DimSizes]], %[[LvlSizes]], %[[LvlTypes]], %[[Dim2Lvl]], %[[Lvl2Dim]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %[[Reader]])
126//       CHECK: call @delSparseTensorReader(%[[Reader]])
127//       CHECK: return %[[T]] : !llvm.ptr
128func.func @sparse_new3d(%arg0: !llvm.ptr) -> tensor<?x?x?xf32, #SparseTensor> {
129  %0 = sparse_tensor.new %arg0 : !llvm.ptr to tensor<?x?x?xf32, #SparseTensor>
130  return %0 : tensor<?x?x?xf32, #SparseTensor>
131}
132
133// CHECK-LABEL: func @sparse_init(
134//  CHECK-SAME: %[[I:.*]]: index,
135//  CHECK-SAME: %[[J:.*]]: index) -> !llvm.ptr
136//   CHECK-DAG: %[[Empty:.*]] = arith.constant 0 : i32
137//   CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
138//   CHECK-DAG: %[[C1:.*]] = arith.constant 1 : index
139//   CHECK-DAG: %[[LvlTypes0:.*]] = memref.alloca() : memref<2xi64>
140//   CHECK-DAG: %[[Sizes0:.*]] = memref.alloca() : memref<2xindex>
141//   CHECK-DAG: %[[Iota0:.*]] = memref.alloca() : memref<2xindex>
142//   CHECK-DAG: %[[LvlTypes:.*]] = memref.cast %[[LvlTypes0]] : memref<2xi64> to memref<?xi64>
143//   CHECK-DAG: %[[Sizes:.*]] = memref.cast %[[Sizes0]] : memref<2xindex> to memref<?xindex>
144//   CHECK-DAG: %[[Iota:.*]] = memref.cast %[[Iota0]] : memref<2xindex> to memref<?xindex>
145//   CHECK-DAG: memref.store %[[I]], %[[Sizes0]][%[[C0]]] : memref<2xindex>
146//   CHECK-DAG: memref.store %[[J]], %[[Sizes0]][%[[C1]]] : memref<2xindex>
147//   CHECK-DAG: %[[NP:.*]] = llvm.mlir.zero : !llvm.ptr
148//       CHECK: %[[T:.*]] = call @newSparseTensor(%[[Sizes]], %[[Sizes]], %[[LvlTypes]], %[[Iota]], %[[Iota]], %{{.*}}, %{{.*}}, %{{.*}}, %[[Empty]], %[[NP]])
149//       CHECK: return %[[T]] : !llvm.ptr
150func.func @sparse_init(%arg0: index, %arg1: index) -> tensor<?x?xf64, #CSR> {
151  %0 = tensor.empty(%arg0, %arg1) : tensor<?x?xf64, #CSR>
152  %1 = sparse_tensor.load %0 : tensor<?x?xf64, #CSR>
153  return %1 : tensor<?x?xf64, #CSR>
154}
155
156// CHECK-LABEL: func @sparse_release(
157//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
158//       CHECK: call @delSparseTensor(%[[A]]) : (!llvm.ptr) -> ()
159//       CHECK: return
160func.func @sparse_release(%arg0: tensor<128xf64, #SparseVector>) {
161  bufferization.dealloc_tensor %arg0 : tensor<128xf64, #SparseVector>
162  return
163}
164
165// CHECK-LABEL: func @sparse_nop_cast(
166//  CHECK-SAME: %[[A:.*]]: !llvm.ptr) -> !llvm.ptr
167//       CHECK: return %[[A]] : !llvm.ptr
168func.func @sparse_nop_cast(%arg0: tensor<64xf32, #SparseVector>) -> tensor<?xf32, #SparseVector> {
169  %0 = tensor.cast %arg0 : tensor<64xf32, #SparseVector> to tensor<?xf32, #SparseVector>
170  return %0 : tensor<?xf32, #SparseVector>
171}
172
173// CHECK-LABEL: func @sparse_positions(
174//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
175//       CHECK: %[[C:.*]] = arith.constant 0 : index
176//       CHECK: %[[T:.*]] = call @sparsePositions0(%[[A]], %[[C]]) : (!llvm.ptr, index) -> memref<?xindex>
177//       CHECK: return %[[T]] : memref<?xindex>
178func.func @sparse_positions(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> {
179  %0 = sparse_tensor.positions %arg0 { level = 0 : index } : tensor<128xf64, #SparseVector> to memref<?xindex>
180  return %0 : memref<?xindex>
181}
182
183// CHECK-LABEL: func @sparse_positions64(
184//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
185//       CHECK: %[[C:.*]] = arith.constant 0 : index
186//       CHECK: %[[T:.*]] = call @sparsePositions64(%[[A]], %[[C]]) : (!llvm.ptr, index) -> memref<?xi64>
187//       CHECK: return %[[T]] : memref<?xi64>
188func.func @sparse_positions64(%arg0: tensor<128xf64, #SparseVector64>) -> memref<?xi64> {
189  %0 = sparse_tensor.positions %arg0 { level = 0 : index } : tensor<128xf64, #SparseVector64> to memref<?xi64>
190  return %0 : memref<?xi64>
191}
192
193// CHECK-LABEL: func @sparse_positions32(
194//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
195//       CHECK: %[[C:.*]] = arith.constant 0 : index
196//       CHECK: %[[T:.*]] = call @sparsePositions32(%[[A]], %[[C]]) : (!llvm.ptr, index) -> memref<?xi32>
197//       CHECK: return %[[T]] : memref<?xi32>
198func.func @sparse_positions32(%arg0: tensor<128xf64, #SparseVector32>) -> memref<?xi32> {
199  %0 = sparse_tensor.positions %arg0 { level = 0 : index } : tensor<128xf64, #SparseVector32> to memref<?xi32>
200  return %0 : memref<?xi32>
201}
202
203// CHECK-LABEL: func @sparse_indices(
204//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
205//       CHECK: %[[C:.*]] = arith.constant 0 : index
206//       CHECK: %[[T:.*]] = call @sparseCoordinates0(%[[A]], %[[C]]) : (!llvm.ptr, index) -> memref<?xindex>
207//       CHECK: return %[[T]] : memref<?xindex>
208func.func @sparse_indices(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> {
209  %0 = sparse_tensor.coordinates %arg0 { level = 0 : index } : tensor<128xf64, #SparseVector> to memref<?xindex>
210  return %0 : memref<?xindex>
211}
212
213// CHECK-LABEL: func @sparse_indices64(
214//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
215//       CHECK: %[[C:.*]] = arith.constant 0 : index
216//       CHECK: %[[T:.*]] = call @sparseCoordinates64(%[[A]], %[[C]]) : (!llvm.ptr, index) -> memref<?xi64>
217//       CHECK: return %[[T]] : memref<?xi64>
218func.func @sparse_indices64(%arg0: tensor<128xf64, #SparseVector64>) -> memref<?xi64> {
219  %0 = sparse_tensor.coordinates %arg0 { level = 0 : index } : tensor<128xf64, #SparseVector64> to memref<?xi64>
220  return %0 : memref<?xi64>
221}
222
223// CHECK-LABEL: func @sparse_indices32(
224//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
225//       CHECK: %[[C:.*]] = arith.constant 0 : index
226//       CHECK: %[[T:.*]] = call @sparseCoordinates32(%[[A]], %[[C]]) : (!llvm.ptr, index) -> memref<?xi32>
227//       CHECK: return %[[T]] : memref<?xi32>
228func.func @sparse_indices32(%arg0: tensor<128xf64, #SparseVector32>) -> memref<?xi32> {
229  %0 = sparse_tensor.coordinates %arg0 { level = 0 : index } : tensor<128xf64, #SparseVector32> to memref<?xi32>
230  return %0 : memref<?xi32>
231}
232
233// CHECK-LABEL: func @sparse_valuesf64(
234//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
235//       CHECK: %[[T:.*]] = call @sparseValuesF64(%[[A]]) : (!llvm.ptr) -> memref<?xf64>
236//       CHECK: return %[[T]] : memref<?xf64>
237func.func @sparse_valuesf64(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xf64> {
238  %0 = sparse_tensor.values %arg0 : tensor<128xf64, #SparseVector> to memref<?xf64>
239  return %0 : memref<?xf64>
240}
241
242// CHECK-LABEL: func @sparse_valuesf32(
243//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
244//       CHECK: %[[T:.*]] = call @sparseValuesF32(%[[A]]) : (!llvm.ptr) -> memref<?xf32>
245//       CHECK: return %[[T]] : memref<?xf32>
246func.func @sparse_valuesf32(%arg0: tensor<128xf32, #SparseVector>) -> memref<?xf32> {
247  %0 = sparse_tensor.values %arg0: tensor<128xf32, #SparseVector> to memref<?xf32>
248  return %0 : memref<?xf32>
249}
250
251// CHECK-LABEL: func @sparse_valuesi32(
252//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
253//       CHECK: %[[T:.*]] = call @sparseValuesI32(%[[A]]) : (!llvm.ptr) -> memref<?xi32>
254//       CHECK: return %[[T]] : memref<?xi32>
255func.func @sparse_valuesi32(%arg0: tensor<128xi32, #SparseVector>) -> memref<?xi32> {
256  %0 = sparse_tensor.values %arg0: tensor<128xi32, #SparseVector> to memref<?xi32>
257  return %0 : memref<?xi32>
258}
259
260// CHECK-LABEL: func @sparse_valuesi16(
261//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
262//       CHECK: %[[T:.*]] = call @sparseValuesI16(%[[A]]) : (!llvm.ptr) -> memref<?xi16>
263//       CHECK: return %[[T]] : memref<?xi16>
264func.func @sparse_valuesi16(%arg0: tensor<128xi16, #SparseVector>) -> memref<?xi16> {
265  %0 = sparse_tensor.values %arg0: tensor<128xi16, #SparseVector> to memref<?xi16>
266  return %0 : memref<?xi16>
267}
268
269// CHECK-LABEL: func @sparse_valuesi8(
270//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
271//       CHECK: %[[T:.*]] = call @sparseValuesI8(%[[A]]) : (!llvm.ptr) -> memref<?xi8>
272//       CHECK: return %[[T]] : memref<?xi8>
273func.func @sparse_valuesi8(%arg0: tensor<128xi8, #SparseVector>) -> memref<?xi8> {
274  %0 = sparse_tensor.values %arg0: tensor<128xi8, #SparseVector> to memref<?xi8>
275  return %0 : memref<?xi8>
276}
277
278// CHECK-LABEL: func @sparse_noe(
279//  CHECK-SAME: %[[A:.*]]: !llvm.ptr)
280//   CHECK-DAG: %[[C:.*]] = arith.constant 0 : index
281//   CHECK-DAG: %[[T:.*]] = call @sparseValuesF64(%[[A]]) : (!llvm.ptr) -> memref<?xf64>
282//       CHECK: %[[NOE:.*]] = memref.dim %[[T]], %[[C]] : memref<?xf64>
283//       CHECK: return %[[NOE]] : index
284func.func @sparse_noe(%arg0: tensor<128xf64, #SparseVector>) -> index {
285  %0 = sparse_tensor.number_of_entries %arg0 : tensor<128xf64, #SparseVector>
286  return %0 : index
287}
288
289// CHECK-LABEL: func @sparse_reconstruct(
290//  CHECK-SAME: %[[A:.*]]: !llvm.ptr
291//       CHECK: return %[[A]] : !llvm.ptr
292func.func @sparse_reconstruct(%arg0: tensor<128xf32, #SparseVector>) -> tensor<128xf32, #SparseVector> {
293  %0 = sparse_tensor.load %arg0 : tensor<128xf32, #SparseVector>
294  return %0 : tensor<128xf32, #SparseVector>
295}
296
297// CHECK-LABEL: func @sparse_reconstruct_ins(
298//  CHECK-SAME: %[[A:.*]]: !llvm.ptr
299//       CHECK: call @endLexInsert(%[[A]]) : (!llvm.ptr) -> ()
300//       CHECK: return %[[A]] : !llvm.ptr
301func.func @sparse_reconstruct_ins(%arg0: tensor<128xf32, #SparseVector>) -> tensor<128xf32, #SparseVector> {
302  %0 = sparse_tensor.load %arg0 hasInserts : tensor<128xf32, #SparseVector>
303  return %0 : tensor<128xf32, #SparseVector>
304}
305
306// CHECK-LABEL: func @sparse_insert(
307//  CHECK-SAME: %[[A:.*]]: !llvm.ptr,
308//  CHECK-SAME: %[[B:.*]]: index,
309//  CHECK-SAME: %[[C:.*]]: f32) -> !llvm.ptr {
310//   CHECK-DAG: %[[M:.*]] = memref.alloca() : memref<1xindex>
311//   CHECK-DAG: %[[V:.*]] = memref.alloca() : memref<f32>
312//   CHECK-DAG: %[[MC:.*]] = memref.cast %[[M]] : memref<1xindex> to memref<?xindex>
313//   CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
314//   CHECK-DAG: memref.store %[[B]], %[[M]][%[[C0]]] : memref<1xindex>
315//   CHECK-DAG: memref.store %[[C]], %[[V]][] : memref<f32>
316//       CHECK: call @lexInsertF32(%[[A]], %[[MC]], %[[V]]) : (!llvm.ptr, memref<?xindex>, memref<f32>) -> ()
317//       CHECK: return %[[A]] : !llvm.ptr
318func.func @sparse_insert(%arg0: tensor<128xf32, #SparseVector>,
319                         %arg1: index,
320                         %arg2: f32) -> tensor<128xf32, #SparseVector> {
321  %0 = tensor.insert %arg2 into %arg0[%arg1] : tensor<128xf32, #SparseVector>
322  return %0 : tensor<128xf32, #SparseVector>
323}
324
325// CHECK-LABEL: func @sparse_expansion1()
326//       CHECK: %[[N:.*]] = call @newSparseTensor
327//       CHECK: %[[A:.*]] = memref.alloc() : memref<8xf64>
328//       CHECK: %[[B:.*]] = memref.alloc() : memref<8xi1>
329//       CHECK: %[[C:.*]] = memref.alloc() : memref<8xindex>
330//       CHECK: %[[D:.*]] = memref.cast %[[C]] : memref<8xindex> to memref<?xindex>
331//   CHECK-DAG: linalg.fill ins(%{{.*}} : f64) outs(%[[A]] : memref<8xf64>)
332//   CHECK-DAG: linalg.fill ins(%{{.*}} : i1) outs(%[[B]] : memref<8xi1>)
333//       CHECK: return %[[D]] : memref<?xindex>
334func.func @sparse_expansion1() -> memref<?xindex> {
335  %0 = tensor.empty() : tensor<4x8xf64, #CSR>
336  %values, %filled, %added, %count = sparse_tensor.expand %0
337    : tensor<4x8xf64, #CSR> to memref<?xf64>, memref<?xi1>, memref<?xindex>
338  return %added : memref<?xindex>
339}
340
341// CHECK-LABEL: func @sparse_expansion2()
342//       CHECK: %[[N:.*]] = call @newSparseTensor
343//       CHECK: %[[A:.*]] = memref.alloc() : memref<4xf64>
344//       CHECK: %[[B:.*]] = memref.alloc() : memref<4xi1>
345//       CHECK: %[[C:.*]] = memref.alloc() : memref<4xindex>
346//       CHECK: %[[D:.*]] = memref.cast %[[C]] : memref<4xindex> to memref<?xindex>
347//   CHECK-DAG: linalg.fill ins(%{{.*}} : f64) outs(%[[A]] : memref<4xf64>)
348//   CHECK-DAG: linalg.fill ins(%{{.*}} : i1) outs(%[[B]] : memref<4xi1>)
349//       CHECK: return %[[D]] : memref<?xindex>
350func.func @sparse_expansion2() -> memref<?xindex> {
351  %0 = tensor.empty() : tensor<4x8xf64, #CSC>
352  %values, %filled, %added, %count = sparse_tensor.expand %0
353    : tensor<4x8xf64, #CSC> to memref<?xf64>, memref<?xi1>, memref<?xindex>
354  return %added : memref<?xindex>
355}
356
357// CHECK-LABEL: func @sparse_expansion3(
358//       CHECK: %[[C1:.*]] = arith.constant 1 : index
359//       CHECK: %[[N:.*]] = call @newSparseTensor
360//       CHECK: %[[S:.*]] = call @sparseLvlSize(%[[N]], %[[C1]])
361//       CHECK: %[[A:.*]] = memref.alloc(%[[S]]) : memref<?xf64>
362//       CHECK: %[[B:.*]] = memref.alloc(%[[S]]) : memref<?xi1>
363//       CHECK: %[[C:.*]] = memref.alloc(%[[S]]) : memref<?xindex>
364//   CHECK-DAG: linalg.fill ins(%{{.*}} : f64) outs(%[[A]] : memref<?xf64>)
365//   CHECK-DAG: linalg.fill ins(%{{.*}} : i1) outs(%[[B]] : memref<?xi1>)
366//       CHECK: return %[[C]] : memref<?xindex>
367func.func @sparse_expansion3(%arg0: index, %arg1: index) -> memref<?xindex> {
368  %0 = tensor.empty(%arg0, %arg1) : tensor<?x?xf64, #CSC>
369  %values, %filled, %added, %count = sparse_tensor.expand %0
370    : tensor<?x?xf64, #CSC> to memref<?xf64>, memref<?xi1>, memref<?xindex>
371  return %added : memref<?xindex>
372}
373
374// CHECK-LABEL: func @sparse_compression(
375//  CHECK-SAME: %[[A:.*0]]: !llvm.ptr,
376//  CHECK-SAME: %[[B:.*1]]: memref<?xf64>,
377//  CHECK-SAME: %[[C:.*2]]: memref<?xi1>,
378//  CHECK-SAME: %[[D:.*3]]: memref<?xindex>,
379//  CHECK-SAME: %[[E:.*4]]: index,
380//  CHECK-SAME: %[[F:.*5]]: index) -> !llvm.ptr {
381//   CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
382//   CHECK-DAG: %[[X:.*]] = memref.alloca() : memref<2xindex>
383//   CHECK-DAG: %[[Y:.*]] = memref.cast %[[X]] : memref<2xindex> to memref<?xindex>
384//       CHECK: memref.store %[[F]], %[[X]][%[[C0]]] : memref<2xindex>
385//       CHECK: call @expInsertF64(%[[A]], %[[Y]], %[[B]], %[[C]], %[[D]], %[[E]])
386//   CHECK-DAG: memref.dealloc %[[B]] : memref<?xf64>
387//   CHECK-DAG: memref.dealloc %[[C]] : memref<?xi1>
388//   CHECK-DAG: memref.dealloc %[[D]] : memref<?xindex>
389//       CHECK: return %[[A]] : !llvm.ptr
390func.func @sparse_compression(%tensor: tensor<8x8xf64, #CSR>,
391                              %values: memref<?xf64>,
392                              %filled: memref<?xi1>,
393                              %added: memref<?xindex>,
394                              %count: index,
395                              %i: index) -> tensor<8x8xf64, #CSR> {
396  %0 = sparse_tensor.compress %values, %filled, %added, %count into %tensor[%i]
397    : memref<?xf64>, memref<?xi1>, memref<?xindex>, tensor<8x8xf64, #CSR>
398  return %0 : tensor<8x8xf64, #CSR>
399}
400
401// CHECK-LABEL: func @sparse_and_dense_init(
402//       CHECK: %[[S:.*]] = call @newSparseTensor
403//       CHECK: %[[D:.*]] = tensor.empty
404//       CHECK: return %[[S]], %[[D]] : !llvm.ptr, tensor<?x?xf64>
405func.func @sparse_and_dense_init(%arg0: index, %arg1: index)
406           -> (tensor<?x?xf64, #CSR>, tensor<?x?xf64>) {
407  %0 = tensor.empty(%arg0, %arg1) : tensor<?x?xf64, #CSR>
408  %1 = sparse_tensor.load %0 : tensor<?x?xf64, #CSR>
409  %2 = tensor.empty(%arg0, %arg1) : tensor<?x?xf64>
410  return %1, %2 : tensor<?x?xf64, #CSR>, tensor<?x?xf64>
411}
412