Skip to content

Commit 3d3e211

Browse files
committed
[capture-promotion] Instead of using create*Borrow, use emit*BorrowOperation
This fixes a bug where we were inserting a begin_borrow on a guaranteed parameter, but never paired it with an end_borrow so the verifier was unhappy.
1 parent 345e988 commit 3d3e211

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/SILOptimizer/IPO/CapturePromotion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ ClosureCloner::populateCloned() {
485485
if (Cloned->hasOwnership() &&
486486
MappedValue.getOwnershipKind() != ValueOwnershipKind::Any) {
487487
SILLocation Loc(const_cast<ValueDecl *>((*I)->getDecl()));
488-
MappedValue = getBuilder().createBeginBorrow(Loc, MappedValue);
488+
MappedValue = getBuilder().emitBeginBorrowOperation(Loc, MappedValue);
489489
}
490490
entryArgs.push_back(MappedValue);
491491

@@ -578,7 +578,7 @@ void ClosureCloner::visitDestroyValueInst(DestroyValueInst *Inst) {
578578
Value.getOwnershipKind() != ValueOwnershipKind::Any) {
579579
auto *BBI = cast<BeginBorrowInst>(Value);
580580
Value = BBI->getOperand();
581-
B.createEndBorrow(Inst->getLoc(), BBI, Value);
581+
B.emitEndBorrowOperation(Inst->getLoc(), BBI);
582582
}
583583

584584
typeLowering.emitDestroyValue(B, Inst->getLoc(), Value);

test/SILOptimizer/capture_promotion_ownership.sil

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ sil @foo_allocating_init : $@convention(thin) (@thick Foo.Type) -> @owned Foo
3030
sil @baz_init : $@convention(thin) (@thin Baz.Type) -> @owned Baz
3131
sil @dummy_func : $@convention(thin) (Int, Int, Int) -> Int
3232
sil @destructured_baz_user : $@convention(thin) (@owned Bar, @guaranteed Bar, Int) -> ()
33+
sil @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
3334

3435
// CHECK-LABEL: sil [ossa] @test_capture_promotion
3536
sil [ossa] @test_capture_promotion : $@convention(thin) () -> @owned @callee_owned () -> (Int, Builtin.Int64) {
@@ -471,3 +472,36 @@ bb0(%0 : @owned $<τ_0_0> { var τ_0_0 } <Baz>, %1 : @owned $<τ_0_0> { var τ_0
471472
%t = tuple()
472473
return %t : $()
473474
}
475+
476+
477+
478+
// Make sure that we properly handle guaranteed arguments when specializing here.
479+
sil [ossa] @capture_promotion_callee_guaranteed_box : $@convention(thin) (@guaranteed { var Builtin.NativeObject }) -> () {
480+
bb0(%0 : @guaranteed ${ var Builtin.NativeObject }):
481+
%1 = project_box %0 : ${ var Builtin.NativeObject }, 0
482+
%2 = begin_access [read] [unknown] %1 : $*Builtin.NativeObject
483+
%3 = load [copy] %2 : $*Builtin.NativeObject
484+
end_access %2 : $*Builtin.NativeObject
485+
%4 = function_ref @guaranteed_nativeobject_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
486+
apply %4(%3) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
487+
destroy_value %3 : $Builtin.NativeObject
488+
%9999 = tuple()
489+
return %9999 : $()
490+
}
491+
492+
// Just make sure that we specialized so that the ownership verifier can make
493+
// sure we emitted ownership correctly.
494+
//
495+
// CHECK-LABEL: sil [ossa] @capture_promotion_caller_guaranteed_box : $@convention(thin) (@owned Builtin.NativeObject) -> @owned @callee_owned () -> () {
496+
// CHECK: [[FUNC_REF:%.*]] = function_ref @$s39capture_promotion_callee_guaranteed_boxTf2i_n :
497+
// CHECK: apply [[FUNC_REF]](
498+
// CHECK: } // end sil function 'capture_promotion_caller_guaranteed_box'
499+
sil [ossa] @capture_promotion_caller_guaranteed_box : $@convention(thin) (@owned Builtin.NativeObject) -> @owned @callee_owned () -> () {
500+
bb0(%0 : @owned $Builtin.NativeObject):
501+
%1 = alloc_box ${ var Builtin.NativeObject }
502+
%2 = project_box %1 : ${ var Builtin.NativeObject }, 0
503+
store %0 to [init] %2 : $*Builtin.NativeObject
504+
%3 = function_ref @capture_promotion_callee_guaranteed_box : $@convention(thin) (@guaranteed { var Builtin.NativeObject }) -> ()
505+
%pai = partial_apply %3(%1) : $@convention(thin) (@guaranteed { var Builtin.NativeObject }) -> ()
506+
return %pai : $@callee_owned () -> ()
507+
}

0 commit comments

Comments
 (0)