Skip to content

Commit 08ad17a

Browse files
committed
[mlir][SliceAnalysis] Fix stack overflow in graph regions
1 parent c6c2e21 commit 08ad17a

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
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: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
using namespace mlir;
2828

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

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

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

7176
void mlir::getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
7277
const SliceOptions &options) {
78+
DenseSet<Operation *> visited;
7379
for (Operation *user : root.getUsers())
74-
getForwardSliceImpl(user, forwardSlice, options.filter);
80+
getForwardSliceImpl(user, visited, forwardSlice, options.filter);
7581

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

83-
static void getBackwardSliceImpl(Operation *op,
89+
static void getBackwardSliceImpl(Operation *op, DenseSet<Operation *> &visited,
8490
SetVector<Operation *> *backwardSlice,
8591
const BackwardSliceOptions &options) {
8692
if (!op || op->hasTrait<OpTrait::IsIsolatedFromAbove>())
@@ -94,8 +100,11 @@ static void getBackwardSliceImpl(Operation *op,
94100

95101
auto processValue = [&](Value value) {
96102
if (auto *definingOp = value.getDefiningOp()) {
97-
if (backwardSlice->count(definingOp) == 0)
98-
getBackwardSliceImpl(definingOp, backwardSlice, options);
103+
if (backwardSlice->count(definingOp) == 0 &&
104+
visited.insert(definingOp).second)
105+
getBackwardSliceImpl(definingOp, visited, backwardSlice, options);
106+
107+
visited.erase(definingOp);
99108
} else if (auto blockArg = dyn_cast<BlockArgument>(value)) {
100109
if (options.omitBlockArguments)
101110
return;
@@ -108,7 +117,7 @@ static void getBackwardSliceImpl(Operation *op,
108117
if (parentOp && backwardSlice->count(parentOp) == 0) {
109118
assert(parentOp->getNumRegions() == 1 &&
110119
llvm::hasSingleElement(parentOp->getRegion(0).getBlocks()));
111-
getBackwardSliceImpl(parentOp, backwardSlice, options);
120+
getBackwardSliceImpl(parentOp, visited, backwardSlice, options);
112121
}
113122
} else {
114123
llvm_unreachable("No definingOp and not a block argument.");
@@ -138,7 +147,8 @@ static void getBackwardSliceImpl(Operation *op,
138147
void mlir::getBackwardSlice(Operation *op,
139148
SetVector<Operation *> *backwardSlice,
140149
const BackwardSliceOptions &options) {
141-
getBackwardSliceImpl(op, backwardSlice, options);
150+
DenseSet<Operation *> visited;
151+
getBackwardSliceImpl(op, visited, backwardSlice, options);
142152

143153
if (!options.inclusive) {
144154
// 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)