[mlir][AsmPrinter] Don't use string comparison when filtering list attributes

In .mlir modules with larges amounts of attributes, e.g. a function with a larger number of argument attributes, the string comparison filtering greatly affects compile time. This revision switches to using a SmallDenseSet in these situations, resulting in over a 10x speed up in some situations.

Differential Revision: https://reviews.llvm.org/D97980
This commit is contained in:
River Riddle 2021-03-05 12:42:24 -08:00
parent 29812a6195
commit f175ba4a54
1 changed files with 34 additions and 23 deletions

View File

@ -441,13 +441,18 @@ private:
/// 'elidedAttrs'. /// 'elidedAttrs'.
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs, void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
ArrayRef<StringRef> elidedAttrs = {}) override { ArrayRef<StringRef> elidedAttrs = {}) override {
// Filter out any attributes that shouldn't be included. if (attrs.empty())
SmallVector<NamedAttribute, 8> filteredAttrs( return;
llvm::make_filter_range(attrs, [&](NamedAttribute attr) { if (elidedAttrs.empty()) {
return !llvm::is_contained(elidedAttrs, attr.first.strref()); for (const NamedAttribute &attr : attrs)
})); printAttribute(attr.second);
for (const NamedAttribute &attr : filteredAttrs) return;
printAttribute(attr.second); }
llvm::SmallDenseSet<StringRef> elidedAttrsSet(elidedAttrs.begin(),
elidedAttrs.end());
for (const NamedAttribute &attr : attrs)
if (!elidedAttrsSet.contains(attr.first.strref()))
printAttribute(attr.second);
} }
void printOptionalAttrDictWithKeyword( void printOptionalAttrDictWithKeyword(
ArrayRef<NamedAttribute> attrs, ArrayRef<NamedAttribute> attrs,
@ -1916,25 +1921,31 @@ void ModulePrinter::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
if (attrs.empty()) if (attrs.empty())
return; return;
// Filter out any attributes that shouldn't be included. // Functor used to print a filtered attribute list.
SmallVector<NamedAttribute, 8> filteredAttrs( auto printFilteredAttributesFn = [&](auto filteredAttrs) {
llvm::make_filter_range(attrs, [&](NamedAttribute attr) { // Print the 'attributes' keyword if necessary.
return !llvm::is_contained(elidedAttrs, attr.first.strref()); if (withKeyword)
})); os << " attributes";
// If there are no attributes left to print after filtering, then we're done. // Otherwise, print them all out in braces.
if (filteredAttrs.empty()) os << " {";
return; interleaveComma(filteredAttrs,
[&](NamedAttribute attr) { printNamedAttribute(attr); });
os << '}';
};
// Print the 'attributes' keyword if necessary. // If no attributes are elided, we can directly print with no filtering.
if (withKeyword) if (elidedAttrs.empty())
os << " attributes"; return printFilteredAttributesFn(attrs);
// Otherwise, print them all out in braces. // Otherwise, filter out any attributes that shouldn't be included.
os << " {"; llvm::SmallDenseSet<StringRef> elidedAttrsSet(elidedAttrs.begin(),
interleaveComma(filteredAttrs, elidedAttrs.end());
[&](NamedAttribute attr) { printNamedAttribute(attr); }); auto filteredAttrs = llvm::make_filter_range(attrs, [&](NamedAttribute attr) {
os << '}'; return !elidedAttrsSet.contains(attr.first.strref());
});
if (!filteredAttrs.empty())
printFilteredAttributesFn(filteredAttrs);
} }
void ModulePrinter::printNamedAttribute(NamedAttribute attr) { void ModulePrinter::printNamedAttribute(NamedAttribute attr) {