[Mlir-commits] [mlir] 476ca09 - [mlir][ods] Adding attribute setters generation

Lei Zhang llvmlistbot at llvm.org
Wed Feb 19 08:49:41 PST 2020


Author: Alexandre Eichenberger
Date: 2020-02-19T11:49:34-05:00
New Revision: 476ca094c846a0b6d4d9f37710aba21a6b0b265a

URL: https://github.com/llvm/llvm-project/commit/476ca094c846a0b6d4d9f37710aba21a6b0b265a
DIFF: https://github.com/llvm/llvm-project/commit/476ca094c846a0b6d4d9f37710aba21a6b0b265a.diff

LOG: [mlir][ods] Adding attribute setters generation

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.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/mlir/test/mlir-tblgen/op-attribute.td b/mlir/test/mlir-tblgen/op-attribute.td
index e400bf395f41..390725cc7c2a 100644
--- a/mlir/test/mlir-tblgen/op-attribute.td
+++ b/mlir/test/mlir-tblgen/op-attribute.td
@@ -53,6 +53,16 @@ def AOp : NS_Op<"a_op", []> {
 // 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
 // ---
 

diff  --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 1740cb528e7c..6af100ed95fa 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -171,6 +171,9 @@ class OpEmitter {
   // 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 @@ OpEmitter::OpEmitter(const Operator &op)
   genNamedResultGetters();
   genNamedRegionGetters();
   genAttrGetters();
+  genAttrSetters();
   genBuilder();
   genParser();
   genPrinter();
@@ -381,6 +385,25 @@ void OpEmitter::genAttrGetters() {
   }
 }
 
+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


        


More information about the Mlir-commits mailing list