Skip to content

SimplifyCFG: fix a crash caused by unreachable CFG cycles with block arguments. #34320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions lib/SILOptimizer/Transforms/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,21 +1248,8 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
LLVM_DEBUG(llvm::dbgs() << "merge bb" << BB->getDebugID() << " with bb"
<< DestBB->getDebugID() << '\n');

// If there are any BB arguments in the destination, replace them with the
// branch operands, since they must dominate the dest block.
for (unsigned i = 0, e = BI->getArgs().size(); i != e; ++i) {
if (DestBB->getArgument(i) != BI->getArg(i)) {
SILValue Val = BI->getArg(i);
DestBB->getArgument(i)->replaceAllUsesWith(Val);
if (!isVeryLargeFunction) {
if (auto *I = dyn_cast<SingleValueInstruction>(Val)) {
// Replacing operands may trigger constant folding which then could
// trigger other simplify-CFG optimizations.
ConstFolder.addToWorklist(I);
ConstFolder.processWorkList();
}
}
} else {
if (DestBB->getArgument(i) == BI->getArg(i)) {
// We must be processing an unreachable part of the cfg with a cycle.
// bb1(arg1): // preds: bb3
// br bb2
Expand All @@ -1273,6 +1260,23 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
// bb3: // preds: bb2
// br bb1(arg1)
assert(!isReachable(BB) && "Should only occur in unreachable block");
return Simplified;
}
}

// If there are any BB arguments in the destination, replace them with the
// branch operands, since they must dominate the dest block.
for (unsigned i = 0, e = BI->getArgs().size(); i != e; ++i) {
assert(DestBB->getArgument(i) != BI->getArg(i));
SILValue Val = BI->getArg(i);
DestBB->getArgument(i)->replaceAllUsesWith(Val);
if (!isVeryLargeFunction) {
if (auto *I = dyn_cast<SingleValueInstruction>(Val)) {
// Replacing operands may trigger constant folding which then could
// trigger other simplify-CFG optimizations.
ConstFolder.addToWorklist(I);
ConstFolder.processWorkList();
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ bool ReachableBlocks::visit(SILFunction *f,
/// Remove all instructions in the body of \p bb in safe manner by using
/// undef.
void swift::clearBlockBody(SILBasicBlock *bb) {

for (SILArgument *arg : bb->getArguments()) {
arg->replaceAllUsesWithUndef();
}

// Instructions in the dead block may be used by other dead blocks. Replace
// any uses of them with undef values.
while (!bb->empty()) {
Expand Down
23 changes: 23 additions & 0 deletions test/SILOptimizer/simplify-cfg-crash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s

// Check that the optimizer does not crash.


public class Base {
@inline(never)
final func next() -> Base? {
return self
}
}

public class Derived : Base {}

// CHECK: sil {{.*}}testit
public func testit(_ x: Base?) -> Derived? {
var i: Base? = x
while (i is Derived) == false && i != nil {
i = i?.next()
}
return i as? Derived
}