Skip to content

[CapturePromotion] dealloc_box doesn't mutate. #77401

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 2 commits into from
Nov 6, 2024
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
9 changes: 2 additions & 7 deletions lib/SILOptimizer/Mandatory/CapturePromotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,15 +1071,10 @@ class NonEscapingUserVisitor
ALWAYS_NON_ESCAPING_INST(StrongRelease)
ALWAYS_NON_ESCAPING_INST(DestroyValue)
ALWAYS_NON_ESCAPING_INST(EndBorrow)
ALWAYS_NON_ESCAPING_INST(DeallocBox)
ALWAYS_NON_ESCAPING_INST(EndAccess)
Copy link
Contributor

@meg-gupta meg-gupta Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR - I noticed copy_value / begin_borrow is not handled here. Maybe it is okay since findEscapeOrMutationUses is looking for it.

#undef ALWAYS_NON_ESCAPING_INST

bool visitDeallocBoxInst(DeallocBoxInst *dbi) {
markCurrentOpAsMutation();
return true;
}

bool visitEndAccessInst(EndAccessInst *) { return true; }

bool visitApplyInst(ApplyInst *ai) {
auto argIndex = currentOp.get()->getOperandNumber() - 1;
SILFunctionConventions substConv(ai->getSubstCalleeType(), ai->getModule());
Expand Down
36 changes: 36 additions & 0 deletions test/SILOptimizer/capture_promotion_ownership.sil
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,39 @@ bb0:
%28 = tuple ()
return %28 : $()
}

sil [ossa] @closable : $@convention(thin) (@guaranteed { var Bar }) -> Builtin.Int64 {
entry(%box : @guaranteed ${ var Bar }):
%retval = integer_literal $Builtin.Int64, 0
return %retval : $Builtin.Int64
}

// Test that a dealloc_box is not regarded as a mutation which would obstruct promotion.
// CHECK-LABEL: sil [ossa] @with_dealloc_box : {{.*}} {
// CHECK: load [copy]
// CHECK-LABEL: } // end sil function 'with_dealloc_box'
sil [ossa] @with_dealloc_box : $@convention(thin) (@owned Bar) -> () {
entry(%idle_incoming : @owned $Bar):
%box = alloc_box ${ var Bar }
%box_borrow = begin_borrow [var_decl] %box : ${ var Bar }
%box_addr = project_box %box_borrow : ${ var Bar }, 0
store %idle_incoming to [init] %box_addr : $*Bar
%closable = function_ref @closable : $@convention(thin) (@guaranteed { var Bar }) -> Builtin.Int64
%box_copy = copy_value %box_borrow : ${ var Bar }
mark_function_escape %box_addr : $*Bar
%closure = partial_apply [callee_guaranteed] %closable(%box_copy) : $@convention(thin) (@guaranteed { var Bar }) -> Builtin.Int64
cond_br undef, exit, die

die:
destroy_value %closure : $@callee_guaranteed () -> Builtin.Int64
end_borrow %box_borrow : ${ var Bar }
dealloc_box %box : ${ var Bar }
unreachable

exit:
destroy_value %closure : $@callee_guaranteed () -> Builtin.Int64
end_borrow %box_borrow : ${ var Bar }
destroy_value %box : ${ var Bar }
%retval = tuple ()
return %retval :$()
}