Skip to content

Commit 02d7b26

Browse files
committed
[mlir] Register the print-op-graph pass using ODS
Move over to ODS & use pass options.
1 parent 557d2ad commit 02d7b26

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

mlir/include/mlir/Transforms/Passes.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,4 +730,22 @@ def SymbolDCE : Pass<"symbol-dce"> {
730730
}];
731731
let constructor = "mlir::createSymbolDCEPass()";
732732
}
733+
734+
def ViewOpGraphPass : Pass<"symbol-dce", "ModuleOp"> {
735+
let summary = "Print graphviz view of module";
736+
let description = [{
737+
This pass prints a graphviz per block of a module.
738+
739+
- Op are represented as nodes;
740+
- Uses as edges;
741+
}];
742+
let constructor = "mlir::createPrintOpGraphPass()";
743+
let options = [
744+
Option<"title", "title", "std::string",
745+
/*default=*/"", "The prefix of the title of the graph">,
746+
Option<"shortNames", "short-names", "bool", /*default=*/"false",
747+
"Use short names">
748+
];
749+
}
750+
733751
#endif // MLIR_TRANSFORMS_PASSES

mlir/lib/Transforms/ViewOpGraph.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ namespace {
104104
// PrintOpPass is simple pass to write graph per function.
105105
// Note: this is a module pass only to avoid interleaving on the same ostream
106106
// due to multi-threading over functions.
107-
struct PrintOpPass : public PrintOpBase<PrintOpPass> {
108-
explicit PrintOpPass(raw_ostream &os = llvm::errs(), bool short_names = false,
109-
const Twine &title = "")
110-
: os(os), title(title.str()), short_names(short_names) {}
107+
class PrintOpPass : public ViewOpGraphPassBase<PrintOpPass> {
108+
public:
109+
PrintOpPass(raw_ostream &os, bool shortNames, const Twine &title) : os(os) {
110+
this->shortNames = shortNames;
111+
this->title = title.str();
112+
}
111113

112114
std::string getOpName(Operation &op) {
113115
auto symbolAttr =
@@ -133,7 +135,7 @@ struct PrintOpPass : public PrintOpBase<PrintOpPass> {
133135
auto blockName = llvm::hasSingleElement(region)
134136
? ""
135137
: ("__" + llvm::utostr(indexed_block.index()));
136-
llvm::WriteGraph(os, &indexed_block.value(), short_names,
138+
llvm::WriteGraph(os, &indexed_block.value(), shortNames,
137139
Twine(title) + opName + blockName);
138140
}
139141
}
@@ -144,9 +146,7 @@ struct PrintOpPass : public PrintOpBase<PrintOpPass> {
144146

145147
private:
146148
raw_ostream &os;
147-
std::string title;
148149
int unnamedOpCtr = 0;
149-
bool short_names;
150150
};
151151
} // namespace
152152

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: mlir-opt -allow-unregistered-dialect -print-op-graph %s -o %t 2>&1 | FileCheck %s
2+
3+
// CHECK-LABEL: digraph "merge_blocks"
4+
func @merge_blocks(%arg0: i32, %arg1 : i32) -> () {
5+
%0:2 = "test.merge_blocks"() ({
6+
^bb0:
7+
"test.br"(%arg0, %arg1)[^bb1] : (i32, i32) -> ()
8+
^bb1(%arg3 : i32, %arg4 : i32):
9+
"test.return"(%arg3, %arg4) : (i32, i32) -> ()
10+
}) : () -> (i32, i32)
11+
"test.return"(%0#0, %0#1) : (i32, i32) -> ()
12+
}

0 commit comments

Comments
 (0)