Skip to content

[mlir][docgen] Display full attribute descriptions in expandable regions #67009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mlir/include/mlir/IR/EnumAttr.td
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ class I64BitEnumAttrCaseGroup<string sym, list<BitEnumAttrCaseBase> cases,
class EnumAttrInfo<
string name, list<EnumAttrCaseInfo> cases, Attr baseClass> :
Attr<baseClass.predicate, baseClass.summary> {

// Generate a description of this enums members for the MLIR docs.
let description =
"Enum cases:\n" # !interleave(
!foreach(case, cases,
"* " # case.str # " (`" # case.symbol # "`)"), "\n");

// The C++ enum class name
string className = name;

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

// The backing enumeration.
EnumAttrInfo enum = enumInfo;
Expand Down
38 changes: 32 additions & 6 deletions mlir/tools/mlir-tblgen/OpDocGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ static void emitOpTraitsDoc(const Operator &op, raw_ostream &os) {
}
}

static StringRef resolveAttrDescription(const Attribute &attr) {
StringRef description = attr.getDescription();
if (description.empty())
return attr.getBaseAttr().getDescription();
return description;
}

static void emitOpDoc(const Operator &op, raw_ostream &os) {
std::string classNameStr = op.getQualCppClassName();
StringRef className = classNameStr;
Expand All @@ -192,16 +199,35 @@ static void emitOpDoc(const Operator &op, raw_ostream &os) {

// Emit attributes.
if (op.getNumAttributes() != 0) {
// TODO: Attributes are only documented by TableGen name, with no further
// info. This should be improved.
os << "\n#### Attributes:\n\n";
os << "| Attribute | MLIR Type | Description |\n"
<< "| :-------: | :-------: | ----------- |\n";
// Note: This table is HTML rather than markdown so the attribute's
// description can appear in an expandable region. The description may be
// multiple lines, which is not supported in a markdown table cell.
os << "<table>\n";
// Header.
os << "<tr><th>Attribute</th><th>MLIR Type</th><th>Description</th></tr>\n";
for (const auto &it : op.getAttributes()) {
StringRef storageType = it.attr.getStorageType();
os << "| `" << it.name << "` | " << storageType << " | "
<< it.attr.getSummary() << "\n";
// Name and storage type.
os << "<tr>";
os << "<td><code>" << it.name << "</code></td><td>" << storageType
<< "</td><td>";
StringRef description = resolveAttrDescription(it.attr);
if (!description.empty()) {
// Expandable description.
// This appears as just the summary, but when clicked shows the full
// description.
os << "<details>"
<< "<summary>" << it.attr.getSummary() << "</summary>"
<< "{{% markdown %}}" << description << "{{% /markdown %}}"
<< "</details>";
Comment on lines +221 to +223
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll echo the comment on the merged commit (d339d8f#commitcomment-128747641):

Hi folks. This breaks documentation generators for out-of-tree projects that don't have the custom markdown shortcode. Is there any way we could make this opt-in instead of breaking?

I can add the custom shortcode on our project downstream easily enough, but I'm just hoping upstream folks can consider us out-of-tree folks when making changes that don't really need to be backwards incompatible.

This breaks downstream projects that use other markdown renderers. Please revert and/or make this opt-in via a flag like

//===----------------------------------------------------------------------===//
// Commandline Options
//===----------------------------------------------------------------------===//
static llvm::cl::OptionCategory
docCat("Options for -gen-(attrdef|typedef|op|dialect)-doc");
llvm::cl::opt<std::string>
stripPrefix("strip-prefix",
llvm::cl::desc("Strip prefix of the fully qualified names"),
llvm::cl::init("::mlir::"), llvm::cl::cat(docCat));

Here is how one renderer handles this custom syntax (i.e. unsupported, broken):
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add an option shortly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, much appreciated!

} else {
// Fallback: Single-line summary.
os << it.attr.getSummary();
}
os << "</td></tr>\n";
}
os << "<table>\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not valid HTML/markdown. Tags must be closed (</table>), not opened twice.

}

// Emit each of the operands.
Expand Down