Skip to content

Commit 363a093

Browse files
committed
Add comments and missing inserts
1 parent d5d2790 commit 363a093

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

mlir/lib/Analysis/SliceAnalysis.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,27 @@ getForwardSliceImpl(Operation *op, DenseSet<Operation *> &visited,
4141
for (Region &region : op->getRegions())
4242
for (Block &block : region)
4343
for (Operation &blockOp : block)
44-
if (forwardSlice->count(&blockOp) == 0)
44+
if (forwardSlice->count(&blockOp) == 0) {
45+
// We don't have to check if the 'blockOp' is already visited because
46+
// there cannot be a traversal path from this nested op to the parent
47+
// and thus a cycle cannot be closed here. We still have to mark it
48+
// as visited to stop before visiting this operation again if it is
49+
// part of a cycle.
50+
visited.insert(&blockOp);
4551
getForwardSliceImpl(&blockOp, visited, forwardSlice, filter);
52+
visited.erase(&blockOp);
53+
}
4654

4755
for (Value result : op->getResults())
4856
for (Operation *userOp : result.getUsers()) {
57+
// A cycle can only occur within a basic block (not across regions or
58+
// basic blocks) because the parent region must be a graph region, graph
59+
// regions are restricted to always have 0 or 1 blocks, and there cannot
60+
// be a def-use edge from a nested operation to an operation in an
61+
// ancestor region. Therefore, we don't have to but may use the same
62+
// 'visited' set across regions/blocks as long as we remove operations
63+
// from the set again when the DFS traverses back from the leaf to the
64+
// root.
4965
if (forwardSlice->count(userOp) == 0 && visited.insert(userOp).second)
5066
getForwardSliceImpl(userOp, visited, forwardSlice, filter);
5167

@@ -58,6 +74,7 @@ getForwardSliceImpl(Operation *op, DenseSet<Operation *> &visited,
5874
void mlir::getForwardSlice(Operation *op, SetVector<Operation *> *forwardSlice,
5975
const ForwardSliceOptions &options) {
6076
DenseSet<Operation *> visited;
77+
visited.insert(op);
6178
getForwardSliceImpl(op, visited, forwardSlice, options.filter);
6279
if (!options.inclusive) {
6380
// Don't insert the top level operation, we just queried on it and don't
@@ -75,8 +92,11 @@ void mlir::getForwardSlice(Operation *op, SetVector<Operation *> *forwardSlice,
7592
void mlir::getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
7693
const SliceOptions &options) {
7794
DenseSet<Operation *> visited;
78-
for (Operation *user : root.getUsers())
95+
for (Operation *user : root.getUsers()) {
96+
visited.insert(user);
7997
getForwardSliceImpl(user, visited, forwardSlice, options.filter);
98+
visited.erase(user);
99+
}
80100

81101
// Reverse to get back the actual topological order.
82102
// std::reverse does not work out of the box on SetVector and I want an
@@ -158,6 +178,7 @@ LogicalResult mlir::getBackwardSlice(Operation *op,
158178
SetVector<Operation *> *backwardSlice,
159179
const BackwardSliceOptions &options) {
160180
DenseSet<Operation *> visited;
181+
visited.insert(op);
161182
LogicalResult result =
162183
getBackwardSliceImpl(op, visited, backwardSlice, options);
163184

0 commit comments

Comments
 (0)