Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary Attributes with new lines. #74

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,12 @@ class AsmPrinter::Impl {

/// Print the given attribute or an alias.
void printAttribute(Attribute attr,
AttrTypeElision typeElision = AttrTypeElision::Never);
AttrTypeElision typeElision = AttrTypeElision::Never,
SmallString<16> separator = StringRef(", "));
/// Print the given attribute without considering an alias.
void printAttributeImpl(Attribute attr,
AttrTypeElision typeElision = AttrTypeElision::Never);
AttrTypeElision typeElision = AttrTypeElision::Never,
SmallString<16> separator = StringRef(", "));

/// Print the alias for the given attribute, return failure if no alias could
/// be printed.
Expand Down Expand Up @@ -422,8 +424,10 @@ class AsmPrinter::Impl {
protected:
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
ArrayRef<StringRef> elidedAttrs = {},
unsigned currentIndent = 0, bool withKeyword = false);
void printNamedAttribute(NamedAttribute attr);
unsigned currentIndent = 0,
bool withKeyword = false);
void printNamedAttribute(NamedAttribute attr,
SmallString<16> separator = StringRef(", "));
void printTrailingLocation(Location loc, bool allowAlias = true);
void printLocationInternal(LocationAttr loc, bool pretty = false,
bool isTopLevel = false);
Expand Down Expand Up @@ -2110,7 +2114,8 @@ LogicalResult AsmPrinter::Impl::printAlias(Type type) {
}

void AsmPrinter::Impl::printAttribute(Attribute attr,
AttrTypeElision typeElision) {
AttrTypeElision typeElision,
SmallString<16> separator) {
if (!attr) {
os << "<<NULL ATTRIBUTE>>";
return;
Expand All @@ -2119,11 +2124,12 @@ void AsmPrinter::Impl::printAttribute(Attribute attr,
// Try to print an alias for this attribute.
if (succeeded(printAlias(attr)))
return;
return printAttributeImpl(attr, typeElision);
return printAttributeImpl(attr, typeElision, separator);
}

void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
AttrTypeElision typeElision) {
AttrTypeElision typeElision,
SmallString<16> separator) {
if (!isa<BuiltinDialect>(attr.getDialect())) {
printDialectAttribute(attr);
} else if (auto opaqueAttr = llvm::dyn_cast<OpaqueAttr>(attr)) {
Expand All @@ -2134,8 +2140,9 @@ void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
return;
} else if (auto dictAttr = llvm::dyn_cast<DictionaryAttr>(attr)) {
os << '{';
interleaveComma(dictAttr.getValue(),
[&](NamedAttribute attr) { printNamedAttribute(attr); });
interleave(
dictAttr.getValue(),
[&](NamedAttribute attr) { printNamedAttribute(attr); }, separator);
os << '}';

} else if (auto intAttr = llvm::dyn_cast<IntegerAttr>(attr)) {
Expand Down Expand Up @@ -2171,7 +2178,7 @@ void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
} else if (auto arrayAttr = llvm::dyn_cast<ArrayAttr>(attr)) {
os << '[';
interleaveComma(arrayAttr.getValue(), [&](Attribute attr) {
printAttribute(attr, AttrTypeElision::May);
printAttribute(attr, AttrTypeElision::May, separator);
});
os << ']';

Expand Down Expand Up @@ -2590,7 +2597,7 @@ void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
attrs.size() > *printerFlags.getNewlineAfterAttrLimit()) {

// Increase indent to match the visually match the "{ " below.
//currentIndent += 2;
// currentIndent += 2;

separator.clear();
separator.reserve(currentIndent + 2);
Expand All @@ -2605,7 +2612,8 @@ void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,

// Otherwise, print them all out in braces.
interleave(
filteredAttrs, [&](NamedAttribute attr) { printNamedAttribute(attr); },
filteredAttrs,
[&](NamedAttribute attr) { printNamedAttribute(attr, separator); },
mgehre-amd marked this conversation as resolved.
Show resolved Hide resolved
separator);
os << '}';
};
Expand All @@ -2623,7 +2631,8 @@ void AsmPrinter::Impl::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
if (!filteredAttrs.empty())
printFilteredAttributesFn(filteredAttrs);
}
void AsmPrinter::Impl::printNamedAttribute(NamedAttribute attr) {
void AsmPrinter::Impl::printNamedAttribute(NamedAttribute attr,
SmallString<16> separator) {
// Print the name without quotes if possible.
::printKeywordOrString(attr.getName().strref(), os);

Expand All @@ -2632,7 +2641,8 @@ void AsmPrinter::Impl::printNamedAttribute(NamedAttribute attr) {
return;

os << " = ";
printAttribute(attr.getValue());
printAttribute(attr.getValue(), /*typeElision*/ AttrTypeElision::Never,
separator);
}

void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
Expand Down Expand Up @@ -3026,7 +3036,8 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
/// Print an optional attribute dictionary with a given set of elided values.
void printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
ArrayRef<StringRef> elidedAttrs = {}) override {
Impl::printOptionalAttrDict(attrs, elidedAttrs, currentIndent + indentWidth);
Impl::printOptionalAttrDict(attrs, elidedAttrs,
currentIndent + indentWidth);
}
void printOptionalAttrDictWithKeyword(
ArrayRef<NamedAttribute> attrs,
Expand Down