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 type interface used to test the ODS generation of type interfaces.
16def TestTypeInterface : TypeInterface<"TestTypeInterface"> {
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    InterfaceMethod<"Prints the type name.",
25      "void", "printTypeB", (ins "::mlir::Location":$loc),
26      [{}], /*defaultImplementation=*/[{
27        emitRemark(loc) << $_type << " - TestB";
28      }]
29    >,
30    InterfaceMethod<"Prints the type name.",
31      "void", "printTypeC", (ins "::mlir::Location":$loc)
32    >,
33    // It should be possible to use the interface type name as result type
34    // as well as in the implementation.
35    InterfaceMethod<"Prints the type name and returns the type as interface.",
36      "TestTypeInterface", "printTypeRet", (ins "::mlir::Location":$loc),
37      [{}], /*defaultImplementation=*/[{
38        emitRemark(loc) << $_type << " - TestRet";
39        return $_type;
40      }]
41    >,
42  ];
43  let extraClassDeclaration = [{
44    /// Prints the type name.
45    void printTypeD(::mlir::Location loc) const {
46      emitRemark(loc) << *this << " - TestD";
47    }
48  }];
49  let extraTraitClassDeclaration = [{
50    /// Prints the type name.
51    void printTypeE(::mlir::Location loc) const {
52      emitRemark(loc) << $_type << " - TestE";
53    }
54  }];
55}
56
57def TestExternalTypeInterface : TypeInterface<"TestExternalTypeInterface"> {
58  let cppNamespace = "::mlir";
59  let methods = [
60    InterfaceMethod<"Returns the bitwidth of the type plus 'arg'.",
61      "unsigned", "getBitwidthPlusArg", (ins "unsigned":$arg)>,
62    StaticInterfaceMethod<"Returns some value plus 'arg'.",
63      "unsigned", "staticGetSomeValuePlusArg", (ins "unsigned":$arg)>,
64    InterfaceMethod<"Returns the argument doubled.",
65      "unsigned", "getBitwidthPlusDoubleArgument", (ins "unsigned":$arg), "",
66      "return $_type.getIntOrFloatBitWidth() + 2 * arg;">,
67    StaticInterfaceMethod<"Returns the argument.",
68      "unsigned", "staticGetArgument", (ins "unsigned":$arg), "",
69      "return arg;">,
70  ];
71}
72
73def TestExternalFallbackTypeInterface
74    : TypeInterface<"TestExternalFallbackTypeInterface"> {
75  let cppNamespace = "::mlir";
76  let methods = [
77    InterfaceMethod<"Returns the bitwidth of the given integer type.",
78      "unsigned", "getBitwidth", (ins), "", "return $_type.getWidth();">,
79  ];
80}
81
82def TestExternalAttrInterface : AttrInterface<"TestExternalAttrInterface"> {
83  let cppNamespace = "::mlir";
84  let methods = [
85    InterfaceMethod<"Gets the dialect pointer.", "const ::mlir::Dialect *",
86      "getDialectPtr">,
87    StaticInterfaceMethod<"Returns some number.", "int", "getSomeNumber">,
88  ];
89}
90
91def TestExternalOpInterface : OpInterface<"TestExternalOpInterface"> {
92  let cppNamespace = "::mlir";
93  let methods = [
94    InterfaceMethod<"Returns the length of the operation name plus arg.",
95      "unsigned", "getNameLengthPlusArg", (ins "unsigned":$arg)>,
96    StaticInterfaceMethod<
97      "Returns the length of the operation name plus arg twice.", "unsigned",
98      "getNameLengthPlusArgTwice", (ins "unsigned":$arg)>,
99    InterfaceMethod<
100      "Returns the length of the product of the operation name and arg.",
101      "unsigned", "getNameLengthTimesArg", (ins "unsigned":$arg), "",
102      "return arg * $_op->getName().getStringRef().size();">,
103    StaticInterfaceMethod<"Returns the length of the operation name minus arg.",
104      "unsigned", "getNameLengthMinusArg", (ins "unsigned":$arg), "",
105      "return ConcreteOp::getOperationName().size() - arg;">,
106  ];
107}
108
109def TestEffectOpInterface
110    : EffectOpInterfaceBase<"TestEffectOpInterface",
111                            "::mlir::TestEffects::Effect"> {
112  let cppNamespace = "::mlir";
113}
114
115class TestEffect<string effectName>
116    : SideEffect<TestEffectOpInterface, effectName, DefaultResource>;
117
118class TestEffects<list<TestEffect> effects = []>
119   : SideEffectsTraitBase<TestEffectOpInterface, effects>;
120
121def TestConcreteEffect : TestEffect<"TestEffects::Concrete">;
122
123#endif // MLIR_TEST_DIALECT_TEST_INTERFACES
124