Skip to content

Commit 29d046f

Browse files
committed
[SIL] Add BorrowedValueKind::BeginApplyToken.
The token is a guaranteed value introduced by the begin_apply and consumed by its end_apply, abort_apply, and end_borrow users.
1 parent 5307ac6 commit 29d046f

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ class BorrowedValueKind {
490490
BeginBorrow,
491491
SILFunctionArgument,
492492
Phi,
493+
BeginApplyToken,
493494
};
494495

495496
private:
@@ -520,6 +521,13 @@ class BorrowedValueKind {
520521
}
521522
return Kind::Phi;
522523
}
524+
case ValueKind::MultipleValueInstructionResult:
525+
if (!isaResultOf<BeginApplyInst>(value))
526+
return Kind::Invalid;
527+
if (value->isBeginApplyToken()) {
528+
return Kind::BeginApplyToken;
529+
}
530+
return Kind::Invalid;
523531
}
524532
}
525533

@@ -540,6 +548,7 @@ class BorrowedValueKind {
540548
case BorrowedValueKind::BeginBorrow:
541549
case BorrowedValueKind::LoadBorrow:
542550
case BorrowedValueKind::Phi:
551+
case BorrowedValueKind::BeginApplyToken:
543552
return true;
544553
case BorrowedValueKind::SILFunctionArgument:
545554
return false;

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,9 @@ void BorrowedValueKind::print(llvm::raw_ostream &os) const {
913913
case BorrowedValueKind::Phi:
914914
os << "Phi";
915915
return;
916+
case BorrowedValueKind::BeginApplyToken:
917+
os << "BeginApply";
918+
return;
916919
}
917920
llvm_unreachable("Covered switch isn't covered?!");
918921
}
@@ -945,6 +948,7 @@ bool BorrowedValue::visitLocalScopeEndingUses(
945948
case BorrowedValueKind::LoadBorrow:
946949
case BorrowedValueKind::BeginBorrow:
947950
case BorrowedValueKind::Phi:
951+
case BorrowedValueKind::BeginApplyToken:
948952
for (auto *use : lookThroughBorrowedFromUser(value)->getUses()) {
949953
if (use->isLifetimeEnding()) {
950954
if (!visitor(use))
@@ -1819,6 +1823,7 @@ class FindEnclosingDefs {
18191823

18201824
case BorrowedValueKind::LoadBorrow:
18211825
case BorrowedValueKind::SILFunctionArgument:
1826+
case BorrowedValueKind::BeginApplyToken:
18221827
// There is no enclosing def on this path.
18231828
return true;
18241829
}
@@ -2037,6 +2042,7 @@ class FindEnclosingDefs {
20372042
}
20382043
case BorrowedValueKind::LoadBorrow:
20392044
case BorrowedValueKind::SILFunctionArgument:
2045+
case BorrowedValueKind::BeginApplyToken:
20402046
// There is no enclosing def on this path.
20412047
break;
20422048
}

lib/SILOptimizer/Utils/CanonicalizeBorrowScope.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ SILValue CanonicalizeBorrowScope::getCanonicalBorrowedDef(SILValue def) {
140140

141141
case BorrowedValueKind::LoadBorrow:
142142
case BorrowedValueKind::Phi:
143+
case BorrowedValueKind::BeginApplyToken:
143144
break;
144145
}
145146
}
@@ -170,6 +171,7 @@ bool CanonicalizeBorrowScope::computeBorrowLiveness() {
170171
// can handle persistentCopies.
171172
return false;
172173
case BorrowedValueKind::BeginBorrow:
174+
case BorrowedValueKind::BeginApplyToken:
173175
break;
174176
}
175177
// Note that there is no need to look through any reborrows. The reborrowed

test/SILOptimizer/ossa_lifetime_completion.sil

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,27 @@ entry(%instance : @owned $C):
513513
store %instance to [init] %addr : $*C
514514
unreachable
515515
}
516+
517+
// CHECK-LABEL: sil [ossa] @begin_apply : {{.*}} {
518+
// CHECK: ({{%[^,]+}}, [[TOKEN:%[^,]+]]) = begin_apply undef()
519+
// CHECK: cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
520+
// CHECK: [[LEFT]]:
521+
// CHECK: end_apply [[TOKEN]]
522+
// CHECK: [[RIGHT]]:
523+
// CHECK: end_borrow [[TOKEN]]
524+
// CHECK: unreachable
525+
// CHECK-LABEL: } // end sil function 'begin_apply'
526+
sil [ossa] @begin_apply : $@convention(thin) () -> () {
527+
entry:
528+
(%_, %token) = begin_apply undef() : $@yield_once @convention(thin) () -> (@yields @in_guaranteed ())
529+
specify_test "ossa-lifetime-completion %token"
530+
cond_br undef, left, right
531+
532+
left:
533+
end_apply %token as $()
534+
%retval = tuple ()
535+
return %retval : $()
536+
537+
right:
538+
unreachable
539+
}

0 commit comments

Comments
 (0)