Skip to content

Fix LICM to avoid hoisting never-executed traps #33431

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
Aug 13, 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
10 changes: 10 additions & 0 deletions include/swift/SIL/LoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class SILLoop : public llvm::LoopBase<SILBasicBlock, SILLoop> {
/// this loop by unrolling or versioning.
bool canDuplicate(SILInstruction *Inst) const;

void getExitingAndLatchBlocks(
SmallVectorImpl<SILBasicBlock *> &ExitingAndLatchBlocks) const {
this->getExitingBlocks(ExitingAndLatchBlocks);
SILBasicBlock *header = getHeader();
for (auto *predBB : header->getPredecessorBlocks()) {
if (contains(predBB) && !this->isLoopExiting(predBB))
ExitingAndLatchBlocks.push_back(predBB);
}
}

private:
friend class llvm::LoopInfoBase<SILBasicBlock, SILLoop>;

Expand Down
6 changes: 3 additions & 3 deletions lib/SILOptimizer/LoopTransforms/LICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,16 @@ static void getDominatingBlocks(SmallVectorImpl<SILBasicBlock *> &domBlocks,
SILLoop *Loop, DominanceInfo *DT) {
auto HeaderBB = Loop->getHeader();
auto DTRoot = DT->getNode(HeaderBB);
SmallVector<SILBasicBlock *, 8> ExitingBBs;
Loop->getExitingBlocks(ExitingBBs);
SmallVector<SILBasicBlock *, 8> ExitingAndLatchBBs;
Loop->getExitingAndLatchBlocks(ExitingAndLatchBBs);
for (llvm::df_iterator<DominanceInfoNode *> It = llvm::df_begin(DTRoot),
E = llvm::df_end(DTRoot);
It != E;) {
auto *CurBB = It->getBlock();

// Don't decent into control-dependent code. Only traverse into basic blocks
// that dominate all exits.
if (!std::all_of(ExitingBBs.begin(), ExitingBBs.end(),
if (!std::all_of(ExitingAndLatchBBs.begin(), ExitingAndLatchBBs.end(),
[=](SILBasicBlock *ExitBB) {
return DT->dominates(CurBB, ExitBB);
})) {
Expand Down
79 changes: 79 additions & 0 deletions test/SILOptimizer/licm.sil
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,82 @@ bb9(%39 : $Builtin.Int64): // Preds: bb7
dealloc_stack %3 : $*Index // id: %41
return %40 : $Int64 // id: %42
}

// testConditionalTrapInInfiniteSyncLoop and
// testConditionalTrapDominatingSyncLoopExit
//
// It's legal for the optimizer to consider code after the loop as
// always reachable, but when a loop has no exits, or when the loops
// exits are dominated by a conditional statement we should not
// consider conditional statements within the loop as dominating all
// possible execution paths through the loop. At least not when there
// is at least one path through the loop that contains a
// "synchronization point", such as a function that may contain a
// memory barrier, perform I/O, or exit the program.
sil @mayExit : $@convention(thin) () -> ()

// CHECK-LABEL: sil @testConditionalTrapInInfiniteSyncLoop : $@convention(thin) (Builtin.Int1, Builtin.Int1) -> () {
// CHECK: bb0
// CHECK-NOT: cond_fail
// CHECK: br bb1
// CHECK: bb1:
// CHECK: cond_br %0, bb2, bb3
// CHECK: bb2:
// CHECK: cond_fail %1 : $Builtin.Int1, "arithmetic overflow"
// CHECK-LABEL: } // end sil function 'testConditionalTrapInInfiniteSyncLoop'
sil @testConditionalTrapInInfiniteSyncLoop : $@convention(thin) (Builtin.Int1, Builtin.Int1) -> () {
bb0(%0 : $Builtin.Int1, %1 : $Builtin.Int1):
br bb1

bb1: // loop head
cond_br %0, bb2, bb3

bb2: // maybe never executed
cond_fail %1 : $Builtin.Int1, "arithmetic overflow"
br bb4

bb3:
// synchronization point: has "real" side-effects that we can't
// reorder with traps
%f = function_ref @mayExit : $@convention(thin) () -> ()
apply %f() : $@convention(thin) () -> ()
br bb4

bb4: // latch
br bb1
}

// CHECK-LABEL: sil @testConditionalTrapDominatingSyncLoopExit : $@convention(thin) (Builtin.Int1, Builtin.Int1, Builtin.Int1) -> () {
// CHECK: bb0
// CHECK-NOT: cond_fail
// CHECK: br bb1
// CHECK: bb1:
// CHECK: cond_br %0, bb2, bb4
// CHECK: bb2:
// CHECK: cond_fail %1 : $Builtin.Int1, "arithmetic overflow"
// CHECK-LABEL: } // end sil function 'testConditionalTrapDominatingSyncLoopExit'
sil @testConditionalTrapDominatingSyncLoopExit : $@convention(thin) (Builtin.Int1, Builtin.Int1, Builtin.Int1) -> () {
bb0(%0 : $Builtin.Int1, %1 : $Builtin.Int1, %2 : $Builtin.Int1):
br bb1

bb1: // loop head
cond_br %0, bb2, bb4

bb2: // maybe never executed
cond_fail %1 : $Builtin.Int1, "arithmetic overflow"
cond_br %2, bb3, bb5

bb3: // tail
br bb1

bb4:
// synchronization point: has "real" side-effects that we can't
// reorder with traps
%f = function_ref @mayExit : $@convention(thin) () -> ()
apply %f() : $@convention(thin) () -> ()
br bb1

bb5:
%99 = tuple ()
return %99 : $()
}