@@ -424,17 +424,40 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
424
424
SmallVector<SILValue, 4 > entryArgs;
425
425
entryArgs.reserve (AppliedArgs.size ());
426
426
SmallBitVector borrowedArgs (AppliedArgs.size ());
427
+ SmallBitVector copiedArgs (AppliedArgs.size ());
428
+ auto enableLexicalLifetimes =
429
+ Apply.getFunction ()
430
+ ->getModule ()
431
+ .getASTContext ()
432
+ .LangOpts .EnableExperimentalLexicalLifetimes ;
427
433
428
434
auto calleeConv = getCalleeFunction ()->getConventions ();
429
435
for (auto p : llvm::enumerate (AppliedArgs)) {
430
436
SILValue callArg = p.value ();
431
437
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
+ }
437
452
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
+ }
438
461
}
439
462
}
440
463
entryArgs.push_back (callArg);
@@ -443,7 +466,6 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
443
466
// Create the return block and set ReturnToBB for use in visitTerminator
444
467
// callbacks.
445
468
SILBasicBlock *callerBlock = Apply.getParent ();
446
- SILBasicBlock *throwBlock = nullptr ;
447
469
SmallVector<SILInstruction *, 1 > endBorrowInsertPts;
448
470
449
471
switch (Apply.getKind ()) {
@@ -476,7 +498,7 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
476
498
auto *tai = cast<TryApplyInst>(Apply);
477
499
ReturnToBB = tai->getNormalBB ();
478
500
endBorrowInsertPts.push_back (&*ReturnToBB->begin ());
479
- throwBlock = tai->getErrorBB ();
501
+ endBorrowInsertPts. push_back (&* tai->getErrorBB ()-> begin () );
480
502
break ;
481
503
}
482
504
}
@@ -491,12 +513,23 @@ SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
491
513
492
514
for (auto *insertPt : endBorrowInsertPts) {
493
515
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
+ }
500
533
}
501
534
}
502
535
}
0 commit comments