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