diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td index 2af48e777a94..5ccd2a3e8e5a 100644 --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -518,6 +518,17 @@ class Attr : // convertFromStorage method to handle the case where the attribute is // not present. bit isOptional = 0; + + // What is the base-level Attr instantiation that this Attr is built upon. + // Unset means this is a base-level Attr. + // + // This field is used by attribute wrapper classes (DefaultValuedAttr, + // OptionalAttr, etc.) to retrive the base-level attribute definition. + // This can be used for getting its name; otherwise, we will see + // "anonymous_" as the attribute def name because of template + // instantiation. + // TOOD(b/132458159): deduplicate the fields in attribute wrapper classes. + Attr baseAttr = ?; } //===----------------------------------------------------------------------===// @@ -535,9 +546,7 @@ class DefaultValuedAttr : let constBuilderCall = attr.constBuilderCall; let defaultValue = val; - // Remember `attr`'s def name. - // TOOD(b/132458159): consider embedding Attr as a field. - string baseAttr = !cast(attr); + let baseAttr = attr; } // Decorates an attribute as optional. The return type of the generated @@ -551,9 +560,7 @@ class OptionalAttr : Attr { attr.convertFromStorage # ") : (llvm::None)"; let isOptional = 1; - // Remember `attr`'s def name. - // TOOD(b/132458159): consider embedding Attr as a field. - string baseAttr = !cast(attr); + let baseAttr = attr; } //===----------------------------------------------------------------------===// @@ -891,6 +898,8 @@ class Confined constraints> : Attr< let constBuilderCall = attr.constBuilderCall; let defaultValue = attr.defaultValue; let isOptional = attr.isOptional; + + let baseAttr = attr; } // An AttrConstraint that holds if all attr constraints specified in diff --git a/mlir/include/mlir/TableGen/Attribute.h b/mlir/include/mlir/TableGen/Attribute.h index a3fbf6dcf4b9..6950510f51d8 100644 --- a/mlir/include/mlir/TableGen/Attribute.h +++ b/mlir/include/mlir/TableGen/Attribute.h @@ -52,14 +52,6 @@ public: explicit Attribute(const llvm::Record *record); explicit Attribute(const llvm::DefInit *init); - // Returns true if this attribute is a derived attribute (i.e., a subclass - // of `DerivedAttr`). - bool isDerivedAttr() const; - - // Returns true if this attribute is a type attribute (i.e., a subclass - // of `TypeAttrBase`). - bool isTypeAttr() const; - // Returns true if this attribute has storage type set. bool hasStorageType() const; @@ -84,6 +76,10 @@ public: // the constant value. StringRef getConstBuilderTemplate() const; + // Returns the base-level attribute that this attribute constraint is + // built upon. + Attribute getBaseAttr() const; + // Returns whether this attribute has a default value's initializer. bool hasDefaultValueInitializer() const; // Returns the default value's initializer for this attribute. @@ -92,6 +88,14 @@ public: // Returns whether this attribute is optional. bool isOptional() const; + // Returns true if this attribute is a derived attribute (i.e., a subclass + // of `DerivedAttr`). + bool isDerivedAttr() const; + + // Returns true if this attribute is a type attribute (i.e., a subclass + // of `TypeAttrBase`). + bool isTypeAttr() const; + // Returns this attribute's TableGen def name. If this is an `OptionalAttr` // or `DefaultValuedAttr` without explicit name, returns the base attribute's // name. diff --git a/mlir/lib/TableGen/Attribute.cpp b/mlir/lib/TableGen/Attribute.cpp index f5eb6d3bd9ad..2daccc8e23fe 100644 --- a/mlir/lib/TableGen/Attribute.cpp +++ b/mlir/lib/TableGen/Attribute.cpp @@ -96,6 +96,14 @@ StringRef tblgen::Attribute::getConstBuilderTemplate() const { return getValueAsString(init); } +tblgen::Attribute tblgen::Attribute::getBaseAttr() const { + if (const auto *defInit = + llvm::dyn_cast(def->getValueInit("baseAttr"))) { + return Attribute(defInit).getBaseAttr(); + } + return *this; +} + bool tblgen::Attribute::hasDefaultValueInitializer() const { const auto *init = def->getValueInit("defaultValue"); return !getValueAsString(init).empty(); @@ -111,8 +119,9 @@ bool tblgen::Attribute::isOptional() const { } StringRef tblgen::Attribute::getAttrDefName() const { - if (def->isAnonymous() && (isOptional() || hasDefaultValueInitializer())) - return getValueAsString(def->getValueInit("baseAttr")); + if (def->isAnonymous()) { + return getBaseAttr().def->getName(); + } return def->getName(); }