Skip to content

Commit 6163db1

Browse files
Merge pull request #39495 from nate-chandler/lexical_lifetimes/inliner
[SILInliner] Added lexical lifetimes for arguments.
2 parents 3222322 + 6bcbb04 commit 6163db1

File tree

2 files changed

+432
-13
lines changed

2 files changed

+432
-13
lines changed

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,40 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
424424
SmallVector<SILValue, 4> entryArgs;
425425
entryArgs.reserve(AppliedArgs.size());
426426
SmallBitVector borrowedArgs(AppliedArgs.size());
427+
SmallBitVector copiedArgs(AppliedArgs.size());
428+
auto enableLexicalLifetimes =
429+
Apply.getFunction()
430+
->getModule()
431+
.getASTContext()
432+
.LangOpts.EnableExperimentalLexicalLifetimes;
427433

428434
auto calleeConv = getCalleeFunction()->getConventions();
429435
for (auto p : llvm::enumerate(AppliedArgs)) {
430436
SILValue callArg = p.value();
431437
unsigned idx = p.index();
432-
// Insert begin/end borrow for guaranteed arguments.
433-
if (idx >= calleeConv.getSILArgIndexOfFirstParam() &&
434-
calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {
435-
if (SILValue newValue = borrowFunctionArgument(callArg, Apply)) {
436-
callArg = newValue;
438+
if (idx >= calleeConv.getSILArgIndexOfFirstParam()) {
439+
if (Apply.getFunction()->hasOwnership() && enableLexicalLifetimes) {
440+
if (!callArg->getType().isTrivial(*Apply.getFunction())) {
441+
SILBuilderWithScope builder(Apply.getInstruction(), getBuilder());
442+
if (calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {
443+
callArg = builder.createBeginBorrow(Apply.getLoc(), callArg,
444+
/*isLexical*/ true);
445+
} else { /*owned*/
446+
auto *bbi = builder.createBeginBorrow(Apply.getLoc(), callArg,
447+
/*isLexical*/ true);
448+
callArg = builder.createCopyValue(Apply.getLoc(), bbi);
449+
copiedArgs[idx] = true;
450+
}
451+
}
437452
borrowedArgs[idx] = true;
453+
} else {
454+
// Insert begin/end borrow for guaranteed arguments.
455+
if (calleeConv.getParamInfoForSILArg(idx).isGuaranteed()) {
456+
if (SILValue newValue = borrowFunctionArgument(callArg, Apply)) {
457+
callArg = newValue;
458+
borrowedArgs[idx] = true;
459+
}
460+
}
438461
}
439462
}
440463
entryArgs.push_back(callArg);
@@ -443,7 +466,6 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
443466
// Create the return block and set ReturnToBB for use in visitTerminator
444467
// callbacks.
445468
SILBasicBlock *callerBlock = Apply.getParent();
446-
SILBasicBlock *throwBlock = nullptr;
447469
SmallVector<SILInstruction *, 1> endBorrowInsertPts;
448470

449471
switch (Apply.getKind()) {
@@ -476,7 +498,7 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
476498
auto *tai = cast<TryApplyInst>(Apply);
477499
ReturnToBB = tai->getNormalBB();
478500
endBorrowInsertPts.push_back(&*ReturnToBB->begin());
479-
throwBlock = tai->getErrorBB();
501+
endBorrowInsertPts.push_back(&*tai->getErrorBB()->begin());
480502
break;
481503
}
482504
}
@@ -491,12 +513,23 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
491513

492514
for (auto *insertPt : endBorrowInsertPts) {
493515
SILBuilderWithScope returnBuilder(insertPt, getBuilder());
494-
returnBuilder.createEndBorrow(Apply.getLoc(), entryArgs[i]);
495-
}
496-
497-
if (throwBlock) {
498-
SILBuilderWithScope throwBuilder(throwBlock->begin(), getBuilder());
499-
throwBuilder.createEndBorrow(Apply.getLoc(), entryArgs[i]);
516+
SILValue original;
517+
SILValue borrow;
518+
if (copiedArgs.test(i)) {
519+
auto *cvi =
520+
cast<CopyValueInst>(entryArgs[i]->getDefiningInstruction());
521+
auto *bbi = cast<BeginBorrowInst>(
522+
cvi->getOperand()->getDefiningInstruction());
523+
borrow = bbi;
524+
original = bbi->getOperand();
525+
} else {
526+
original = SILValue();
527+
borrow = entryArgs[i];
528+
}
529+
returnBuilder.createEndBorrow(Apply.getLoc(), borrow);
530+
if (original) {
531+
returnBuilder.createDestroyValue(Apply.getLoc(), original);
532+
}
500533
}
501534
}
502535
}

0 commit comments

Comments
 (0)