Skip to content

Commit 12abf7e

Browse files
Merge pull request #83741 from nate-chandler/rdar158353230
[OSSA] Fix borrowCopyOverGuaranteedUsers at dead-ends.
2 parents f96a5e5 + 017f003 commit 12abf7e

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

include/swift/SILOptimizer/Utils/OwnershipOptUtils.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ void extendLocalBorrow(BeginBorrowInst *beginBorrow,
7979
/// newly created phis do not yet have a borrow scope.
8080
bool createBorrowScopeForPhiOperands(SILPhiArgument *newPhi);
8181

82-
SILValue
83-
makeGuaranteedValueAvailable(SILValue value, SILInstruction *user,
84-
DeadEndBlocks &deBlocks,
85-
InstModCallbacks callbacks = InstModCallbacks());
86-
8782
/// Compute the liveness boundary for a guaranteed value. Returns true if no
8883
/// uses are pointer escapes. If pointer escapes are present, the liveness
8984
/// boundary is still valid for all known uses.

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,6 @@ struct OwnershipLifetimeExtender {
805805
/// the BorrowedValue that begins the scope.
806806
SILValue borrowOverSingleUse(SILValue newValue,
807807
Operand *singleGuaranteedUse);
808-
809-
SILValue
810-
borrowOverSingleNonLifetimeEndingUser(SILValue newValue,
811-
SILInstruction *nonLifetimeEndingUser);
812808
};
813809

814810
} // end anonymous namespace
@@ -973,7 +969,14 @@ BeginBorrowInst *OwnershipLifetimeExtender::borrowCopyOverGuaranteedUsers(
973969
// Create destroys at the end of copy's lifetime. This only needs to consider
974970
// uses that end the borrow scope.
975971
{
976-
ValueLifetimeAnalysis lifetimeAnalysis(copy, borrow->getEndBorrows());
972+
SmallVector<SILInstruction *, 16> users;
973+
for (auto *user : guaranteedUsers) {
974+
users.push_back(user);
975+
}
976+
for (auto *user : borrow->getEndBorrows()) {
977+
users.push_back(user);
978+
}
979+
ValueLifetimeAnalysis lifetimeAnalysis(copy, users);
977980
ValueLifetimeBoundary copyBoundary;
978981
lifetimeAnalysis.computeLifetimeBoundary(copyBoundary);
979982

@@ -1092,27 +1095,6 @@ OwnershipLifetimeExtender::borrowOverSingleUse(SILValue newValue,
10921095
return newBeginBorrow;
10931096
}
10941097

1095-
SILValue OwnershipLifetimeExtender::borrowOverSingleNonLifetimeEndingUser(
1096-
SILValue newValue, SILInstruction *nonLifetimeEndingUser) {
1097-
// Avoid borrowing guaranteed function arguments.
1098-
if (isa<SILFunctionArgument>(newValue) &&
1099-
newValue->getOwnershipKind() == OwnershipKind::Guaranteed) {
1100-
return newValue;
1101-
}
1102-
auto borrowPt = newValue->getNextInstruction()->getIterator();
1103-
return borrowCopyOverGuaranteedUsers(
1104-
newValue, borrowPt, ArrayRef<SILInstruction *>(nonLifetimeEndingUser));
1105-
}
1106-
1107-
SILValue swift::makeGuaranteedValueAvailable(SILValue value,
1108-
SILInstruction *user,
1109-
DeadEndBlocks &deBlocks,
1110-
InstModCallbacks callbacks) {
1111-
OwnershipFixupContext ctx{callbacks, deBlocks};
1112-
OwnershipLifetimeExtender extender{ctx};
1113-
return extender.borrowOverSingleNonLifetimeEndingUser(value, user);
1114-
}
1115-
11161098
//===----------------------------------------------------------------------===//
11171099
// OwnershipRAUWUtility - RAUW + fix ownership
11181100
//===----------------------------------------------------------------------===//

test/SILOptimizer/cse_ossa_nontrivial.sil

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,3 +1164,38 @@ bb0:
11641164
%13 = tuple ()
11651165
return %13
11661166
}
1167+
1168+
/// A scenario where a borrow of a copy is introduced but the cfg is such that
1169+
/// no end_borrows are inserted for the borrow. The copy must still be
1170+
/// destroyed.
1171+
// CHECK-LABEL: sil [ossa] @no_boundary_for_end_borrows : {{.*}} {
1172+
// TODO: There shouldn't actually be a copy_value here, but lifetime transfer
1173+
// APIs are written and OSSARAUWHelper is in use, there will be.
1174+
// Even then, this test case shouldn't crash.
1175+
// CHECK: copy_value
1176+
// CHECK: destroy_value
1177+
// CHECK-LABEL: } // end sil function 'no_boundary_for_end_borrows'
1178+
sil [ossa] @no_boundary_for_end_borrows : $@convention(thin) (@guaranteed NonTrivialStruct) -> () {
1179+
entry(%s : @guaranteed $NonTrivialStruct):
1180+
%c1 = struct_extract %s : $NonTrivialStruct, #NonTrivialStruct.val
1181+
cond_br undef, loop_entry, exit
1182+
1183+
exit:
1184+
apply undef(%c1) : $@convention(thin) (@guaranteed Klass) -> ()
1185+
return undef : $()
1186+
1187+
loop_entry:
1188+
%c2 = struct_extract %s : $NonTrivialStruct, #NonTrivialStruct.val
1189+
apply undef(%c2) : $@convention(thin) (@guaranteed Klass) -> ()
1190+
br loop_header
1191+
1192+
loop_header:
1193+
apply undef(%c2) : $@convention(thin) (@guaranteed Klass) -> ()
1194+
cond_br undef, die, loop_backedge
1195+
1196+
loop_backedge:
1197+
br loop_header
1198+
1199+
die:
1200+
unreachable
1201+
}

0 commit comments

Comments
 (0)