Skip to content

[DCE] Tweaked code to end borrows before destroys. #39760

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
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
8 changes: 7 additions & 1 deletion lib/SILOptimizer/Transforms/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,19 @@ bool DCE::removeDead() {
// may also have been dead and a destroy_value of its baseValue may
// have been inserted before the pred's terminator. Make sure to
// adjust the insertPt before any destroy_value.
//
// FIXME: This code currently can reorder destroys, e.g., when the
// block already contains a destroy_value just before the
// terminator. Fix this by making note of the added
// destroy_value insts and only moving the insertion point
// before those that are newly added.
for (SILInstruction &predInst : llvm::reverse(*pred)) {
if (&predInst == predTerm)
continue;
if (!isa<DestroyValueInst>(&predInst)) {
insertPt = &*std::next(predInst.getIterator());
break;
}
insertPt = &predInst;
}
}

Expand Down
41 changes: 41 additions & 0 deletions test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,44 @@ exit:
%retval = tuple ()
return %retval : $()
}

// CHECK-LABEL: sil hidden [ossa] @add_end_borrow_after_destroy_value : {{.*}} {
// CHECK: bb0({{%[^,]+}} : $Builtin.Int1, [[INSTANCE_1:%[^,]+]] : @owned $Klass, {{%[^,]+}} : @owned $Klass):
// CHECK: [[LIFETIME_1:%[^,]+]] = begin_borrow [[INSTANCE_1]]
// CHECK: copy_value [[LIFETIME_1]]
// CHECK: cond_br {{%[^,]+}}, [[BASIC_BLOCK1:bb[0-9]+]], [[BASIC_BLOCK2:bb[0-9]+]]
// CHECK: [[BASIC_BLOCK1]]:
// CHECK: end_borrow [[LIFETIME_1]]
// CHECK: destroy_value [[INSTANCE_1]]
// CHECK: [[BASIC_BLOCK2]]:
// CHECK: end_borrow [[LIFETIME_1]]
// CHECK: destroy_value [[INSTANCE_1]]
// CHECK-LABEL: } // end sil function 'add_end_borrow_after_destroy_value'
sil hidden [ossa] @add_end_borrow_after_destroy_value : $@convention(thin) (Builtin.Int1, @owned Klass, @owned Klass) -> () {
bb0(%condition : $Builtin.Int1, %instance_1 : @owned $Klass, %instance_2 : @owned $Klass):
%lifetime_1 = begin_borrow %instance_1 : $Klass

%copy_1 = copy_value %lifetime_1 : $Klass
%stack_addr = alloc_stack $Klass
store %copy_1 to [init] %stack_addr : $*Klass
destroy_addr %stack_addr : $*Klass
dealloc_stack %stack_addr : $*Klass

cond_br %condition, bb1, bb2

bb1:
end_borrow %lifetime_1 : $Klass
destroy_value %instance_1 : $Klass
%lifetime_2 = begin_borrow %instance_2 : $Klass
br bb3(%instance_2 : $Klass, %lifetime_2 : $Klass)

bb2:
destroy_value %instance_2 : $Klass
br bb3(%instance_1 : $Klass, %lifetime_1 : $Klass)

bb3(%original : @owned $Klass, %lifetime : @guaranteed $Klass):
end_borrow %lifetime : $Klass
destroy_value %original : $Klass
%result = tuple ()
return %result : $()
}