Skip to content

Commit 4646b53

Browse files
committed
[mlir][docgen] Display full attribute descriptions in expandable regions
This updates the table of op attributes so that clicking the summary expands to show the complete description. Attribute | MLIR Type | Description <name> <type> ▶ <summary> <-- Click to expand Enum attributes have now also been updated to generate a description that lists all the cases (with both their MLIR and C++ names). This makes viewing enums on the MLIR docs much nicer.
1 parent 431969e commit 4646b53

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

mlir/include/mlir/IR/EnumAttr.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ class I64BitEnumAttrCaseGroup<string sym, list<BitEnumAttrCaseBase> cases,
113113
class EnumAttrInfo<
114114
string name, list<EnumAttrCaseInfo> cases, Attr baseClass> :
115115
Attr<baseClass.predicate, baseClass.summary> {
116+
117+
// Generate a description of this enums members for the MLIR docs.
118+
let description =
119+
"Enum cases:\n" # !interleave(
120+
!foreach(case, cases,
121+
"* " # case.str # " (`" # case.symbol # "`)"), "\n");
122+
116123
// The C++ enum class name
117124
string className = name;
118125

@@ -381,6 +388,7 @@ class EnumAttr<Dialect dialect, EnumAttrInfo enumInfo, string name = "",
381388
list <Trait> traits = []>
382389
: AttrDef<dialect, enumInfo.className, traits> {
383390
let summary = enumInfo.summary;
391+
let description = enumInfo.description;
384392

385393
// The backing enumeration.
386394
EnumAttrInfo enum = enumInfo;

mlir/tools/mlir-tblgen/OpDocGen.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ static void emitOpTraitsDoc(const Operator &op, raw_ostream &os) {
173173
}
174174
}
175175

176+
static StringRef resolveAttrDescription(const Attribute &attr) {
177+
StringRef description = attr.getDescription();
178+
if (description.empty())
179+
return attr.getBaseAttr().getDescription();
180+
return description;
181+
}
182+
176183
static void emitOpDoc(const Operator &op, raw_ostream &os) {
177184
std::string classNameStr = op.getQualCppClassName();
178185
StringRef className = classNameStr;
@@ -195,13 +202,34 @@ static void emitOpDoc(const Operator &op, raw_ostream &os) {
195202
// TODO: Attributes are only documented by TableGen name, with no further
196203
// info. This should be improved.
197204
os << "\n#### Attributes:\n\n";
198-
os << "| Attribute | MLIR Type | Description |\n"
199-
<< "| :-------: | :-------: | ----------- |\n";
205+
// Note: This table is HTML rather than markdown so the attribute's
206+
// description can appear in an expandable region. The description may be
207+
// multiple lines, which is not supported in a markdown table cell.
208+
os << "<table>\n";
209+
// Header.
210+
os << "<tr><th>Attribute</th><th>MLIR Type</th><th>Description</th></tr>\n";
200211
for (const auto &it : op.getAttributes()) {
201212
StringRef storageType = it.attr.getStorageType();
202-
os << "| `" << it.name << "` | " << storageType << " | "
203-
<< it.attr.getSummary() << "\n";
213+
// Name and storage type.
214+
os << "<tr>";
215+
os << "<td><code>" << it.name << "</code></td><td>" << storageType
216+
<< "</td><td>";
217+
StringRef description = resolveAttrDescription(it.attr);
218+
if (!description.empty()) {
219+
// Expandable description.
220+
// This appears as just the summary, but when clicked shows the full
221+
// description.
222+
os << "<details>"
223+
<< "<summary>" << it.attr.getSummary() << "</summary>"
224+
<< "{{% markdown %}}" << description << "{{% /markdown %}}"
225+
<< "</details>";
226+
} else {
227+
// Fallback: Single-line summary.
228+
os << it.attr.getSummary();
229+
}
230+
os << "</td></tr>\n";
204231
}
232+
os << "<table>\n";
205233
}
206234

207235
// Emit each of the operands.

0 commit comments

Comments
 (0)