Skip to content

Commit f2ccd17

Browse files
committed
CanonicalOSSALifetime: canonicalize guaranteed values.
If a guaranteed value is not a recognized and handled borrow introducer, then treat the copy as a separate owned live range.
1 parent 02784a2 commit f2ccd17

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

include/swift/SILOptimizer/Utils/CanonicalOSSALifetime.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,7 @@ class CanonicalOSSAConsumeInfo {
180180
class CanonicalizeOSSALifetime {
181181
public:
182182
/// Find the original definition of a potentially copied value.
183-
///
184-
/// This use-def walk must be consistent with the def-use walks performed
185-
/// within the canonicalizeValueLifetime() implementation.
186-
static SILValue getCanonicalCopiedDef(SILValue v) {
187-
while (true) {
188-
if (auto *copy = dyn_cast<CopyValueInst>(v)) {
189-
v = copy->getOperand();
190-
continue;
191-
}
192-
return v;
193-
}
194-
}
183+
static SILValue getCanonicalCopiedDef(SILValue v);
195184

196185
private:
197186
/// If true, then debug_value instructions outside of non-debug

lib/SILOptimizer/Utils/CanonicalOSSALifetime.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
/// destroys. Initializes `liveness`.
2222
///
2323
/// 2. Find `def`s final destroy points based on its pruned
24-
/// liveness. Initializes `consumes` and inserts new destroy_value
25-
/// instructions.
24+
/// liveness. Initializes `consumes` and inserts new destroy_value
25+
/// instructions.
2626
///
27-
/// 3. Rewrite `def`s original copies and destroys, inserting new copies
28-
/// where needed. Deletes original copies and destroys and inserts new copies.
27+
/// 3. Rewrite `def`s original copies and destroys, inserting new copies where
28+
/// needed. Deletes original copies and destroys and inserts new copies.
2929
///
3030
/// See CanonicalOSSALifetime.h for examples.
3131
///
@@ -57,6 +57,34 @@ STATISTIC(NumCopiesGenerated, "number of copy_value instructions created");
5757
STATISTIC(NumDestroysGenerated, "number of destroy_value instructions created");
5858
STATISTIC(NumUnknownUsers, "number of functions with unknown users");
5959

60+
/// This use-def walk must be consistent with the def-use walks performed
61+
/// within the canonicalizeValueLifetime() implementation.
62+
SILValue CanonicalizeOSSALifetime::getCanonicalCopiedDef(SILValue v) {
63+
while (auto *copy = dyn_cast<CopyValueInst>(v)) {
64+
auto def = copy->getOperand();
65+
if (def.getOwnershipKind() == OwnershipKind::Owned) {
66+
v = def;
67+
continue;
68+
}
69+
if (auto borrowedVal = BorrowedValue::get(def)) {
70+
// Any def's that aren't filtered out here must be handled by
71+
// computeBorrowLiveness.
72+
switch (borrowedVal->kind) {
73+
case BorrowedValueKind::SILFunctionArgument:
74+
case BorrowedValueKind::BeginBorrow:
75+
return def;
76+
case BorrowedValueKind::LoadBorrow:
77+
case BorrowedValueKind::Phi:
78+
break;
79+
}
80+
}
81+
// This guaranteed value cannot be handled, treat the copy as an owned
82+
// live range def instead.
83+
return copy;
84+
}
85+
return v;
86+
}
87+
6088
//===----------------------------------------------------------------------===//
6189
// MARK: Rewrite borrow scopes
6290
//===----------------------------------------------------------------------===//
@@ -224,7 +252,7 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
224252
if (pruneDebug) {
225253
if (auto *dvi = dyn_cast<DebugValueInst>(user)) {
226254
// Only instructions potentially outside current pruned liveness are
227-
// insteresting.
255+
// interesting.
228256
if (liveness.getBlockLiveness(dvi->getParent())
229257
!= PrunedLiveBlocks::LiveOut) {
230258
recordDebugValue(dvi);

0 commit comments

Comments
 (0)