Skip to content

Commit b741aa0

Browse files
committed
SILOptimizer: fix a stack overflow in DCE
For very large control flow graphs the markControllingTerminatorsLive can stack overflow. Fix this by doing the work iteratively instead of recursively. rdar://106198943
1 parent fdc9817 commit b741aa0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ void DCE::markInstructionLive(SILInstruction *Inst) {
215215

216216
LLVM_DEBUG(llvm::dbgs() << "Marking as live: " << *Inst);
217217

218-
markControllingTerminatorsLive(Inst->getParent());
219218
Worklist.push_back(Inst);
220219
}
221220

@@ -450,6 +449,8 @@ void DCE::propagateLiveBlockArgument(SILArgument *Arg) {
450449
// Given an instruction which is considered live, propagate that liveness
451450
// back to the instructions that produce values it consumes.
452451
void DCE::propagateLiveness(SILInstruction *I) {
452+
markControllingTerminatorsLive(I->getParent());
453+
453454
if (!isa<TermInst>(I)) {
454455
for (auto &O : I->getAllOperands())
455456
markValueLive(O.get());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %gyb %s > %t/main.swift
3+
// RUN: %target-swift-frontend -O -parse-as-library -sil-verify-none -emit-sil %t/main.swift -o /dev/null
4+
5+
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
6+
// REQUIRES: long_test
7+
8+
// Check that the compiler does not crash in DCE due to a stack overflow.
9+
// rdar://106198943
10+
11+
public func test(_ a: Int) -> Int {
12+
switch (a) {
13+
% for i in range(32000):
14+
case ${i}: return ${i % 7}
15+
% end
16+
default: return -1
17+
}
18+
}
19+

0 commit comments

Comments
 (0)