Skip to content

Commit 986d155

Browse files
committed
[CapturePromotion] dealloc_box doesn't mutate.
The instruction only deallocates the box, it doesn't destroy its contents. It's even less mutating than a `destroy_value`, which is already regarded as non-mutating.
1 parent b4b99e9 commit 986d155

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/SILOptimizer/Mandatory/CapturePromotion.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,13 +1071,9 @@ class NonEscapingUserVisitor
10711071
ALWAYS_NON_ESCAPING_INST(StrongRelease)
10721072
ALWAYS_NON_ESCAPING_INST(DestroyValue)
10731073
ALWAYS_NON_ESCAPING_INST(EndBorrow)
1074+
ALWAYS_NON_ESCAPING_INST(DeallocBox)
10741075
#undef ALWAYS_NON_ESCAPING_INST
10751076

1076-
bool visitDeallocBoxInst(DeallocBoxInst *dbi) {
1077-
markCurrentOpAsMutation();
1078-
return true;
1079-
}
1080-
10811077
bool visitEndAccessInst(EndAccessInst *) { return true; }
10821078

10831079
bool visitApplyInst(ApplyInst *ai) {

test/SILOptimizer/capture_promotion_ownership.sil

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,39 @@ bb0:
553553
%28 = tuple ()
554554
return %28 : $()
555555
}
556+
557+
sil [ossa] @closable : $@convention(thin) (@guaranteed { var Bar }) -> Builtin.Int64 {
558+
entry(%box : @guaranteed ${ var Bar }):
559+
%retval = integer_literal $Builtin.Int64, 0
560+
return %retval : $Builtin.Int64
561+
}
562+
563+
// Test that a dealloc_box is not regarded as a mutation which would obstruct promotion.
564+
// CHECK-LABEL: sil [ossa] @with_dealloc_box : {{.*}} {
565+
// CHECK: load [copy]
566+
// CHECK-LABEL: } // end sil function 'with_dealloc_box'
567+
sil [ossa] @with_dealloc_box : $@convention(thin) (@owned Bar) -> () {
568+
entry(%idle_incoming : @owned $Bar):
569+
%box = alloc_box ${ var Bar }
570+
%box_borrow = begin_borrow [var_decl] %box : ${ var Bar }
571+
%box_addr = project_box %box_borrow : ${ var Bar }, 0
572+
store %idle_incoming to [init] %box_addr : $*Bar
573+
%closable = function_ref @closable : $@convention(thin) (@guaranteed { var Bar }) -> Builtin.Int64
574+
%box_copy = copy_value %box_borrow : ${ var Bar }
575+
mark_function_escape %box_addr : $*Bar
576+
%closure = partial_apply [callee_guaranteed] %closable(%box_copy) : $@convention(thin) (@guaranteed { var Bar }) -> Builtin.Int64
577+
cond_br undef, exit, die
578+
579+
die:
580+
destroy_value %closure : $@callee_guaranteed () -> Builtin.Int64
581+
end_borrow %box_borrow : ${ var Bar }
582+
dealloc_box %box : ${ var Bar }
583+
unreachable
584+
585+
exit:
586+
destroy_value %closure : $@callee_guaranteed () -> Builtin.Int64
587+
end_borrow %box_borrow : ${ var Bar }
588+
destroy_value %box : ${ var Bar }
589+
%retval = tuple ()
590+
return %retval :$()
591+
}

0 commit comments

Comments
 (0)