xref: /llvm-project/mlir/test/mlir-tblgen/op-python-bindings.td (revision acde3f722ff3766f6f793884108d342b78623fe4)
1// RUN: mlir-tblgen -gen-python-op-bindings -bind-dialect=test -I %S/../../include %s | FileCheck %s
2
3include "mlir/IR/OpBase.td"
4include "mlir/IR/AttrTypeBase.td"
5include "mlir/Interfaces/InferTypeOpInterface.td"
6
7// CHECK: @_ods_cext.register_dialect
8// CHECK: class _Dialect(_ods_ir.Dialect):
9  // CHECK: DIALECT_NAMESPACE = "test"
10def Test_Dialect : Dialect {
11  let name = "test";
12  let cppNamespace = "Test";
13}
14
15class TestOp<string mnemonic, list<Trait> traits = []> :
16    Op<Test_Dialect, mnemonic, traits>;
17
18// CHECK: @_ods_cext.register_operation(_Dialect)
19// CHECK: class AttrSizedOperandsOp(_ods_ir.OpView):
20// CHECK-LABEL: OPERATION_NAME = "test.attr_sized_operands"
21// CHECK: _ODS_OPERAND_SEGMENTS = [-1,1,0,]
22def AttrSizedOperandsOp : TestOp<"attr_sized_operands",
23                                 [AttrSizedOperandSegments]> {
24  // CHECK: def __init__(self, variadic1, non_variadic, *, variadic2=None, loc=None, ip=None):
25  // CHECK:   operands = []
26  // CHECK:   results = []
27  // CHECK:   attributes = {}
28  // CHECK:   regions = None
29  // CHECK:   operands.append(_get_op_results_or_values(variadic1))
30  // CHECK:   operands.append(non_variadic)
31  // CHECK:   operands.append(variadic2)
32  // CHECK:   _ods_successors = None
33  // CHECK:   super().__init__(
34  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
35  // CHECK:     attributes=attributes, results=results, operands=operands,
36  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
37
38  // CHECK: @builtins.property
39  // CHECK: def variadic1(self):
40  // CHECK:   operand_range = _ods_segmented_accessor(
41  // CHECK:       self.operation.operands,
42  // CHECK:       self.operation.attributes["operandSegmentSizes"], 0)
43  // CHECK:   return operand_range
44  // CHECK-NOT: if len(operand_range)
45  //
46  // CHECK: @builtins.property
47  // CHECK: def non_variadic(self):
48  // CHECK:   operand_range = _ods_segmented_accessor(
49  // CHECK:       self.operation.operands,
50  // CHECK:       self.operation.attributes["operandSegmentSizes"], 1)
51  // CHECK:   return operand_range[0]
52  //
53  // CHECK: @builtins.property
54  // CHECK: def variadic2(self):
55  // CHECK:   operand_range = _ods_segmented_accessor(
56  // CHECK:       self.operation.operands,
57  // CHECK:       self.operation.attributes["operandSegmentSizes"], 2)
58  // CHECK:   return operand_range[0] if len(operand_range) > 0 else None
59  let arguments = (ins Variadic<AnyType>:$variadic1, AnyType:$non_variadic,
60                   Optional<AnyType>:$variadic2);
61}
62
63// CHECK: def attr_sized_operands(variadic1, non_variadic, *, variadic2=None, loc=None, ip=None)
64// CHECK:   return AttrSizedOperandsOp(variadic1=variadic1, non_variadic=non_variadic, variadic2=variadic2, loc=loc, ip=ip)
65
66// CHECK: @_ods_cext.register_operation(_Dialect)
67// CHECK: class AttrSizedResultsOp(_ods_ir.OpView):
68// CHECK-LABEL: OPERATION_NAME = "test.attr_sized_results"
69// CHECK: _ODS_RESULT_SEGMENTS = [0,1,-1,]
70def AttrSizedResultsOp : TestOp<"attr_sized_results",
71                               [AttrSizedResultSegments]> {
72  // CHECK: def __init__(self, variadic1, non_variadic, variadic2, *, loc=None, ip=None):
73  // CHECK:   operands = []
74  // CHECK:   results = []
75  // CHECK:   attributes = {}
76  // CHECK:   regions = None
77  // CHECK:   if variadic1 is not None: results.append(variadic1)
78  // CHECK:   results.append(non_variadic)
79  // CHECK:   results.append(variadic2)
80  // CHECK:   _ods_successors = None
81  // CHECK:   super().__init__(
82  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
83  // CHECK:     attributes=attributes, results=results, operands=operands,
84  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
85
86  // CHECK: @builtins.property
87  // CHECK: def variadic1(self):
88  // CHECK:   result_range = _ods_segmented_accessor(
89  // CHECK:       self.operation.results,
90  // CHECK:       self.operation.attributes["resultSegmentSizes"], 0)
91  // CHECK:   return result_range[0] if len(result_range) > 0 else None
92  //
93  // CHECK: @builtins.property
94  // CHECK: def non_variadic(self):
95  // CHECK:   result_range = _ods_segmented_accessor(
96  // CHECK:       self.operation.results,
97  // CHECK:       self.operation.attributes["resultSegmentSizes"], 1)
98  // CHECK:   return result_range[0]
99  //
100  // CHECK: @builtins.property
101  // CHECK: def variadic2(self):
102  // CHECK:   result_range = _ods_segmented_accessor(
103  // CHECK:       self.operation.results,
104  // CHECK:       self.operation.attributes["resultSegmentSizes"], 2)
105  // CHECK:   return result_range
106  // CHECK-NOT: if len(result_range)
107  let results = (outs Optional<AnyType>:$variadic1, AnyType:$non_variadic,
108                 Variadic<AnyType>:$variadic2);
109}
110
111// CHECK: def attr_sized_results(variadic1, non_variadic, variadic2, *, loc=None, ip=None)
112// CHECK:   return _get_op_result_or_op_results(AttrSizedResultsOp(variadic1=variadic1, non_variadic=non_variadic, variadic2=variadic2, loc=loc, ip=ip))
113
114
115// CHECK: @_ods_cext.register_operation(_Dialect)
116// CHECK: class AttributedOp(_ods_ir.OpView):
117// CHECK-LABEL: OPERATION_NAME = "test.attributed_op"
118// CHECK-NOT: _ODS_OPERAND_SEGMENTS
119// CHECK-NOT: _ODS_RESULT_SEGMENTS
120def AttributedOp : TestOp<"attributed_op"> {
121  // CHECK: def __init__(self, i32attr, in_, *, optionalF32Attr=None, unitAttr=None, loc=None, ip=None):
122  // CHECK:   operands = []
123  // CHECK:   results = []
124  // CHECK:   attributes = {}
125  // CHECK:   regions = None
126  // CHECK:   attributes["i32attr"] = (i32attr if (
127  // CHECK-NEXT:   isinstance(i32attr, _ods_ir.Attribute) or
128  // CHECK-NEXT:   not _ods_ir.AttrBuilder.contains('I32Attr')
129  // CHECK-NEXT:   _ods_ir.AttrBuilder.get('I32Attr')(i32attr, context=_ods_context)
130  // CHECK:   if optionalF32Attr is not None: attributes["optionalF32Attr"] = (optionalF32Attr
131  // CHECK:   if bool(unitAttr): attributes["unitAttr"] = _ods_ir.UnitAttr.get(
132  // CHECK:     _ods_get_default_loc_context(loc))
133  // CHECK:   attributes["in"] = (in_
134  // CHECK:   _ods_successors = None
135  // CHECK:   super().__init__(
136  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
137  // CHECK:     attributes=attributes, results=results, operands=operands,
138  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
139
140  // CHECK: @builtins.property
141  // CHECK: def i32attr(self):
142  // CHECK:   return self.operation.attributes["i32attr"]
143
144  // CHECK: @builtins.property
145  // CHECK: def optionalF32Attr(self):
146  // CHECK:   if "optionalF32Attr" not in self.operation.attributes:
147  // CHECK:     return None
148  // CHECK:   return self.operation.attributes["optionalF32Attr"]
149
150  // CHECK: @builtins.property
151  // CHECK: def unitAttr(self):
152  // CHECK:   return "unitAttr" in self.operation.attributes
153
154  // CHECK: @builtins.property
155  // CHECK: def in_(self):
156  // CHECK:   return self.operation.attributes["in"]
157
158  let arguments = (ins I32Attr:$i32attr, OptionalAttr<F32Attr>:$optionalF32Attr,
159                   UnitAttr:$unitAttr, I32Attr:$in);
160}
161
162// CHECK: def attributed_op(i32attr, in_, *, optional_f32_attr=None, unit_attr=None, loc=None, ip=None)
163// CHECK:     return AttributedOp(i32attr=i32attr, in_=in_, optionalF32Attr=optional_f32_attr, unitAttr=unit_attr, loc=loc, ip=ip)
164
165// CHECK: @_ods_cext.register_operation(_Dialect)
166// CHECK: class AttributedOpWithOperands(_ods_ir.OpView):
167// CHECK-LABEL: OPERATION_NAME = "test.attributed_op_with_operands"
168// CHECK-NOT: _ODS_OPERAND_SEGMENTS
169// CHECK-NOT: _ODS_RESULT_SEGMENTS
170def AttributedOpWithOperands : TestOp<"attributed_op_with_operands"> {
171  // CHECK: def __init__(self, _gen_arg_0, _gen_arg_2, *, in_=None, is_=None, loc=None, ip=None):
172  // CHECK:   operands = []
173  // CHECK:   results = []
174  // CHECK:   attributes = {}
175  // CHECK:   regions = None
176  // CHECK:   operands.append(_gen_arg_0)
177  // CHECK:   operands.append(_gen_arg_2)
178  // CHECK:   if bool(in_): attributes["in"] = _ods_ir.UnitAttr.get(
179  // CHECK:     _ods_get_default_loc_context(loc))
180  // CHECK:   if is_ is not None: attributes["is"] = (is_
181  // CHECK:   _ods_successors = None
182  // CHECK:   super().__init__(
183  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
184  // CHECK:     attributes=attributes, results=results, operands=operands,
185  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
186
187  // CHECK: @builtins.property
188  // CHECK: def in_(self):
189  // CHECK:   return "in" in self.operation.attributes
190
191  // CHECK: @builtins.property
192  // CHECK: def is_(self):
193  // CHECK:   if "is" not in self.operation.attributes:
194  // CHECK:     return None
195  // CHECK:   return self.operation.attributes["is"]
196  let arguments = (ins I32, UnitAttr:$in, F32, OptionalAttr<F32Attr>:$is);
197}
198
199// CHECK: def attributed_op_with_operands(_gen_arg_0, _gen_arg_2, *, in_=None, is_=None, loc=None, ip=None)
200// CHECK:   return AttributedOpWithOperands(_gen_arg_0=_gen_arg_0, _gen_arg_2=_gen_arg_2, in_=in_, is_=is_, loc=loc, ip=ip)
201
202// CHECK: @_ods_cext.register_operation(_Dialect)
203// CHECK: class DefaultValuedAttrsOp(_ods_ir.OpView):
204// CHECK-LABEL: OPERATION_NAME = "test.default_valued_attrs"
205def DefaultValuedAttrsOp : TestOp<"default_valued_attrs"> {
206  // CHECK: def __init__(self, *, arr=None, unsupported=None, loc=None, ip=None):
207  // CHECK:   operands = []
208  // CHECK:   results = []
209  // CHECK:   attributes = {}
210  // CHECK:   regions = None
211  // CHECK:   if arr is not None: attributes["arr"] = (arr
212  // CHECK:   if unsupported is not None: attributes["unsupported"] = (unsupported
213  // CHECK:   _ods_successors = None
214  // CHECK:   super().__init__(
215  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
216  // CHECK:     attributes=attributes, results=results, operands=operands,
217  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
218
219  let arguments = (ins DefaultValuedAttr<I64ArrayAttr, "{}">:$arr,
220                       DefaultValuedAttr<I64ArrayAttr, "dummy_func()">:$unsupported);
221  let results = (outs);
222}
223
224// CHECK: def default_valued_attrs(*, arr=None, unsupported=None, loc=None, ip=None)
225// CHECK:   return DefaultValuedAttrsOp(arr=arr, unsupported=unsupported, loc=loc, ip=ip)
226
227// CHECK-LABEL: OPERATION_NAME = "test.derive_result_types_op"
228def DeriveResultTypesOp : TestOp<"derive_result_types_op", [FirstAttrDerivedResultType]> {
229  // CHECK: def __init__(self, type_, *, loc=None, ip=None):
230  // CHECK:   operands = []
231  // CHECK:   results = []
232  // CHECK:   _ods_result_type_source_attr = attributes["type"]
233  // CHECK:   _ods_derived_result_type = (
234  // CHECK:       _ods_ir.TypeAttr(_ods_result_type_source_attr).value
235  // CHECK:       if _ods_ir.TypeAttr.isinstance(_ods_result_type_source_attr) else
236  // CHECK:       _ods_result_type_source_attr.type)
237  // CHECK:   results.extend([_ods_derived_result_type] * 2)
238  let arguments = (ins TypeAttr:$type);
239  let results = (outs AnyType:$res, AnyType);
240}
241
242// CHECK: def derive_result_types_op(type_, *, loc=None, ip=None)
243// CHECK:   return DeriveResultTypesOp(type_=type_, loc=loc, ip=ip).results
244
245// CHECK-LABEL: OPERATION_NAME = "test.derive_result_types_variadic_op"
246def DeriveResultTypesVariadicOp : TestOp<"derive_result_types_variadic_op", [FirstAttrDerivedResultType]> {
247  // CHECK: def __init__(self, res, _gen_res_1, type_, *, loc=None, ip=None):
248  let arguments = (ins TypeAttr:$type);
249  let results = (outs AnyType:$res, Variadic<AnyType>);
250}
251
252// CHECK: def derive_result_types_variadic_op(res, _gen_res_1, type_, *, loc=None, ip=None)
253// CHECK:   return _get_op_result_or_op_results(DeriveResultTypesVariadicOp(res=res, _gen_res_1=_gen_res_1, type_=type_, loc=loc, ip=ip))
254
255// CHECK: @_ods_cext.register_operation(_Dialect)
256// CHECK: class EmptyOp(_ods_ir.OpView):
257// CHECK-LABEL: OPERATION_NAME = "test.empty"
258def EmptyOp : TestOp<"empty">;
259  // CHECK: def __init__(self, *, loc=None, ip=None):
260  // CHECK:   operands = []
261  // CHECK:   results = []
262  // CHECK:   attributes = {}
263  // CHECK:   regions = None
264  // CHECK:   _ods_successors = None
265  // CHECK:   super().__init__(
266  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
267  // CHECK:     attributes=attributes, results=results, operands=operands,
268  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
269
270// CHECK: def empty(*, loc=None, ip=None)
271// CHECK:   return EmptyOp(loc=loc, ip=ip)
272
273// CHECK-LABEL: OPERATION_NAME = "test.infer_result_types_implied_op"
274def InferResultTypesImpliedOp : TestOp<"infer_result_types_implied_op"> {
275  // CHECK:  def __init__(self, *, loc=None, ip=None):
276  // CHECK:    _ods_context = _ods_get_default_loc_context(loc)
277  // CHECK:   super().__init__(
278  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
279  // CHECK:     attributes=attributes, operands=operands,
280  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
281  let results = (outs I32:$i32, F32:$f32);
282}
283
284// CHECK: def infer_result_types_implied_op(*, loc=None, ip=None)
285// CHECK:   return InferResultTypesImpliedOp(loc=loc, ip=ip).results
286
287// CHECK-LABEL: OPERATION_NAME = "test.infer_result_types_op"
288def InferResultTypesOp : TestOp<"infer_result_types_op", [InferTypeOpInterface]> {
289  // CHECK:  def __init__(self, *, loc=None, ip=None):
290  // CHECK:    operands = []
291  // CHECK:   super().__init__(
292  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS,
293  // CHECK:     attributes=attributes, operands=operands,
294  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
295  let results = (outs AnyType, AnyType, AnyType);
296}
297
298// CHECK: def infer_result_types_op(*, loc=None, ip=None)
299// CHECK:   return InferResultTypesOp(loc=loc, ip=ip).results
300
301// CHECK: @_ods_cext.register_operation(_Dialect)
302// CHECK: class MissingNamesOp(_ods_ir.OpView):
303// CHECK-LABEL: OPERATION_NAME = "test.missing_names"
304def MissingNamesOp : TestOp<"missing_names"> {
305  // CHECK: def __init__(self, i32, _gen_res_1, i64, _gen_arg_0, f32, _gen_arg_2, *, loc=None, ip=None):
306  // CHECK:   operands = []
307  // CHECK:   results = []
308  // CHECK:   attributes = {}
309  // CHECK:   regions = None
310  // CHECK:   operands.append(_gen_arg_0)
311  // CHECK:   operands.append(f32)
312  // CHECK:   operands.append(_gen_arg_2)
313  // CHECK:   results.append(i32)
314  // CHECK:   results.append(_gen_res_1)
315  // CHECK:   results.append(i64)
316  // CHECK:   _ods_successors = None
317  // CHECK:   super().__init__(
318  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
319  // CHECK:     attributes=attributes, results=results, operands=operands,
320  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
321
322  // CHECK: @builtins.property
323  // CHECK: def f32(self):
324  // CHECK:   return self.operation.operands[1]
325  let arguments = (ins I32, F32:$f32, I64);
326
327  // CHECK: @builtins.property
328  // CHECK: def i32(self):
329  // CHECK:   return self.operation.results[0]
330  //
331  // CHECK: @builtins.property
332  // CHECK: def i64(self):
333  // CHECK:   return self.operation.results[2]
334  let results = (outs I32:$i32, AnyFloat, I64:$i64);
335}
336
337// CHECK: def missing_names(i32, _gen_res_1, i64, _gen_arg_0, f32, _gen_arg_2, *, loc=None, ip=None)
338// CHECK:   return MissingNamesOp(i32=i32, _gen_res_1=_gen_res_1, i64=i64, _gen_arg_0=_gen_arg_0, f32=f32, _gen_arg_2=_gen_arg_2, loc=loc, ip=ip).results
339
340// CHECK: @_ods_cext.register_operation(_Dialect)
341// CHECK: class OneOptionalOperandOp(_ods_ir.OpView):
342// CHECK-LABEL: OPERATION_NAME = "test.one_optional_operand"
343// CHECK-NOT: _ODS_OPERAND_SEGMENTS
344// CHECK-NOT: _ODS_RESULT_SEGMENTS
345def OneOptionalOperandOp : TestOp<"one_optional_operand"> {
346  let arguments = (ins AnyType:$non_optional, Optional<AnyType>:$optional);
347  // CHECK: def __init__(self, non_optional, *, optional=None, loc=None, ip=None):
348  // CHECK:   operands = []
349  // CHECK:   results = []
350  // CHECK:   attributes = {}
351  // CHECK:   regions = None
352  // CHECK:   operands.append(non_optional)
353  // CHECK:   if optional is not None: operands.append(optional)
354  // CHECK:   _ods_successors = None
355  // CHECK:   super().__init__(
356  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
357  // CHECK:     attributes=attributes, results=results, operands=operands,
358  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
359
360  // CHECK: @builtins.property
361  // CHECK: def non_optional(self):
362  // CHECK:   return self.operation.operands[0]
363
364  // CHECK: @builtins.property
365  // CHECK: def optional(self):
366  // CHECK:   return None if len(self.operation.operands) < 2 else self.operation.operands[1]
367}
368
369// CHECK: def one_optional_operand(non_optional, *, optional=None, loc=None, ip=None)
370// CHECK:   return OneOptionalOperandOp(non_optional=non_optional, optional=optional, loc=loc, ip=ip)
371
372// CHECK: @_ods_cext.register_operation(_Dialect)
373// CHECK: class OneVariadicOperandOp(_ods_ir.OpView):
374// CHECK-LABEL: OPERATION_NAME = "test.one_variadic_operand"
375// CHECK-NOT: _ODS_OPERAND_SEGMENTS
376// CHECK-NOT: _ODS_RESULT_SEGMENTS
377def OneVariadicOperandOp : TestOp<"one_variadic_operand"> {
378  // CHECK: def __init__(self, non_variadic, variadic, *, loc=None, ip=None):
379  // CHECK:   operands = []
380  // CHECK:   results = []
381  // CHECK:   attributes = {}
382  // CHECK:   regions = None
383  // CHECK:   operands.append(non_variadic)
384  // CHECK:   operands.extend(_get_op_results_or_values(variadic))
385  // CHECK:   _ods_successors = None
386  // CHECK:   super().__init__(
387  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
388  // CHECK:     attributes=attributes, results=results, operands=operands,
389  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
390
391  // CHECK: @builtins.property
392  // CHECK: def non_variadic(self):
393  // CHECK:   return self.operation.operands[0]
394  //
395  // CHECK: @builtins.property
396  // CHECK: def variadic(self):
397  // CHECK:   _ods_variadic_group_length = len(self.operation.operands) - 2 + 1
398  // CHECK:   return self.operation.operands[1:1 + _ods_variadic_group_length]
399  let arguments = (ins AnyType:$non_variadic, Variadic<AnyType>:$variadic);
400}
401
402// CHECK: def one_variadic_operand(non_variadic, variadic, *, loc=None, ip=None)
403// CHECK:   return OneVariadicOperandOp(non_variadic=non_variadic, variadic=variadic, loc=loc, ip=ip)
404
405// CHECK: @_ods_cext.register_operation(_Dialect)
406// CHECK: class OneVariadicResultOp(_ods_ir.OpView):
407// CHECK-LABEL: OPERATION_NAME = "test.one_variadic_result"
408// CHECK-NOT: _ODS_OPERAND_SEGMENTS
409// CHECK-NOT: _ODS_RESULT_SEGMENTS
410def OneVariadicResultOp : TestOp<"one_variadic_result"> {
411  // CHECK: def __init__(self, variadic, non_variadic, *, loc=None, ip=None):
412  // CHECK:   operands = []
413  // CHECK:   results = []
414  // CHECK:   attributes = {}
415  // CHECK:   regions = None
416  // CHECK:   results.extend(variadic)
417  // CHECK:   results.append(non_variadic)
418  // CHECK:   _ods_successors = None
419  // CHECK:   super().__init__(
420  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
421  // CHECK:     attributes=attributes, results=results, operands=operands,
422  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
423
424  // CHECK: @builtins.property
425  // CHECK: def variadic(self):
426  // CHECK:   _ods_variadic_group_length = len(self.operation.results) - 2 + 1
427  // CHECK:   return self.operation.results[0:0 + _ods_variadic_group_length]
428  //
429  // CHECK: @builtins.property
430  // CHECK: def non_variadic(self):
431  // CHECK:   _ods_variadic_group_length = len(self.operation.results) - 2 + 1
432  // CHECK:   return self.operation.results[1 + _ods_variadic_group_length - 1]
433  let results = (outs Variadic<AnyType>:$variadic, AnyType:$non_variadic);
434}
435
436// CHECK: def one_variadic_result(variadic, non_variadic, *, loc=None, ip=None)
437// CHECK:   return _get_op_result_or_op_results(OneVariadicResultOp(variadic=variadic, non_variadic=non_variadic, loc=loc, ip=ip))
438
439// CHECK: @_ods_cext.register_operation(_Dialect)
440// CHECK: class PythonKeywordOp(_ods_ir.OpView):
441// CHECK-LABEL: OPERATION_NAME = "test.python_keyword"
442def PythonKeywordOp : TestOp<"python_keyword"> {
443  // CHECK: def __init__(self, in_, *, loc=None, ip=None):
444  // CHECK:   operands = []
445  // CHECK:   results = []
446  // CHECK:   attributes = {}
447  // CHECK:   regions = None
448  // CHECK:   operands.append(in_)
449  // CHECK:   _ods_successors = None
450  // CHECK:   super().__init__(
451  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
452  // CHECK:     attributes=attributes, results=results, operands=operands,
453  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
454
455  // CHECK: @builtins.property
456  // CHECK: def in_(self):
457  // CHECK:   return self.operation.operands[0]
458  let arguments = (ins AnyType:$in);
459}
460
461// CHECK: def python_keyword(in_, *, loc=None, ip=None)
462// CHECK:   return PythonKeywordOp(in_=in_, loc=loc, ip=ip)
463
464// CHECK-LABEL: OPERATION_NAME = "test.same_results"
465def SameResultsOp : TestOp<"same_results", [SameOperandsAndResultType]> {
466  // CHECK: def __init__(self, in1, in2, *, loc=None, ip=None):
467  // CHECK: operands = []
468  // CHECK: results = []
469  // CHECK: operands.append
470  // CHECK: results.extend([operands[0].type] * 1)
471  let arguments = (ins AnyType:$in1, AnyType:$in2);
472  let results = (outs AnyType:$res);
473}
474
475// CHECK: def same_results(in1, in2, *, loc=None, ip=None)
476// CHECK:   return SameResultsOp(in1=in1, in2=in2, loc=loc, ip=ip)
477
478// CHECK-LABEL: OPERATION_NAME = "test.same_results_variadic"
479def SameResultsVariadicOp : TestOp<"same_results_variadic", [SameOperandsAndResultType]> {
480  // CHECK: def __init__(self, res, in1, in2, *, loc=None, ip=None):
481  let arguments = (ins AnyType:$in1, AnyType:$in2);
482  let results = (outs Variadic<AnyType>:$res);
483}
484
485// CHECK: def same_results_variadic(res, in1, in2, *, loc=None, ip=None)
486// CHECK:   return _get_op_result_or_op_results(SameResultsVariadicOp(res=res, in1=in1, in2=in2, loc=loc, ip=ip))
487
488
489// CHECK: @_ods_cext.register_operation(_Dialect)
490// CHECK: class SameVariadicOperandSizeOp(_ods_ir.OpView):
491// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_operand"
492def SameVariadicOperandSizeOp : TestOp<"same_variadic_operand",
493                                       [SameVariadicOperandSize]> {
494  // CHECK: @builtins.property
495  // CHECK: def variadic1(self):
496  // CHECK:   start, elements_per_group = _ods_equally_sized_accessor(self.operation.operands, 1, 2, 0, 0)
497  // CHECK:   return self.operation.operands[start:start + elements_per_group]
498  //
499  // CHECK: @builtins.property
500  // CHECK: def non_variadic(self):
501  // CHECK:   start, elements_per_group = _ods_equally_sized_accessor(self.operation.operands, 1, 2, 0, 1)
502  // CHECK:   return self.operation.operands[start]
503  //
504  // CHECK: @builtins.property
505  // CHECK: def variadic2(self):
506  // CHECK:   start, elements_per_group = _ods_equally_sized_accessor(self.operation.operands, 1, 2, 1, 1)
507  // CHECK:   return self.operation.operands[start:start + elements_per_group]
508  let arguments = (ins Variadic<AnyType>:$variadic1, AnyType:$non_variadic,
509                   Variadic<AnyType>:$variadic2);
510}
511
512// CHECK: def same_variadic_operand(variadic1, non_variadic, variadic2, *, loc=None, ip=None)
513// CHECK:   return SameVariadicOperandSizeOp(variadic1=variadic1, non_variadic=non_variadic, variadic2=variadic2, loc=loc, ip=ip)
514
515// CHECK: @_ods_cext.register_operation(_Dialect)
516// CHECK: class SameVariadicResultSizeOp(_ods_ir.OpView):
517// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_result"
518def SameVariadicResultSizeOp : TestOp<"same_variadic_result",
519                                      [SameVariadicResultSize]> {
520  // CHECK: @builtins.property
521  // CHECK: def variadic1(self):
522  // CHECK:   start, elements_per_group = _ods_equally_sized_accessor(self.operation.results, 1, 2, 0, 0)
523  // CHECK:   return self.operation.results[start:start + elements_per_group]
524  //
525  // CHECK: @builtins.property
526  // CHECK: def non_variadic(self):
527  // CHECK:   start, elements_per_group = _ods_equally_sized_accessor(self.operation.results, 1, 2, 0, 1)
528  // CHECK:   return self.operation.results[start]
529  //
530  // CHECK: @builtins.property
531  // CHECK: def variadic2(self):
532  // CHECK:   start, elements_per_group = _ods_equally_sized_accessor(self.operation.results, 1, 2, 1, 1)
533  // CHECK:   return self.operation.results[start:start + elements_per_group]
534  let results = (outs Variadic<AnyType>:$variadic1, AnyType:$non_variadic,
535                 Variadic<AnyType>:$variadic2);
536}
537
538// CHECK: def same_variadic_result(variadic1, non_variadic, variadic2, *, loc=None, ip=None)
539// CHECK:   return _get_op_result_or_op_results(SameVariadicResultSizeOp(variadic1=variadic1, non_variadic=non_variadic, variadic2=variadic2, loc=loc, ip=ip))
540
541// CHECK: @_ods_cext.register_operation(_Dialect)
542// CHECK: class SimpleOp(_ods_ir.OpView):
543// CHECK-LABEL: OPERATION_NAME = "test.simple"
544def SimpleOp : TestOp<"simple"> {
545  // CHECK: def __init__(self, i64, f64, i32, f32, *, loc=None, ip=None):
546  // CHECK:   operands = []
547  // CHECK:   results = []
548  // CHECK:   attributes = {}
549  // CHECK:   regions = None
550  // CHECK:   operands.append(i32)
551  // CHECK:   operands.append(f32)
552  // CHECK:   results.append(i64)
553  // CHECK:   results.append(f64)
554  // CHECK:   _ods_successors = None
555  // CHECK:   super().__init__(
556  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
557  // CHECK:     attributes=attributes, results=results, operands=operands,
558  // CHECK:     successors=_ods_successors, regions=regions, loc=loc, ip=ip)
559
560  // CHECK: @builtins.property
561  // CHECK: def i32(self):
562  // CHECK:   return self.operation.operands[0]
563  //
564  // CHECK: @builtins.property
565  // CHECK: def f32(self):
566  // CHECK:   return self.operation.operands[1]
567  let arguments = (ins I32:$i32, F32:$f32);
568
569  // CHECK: @builtins.property
570  // CHECK: def i64(self):
571  // CHECK:   return self.operation.results[0]
572  //
573  // CHECK: @builtins.property
574  // CHECK: def f64(self):
575  // CHECK:   return self.operation.results[1]
576  let results = (outs I64:$i64, AnyFloat:$f64);
577}
578
579// CHECK: def simple(i64, f64, i32, f32, *, loc=None, ip=None)
580// CHECK:   return SimpleOp(i64=i64, f64=f64, i32=i32, f32=f32, loc=loc, ip=ip).results
581
582// CHECK: class VariadicAndNormalRegionOp(_ods_ir.OpView):
583// CHECK-LABEL: OPERATION_NAME = "test.variadic_and_normal_region"
584def VariadicAndNormalRegionOp : TestOp<"variadic_and_normal_region"> {
585  // CHECK:  def __init__(self, num_variadic, *, loc=None, ip=None):
586  // CHECK:    operands = []
587  // CHECK:    results = []
588  // CHECK:    attributes = {}
589  // CHECK:    regions = None
590  // CHECK:    _ods_successors = None
591  // CHECK:    regions = 2 + num_variadic
592  // CHECK:    super().__init__(
593  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
594  // CHECK:      attributes=attributes, results=results, operands=operands,
595  // CHECK:      successors=_ods_successors, regions=regions, loc=loc, ip=ip)
596  let regions = (region AnyRegion:$region, AnyRegion, VariadicRegion<AnyRegion>:$variadic);
597
598  // CHECK:  @builtins.property
599  // CHECK:  def region(self):
600  // CHECK:    return self.regions[0]
601
602  // CHECK:  @builtins.property
603  // CHECK:  def variadic(self):
604  // CHECK:    return self.regions[2:]
605}
606
607// CHECK: def variadic_and_normal_region(num_variadic, *, loc=None, ip=None)
608// CHECK:   return VariadicAndNormalRegionOp(num_variadic=num_variadic, loc=loc, ip=ip)
609
610// CHECK: class VariadicRegionOp(_ods_ir.OpView):
611// CHECK-LABEL: OPERATION_NAME = "test.variadic_region"
612def VariadicRegionOp : TestOp<"variadic_region"> {
613  // CHECK:  def __init__(self, num_variadic, *, loc=None, ip=None):
614  // CHECK:    operands = []
615  // CHECK:    results = []
616  // CHECK:    attributes = {}
617  // CHECK:    regions = None
618  // CHECK:    _ods_successors = None
619  // CHECK:    regions = 0 + num_variadic
620  // CHECK:    super().__init__(
621  // CHECK:     self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS
622  // CHECK:      attributes=attributes, results=results, operands=operands,
623  // CHECK:      successors=_ods_successors, regions=regions, loc=loc, ip=ip)
624  let regions = (region VariadicRegion<AnyRegion>:$Variadic);
625
626  // CHECK:  @builtins.property
627  // CHECK:  def Variadic(self):
628  // CHECK:    return self.regions[0:]
629}
630
631// CHECK: def variadic_region(num_variadic, *, loc=None, ip=None)
632// CHECK:   return VariadicRegionOp(num_variadic=num_variadic, loc=loc, ip=ip)
633
634// CHECK: @_ods_cext.register_operation(_Dialect)
635// CHECK: class WithSpecialCharactersOp(_ods_ir.OpView):
636// CHECK-LABEL: OPERATION_NAME = "test.123with--special.characters"
637def WithSpecialCharactersOp : TestOp<"123with--special.characters"> {
638}
639
640// CHECK: def _123with__special_characters(*, loc=None, ip=None)
641// CHECK:   return WithSpecialCharactersOp(loc=loc, ip=ip)
642
643// CHECK: @_ods_cext.register_operation(_Dialect)
644// CHECK: class WithSuccessorsOp(_ods_ir.OpView):
645// CHECK-LABEL: OPERATION_NAME = "test.with_successors"
646def WithSuccessorsOp : TestOp<"with_successors"> {
647  // CHECK-NOT:  _ods_successors = None
648  // CHECK:      _ods_successors = []
649  // CHECK-NEXT: _ods_successors.append(successor)
650  // CHECK-NEXT: _ods_successors.extend(successors)
651  let successors = (successor AnySuccessor:$successor,
652                              VariadicSuccessor<AnySuccessor>:$successors);
653}
654
655// CHECK: def with_successors(successor, successors, *, loc=None, ip=None)
656// CHECK:   return WithSuccessorsOp(successor=successor, successors=successors, loc=loc, ip=ip)
657