Skip to content

Commit b12e0ef

Browse files
committed
Don't hoist scoped instructions in dead end loops.
1 parent 38f28c1 commit b12e0ef

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LoopInvariantCodeMotion.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,10 @@ private extension MovableInstructions {
643643

644644
/// Hoist and sink scoped instructions.
645645
mutating func hoistWithSinkScopedInstructions(outOf loop: Loop, _ context: FunctionPassContext) -> Bool {
646-
guard !loop.hasNoExitBlocks else {
646+
// Since we don't sink scoped instructions to dead exit blocks, we need to check there's
647+
// at least one exit block to which we can sink end instructions. Otherwise we could end up
648+
// with partially hoisted scoped instruction that could lead to e.g. value overconsumption.
649+
guard !loop.hasNoExitBlocks, loop.exitBlocks.contains(where: { !context.deadEndBlocks.isDeadEnd($0) }) else {
647650
return false
648651
}
649652

0 commit comments

Comments
 (0)