summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mlir/lib/IR/AsmPrinter.cpp42
-rw-r--r--mlir/test/IR/pretty-attributes.mlir12
2 files changed, 37 insertions, 17 deletions
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index 655a776118c..1d3f9d74403 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -733,6 +733,19 @@ static void printSymbolReference(StringRef symbolRef, raw_ostream &os) {
os << '"';
}
+// Print out a valid ElementsAttr that is succinct and can represent any
+// potential shape/type, for use when eliding a large ElementsAttr.
+//
+// We choose to use an opaque ElementsAttr literal with conspicuous content to
+// hopefully alert readers to the fact that this has been elided.
+//
+// Unfortunately, neither of the strings of an opaque ElementsAttr literal will
+// accept the string "elided". The first string must be a registered dialect
+// name and the latter must be a hex constant.
+static void printElidedElementsAttr(raw_ostream &os) {
+ os << R"(opaque<"", "0xDEADBEEF">)";
+}
+
void ModulePrinter::printAttribute(Attribute attr, bool mayElideType) {
if (!attr) {
os << "<<NULL ATTRIBUTE>>";
@@ -836,19 +849,20 @@ void ModulePrinter::printAttribute(Attribute attr, bool mayElideType) {
}
case StandardAttributes::OpaqueElements: {
auto eltsAttr = attr.cast<OpaqueElementsAttr>();
+ if (printerFlags.shouldElideElementsAttr(eltsAttr)) {
+ printElidedElementsAttr(os);
+ break;
+ }
os << "opaque<\"" << eltsAttr.getDialect()->getNamespace() << "\", ";
- os << '"' << "0x";
-
- // Check for large ElementsAttr elision.
- if (printerFlags.shouldElideElementsAttr(eltsAttr))
- os << "...";
- else
- os << llvm::toHex(eltsAttr.getValue());
- os << "\">";
+ os << '"' << "0x" << llvm::toHex(eltsAttr.getValue()) << "\">";
break;
}
case StandardAttributes::DenseElements: {
auto eltsAttr = attr.cast<DenseElementsAttr>();
+ if (printerFlags.shouldElideElementsAttr(eltsAttr)) {
+ printElidedElementsAttr(os);
+ break;
+ }
os << "dense<";
printDenseElementsAttr(eltsAttr);
os << '>';
@@ -856,6 +870,11 @@ void ModulePrinter::printAttribute(Attribute attr, bool mayElideType) {
}
case StandardAttributes::SparseElements: {
auto elementsAttr = attr.cast<SparseElementsAttr>();
+ if (printerFlags.shouldElideElementsAttr(elementsAttr.getIndices()) ||
+ printerFlags.shouldElideElementsAttr(elementsAttr.getValues())) {
+ printElidedElementsAttr(os);
+ break;
+ }
os << "sparse<";
printDenseElementsAttr(elementsAttr.getIndices());
os << ", ";
@@ -916,13 +935,6 @@ void ModulePrinter::printDenseElementsAttr(DenseElementsAttr attr) {
return;
}
- // Check for large elements attr elision. We explicitly check *after* splat,
- // as the splat printing is already elided.
- if (printerFlags.shouldElideElementsAttr(attr)) {
- os << "...";
- return;
- }
-
// Special case for degenerate tensors.
auto numElements = type.getNumElements();
if (numElements == 0) {
diff --git a/mlir/test/IR/pretty-attributes.mlir b/mlir/test/IR/pretty-attributes.mlir
index cadb2dae602..bbcbe5b7b38 100644
--- a/mlir/test/IR/pretty-attributes.mlir
+++ b/mlir/test/IR/pretty-attributes.mlir
@@ -1,10 +1,18 @@
// RUN: mlir-opt %s -mlir-elide-elementsattrs-if-larger=2 | FileCheck %s
+// Ensure that the elided version is still parseable, although depending on
+// what has been elided, it may not be semantically meaningful.
+// In the typical case where what is being elided is a very large constant
+// tensor which passes don't look at directly, this isn't an issue.
+// RUN: mlir-opt %s -mlir-elide-elementsattrs-if-larger=2 | mlir-opt
-// CHECK: dense<...> : tensor<3xi32>
+// CHECK: opaque<"", "0xDEADBEEF"> : tensor<3xi32>
"test.dense_attr"() {foo.dense_attr = dense<[1, 2, 3]> : tensor<3xi32>} : () -> ()
// CHECK: dense<[1, 2]> : tensor<2xi32>
"test.non_elided_dense_attr"() {foo.dense_attr = dense<[1, 2]> : tensor<2xi32>} : () -> ()
-// CHECK: sparse<..., -2.{{0+}}e+00> : vector<1x1x1xf16>
+// CHECK: opaque<"", "0xDEADBEEF"> : vector<1x1x1xf16>
"test.sparse_attr"() {foo.sparse_attr = sparse<[[1, 2, 3]], -2.0> : vector<1x1x1xf16>} : () -> ()
+
+// CHECK: opaque<"", "0xDEADBEEF"> : tensor<100xf32>
+"test.opaue_attr"() {foo.opaque_attr = opaque<"", "0xEBFE"> : tensor<100xf32> } : () -> ()
OpenPOWER on IntegriCloud