diff --git a/mlir/include/mlir/Transforms/Passes.td b/mlir/include/mlir/Transforms/Passes.td --- a/mlir/include/mlir/Transforms/Passes.td +++ b/mlir/include/mlir/Transforms/Passes.td @@ -697,6 +697,14 @@ - Uses as edges; - Regions/blocks as subgraphs. }]; + let options = [ + Option<"maxAttrLen", "max-attr-len", "unsigned", + /*default=*/"20", "Limit attribute/type length to number of chars">, + Option<"printAttrs", "print-attrs", "bool", + /*default=*/"true", "Print attributes of operations">, + Option<"printResultTypes", "print-result-types", "bool", + /*default=*/"true", "Print result types of operations"> + ]; let constructor = "mlir::createPrintOpGraphPass()"; } diff --git a/mlir/lib/Transforms/ViewOpGraph.cpp b/mlir/lib/Transforms/ViewOpGraph.cpp --- a/mlir/lib/Transforms/ViewOpGraph.cpp +++ b/mlir/lib/Transforms/ViewOpGraph.cpp @@ -143,7 +143,10 @@ } // Print all other attributes. - attr.print(os); + std::string buf; + llvm::raw_string_ostream ss(buf); + attr.print(ss); + os << truncateString(ss.str()); } /// Append an edge to the list of edges. @@ -199,14 +202,23 @@ llvm::raw_string_ostream os(buf); // Print operation name and type. - os << op->getName() << " : ("; - interleaveComma(op->getResultTypes(), os); - os << ")\n"; + os << op->getName(); + if (printResultTypes) { + os << " : ("; + std::string buf; + llvm::raw_string_ostream ss(buf); + interleaveComma(op->getResultTypes(), ss); + os << truncateString(ss.str()) << ")"; + os << ")"; + } // Print attributes. - for (auto attr : op->getAttrs()) { - os << '\n' << attr.first << ": "; - emitMlirAttr(os, attr.second); + if (printAttrs) { + os << "\n"; + for (auto attr : op->getAttrs()) { + os << '\n' << attr.first << ": "; + emitMlirAttr(os, attr.second); + } } return os.str(); @@ -274,6 +286,13 @@ processBlock(block); } + /// Truncate long strings. + std::string truncateString(std::string str) { + if (str.length() <= maxAttrLen) + return str; + return str.substr(0, maxAttrLen) + "..."; + } + // Output stream to write DOT file to. raw_indented_ostream os; raw_ostream &plainOs;