Skip to content

Commit d339d8f

Browse files
authored
[mlir][docgen] Display full attribute descriptions in expandable regions (#67009)
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. **Example** Default view: ![image](https://github.com/llvm/llvm-project/assets/11597044/922669c7-b838-4230-bcfd-a77cde0f335d) Expanded: ![image](https://github.com/llvm/llvm-project/assets/11597044/41da086e-a5ce-45dd-9f44-9d10a4d5f2e1) --- This requires: llvm/mlir-www#158 (adds a very simple markdown shortcode)
1 parent 4731623 commit d339d8f

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
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 & 6 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;
@@ -192,16 +199,35 @@ static void emitOpDoc(const Operator &op, raw_ostream &os) {
192199

193200
// Emit attributes.
194201
if (op.getNumAttributes() != 0) {
195-
// TODO: Attributes are only documented by TableGen name, with no further
196-
// info. This should be improved.
197202
os << "\n#### Attributes:\n\n";
198-
os << "| Attribute | MLIR Type | Description |\n"
199-
<< "| :-------: | :-------: | ----------- |\n";
203+
// Note: This table is HTML rather than markdown so the attribute's
204+
// description can appear in an expandable region. The description may be
205+
// multiple lines, which is not supported in a markdown table cell.
206+
os << "<table>\n";
207+
// Header.
208+
os << "<tr><th>Attribute</th><th>MLIR Type</th><th>Description</th></tr>\n";
200209
for (const auto &it : op.getAttributes()) {
201210
StringRef storageType = it.attr.getStorageType();
202-
os << "| `" << it.name << "` | " << storageType << " | "
203-
<< it.attr.getSummary() << "\n";
211+
// Name and storage type.
212+
os << "<tr>";
213+
os << "<td><code>" << it.name << "</code></td><td>" << storageType
214+
<< "</td><td>";
215+
StringRef description = resolveAttrDescription(it.attr);
216+
if (!description.empty()) {
217+
// Expandable description.
218+
// This appears as just the summary, but when clicked shows the full
219+
// description.
220+
os << "<details>"
221+
<< "<summary>" << it.attr.getSummary() << "</summary>"
222+
<< "{{% markdown %}}" << description << "{{% /markdown %}}"
223+
<< "</details>";
224+
} else {
225+
// Fallback: Single-line summary.
226+
os << it.attr.getSummary();
227+
}
228+
os << "</td></tr>\n";
204229
}
230+
os << "<table>\n";
205231
}
206232

207233
// Emit each of the operands.

0 commit comments

Comments
 (0)