Skip to content

Commit

Permalink
Sezgin.fix newline in arrayattr containing dictattr (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Murat Sezgin Baloglu <murat.sezgin.baloglu@xilinx.com>
  • Loading branch information
sezgin1947 and Murat Sezgin Baloglu authored Sep 15, 2023
1 parent dd37d22 commit 7edad39
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 28 deletions.
60 changes: 47 additions & 13 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2140,18 +2140,27 @@ void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
return;
} else if (auto dictAttr = llvm::dyn_cast<DictionaryAttr>(attr)) {
os << '{';
if (printerFlags.getNewlineAfterAttrLimit() &&
llvm::dyn_cast<DictionaryAttr>(attr).getValue().vec().size() >
*printerFlags.getNewlineAfterAttrLimit() &&
separator.size() > 2) {
SmallString<16> separatorBracket = StringRef("");
bool breakOnNewLine =
printerFlags.getNewlineAfterAttrLimit() && separator.size() > 2;
if (breakOnNewLine) {
separator.reserve(separator.capacity() + 2);
separator.push_back(' ');
separator.append(" ");
separatorBracket.reserve(separator.size() + 2);
separatorBracket.push_back('\n');
for (size_t i = 0; i < separator.size() - 2; ++i) {
separatorBracket.push_back(' ');
}
os << separatorBracket;
}
interleave(
dictAttr.getValue(),
[&](NamedAttribute attr) { printNamedAttribute(attr); }, separator);
if (breakOnNewLine) {
separatorBracket.pop_back_n(2);
os << separatorBracket;
}
os << '}';

} else if (auto intAttr = llvm::dyn_cast<IntegerAttr>(attr)) {
Type intType = intAttr.getType();
if (intType.isSignlessInteger(1)) {
Expand All @@ -2161,9 +2170,9 @@ void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
return;
}

// Only print attributes as unsigned if they are explicitly unsigned or are
// signless 1-bit values. Indexes, signed values, and multi-bit signless
// values print as signed.
// Only print attributes as unsigned if they are explicitly unsigned or
// are signless 1-bit values. Indexes, signed values, and multi-bit
// signless values print as signed.
bool isUnsigned =
intType.isUnsignedInteger() || intType.isSignlessInteger(1);
intAttr.getValue().print(os, !isUnsigned);
Expand All @@ -2184,11 +2193,36 @@ 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, separator);
});
bool breakOnNewLine =
printerFlags.getNewlineAfterAttrLimit() && separator.size() > 2;
bool isDictAttrPresent = false;
for (auto attribute : arrayAttr.getValue()) {
if (auto dictAttr = llvm::dyn_cast<DictionaryAttr>(attribute))
isDictAttrPresent = true;
}
if (isDictAttrPresent && breakOnNewLine) {
separator.reserve(separator.capacity() + 2);
separator.append(" ");
SmallString<16> separatorBracket = StringRef("\n");
separatorBracket.reserve(separator.size() + 2);
for (size_t i = 0; i < separator.size() - 2; ++i) {
separatorBracket.push_back(' ');
}
os << separatorBracket;
interleave(
arrayAttr.getValue(),
[&](Attribute attr) {
printAttribute(attr, AttrTypeElision::May, separator);
},
separator);
separatorBracket.pop_back_n(2);
os << separatorBracket;
} else {
interleaveComma(arrayAttr.getValue(), [&](Attribute attr) {
printAttribute(attr, AttrTypeElision::May, separator);
});
}
os << ']';

} else if (auto affineMapAttr = llvm::dyn_cast<AffineMapAttr>(attr)) {
os << "affine_map<";
affineMapAttr.getValue().print(os);
Expand Down
37 changes: 22 additions & 15 deletions mlir/test/IR/mlir-newline-after-attr.mlir
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
// RUN: mlir-opt %s -mlir-newline-after-attr=2 | FileCheck %s
// RUN: mlir-opt %s -mlir-newline-after-attr=2 | FileCheck --strict-whitespace --match-full-lines %s
// Ensure that the printed version is still parseable.
// RUN: mlir-opt %s -mlir-newline-after-attr=2 | mlir-opt

// CHECK: foo.dense_attr =
// CHECK: "test.op"() {foo.dense_attr = dense<1> : tensor<3xi32>} : () -> ()
"test.op"() {foo.dense_attr = dense<1> : tensor<3xi32>} : () -> ()

// CHECK: foo.dense_attr =
// CHECK: foo.second_attr =
// CHECK: "test.op"() {foo.dense_attr = dense<1> : tensor<3xi32>, foo.second_attr = dense<2> : tensor<3xi32>} : () -> ()
"test.op"() {foo.dense_attr = dense<1> : tensor<3xi32>, foo.second_attr = dense<2> : tensor<3xi32>} : () -> ()

// CHECK: Operands = [{foo.vect_attr_1_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_start = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_start = dense<0> : vector<3xindex>}, {foo.vect_attr_1_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_start = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_start = dense<0> : vector<3xindex>}],
// CHECK: "test.op"() {
// CHECK-NEXT: Operands = [
// CHECK-NEXT: {
// CHECK-NEXT: foo.vect_attr_1_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_start = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_start = dense<0> : vector<3xindex>
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: foo.vect_attr_1_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_1_start = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_count = dense<1> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_end = dense<0> : vector<3xindex>,
// CHECK-NEXT: foo.vect_attr_2_start = dense<0> : vector<3xindex>
// CHECK-NEXT: }
// CHECK-NEXT: ],
"test.op"() {foo.dense_attr = dense<1> : tensor<3xi32>, foo.second_attr = dense<2> : tensor<3xi32>, Operands = [{foo.vect_attr_1_start = dense<0> : vector<3xindex>, foo.vect_attr_1_end = dense<0> : vector<3xindex>, foo.vect_attr_1_count = dense<1> : vector<3xindex>, foo.vect_attr_2_start = dense<0> : vector<3xindex>, foo.vect_attr_2_end = dense<0> : vector<3xindex>, foo.vect_attr_2_count = dense<1> : vector<3xindex>}, {foo.vect_attr_1_start = dense<0> : vector<3xindex>, foo.vect_attr_1_end = dense<0> : vector<3xindex>, foo.vect_attr_1_count = dense<1> : vector<3xindex>, foo.vect_attr_2_start = dense<0> : vector<3xindex>, foo.vect_attr_2_end = dense<0> : vector<3xindex>, foo.vect_attr_2_count = dense<1> : vector<3xindex>}]} : () -> ()

0 comments on commit 7edad39

Please sign in to comment.