1// RUN: mlir-opt -allow-unregistered-dialect %s -split-input-file -verify-diagnostics
2
3// -----
4
5func.func @affine_apply_operand_non_index(%arg0 : i32) {
6  // Custom parser automatically assigns all arguments the `index` so we must
7  // use the generic syntax here to exercise the verifier.
8  // expected-error@+1 {{op operand #0 must be index, but got 'i32'}}
9  %0 = "affine.apply"(%arg0) {map = affine_map<(d0) -> (d0)>} : (i32) -> (index)
10  return
11}
12
13// -----
14
15func.func @affine_apply_resul_non_index(%arg0 : index) {
16  // Custom parser automatically assigns `index` as the result type so we must
17  // use the generic syntax here to exercise the verifier.
18  // expected-error@+1 {{op result #0 must be index, but got 'i32'}}
19  %0 = "affine.apply"(%arg0) {map = affine_map<(d0) -> (d0)>} : (index) -> (i32)
20  return
21}
22
23// -----
24
25#map = affine_map<(d0)[s0] -> (d0 + s0)>
26
27func.func @affine_for_lower_bound_invalid_dim(%arg : index) {
28  affine.for %n0 = 0 to 7 {
29    %dim = arith.addi %arg, %arg : index
30
31    // expected-error@+1 {{operand cannot be used as a dimension id}}
32    affine.for %n1 = 0 to #map(%dim)[%arg] {
33    }
34  }
35  return
36}
37
38// -----
39
40#map = affine_map<(d0)[s0] -> (d0 + s0)>
41
42func.func @affine_for_upper_bound_invalid_dim(%arg : index) {
43  affine.for %n0 = 0 to 7 {
44    %dim = arith.addi %arg, %arg : index
45
46    // expected-error@+1 {{operand cannot be used as a dimension id}}
47    affine.for %n1 = #map(%dim)[%arg] to 7 {
48    }
49  }
50  return
51}
52
53// -----
54func.func @affine_load_invalid_dim(%M : memref<10xi32>) {
55  "unknown"() ({
56  ^bb0(%arg: index):
57    affine.load %M[%arg] : memref<10xi32>
58    // expected-error@-1 {{index must be a dimension or symbol identifier}}
59    cf.br ^bb1
60  ^bb1:
61    cf.br ^bb1
62  }) : () -> ()
63  return
64}
65
66// -----
67
68#map0 = affine_map<(d0)[s0] -> (d0 + s0)>
69
70func.func @affine_for_lower_bound_invalid_sym() {
71  affine.for %i0 = 0 to 7 {
72    // expected-error@+1 {{operand cannot be used as a symbol}}
73    affine.for %n0 = #map0(%i0)[%i0] to 7 {
74    }
75  }
76  return
77}
78
79// -----
80
81#map0 = affine_map<(d0)[s0] -> (d0 + s0)>
82
83func.func @affine_for_upper_bound_invalid_sym() {
84  affine.for %i0 = 0 to 7 {
85    // expected-error@+1 {{operand cannot be used as a symbol}}
86    affine.for %n0 = 0 to #map0(%i0)[%i0] {
87    }
88  }
89  return
90}
91
92// -----
93
94#set0 = affine_set<(i)[N] : (i >= 0, N - i >= 0)>
95
96func.func @affine_if_invalid_dim(%arg : index) {
97  affine.for %n0 = 0 to 7 {
98    %dim = arith.addi %arg, %arg : index
99
100    // expected-error@+1 {{operand cannot be used as a dimension id}}
101    affine.if #set0(%dim)[%n0] {}
102  }
103  return
104}
105
106// -----
107
108#set0 = affine_set<(i)[N] : (i >= 0, N - i >= 0)>
109
110func.func @affine_if_invalid_sym() {
111  affine.for %i0 = 0 to 7 {
112    // expected-error@+1 {{operand cannot be used as a symbol}}
113    affine.if #set0(%i0)[%i0] {}
114  }
115  return
116}
117
118// -----
119
120#set0 = affine_set<(i)[N] : (i >= 0, N - i >= 0)>
121
122func.func @affine_if_invalid_dimop_dim(%arg0: index, %arg1: index, %arg2: index, %arg3: index) {
123  affine.for %n0 = 0 to 7 {
124    %0 = memref.alloc(%arg0, %arg1, %arg2, %arg3) : memref<?x?x?x?xf32>
125    %c0 = arith.constant 0 : index
126    %dim = memref.dim %0, %c0 : memref<?x?x?x?xf32>
127
128    // expected-error@+1 {{operand cannot be used as a symbol}}
129    affine.if #set0(%dim)[%n0] {}
130  }
131  return
132}
133
134// -----
135
136func.func @affine_store_missing_l_square(%C: memref<4096x4096xf32>) {
137  %9 = arith.constant 0.0 : f32
138  // expected-error@+1 {{expected '['}}
139  affine.store %9, %C : memref<4096x4096xf32>
140  return
141}
142
143// -----
144
145func.func @affine_store_wrong_value_type(%C: memref<f32>) {
146  %c0 = arith.constant 0 : i32
147  // expected-error@+1 {{value to store must have the same type as memref element type}}
148  "affine.store"(%c0, %C) : (i32, memref<f32>) -> ()
149  return
150}
151
152// -----
153
154func.func @affine_min(%arg0 : index, %arg1 : index, %arg2 : index) {
155  // expected-error@+1 {{operand count and affine map dimension and symbol count must match}}
156  %0 = affine.min affine_map<(d0) -> (d0)> (%arg0, %arg1)
157
158  return
159}
160
161// -----
162
163func.func @affine_min(%arg0 : index, %arg1 : index, %arg2 : index) {
164  // expected-error@+1 {{operand count and affine map dimension and symbol count must match}}
165  %0 = affine.min affine_map<()[s0] -> (s0)> (%arg0, %arg1)
166
167  return
168}
169
170// -----
171
172func.func @affine_min(%arg0 : index, %arg1 : index, %arg2 : index) {
173  // expected-error@+1 {{operand count and affine map dimension and symbol count must match}}
174  %0 = affine.min affine_map<(d0) -> (d0)> ()
175
176  return
177}
178
179// -----
180
181func.func @affine_max(%arg0 : index, %arg1 : index, %arg2 : index) {
182  // expected-error@+1 {{operand count and affine map dimension and symbol count must match}}
183  %0 = affine.max affine_map<(d0) -> (d0)> (%arg0, %arg1)
184
185  return
186}
187
188// -----
189
190func.func @affine_max(%arg0 : index, %arg1 : index, %arg2 : index) {
191  // expected-error@+1 {{operand count and affine map dimension and symbol count must match}}
192  %0 = affine.max affine_map<()[s0] -> (s0)> (%arg0, %arg1)
193
194  return
195}
196
197// -----
198
199func.func @affine_max(%arg0 : index, %arg1 : index, %arg2 : index) {
200  // expected-error@+1 {{operand count and affine map dimension and symbol count must match}}
201  %0 = affine.max affine_map<(d0) -> (d0)> ()
202
203  return
204}
205
206// -----
207
208func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
209  // expected-error@+1 {{the number of region arguments (1) and the number of map groups for lower (2) and upper bound (2), and the number of steps (2) must all match}}
210  affine.parallel (%i) = (0, 0) to (100, 100) step (10, 10) {
211  }
212}
213
214// -----
215
216func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
217  // expected-error@+1 {{the number of region arguments (2) and the number of map groups for lower (1) and upper bound (2), and the number of steps (2) must all match}}
218  affine.parallel (%i, %j) = (0) to (100, 100) step (10, 10) {
219  }
220}
221
222// -----
223
224func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
225  // expected-error@+1 {{the number of region arguments (2) and the number of map groups for lower (2) and upper bound (1), and the number of steps (2) must all match}}
226  affine.parallel (%i, %j) = (0, 0) to (100) step (10, 10) {
227  }
228}
229
230// -----
231
232func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
233  // expected-error@+1 {{the number of region arguments (2) and the number of map groups for lower (2) and upper bound (2), and the number of steps (1) must all match}}
234  affine.parallel (%i, %j) = (0, 0) to (100, 100) step (10) {
235  }
236}
237
238// -----
239
240func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
241  affine.for %x = 0 to 7 {
242    %y = arith.addi %x, %x : index
243    // expected-error@+1 {{operand cannot be used as a dimension id}}
244    affine.parallel (%i, %j) = (0, 0) to (%y, 100) step (10, 10) {
245    }
246  }
247  return
248}
249
250// -----
251
252func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
253  affine.for %x = 0 to 7 {
254    %y = arith.addi %x, %x : index
255    // expected-error@+1 {{operand cannot be used as a symbol}}
256    affine.parallel (%i, %j) = (0, 0) to (symbol(%y), 100) step (10, 10) {
257    }
258  }
259  return
260}
261
262// -----
263
264func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
265  %0 = memref.alloc() : memref<100x100xf32>
266  //  expected-error@+1 {{reduction must be specified for each output}}
267  %1 = affine.parallel (%i, %j) = (0, 0) to (100, 100) step (10, 10) -> (f32) {
268    %2 = affine.load %0[%i, %j] : memref<100x100xf32>
269    affine.yield %2 : f32
270  }
271  return
272}
273
274// -----
275
276func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
277  %0 = memref.alloc() : memref<100x100xf32>
278  //  expected-error@+1 {{invalid reduction value: "bad"}}
279  %1 = affine.parallel (%i, %j) = (0, 0) to (100, 100) step (10, 10) reduce ("bad") -> (f32) {
280    %2 = affine.load %0[%i, %j] : memref<100x100xf32>
281    affine.yield %2 : f32
282  }
283  return
284}
285
286// -----
287
288func.func @affine_parallel(%arg0 : index, %arg1 : index, %arg2 : index) {
289  %0 = memref.alloc() : memref<100x100xi32>
290  %1 = affine.parallel (%i, %j) = (0, 0) to (100, 100) step (10, 10) reduce ("minf") -> (f32) {
291    %2 = affine.load %0[%i, %j] : memref<100x100xi32>
292    //  expected-error@+1 {{types mismatch between yield op and its parent}}
293    affine.yield %2 : i32
294  }
295  return
296}
297
298// -----
299
300func.func @vector_load_invalid_vector_type() {
301  %0 = memref.alloc() : memref<100xf32>
302  affine.for %i0 = 0 to 16 step 8 {
303    // expected-error@+1 {{requires memref and vector types of the same elemental type}}
304    %1 = affine.vector_load %0[%i0] : memref<100xf32>, vector<8xf64>
305  }
306  return
307}
308
309// -----
310
311func.func @vector_store_invalid_vector_type() {
312  %0 = memref.alloc() : memref<100xf32>
313  %1 = arith.constant dense<7.0> : vector<8xf64>
314  affine.for %i0 = 0 to 16 step 8 {
315    // expected-error@+1 {{requires memref and vector types of the same elemental type}}
316    affine.vector_store %1, %0[%i0] : memref<100xf32>, vector<8xf64>
317  }
318  return
319}
320
321// -----
322
323func.func @vector_load_vector_memref() {
324  %0 = memref.alloc() : memref<100xvector<8xf32>>
325  affine.for %i0 = 0 to 4 {
326    // expected-error@+1 {{requires memref and vector types of the same elemental type}}
327    %1 = affine.vector_load %0[%i0] : memref<100xvector<8xf32>>, vector<8xf32>
328  }
329  return
330}
331
332// -----
333
334func.func @vector_store_vector_memref() {
335  %0 = memref.alloc() : memref<100xvector<8xf32>>
336  %1 = arith.constant dense<7.0> : vector<8xf32>
337  affine.for %i0 = 0 to 4 {
338    // expected-error@+1 {{requires memref and vector types of the same elemental type}}
339    affine.vector_store %1, %0[%i0] : memref<100xvector<8xf32>>, vector<8xf32>
340  }
341  return
342}
343
344// -----
345
346func.func @affine_if_with_then_region_args(%N: index) {
347  %c = arith.constant 200 : index
348  %i = arith.constant 20: index
349  // expected-error@+1 {{affine.if' op region #0 should have no arguments}}
350  affine.if affine_set<(i)[N] : (i - 2 >= 0, 4 - i >= 0)>(%i)[%c]  {
351    ^bb0(%arg:i32):
352      %w = affine.apply affine_map<(d0,d1)[s0] -> (d0+d1+s0)> (%i, %i) [%N]
353  }
354  return
355}
356
357// -----
358
359func.func @affine_if_with_else_region_args(%N: index) {
360  %c = arith.constant 200 : index
361  %i = arith.constant 20: index
362  // expected-error@+1 {{affine.if' op region #1 should have no arguments}}
363  affine.if affine_set<(i)[N] : (i - 2 >= 0, 4 - i >= 0)>(%i)[%c]  {
364      %w = affine.apply affine_map<(d0,d1)[s0] -> (d0+d1+s0)> (%i, %i) [%N]
365  } else {
366    ^bb0(%arg:i32):
367      %w = affine.apply affine_map<(d0,d1)[s0] -> (d0-d1+s0)> (%i, %i) [%N]
368  }
369  return
370}
371
372// -----
373
374func.func @affine_for_iter_args_mismatch(%buffer: memref<1024xf32>) -> f32 {
375  %sum_0 = arith.constant 0.0 : f32
376  // expected-error@+1 {{mismatch between the number of loop-carried values and results}}
377  %res = affine.for %i = 0 to 10 step 2 iter_args(%sum_iter = %sum_0) -> (f32, f32) {
378    %t = affine.load %buffer[%i] : memref<1024xf32>
379    affine.yield %t : f32
380  }
381  return %res : f32
382}
383
384
385// -----
386
387func.func @result_number() {
388  // expected-error@+1 {{result number not allowed}}
389  affine.for %n0#0 = 0 to 7 {
390  }
391  return
392}
393
394// -----
395
396func.func @malformed_for_percent() {
397  affine.for i = 1 to 10 { // expected-error {{expected SSA operand}}
398
399// -----
400
401func.func @malformed_for_equal() {
402  affine.for %i 1 to 10 { // expected-error {{expected '='}}
403
404// -----
405
406func.func @malformed_for_to() {
407  affine.for %i = 1 too 10 { // expected-error {{expected 'to' between bounds}}
408  }
409}
410
411// -----
412
413func.func @incomplete_for() {
414  affine.for %i = 1 to 10 step 2
415}        // expected-error @-1 {{expected '{' to begin a region}}
416
417// -----
418
419#map0 = affine_map<(d0) -> (d0 floordiv 4)>
420
421func.func @reference_to_iv_in_bound() {
422  // expected-error@+2 {{region entry argument '%i0' is already in use}}
423  // expected-note@+1 {{previously referenced here}}
424  affine.for %i0 = #map0(%i0) to 10 {
425  }
426}
427
428// -----
429
430func.func @nonconstant_step(%1 : i32) {
431  affine.for %2 = 1 to 5 step %1 { // expected-error {{expected attribute value}}
432
433// -----
434
435func.func @for_negative_stride() {
436  affine.for %i = 1 to 10 step -1
437}        // expected-error@-1 {{expected step to be representable as a positive signed integer}}
438
439// -----
440
441func.func @invalid_if_conditional2() {
442  affine.for %i = 1 to 10 {
443    affine.if affine_set<(i)[N] : (i >= )>  // expected-error {{expected affine expression}}
444  }
445}
446
447// -----
448
449func.func @invalid_if_conditional3() {
450  affine.for %i = 1 to 10 {
451    affine.if affine_set<(i)[N] : (i == )>  // expected-error {{expected affine expression}}
452  }
453}
454
455// -----
456
457func.func @invalid_if_conditional6() {
458  affine.for %i = 1 to 10 {
459    affine.if affine_set<(i) : (i)> // expected-error {{expected '== affine-expr' or '>= affine-expr' at end of affine constraint}}
460  }
461}
462
463// -----
464// TODO: support affine.if (1)?
465func.func @invalid_if_conditional7() {
466  affine.for %i = 1 to 10 {
467    affine.if affine_set<(i) : (1)> // expected-error {{expected '== affine-expr' or '>= affine-expr' at end of affine constraint}}
468  }
469}
470
471// -----
472
473func.func @missing_for_max(%arg0: index, %arg1: index, %arg2: memref<100xf32>) {
474  // expected-error @+1 {{lower loop bound affine map with multiple results requires 'max' prefix}}
475  affine.for %i0 = affine_map<()[s]->(0,s-1)>()[%arg0] to %arg1 {
476  }
477  return
478}
479
480// -----
481
482func.func @missing_for_min(%arg0: index, %arg1: index, %arg2: memref<100xf32>) {
483  // expected-error @+1 {{upper loop bound affine map with multiple results requires 'min' prefix}}
484  affine.for %i0 = %arg0 to affine_map<()[s]->(100,s+1)>()[%arg1] {
485  }
486  return
487}
488