Skip to content

Commit 4b9a7c0

Browse files
committed
- remove the not needed transitive use visitors
1 parent cc5780e commit 4b9a7c0

File tree

8 files changed

+29
-190
lines changed

8 files changed

+29
-190
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/BorrowUtils.swift

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,15 @@ enum BeginBorrowValue {
366366

367367
/// The EndBorrows, reborrows (phis), and consumes (of closures)
368368
/// that end the local borrow scope. Empty if hasLocalScope is false.
369-
var scopeEndingOperands: LazyFilterSequence<TransitiveBorrowedFromUses> {
369+
var scopeEndingOperands: LazyFilterSequence<UseList> {
370370
switch self {
371371
case let .beginApply(value):
372372
return (value.definingInstruction
373-
as! BeginApplyInst).token.transitiveBorrowedFromUses.endingLifetime
373+
as! BeginApplyInst).token.uses.endingLifetime
374+
case let .reborrow(phi):
375+
return phi.value.lookThroughBorrowedFromUser.uses.endingLifetime
374376
default:
375-
return value.transitiveBorrowedFromUses.endingLifetime
377+
return value.uses.endingLifetime
376378
}
377379
}
378380
}
@@ -384,44 +386,6 @@ extension Value {
384386
}
385387
return self
386388
}
387-
388-
/// Get uses by looking transitively through forwarding (only operand 0) borrowed-from instructions.
389-
var transitiveBorrowedFromUses: TransitiveBorrowedFromUses {
390-
TransitiveBorrowedFromUses(of: self)
391-
}
392-
}
393-
394-
struct TransitiveBorrowedFromUses : CollectionLikeSequence, IteratorProtocol {
395-
private var currentUse: Operand?
396-
397-
public mutating func next() -> Operand? {
398-
if let use = currentUse {
399-
currentUse = Self.nextValidUse(use.nextUse, of: use.value)
400-
return use
401-
}
402-
return nil
403-
}
404-
405-
init(of value: Value) {
406-
precondition(!(value is BorrowedFromInst), "Can't get TransitiveBorrowedFromUses from \(value)")
407-
currentUse = Self.nextValidUse(value.uses.first, of: value)
408-
}
409-
410-
private static func nextValidUse(_ use: Operand?, of value: Value) -> Operand? {
411-
if let use = use {
412-
// Walk down and look through a borrowed-from user.
413-
if let bfiUser = use.forwardingBorrowedFromUser {
414-
return nextValidUse(bfiUser.uses.first, of: bfiUser)
415-
}
416-
return use
417-
}
418-
// No more uses in the current use-list: Walk up and see if any borrowed-from further up in the def-use
419-
// chain contains more uses.
420-
if let bfiDef = value as? BorrowedFromInst {
421-
return nextValidUse(bfiDef.operands[0].nextUse, of: bfiDef.operands[0].value)
422-
}
423-
return nil
424-
}
425389
}
426390

427391
/// Find the borrow introducers for `value`. This gives you a set of
@@ -603,7 +567,7 @@ let borrowIntroducersTest = FunctionTest("borrow_introducers") {
603567
defer {
604568
introducers.deinitialize()
605569
}
606-
gatherBorrowIntroducers(for: lookThroughBorrowedFromUser(of: value), in: &introducers, context)
570+
gatherBorrowIntroducers(for: value.lookThroughBorrowedFromUser, in: &introducers, context)
607571
introducers.forEach { print($0) }
608572
}
609573

@@ -616,25 +580,18 @@ let enclosingValuesTest = FunctionTest("enclosing_values") {
616580
defer {
617581
enclosing.deinitialize()
618582
}
619-
gatherEnclosingValues(for: lookThroughBorrowedFromUser(of: value), in: &enclosing, context)
583+
gatherEnclosingValues(for: value.lookThroughBorrowedFromUser, in: &enclosing, context)
620584
enclosing.forEach { print($0) }
621585
}
622586

623-
let transitiveBorrowedFromUsesTest = FunctionTest("transitive_borrowed_from_uses") {
624-
function, arguments, context in
625-
let value = arguments.takeValue()
626-
print("Transitive borrowed uses for: \(value)")
627-
for use in value.transitiveBorrowedFromUses.map({"\($0)"}).sorted() {
628-
print(use)
629-
}
630-
}
631-
632-
private func lookThroughBorrowedFromUser(of value: Value) -> Value {
633-
for use in value.uses {
634-
if let bfi = use.forwardingBorrowedFromUser {
635-
return bfi
587+
extension Value {
588+
var lookThroughBorrowedFromUser: Value {
589+
for use in uses {
590+
if let bfi = use.forwardingBorrowedFromUser {
591+
return bfi
592+
}
636593
}
594+
return self
637595
}
638-
return value
639596
}
640597

SwiftCompilerSources/Sources/Optimizer/Utilities/OwnershipLiveness.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func computeLinearLiveness(for definingValue: Value, _ context: Context)
5656
var range = InstructionRange(for: definingValue, context)
5757

5858
// Compute liveness.
59-
definingValue.transitiveBorrowedFromUses.endingLifetime.forEach {
59+
definingValue.lookThroughBorrowedFromUser.uses.endingLifetime.forEach {
6060
range.insert($0.instruction)
6161
}
6262
return range
@@ -664,7 +664,7 @@ extension InteriorUseWalker: AddressUseVisitor {
664664
if handleInner(borrowed: load) == .abortWalk {
665665
return .abortWalk
666666
}
667-
return load.transitiveBorrowedFromUses.endingLifetime.walk {
667+
return load.uses.endingLifetime.walk {
668668
useVisitor($0)
669669
}
670670
default:

SwiftCompilerSources/Sources/Optimizer/Utilities/Test.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ public func registerOptimizerTests() {
141141
forwardingDefUseTest,
142142
borrowIntroducersTest,
143143
enclosingValuesTest,
144-
transitiveBorrowedFromUsesTest,
145144
linearLivenessTest,
146145
interiorLivenessTest,
147146
variableIntroducerTest,

include/swift/SIL/OwnershipUtils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ inline bool isForwardingConsume(SILValue value) {
9191
//===----------------------------------------------------------------------===//
9292

9393
BorrowedFromInst *getBorrowedFromUser(SILValue v);
94-
SILValue lookThroughSingleBorrowedFromUser(SILValue v);
94+
SILValue lookThroughBorrowedFromUser(SILValue v);
9595
SILValue lookThroughBorrowedFromDef(SILValue v);
96-
void getTransitiveConsumingUses(SILValue v, llvm::SmallVectorImpl<Operand *> &results);
9796

9897
/// Whether the specified OSSA-lifetime introducer has a pointer escape.
9998
///

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ BorrowedFromInst *swift::getBorrowedFromUser(SILValue v) {
214214
return nullptr;
215215
}
216216

217-
SILValue swift::lookThroughSingleBorrowedFromUser(SILValue v) {
217+
SILValue swift::lookThroughBorrowedFromUser(SILValue v) {
218218
if (BorrowedFromInst *bfi = getBorrowedFromUser(v))
219219
return bfi;
220220
return v;
@@ -227,18 +227,6 @@ SILValue swift::lookThroughBorrowedFromDef(SILValue v) {
227227
return v;
228228
}
229229

230-
void swift::getTransitiveConsumingUses(SILValue v, llvm::SmallVectorImpl<Operand *> &results) {
231-
for (Operand *use : v->getUses()) {
232-
if (use->isLifetimeEnding()) {
233-
results.push_back(use);
234-
} else if (auto *bfi = dyn_cast<BorrowedFromInst>(use->getUser())) {
235-
if (use->getOperandNumber() == 0) {
236-
getTransitiveConsumingUses(bfi, results);
237-
}
238-
}
239-
}
240-
}
241-
242230
//===----------------------------------------------------------------------===//
243231
// Guaranteed Use-Point (Lifetime) Discovery
244232
//===----------------------------------------------------------------------===//
@@ -943,18 +931,17 @@ bool BorrowedValue::visitLocalScopeEndingUses(
943931
llvm_unreachable("Using invalid case?!");
944932
case BorrowedValueKind::SILFunctionArgument:
945933
llvm_unreachable("Should only call this with a local scope");
946-
case BorrowedValueKind::Phi:
947934
case BorrowedValueKind::LoadBorrow:
948-
case BorrowedValueKind::BeginBorrow: {
949-
llvm::SmallVector<Operand *, 16> consumingUses;
950-
getTransitiveConsumingUses(v, consumingUses);
951-
for (Operand *use : consumingUses) {
952-
if (!visitor(use))
953-
return false;
935+
case BorrowedValueKind::BeginBorrow:
936+
case BorrowedValueKind::Phi:
937+
for (auto *use : lookThroughBorrowedFromUser(value)->getUses()) {
938+
if (use->isLifetimeEnding()) {
939+
if (!visitor(use))
940+
return false;
941+
}
954942
}
955943
return true;
956944
}
957-
}
958945
llvm_unreachable("Covered switch isn't covered?!");
959946
}
960947

@@ -2298,9 +2285,7 @@ void swift::visitTransitiveEndBorrows(
22982285

22992286
while (!worklist.empty()) {
23002287
auto val = worklist.pop();
2301-
llvm::SmallVector<Operand *, 16> consumingUses;
2302-
getTransitiveConsumingUses(val, consumingUses);
2303-
for (auto *consumingUse : consumingUses) {
2288+
for (auto *consumingUse : lookThroughBorrowedFromUser(val)->getConsumingUses()) {
23042289
auto *consumingUser = consumingUse->getUser();
23052290
if (auto *branch = dyn_cast<BranchInst>(consumingUser)) {
23062291
auto *succBlock = branch->getSingleSuccessorBlock();

lib/SIL/Verifier/GuaranteedPhiVerifier.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ bool GuaranteedPhiVerifier::verifyDependentPhiLifetime(SILPhiArgument *phi,
2828
dependentPhiToBaseValueMap[phi].insert(baseValue);
2929

3030
// Now, verify whether phi's lifetime is within baseValue's lifetime
31-
SmallVector<Operand *, 4> baseValConsumingUses;
32-
getTransitiveConsumingUses(baseValue, baseValConsumingUses);
31+
SmallVector<Operand *, 4> baseValConsumingUses(lookThroughBorrowedFromUser(baseValue)->getConsumingUses());
3332
// If the baseValue has no consuming uses, there is nothing more to verify
3433
if (baseValConsumingUses.empty())
3534
return false;
3635

37-
SmallVector<Operand *, 4> phiUses;
38-
getTransitiveConsumingUses(phi, phiUses);
36+
SmallVector<Operand *, 4> phiUses(lookThroughBorrowedFromUser(phi)->getUses());
3937
LinearLifetimeChecker checker(deadEndBlocks);
4038
// newErrorBuilder is consumed at the end of the checkValue function.
4139
// Copy initial state from errorBuilder everytime

lib/SILOptimizer/Transforms/ConditionForwarding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ bool ConditionForwarding::tryOptimize(SwitchEnumInst *SEI) {
164164
if (!Arg)
165165
return false;
166166

167-
SILValue argValue = lookThroughSingleBorrowedFromUser(Arg);
167+
SILValue argValue = lookThroughBorrowedFromUser(Arg);
168168

169169
// The switch_enum must be the only use of the Enum, except it may be used in
170170
// SEI's successors.

test/SILOptimizer/transitive_borrowed_from_uses_unit.sil

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)