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=*/"false", "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 @@ -149,7 +149,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. @@ -201,14 +204,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 (this->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 (this->printAttrs) { + os << "\n"; + for (auto attr : op->getAttrs()) { + os << '\n' << attr.first << ": "; + emitMlirAttr(os, attr.second); + } } return os.str(); @@ -278,6 +290,13 @@ processBlock(block); } + /// Truncate long strings. + std::string truncateString(std::string str) { + if (str.length() <= this->maxAttrLen) + return str; + return str.substr(0, this->maxAttrLen) + "..."; + } + // Current indentation (for writing to `os`). unsigned indent = 0; // Output stream to write DOT file to.