Skip to content

[SILInliner] Only guaranteed args get lexical lifetimes. #39712

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
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
59 changes: 13 additions & 46 deletions lib/SILOptimizer/Utils/SILInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,41 +428,18 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
entryArgs.reserve(AppliedArgs.size());
SmallBitVector borrowedArgs(AppliedArgs.size());
SmallBitVector copiedArgs(AppliedArgs.size());
auto enableLexicalLifetimes =
Apply.getFunction()
->getModule()
.getASTContext()
.LangOpts.EnableExperimentalLexicalLifetimes;

auto calleeConv = getCalleeFunction()->getConventions();
for (auto p : llvm::enumerate(AppliedArgs)) {
SILValue callArg = p.value();
unsigned idx = p.index();
if (idx >= calleeConv.getSILArgIndexOfFirstParam()) {
if (enableLexicalLifetimes) {
if (Apply.getFunction()->hasOwnership() &&
!callArg->getType().isTrivial(*Apply.getFunction()) &&
!callArg->getType().isAddress()) {
SILBuilderWithScope builder(Apply.getInstruction(), getBuilder());
if (calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {
callArg = builder.createBeginBorrow(Apply.getLoc(), callArg,
/*isLexical*/ true);
} else { /*owned*/
auto *bbi = builder.createBeginBorrow(Apply.getLoc(), callArg,
/*isLexical*/ true);
callArg = builder.createCopyValue(Apply.getLoc(), bbi);
copiedArgs[idx] = true;
}
// Insert begin/end borrow for guaranteed arguments.
if (calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {
if (SILValue newValue = borrowFunctionArgument(callArg, Apply)) {
callArg = newValue;
borrowedArgs[idx] = true;
}
} else {
// Insert begin/end borrow for guaranteed arguments.
if (calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {
if (SILValue newValue = borrowFunctionArgument(callArg, Apply)) {
callArg = newValue;
borrowedArgs[idx] = true;
}
}
}
}
entryArgs.push_back(callArg);
Expand Down Expand Up @@ -518,23 +495,7 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {

for (auto *insertPt : endBorrowInsertPts) {
SILBuilderWithScope returnBuilder(insertPt, getBuilder());
SILValue original;
SILValue borrow;
if (copiedArgs.test(i)) {
auto *cvi =
cast<CopyValueInst>(entryArgs[i]->getDefiningInstruction());
auto *bbi = cast<BeginBorrowInst>(
cvi->getOperand()->getDefiningInstruction());
borrow = bbi;
original = bbi->getOperand();
} else {
original = SILValue();
borrow = entryArgs[i];
}
returnBuilder.createEndBorrow(Apply.getLoc(), borrow);
if (original) {
returnBuilder.createDestroyValue(Apply.getLoc(), original);
}
returnBuilder.createEndBorrow(Apply.getLoc(), entryArgs[i]);
}
}
}
Expand Down Expand Up @@ -636,13 +597,19 @@ void SILInlineCloner::postFixUp(SILFunction *calleeFunction) {

SILValue SILInlineCloner::borrowFunctionArgument(SILValue callArg,
FullApplySite AI) {
auto enableLexicalLifetimes = Apply.getFunction()
->getModule()
.getASTContext()
.LangOpts.EnableExperimentalLexicalLifetimes;
if (!AI.getFunction()->hasOwnership() ||
callArg.getOwnershipKind() != OwnershipKind::Owned) {
(callArg.getOwnershipKind() != OwnershipKind::Owned &&
!enableLexicalLifetimes)) {
return SILValue();
}

SILBuilderWithScope beginBuilder(AI.getInstruction(), getBuilder());
return beginBuilder.createBeginBorrow(AI.getLoc(), callArg);
return beginBuilder.createBeginBorrow(AI.getLoc(), callArg,
/*isLexical=*/enableLexicalLifetimes);
}

void SILInlineCloner::visitDebugValueInst(DebugValueInst *Inst) {
Expand Down
89 changes: 6 additions & 83 deletions test/SILOptimizer/inline_lifetime.sil
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ entry(%instance : $*S):
// tests

// CHECK-LABEL: sil [ossa] @caller_owned_callee_owned : $@convention(thin) (@owned C) -> () {
// CHECK: {{bb[^,]+}}([[INSTANCE:%[^,]+]] : @owned $C):
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[INSTANCE]]
// CHECK: [[COPY:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK: destroy_value [[COPY]]
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[INSTANCE]]
// CHECK: return [[RETVAL]]
// CHECK-NOT: begin_borrow [lexical]
// CHECK-LABEL: } // end sil function 'caller_owned_callee_owned'
sil [ossa] @caller_owned_callee_owned : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
Expand Down Expand Up @@ -87,15 +80,7 @@ entry(%instance : @guaranteed $C):
}

// CHECK-LABEL: sil [ossa] @caller_guaranteed_callee_owned : $@convention(thin) (@guaranteed C) -> () {
// CHECK: {{bb[^,]+}}([[INSTANCE:%[^,]+]] : @guaranteed $C):
// CHECK: [[COPY:%[^,]+]] = copy_value [[INSTANCE]]
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[COPY]]
// CHECK: [[LIFETIME_OWNED:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK: destroy_value [[LIFETIME_OWNED]]
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[COPY]]
// CHECK: return [[RETVAL]]
// CHECK-NOT: begin_borrow [lexical]
// CHECK-LABEL: } // end sil function 'caller_guaranteed_callee_owned'
sil [ossa] @caller_guaranteed_callee_owned : $@convention(thin) (@guaranteed C) -> () {
entry(%instance : @guaranteed $C):
Expand Down Expand Up @@ -195,22 +180,7 @@ bb2:
// tests

// CHECK-LABEL: sil [ossa] @caller_owned_callee_coro_owned : $@convention(method) (@owned C) -> () {
// CHECK: {{bb[^,]+}}([[INSTANCE:%[^,]+]] : @owned $C):
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[INSTANCE]]
// CHECK: [[LIFETIME_OWNED:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK: [[ADDR:%[^,]+]] = alloc_stack $C
// CHECK: store [[LIFETIME_OWNED]] to [init] [[ADDR]]
// CHECK: destroy_addr [[ADDR]]
// CHECK: dealloc_stack [[ADDR]]
// CHECK: [[ORIGINAL_RETVAL:%[^,]+]] = tuple ()
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[INSTANCE]]
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
// CHECK: return [[RETVAL]]
// CHECK: bb1:
// CHECK: destroy_addr [[ADDR]]
// CHECK: dealloc_stack [[ADDR]]
// CHECK: unreachable
// CHECK-NOT: begin_borrow [lexical]
// CHECK-LABEL: } // end sil function 'caller_owned_callee_coro_owned'
sil [ossa] @caller_owned_callee_coro_owned : $@convention(method) (@owned C) -> () {
bb0(%instance : @owned $C):
Expand Down Expand Up @@ -250,23 +220,7 @@ bb0(%instance : @owned $C):
}

// CHECK-LABEL: sil [ossa] @caller_guaranteed_callee_coro_owned : $@convention(method) (@guaranteed C) -> () {
// CHECK: {{bb[^,]+}}([[INSTANCE:%[^,]+]] : @guaranteed $C):
// CHECK: [[COPY:%[^,]+]] = copy_value [[INSTANCE]]
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[COPY]]
// CHECK: [[LIFETIME_OWNED:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK: [[ADDR:%[^,]+]] = alloc_stack $C
// CHECK: store [[LIFETIME_OWNED]] to [init] [[ADDR]]
// CHECK: destroy_addr [[ADDR]]
// CHECK: dealloc_stack [[ADDR]]
// CHECK: [[ORIGINAL_RETVAL:%[^,]+]] = tuple ()
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[COPY]]
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
// CHECK: return [[RETVAL]]
// CHECK: bb1:
// CHECK: destroy_addr [[ADDR]]
// CHECK: dealloc_stack [[ADDR]]
// CHECK: unreachable
// CHECK-NOT: begin_borrow [lexical]
// CHECK-LABEL: } // end sil function 'caller_guaranteed_callee_coro_owned'
sil [ossa] @caller_guaranteed_callee_coro_owned : $@convention(method) (@guaranteed C) -> () {
bb0(%instance : @guaranteed $C):
Expand Down Expand Up @@ -393,22 +347,7 @@ bb2:
// tests

// CHECK-LABEL: sil [ossa] @callee_owned_callee_error_owned : $@convention(thin) (@owned C) -> @error Error {
// CHECK: {{bb[^,]+}}([[INSTANCE:%[^,]+]] : @owned $C):
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[INSTANCE]]
// CHECK: [[LIFETIME_OWNED:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK: cond_br undef, [[THROW_BLOCK:bb[^,]+]], [[REGULAR_BLOCK:bb[0-9]+]]
// CHECK: [[THROW_BLOCK]]:
// CHECK: destroy_value [[LIFETIME_OWNED]]
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[INSTANCE]]
// CHECK: throw undef
// CHECK: [[REGULAR_BLOCK]]:
// CHECK: destroy_value [[LIFETIME_OWNED]]
// CHECK: [[ORIGINAL_RETVAL:%[^,]+]] = tuple ()
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[INSTANCE]]
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
// CHECK: return [[RETVAL]]
// CHECK-NOT: begin_borrow [lexical]
// CHECK-LABEL: } // end sil function 'callee_owned_callee_error_owned'
sil [ossa] @callee_owned_callee_error_owned : $@convention(thin) (@owned C) -> @error Error {
bb0(%instance : @owned $C):
Expand Down Expand Up @@ -451,23 +390,7 @@ bb2(%12 : @owned $Error):
}

// CHECK-LABEL: sil [ossa] @callee_guaranteed_callee_error_owned : $@convention(thin) (@guaranteed C) -> @error Error {
// CHECK: {{bb[^,]+}}([[INSTANCE:%[^,]+]] : @guaranteed $C):
// CHECK: [[COPY:%[^,]+]] = copy_value [[INSTANCE]]
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[COPY]]
// CHECK: [[LIFETIME_OWNED:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK: cond_br undef, [[THROW_BLOCK:bb[^,]+]], [[REGULAR_BLOCK:bb[0-9]+]]
// CHECK: [[THROW_BLOCK]]:
// CHECK: destroy_value [[LIFETIME_OWNED]]
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[COPY]]
// CHECK: throw undef
// CHECK: [[REGULAR_BLOCK]]:
// CHECK: destroy_value [[LIFETIME_OWNED]]
// CHECK: [[ORIGINAL_RETVAL:%[^,]+]] = tuple ()
// CHECK: end_borrow [[LIFETIME]]
// CHECK: destroy_value [[COPY]]
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
// CHECK: return [[RETVAL]]
// CHECK-NOT: begin_borrow [lexical]
// CHECK-LABEL: } // end sil function 'callee_guaranteed_callee_error_owned'
sil [ossa] @callee_guaranteed_callee_error_owned : $@convention(thin) (@guaranteed C) -> @error Error {
bb0(%instance : @guaranteed $C):
Expand Down