Skip to content

Commit 083ab98

Browse files
committed
Simplify OwnershipLiveness.swift, remove reborrowingUse.
Remove complexity around reborrows. They no longer need special treatment.
1 parent fc9a20f commit 083ab98

File tree

2 files changed

+16
-56
lines changed

2 files changed

+16
-56
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/BorrowUtils.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ import SIL
142142
///
143143
/// Note: This must handle all instructions with a .borrow operand ownership.
144144
///
145-
/// Note: borrowed_from is a BorrowingInstruction because it creates a borrow scope for its base operand. Its result,
146-
/// however, is only a BeginBorrowValue (.reborrow) if it forwards a reborrow phi. Otherwise, it simply forwards a
147-
/// guaranteed value and does not introduce a separate borrow scope.
145+
/// Note: borrowed_from is a BorrowingInstruction because it creates a borrow scope for its enclosing operands. Its
146+
/// result, however, is only a BeginBorrowValue (.reborrow) if it forwards a reborrow phi. Otherwise, it simply forwards
147+
/// a guaranteed value and does not introduce a separate borrow scope.
148148
///
149149
/// Note: mark_dependence [nonescaping] is a BorrowingInstruction because it creates a borrow scope for its base
150150
/// operand. Its result, however, is not a BeginBorrowValue. Instead it is a ForwardingInstruction relative to its value

SwiftCompilerSources/Sources/Optimizer/Utilities/OwnershipLiveness.swift

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ struct InteriorLivenessResult: CustomDebugStringConvertible {
200200
/// entry points. Additionally, the implementation may recurse into inner
201201
/// borrow scopes, skipping over the uses within inner scopes using:
202202
/// - `visitInnerBorrowUses(of: BorrowingInstruction, operand:)`
203-
/// - `visitInnerScopeUses(of: Value)`
203+
/// - `visitDependentUses(of: Value)`
204204
///
205205
/// Visitors implement:
206206
///
@@ -210,12 +210,9 @@ struct InteriorLivenessResult: CustomDebugStringConvertible {
210210
/// - pointerEscapingUse(of:)
211211
/// - dependentUse(of:into:)
212212
/// - borrowingUse(of:by:)
213-
/// - reborrowingUse(of:isInnerlifetime:)
214213
///
215-
/// This only visits the first level of uses. The implementation may
216-
/// transitively visit forwarding operations in its implementation of
217-
/// `forwardingUse(of:isInnerlifetime:)` and
218-
/// `reborrowingUse(of:isInnerlifetime:)`.
214+
/// This only visits the first level of uses. The implementation may transitively visit forwarded, borrowed, dependent,
215+
/// or address values in the overrides listed above.
219216
///
220217
/// `isInnerlifetime` indicates whether the value being used is
221218
/// defined by the "outer" OSSA lifetime or an inner borrow scope.
@@ -275,16 +272,7 @@ protocol OwnershipUseVisitor {
275272
mutating func dependentUse(of operand: Operand, into value: Value) -> WalkResult
276273

277274
/// A use that is scoped to an inner borrow scope.
278-
///
279-
/// Call `visitInnerScopeUses(of:)` to recursively classify any
280-
/// scope-ending uses and forwarded dependent values.
281275
mutating func borrowingUse(of operand: Operand, by borrowInst: BorrowingInstruction) -> WalkResult
282-
283-
/// A reborrow operand.
284-
///
285-
/// Call `visitInnerBorrowUses(of:)` or `visitInnerScopeUses(of:)` to recursively classify scope-ending uses (such as
286-
/// reborrow and end_borrow).
287-
mutating func reborrowingUse(of operand: Operand, isInnerLifetime: Bool) -> WalkResult
288276
}
289277

290278
extension OwnershipUseVisitor {
@@ -321,31 +309,17 @@ extension OwnershipUseVisitor {
321309
}
322310
}
323311

324-
/// Visit only those uses of a value within an inner borrow scope that may affect the outer lifetime. An inner borrow
325-
/// scope is one in which the borrowing operand is itself a use of the outer lifetime, including: begin_borrow,
326-
/// reborrow, borrowed_from, partial_apply, mark_dependence, or an inner adjacent phi (where original SSA def is a phi
327-
/// in the same block).
312+
/// Handle an owned dependent value, such as a closure capture or owned mark_dependence.
328313
///
329-
/// An owned lifetime may also be considered an inner borrow scope if it depends on a borrowed operand, such as a
330-
/// closure capture or owned mark_dependence.
314+
/// When a borrow introduces an owned value, each OSSA lifetime is effectively a separate borrow scope. A destroy or
315+
/// consumes ends that borrow scope, while a forwarding consume effectively "reborrows".
331316
///
332-
/// Precondition: BeginBorrowValue(value) != nil || value.ownership == .owned
333-
mutating func visitInnerScopeUses(of value: Value) -> WalkResult {
334-
if let beginBorrow = BeginBorrowValue(value) {
335-
return beginBorrow.scopeEndingOperands.walk {
336-
switch $0.ownership {
337-
case .endBorrow:
338-
return ownershipLeafUse(of: $0, isInnerLifetime: true)
339-
case .reborrow:
340-
return reborrowingUse(of: $0, isInnerLifetime: true)
341-
default:
342-
fatalError("invalid borrow scope ending operand ownership")
343-
}
344-
}
345-
}
346-
// When a borrow introduces an owned value, each OSSA lifetime is effectively a separate borrow scope. A destroy
347-
// ends the borrow scope, while a forwarding consume effectively "reborrows".
317+
/// Preconditions:
318+
/// - value.ownership == .owned
319+
/// - value.type.isEscapable
320+
mutating func visitDependentUses(of value: Value) -> WalkResult {
348321
assert(value.ownership == .owned, "inner value must be a reborrow or owned forward")
322+
assert(value.type.isEscapable(in: value.parentFunction), "cannot handle non-escapable dependent values")
349323
return value.uses.endingLifetime.walk {
350324
switch $0.ownership {
351325
case .forwardingConsume:
@@ -361,10 +335,6 @@ extension OwnershipUseVisitor {
361335
// Visit uses of borrowing instruction (operandOwnerhip == .borrow),
362336
// skipping uses within the borrow scope.
363337
mutating func visitInnerBorrowUses(of borrowInst: BorrowingInstruction, operand: Operand) -> WalkResult {
364-
// Delegate begin_borrow to visitInnerScopeUses, because it dispatches to reborrowingUse.
365-
if let beginBorrow = BeginBorrowValue(resultOf: borrowInst) {
366-
return visitInnerScopeUses(of: beginBorrow.value)
367-
}
368338
if let dependent = borrowInst.dependentValue {
369339
if dependent.ownership == .guaranteed {
370340
return visitAllUses(of: dependent)
@@ -433,12 +403,9 @@ extension OwnershipUseVisitor {
433403
}
434404
return pointerEscapingUse(of: operand)
435405

436-
case .instantaneousUse, .forwardingUnowned, .unownedInstantaneousUse, .bitwiseEscape, .endBorrow:
406+
case .instantaneousUse, .forwardingUnowned, .unownedInstantaneousUse, .bitwiseEscape, .endBorrow, .reborrow:
437407
return ownershipLeafUse(of: operand, isInnerLifetime: false)
438408

439-
case .reborrow:
440-
return reborrowingUse(of: operand, isInnerLifetime: false)
441-
442409
case .guaranteedForwarding:
443410
return forwardingUse(of: operand, isInnerLifetime: false)
444411

@@ -647,13 +614,6 @@ extension InteriorUseWalker: OwnershipUseVisitor {
647614
}
648615
return visitInnerBorrowUses(of: borrowInst, operand: operand)
649616
}
650-
651-
// Visit a reborrow operand. This ends an outer lifetime and extends
652-
// an inner lifetime.
653-
mutating func reborrowingUse(of operand: Operand, isInnerLifetime: Bool)
654-
-> WalkResult {
655-
return isInnerLifetime ? walkDown(operand: operand) : useVisitor(operand)
656-
}
657617
}
658618

659619
extension InteriorUseWalker: AddressUseVisitor {
@@ -783,7 +743,7 @@ extension InteriorUseWalker {
783743
if handleInner(borrowed: value) == .abortWalk {
784744
return .abortWalk
785745
}
786-
return visitInnerScopeUses(of: value)
746+
return visitDependentUses(of: value)
787747
case .guaranteed:
788748
return visitAllUses(of: value)
789749
default:

0 commit comments

Comments
 (0)