Skip to content

[mlir][docgen] Add ops source link #73657

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 4 commits into from
Nov 30, 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
4 changes: 2 additions & 2 deletions mlir/test/mlir-tblgen/gen-dialect-doc.td
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def TestTypeDefParams : TypeDef<Test_Dialect, "TestTypeDefParams"> {
// CHECK: Interfaces: NoMemoryEffect (MemoryEffectOpInterface)
// CHECK: Effects: MemoryEffects::Effect{}

// CHECK: ## Attribute constraint definition
// CHECK: ## Attribute constraints
// CHECK: ### attribute summary
// CHECK: attribute description

Expand All @@ -97,7 +97,7 @@ def TestTypeDefParams : TypeDef<Test_Dialect, "TestTypeDefParams"> {
// CHECK: Syntax:
// CHECK: #test.test_attr_def_params

// CHECK: ## Type constraint definition
// CHECK: ## Type constraints
// CHECK: ### type summary
// CHECK: type description

Expand Down
34 changes: 24 additions & 10 deletions mlir/tools/mlir-tblgen/OpDocGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,22 @@ static void emitOpDoc(const Operator &op, raw_ostream &os) {
os << "\n";
}

static void emitSourceLink(StringRef inputFilename, raw_ostream &os) {
size_t pathBegin = inputFilename.find("mlir/include/mlir/");
if (pathBegin == StringRef::npos)
return;

StringRef inputFromMlirInclude = inputFilename.substr(pathBegin);

os << "[source](https://github.com/llvm/llvm-project/blob/main/"
<< inputFromMlirInclude << ")\n\n";
}

static void emitOpDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
auto opDefs = getRequestedOpDefinitions(recordKeeper);

os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
emitSourceLink(recordKeeper.getInputFilename(), os);
for (const llvm::Record *opDef : opDefs)
emitOpDoc(Operator(opDef), os);
}
Expand Down Expand Up @@ -392,12 +404,13 @@ static void maybeNest(bool nest, llvm::function_ref<void(raw_ostream &os)> fn,
}
}

static void emitBlock(ArrayRef<Attribute> attributes,
static void emitBlock(ArrayRef<Attribute> attributes, StringRef inputFilename,
ArrayRef<AttrDef> attrDefs, ArrayRef<OpDocGroup> ops,
ArrayRef<Type> types, ArrayRef<TypeDef> typeDefs,
raw_ostream &os) {
if (!ops.empty()) {
os << "## Operation definition\n\n";
os << "## Operations\n\n";
emitSourceLink(inputFilename, os);
for (const OpDocGroup &grouping : ops) {
bool nested = !grouping.summary.empty();
maybeNest(
Expand All @@ -417,32 +430,32 @@ static void emitBlock(ArrayRef<Attribute> attributes,
}

if (!attributes.empty()) {
os << "## Attribute constraint definition\n\n";
os << "## Attribute constraints\n\n";
for (const Attribute &attr : attributes)
emitAttrDoc(attr, os);
}

if (!attrDefs.empty()) {
os << "## Attribute definition\n\n";
os << "## Attributes\n\n";
for (const AttrDef &def : attrDefs)
emitAttrOrTypeDefDoc(def, os);
}

// TODO: Add link between use and def for types
if (!types.empty()) {
os << "## Type constraint definition\n\n";
os << "## Type constraints\n\n";
for (const Type &type : types)
emitTypeDoc(type, os);
}

if (!typeDefs.empty()) {
os << "## Type definition\n\n";
os << "## Types\n\n";
for (const TypeDef &def : typeDefs)
emitAttrOrTypeDefDoc(def, os);
}
}

static void emitDialectDoc(const Dialect &dialect,
static void emitDialectDoc(const Dialect &dialect, StringRef inputFilename,
ArrayRef<Attribute> attributes,
ArrayRef<AttrDef> attrDefs, ArrayRef<OpDocGroup> ops,
ArrayRef<Type> types, ArrayRef<TypeDef> typeDefs,
Expand All @@ -456,7 +469,7 @@ static void emitDialectDoc(const Dialect &dialect,
if (!r.match(dialect.getDescription()))
os << "[TOC]\n\n";

emitBlock(attributes, attrDefs, ops, types, typeDefs, os);
emitBlock(attributes, inputFilename, attrDefs, ops, types, typeDefs, os);
}

static bool emitDialectDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
Expand Down Expand Up @@ -536,8 +549,9 @@ static bool emitDialectDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
});

os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
emitDialectDoc(*dialect, dialectAttrs, dialectAttrDefs, dialectOps,
dialectTypes, dialectTypeDefs, os);
emitDialectDoc(*dialect, recordKeeper.getInputFilename(), dialectAttrs,
dialectAttrDefs, dialectOps, dialectTypes, dialectTypeDefs,
os);
return false;
}

Expand Down