diff --git a/mlir/test/mlir-tblgen/gen-dialect-doc.td b/mlir/test/mlir-tblgen/gen-dialect-doc.td --- a/mlir/test/mlir-tblgen/gen-dialect-doc.td +++ b/mlir/test/mlir-tblgen/gen-dialect-doc.td @@ -1,6 +1,7 @@ // RUN: mlir-tblgen -gen-dialect-doc -I %S/../../include %s | FileCheck %s include "mlir/IR/OpBase.td" +include "mlir/Interfaces/SideEffectInterfaces.td" def Test_Dialect : Dialect { let name = "test"; @@ -11,12 +12,15 @@ }]; let cppNamespace = "NS"; } -def AOp : Op; +def AOp : Op]>; // CHECK: Dialect without a [TOC] here. // CHECK: TOC added by tool. // CHECK: [TOC] // CHECK-NOT: [TOC] +// CHECK: Traits: SingleBlockImplicitTerminator +// CHECK: Interfaces: NoSideEffect (MemoryEffectOpInterface) +// CHECK: Effects: MemoryEffects::Effect{} def Toc_Dialect : Dialect { let name = "test_toc"; diff --git a/mlir/tools/mlir-tblgen/OpDocGen.cpp b/mlir/tools/mlir-tblgen/OpDocGen.cpp --- a/mlir/tools/mlir-tblgen/OpDocGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDocGen.cpp @@ -60,7 +60,7 @@ template static void emitNamedConstraint(const T &it, raw_ostream &os) { if (!it.name.empty()) - os << "`" << it.name << "`"; + os << "| `" << it.name << "`"; else os << "«unnamed»"; os << " | " << it.constraint.getSummary() << "\n"; @@ -88,6 +88,61 @@ os << "```\n\n"; } +static void emitOpTraitsDoc(Operator op, raw_ostream &os) { + // TODO: We should link to the trait/documentation of it. That also means we + // should add descriptions to traits that can be queried. + // Collect using set to sort effects, interfaces & traits. + std::set effects, interfaces, traits; + for (auto &trait : op.getTraits()) { + if (auto *nt = dyn_cast(&trait)) + continue; + + std::string name = trait.getDef().getName().str(); + StringRef ref = name; + StringRef traitName = trait.getDef().getValueAsString("trait"); + traitName.consume_back("::Trait"); + traitName.consume_back("::Impl"); + if (ref.startswith("anonymous_")) + name = traitName.str(); + if (auto *nt = dyn_cast(&trait)) { + if (trait.getDef().isSubClassOf("SideEffectsTraitBase")) { + auto effectName = trait.getDef().getValueAsString("baseEffectName"); + effectName.consume_front("::"); + effectName.consume_front("mlir::"); + std::string effectStr; + llvm::raw_string_ostream os(effectStr); + os << effectName << "{"; + auto list = trait.getDef().getValueAsListOfDefs("effects"); + llvm::interleaveComma(list, os, [&](Record *rec) { + StringRef effect = rec->getValueAsString("effect"); + effect.consume_front("::"); + effect.consume_front("mlir::"); + os << effect << " on " << rec->getValueAsString("resource"); + }); + os << "}"; + effects.insert(os.str()); + name.append(llvm::formatv(" ({0})", traitName).str()); + } + interfaces.insert(name); + continue; + } + + traits.insert(name); + } + if (!traits.empty()) { + llvm::interleaveComma(traits, os << "\nTraits: "); + os << "\n"; + } + if (!interfaces.empty()) { + llvm::interleaveComma(interfaces, os << "\nInterfaces: "); + os << "\n"; + } + if (!effects.empty()) { + llvm::interleaveComma(effects, os << "\nEffects: "); + os << "\n"; + } +} + static void emitOpDoc(Operator op, raw_ostream &os) { os << llvm::formatv("### `{0}` ({1})\n", op.getOperationName(), op.getQualCppClassName()); @@ -101,6 +156,8 @@ if (op.hasDescription()) mlir::tblgen::emitDescription(op.getDescription(), os); + emitOpTraitsDoc(op, os); + // Emit attributes. if (op.getNumAttributes() != 0) { // TODO: Attributes are only documented by TableGen name, with no further @@ -110,7 +167,7 @@ << "| :-------: | :-------: | ----------- |\n"; for (const auto &it : op.getAttributes()) { StringRef storageType = it.attr.getStorageType(); - os << "`" << it.name << "` | " << storageType << " | " + os << "| `" << it.name << "` | " << storageType << " | " << it.attr.getSummary() << "\n"; } }