Skip to content

Commit 0881b6e

Browse files
committed
Add comments and missing inserts
1 parent 08ad17a commit 0881b6e

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
@@ -42,11 +42,27 @@ getForwardSliceImpl(Operation *op, DenseSet<Operation *> &visited,
4242
for (Region &region : op->getRegions())
4343
for (Block &block : region)
4444
for (Operation &blockOp : block)
45-
if (forwardSlice->count(&blockOp) == 0)
45+
if (forwardSlice->count(&blockOp) == 0) {
46+
// We don't have to check if the 'blockOp' is already visited because
47+
// there cannot be a traversal path from this nested op to the parent
48+
// and thus a cycle cannot be closed here. We still have to mark it
49+
// as visited to stop before visiting this operation again if it is
50+
// part of a cycle.
51+
visited.insert(&blockOp);
4652
getForwardSliceImpl(&blockOp, visited, forwardSlice, filter);
53+
visited.erase(&blockOp);
54+
}
4755

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

@@ -59,6 +75,7 @@ getForwardSliceImpl(Operation *op, DenseSet<Operation *> &visited,
5975
void mlir::getForwardSlice(Operation *op, SetVector<Operation *> *forwardSlice,
6076
const ForwardSliceOptions &options) {
6177
DenseSet<Operation *> visited;
78+
visited.insert(op);
6279
getForwardSliceImpl(op, visited, forwardSlice, options.filter);
6380
if (!options.inclusive) {
6481
// Don't insert the top level operation, we just queried on it and don't
@@ -76,8 +93,11 @@ void mlir::getForwardSlice(Operation *op, SetVector<Operation *> *forwardSlice,
7693
void mlir::getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
7794
const SliceOptions &options) {
7895
DenseSet<Operation *> visited;
79-
for (Operation *user : root.getUsers())
96+
for (Operation *user : root.getUsers()) {
97+
visited.insert(user);
8098
getForwardSliceImpl(user, visited, forwardSlice, options.filter);
99+
visited.erase(user);
100+
}
81101

82102
// Reverse to get back the actual topological order.
83103
// std::reverse does not work out of the box on SetVector and I want an
@@ -148,6 +168,7 @@ void mlir::getBackwardSlice(Operation *op,
148168
SetVector<Operation *> *backwardSlice,
149169
const BackwardSliceOptions &options) {
150170
DenseSet<Operation *> visited;
171+
visited.insert(op);
151172
getBackwardSliceImpl(op, visited, backwardSlice, options);
152173

153174
if (!options.inclusive) {

0 commit comments

Comments
 (0)