Skip to content

SimplifyBranch: don't leave an empty block after merging two basic blocks #68429

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
Sep 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ private extension BranchInst {
}
parentBB.moveAllInstructions(toBeginOf: targetBB, context)
parentBB.moveAllArguments(to: targetBB, context)
context.erase(block: parentBB)
} else {
targetBB.moveAllInstructions(toEndOf: parentBB, context)
context.erase(block: targetBB)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ extension MutatingContext {
erase(instruction: inst)
}

func erase(block: BasicBlock) {
_bridged.eraseBlock(block.bridged)
}

func tryOptimizeApplyOfPartialApply(closure: PartialApplyInst) -> Bool {
if _bridged.tryOptimizeApplyOfPartialApply(closure.bridged) {
notifyInstructionsChanged()
Expand Down Expand Up @@ -227,10 +231,6 @@ struct FunctionPassContext : MutatingContext {
}
}

func erase(block: BasicBlock) {
_bridged.eraseBlock(block.bridged)
}

func modifyEffects(in function: Function, _ body: (inout FunctionEffects) -> ()) {
notifyEffectsChanged()
function._modifyEffects(body)
Expand Down
28 changes: 28 additions & 0 deletions test/SILOptimizer/simplify_branch_crash.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -sil-opt-pass-count=1.1 -simplification -simplify-instruction=br | %FileCheck %s

// REQUIRES: swift_in_compiler

import Swift
import Builtin

// Check that branch simplification doesn't leave empty blocks.
// -sil-opt-pass-count=1.1 prevents dead block elimination which would hide the problem.



// CHECK-LABEL: sil @dont_leave_empty_blocks
// CHECK: bb0(%0 : $Builtin.Int64):
// CHECK-NEXT: br bb1
// CHECK: bb1:
// CHECK-NEXT: return %0
// CHECK: } // end sil function 'dont_leave_empty_blocks'
sil @dont_leave_empty_blocks : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
bb0(%0 : $Builtin.Int64):
br bb1

bb1:
br bb2

bb2:
return %0 : $Builtin.Int64
}