Skip to content

Commit 6bcbb04

Browse files
committed
[SILInliner] Added lexical lifetimes for arguments.
When the -enable-experimental-lexical-lifetimes flag is passed, added lexical borrow scopes for every non-trivial argument when inlining a function.
1 parent e7d5b5e commit 6bcbb04

File tree

2 files changed

+431
-6
lines changed

2 files changed

+431
-6
lines changed

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 45 additions & 6 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);
@@ -490,7 +513,23 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
490513

491514
for (auto *insertPt : endBorrowInsertPts) {
492515
SILBuilderWithScope returnBuilder(insertPt, getBuilder());
493-
returnBuilder.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+
}
494533
}
495534
}
496535
}

0 commit comments

Comments
 (0)