Skip to content

Commit d5d2790

Browse files
committed
[mlir][SliceAnalysis] Fix stack overflow in graph regions
1 parent fda3fbe commit d5d2790

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

mlir/include/mlir/Analysis/SliceAnalysis.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ using ForwardSliceOptions = SliceOptions;
6565
///
6666
/// The implementation traverses the use chains in postorder traversal for
6767
/// efficiency reasons: if an operation is already in `forwardSlice`, no
68-
/// need to traverse its uses again. Since use-def chains form a DAG, this
69-
/// terminates.
68+
/// need to traverse its uses again. In the presence of use-def cycles in a
69+
/// graph region, the traversal stops at the first operation that was already
70+
/// visited (which is not added to the slice anymore).
7071
///
7172
/// Upon return to the root call, `forwardSlice` is filled with a
7273
/// postorder list of uses (i.e. a reverse topological order). To get a proper
@@ -114,8 +115,9 @@ void getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
114115
///
115116
/// The implementation traverses the def chains in postorder traversal for
116117
/// efficiency reasons: if an operation is already in `backwardSlice`, no
117-
/// need to traverse its definitions again. Since useuse-def chains form a DAG,
118-
/// this terminates.
118+
/// need to traverse its definitions again. In the presence of use-def cycles
119+
/// in a graph region, the traversal stops at the first operation that was
120+
/// already visited (which is not added to the slice anymore).
119121
///
120122
/// Upon return to the root call, `backwardSlice` is filled with a
121123
/// postorder list of defs. This happens to be a topological order, from the

mlir/lib/Analysis/SliceAnalysis.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
using namespace mlir;
2727

2828
static void
29-
getForwardSliceImpl(Operation *op, SetVector<Operation *> *forwardSlice,
29+
getForwardSliceImpl(Operation *op, DenseSet<Operation *> &visited,
30+
SetVector<Operation *> *forwardSlice,
3031
const SliceOptions::TransitiveFilter &filter = nullptr) {
3132
if (!op)
3233
return;
@@ -41,19 +42,23 @@ getForwardSliceImpl(Operation *op, SetVector<Operation *> *forwardSlice,
4142
for (Block &block : region)
4243
for (Operation &blockOp : block)
4344
if (forwardSlice->count(&blockOp) == 0)
44-
getForwardSliceImpl(&blockOp, forwardSlice, filter);
45-
for (Value result : op->getResults()) {
46-
for (Operation *userOp : result.getUsers())
47-
if (forwardSlice->count(userOp) == 0)
48-
getForwardSliceImpl(userOp, forwardSlice, filter);
49-
}
45+
getForwardSliceImpl(&blockOp, visited, forwardSlice, filter);
46+
47+
for (Value result : op->getResults())
48+
for (Operation *userOp : result.getUsers()) {
49+
if (forwardSlice->count(userOp) == 0 && visited.insert(userOp).second)
50+
getForwardSliceImpl(userOp, visited, forwardSlice, filter);
51+
52+
visited.erase(userOp);
53+
}
5054

5155
forwardSlice->insert(op);
5256
}
5357

5458
void mlir::getForwardSlice(Operation *op, SetVector<Operation *> *forwardSlice,
5559
const ForwardSliceOptions &options) {
56-
getForwardSliceImpl(op, forwardSlice, options.filter);
60+
DenseSet<Operation *> visited;
61+
getForwardSliceImpl(op, visited, forwardSlice, options.filter);
5762
if (!options.inclusive) {
5863
// Don't insert the top level operation, we just queried on it and don't
5964
// want it in the results.
@@ -69,8 +74,9 @@ void mlir::getForwardSlice(Operation *op, SetVector<Operation *> *forwardSlice,
6974

7075
void mlir::getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
7176
const SliceOptions &options) {
77+
DenseSet<Operation *> visited;
7278
for (Operation *user : root.getUsers())
73-
getForwardSliceImpl(user, forwardSlice, options.filter);
79+
getForwardSliceImpl(user, visited, forwardSlice, options.filter);
7480

7581
// Reverse to get back the actual topological order.
7682
// std::reverse does not work out of the box on SetVector and I want an
@@ -80,6 +86,7 @@ void mlir::getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
8086
}
8187

8288
static LogicalResult getBackwardSliceImpl(Operation *op,
89+
DenseSet<Operation *> &visited,
8390
SetVector<Operation *> *backwardSlice,
8491
const BackwardSliceOptions &options) {
8592
if (!op || op->hasTrait<OpTrait::IsIsolatedFromAbove>())
@@ -93,8 +100,12 @@ static LogicalResult getBackwardSliceImpl(Operation *op,
93100

94101
auto processValue = [&](Value value) {
95102
if (auto *definingOp = value.getDefiningOp()) {
96-
if (backwardSlice->count(definingOp) == 0)
97-
return getBackwardSliceImpl(definingOp, backwardSlice, options);
103+
if (backwardSlice->count(definingOp) == 0 &&
104+
visited.insert(definingOp).second)
105+
return getBackwardSliceImpl(definingOp, visited, backwardSlice,
106+
options);
107+
108+
visited.erase(definingOp);
98109
} else if (auto blockArg = dyn_cast<BlockArgument>(value)) {
99110
if (options.omitBlockArguments)
100111
return success();
@@ -107,7 +118,8 @@ static LogicalResult getBackwardSliceImpl(Operation *op,
107118
if (parentOp && backwardSlice->count(parentOp) == 0) {
108119
if (parentOp->getNumRegions() == 1 &&
109120
llvm::hasSingleElement(parentOp->getRegion(0).getBlocks())) {
110-
return getBackwardSliceImpl(parentOp, backwardSlice, options);
121+
return getBackwardSliceImpl(parentOp, visited, backwardSlice,
122+
options);
111123
}
112124
}
113125
} else {
@@ -145,7 +157,9 @@ static LogicalResult getBackwardSliceImpl(Operation *op,
145157
LogicalResult mlir::getBackwardSlice(Operation *op,
146158
SetVector<Operation *> *backwardSlice,
147159
const BackwardSliceOptions &options) {
148-
LogicalResult result = getBackwardSliceImpl(op, backwardSlice, options);
160+
DenseSet<Operation *> visited;
161+
LogicalResult result =
162+
getBackwardSliceImpl(op, visited, backwardSlice, options);
149163

150164
if (!options.inclusive) {
151165
// Don't insert the top level operation, we just queried on it and don't

mlir/test/Dialect/Affine/slicing-utils.mlir

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,26 @@ func.func @slicing_test_multiple_return(%arg0: index) -> (index, index) {
292292
%0:2 = "slicing-test-op"(%arg0, %arg0): (index, index) -> (index, index)
293293
return %0#0, %0#1 : index, index
294294
}
295+
296+
// -----
297+
298+
// FWD-LABEL: graph_region_with_cycle
299+
// BWD-LABEL: graph_region_with_cycle
300+
// FWDBWD-LABEL: graph_region_with_cycle
301+
func.func @graph_region_with_cycle() {
302+
test.isolated_graph_region {
303+
// FWD: matched: [[V0:%.+]] = "slicing-test-op"([[V1:%.+]]) : (i1) -> i1 forward static slice:
304+
// FWD: [[V1]] = "slicing-test-op"([[V0]]) : (i1) -> i1
305+
// FWD: matched: [[V1]] = "slicing-test-op"([[V0]]) : (i1) -> i1 forward static slice:
306+
// FWD: [[V0]] = "slicing-test-op"([[V1]]) : (i1) -> i1
307+
308+
// BWD: matched: [[V0:%.+]] = "slicing-test-op"([[V1:%.+]]) : (i1) -> i1 backward static slice:
309+
// BWD: [[V1]] = "slicing-test-op"([[V0]]) : (i1) -> i1
310+
// BWD: matched: [[V1]] = "slicing-test-op"([[V0]]) : (i1) -> i1 backward static slice:
311+
// BWD: [[V0]] = "slicing-test-op"([[V1]]) : (i1) -> i1
312+
%0 = "slicing-test-op"(%1) : (i1) -> i1
313+
%1 = "slicing-test-op"(%0) : (i1) -> i1
314+
}
315+
316+
return
317+
}

0 commit comments

Comments
 (0)