Skip to content

Commit ab0be3a

Browse files
committed
[ShrinkBorrowScope] Return modified copy_values.
So that CopyPropagation and other clients can react accordingly, pass back a list of copy_value instructions that were rewritten by ShrinkBorrowScope. In CopyPropagation, add each modified copy to the copy worklist.
1 parent 834558e commit ab0be3a

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

include/swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ class CanonicalizeBorrowScope {
147147
bool consolidateBorrowScope();
148148
};
149149

150-
bool shrinkBorrowScope(BeginBorrowInst *bbi, InstructionDeleter &deleter);
150+
bool shrinkBorrowScope(
151+
BeginBorrowInst *bbi, InstructionDeleter &deleter,
152+
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts);
151153

152154
} // namespace swift
153155

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ void CopyPropagation::run() {
438438
// blocks and pushing begin_borrows as we see them and then popping them
439439
// off the end will result in shrinking inner borrow scopes first.
440440
while (auto *bbi = beginBorrowsToShrink.pop()) {
441-
changed |= shrinkBorrowScope(bbi, deleter);
441+
SmallVector<CopyValueInst *, 4> modifiedCopyValueInsts;
442+
changed |= shrinkBorrowScope(bbi, deleter, modifiedCopyValueInsts);
443+
for (auto *cvi : modifiedCopyValueInsts)
444+
defWorklist.updateForCopy(cvi);
442445
}
443446
deleter.cleanupDeadInstructions();
444447

lib/SILOptimizer/Utils/ShrinkBorrowScope.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class ShrinkBorrowScope {
4747

4848
InstructionDeleter &deleter;
4949

50+
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts;
51+
5052
SmallPtrSet<SILInstruction *, 16> users;
5153
llvm::SmallVector<std::pair<SILBasicBlock *, SILInstruction *>>
5254
barrierInstructions;
@@ -66,8 +68,10 @@ class ShrinkBorrowScope {
6668
llvm::SmallDenseMap<SILBasicBlock *, SILInstruction *> startingInstructions;
6769

6870
public:
69-
ShrinkBorrowScope(BeginBorrowInst *bbi, InstructionDeleter &deleter)
70-
: introducer(bbi), deleter(deleter) {}
71+
ShrinkBorrowScope(BeginBorrowInst *bbi, InstructionDeleter &deleter,
72+
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts)
73+
: introducer(bbi), deleter(deleter),
74+
modifiedCopyValueInsts(modifiedCopyValueInsts) {}
7175

7276
bool run();
7377

@@ -156,6 +160,7 @@ class ShrinkBorrowScope {
156160
auto borrowee = introducer->getOperand();
157161
if (auto *cvi = dyn_cast<CopyValueInst>(argument)) {
158162
cvi->setOperand(borrowee);
163+
modifiedCopyValueInsts.push_back(cvi);
159164
} else {
160165
apply.setArgument(index, borrowee);
161166
}
@@ -172,6 +177,7 @@ class ShrinkBorrowScope {
172177
if (canReplaceValueWithBorrowedValue(cvi->getOperand())) {
173178
auto borrowee = introducer->getOperand();
174179
cvi->setOperand(borrowee);
180+
modifiedCopyValueInsts.push_back(cvi);
175181
return true;
176182
}
177183
}
@@ -318,8 +324,9 @@ void ShrinkBorrowScope::createEndBorrow(SILInstruction *insertionPoint) {
318324
introducer);
319325
}
320326

321-
bool swift::shrinkBorrowScope(BeginBorrowInst *bbi,
322-
InstructionDeleter &deleter) {
323-
ShrinkBorrowScope borrowShrinker(bbi, deleter);
327+
bool swift::shrinkBorrowScope(
328+
BeginBorrowInst *bbi, InstructionDeleter &deleter,
329+
SmallVectorImpl<CopyValueInst *> &modifiedCopyValueInsts) {
330+
ShrinkBorrowScope borrowShrinker(bbi, deleter, modifiedCopyValueInsts);
324331
return borrowShrinker.run();
325332
}

0 commit comments

Comments
 (0)