Skip to content

Commit e84bcf5

Browse files
committed
Add computeBorrowLiveRange
Compute the live range for the borrow scopes of a guaranteed value. This returns a separate instruction range for each of the value's borrow introducers. Unioning those ranges would be incorrect. We typically want their intersection.
1 parent e40fd35 commit e84bcf5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/BorrowUtils.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,28 @@ func gatherBorrowIntroducers(for value: Value,
404404
&cache, context)
405405
}
406406

407+
/// Compute the live range for the borrow scopes of a guaranteed value. This returns a separate instruction range for
408+
/// each of the value's borrow introducers. Unioning those ranges would be incorrect. We typically want their
409+
/// intersection.
410+
func computeBorrowLiveRange(for value: Value, _ context: FunctionPassContext)
411+
-> SingleInlineArray<(BeginBorrowValue, InstructionRange)> {
412+
assert(value.ownership == .guaranteed)
413+
414+
var ranges = SingleInlineArray<(BeginBorrowValue, InstructionRange)>()
415+
var introducers = Stack<BeginBorrowValue>(context)
416+
defer { introducers.deinitialize() }
417+
gatherBorrowIntroducers(for: value, in: &introducers, context)
418+
// If introducers is empty, then the dependence is on a trivial value, so
419+
// there is no ownership range.
420+
while let beginBorrow = introducers.pop() {
421+
/// FIXME: Remove calls to computeInteriorLiveness as soon as lifetime completion runs immediately after
422+
/// SILGen. Instead, this should compute linear liveness for borrowed value by switching over BeginBorrowValue, just
423+
/// like LifetimeDependenc.Scope.computeRange().
424+
ranges.push((beginBorrow, computeInteriorLiveness(for: beginBorrow.value, context)))
425+
}
426+
return ranges
427+
}
428+
407429
private struct BorrowIntroducers {
408430
typealias CachedIntroducers = SingleInlineArray<BeginBorrowValue>
409431
struct Cache {

0 commit comments

Comments
 (0)