xref: /llvm-project/mlir/test/mlir-tblgen/op-format-spec.td (revision 44b3cf46e992b76a9e188367922a1184437dfa4c)
1// RUN: mlir-tblgen -gen-op-decls -I %S/../../include %s -o=%t
2
3// This file contains tests for the specification of the declarative op format.
4
5include "mlir/IR/OpBase.td"
6include "mlir/Interfaces/InferTypeOpInterface.td"
7
8def TestDialect : Dialect {
9  let name = "test";
10}
11class TestFormat_Op<string fmt, list<Trait> traits = []>
12    : Op<TestDialect, "format_op", traits> {
13  let assemblyFormat = fmt;
14}
15
16//===----------------------------------------------------------------------===//
17// Directives
18//===----------------------------------------------------------------------===//
19
20//===----------------------------------------------------------------------===//
21// attr-dict
22
23// CHECK-NOT: error
24def DirectiveAttrDictValidA : TestFormat_Op<[{
25  attr-dict
26}]>;
27def DirectiveAttrDictValidB : TestFormat_Op<[{
28  attr-dict-with-keyword
29}]>;
30
31//===----------------------------------------------------------------------===//
32// custom
33
34// CHECK-NOT: error
35def DirectiveCustomValidA : TestFormat_Op<[{
36  custom<MyDirective>($operand) attr-dict
37}]>, Arguments<(ins Optional<I64>:$operand)>;
38def DirectiveCustomValidB : TestFormat_Op<[{
39  custom<MyDirective>($operand, type($operand), type($result)) attr-dict
40}]>, Arguments<(ins I64:$operand)>, Results<(outs I64:$result)>;
41def DirectiveCustomValidC : TestFormat_Op<[{
42  custom<MyDirective>($attr) attr-dict
43}]>, Arguments<(ins I64Attr:$attr)>;
44def DirectiveCustomValidD : TestFormat_Op<[{
45  (`(` custom<MyDirective>($operand)^ `)`)? attr-dict
46}]>, Arguments<(ins Optional<I64>:$operand)>;
47def DirectiveCustomValidE : TestFormat_Op<[{
48  custom<MyDirective>(prop-dict) attr-dict
49}]>, Arguments<(ins UnitAttr:$flag)>;
50
51//===----------------------------------------------------------------------===//
52// functional-type
53
54// CHECK-NOT: error
55def DirectiveFunctionalTypeValid : TestFormat_Op<[{
56  functional-type(operands, results) attr-dict
57}]>;
58
59//===----------------------------------------------------------------------===//
60// operands
61
62// CHECK-NOT: error:
63def DirectiveOperandsValid : TestFormat_Op<[{
64  operands attr-dict
65}]>;
66
67//===----------------------------------------------------------------------===//
68// regions
69
70// CHECK-NOT: error:
71def DirectiveRegionsValid : TestFormat_Op<[{
72  regions attr-dict
73}]>;
74
75//===----------------------------------------------------------------------===//
76// results
77
78// CHECK-NOT: error:
79def DirectiveResultsInvalidA : TestFormat_Op<[{
80  type(results) attr-dict
81}]>;
82
83//===----------------------------------------------------------------------===//
84// successors
85
86// CHECK-NOT: error:
87def DirectiveSuccessorsInvalidA : TestFormat_Op<[{
88  successors attr-dict
89}]>;
90
91//===----------------------------------------------------------------------===//
92// type
93
94// CHECK-NOT: error:
95def DirectiveTypeValid : TestFormat_Op<[{
96  type(operands) attr-dict
97}]>;
98
99//===----------------------------------------------------------------------===//
100// Literals
101//===----------------------------------------------------------------------===//
102
103// CHECK-NOT: error
104def LiteralValid : TestFormat_Op<[{
105  `_` `:` `,` `=` `<` `>` `(` `)` `[` `]` `?` `+` `*` ` ` `` `->` `\n` `abc$._`
106  attr-dict
107}]>;
108
109//===----------------------------------------------------------------------===//
110// OIList Element
111//===----------------------------------------------------------------------===//
112
113// CHECK-NOT: error
114def OIListTrivial : TestFormat_Op<[{
115  oilist(`keyword` `(` `)` | `otherkeyword` `(` `)`) attr-dict
116}]>;
117def OIListSimple : TestFormat_Op<[{
118  oilist( `keyword` $arg0 `:` type($arg0)
119        | `otherkeyword` $arg1 `:` type($arg1)
120        | `thirdkeyword` $arg2 `:` type($arg2) )
121  attr-dict
122}], [AttrSizedOperandSegments]>, Arguments<(ins Optional<AnyType>:$arg0, Optional<AnyType>:$arg1, Optional<AnyType>:$arg2)>;
123def OIListVariadic : TestFormat_Op<[{
124  oilist( `keyword` `(` $args0 `:` type($args0) `)`
125        | `otherkeyword` `(` $args1 `:` type($args1) `)`
126        | `thirdkeyword` `(` $args2 `:` type($args2) `)`)
127  attr-dict
128}], [AttrSizedOperandSegments]>, Arguments<(ins Variadic<AnyType>:$args0, Variadic<AnyType>:$args1, Variadic<AnyType>:$args2)>;
129def OIListCustom : TestFormat_Op<[{
130  oilist( `private` `(` $arg0 `:` type($arg0) `)`
131        | `nowait`
132        | `reduction` custom<ReductionClause>($arg1, type($arg1))) attr-dict
133}], [AttrSizedOperandSegments]>, Arguments<(ins Optional<AnyType>:$arg0, Optional<AnyType>:$arg1)>;
134
135//===----------------------------------------------------------------------===//
136// Optional Groups
137//===----------------------------------------------------------------------===//
138
139// CHECK-NOT: error
140def OptionalValidA : TestFormat_Op<[{
141  (` ` `` $arg^)? attr-dict
142}]>, Arguments<(ins Optional<I32>:$arg)>;
143
144//===----------------------------------------------------------------------===//
145// Strings
146//===----------------------------------------------------------------------===//
147
148// CHECK-NOT: error
149def StringInvalidA : TestFormat_Op<[{ custom<Foo>("foo") attr-dict }]>;
150
151//===----------------------------------------------------------------------===//
152// Variables
153//===----------------------------------------------------------------------===//
154
155// CHECK-NOT: error:
156def VariableValidA : TestFormat_Op<[{
157  $attr `:` attr-dict
158}]>, Arguments<(ins OptionalAttr<I1Attr>:$attr)>;
159def VariableValidB : TestFormat_Op<[{
160  (`foo` $attr^)? `:` attr-dict
161}]>, Arguments<(ins OptionalAttr<I1Attr>:$attr)>;
162
163//===----------------------------------------------------------------------===//
164// Coverage Checks
165//===----------------------------------------------------------------------===//
166
167// CHECK-NOT: error
168def ZCoverageValidA : TestFormat_Op<[{
169  $operand type($operand) type($result) attr-dict
170}]>, Arguments<(ins AnyMemRef:$operand)>, Results<(outs AnyMemRef:$result)>;
171def ZCoverageValidB : TestFormat_Op<[{
172  $operand type(operands) type(results) attr-dict
173}]>, Arguments<(ins AnyMemRef:$operand)>, Results<(outs AnyMemRef:$result)>;
174def ZCoverageValidC : TestFormat_Op<[{
175  operands functional-type(operands, results) attr-dict
176}]>, Arguments<(ins AnyMemRef:$operand)>, Results<(outs AnyMemRef:$result)>;
177
178// Check that we can infer type equalities from certain traits.
179def ZCoverageValidD : TestFormat_Op<[{
180  operands type($result) attr-dict
181}], [SameOperandsAndResultType]>, Arguments<(ins AnyMemRef)>,
182     Results<(outs AnyMemRef:$result)>;
183def ZCoverageValidE : TestFormat_Op<[{
184  $operand type($operand) attr-dict
185}], [SameOperandsAndResultType]>, Arguments<(ins AnyMemRef:$operand)>,
186     Results<(outs AnyMemRef)>;
187def ZCoverageValidF : TestFormat_Op<[{
188  operands type($other) attr-dict
189}], [SameTypeOperands]>, Arguments<(ins AnyMemRef:$operand, AnyMemRef:$other)>;
190def ZCoverageValidG : TestFormat_Op<[{
191  operands type($other) attr-dict
192}], [AllTypesMatch<["operand", "other"]>]>,
193     Arguments<(ins AnyMemRef:$operand, AnyMemRef:$other)>;
194def ZCoverageValidH : TestFormat_Op<[{
195  operands type($result) attr-dict
196}], [AllTypesMatch<["operand", "result"]>]>,
197     Arguments<(ins AnyMemRef:$operand)>, Results<(outs AnyMemRef:$result)>;
198def ZCoverageValidI : TestFormat_Op<[{
199  operands type(operands) attr-dict
200}], [InferTypeOpInterface]>, Arguments<(ins Variadic<I64>:$inputs)>, Results<(outs I64:$result)>;
201