[Mlir-commits] [mlir] [mlir][docgen] Add ops source link (PR #73657)

Rik Huijzer llvmlistbot at llvm.org
Wed Nov 29 01:02:59 PST 2023


https://github.com/rikhuijzer updated https://github.com/llvm/llvm-project/pull/73657

>From 438dfd796d1a0431278cd89790f5bff796fa4fac Mon Sep 17 00:00:00 2001
From: Rik Huijzer <github at huijzer.xyz>
Date: Tue, 28 Nov 2023 15:53:04 +0100
Subject: [PATCH 1/3] [mlir][docgen] Add ops source link

---
 mlir/tools/mlir-tblgen/OpDocGen.cpp | 36 +++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/mlir/tools/mlir-tblgen/OpDocGen.cpp b/mlir/tools/mlir-tblgen/OpDocGen.cpp
index 773ad6ec198b957..f158340126689ba 100644
--- a/mlir/tools/mlir-tblgen/OpDocGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDocGen.cpp
@@ -265,10 +265,24 @@ 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 << "<span class=\"op-definitions-source-link\">\n";
+  os << "  <a href=\"https://github.com/llvm/llvm-project/blob/main/"
+     << inputFromMlirInclude << "\">source</a>\n";
+  os << "</span>\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);
 }
@@ -392,12 +406,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(
@@ -417,32 +432,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 << "## Type definitions\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,
@@ -456,7 +471,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) {
@@ -536,8 +551,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;
 }
 

>From 29f53988a2ae062210eab1e4e9b10a201f21692f Mon Sep 17 00:00:00 2001
From: Rik Huijzer <github at huijzer.xyz>
Date: Tue, 28 Nov 2023 16:05:38 +0100
Subject: [PATCH 2/3] Fix typo

---
 mlir/tools/mlir-tblgen/OpDocGen.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/tools/mlir-tblgen/OpDocGen.cpp b/mlir/tools/mlir-tblgen/OpDocGen.cpp
index f158340126689ba..cac1131c0369a9c 100644
--- a/mlir/tools/mlir-tblgen/OpDocGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDocGen.cpp
@@ -451,7 +451,7 @@ static void emitBlock(ArrayRef<Attribute> attributes, StringRef inputFilename,
   }
 
   if (!typeDefs.empty()) {
-    os << "## Type definitions\n\n";
+    os << "## Types\n\n";
     for (const TypeDef &def : typeDefs)
       emitAttrOrTypeDefDoc(def, os);
   }

>From 572a52ee941ea1d296f20248da80e5183e1006b4 Mon Sep 17 00:00:00 2001
From: Rik Huijzer <github at huijzer.xyz>
Date: Wed, 29 Nov 2023 10:02:45 +0100
Subject: [PATCH 3/3] Fix tests

---
 mlir/test/mlir-tblgen/gen-dialect-doc.td | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/test/mlir-tblgen/gen-dialect-doc.td b/mlir/test/mlir-tblgen/gen-dialect-doc.td
index 120164345fb2ce3..ca0b6e38edf82c2 100644
--- a/mlir/test/mlir-tblgen/gen-dialect-doc.td
+++ b/mlir/test/mlir-tblgen/gen-dialect-doc.td
@@ -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
 
@@ -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
 



More information about the Mlir-commits mailing list