Skip to content

[SILInliner] Borrow guaranteed yields. #62286

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 1 commit into from
Dec 7, 2022
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
18 changes: 18 additions & 0 deletions lib/SILOptimizer/Utils/SILInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,20 @@ class BeginApplySite {
auto calleeYields = yield->getYieldedValues();
auto callerYields = BeginApply->getYieldedValues();
assert(calleeYields.size() == callerYields.size());
SmallVector<BeginBorrowInst *, 2> guaranteedYields;
for (auto i : indices(calleeYields)) {
auto remappedYield = getMappedValue(calleeYields[i]);
// When owned values are yielded @guaranteed, the mapped value must be
// borrowed and the result be substituted in place of the originally
// yielded value. Otherwise, there could be uses of the original value
// which require an @guaranteed operand into which we'd be attempting to
// substitute an @owned operand.
if (calleeYields[i]->getOwnershipKind() == OwnershipKind::Owned &&
!yield->getOperandRef(i).isConsuming()) {
auto *bbi = Builder->createBeginBorrow(Loc, remappedYield);
guaranteedYields.push_back(bbi);
remappedYield = bbi;
}
callerYields[i]->replaceAllUsesWith(remappedYield);
}
Builder->createBranch(Loc, returnToBB);
Expand All @@ -172,11 +184,17 @@ class BeginApplySite {
if (EndApply) {
SavedInsertionPointRAII savedIP(*Builder, EndApplyBB);
auto resumeBB = remapBlock(yield->getResumeBB());
for (auto *bbi : guaranteedYields) {
Builder->createEndBorrow(EndApply->getLoc(), bbi);
}
Builder->createBranch(EndApply->getLoc(), resumeBB);
}
if (AbortApply) {
SavedInsertionPointRAII savedIP(*Builder, AbortApplyBB);
auto unwindBB = remapBlock(yield->getUnwindBB());
for (auto *bbi : guaranteedYields) {
Builder->createEndBorrow(EndApply->getLoc(), bbi);
}
Builder->createBranch(AbortApply->getLoc(), unwindBB);
}
return true;
Expand Down
62 changes: 62 additions & 0 deletions test/SILOptimizer/inline_begin_apply.sil
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ unwind:
unwind
}

sil [ossa] @getSomeClass : $@convention(thin) () -> @owned SomeClass

sil [transparent] [ossa] @yield_owned_as_guaranteed : $@yield_once @convention(thin) () -> @yields @guaranteed SomeClass {
%getSomeClass = function_ref @getSomeClass : $@convention(thin) () -> @owned SomeClass
%SomeClass = apply %getSomeClass() : $@convention(thin) () -> @owned SomeClass
yield %SomeClass : $SomeClass, resume bb1, unwind bb2

bb1:
destroy_value %SomeClass : $SomeClass
%retval = tuple ()
return %retval : $()

bb2:
destroy_value %SomeClass : $SomeClass
unwind
}

sil [transparent] [ossa] @test_unreachable : $@yield_once <C: SomeClass> () -> (@yields @in Indirect<C>) {
entry:
unreachable
Expand Down Expand Up @@ -555,3 +572,48 @@ bb2:
return %ret : $()
}


// CHECK-LABEL: sil [ossa] @test_store_borrow_owned_as_guaranteed_yield : {{.*}} {
// CHECK: {{bb[0-9]+}}:
// CHECK: [[ADDR:%[^,]+]] = alloc_stack $SomeClass
// CHECK: [[INSTANCE:%[^,]+]] = apply
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [[INSTANCE]]
// CHECK: [[BORROW:%[^,]+]] = store_borrow [[LIFETIME]] to [[ADDR]]
// CHECK: yield [[BORROW]] {{.*}}, resume [[BB_RESUME:bb[0-9]+]], unwind [[BB_UNWIND:bb[0-9]+]]
// CHECK: [[BB_RESUME]]:
// CHECK: end_borrow [[BORROW]]
// CHECK: dealloc_stack [[ADDR]]
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[INSTANCE]]
// CHECK: tuple ()
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
// CHECK: return [[RETVAL]]
// CHECK: [[BB_UNWIND]]:
// CHECK: end_borrow [[BORROW]]
// CHECK: dealloc_stack [[ADDR]]
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[INSTANCE]]
// CHECK: unwind
// CHECK-LABEL: } // end sil function 'test_store_borrow_owned_as_guaranteed_yield'
sil [ossa] @test_store_borrow_owned_as_guaranteed_yield : $@yield_once @convention(thin) @substituted <T> () -> @yields @in_guaranteed T for <SomeClass> {
entry:
%addr = alloc_stack $SomeClass
%callee = function_ref @yield_owned_as_guaranteed : $@yield_once @convention(thin) () -> @yields @guaranteed SomeClass
(%instance, %token) = begin_apply %callee() : $@yield_once @convention(thin) () -> @yields @guaranteed SomeClass
%borrow = store_borrow %instance to %addr : $*SomeClass
yield %borrow : $*SomeClass, resume bb_resume, unwind bb_unwind

bb_resume:
end_borrow %borrow : $*SomeClass
dealloc_stack %addr : $*SomeClass
end_apply %token
%retval = tuple ()
return %retval : $()

bb_unwind:
end_borrow %borrow : $*SomeClass
dealloc_stack %addr : $*SomeClass
abort_apply %token
unwind

}