diff options
| author | River Riddle <riverriddle@google.com> | 2019-11-05 17:58:16 -0800 |
|---|---|---|
| committer | A. Unique TensorFlower <gardener@tensorflow.org> | 2019-11-05 17:58:48 -0800 |
| commit | 8e0f4860cdc07b4c681c434792cede24f16961e3 (patch) | |
| tree | 0738f42b14d9f8cfce24ae9b5ffcde9a384d772c /mlir/lib/IR | |
| parent | 500e858e6522c3b55c3f23e95689f146e09db43e (diff) | |
| download | bcm5719-llvm-8e0f4860cdc07b4c681c434792cede24f16961e3.tar.gz bcm5719-llvm-8e0f4860cdc07b4c681c434792cede24f16961e3.zip | |
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
Diffstat (limited to 'mlir/lib/IR')
| -rw-r--r-- | mlir/lib/IR/AsmPrinter.cpp | 34 | ||||
| -rw-r--r-- | mlir/lib/IR/FunctionSupport.cpp | 5 | ||||
| -rw-r--r-- | mlir/lib/IR/Module.cpp | 15 |
3 files changed, 27 insertions, 27 deletions
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp index 0e6b7882e14..af958c8e61f 100644 --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -421,7 +421,8 @@ public: protected: void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs, - ArrayRef<StringRef> elidedAttrs = {}); + ArrayRef<StringRef> elidedAttrs = {}, + bool withKeyword = false); void printTrailingLocation(Location loc); void printLocationInternal(LocationAttr loc, bool pretty = false); void printDenseElementsAttr(DenseElementsAttr attr); @@ -1327,27 +1328,26 @@ void ModulePrinter::printIntegerSet(IntegerSet set) { //===----------------------------------------------------------------------===// 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 (attrs.empty()) return; // Filter out any attributes that shouldn't be included. - SmallVector<NamedAttribute, 8> filteredAttrs; - for (auto attr : attrs) { - // If the caller has requested that this attribute be ignored, then drop it. - if (llvm::any_of(elidedAttrs, - [&](StringRef elided) { return attr.first.is(elided); })) - continue; - - // Otherwise add it to our filteredAttrs list. - filteredAttrs.push_back(attr); - } + SmallVector<NamedAttribute, 8> filteredAttrs( + llvm::make_filter_range(attrs, [&](NamedAttribute attr) { + return !llvm::is_contained(elidedAttrs, attr.first.strref()); + })); // If there are no attributes left to print after filtering, then we're done. if (filteredAttrs.empty()) return; + // Print the 'attributes' keyword if necessary. + if (withKeyword) + os << " attributes "; + // Otherwise, print them all out in braces. os << " {"; interleaveComma(filteredAttrs, [&](NamedAttribute attr) { @@ -1389,8 +1389,14 @@ public: void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs, 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 }; diff --git a/mlir/lib/IR/FunctionSupport.cpp b/mlir/lib/IR/FunctionSupport.cpp index aa9965e89d1..d1ba2d30fa1 100644 --- a/mlir/lib/IR/FunctionSupport.cpp +++ b/mlir/lib/IR/FunctionSupport.cpp @@ -183,9 +183,8 @@ mlir::impl::parseFunctionLikeOp(OpAsmParser &parser, OperationState &result, << (errorMessage.empty() ? "" : ": ") << errorMessage; // If function attributes are present, parse them. - if (succeeded(parser.parseOptionalKeyword("attributes"))) - if (parser.parseOptionalAttrDict(result.attributes)) - return failure(); + if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) + return failure(); // Add the attributes to the function arguments. SmallString<8> attrNameBuf; diff --git a/mlir/lib/IR/Module.cpp b/mlir/lib/IR/Module.cpp index 3a08ee3bf27..f5cc98e39cf 100644 --- a/mlir/lib/IR/Module.cpp +++ b/mlir/lib/IR/Module.cpp @@ -48,9 +48,8 @@ ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) { result.attributes); // If module attributes are present, parse them. - if (succeeded(parser.parseOptionalKeyword("attributes"))) - if (parser.parseOptionalAttrDict(result.attributes)) - return failure(); + if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) + return failure(); // Parse the module body. auto *body = result.addRegion(); @@ -65,18 +64,14 @@ ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) { void ModuleOp::print(OpAsmPrinter &p) { p << "module"; - Optional<StringRef> name = getName(); - if (name) { + if (Optional<StringRef> name = getName()) { p << ' '; p.printSymbolName(*name); } // Print the module attributes. - auto attrs = getAttrs(); - if (!attrs.empty() && !(attrs.size() == 1 && name)) { - p << " attributes"; - p.printOptionalAttrDict(attrs, {mlir::SymbolTable::getSymbolAttrName()}); - } + p.printOptionalAttrDictWithKeyword(getAttrs(), + {mlir::SymbolTable::getSymbolAttrName()}); // Print the region. p.printRegion(getOperation()->getRegion(0), /*printEntryBlockArgs=*/false, |

