Skip to content

Commit 8e50c97

Browse files
authored
Merge pull request swiftlang#28725 from gottesmm/pr-1b10b0856adef409da4bed3d4af33137abf625cf
[ownership] Change BorrowScopeIntroducerValue::areInstructionsWithinScope to take SILInstructions instead of BranchPropagatedUser.
2 parents 49c26c2 + d815c60 commit 8e50c97

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ class LinearLifetimeChecker {
187187
bool validateLifetime(SILValue value,
188188
ArrayRef<SILInstruction *> consumingUses,
189189
ArrayRef<SILInstruction *> nonConsumingUses) {
190+
assert(llvm::all_of(
191+
consumingUses,
192+
[](SILInstruction *i) { return !isa<CondBranchInst>(i); }) &&
193+
"Passed cond branch to a non-BranchPropagatedUser API");
194+
assert(llvm::all_of(
195+
nonConsumingUses,
196+
[](SILInstruction *i) { return !isa<CondBranchInst>(i); }) &&
197+
"Passed cond branch to a non-BranchPropagatedUser API");
190198
auto *consumingUsesCast =
191199
reinterpret_cast<const BranchPropagatedUser *>(consumingUses.data());
192200
auto *nonConsumingUsesCast =
@@ -420,11 +428,11 @@ struct BorrowScopeIntroducingValue {
420428
///
421429
/// NOTE: Scratch space is used internally to this method to store the end
422430
/// borrow scopes if needed.
423-
bool areInstructionsWithinScope(
424-
ArrayRef<BranchPropagatedUser> instructions,
425-
SmallVectorImpl<BranchPropagatedUser> &scratchSpace,
426-
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
427-
DeadEndBlocks &deadEndBlocks) const;
431+
bool
432+
areInstructionsWithinScope(ArrayRef<SILInstruction *> instructions,
433+
SmallVectorImpl<SILInstruction *> &scratchSpace,
434+
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
435+
DeadEndBlocks &deadEndBlocks) const;
428436

429437
private:
430438
/// Internal constructor for failable static constructor. Please do not expand

lib/SIL/OwnershipUtils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
195195
}
196196

197197
bool BorrowScopeIntroducingValue::areInstructionsWithinScope(
198-
ArrayRef<BranchPropagatedUser> instructions,
199-
SmallVectorImpl<BranchPropagatedUser> &scratchSpace,
198+
ArrayRef<SILInstruction *> instructions,
199+
SmallVectorImpl<SILInstruction *> &scratchSpace,
200200
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
201201
DeadEndBlocks &deadEndBlocks) const {
202202
// Make sure that we clear our scratch space/utilities before we exit.
@@ -214,8 +214,9 @@ bool BorrowScopeIntroducingValue::areInstructionsWithinScope(
214214
return true;
215215

216216
// Otherwise, gather up our local scope ending instructions.
217-
visitLocalScopeEndingUses(
218-
[&scratchSpace](Operand *op) { scratchSpace.emplace_back(op); });
217+
visitLocalScopeEndingUses([&scratchSpace](Operand *op) {
218+
scratchSpace.emplace_back(op->getUser());
219+
});
219220

220221
LinearLifetimeChecker checker(visitedBlocks, deadEndBlocks);
221222
return checker.validateLifetime(value, scratchSpace, instructions);

lib/SILOptimizer/Mandatory/SemanticARCOpts.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "swift/Basic/BlotSetVector.h"
1515
#include "swift/Basic/STLExtras.h"
1616
#include "swift/SIL/BasicBlockUtils.h"
17-
#include "swift/SIL/BranchPropagatedUser.h"
1817
#include "swift/SIL/DebugUtils.h"
1918
#include "swift/SIL/MemAccessUtils.h"
2019
#include "swift/SIL/OwnershipUtils.h"
@@ -577,21 +576,19 @@ bool SemanticARCOptVisitor::performGuaranteedCopyValueOptimization(CopyValueInst
577576
// need to insert an end_borrow since all of our borrow introducers are
578577
// non-local scopes.
579578
{
580-
SmallVector<BranchPropagatedUser, 8> destroysForLinearLifetimeCheck;
581579
bool foundNonDeadEnd = false;
582580
for (auto *dvi : destroys) {
583581
foundNonDeadEnd |= !getDeadEndBlocks().isDeadEnd(dvi->getParent());
584-
destroysForLinearLifetimeCheck.push_back(&dvi->getAllOperands()[0]);
585582
}
586583
if (!foundNonDeadEnd && haveAnyLocalScopes)
587584
return false;
588-
SmallVector<BranchPropagatedUser, 8> scratchSpace;
585+
SmallVector<SILInstruction *, 8> scratchSpace;
589586
SmallPtrSet<SILBasicBlock *, 4> visitedBlocks;
590587
if (llvm::any_of(borrowScopeIntroducers,
591588
[&](BorrowScopeIntroducingValue borrowScope) {
592589
return !borrowScope.areInstructionsWithinScope(
593-
destroysForLinearLifetimeCheck, scratchSpace,
594-
visitedBlocks, getDeadEndBlocks());
590+
destroys, scratchSpace, visitedBlocks,
591+
getDeadEndBlocks());
595592
})) {
596593
return false;
597594
}
@@ -786,22 +783,15 @@ class StorageGuaranteesLoadVisitor
786783

787784
// Use the linear lifetime checker to check whether the copied
788785
// value is dominated by the lifetime of the borrow it's based on.
789-
SmallVector<BranchPropagatedUser, 4> baseEndBorrows;
790-
for (auto *use : borrowInst->getUses()) {
791-
if (isa<EndBorrowInst>(use->getUser())) {
792-
baseEndBorrows.emplace_back(use);
793-
}
794-
}
795-
796-
SmallVector<BranchPropagatedUser, 4> valueDestroys;
797-
for (auto *use : Load->getUses()) {
798-
if (isa<DestroyValueInst>(use->getUser())) {
799-
valueDestroys.emplace_back(use);
800-
}
801-
}
802-
803-
SmallPtrSet<SILBasicBlock *, 4> visitedBlocks;
786+
SmallVector<SILInstruction *, 4> baseEndBorrows;
787+
llvm::copy(borrowInst->getUsersOfType<EndBorrowInst>(),
788+
std::back_inserter(baseEndBorrows));
804789

790+
SmallVector<SILInstruction *, 4> valueDestroys;
791+
llvm::copy(Load->getUsersOfType<DestroyValueInst>(),
792+
std::back_inserter(valueDestroys));
793+
794+
SmallPtrSet<SILBasicBlock *, 4> visitedBlocks;
805795
LinearLifetimeChecker checker(visitedBlocks, ARCOpt.getDeadEndBlocks());
806796
// Returns true on success. So we invert.
807797
bool foundError =

0 commit comments

Comments
 (0)