Add (parse|print)OptionalAttrDictWithKeyword hooks to simplify parsing attribute dictionaries with regions.
Many operations with regions add an additional 'attributes' prefix when printing the attribute dictionary to differentiate it from the region body. This leads to duplicated logic for detecting when to actually print the attribute dictionary. PiperOrigin-RevId: 278747681 Change-Id: I14cd3c5c297a98e756f84a3fb4a24d440deebd0b
This commit is contained in:
parent
98c86c3680
commit
21e4e8b991
@ -78,6 +78,12 @@ public:
|
|||||||
virtual void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
virtual void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
||||||
ArrayRef<StringRef> elidedAttrs = {}) = 0;
|
ArrayRef<StringRef> elidedAttrs = {}) = 0;
|
||||||
|
|
||||||
|
/// If the specified operation has attributes, print out an attribute
|
||||||
|
/// dictionary prefixed with 'attributes'.
|
||||||
|
virtual void
|
||||||
|
printOptionalAttrDictWithKeyword(ArrayRef<NamedAttribute> attrs,
|
||||||
|
ArrayRef<StringRef> elidedAttrs = {}) = 0;
|
||||||
|
|
||||||
/// Print the entire operation with the default generic assembly form.
|
/// Print the entire operation with the default generic assembly form.
|
||||||
virtual void printGenericOp(Operation *op) = 0;
|
virtual void printGenericOp(Operation *op) = 0;
|
||||||
|
|
||||||
@ -342,6 +348,11 @@ public:
|
|||||||
virtual ParseResult
|
virtual ParseResult
|
||||||
parseOptionalAttrDict(SmallVectorImpl<NamedAttribute> &result) = 0;
|
parseOptionalAttrDict(SmallVectorImpl<NamedAttribute> &result) = 0;
|
||||||
|
|
||||||
|
/// Parse a named dictionary into 'result' if the `attributes` keyword is
|
||||||
|
/// present.
|
||||||
|
virtual ParseResult
|
||||||
|
parseOptionalAttrDictWithKeyword(SmallVectorImpl<NamedAttribute> &result) = 0;
|
||||||
|
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
// Identifier Parsing
|
// Identifier Parsing
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
|
|||||||
20
third_party/mlir/lib/Dialect/SPIRV/SPIRVOps.cpp
vendored
20
third_party/mlir/lib/Dialect/SPIRV/SPIRVOps.cpp
vendored
@ -1628,10 +1628,8 @@ static ParseResult parseModuleOp(OpAsmParser &parser, OperationState &state) {
|
|||||||
if (parser.parseRegion(*body, /*arguments=*/{}, /*argTypes=*/{}))
|
if (parser.parseRegion(*body, /*arguments=*/{}, /*argTypes=*/{}))
|
||||||
return failure();
|
return failure();
|
||||||
|
|
||||||
if (succeeded(parser.parseOptionalKeyword("attributes"))) {
|
if (parser.parseOptionalAttrDictWithKeyword(state.attributes))
|
||||||
if (parser.parseOptionalAttrDict(state.attributes))
|
return failure();
|
||||||
return failure();
|
|
||||||
}
|
|
||||||
|
|
||||||
spirv::ModuleOp::ensureTerminator(*body, parser.getBuilder(), state.location);
|
spirv::ModuleOp::ensureTerminator(*body, parser.getBuilder(), state.location);
|
||||||
return success();
|
return success();
|
||||||
@ -1657,19 +1655,7 @@ static void print(spirv::ModuleOp moduleOp, OpAsmPrinter &printer) {
|
|||||||
|
|
||||||
printer.printRegion(op->getRegion(0), /*printEntryBlockArgs=*/false,
|
printer.printRegion(op->getRegion(0), /*printEntryBlockArgs=*/false,
|
||||||
/*printBlockTerminators=*/false);
|
/*printBlockTerminators=*/false);
|
||||||
|
printer.printOptionalAttrDictWithKeyword(op->getAttrs(), elidedAttrs);
|
||||||
bool printAttrDict =
|
|
||||||
elidedAttrs.size() != 2 ||
|
|
||||||
llvm::any_of(op->getAttrs(), [&addressingModelAttrName,
|
|
||||||
&memoryModelAttrName](NamedAttribute attr) {
|
|
||||||
return attr.first != addressingModelAttrName &&
|
|
||||||
attr.first != memoryModelAttrName;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (printAttrDict) {
|
|
||||||
printer << " attributes";
|
|
||||||
printer.printOptionalAttrDict(op->getAttrs(), elidedAttrs);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static LogicalResult verify(spirv::ModuleOp moduleOp) {
|
static LogicalResult verify(spirv::ModuleOp moduleOp) {
|
||||||
|
|||||||
34
third_party/mlir/lib/IR/AsmPrinter.cpp
vendored
34
third_party/mlir/lib/IR/AsmPrinter.cpp
vendored
@ -421,7 +421,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
||||||
ArrayRef<StringRef> elidedAttrs = {});
|
ArrayRef<StringRef> elidedAttrs = {},
|
||||||
|
bool withKeyword = false);
|
||||||
void printTrailingLocation(Location loc);
|
void printTrailingLocation(Location loc);
|
||||||
void printLocationInternal(LocationAttr loc, bool pretty = false);
|
void printLocationInternal(LocationAttr loc, bool pretty = false);
|
||||||
void printDenseElementsAttr(DenseElementsAttr attr);
|
void printDenseElementsAttr(DenseElementsAttr attr);
|
||||||
@ -1327,27 +1328,26 @@ void ModulePrinter::printIntegerSet(IntegerSet set) {
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
void ModulePrinter::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
void ModulePrinter::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
||||||
ArrayRef<StringRef> elidedAttrs) {
|
ArrayRef<StringRef> elidedAttrs,
|
||||||
|
bool withKeyword) {
|
||||||
// If there are no attributes, then there is nothing to be done.
|
// If there are no attributes, then there is nothing to be done.
|
||||||
if (attrs.empty())
|
if (attrs.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Filter out any attributes that shouldn't be included.
|
// Filter out any attributes that shouldn't be included.
|
||||||
SmallVector<NamedAttribute, 8> filteredAttrs;
|
SmallVector<NamedAttribute, 8> filteredAttrs(
|
||||||
for (auto attr : attrs) {
|
llvm::make_filter_range(attrs, [&](NamedAttribute attr) {
|
||||||
// If the caller has requested that this attribute be ignored, then drop it.
|
return !llvm::is_contained(elidedAttrs, attr.first.strref());
|
||||||
if (llvm::any_of(elidedAttrs,
|
}));
|
||||||
[&](StringRef elided) { return attr.first.is(elided); }))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Otherwise add it to our filteredAttrs list.
|
|
||||||
filteredAttrs.push_back(attr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there are no attributes left to print after filtering, then we're done.
|
// If there are no attributes left to print after filtering, then we're done.
|
||||||
if (filteredAttrs.empty())
|
if (filteredAttrs.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// Print the 'attributes' keyword if necessary.
|
||||||
|
if (withKeyword)
|
||||||
|
os << " attributes ";
|
||||||
|
|
||||||
// Otherwise, print them all out in braces.
|
// Otherwise, print them all out in braces.
|
||||||
os << " {";
|
os << " {";
|
||||||
interleaveComma(filteredAttrs, [&](NamedAttribute attr) {
|
interleaveComma(filteredAttrs, [&](NamedAttribute attr) {
|
||||||
@ -1389,8 +1389,14 @@ public:
|
|||||||
|
|
||||||
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
|
||||||
ArrayRef<StringRef> elidedAttrs = {}) override {
|
ArrayRef<StringRef> elidedAttrs = {}) override {
|
||||||
return ModulePrinter::printOptionalAttrDict(attrs, elidedAttrs);
|
ModulePrinter::printOptionalAttrDict(attrs, elidedAttrs);
|
||||||
};
|
}
|
||||||
|
void printOptionalAttrDictWithKeyword(
|
||||||
|
ArrayRef<NamedAttribute> attrs,
|
||||||
|
ArrayRef<StringRef> elidedAttrs = {}) override {
|
||||||
|
ModulePrinter::printOptionalAttrDict(attrs, elidedAttrs,
|
||||||
|
/*withKeyword=*/true);
|
||||||
|
}
|
||||||
|
|
||||||
enum { nameSentinel = ~0U };
|
enum { nameSentinel = ~0U };
|
||||||
|
|
||||||
|
|||||||
5
third_party/mlir/lib/IR/FunctionSupport.cpp
vendored
5
third_party/mlir/lib/IR/FunctionSupport.cpp
vendored
@ -183,9 +183,8 @@ mlir::impl::parseFunctionLikeOp(OpAsmParser &parser, OperationState &result,
|
|||||||
<< (errorMessage.empty() ? "" : ": ") << errorMessage;
|
<< (errorMessage.empty() ? "" : ": ") << errorMessage;
|
||||||
|
|
||||||
// If function attributes are present, parse them.
|
// If function attributes are present, parse them.
|
||||||
if (succeeded(parser.parseOptionalKeyword("attributes")))
|
if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
|
||||||
if (parser.parseOptionalAttrDict(result.attributes))
|
return failure();
|
||||||
return failure();
|
|
||||||
|
|
||||||
// Add the attributes to the function arguments.
|
// Add the attributes to the function arguments.
|
||||||
SmallString<8> attrNameBuf;
|
SmallString<8> attrNameBuf;
|
||||||
|
|||||||
15
third_party/mlir/lib/IR/Module.cpp
vendored
15
third_party/mlir/lib/IR/Module.cpp
vendored
@ -48,9 +48,8 @@ ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) {
|
|||||||
result.attributes);
|
result.attributes);
|
||||||
|
|
||||||
// If module attributes are present, parse them.
|
// If module attributes are present, parse them.
|
||||||
if (succeeded(parser.parseOptionalKeyword("attributes")))
|
if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
|
||||||
if (parser.parseOptionalAttrDict(result.attributes))
|
return failure();
|
||||||
return failure();
|
|
||||||
|
|
||||||
// Parse the module body.
|
// Parse the module body.
|
||||||
auto *body = result.addRegion();
|
auto *body = result.addRegion();
|
||||||
@ -65,18 +64,14 @@ ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) {
|
|||||||
void ModuleOp::print(OpAsmPrinter &p) {
|
void ModuleOp::print(OpAsmPrinter &p) {
|
||||||
p << "module";
|
p << "module";
|
||||||
|
|
||||||
Optional<StringRef> name = getName();
|
if (Optional<StringRef> name = getName()) {
|
||||||
if (name) {
|
|
||||||
p << ' ';
|
p << ' ';
|
||||||
p.printSymbolName(*name);
|
p.printSymbolName(*name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the module attributes.
|
// Print the module attributes.
|
||||||
auto attrs = getAttrs();
|
p.printOptionalAttrDictWithKeyword(getAttrs(),
|
||||||
if (!attrs.empty() && !(attrs.size() == 1 && name)) {
|
{mlir::SymbolTable::getSymbolAttrName()});
|
||||||
p << " attributes";
|
|
||||||
p.printOptionalAttrDict(attrs, {mlir::SymbolTable::getSymbolAttrName()});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print the region.
|
// Print the region.
|
||||||
p.printRegion(getOperation()->getRegion(0), /*printEntryBlockArgs=*/false,
|
p.printRegion(getOperation()->getRegion(0), /*printEntryBlockArgs=*/false,
|
||||||
|
|||||||
11
third_party/mlir/lib/Parser/Parser.cpp
vendored
11
third_party/mlir/lib/Parser/Parser.cpp
vendored
@ -1533,7 +1533,7 @@ Attribute Parser::parseAttribute(Type type) {
|
|||||||
///
|
///
|
||||||
ParseResult
|
ParseResult
|
||||||
Parser::parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes) {
|
Parser::parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes) {
|
||||||
if (!consumeIf(Token::l_brace))
|
if (parseToken(Token::l_brace, "expected '{' in attribute dictionary"))
|
||||||
return failure();
|
return failure();
|
||||||
|
|
||||||
auto parseElt = [&]() -> ParseResult {
|
auto parseElt = [&]() -> ParseResult {
|
||||||
@ -3874,6 +3874,15 @@ public:
|
|||||||
return parser.parseAttributeDict(result);
|
return parser.parseAttributeDict(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse a named dictionary into 'result' if the `attributes` keyword is
|
||||||
|
/// present.
|
||||||
|
ParseResult parseOptionalAttrDictWithKeyword(
|
||||||
|
SmallVectorImpl<NamedAttribute> &result) override {
|
||||||
|
if (failed(parseOptionalKeyword("attributes")))
|
||||||
|
return success();
|
||||||
|
return parser.parseAttributeDict(result);
|
||||||
|
}
|
||||||
|
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
// Identifier Parsing
|
// Identifier Parsing
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user