[Mlir-commits] [mlir] 7f76471 - [mlir][tblgen] Add support for extraClassDefinition in AttrDef

Jeff Niu llvmlistbot at llvm.org
Tue Jul 19 09:13:37 PDT 2022


Author: bhatuzdaname
Date: 2022-07-19T09:13:32-07:00
New Revision: 7f76471ee896b39eca64ebff87cdbd6bed3fc85a

URL: https://github.com/llvm/llvm-project/commit/7f76471ee896b39eca64ebff87cdbd6bed3fc85a
DIFF: https://github.com/llvm/llvm-project/commit/7f76471ee896b39eca64ebff87cdbd6bed3fc85a.diff

LOG: [mlir][tblgen] Add support for extraClassDefinition in AttrDef

For AttrDef declarations, place specified code in extraClassDefinition into the generated *.cpp.inc file.

Reviewed By: Mogball, rriddle

Differential Revision: https://reviews.llvm.org/D129574

Added: 
    

Modified: 
    mlir/docs/AttributesAndTypes.md
    mlir/include/mlir/IR/AttrTypeBase.td
    mlir/include/mlir/TableGen/AttrOrTypeDef.h
    mlir/include/mlir/TableGen/Class.h
    mlir/lib/TableGen/AttrOrTypeDef.cpp
    mlir/test/mlir-tblgen/attrdefs.td
    mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/docs/AttributesAndTypes.md b/mlir/docs/AttributesAndTypes.md
index e7cd74c9315a..df650e5f9a7e 100644
--- a/mlir/docs/AttributesAndTypes.md
+++ b/mlir/docs/AttributesAndTypes.md
@@ -1065,13 +1065,16 @@ public:
 
 The declarative Attribute and Type definitions try to auto-generate as much
 logic and methods as possible. With that said, there will always be long-tail
-cases that won't be covered. For such cases, `extraClassDeclaration` can be
-used. Code within the `extraClassDeclaration` field will be copied literally to
-the generated C++ Attribute or Type class.
-
-Note that `extraClassDeclaration` is a mechanism intended for long-tail cases by
-power users; for not-yet-implemented widely-applicable cases, improving the
-infrastructure is preferable.
+cases that won't be covered. For such cases, `extraClassDeclaration` and
+`extraClassDefinition` can be used. Code within the `extraClassDeclaration`
+field will be copied literally to the generated C++ Attribute or Type class.
+Code within `extraClassDefinition` will be added to the generated source file
+inside the class's C++ namespace. The substitution `$cppClass` will be replaced
+by the Attribute or Type's C++ class name.
+
+Note that these are mechanisms intended for long-tail cases by power users; for
+not-yet-implemented widely-applicable cases, improving the infrastructure is
+preferable.
 
 ### Registering with the Dialect
 

diff  --git a/mlir/include/mlir/IR/AttrTypeBase.td b/mlir/include/mlir/IR/AttrTypeBase.td
index d72cb110650c..93dd128d329b 100644
--- a/mlir/include/mlir/IR/AttrTypeBase.td
+++ b/mlir/include/mlir/IR/AttrTypeBase.td
@@ -223,6 +223,11 @@ class AttrOrTypeDef<string valueType, string name, list<Trait> defTraits,
 
   // Extra code to include in the class declaration.
   code extraClassDeclaration = [{}];
+
+  // Additional code that will be added to the generated source file. The
+  // generated code is placed inside the class's C++ namespace. `$cppClass` is
+  // replaced by the class name.
+  code extraClassDefinition = [{}];
 }
 
 // Define a new attribute, named `name`, belonging to `dialect` that inherits

diff  --git a/mlir/include/mlir/TableGen/AttrOrTypeDef.h b/mlir/include/mlir/TableGen/AttrOrTypeDef.h
index d4bab72f485a..1abaf64b878e 100644
--- a/mlir/include/mlir/TableGen/AttrOrTypeDef.h
+++ b/mlir/include/mlir/TableGen/AttrOrTypeDef.h
@@ -201,6 +201,9 @@ class AttrOrTypeDef {
   /// Returns the def's extra class declaration code.
   Optional<StringRef> getExtraDecls() const;
 
+  /// Returns the def's extra class definition code.
+  Optional<StringRef> getExtraDefs() const;
+
   /// Get the code location (for error printing).
   ArrayRef<SMLoc> getLoc() const;
 

diff  --git a/mlir/include/mlir/TableGen/Class.h b/mlir/include/mlir/TableGen/Class.h
index a8a710ff85fe..efcd73ae61de 100644
--- a/mlir/include/mlir/TableGen/Class.h
+++ b/mlir/include/mlir/TableGen/Class.h
@@ -557,7 +557,7 @@ class ExtraClassDeclaration
   StringRef extraClassDeclaration;
   /// The string of the extra class definitions. It is re-indented before
   /// printed.
-  StringRef extraClassDefinition;
+  std::string extraClassDefinition;
 };
 
 /// A class used to emit C++ classes from Tablegen.  Contains a list of public

diff  --git a/mlir/lib/TableGen/AttrOrTypeDef.cpp b/mlir/lib/TableGen/AttrOrTypeDef.cpp
index d399c06d2f9c..acd0e5529fdd 100644
--- a/mlir/lib/TableGen/AttrOrTypeDef.cpp
+++ b/mlir/lib/TableGen/AttrOrTypeDef.cpp
@@ -179,6 +179,11 @@ Optional<StringRef> AttrOrTypeDef::getExtraDecls() const {
   return value.empty() ? Optional<StringRef>() : value;
 }
 
+Optional<StringRef> AttrOrTypeDef::getExtraDefs() const {
+  auto value = def->getValueAsString("extraClassDefinition");
+  return value.empty() ? Optional<StringRef>() : value;
+}
+
 ArrayRef<SMLoc> AttrOrTypeDef::getLoc() const { return def->getLoc(); }
 
 bool AttrOrTypeDef::skipDefaultBuilders() const {

diff  --git a/mlir/test/mlir-tblgen/attrdefs.td b/mlir/test/mlir-tblgen/attrdefs.td
index 8de12eb2b300..7fc186362df2 100644
--- a/mlir/test/mlir-tblgen/attrdefs.td
+++ b/mlir/test/mlir-tblgen/attrdefs.td
@@ -158,3 +158,23 @@ def G_BuilderWithReturnTypeAttr : TestAttr<"BuilderWithReturnType"> {
 // DECL-LABEL: class BuilderWithReturnTypeAttr
 // DECL: ::mlir::Attribute get(
 // DECL: ::mlir::Attribute getChecked(
+
+def H_TestExtraClassAttr : TestAttr<"TestExtraClass"> {
+  let extraClassDeclaration = [{
+    /// Test Method
+    static int getFoo(int i);
+  }];
+  let extraClassDefinition = [{
+    int $cppClass::getFoo(int i) {
+      return i+1;
+    }
+  }];
+}
+
+// DECL-LABEL: TestExtraClassAttr : public ::mlir::Attribute
+// DECL: /// Test Method
+// DECL-NEXT: static int getFoo(int i);
+
+// DEF-LABEL: int TestExtraClassAttr::getFoo(int i) {
+// DEF: return i+1;
+// DEF-NEXT: }

diff  --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index ef2bd4102b6f..6e32109cd64d 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -214,14 +214,26 @@ void DefGen::createParentWithTraits() {
   defCls.addParent(std::move(defParent));
 }
 
+/// Extra class definitions have a `$cppClass` substitution that is to be
+/// replaced by the C++ class name.
+static std::string formatExtraDefinitions(const AttrOrTypeDef &def) {
+  if (Optional<StringRef> extraDef = def.getExtraDefs()) {
+    FmtContext ctx = FmtContext().addSubst("cppClass", def.getCppClassName());
+    return tgfmt(*extraDef, &ctx).str();
+  }
+  return "";
+}
+
 void DefGen::emitTopLevelDeclarations() {
   // Inherit constructors from the attribute or type class.
   defCls.declare<VisibilityDeclaration>(Visibility::Public);
   defCls.declare<UsingDeclaration>("Base::Base");
 
   // Emit the extra declarations first in case there's a definition in there.
-  if (Optional<StringRef> extraDecl = def.getExtraDecls())
-    defCls.declare<ExtraClassDeclaration>(*extraDecl);
+  Optional<StringRef> extraDecl = def.getExtraDecls();
+  std::string extraDef = formatExtraDefinitions(def);
+  defCls.declare<ExtraClassDeclaration>(extraDecl ? *extraDecl : "",
+                                        std::move(extraDef));
 }
 
 void DefGen::emitBuilders() {


        


More information about the Mlir-commits mailing list