Skip to content

Commit 83224b2

Browse files
committed
SIL: automatically set the reborrow flags for phi-arguments when creating an end_borrow or setting the operand of an end_borrow
This is done for all transitively incoming phi-arguments of an end_borrow operand.
1 parent e934bb1 commit 83224b2

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,10 @@ bool isNestedLexicalBeginBorrow(BeginBorrowInst *bbi);
14041404
/// then the move_value is redundant.
14051405
bool isRedundantMoveValue(MoveValueInst *mvi);
14061406

1407+
/// Sets the reborrow flags for all transitively incoming phi-arguments of
1408+
/// `forEndBorrowValue`, which is the operand value of an `end_borrow`.
1409+
void updateReborrowFlags(SILValue forEndBorrowValue);
1410+
14071411
} // namespace swift
14081412

14091413
#endif

include/swift/SIL/SILBuilder.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -941,14 +941,7 @@ class SILBuilder {
941941
return lowering.emitStore(*this, Loc, Src, DestAddr, Qualifier);
942942
}
943943

944-
EndBorrowInst *createEndBorrow(SILLocation loc, SILValue borrowedValue) {
945-
ASSERT(!SILArgument::isTerminatorResult(borrowedValue) &&
946-
"terminator results do not have end_borrow");
947-
ASSERT(!isa<SILFunctionArgument>(borrowedValue) &&
948-
"Function arguments should never have an end_borrow");
949-
return insert(new (getModule())
950-
EndBorrowInst(getSILDebugLocation(loc), borrowedValue));
951-
}
944+
EndBorrowInst *createEndBorrow(SILLocation loc, SILValue borrowedValue);
952945

953946
BeginAccessInst *createBeginAccess(SILLocation loc, SILValue address,
954947
SILAccessKind accessKind,

include/swift/SIL/SILValue.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,11 @@ class Operand {
10731073
removeFromCurrent();
10741074
TheValue = newValue;
10751075
insertIntoCurrent();
1076+
updateReborrowFlags();
10761077
verify();
10771078
}
10781079

1080+
void updateReborrowFlags();
10791081
void verify() const;
10801082

10811083
/// Swap the given operand with the current one.

lib/SIL/IR/SILBuilder.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/SIL/SILBuilder.h"
1414
#include "swift/AST/Expr.h"
1515
#include "swift/Basic/Assertions.h"
16+
#include "swift/SIL/OwnershipUtils.h"
1617
#include "swift/SIL/Projection.h"
1718
#include "swift/SIL/SILGlobalVariable.h"
1819

@@ -681,6 +682,17 @@ void SILBuilder::emitScopedBorrowOperation(SILLocation loc, SILValue original,
681682
createEndBorrow(loc, value);
682683
}
683684

685+
EndBorrowInst *SILBuilder::createEndBorrow(SILLocation loc, SILValue borrowedValue) {
686+
ASSERT(!SILArgument::isTerminatorResult(borrowedValue) &&
687+
"terminator results do not have end_borrow");
688+
ASSERT(!isa<SILFunctionArgument>(borrowedValue) &&
689+
"Function arguments should never have an end_borrow");
690+
updateReborrowFlags(borrowedValue);
691+
return insert(new (getModule())
692+
EndBorrowInst(getSILDebugLocation(loc), borrowedValue));
693+
}
694+
695+
684696
SILPhiArgument *SILBuilder::createSwitchOptional(
685697
SILLocation loc, SILValue operand,
686698
SILBasicBlock *someBB, SILBasicBlock *noneBB,

lib/SIL/IR/SILValue.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
441441
// Operand
442442
//===----------------------------------------------------------------------===//
443443

444+
void Operand::updateReborrowFlags() {
445+
if (isa<EndBorrowInst>(getUser())) {
446+
swift::updateReborrowFlags(get());
447+
}
448+
}
449+
444450
void Operand::verify() const {
445451
if (isa<BorrowedFromInst>(getUser()) && getOperandNumber() == 0) {
446452
assert(isa<SILArgument>(get()) || isa<SILUndef>(get()));

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,3 +2422,20 @@ bool swift::isRedundantMoveValue(MoveValueInst *mvi) {
24222422
auto moveHasEscape = findPointerEscape(mvi);
24232423
return moveHasEscape == originalHasEscape;
24242424
}
2425+
2426+
void swift::updateReborrowFlags(SILValue forEndBorrowValue) {
2427+
ValueWorklist worklist(forEndBorrowValue);
2428+
while (SILValue v = worklist.pop()) {
2429+
if (auto *bfi = dyn_cast<BorrowedFromInst>(v)) {
2430+
v = bfi->getBorrowedValue();
2431+
}
2432+
if (auto *arg = dyn_cast<SILPhiArgument>(v)) {
2433+
if (arg->isPhi() && !arg->isReborrow()) {
2434+
arg->setReborrow(true);
2435+
for (auto *predBlock : arg->getParent()->getPredecessorBlocks()) {
2436+
worklist.pushIfNotVisited(arg->getIncomingPhiValue(predBlock));
2437+
}
2438+
}
2439+
}
2440+
}
2441+
}

0 commit comments

Comments
 (0)