1 //===-- TestInterfaces.td - Test dialect interfaces --------*- tablegen -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef MLIR_TEST_DIALECT_TEST_INTERFACES
10 #define MLIR_TEST_DIALECT_TEST_INTERFACES
12 include "mlir/IR/OpBase.td"
13 include "mlir/Interfaces/SideEffectInterfaceBase.td"
15 // A set of type interfaces used to test interface inheritance.
16 def TestBaseTypeInterfacePrintTypeA : TypeInterface<"TestBaseTypeInterfacePrintTypeA"> {
17 let cppNamespace = "::test";
19 InterfaceMethod<"Prints the type name.",
20 "void", "printTypeA", (ins "::mlir::Location":$loc), [{
21 emitRemark(loc) << $_type << " - TestA";
26 def TestBaseTypeInterfacePrintTypeB
27 : TypeInterface<"TestBaseTypeInterfacePrintTypeB", [TestBaseTypeInterfacePrintTypeA]> {
28 let cppNamespace = "::test";
30 InterfaceMethod<"Prints the type name.",
31 "void", "printTypeB", (ins "::mlir::Location":$loc),
32 [{}], /*defaultImplementation=*/[{
33 emitRemark(loc) << $_type << " - TestB";
39 // A type interface used to test the ODS generation of type interfaces.
41 : TypeInterface<"TestTypeInterface", [TestBaseTypeInterfacePrintTypeB]> {
42 let cppNamespace = "::test";
44 InterfaceMethod<"Prints the type name.",
45 "void", "printTypeC", (ins "::mlir::Location":$loc)
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";
57 let extraClassDeclaration = [{
58 /// Prints the type name.
59 void printTypeD(::mlir::Location loc) const {
60 emitRemark(loc) << *this << " - TestD";
63 let extraTraitClassDeclaration = [{
64 /// Prints the type name.
65 void printTypeE(::mlir::Location loc) const {
66 emitRemark(loc) << $_type << " - TestE";
71 def TestExternalTypeInterface : TypeInterface<"TestExternalTypeInterface"> {
72 let cppNamespace = "::mlir";
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), "",
87 def TestExternalFallbackTypeInterface
88 : TypeInterface<"TestExternalFallbackTypeInterface"> {
89 let cppNamespace = "::mlir";
91 InterfaceMethod<"Returns the bitwidth of the given integer type.",
92 "unsigned", "getBitwidth", (ins), "", "return $_type.getWidth();">,
96 def TestExternalAttrInterface : AttrInterface<"TestExternalAttrInterface"> {
97 let cppNamespace = "::mlir";
99 InterfaceMethod<"Gets the dialect pointer.", "const ::mlir::Dialect *",
101 StaticInterfaceMethod<"Returns some number.", "int", "getSomeNumber">,
105 def TestExternalOpInterface : OpInterface<"TestExternalOpInterface"> {
106 let cppNamespace = "::mlir";
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)>,
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;">,
123 def TestEffectOpInterface
124 : EffectOpInterfaceBase<"TestEffectOpInterface",
125 "::mlir::TestEffects::Effect"> {
126 let cppNamespace = "::mlir";
129 class TestEffect<string effectName>
130 : SideEffect<TestEffectOpInterface, effectName, DefaultResource, 0,
133 class TestEffects<list<TestEffect> effects = []>
134 : SideEffectsTraitBase<TestEffectOpInterface, effects>;
136 def TestConcreteEffect : TestEffect<"TestEffects::Concrete">;
138 def TestOptionallyImplementedOpInterface
139 : OpInterface<"TestOptionallyImplementedOpInterface"> {
140 let cppNamespace = "::mlir";
143 InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
146 let extraClassOf = [{
147 return $_op.getImplementsInterface();
151 def TestOptionallyImplementedAttrInterface
152 : AttrInterface<"TestOptionallyImplementedAttrInterface"> {
153 let cppNamespace = "::mlir";
156 InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
159 let extraClassOf = [{
160 return $_attr.getImplementsInterface();
164 def TestOptionallyImplementedTypeInterface
165 : TypeInterface<"TestOptionallyImplementedTypeInterface"> {
166 let cppNamespace = "::mlir";
169 InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
172 let extraClassOf = [{
173 return $_type.getImplementsInterface();
177 #endif // MLIR_TEST_DIALECT_TEST_INTERFACES