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

Alexandre Eichenberger via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 10:29:54 PST 2020


AlexEichenberger created this revision.
AlexEichenberger added reviewers: mehdi_amini, rriddle.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, aartbik, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, jpienaar.
Herald added a project: LLVM.

In some dialects, attributes may have default values that may be determined only after shape inference. For example, attributes that are dependent on the rank of the input cannot be assigned a default value until the rank of the tensor is inferred.

While we can set attributes without explicit setters, referring to the attributes via accessors instead of having to use the string interface is better for compile time verification.

The proposed patch add one method per operation attribute that let us set its value. The code is a very small modification of the existing getter methods.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74143

Files:
  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,28 @@
   }
 }
 
+// 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.
+void OpEmitter::genAttrSetters() {
+  FmtContext fctx;
+  fctx.withBuilder("mlir::Builder(this->getContext())");
+  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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74143.242938.patch
Type: text/x-patch
Size: 1724 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200206/a476ac06/attachment.bin>


More information about the llvm-commits mailing list