1//===-- python_test_ops.td - Python test Op definitions ----*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef PYTHON_TEST_OPS
10#define PYTHON_TEST_OPS
11
12include "mlir/Bindings/Python/Attributes.td"
13include "mlir/IR/OpBase.td"
14include "mlir/Interfaces/InferTypeOpInterface.td"
15
16def Python_Test_Dialect : Dialect {
17  let name = "python_test";
18  let cppNamespace = "python_test";
19}
20
21class TestType<string name, string typeMnemonic>
22    : TypeDef<Python_Test_Dialect, name> {
23  let mnemonic = typeMnemonic;
24}
25
26class TestAttr<string name, string attrMnemonic>
27    : AttrDef<Python_Test_Dialect, name> {
28  let mnemonic = attrMnemonic;
29}
30
31class TestOp<string mnemonic, list<Trait> traits = []>
32    : Op<Python_Test_Dialect, mnemonic, traits>;
33
34//===----------------------------------------------------------------------===//
35// Type definitions.
36//===----------------------------------------------------------------------===//
37
38def TestType : TestType<"TestType", "test_type">;
39
40//===----------------------------------------------------------------------===//
41// Attribute definitions.
42//===----------------------------------------------------------------------===//
43
44def TestAttr : TestAttr<"TestAttr", "test_attr">;
45
46//===----------------------------------------------------------------------===//
47// Operation definitions.
48//===----------------------------------------------------------------------===//
49
50def AttributedOp : TestOp<"attributed_op"> {
51  let arguments = (ins I32Attr:$mandatory_i32,
52                   OptionalAttr<I32Attr>:$optional_i32,
53                   UnitAttr:$unit);
54}
55
56def PropertyOp : TestOp<"property_op"> {
57  let arguments = (ins I32Attr:$property,
58                   I32:$idx);
59}
60
61def DummyOp : TestOp<"dummy_op"> {
62}
63
64def InferResultsOp : TestOp<"infer_results_op", [InferTypeOpInterface]> {
65  let arguments = (ins);
66  let results = (outs AnyInteger:$single, AnyInteger:$doubled);
67
68  let extraClassDeclaration = [{
69    static ::mlir::LogicalResult inferReturnTypes(
70      ::mlir::MLIRContext *context, ::llvm::Optional<::mlir::Location> location,
71      ::mlir::ValueRange operands, ::mlir::DictionaryAttr attributes,
72      ::mlir::RegionRange regions,
73      ::llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) {
74      ::mlir::Builder b(context);
75      inferredReturnTypes.push_back(b.getI32Type());
76      inferredReturnTypes.push_back(b.getI64Type());
77      return ::mlir::success();
78    }
79  }];
80}
81
82// If all result types are buildable, the InferTypeOpInterface is implied and is
83// autogenerated by C++ ODS.
84def InferResultsImpliedOp : TestOp<"infer_results_implied_op"> {
85  let results = (outs I32:$integer, F64:$flt, Index:$index);
86}
87
88def SameOperandAndResultTypeOp : TestOp<"same_operand_and_result_type_op",
89                                        [SameOperandsAndResultType]> {
90  let arguments = (ins Variadic<AnyType>);
91  let results = (outs AnyType:$one, AnyType:$two);
92}
93
94def FirstAttrDeriveTypeAttrOp : TestOp<"first_attr_derive_type_attr_op",
95                               [FirstAttrDerivedResultType]> {
96  let arguments = (ins AnyType:$input, TypeAttr:$type);
97  let results = (outs AnyType:$one, AnyType:$two);
98}
99
100def FirstAttrDeriveAttrOp : TestOp<"first_attr_derive_attr_op",
101                               [FirstAttrDerivedResultType]> {
102  let arguments = (ins AnyAttr:$iattr);
103  let results = (outs AnyType:$one, AnyType:$two, AnyType:$three);
104}
105
106def OptionalOperandOp : TestOp<"optional_operand_op"> {
107  let arguments = (ins Optional<AnyType>:$input);
108  let results = (outs I32:$result);
109}
110
111#endif // PYTHON_TEST_OPS
112