Skip to content

Fix copy-to-borrow optimization's end_borrow insertion #79810

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
Mar 7, 2025
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 @@ -124,10 +124,6 @@ private struct Uses {
// E.g. the none-case of a switch_enum of an Optional.
private(set) var nonDestroyingLiverangeExits: Stack<Instruction>

var allLifetimeEndingInstructions: [Instruction] {
Array(destroys.lazy.map { $0 }) + Array(nonDestroyingLiverangeExits)
}

private(set) var usersInDeadEndBlocks: Stack<Instruction>

init(_ context: FunctionPassContext) {
Expand Down Expand Up @@ -323,7 +319,16 @@ private func createEndBorrows(for beginBorrow: Value, atEndOf liverange: Instruc
// destroy_value %2
// destroy_value %3 // The final destroy. Here we need to create the `end_borrow`(s)
//
for endInst in collectedUses.allLifetimeEndingInstructions {

var allLifetimeEndingInstructions = InstructionWorklist(context)
allLifetimeEndingInstructions.pushIfNotVisited(contentsOf: collectedUses.destroys.lazy.map { $0 })
allLifetimeEndingInstructions.pushIfNotVisited(contentsOf: collectedUses.nonDestroyingLiverangeExits)

defer {
allLifetimeEndingInstructions.deinitialize()
}

while let endInst = allLifetimeEndingInstructions.pop() {
if !liverange.contains(endInst) {
let builder = Builder(before: endInst, context)
builder.createEndBorrow(of: beginBorrow)
Expand Down
46 changes: 46 additions & 0 deletions test/SILOptimizer/copy-to-borrow-optimization.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2156,3 +2156,49 @@ bb0(%0 : $*MultiPayload):
return %2
}

// CHECK-LABEL: sil [ossa] @duplicate_lifetime_end1 :
// CHECK: load_borrow %0
// CHECK: } // end sil function 'duplicate_lifetime_end1'
sil [ossa] @duplicate_lifetime_end1 : $@convention(thin) (@in_guaranteed (C, Optional<C>)) -> () {
bb0(%0 : $*(C, Optional<C>)):
%1 = load [copy] %0
(%2, %3) = destructure_tuple %1
switch_enum %3, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1(%4 : @owned $C):
destroy_value %2
destroy_value %4
br bb3

bb2:
debug_value %2
destroy_value %2
br bb3

bb3:
%r = tuple ()
return %r
}

// CHECK-LABEL: sil [ossa] @duplicate_lifetime_end2 :
// CHECK: load_borrow %0
// CHECK: } // end sil function 'duplicate_lifetime_end2'
sil [ossa] @duplicate_lifetime_end2 : $@convention(thin) (@in_guaranteed (C, Optional<C>)) -> () {
bb0(%0 : $*(C, Optional<C>)):
%1 = load [copy] %0
(%2, %3) = destructure_tuple %1
switch_enum %3, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1(%4 : @owned $C):
destroy_value %2
destroy_value %4
br bb3

bb2:
destroy_value %2
br bb3

bb3:
%r = tuple ()
return %r
}