[TableGen] Return base attribute's name for anonymous OptionalAttr/DefaultValuedAttr

--

PiperOrigin-RevId: 247693280
This commit is contained in:
Lei Zhang 2019-05-10 16:11:02 -07:00 committed by Mehdi Amini
parent 4a6264f5c5
commit df5000fd31
4 changed files with 16 additions and 3 deletions
mlir
include/mlir
lib/TableGen
tools/mlir-tblgen

View File

@ -495,6 +495,10 @@ class DefaultValuedAttr<Attr attr, string val> :
let convertFromStorage = attr.convertFromStorage; let convertFromStorage = attr.convertFromStorage;
let constBuilderCall = attr.constBuilderCall; let constBuilderCall = attr.constBuilderCall;
let defaultValue = val; 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 // 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 # "(" # let convertFromStorage = "$_self ? " # returnType # "(" #
attr.convertFromStorage # ") : (llvm::None)"; attr.convertFromStorage # ") : (llvm::None)";
let isOptional = 0b1; 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 // A generic attribute that must be constructed around a specific type

View File

@ -92,7 +92,10 @@ public:
// Returns whether this attribute is optional. // Returns whether this attribute is optional.
bool isOptional() const; 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 // Returns the code body for derived attribute. Aborts if this is not a
// derived attribute. // derived attribute.

View File

@ -110,7 +110,9 @@ bool tblgen::Attribute::isOptional() const {
return def->getValueAsBit("isOptional"); 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(); return def->getName();
} }

View File

@ -234,7 +234,7 @@ PatternEmitter::PatternEmitter(Record *pat, RecordOperatorMap *mapper,
std::string PatternEmitter::handleConstantAttr(Attribute attr, std::string PatternEmitter::handleConstantAttr(Attribute attr,
StringRef value) { StringRef value) {
if (!attr.isConstBuildable()) if (!attr.isConstBuildable())
PrintFatalError(loc, "Attribute " + attr.getTableGenDefName() + PrintFatalError(loc, "Attribute " + attr.getAttrDefName() +
" does not have the 'constBuilderCall' field"); " does not have the 'constBuilderCall' field");
// TODO(jpienaar): Verify the constants here // TODO(jpienaar): Verify the constants here