Skip to content

Commit 6e75813

Browse files
committed
OwnershipLiveness fix: support visitInnerUses
DestroyHoisting is using this utility. This requires complete liveness. But we can't rely on complete liveness because complete lifetimes is not enabled. Instead, we use a visitInnerUses flag to try to ignore borrow scopes. That flag was never properly implemented. Make an attempt here.
1 parent 2542515 commit 6e75813

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OwnershipLiveness.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ struct InteriorLivenessResult: CustomDebugStringConvertible {
194194
///
195195
/// The top-level entry points are:
196196
/// - `classify(operand:)`
197-
/// - `visitAllUses(of:)`
197+
/// - `visitOwnershipUses(of:)`
198198
///
199199
/// The implementation may recursively call back to the top-level
200200
/// entry points. Additionally, the implementation may recurse into inner
@@ -217,7 +217,7 @@ struct InteriorLivenessResult: CustomDebugStringConvertible {
217217
/// `isInnerlifetime` indicates whether the value being used is
218218
/// defined by the "outer" OSSA lifetime or an inner borrow scope.
219219
/// When the OwnershipUseVisitor is invoked on an outer value
220-
/// (visitAllUses(of:)), it visits all the uses of that value
220+
/// (visitOwnershipUses(of:)), it visits all the uses of that value
221221
/// and also visits the lifetime-ending uses of any inner borrow
222222
/// scopes. This provides a complete set of liveness "use points":
223223
///
@@ -297,7 +297,7 @@ extension OwnershipUseVisitor {
297297
/// adjacent phis and treat them like inner borrows.
298298
///
299299
/// This is only called for uses in the outer lifetime.
300-
mutating func visitAllUses(of value: Value) -> WalkResult {
300+
mutating func visitOwnershipUses(of value: Value) -> WalkResult {
301301
switch value.ownership {
302302
case .owned:
303303
return value.uses.ignoreTypeDependence.walk { classifyOwned(operand: $0) }
@@ -349,7 +349,7 @@ extension OwnershipUseVisitor {
349349
mutating func visitInnerBorrowUses(of borrowInst: BorrowingInstruction, operand: Operand) -> WalkResult {
350350
if let dependent = borrowInst.dependentValue {
351351
if dependent.ownership == .guaranteed {
352-
return visitAllUses(of: dependent)
352+
return visitOwnershipUses(of: dependent)
353353
}
354354
return pointerEscapingUse(of: operand)
355355
}
@@ -544,7 +544,7 @@ struct InteriorUseWalker {
544544
}
545545

546546
mutating func visitUses() -> WalkResult {
547-
return visitAllUses(of: definingValue)
547+
return visitOwnershipUses(of: definingValue)
548548
}
549549
}
550550

@@ -624,13 +624,21 @@ extension InteriorUseWalker: OwnershipUseVisitor {
624624
if useVisitor(operand) == .abortWalk {
625625
return .abortWalk
626626
}
627-
if visitInnerUses {
628-
guard let innerValue = borrowInst.innerValue else {
629-
return setPointerEscape(of: operand)
630-
}
631-
return visitAllUses(of: innerValue)
627+
if visitInnerBorrowUses(of: borrowInst, operand: operand) == .abortWalk {
628+
return .abortWalk
629+
}
630+
if !visitInnerUses {
631+
return .continueWalk
632+
}
633+
guard let innerValue = borrowInst.innerValue else {
634+
return setPointerEscape(of: operand)
635+
}
636+
// Call visitInnerBorrowUses before visitOwnershipUses because it will visit uses of tokens, such as
637+
// the begin_apply token, which don't have ownership.
638+
if innerValue.type.isAddress {
639+
return interiorPointerUse(of: operand, into: innerValue)
632640
}
633-
return visitInnerBorrowUses(of: borrowInst, operand: operand)
641+
return visitOwnershipUses(of: innerValue)
634642
}
635643
}
636644

@@ -764,7 +772,7 @@ extension InteriorUseWalker {
764772
return visitOwnedDependentUses(of: value)
765773
case .guaranteed:
766774
// Handle a forwarded guaranteed value exactly like the outer borrow.
767-
return visitAllUses(of: value)
775+
return visitOwnershipUses(of: value)
768776
default:
769777
fatalError("ownership requires a lifetime")
770778
}

0 commit comments

Comments
 (0)