1// RUN: mlir-opt -allow-unregistered-dialect %s -split-input-file -verify-diagnostics
2
3// Check different error cases.
4
5func.func @bad_branch() {
6^bb12:
7  cf.br ^missing  // expected-error {{reference to an undefined block}}
8}
9
10// -----
11
12func.func @block_redef() {
13^bb42:
14  return
15^bb42:        // expected-error {{redefinition of block '^bb42'}}
16  return
17}
18
19// -----
20
21func.func @no_terminator() {   // expected-error {{empty block: expect at least a terminator}}
22^bb40:
23  return
24^bb41:
25^bb42:
26  return
27}
28
29// -----
30
31func.func @block_no_rparen() {
32^bb42 (%bb42 : i32: // expected-error {{expected ')'}}
33  return
34}
35
36// -----
37
38func.func @block_arg_no_ssaid() {
39^bb42 (i32): // expected-error {{expected SSA operand}}
40  return
41}
42
43// -----
44
45func.func @block_arg_no_type() {
46^bb42 (%0): // expected-error {{expected ':' and type for SSA operand}}
47  return
48}
49
50// -----
51
52func.func @block_arg_no_close_paren() {
53^bb42:
54  cf.br ^bb2( // expected-error {{expected ':'}}
55  return
56}
57
58// -----
59
60func.func @block_first_has_predecessor() {
61// expected-error@-1 {{entry block of region may not have predecessors}}
62^bb42:
63  cf.br ^bb43
64^bb43:
65  cf.br ^bb42
66}
67
68// -----
69
70func.func @no_return() {
71  %x = arith.constant 0 : i32
72  %y = arith.constant 1 : i32  // expected-error {{block with no terminator}}
73}
74
75// -----
76
77func.func @no_terminator() {
78  cf.br ^bb1
79^bb1:
80  %x = arith.constant 0 : i32
81  %y = arith.constant 1 : i32  // expected-error {{block with no terminator}}
82}
83
84// -----
85
86func.func @no_block_arg_enclosing_parens() {
87^bb %x: i32 : // expected-error {{expected ':' after block name}}
88  return
89}
90
91// -----
92
93func.func @bad_op_type() {
94^bb40:
95  "foo"() : i32  // expected-error {{expected function type}}
96  return
97}
98// -----
99
100func.func @no_terminator() {
101^bb40:
102  "foo"() : ()->()
103  ""() : ()->()  // expected-error {{empty operation name is invalid}}
104  return
105}
106
107// -----
108
109func.func @non_operation() {
110  test.asd   // expected-error {{custom op 'test.asd' is unknown}}
111}
112
113// -----
114
115func.func @non_operation() {
116  // expected-error@+1 {{custom op 'asd' is unknown (tried 'func.asd' as well)}}
117  asd
118}
119
120// -----
121
122func.func @test() {
123^bb40:
124  %1 = "foo"() : (i32)->i64 // expected-error {{expected 0 operand types but had 1}}
125  return
126}
127
128// -----
129
130func.func @redef() {
131^bb42:
132  %x = "xxx"(){index = 0} : ()->i32 // expected-note {{previously defined here}}
133  %x = "xxx"(){index = 0} : ()->i32 // expected-error {{redefinition of SSA value '%x'}}
134  return
135}
136
137// -----
138
139func.func @undef() {
140^bb42:
141  %x = "xxx"(%y) : (i32)->i32   // expected-error {{use of undeclared SSA value}}
142  return
143}
144
145// -----
146
147func.func @malformed_type(%a : intt) { // expected-error {{expected non-function type}}
148}
149
150// -----
151
152func.func @argError() {
153^bb1(%a: i64):  // expected-note {{previously defined here}}
154  cf.br ^bb2
155^bb2(%a: i64):  // expected-error{{redefinition of SSA value '%a'}}
156  return
157}
158
159// -----
160
161func.func @br_mismatch() {
162^bb0:
163  %0:2 = "foo"() : () -> (i1, i17)
164  // expected-error @+1 {{branch has 2 operands for successor #0, but target block has 1}}
165  cf.br ^bb1(%0#1, %0#0 : i17, i1)
166
167^bb1(%x: i17):
168  return
169}
170
171// -----
172
173func.func @succ_arg_type_mismatch() {
174^bb0:
175  %0 = "getBool"() : () -> i1
176  // expected-error @+1 {{type mismatch for bb argument #0 of successor #0}}
177  cf.br ^bb1(%0 : i1)
178
179^bb1(%x: i32):
180  return
181}
182
183
184// -----
185
186func.func @condbr_notbool() {
187^bb0:
188  %a = "foo"() : () -> i32 // expected-note {{prior use here}}
189  cf.cond_br %a, ^bb0, ^bb0 // expected-error {{use of value '%a' expects different type than prior uses: 'i1' vs 'i32'}}
190}
191
192// -----
193
194func.func @condbr_badtype() {
195^bb0:
196  %c = "foo"() : () -> i1
197  %a = "foo"() : () -> i32
198  cf.cond_br %c, ^bb0(%a, %a : i32, ^bb0) // expected-error {{expected non-function type}}
199}
200
201// -----
202
203func.func @condbr_a_bb_is_not_a_type() {
204^bb0:
205  %c = "foo"() : () -> i1
206  %a = "foo"() : () -> i32
207  cf.cond_br %c, ^bb0(%a, %a : i32, i32), i32 // expected-error {{expected block name}}
208}
209
210// -----
211
212func.func @successors_in_non_terminator(%a : i32, %b : i32) {
213  %c = "arith.addi"(%a, %b)[^bb1] : () -> () // expected-error {{successors in non-terminator}}
214^bb1:
215  return
216}
217
218// -----
219
220func.func @undef() {
221^bb0:
222  %x = "xxx"(%y) : (i32)->i32   // expected-error {{use of undeclared SSA value name}}
223  return
224}
225
226// -----
227
228func.func @undef() {
229  %x = "xxx"(%y) : (i32)->i32   // expected-error {{use of undeclared SSA value name}}
230  return
231}
232
233// -----
234
235func.func @duplicate_induction_var() {
236  affine.for %i = 1 to 10 {   // expected-note {{previously referenced here}}
237    affine.for %i = 1 to 10 { // expected-error {{region entry argument '%i' is already in use}}
238    }
239  }
240  return
241}
242
243// -----
244
245func.func @name_scope_failure() {
246  affine.for %i = 1 to 10 {
247  }
248  "xxx"(%i) : (index)->()   // expected-error {{use of undeclared SSA value name}}
249  return
250}
251
252// -----
253
254func.func @dominance_failure() {
255^bb0:
256  "foo"(%x) : (i32) -> ()    // expected-error {{operand #0 does not dominate this use}}
257  cf.br ^bb1
258^bb1:
259  %x = "bar"() : () -> i32    // expected-note {{operand defined here (op in the same region)}}
260  return
261}
262
263// -----
264
265func.func @dominance_failure() {
266^bb0:
267  "foo"(%x) : (i32) -> ()    // expected-error {{operand #0 does not dominate this use}}
268  %x = "bar"() : () -> i32    // expected-note {{operand defined here (op in the same block)}}
269  cf.br ^bb1
270^bb1:
271  return
272}
273
274// -----
275
276func.func @dominance_failure() {
277  "foo"() ({
278    "foo"(%x) : (i32) -> ()    // expected-error {{operand #0 does not dominate this use}}
279  }) : () -> ()
280  %x = "bar"() : () -> i32    // expected-note {{operand defined here (op in a parent region)}}
281  return
282}
283
284// -----
285
286func.func @dominance_failure() {  //  expected-note {{operand defined as a block argument (block #1 in the same region)}}
287^bb0:
288  cf.br ^bb1(%x : i32)    // expected-error {{operand #0 does not dominate this use}}
289^bb1(%x : i32):
290  return
291}
292
293// -----
294
295func.func @dominance_failure() {  //  expected-note {{operand defined as a block argument (block #1 in a parent region)}}
296^bb0:
297  %f = "foo"() ({
298    "foo"(%x) : (i32) -> ()    // expected-error {{operand #0 does not dominate this use}}
299  }) : () -> (i32)
300  cf.br ^bb1(%f : i32)
301^bb1(%x : i32):
302  return
303}
304
305// -----
306
307// expected-error@+1 {{expected three consecutive dots for an ellipsis}}
308func.func @malformed_ellipsis_one(.)
309
310// -----
311
312// expected-error@+1 {{expected three consecutive dots for an ellipsis}}
313func.func @malformed_ellipsis_two(..)
314
315// -----
316
317func.func private @redef()  // expected-note {{see existing symbol definition here}}
318func.func private @redef()  // expected-error {{redefinition of symbol named 'redef'}}
319
320// -----
321
322func.func @calls(%arg0: i32) {
323  // expected-error@+1 {{expected non-function type}}
324  %z = "casdasda"(%x) : (ppop32) -> i32
325}
326
327// -----
328
329// expected-error@+1 {{expected SSA operand}}
330func.func @n(){^b(
331
332// -----
333
334// This used to crash the parser, but should just error out by interpreting
335// `tensor` as operator rather than as a type.
336func.func @f(f32) {
337^bb0(%a : f32):
338  %18 = arith.cmpi slt, %idx, %idx : index
339  tensor<42 x index  // expected-error {{custom op 'tensor' is unknown (tried 'func.tensor' as well)}}
340  return
341}
342
343// -----
344
345func.func @f(%m : memref<?x?xf32>) {
346  affine.for %i0 = 0 to 42 {
347    // expected-note@+1 {{previously referenced here}}
348    %x = memref.load %m[%i0, %i1] : memref<?x?xf32>
349  }
350  // expected-error@+1 {{region entry argument '%i1' is already in use}}
351  affine.for %i1 = 0 to 42 {
352  }
353  return
354}
355
356// -----
357
358func.func @dialect_type_empty_namespace(!<"">) -> () { // expected-error {{invalid type identifier}}
359  return
360}
361
362// -----
363
364func.func @dialect_type_missing_greater(!foo<) -> () { // expected-error {{unbalanced ')' character in pretty dialect name}}
365  return
366
367// -----
368
369func.func @type_alias_unknown(!unknown_alias) -> () { // expected-error {{undefined symbol alias id 'unknown_alias'}}
370  return
371}
372
373// -----
374
375// expected-error @+1 {{type names with a '.' are reserved for dialect-defined names}}
376!foo.bar = i32
377
378// -----
379
380!missing_eq_alias i32 // expected-error {{expected '=' in type alias definition}}
381
382// -----
383
384!missing_type_alias = // expected-error {{expected non-function type}}
385
386// -----
387
388!redef_alias = i32
389!redef_alias = i32 // expected-error {{redefinition of type alias id 'redef_alias'}}
390
391// -----
392
393func.func @invalid_nested_dominance() {
394  "test.ssacfg_region"() ({
395    // expected-error @+1 {{operand #0 does not dominate this use}}
396    "foo.use" (%1) : (i32) -> ()
397    cf.br ^bb2
398
399  ^bb2:
400    // expected-note @+1 {{operand defined here}}
401    %1 = arith.constant 0 : i32
402    "foo.yield" () : () -> ()
403  }) : () -> ()
404  return
405}
406
407// -----
408
409// expected-error @+1 {{unbalanced ']' character in pretty dialect name}}
410func.func @invalid_unknown_type_dialect_name() -> !invalid.dialect<!x@#]!@#>
411
412// -----
413
414// expected-error @+1 {{expected '<' in tuple type}}
415func.func @invalid_tuple_missing_less(tuple i32>)
416
417// -----
418
419// expected-error @+1 {{expected '>' in tuple type}}
420func.func @invalid_tuple_missing_greater(tuple<i32)
421
422// -----
423
424// Should not crash because of deletion order here.
425func.func @invalid_region_dominance() {
426  "foo.use" (%1) : (i32) -> ()
427  "foo.region"() ({
428    %1 = arith.constant 0 : i32  // This value is used outside of the region.
429    "foo.yield" () : () -> ()
430  }, {
431    // expected-error @+1 {{expected operation name in quotes}}
432    %2 = arith.constant 1 i32  // Syntax error causes region deletion.
433  }) : () -> ()
434  return
435}
436
437// -----
438
439// Should not crash because of deletion order here.
440func.func @invalid_region_block() {
441  "foo.branch"()[^bb2] : () -> ()  // Attempt to jump into the region.
442
443^bb1:
444  "foo.region"() ({
445    ^bb2:
446      "foo.yield"() : () -> ()
447  }, {
448    // expected-error @+1 {{expected operation name in quotes}}
449    %2 = arith.constant 1 i32  // Syntax error causes region deletion.
450  }) : () -> ()
451}
452
453// -----
454
455// Should not crash because of deletion order here.
456func.func @invalid_region_dominance() {
457  "foo.use" (%1) : (i32) -> ()
458  "foo.region"() ({
459    "foo.region"() ({
460      %1 = arith.constant 0 : i32  // This value is used outside of the region.
461      "foo.yield" () : () -> ()
462    }) : () -> ()
463  }, {
464    // expected-error @+1 {{expected operation name in quotes}}
465    %2 = arith.constant 1 i32  // Syntax error causes region deletion.
466  }) : () -> ()
467  return
468}
469
470// -----
471
472func.func @unfinished_region_list() {
473  // expected-error@+1 {{expected ')' to end region list}}
474  "region"() ({},{},{} : () -> ()
475}
476
477// -----
478
479func.func @multi_result_missing_count() {
480  // expected-error@+1 {{expected integer number of results}}
481  %0: = "foo" () : () -> (i32, i32)
482  return
483}
484
485// -----
486
487func.func @multi_result_zero_count() {
488  // expected-error@+1 {{expected named operation to have at least 1 result}}
489  %0:0 = "foo" () : () -> (i32, i32)
490  return
491}
492
493// -----
494
495func.func @multi_result_invalid_identifier() {
496  // expected-error@+1 {{expected valid ssa identifier}}
497  %0, = "foo" () : () -> (i32, i32)
498  return
499}
500
501// -----
502
503func.func @multi_result_mismatch_count() {
504  // expected-error@+1 {{operation defines 2 results but was provided 1 to bind}}
505  %0:1 = "foo" () : () -> (i32, i32)
506  return
507}
508
509// -----
510
511func.func @multi_result_mismatch_count() {
512  // expected-error@+1 {{operation defines 2 results but was provided 3 to bind}}
513  %0, %1, %3 = "foo" () : () -> (i32, i32)
514  return
515}
516
517// -----
518
519func.func @no_result_with_name() {
520  // expected-error@+1 {{cannot name an operation with no results}}
521  %0 = "foo" () : () -> ()
522  return
523}
524
525// -----
526
527func.func @conflicting_names() {
528  // expected-note@+1 {{previously defined here}}
529  %foo, %bar  = "foo" () : () -> (i32, i32)
530
531  // expected-error@+1 {{redefinition of SSA value '%bar'}}
532  %bar, %baz  = "foo" () : () -> (i32, i32)
533  return
534}
535
536// -----
537
538func.func @ssa_name_missing_eq() {
539  // expected-error@+1 {{expected '=' after SSA name}}
540  %0:2 "foo" () : () -> (i32, i32)
541  return
542}
543
544// -----
545
546// expected-error @+1 {{attribute names with a '.' are reserved for dialect-defined names}}
547#foo.attr = i32
548
549// -----
550
551func.func @invalid_region_dominance() {
552  "test.ssacfg_region"() ({
553    // expected-error @+1 {{operand #0 does not dominate this use}}
554    "foo.use" (%def) : (i32) -> ()
555    "foo.yield" () : () -> ()
556  }, {
557    // expected-note @+1 {{operand defined here}}
558    %def = "foo.def" () : () -> i32
559  }) : () -> ()
560  return
561}
562
563// -----
564
565func.func @invalid_region_dominance() {
566  // expected-note @+1 {{operand defined here}}
567  %def = "test.ssacfg_region"() ({
568    // expected-error @+1 {{operand #0 does not dominate this use}}
569    "foo.use" (%def) : (i32) -> ()
570    "foo.yield" () : () -> ()
571  }) : () -> (i32)
572  return
573}
574
575// -----
576
577// expected-error @+1 {{unbalanced ')' character in pretty dialect name}}
578func.func @bad_arrow(%arg : !unreg.ptr<(i32)->)
579
580// -----
581
582// expected-error @+1 {{attribute 'attr' occurs more than once in the attribute list}}
583test.format_symbol_name_attr_op @name { attr = "xx" }
584
585// -----
586
587func.func @forward_reference_type_check() -> (i8) {
588  cf.br ^bb2
589
590^bb1:
591  // expected-note @+1 {{previously used here with type 'i8'}}
592  return %1 : i8
593
594^bb2:
595  // expected-error @+1 {{definition of SSA value '%1#0' has type 'f32'}}
596  %1 = "bar"() : () -> (f32)
597  cf.br ^bb1
598}
599
600// -----
601
602func.func @dominance_error_in_unreachable_op() -> i1 {
603  %c = arith.constant false
604  return %c : i1
605^bb0:
606  "test.ssacfg_region" () ({ // unreachable
607    ^bb1:
608// expected-error @+1 {{operand #0 does not dominate this use}}
609      %2:3 = "bar"(%1) : (i64) -> (i1,i1,i1)
610      cf.br ^bb4
611    ^bb2:
612      cf.br ^bb2
613    ^bb4:
614      %1 = "foo"() : ()->i64   // expected-note {{operand defined here}}
615  }) : () -> ()
616  return %c : i1
617}
618
619// -----
620
621func.func @invalid_region_dominance_with_dominance_free_regions() {
622  test.graph_region {
623    "foo.use" (%1) : (i32) -> ()
624    "foo.region"() ({
625      %1 = arith.constant 0 : i32  // This value is used outside of the region.
626      "foo.yield" () : () -> ()
627    }, {
628      // expected-error @+1 {{expected operation name in quotes}}
629      %2 = arith.constant 1 i32  // Syntax error causes region deletion.
630    }) : () -> ()
631  }
632  return
633}
634
635// -----
636
637// expected-error@+1 {{expected valid attribute name}}
638"t"(){""}
639
640// -----
641
642// This makes sure we emit an error at the end of the correct line, the : is
643// expected at the end of foo, not on the return line.
644func.func @error_at_end_of_line() {
645  // expected-error@+1 {{expected ':' followed by operation type}}
646  %0 = "foo"()
647  return
648}
649
650// -----
651
652// This makes sure we emit an error at the end of the correct line, the : is
653// expected at the end of foo, not on the return line.
654func.func @error_at_end_of_line() {
655  %0 = "foo"()
656  // expected-error@-1 {{expected ':' followed by operation type}}
657
658  // This is a comment and so is the thing above.
659  return
660}
661
662// -----
663
664// This makes sure we emit an error at the end of the correct line, the : is
665// expected at the end of foo, not on the return line.
666// This shows that it backs up to before the comment.
667func.func @error_at_end_of_line() {
668  %0 = "foo"()  // expected-error {{expected ':' followed by operation type}}
669  return
670}
671
672// -----
673
674@foo   // expected-error {{expected operation name in quotes}}
675