[PATCH] D74143: [MLIR] Adding attribute setters generation for table gen

Lei Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 19 08:53:09 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG476ca094c846: [mlir][ods] Adding attribute setters generation (authored by AlexEichenberger, committed by antiagainst).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74143/new/

https://reviews.llvm.org/D74143

Files:
  mlir/test/mlir-tblgen/op-attribute.td
  mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp


Index: mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
===================================================================
--- mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -171,6 +171,9 @@
   // Generates getters for the attributes.
   void genAttrGetters();
 
+  // Generates setter for the attributes.
+  void genAttrSetters();
+
   // Generates getters for named operands.
   void genNamedOperandGetters();
 
@@ -300,6 +303,7 @@
   genNamedResultGetters();
   genNamedRegionGetters();
   genAttrGetters();
+  genAttrSetters();
   genBuilder();
   genParser();
   genPrinter();
@@ -381,6 +385,25 @@
   }
 }
 
+void OpEmitter::genAttrSetters() {
+  // Generate raw named setter type. This is a wrapper class that allows setting
+  // to the attributes via setters instead of having to use the string interface
+  // for better compile time verification.
+  auto emitAttrWithStorageType = [&](StringRef name, Attribute attr) {
+    auto &method = opClass.newMethod("void", (name + "Attr").str(),
+                                     (attr.getStorageType() + " attr").str());
+    auto &body = method.body();
+    body << "  this->setAttr(\"" << name << "\", attr);";
+  };
+
+  for (auto &namedAttr : op.getAttributes()) {
+    const auto &name = namedAttr.name;
+    const auto &attr = namedAttr.attr;
+    if (!attr.isDerivedAttr())
+      emitAttrWithStorageType(name, attr);
+  }
+}
+
 // Generates the named operand getter methods for the given Operator `op` and
 // puts them in `opClass`.  Uses `rangeType` as the return type of getters that
 // return a range of operands (individual operands are `Value ` and each
Index: mlir/test/mlir-tblgen/op-attribute.td
===================================================================
--- mlir/test/mlir-tblgen/op-attribute.td
+++ mlir/test/mlir-tblgen/op-attribute.td
@@ -53,6 +53,16 @@
 // DEF-NEXT:   auto attr = cAttrAttr()
 // DEF-NEXT:   return attr ? Optional<some-return-type>(attr.some-convert-from-storage()) : (llvm::None);
 
+// Test setter methods
+// ---
+
+// DEF:      void AOp::aAttrAttr(some-attr-kind attr) {
+// DEF-NEXT:   this->setAttr("aAttr", attr);
+// DEF:      void AOp::bAttrAttr(some-attr-kind attr) {
+// DEF-NEXT:   this->setAttr("bAttr", attr);
+// DEF:      void AOp::cAttrAttr(some-attr-kind attr) {
+// DEF-NEXT:   this->setAttr("cAttr", attr);
+
 // Test build methods
 // ---
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74143.245429.patch
Type: text/x-patch
Size: 2413 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200219/064ff526/attachment.bin>


More information about the llvm-commits mailing list