Skip to content

Commit ba62b7d

Browse files
committed
Generate cpp comments for Attr/Op/Type Interface
Also add test cases for Attr and Enum
1 parent 49a8491 commit ba62b7d

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

mlir/test/mlir-tblgen/cpp-class-comments.td

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// RUN: mlir-tblgen -gen-dialect-decls -I %S/../../include %s | FileCheck %s --check-prefix=DIALECT
22
// RUN: mlir-tblgen -gen-op-decls -I %S/../../include %s | FileCheck %s --check-prefix=OP
33
// RUN: mlir-tblgen -gen-typedef-decls -I %S/../../include %s | FileCheck %s --check-prefix=TYPE
4-
4+
// RUN: mlir-tblgen -gen-attrdef-decls -I %S/../../include %s | FileCheck %s --check-prefix=ATTR
5+
// RUN: mlir-tblgen -gen-attr-interface-decls -I %S/../../include %s | FileCheck %s --check-prefix=ATTR-INTERFACE
6+
// RUN: mlir-tblgen -gen-op-interface-decls -I %S/../../include %s | FileCheck %s --check-prefix=OP-INTERFACE
7+
// RUN: mlir-tblgen -gen-type-interface-decls -I %S/../../include %s | FileCheck %s --check-prefix=TYPE-INTERFACE
8+
// RUN: mlir-tblgen -gen-enum-decls -I %S/../../include %s | FileCheck %s --check-prefix=ENUM
9+
10+
include "mlir/IR/AttrTypeBase.td"
11+
include "mlir/IR/EnumAttr.td"
512
include "mlir/IR/OpBase.td"
613

714
// check dialect with summary and description
@@ -52,10 +59,7 @@ def A_SomeOp2 : Op<A_Dialect, "some_op2", []>{
5259
// OP-NEXT: class SomeOp2;
5360
}
5461

55-
class AType<string name> : TypeDef<A_Dialect, name> { }
56-
57-
58-
def A_TensorType : AType<"Tensor"> {
62+
def A_TensorType : TypeDef<A_Dialect,"Tensor"> {
5963
let typeName = "a.simple_a_tensor";
6064

6165
let summary = "Tensor Type A summary";
@@ -72,3 +76,64 @@ def A_TensorType : AType<"Tensor"> {
7276
// TYPE-NEXT: /// Tensor Type A description
7377
// TYPE-NEXT: class TensorType;
7478
}
79+
80+
def A_SimpleAttr : AttrDef<A_Dialect,"SimpleA"> {
81+
let attrName = "a.simple_attr";
82+
let summary = "Simple Attr A summary";
83+
84+
let description = [{
85+
Simple Attr A description
86+
}];
87+
// ATTR: /// Simple Attr A summary
88+
// ATTR-NEXT: /// Simple Attr A description
89+
// ATTR-NEXT: class SimpleAAttr;
90+
}
91+
92+
def EncodingTrait : AttrInterface<"EncodingTrait"> {
93+
let cppNamespace = "mlir::a::traits";
94+
let description = [{
95+
Common trait for all layouts.
96+
}];
97+
let methods = [
98+
];
99+
// ATTR-INTERFACE: namespace mlir
100+
// ATTR-INTERFACE-NEXT: namespace a
101+
// ATTR-INTERFACE-NEXT: namespace traits
102+
// ATTR-INTERFACE-NEXT: /// Common trait for all layouts.
103+
// ATTR-INTERFACE-NEXT: class EncodingTrait;
104+
}
105+
106+
def SimpleEncodingTrait : AttrInterface<"SimpleEncodingTrait"> {
107+
let cppNamespace = "a::traits";
108+
// ATTR-INTERFACE: namespace a {
109+
// ATTR-INTERFACE-NEXT: namespace traits {
110+
// ATTR-INTERFACE-NEXT: class SimpleEncodingTrait;
111+
}
112+
113+
def SimpleOpInterface : OpInterface<"SimpleOpInterface"> {
114+
let cppNamespace = "a::traits";
115+
let description = [{
116+
117+
Simple Op Interface description
118+
}];
119+
// OP-INTERFACE: namespace a {
120+
// OP-INTERFACE-NEXT: namespace traits {
121+
// OP-INTERFACE-NEXT: /// Simple Op Interface description
122+
// OP-INTERFACE-NEXT: class SimpleOpInterface;
123+
}
124+
125+
def SimpleTypeInterface : TypeInterface<"SimpleTypeInterface"> {
126+
let description = [{
127+
Simple Type Interface description
128+
}];
129+
// TYPE-INTERFACE: /// Simple Type Interface description
130+
// TYPE-INTERFACE-NEXT: class SimpleTypeInterface;
131+
}
132+
133+
def MyBitEnum: I32BitEnumAttr<"MyBitEnum", "An example bit enum",
134+
[I32BitEnumCaseBit<"Bit0", 0, "tagged">,
135+
I32BitEnumCaseBit<"Bit1", 1>]> {
136+
let genSpecializedAttr = 0;
137+
// ENUM: // An example bit enum
138+
// ENUM-NEXT: enum class MyBitEnum
139+
}

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "CppGenUtilities.h"
1314
#include "DocGenUtilities.h"
1415
#include "mlir/TableGen/Format.h"
1516
#include "mlir/TableGen/GenInfo.h"
@@ -527,6 +528,11 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
527528

528529
// Emit a forward declaration of the interface class so that it becomes usable
529530
// in the signature of its methods.
531+
std::string comments = tblgen::emitSummaryAndDescComments(
532+
"", interface.getDescription().value_or(""));
533+
if (!comments.empty()) {
534+
os << comments << "\n";
535+
}
530536
os << "class " << interfaceName << ";\n";
531537

532538
// Emit the traits struct containing the concept and model declarations.
@@ -589,7 +595,8 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
589595
<< " auto* interface = getInterfaceFor(base);\n"
590596
<< " if (!interface)\n"
591597
" return false;\n"
592-
" " << interfaceName << " odsInterfaceInstance(base, interface);\n"
598+
" "
599+
<< interfaceName << " odsInterfaceInstance(base, interface);\n"
593600
<< " " << tblgen::tgfmt(extraClassOf->trim(), &extraClassOfFmt)
594601
<< "\n }\n";
595602
}

0 commit comments

Comments
 (0)