From: Lei Zhang Date: Fri, 10 May 2019 23:11:02 +0000 (-0700) Subject: [TableGen] Return base attribute's name for anonymous OptionalAttr/DefaultValuedAttr X-Git-Tag: llvmorg-11-init~1466^2~1754 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=df5000fd314a55b3bf0f11d7a7f955863ffee3f7;p=platform%2Fupstream%2Fllvm.git [TableGen] Return base attribute's name for anonymous OptionalAttr/DefaultValuedAttr -- PiperOrigin-RevId: 247693280 --- diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td index 5b186dd..58e6867 100644 --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -495,6 +495,10 @@ class DefaultValuedAttr : 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(attr); } // Decorates an attribute as optional. The return type of the generated @@ -507,6 +511,10 @@ class OptionalAttr : Attr { 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(attr); } // A generic attribute that must be constructed around a specific type diff --git a/mlir/include/mlir/TableGen/Attribute.h b/mlir/include/mlir/TableGen/Attribute.h index f8de36d..f921605 100644 --- a/mlir/include/mlir/TableGen/Attribute.h +++ b/mlir/include/mlir/TableGen/Attribute.h @@ -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. diff --git a/mlir/lib/TableGen/Attribute.cpp b/mlir/lib/TableGen/Attribute.cpp index a165ba8..6e4083c 100644 --- a/mlir/lib/TableGen/Attribute.cpp +++ b/mlir/lib/TableGen/Attribute.cpp @@ -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(); } diff --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp index 26a8f8e..9cf8507 100644 --- a/mlir/tools/mlir-tblgen/RewriterGen.cpp +++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp @@ -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