[TableGen] Return base attribute's name for anonymous OptionalAttr/DefaultValuedAttr
authorLei Zhang <antiagainst@google.com>
Fri, 10 May 2019 23:11:02 +0000 (16:11 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sat, 11 May 2019 02:30:48 +0000 (19:30 -0700)
--

PiperOrigin-RevId: 247693280

mlir/include/mlir/IR/OpBase.td
mlir/include/mlir/TableGen/Attribute.h
mlir/lib/TableGen/Attribute.cpp
mlir/tools/mlir-tblgen/RewriterGen.cpp

index 5b186dd..58e6867 100644 (file)
@@ -495,6 +495,10 @@ class DefaultValuedAttr<Attr attr, string val> :
   let convertFromStorage = attr.convertFromStorage;
   let constBuilderCall = attr.constBuilderCall;
   let defaultValue = val;
+
+  // Remember `attr`'s def name.
+  // TOOD(b/132458159): consider embedding Attr as a field.
+  string baseAttr = !cast<string>(attr);
 }
 
 // Decorates an attribute as optional. The return type of the generated
@@ -507,6 +511,10 @@ class OptionalAttr<Attr attr> : Attr<attr.predicate, attr.description> {
   let convertFromStorage = "$_self ? " # returnType # "(" #
                            attr.convertFromStorage # ") : (llvm::None)";
   let isOptional = 0b1;
+
+  // Remember `attr`'s def name.
+  // TOOD(b/132458159): consider embedding Attr as a field.
+  string baseAttr = !cast<string>(attr);
 }
 
 // A generic attribute that must be constructed around a specific type
index f8de36d..f921605 100644 (file)
@@ -92,7 +92,10 @@ public:
   // Returns whether this attribute is optional.
   bool isOptional() const;
 
-  StringRef getTableGenDefName() const;
+  // Returns this attribute's TableGen def name. If this is an `OptionalAttr`
+  // or `DefaultValuedAttr` without explicit name, returns the base attribute's
+  // name.
+  StringRef getAttrDefName() const;
 
   // Returns the code body for derived attribute. Aborts if this is not a
   // derived attribute.
index a165ba8..6e4083c 100644 (file)
@@ -110,7 +110,9 @@ bool tblgen::Attribute::isOptional() const {
   return def->getValueAsBit("isOptional");
 }
 
-StringRef tblgen::Attribute::getTableGenDefName() const {
+StringRef tblgen::Attribute::getAttrDefName() const {
+  if (def->isAnonymous() && (isOptional() || hasDefaultValueInitializer()))
+    return getValueAsString(def->getValueInit("baseAttr"));
   return def->getName();
 }
 
index 26a8f8e..9cf8507 100644 (file)
@@ -234,7 +234,7 @@ PatternEmitter::PatternEmitter(Record *pat, RecordOperatorMap *mapper,
 std::string PatternEmitter::handleConstantAttr(Attribute attr,
                                                StringRef value) {
   if (!attr.isConstBuildable())
-    PrintFatalError(loc, "Attribute " + attr.getTableGenDefName() +
+    PrintFatalError(loc, "Attribute " + attr.getAttrDefName() +
                              " does not have the 'constBuilderCall' field");
 
   // TODO(jpienaar): Verify the constants here