xref: /llvm-project/mlir/test/lib/Dialect/Test/TestInterfaces.td (revision 0c63122713c2d719789aef4bdfaf4e0b29c3b79e)
1//===-- TestInterfaces.td - Test dialect interfaces --------*- 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 MLIR_TEST_DIALECT_TEST_INTERFACES
10#define MLIR_TEST_DIALECT_TEST_INTERFACES
11
12include "mlir/IR/OpBase.td"
13include "mlir/Interfaces/SideEffectInterfaceBase.td"
14
15// A set of type interfaces used to test interface inheritance.
16def TestBaseTypeInterfacePrintTypeA : TypeInterface<"TestBaseTypeInterfacePrintTypeA"> {
17  let cppNamespace = "::test";
18  let methods = [
19    InterfaceMethod<"Prints the type name.",
20      "void", "printTypeA", (ins "::mlir::Location":$loc), [{
21        emitRemark(loc) << $_type << " - TestA";
22      }]
23    >
24  ];
25}
26def TestBaseTypeInterfacePrintTypeB
27  : TypeInterface<"TestBaseTypeInterfacePrintTypeB", [TestBaseTypeInterfacePrintTypeA]> {
28  let cppNamespace = "::test";
29  let methods = [
30    InterfaceMethod<"Prints the type name.",
31      "void", "printTypeB", (ins "::mlir::Location":$loc),
32      [{}], /*defaultImplementation=*/[{
33        emitRemark(loc) << $_type << " - TestB";
34      }]
35    >
36  ];
37}
38
39// A type interface used to test the ODS generation of type interfaces.
40def TestTypeInterface
41  : TypeInterface<"TestTypeInterface", [TestBaseTypeInterfacePrintTypeB]> {
42  let cppNamespace = "::test";
43  let methods = [
44    InterfaceMethod<"Prints the type name.",
45      "void", "printTypeC", (ins "::mlir::Location":$loc)
46    >,
47    // It should be possible to use the interface type name as result type
48    // as well as in the implementation.
49    InterfaceMethod<"Prints the type name and returns the type as interface.",
50      "TestTypeInterface", "printTypeRet", (ins "::mlir::Location":$loc),
51      [{}], /*defaultImplementation=*/[{
52        emitRemark(loc) << $_type << " - TestRet";
53        return $_type;
54      }]
55    >,
56  ];
57  let extraClassDeclaration = [{
58    /// Prints the type name.
59    void printTypeD(::mlir::Location loc) const {
60      emitRemark(loc) << *this << " - TestD";
61    }
62  }];
63  let extraTraitClassDeclaration = [{
64    /// Prints the type name.
65    void printTypeE(::mlir::Location loc) const {
66      emitRemark(loc) << $_type << " - TestE";
67    }
68  }];
69}
70
71def TestExternalTypeInterface : TypeInterface<"TestExternalTypeInterface"> {
72  let cppNamespace = "::mlir";
73  let methods = [
74    InterfaceMethod<"Returns the bitwidth of the type plus 'arg'.",
75      "unsigned", "getBitwidthPlusArg", (ins "unsigned":$arg)>,
76    StaticInterfaceMethod<"Returns some value plus 'arg'.",
77      "unsigned", "staticGetSomeValuePlusArg", (ins "unsigned":$arg)>,
78    InterfaceMethod<"Returns the argument doubled.",
79      "unsigned", "getBitwidthPlusDoubleArgument", (ins "unsigned":$arg), "",
80      "return $_type.getIntOrFloatBitWidth() + 2 * arg;">,
81    StaticInterfaceMethod<"Returns the argument.",
82      "unsigned", "staticGetArgument", (ins "unsigned":$arg), "",
83      "return arg;">,
84  ];
85}
86
87def TestExternalFallbackTypeInterface
88    : TypeInterface<"TestExternalFallbackTypeInterface"> {
89  let cppNamespace = "::mlir";
90  let methods = [
91    InterfaceMethod<"Returns the bitwidth of the given integer type.",
92      "unsigned", "getBitwidth", (ins), "", "return $_type.getWidth();">,
93  ];
94}
95
96def TestExternalAttrInterface : AttrInterface<"TestExternalAttrInterface"> {
97  let cppNamespace = "::mlir";
98  let methods = [
99    InterfaceMethod<"Gets the dialect pointer.", "const ::mlir::Dialect *",
100      "getDialectPtr">,
101    StaticInterfaceMethod<"Returns some number.", "int", "getSomeNumber">,
102  ];
103}
104
105def TestExternalOpInterface : OpInterface<"TestExternalOpInterface"> {
106  let cppNamespace = "::mlir";
107  let methods = [
108    InterfaceMethod<"Returns the length of the operation name plus arg.",
109      "unsigned", "getNameLengthPlusArg", (ins "unsigned":$arg)>,
110    StaticInterfaceMethod<
111      "Returns the length of the operation name plus arg twice.", "unsigned",
112      "getNameLengthPlusArgTwice", (ins "unsigned":$arg)>,
113    InterfaceMethod<
114      "Returns the length of the product of the operation name and arg.",
115      "unsigned", "getNameLengthTimesArg", (ins "unsigned":$arg), "",
116      "return arg * $_op->getName().getStringRef().size();">,
117    StaticInterfaceMethod<"Returns the length of the operation name minus arg.",
118      "unsigned", "getNameLengthMinusArg", (ins "unsigned":$arg), "",
119      "return ConcreteOp::getOperationName().size() - arg;">,
120  ];
121}
122
123def TestEffectOpInterface
124    : EffectOpInterfaceBase<"TestEffectOpInterface",
125                            "::mlir::TestEffects::Effect"> {
126  let cppNamespace = "::mlir";
127}
128
129class TestEffect<string effectName>
130    : SideEffect<TestEffectOpInterface, effectName, DefaultResource, 0,
131                 PartialEffect>;
132
133class TestEffects<list<TestEffect> effects = []>
134   : SideEffectsTraitBase<TestEffectOpInterface, effects>;
135
136def TestConcreteEffect : TestEffect<"TestEffects::Concrete">;
137
138def TestOptionallyImplementedOpInterface
139    : OpInterface<"TestOptionallyImplementedOpInterface"> {
140  let cppNamespace = "::mlir";
141
142  let methods = [
143    InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
144  ];
145
146  let extraClassOf = [{
147    return $_op.getImplementsInterface();
148  }];
149}
150
151def TestOptionallyImplementedAttrInterface
152    : AttrInterface<"TestOptionallyImplementedAttrInterface"> {
153  let cppNamespace = "::mlir";
154
155  let methods = [
156    InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
157  ];
158
159  let extraClassOf = [{
160    return $_attr.getImplementsInterface();
161  }];
162}
163
164def TestOptionallyImplementedTypeInterface
165    : TypeInterface<"TestOptionallyImplementedTypeInterface"> {
166  let cppNamespace = "::mlir";
167
168  let methods = [
169    InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
170  ];
171
172  let extraClassOf = [{
173    return $_type.getImplementsInterface();
174  }];
175}
176
177#endif // MLIR_TEST_DIALECT_TEST_INTERFACES
178