Skip to content

[mlir] Handle cycles and back edges in --view-op-graph #82002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions mlir/lib/Transforms/ViewOpGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "mlir/IR/Operation.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Support/IndentedOstream.h"
#include "mlir/Transforms/TopologicalSortUtils.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/GraphWriter.h"
#include <map>
Expand Down Expand Up @@ -126,6 +127,12 @@ class PrintOpPass : public impl::ViewOpGraphBase<PrintOpPass> {
/// Emit all edges. This function should be called after all nodes have been
/// emitted.
void emitAllEdgeStmts() {
if (printDataFlowEdges) {
for (const auto &[value, node, label] : dataFlowEdges) {
emitEdgeStmt(valueToNode[value], node, label, kLineStyleDataFlow);
}
}

for (const std::string &edge : edges)
os << edge << ";\n";
edges.clear();
Expand Down Expand Up @@ -313,9 +320,8 @@ class PrintOpPass : public impl::ViewOpGraphBase<PrintOpPass> {
if (printDataFlowEdges) {
unsigned numOperands = op->getNumOperands();
for (unsigned i = 0; i < numOperands; i++)
emitEdgeStmt(valueToNode[op->getOperand(i)], node,
/*label=*/numOperands == 1 ? "" : std::to_string(i),
kLineStyleDataFlow);
dataFlowEdges.push_back({op->getOperand(i), node,
numOperands == 1 ? "" : std::to_string(i)});
}

for (Value result : op->getResults())
Expand Down Expand Up @@ -344,6 +350,8 @@ class PrintOpPass : public impl::ViewOpGraphBase<PrintOpPass> {
std::vector<std::string> edges;
/// Mapping of SSA values to Graphviz nodes/clusters.
DenseMap<Value, Node> valueToNode;
/// Output for data flow edges is delayed until the end to handle cycles
std::vector<std::tuple<Value, Node, std::string>> dataFlowEdges;
/// Counter for generating unique node/subgraph identifiers.
int counter = 0;

Expand Down
24 changes: 24 additions & 0 deletions mlir/test/Transforms/print-op-graph-back-edges.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: mlir-opt -view-op-graph %s -o %t 2>&1 | FileCheck -check-prefix=DFG %s

// DFG-LABEL: digraph G {
// DFG: compound = true;
// DFG: subgraph cluster_1 {
// DFG: v2 [label = " ", shape = plain];
// DFG: label = "builtin.module : ()\n";
// DFG: subgraph cluster_3 {
// DFG: v4 [label = " ", shape = plain];
// DFG: label = "";
// DFG: v5 [fillcolor = "0.000000 1.0 1.0", label = "arith.addi : (index)\n\noverflowFlags: #arith.overflow<none...", shape = ellipse, style = filled];
// DFG: v6 [fillcolor = "0.333333 1.0 1.0", label = "arith.constant : (index)\n\nvalue: 0 : index", shape = ellipse, style = filled];
// DFG: v7 [fillcolor = "0.333333 1.0 1.0", label = "arith.constant : (index)\n\nvalue: 1 : index", shape = ellipse, style = filled];
// DFG: }
// DFG: }
// DFG: v6 -> v5 [label = "0", style = solid];
// DFG: v7 -> v5 [label = "1", style = solid];
// DFG: }

module {
%add = arith.addi %c0, %c1 : index
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
}
51 changes: 51 additions & 0 deletions mlir/test/Transforms/print-op-graph-cycles.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// RUN: mlir-opt -view-op-graph -allow-unregistered-dialect %s -o %t 2>&1 | FileCheck -check-prefix=DFG %s

// DFG-LABEL: digraph G {
// DFG: compound = true;
// DFG: subgraph cluster_1 {
// DFG: v2 [label = " ", shape = plain];
// DFG: label = "builtin.module : ()\n";
// DFG: subgraph cluster_3 {
// DFG: v4 [label = " ", shape = plain];
// DFG: label = "";
// DFG: subgraph cluster_5 {
// DFG: v6 [label = " ", shape = plain];
// DFG: label = "test.graph_region : ()\n";
// DFG: subgraph cluster_7 {
// DFG: v8 [label = " ", shape = plain];
// DFG: label = "";
// DFG: v9 [fillcolor = "0.000000 1.0 1.0", label = "op1 : (i32)\n", shape = ellipse, style = filled];
// DFG: subgraph cluster_10 {
// DFG: v11 [label = " ", shape = plain];
// DFG: label = "test.ssacfg_region : (i32)\n";
// DFG: subgraph cluster_12 {
// DFG: v13 [label = " ", shape = plain];
// DFG: label = "";
// DFG: v14 [fillcolor = "0.166667 1.0 1.0", label = "op2 : (i32)\n", shape = ellipse, style = filled];
// DFG: }
// DFG: }
// DFG: v15 [fillcolor = "0.166667 1.0 1.0", label = "op2 : (i32)\n", shape = ellipse, style = filled];
// DFG: v16 [fillcolor = "0.500000 1.0 1.0", label = "op3 : (i32)\n", shape = ellipse, style = filled];
// DFG: }
// DFG: }
// DFG: }
// DFG: }
// DFG: v9 -> v9 [label = "0", style = solid];
// DFG: v15 -> v9 [label = "1", style = solid];
// DFG: v9 -> v14 [label = "0", style = solid];
// DFG: v11 -> v14 [ltail = cluster_10, style = solid];
// DFG: v15 -> v14 [label = "2", style = solid];
// DFG: v16 -> v14 [label = "3", style = solid];
// DFG: v9 -> v15 [label = "0", style = solid];
// DFG: v16 -> v15 [label = "1", style = solid];
// DFG: v9 -> v16 [label = "", style = solid];
// DFG: }

"test.graph_region"() ({ // A Graph region
%1 = "op1"(%1, %3) : (i32, i32) -> (i32) // OK: %1, %3 allowed here
%2 = "test.ssacfg_region"() ({
%5 = "op2"(%1, %2, %3, %4) : (i32, i32, i32, i32) -> (i32) // OK: %1, %2, %3, %4 all defined in the containing region
}) : () -> (i32)
%3 = "op2"(%1, %4) : (i32, i32) -> (i32) // OK: %4 allowed here
%4 = "op3"(%1) : (i32) -> (i32)
}) : () -> ()