Skip to content

Commit 57eb1db

Browse files
committed
[LifetimeCompletion] Handle project_box.
Port a0c85be to C++ InteriorLiveness to treat `project_box` like an interior pointer use and fix lifetime completion of a borrow that encloses one.
1 parent 4e23b7f commit 57eb1db

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

include/swift/SIL/OwnershipUseVisitor.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ bool OwnershipUseVisitor<Impl>::visitGuaranteedUse(Operand *use) {
391391
return true;
392392

393393
case OperandOwnership::PointerEscape:
394+
// TODO: Change ProjectBox ownership to InteriorPointer and allow them to
395+
// take owned values.
396+
if (isa<ProjectBoxInst>(use->getUser())) {
397+
return visitInteriorPointerUses(use);
398+
}
394399
if (!asImpl().handlePointerEscape(use))
395400
return false;
396401

@@ -443,7 +448,8 @@ bool OwnershipUseVisitor<Impl>::visitGuaranteedUse(Operand *use) {
443448

444449
template <typename Impl>
445450
bool OwnershipUseVisitor<Impl>::visitInteriorPointerUses(Operand *use) {
446-
assert(use->getOperandOwnership() == OperandOwnership::InteriorPointer);
451+
assert(use->getOperandOwnership() == OperandOwnership::InteriorPointer ||
452+
isa<ProjectBoxInst>(use->getUser()));
447453

448454
if (auto scopedAddress = ScopedAddressValue::forUse(use)) {
449455
// e.g. client may need to insert end_borrow if scopedAddress is a store_borrow.

test/SILOptimizer/ossa_lifetime_completion.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,22 @@ exit:
395395
%retval = tuple ()
396396
return %retval : $()
397397
}
398+
399+
// CHECK-LABEL: sil [ossa] @project_box_deadend : {{.*}} {
400+
// CHECK: bb0([[C:%[^,]+]] :
401+
// CHECK: [[BOX:%[^,]+]] = alloc_box
402+
// CHECK: [[BOX_BORROW:%[^,]+]] = begin_borrow [[BOX]]
403+
// CHECK: [[ADDR:%[^,]+]] = project_box [[BOX_BORROW]]
404+
// CHECK: store [[C]] to [init] [[ADDR]]
405+
// CHECK: end_borrow [[BOX_BORROW]]
406+
// CHECK: unreachable
407+
// CHECK-LABEL: } // end sil function 'project_box_deadend'
408+
sil [ossa] @project_box_deadend : $@convention(thin) (@owned C) -> () {
409+
bb0(%0 : @owned $C):
410+
%2 = alloc_box ${ var C }
411+
%3 = begin_borrow %2 : ${ var C }
412+
specify_test "ossa-lifetime-completion %3"
413+
%4 = project_box %3 : ${ var C }, 0
414+
store %0 to [init] %4 : $*C
415+
unreachable
416+
}

0 commit comments

Comments
 (0)