1// RUN: mlir-opt %s -split-input-file -verify-diagnostics
2
3func.func @invalid_new_dense(%arg0: !llvm.ptr<i8>) -> tensor<32xf32> {
4  // expected-error@+1 {{expected a sparse tensor result}}
5  %0 = sparse_tensor.new %arg0 : !llvm.ptr<i8> to tensor<32xf32>
6  return %0 : tensor<32xf32>
7}
8
9// -----
10
11func.func @invalid_release_dense(%arg0: tensor<4xi32>) {
12  // expected-error@+1 {{expected a sparse tensor to release}}
13  sparse_tensor.release %arg0 : tensor<4xi32>
14  return
15}
16
17// -----
18
19func.func @invalid_pointers_dense(%arg0: tensor<128xf64>) -> memref<?xindex> {
20  %c = arith.constant 0 : index
21  // expected-error@+1 {{expected a sparse tensor to get pointers}}
22  %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf64> to memref<?xindex>
23  return %0 : memref<?xindex>
24}
25
26// -----
27
28func.func @invalid_pointers_unranked(%arg0: tensor<*xf64>) -> memref<?xindex> {
29  %c = arith.constant 0 : index
30  // expected-error@+1 {{expected a sparse tensor to get pointers}}
31  %0 = sparse_tensor.pointers %arg0, %c : tensor<*xf64> to memref<?xindex>
32  return %0 : memref<?xindex>
33}
34
35// -----
36
37#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"], pointerBitWidth=32}>
38
39func.func @mismatch_pointers_types(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> {
40  %c = arith.constant 0 : index
41  // expected-error@+1 {{unexpected type for pointers}}
42  %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf64, #SparseVector> to memref<?xindex>
43  return %0 : memref<?xindex>
44}
45
46// -----
47
48#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}>
49
50func.func @pointers_oob(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> {
51  %c = arith.constant 1 : index
52  // expected-error@+1 {{requested pointers dimension out of bounds}}
53  %0 = sparse_tensor.pointers %arg0, %c : tensor<128xf64, #SparseVector> to memref<?xindex>
54  return %0 : memref<?xindex>
55}
56
57// -----
58
59func.func @invalid_indices_dense(%arg0: tensor<10x10xi32>) -> memref<?xindex> {
60  %c = arith.constant 1 : index
61  // expected-error@+1 {{expected a sparse tensor to get indices}}
62  %0 = sparse_tensor.indices %arg0, %c : tensor<10x10xi32> to memref<?xindex>
63  return %0 : memref<?xindex>
64}
65
66// -----
67
68func.func @invalid_indices_unranked(%arg0: tensor<*xf64>) -> memref<?xindex> {
69  %c = arith.constant 0 : index
70  // expected-error@+1 {{expected a sparse tensor to get indices}}
71  %0 = sparse_tensor.indices %arg0, %c : tensor<*xf64> to memref<?xindex>
72  return %0 : memref<?xindex>
73}
74
75// -----
76
77#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}>
78
79func.func @mismatch_indices_types(%arg0: tensor<?xf64, #SparseVector>) -> memref<?xi32> {
80  %c = arith.constant 0 : index
81  // expected-error@+1 {{unexpected type for indices}}
82  %0 = sparse_tensor.indices %arg0, %c : tensor<?xf64, #SparseVector> to memref<?xi32>
83  return %0 : memref<?xi32>
84}
85
86// -----
87
88#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}>
89
90func.func @indices_oob(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xindex> {
91  %c = arith.constant 1 : index
92  // expected-error@+1 {{requested indices dimension out of bounds}}
93  %0 = sparse_tensor.indices %arg0, %c : tensor<128xf64, #SparseVector> to memref<?xindex>
94  return %0 : memref<?xindex>
95}
96
97// -----
98
99func.func @invalid_values_dense(%arg0: tensor<1024xf32>) -> memref<?xf32> {
100  // expected-error@+1 {{expected a sparse tensor to get values}}
101  %0 = sparse_tensor.values %arg0 : tensor<1024xf32> to memref<?xf32>
102  return %0 : memref<?xf32>
103}
104
105// -----
106
107#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}>
108
109func.func @mismatch_values_types(%arg0: tensor<?xf64, #SparseVector>) -> memref<?xf32> {
110  // expected-error@+1 {{unexpected mismatch in element types}}
111  %0 = sparse_tensor.values %arg0 : tensor<?xf64, #SparseVector> to memref<?xf32>
112  return %0 : memref<?xf32>
113}
114
115// -----
116
117func.func @sparse_unannotated_load(%arg0: tensor<16x32xf64>) -> tensor<16x32xf64> {
118  // expected-error@+1 {{expected a sparse tensor to materialize}}
119  %0 = sparse_tensor.load %arg0 : tensor<16x32xf64>
120  return %0 : tensor<16x32xf64>
121}
122
123// -----
124
125func.func @sparse_unannotated_insert(%arg0: tensor<128xf64>, %arg1: memref<?xindex>, %arg2: f64) {
126  // expected-error@+1 {{expected a sparse tensor for insertion}}
127  sparse_tensor.lex_insert %arg0, %arg1, %arg2 : tensor<128xf64>, memref<?xindex>, f64
128  return
129}
130
131// -----
132
133func.func @sparse_unannotated_expansion(%arg0: tensor<128xf64>) {
134  // expected-error@+1 {{expected a sparse tensor for expansion}}
135  %values, %filled, %added, %count = sparse_tensor.expand %arg0
136    : tensor<128xf64> to memref<?xf64>, memref<?xi1>, memref<?xindex>, index
137  return
138}
139
140// -----
141
142func.func @sparse_unannotated_compression(%arg0: tensor<128xf64>, %arg1: memref<?xindex>,
143                                     %arg2: memref<?xf64>, %arg3: memref<?xi1>,
144				     %arg4: memref<?xindex>, %arg5: index) {
145  // expected-error@+1 {{expected a sparse tensor for compression}}
146  sparse_tensor.compress %arg0, %arg1, %arg2, %arg3, %arg4, %arg5
147    : tensor<128xf64>, memref<?xindex>, memref<?xf64>, memref<?xi1>, memref<?xindex>, index
148}
149
150// -----
151
152func.func @sparse_convert_unranked(%arg0: tensor<*xf32>) -> tensor<10xf32> {
153  // expected-error@+1 {{unexpected type in convert}}
154  %0 = sparse_tensor.convert %arg0 : tensor<*xf32> to tensor<10xf32>
155  return %0 : tensor<10xf32>
156}
157
158// -----
159
160#DCSR = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed"]}>
161
162func.func @sparse_convert_rank_mismatch(%arg0: tensor<10x10xf64, #DCSR>) -> tensor<?xf64> {
163  // expected-error@+1 {{unexpected conversion mismatch in rank}}
164  %0 = sparse_tensor.convert %arg0 : tensor<10x10xf64, #DCSR> to tensor<?xf64>
165  return %0 : tensor<?xf64>
166}
167
168// -----
169
170#CSR = #sparse_tensor.encoding<{dimLevelType = ["dense", "compressed"]}>
171
172func.func @sparse_convert_dim_mismatch(%arg0: tensor<10x?xf32>) -> tensor<10x10xf32, #CSR> {
173  // expected-error@+1 {{unexpected conversion mismatch in dimension 1}}
174  %0 = sparse_tensor.convert %arg0 : tensor<10x?xf32> to tensor<10x10xf32, #CSR>
175  return %0 : tensor<10x10xf32, #CSR>
176}
177
178// -----
179
180func.func @invalid_out_dense(%arg0: tensor<10xf64>, %arg1: !llvm.ptr<i8>) {
181  // expected-error@+1 {{expected a sparse tensor for output}}
182  sparse_tensor.out %arg0, %arg1 : tensor<10xf64>, !llvm.ptr<i8>
183  return
184}
185
186// -----
187
188func.func @invalid_binary_num_args_mismatch_overlap(%arg0: f64, %arg1: f64) -> f64 {
189  // expected-error@+1 {{overlap region must have exactly 2 arguments}}
190  %r = sparse_tensor.binary %arg0, %arg1 : f64, f64 to f64
191    overlap={
192      ^bb0(%x: f64):
193        sparse_tensor.yield %x : f64
194    }
195    left={}
196    right={}
197  return %r : f64
198}
199
200// -----
201
202func.func @invalid_binary_num_args_mismatch_right(%arg0: f64, %arg1: f64) -> f64 {
203  // expected-error@+1 {{right region must have exactly 1 arguments}}
204  %r = sparse_tensor.binary %arg0, %arg1 : f64, f64 to f64
205    overlap={}
206    left={}
207    right={
208      ^bb0(%x: f64, %y: f64):
209        sparse_tensor.yield %y : f64
210    }
211  return %r : f64
212}
213
214// -----
215
216func.func @invalid_binary_argtype_mismatch(%arg0: f64, %arg1: f64) -> f64 {
217  // expected-error@+1 {{overlap region argument 2 type mismatch}}
218  %r = sparse_tensor.binary %arg0, %arg1 : f64, f64 to f64
219    overlap={
220      ^bb0(%x: f64, %y: f32):
221        sparse_tensor.yield %x : f64
222    }
223    left=identity
224    right=identity
225  return %r : f64
226}
227
228// -----
229
230func.func @invalid_binary_wrong_return_type(%arg0: f64, %arg1: f64) -> f64 {
231  // expected-error@+1 {{left region yield type mismatch}}
232  %0 = sparse_tensor.binary %arg0, %arg1 : f64, f64 to f64
233    overlap={}
234    left={
235      ^bb0(%x: f64):
236        %1 = arith.constant 0.0 : f32
237        sparse_tensor.yield %1 : f32
238    }
239    right=identity
240  return %0 : f64
241}
242
243// -----
244
245func.func @invalid_binary_wrong_identity_type(%arg0: i64, %arg1: f64) -> f64 {
246  // expected-error@+1 {{left=identity requires first argument to have the same type as the output}}
247  %0 = sparse_tensor.binary %arg0, %arg1 : i64, f64 to f64
248    overlap={}
249    left=identity
250    right=identity
251  return %0 : f64
252}
253
254// -----
255
256func.func @invalid_unary_argtype_mismatch(%arg0: f64) -> f64 {
257  // expected-error@+1 {{present region argument 1 type mismatch}}
258  %r = sparse_tensor.unary %arg0 : f64 to f64
259    present={
260      ^bb0(%x: index):
261        sparse_tensor.yield %x : index
262    }
263    absent={}
264  return %r : f64
265}
266
267// -----
268
269func.func @invalid_unary_num_args_mismatch(%arg0: f64) -> f64 {
270  // expected-error@+1 {{absent region must have exactly 0 arguments}}
271  %r = sparse_tensor.unary %arg0 : f64 to f64
272    present={}
273    absent={
274      ^bb0(%x: f64):
275        sparse_tensor.yield %x : f64
276    }
277  return %r : f64
278}
279
280// -----
281
282func.func @invalid_unary_wrong_return_type(%arg0: f64) -> f64 {
283  // expected-error@+1 {{present region yield type mismatch}}
284  %0 = sparse_tensor.unary %arg0 : f64 to f64
285    present={
286      ^bb0(%x: f64):
287        %1 = arith.constant 0.0 : f32
288        sparse_tensor.yield %1 : f32
289    }
290    absent={}
291  return %0 : f64
292}
293