Skip to content

DestroyHoisting: handle infinite loops correctly #28381

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
Nov 20, 2019
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
30 changes: 26 additions & 4 deletions include/swift/SIL/MemoryLifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ class MemoryLocations {
/// for memory locations. Consider renaming it.
class MemoryDataflow {

/// What kind of terminators can be reached from a block.
enum class ExitReachability : uint8_t {
/// Worst case: the block is part of a cycle which neither reaches a
/// function-exit nor an unreachable-instruction.
InInfiniteLoop,

/// An unreachable-instruction can be reached from the block, but not a
/// function-exit (like "return" or "throw").
ReachesUnreachable,

/// A function-exit can be reached from the block.
/// This is the case for most basic blocks.
ReachesExit
};

public:
using Bits = MemoryLocations::Bits;

Expand All @@ -313,11 +328,10 @@ class MemoryDataflow {
/// This flag is only computed if entryReachabilityAnalysis is called.
bool reachableFromEntry = false;

/// True, if any function-exit block can be reached from this block, i.e. is
/// not a block which eventually ends in an unreachable instruction.
/// What kind of terminators can be reached from this block.
///
/// This flag is only computed if exitReachableAnalysis is called.
bool exitReachable = false;
/// This is only computed if exitReachableAnalysis is called.
ExitReachability exitReachability = ExitReachability::InInfiniteLoop;

BlockState(SILBasicBlock *block = nullptr) : block(block) { }

Expand All @@ -336,6 +350,14 @@ class MemoryDataflow {
killSet |= loc->subLocations;
}
}

bool exitReachable() const {
return exitReachability == ExitReachability::ReachesExit;
}

bool isInInfiniteLoop() const {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for cosmetically, as for the function names, maybe reachesExit() and reachesInfinityLoop() are better

return exitReachability == ExitReachability::InInfiniteLoop;
}
};

private:
Expand Down
13 changes: 9 additions & 4 deletions lib/SIL/MemoryLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,21 @@ void MemoryDataflow::exitReachableAnalysis() {
llvm::SmallVector<BlockState *, 16> workList;
for (BlockState &state : blockStates) {
if (state.block->getTerminator()->isFunctionExiting()) {
state.exitReachable = true;
state.exitReachability = ExitReachability::ReachesExit;
workList.push_back(&state);
} else if (isa<UnreachableInst>(state.block->getTerminator())) {
state.exitReachability = ExitReachability::ReachesUnreachable;
workList.push_back(&state);
}
}
while (!workList.empty()) {
BlockState *state = workList.pop_back_val();
for (SILBasicBlock *pred : state->block->getPredecessorBlocks()) {
BlockState *predState = block2State[pred];
if (!predState->exitReachable) {
predState->exitReachable = true;
if (predState->exitReachability < state->exitReachability) {
// As there are 3 states, each block can be put into the workList 2
// times maximum.
predState->exitReachability = state->exitReachability;
workList.push_back(predState);
}
}
Expand Down Expand Up @@ -806,7 +811,7 @@ void MemoryLifetimeVerifier::checkFunction(MemoryDataflow &dataFlow) {
const Bits &nonTrivialLocations = locations.getNonTrivialLocations();
Bits bits(locations.getNumLocations());
for (BlockState &st : dataFlow) {
if (!st.reachableFromEntry || !st.exitReachable)
if (!st.reachableFromEntry || !st.exitReachable())
continue;

// Check all instructions in the block.
Expand Down
15 changes: 13 additions & 2 deletions lib/SILOptimizer/Transforms/DestroyHoisting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,16 @@ void DestroyHoisting::expandStores(MemoryDataflow &dataFlow) {
// Initialize the dataflow for moving destroys up the control flow.
void DestroyHoisting::initDataflow(MemoryDataflow &dataFlow) {
for (BlockState &st : dataFlow) {
st.entrySet.set();
st.genSet.reset();
st.killSet.reset();
if (st.isInInfiniteLoop()) {
// Ignore blocks which are in an infinite loop and prevent any destroy
// hoisting across such block borders.
st.entrySet.reset();
st.exitSet.reset();
continue;
}
st.entrySet.set();
if (isa<UnreachableInst>(st.block->getTerminator())) {
if (canIgnoreUnreachableBlock(st.block, dataFlow)) {
st.exitSet.set();
Expand Down Expand Up @@ -284,7 +291,7 @@ bool DestroyHoisting::canIgnoreUnreachableBlock(SILBasicBlock *block,
SILBasicBlock *singlePred = block->getSinglePredecessorBlock();
if (!singlePred)
return false;
if (!dataFlow.getState(singlePred)->exitReachable)
if (!dataFlow.getState(singlePred)->exitReachable())
return false;

// Check if none of the locations are touched in the unreachable-block.
Expand Down Expand Up @@ -374,6 +381,10 @@ void DestroyHoisting::moveDestroys(MemoryDataflow &dataFlow) {
if (isa<UnreachableInst>(block->getTerminator()) && state.exitSet.any())
continue;

// Ignore blocks which are in an infinite loop.
if (state.isInInfiniteLoop())
continue;

// Do the inner-block processing.
activeDestroys = state.exitSet;
moveDestroysInBlock(block, activeDestroys, toRemove);
Expand Down
30 changes: 30 additions & 0 deletions test/SILOptimizer/destroy_hoisting.sil
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,33 @@ bb1:
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: sil [ossa] @test_simple_infinite_loop
// CHECK-NOT: destroy_addr
// CHECK: } // end sil function 'test_simple_infinite_loop'
sil [ossa] @test_simple_infinite_loop : $@convention(thin) (@in_guaranteed S) -> () {
bb0(%0 : $*S):
br bb1
bb1:
br bb1
}

// CHECK-LABEL: sil [ossa] @test_infinite_loop
// CHECK-NOT: destroy_addr
// CHECK: bb3:
// CHECK-NEXT: destroy_addr %1
// CHECK-NOT: destroy_addr
// CHECK: } // end sil function 'test_infinite_loop'
sil [ossa] @test_infinite_loop : $@convention(thin) (@in_guaranteed S, @in S) -> () {
bb0(%0 : $*S, %1 : $*S):
cond_br undef, bb1, bb2
bb1:
br bb1
bb2:
br bb3
bb3:
destroy_addr %1 : $*S
%r = tuple ()
return %r : $()
}