1 // RUN: mlir-tblgen -gen-op-interface-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
2 // RUN: mlir-tblgen -gen-op-interface-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF
3 // RUN: mlir-tblgen -gen-op-decls -I %S/../../include %s | FileCheck %s --check-prefix=OP_DECL
4 // RUN: mlir-tblgen -gen-op-interface-docs -I %S/../../include %s | FileCheck %s --check-prefix=DOCS
6 include "mlir/IR/OpBase.td"
8 def ExtraClassOfInterface : OpInterface<"ExtraClassOfInterface"> {
9 let extraClassOf = "return $_op->someOtherMethod();";
12 // DECL: class ExtraClassOfInterface
13 // DECL: static bool classof(::mlir::Operation * base) {
14 // DECL-NEXT: auto* interface = getInterfaceFor(base);
15 // DECL-NEXT: if (!interface)
16 // DECL-NEXT: return false;
17 // DECL-NEXT: ExtraClassOfInterface odsInterfaceInstance(base, interface);
18 // DECL-NEXT: return odsInterfaceInstance->someOtherMethod();
21 def ExtraShardDeclsInterface : OpInterface<"ExtraShardDeclsInterface"> {
22 let extraSharedClassDeclaration = [{
23 bool sharedMethodDeclaration() {
24 return $_op.someOtherMethod();
29 // DECL: class ExtraShardDeclsInterface
30 // DECL: bool sharedMethodDeclaration() {
31 // DECL-NEXT: return (*this).someOtherMethod();
34 // DECL: struct ExtraShardDeclsInterfaceTrait
35 // DECL: bool sharedMethodDeclaration() {
36 // DECL-NEXT: return (*static_cast<ConcreteOp *>(this)).someOtherMethod();
39 def TestInheritanceMultiBaseInterface : OpInterface<"TestInheritanceMultiBaseInterface"> {
42 /*desc=*/[{some function comment}],
45 /*args=*/(ins "int":$input)
50 def TestInheritanceBaseInterface : OpInterface<"TestInheritanceBaseInterface", [TestInheritanceMultiBaseInterface]> {
53 /*desc=*/[{some function comment}],
56 /*args=*/(ins "int":$input)
60 def TestInheritanceMiddleBaseInterface
61 : OpInterface<"TestInheritanceMiddleBaseInterface", [TestInheritanceBaseInterface]> {
64 /*desc=*/[{some function comment}],
67 /*args=*/(ins "int":$input)
71 def TestInheritanceZDerivedInterface
72 : OpInterface<"TestInheritanceZDerivedInterface", [TestInheritanceMiddleBaseInterface]>;
74 // DECL: class TestInheritanceZDerivedInterface
75 // DECL: struct Concept {
76 // DECL: const TestInheritanceMultiBaseInterface::Concept *implTestInheritanceMultiBaseInterface = nullptr;
77 // DECL-NOT: const TestInheritanceMultiBaseInterface::Concept
78 // DECL: const TestInheritanceBaseInterface::Concept *implTestInheritanceBaseInterface = nullptr;
79 // DECL: const TestInheritanceMiddleBaseInterface::Concept *implTestInheritanceMiddleBaseInterface = nullptr;
81 // DECL: void initializeInterfaceConcept(::mlir::detail::InterfaceMap &interfaceMap) {
82 // DECL: implTestInheritanceBaseInterface = interfaceMap.lookup<TestInheritanceBaseInterface>();
83 // DECL: assert(implTestInheritanceBaseInterface && "`TestInheritanceZDerivedInterface` expected its base interface `TestInheritanceBaseInterface` to be registered");
84 // DECL: implTestInheritanceMiddleBaseInterface = interfaceMap.lookup<TestInheritanceMiddleBaseInterface>();
85 // DECL: assert(implTestInheritanceMiddleBaseInterface
88 // DECL: //===----------------------------------------------------------------===//
89 // DECL: // Inherited from TestInheritanceBaseInterface
90 // DECL: //===----------------------------------------------------------------===//
91 // DECL: operator TestInheritanceBaseInterface () const {
92 // DECL: return TestInheritanceBaseInterface(*this, getImpl()->implTestInheritanceBaseInterface);
94 // DECL: /// some function comment
95 // DECL: int foo(int input);
97 // DECL: //===----------------------------------------------------------------===//
98 // DECL: // Inherited from TestInheritanceMiddleBaseInterface
99 // DECL: //===----------------------------------------------------------------===//
100 // DECL: operator TestInheritanceMiddleBaseInterface () const {
101 // DECL: return TestInheritanceMiddleBaseInterface(*this, getImpl()->implTestInheritanceMiddleBaseInterface);
103 // DECL: /// some function comment
104 // DECL: int bar(int input);
106 // DEF: int TestInheritanceZDerivedInterface::foo(int input) {
107 // DEF-NEXT: getImpl()->implTestInheritanceBaseInterface->foo(getImpl()->implTestInheritanceBaseInterface, getOperation(), input);
109 // DEF: int TestInheritanceZDerivedInterface::bar(int input) {
110 // DEF-NEXT: return getImpl()->implTestInheritanceMiddleBaseInterface->bar(getImpl()->implTestInheritanceMiddleBaseInterface, getOperation(), input);
112 def TestOpInterface : OpInterface<"TestOpInterface"> {
113 let description = [{some op interface description}];
117 /*desc=*/[{some function comment}],
119 /*methodName=*/"foo",
120 /*args=*/(ins "int":$input)
123 /*desc=*/[{some function comment}],
125 /*methodName=*/"body_foo",
126 /*args=*/(ins "int":$input),
127 /*body=*/[{ return 0; }]
130 /*desc=*/[{some function comment}],
132 /*methodName=*/"default_foo",
133 /*args=*/(ins "int":$input),
135 /*defaultBody=*/[{ return 0; }]
140 def TestOpInterfaceVerify : OpInterface<"TestOpInterfaceVerify"> {
146 def TestOpInterfaceVerifyRegion : OpInterface<"TestOpInterfaceVerifyRegion"> {
150 let verifyWithRegions = 1;
153 // Define Ops with TestOpInterface and
154 // DeclareOpInterfaceMethods<TestOpInterface> traits to check that there
155 // are not duplicated C++ classes generated.
156 def TestDialect : Dialect {
160 def OpInterfaceOp : Op<TestDialect, "op_interface_op", [TestOpInterface]>;
162 def OpInterfaceInterfacesOp : Op<TestDialect, "op_inherit_interface_op", [TestInheritanceZDerivedInterface]>;
164 def DeclareMethodsOp : Op<TestDialect, "declare_methods_op",
165 [DeclareOpInterfaceMethods<TestOpInterface>]>;
167 def DeclareMethodsWithDefaultOp : Op<TestDialect, "declare_methods_op",
168 [DeclareOpInterfaceMethods<TestOpInterface, ["default_foo"]>]>;
170 // DECL-LABEL: TestOpInterfaceInterfaceTraits
171 // DECL: class TestOpInterface : public ::mlir::OpInterface<TestOpInterface, detail::TestOpInterfaceInterfaceTraits>
173 // DECL: /// some function comment
174 // DECL: int foo(int input);
176 // DECL-LABEL: struct TestOpInterfaceVerifyTrait
179 // DECL-LABEL: struct TestOpInterfaceVerifyRegionTrait
180 // DECL: verifyRegionTrait
182 // Method implementations come last, after all class definitions.
183 // DECL: template<typename ConcreteOp>
184 // DECL: int detail::TestOpInterfaceInterfaceTraits::Model<ConcreteOp>::foo
186 // OP_DECL-LABEL: class DeclareMethodsOp : public
187 // OP_DECL: int foo(int input);
188 // OP_DECL-NOT: int default_foo(int input);
190 // OP_DECL-LABEL: class DeclareMethodsWithDefaultOp : public
191 // OP_DECL: int foo(int input);
192 // OP_DECL: int default_foo(int input);
194 // OP_DECL: class OpInterfaceInterfacesOp :
195 // OP_DECL-SAME: TestInheritanceBaseInterface::Trait, TestInheritanceMiddleBaseInterface::Trait, TestInheritanceZDerivedInterface::Trait
197 // DOCS-LABEL: {{^}}## TestOpInterface (`TestOpInterface`)
198 // DOCS: some op interface description
200 // DOCS: {{^}}### Methods:
202 // DOCS: {{^}}#### `foo`
203 // DOCS: some function comment
205 // DOCS: {{^}}#### `body_foo`
206 // DOCS: some function comment
208 // DOCS: {{^}}#### `default_foo`
209 // DOCS: some function comment