Skip to content

Lifetime dependence utilities #71055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions SwiftCompilerSources/Sources/Optimizer/Utilities/AddressUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//===--- AddressUtils.swift - Utilities for handling SIL addresses -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SIL

/// Classify address uses. This can be used by def-use walkers to
/// ensure complete handling of all legal SIL patterns.
///
/// TODO: Integrate this with SIL verification to ensure completeness.
///
/// TODO: Convert AddressDefUseWalker to conform to AddressUtils after
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would that work? The problem is that AddressDefUseWalker is passing a Path down the walk functions.

/// checking that the additional instructions are handled correctly by
/// escape analysis.
///
/// TODO: Verify that pointerEscape is only called for live ranges in which
/// `findPointerEscape()` returns true.
protocol AddressUseVisitor {
var context: Context { get }

/// An address projection produces a single address result and does not
/// escape its address operand in any other way.
mutating func projectedAddressUse(of operand: Operand, into value: Value)
-> WalkResult

/// An access scope: begin_access, begin_apply, load_borrow.
mutating func scopedAddressUse(of operand: Operand) -> WalkResult

/// A address leaf use cannot propagate the address bits beyond the
/// instruction.
///
/// An apply or builtin propagates an address into the callee, but
/// it is considered a leaf use as long as the argument does not escape.
mutating func leafAddressUse(of operand: Operand) -> WalkResult

/// A loaded address use propagates the value at the address.
mutating func loadedAddressUse(of operand: Operand, into value: Value)
-> WalkResult

/// A loaded address use propagates the value at the address to the
/// destination address operand.
mutating func loadedAddressUse(of operand: Operand, into address: Operand)
-> WalkResult

/// A non-address owned `value` whose ownership depends on the in-memory
/// value at `address`, such as `mark_dependence %value on %address`.
mutating func dependentAddressUse(of operand: Operand, into value: Value)
-> WalkResult

/// A pointer escape may propagate the address beyond the current instruction.
mutating func escapingAddressUse(of operand: Operand) -> WalkResult

/// A unknown address use. This should never be called in valid SIL.
mutating func unknownAddressUse(of operand: Operand) -> WalkResult
}

extension AddressUseVisitor {
/// Classify an address-type operand, dispatching to one of the
/// protocol methods above.
mutating func classifyAddress(operand: Operand) -> WalkResult {
switch operand.instruction {
case is BeginAccessInst, is BeginApplyInst, is LoadBorrowInst,
is StoreBorrowInst:
return scopedAddressUse(of: operand)

case let markDep as MarkDependenceInst:
if markDep.valueOperand == operand {
return projectedAddressUse(of: operand, into: markDep)
}
assert(markDep.baseOperand == operand)
// If another address depends on the current address,
// handle it like a projection.
if markDep.type.isAddress {
return projectedAddressUse(of: operand, into: markDep)
}
if LifetimeDependence(markDependence: markDep, context) != nil {
// This is unreachable from InteriorUseVisitor because the
// base address of a `mark_dependence [nonescaping]` must be a
// `begin_access`, and interior liveness does not check uses of
// the accessed address.
return dependentAddressUse(of: operand, into: markDep)
}
// A potentially escaping value depends on this address.
return escapingAddressUse(of: operand)

case let pai as PartialApplyInst where pai.isOnStack:
return dependentAddressUse(of: operand, into: pai)

case let pai as PartialApplyInst where !pai.isOnStack:
return escapingAddressUse(of: operand)

case is AddressToPointerInst:
return escapingAddressUse(of: operand)

case is StructElementAddrInst, is TupleElementAddrInst,
is IndexAddrInst, is TailAddrInst, is TuplePackElementAddrInst,
is InitEnumDataAddrInst, is UncheckedTakeEnumDataAddrInst,
is InitExistentialAddrInst, is OpenExistentialAddrInst,
is ProjectBlockStorageInst, is UncheckedAddrCastInst,
is UnconditionalCheckedCastAddrInst,
is MarkUninitializedInst, is DropDeinitInst,
is CopyableToMoveOnlyWrapperAddrInst,
is MoveOnlyWrapperToCopyableAddrInst,
is MarkUnresolvedNonCopyableValueInst:
let svi = operand.instruction as! SingleValueInstruction
return projectedAddressUse(of: operand, into: svi)

case is ReturnInst, is ThrowInst, is YieldInst, is TryApplyInst,
is SwitchEnumAddrInst, is CheckedCastAddrBranchInst,
is SelectEnumAddrInst, is InjectEnumAddrInst,
is StoreInst, is StoreUnownedInst, is StoreWeakInst,
is AssignInst, is AssignByWrapperInst, is AssignOrInitInst,
is TupleAddrConstructorInst, is InitBlockStorageHeaderInst,
is RetainValueAddrInst, is ReleaseValueAddrInst,
is DestroyAddrInst, is DeallocStackInst,
is DeinitExistentialAddrInst,
is EndApplyInst, is IsUniqueInst, is MarkFunctionEscapeInst,
is PackElementSetInst:
return leafAddressUse(of: operand)

case is LoadInst, is LoadUnownedInst, is LoadWeakInst,
is ValueMetatypeInst, is ExistentialMetatypeInst,
is PackElementGetInst:
let svi = operand.instruction as! SingleValueInstruction
return loadedAddressUse(of: operand, into: svi)

case let sdai as SourceDestAddrInstruction
where sdai.sourceOperand == operand:
return loadedAddressUse(of: operand, into: sdai.destinationOperand)

case let sdai as SourceDestAddrInstruction
where sdai.destinationOperand == operand:
return leafAddressUse(of: operand)

case let builtin as BuiltinInst:
switch builtin.id {
case .Copy where builtin.operands[1] == operand: // source
return loadedAddressUse(of: operand, into: builtin.operands[0])

case .Copy where builtin.operands[0] == operand: // dest
return leafAddressUse(of: operand)

// Builtins that cannot load a nontrivial value.
case .TSanInoutAccess, .ResumeThrowingContinuationReturning,
.ResumeNonThrowingContinuationReturning, .GenericAdd,
.GenericFAdd, .GenericAnd, .GenericAShr, .GenericLShr, .GenericOr,
.GenericFDiv, .GenericMul, .GenericFMul, .GenericSDiv,
.GenericExactSDiv, .GenericShl, .GenericSRem, .GenericSub,
.GenericFSub, .GenericUDiv, .GenericExactUDiv, .GenericURem,
.GenericFRem, .GenericXor, .TaskRunInline, .ZeroInitializer,
.GetEnumTag, .InjectEnumTag:
return leafAddressUse(of: operand)
default:
// TODO: SIL verification should check that this exhaustively
// recognizes all builtin address uses.
return .abortWalk
}

case is BranchInst, is CondBranchInst:
fatalError("address phi is not allowed")

default:
if operand.instruction.isIncidentalUse {
return leafAddressUse(of: operand)
}
// Unkown instruction.
return unknownAddressUse(of: operand)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ import SIL
///
/// Note: This must handle all instructions with a .borrow operand ownership.
///
/// Note: mark_dependence is a BorrowingInstruction because it creates
/// a borrow scope for its base operand. Its result, however, is not a
/// BeginBorrowValue. It is instead a ForwardingInstruction relative
/// to its value operand.
///
/// TODO: replace BorrowIntroducingInstruction
///
/// TODO: Add non-escaping MarkDependence.
Expand All @@ -153,15 +158,21 @@ enum BorrowingInstruction : CustomStringConvertible, Hashable {
case storeBorrow(StoreBorrowInst)
case beginApply(BeginApplyInst)
case partialApply(PartialApplyInst)
case markDependence(MarkDependenceInst)
case startAsyncLet(BuiltinInst)

init?(_ inst: Instruction) {
switch inst {
case let bbi as BeginBorrowInst: self = .beginBorrow(bbi)
case let sbi as StoreBorrowInst: self = .storeBorrow(sbi)
case let bai as BeginApplyInst: self = .beginApply(bai)
case let bbi as BeginBorrowInst:
self = .beginBorrow(bbi)
case let sbi as StoreBorrowInst:
self = .storeBorrow(sbi)
case let bai as BeginApplyInst:
self = .beginApply(bai)
case let pai as PartialApplyInst where pai.isOnStack:
self = .partialApply(pai)
case let mdi as MarkDependenceInst:
self = .markDependence(mdi)
case let bi as BuiltinInst
where bi.id == .StartAsyncLetWithLocalBuffer:
self = .startAsyncLet(bi)
Expand All @@ -172,11 +183,18 @@ enum BorrowingInstruction : CustomStringConvertible, Hashable {

var instruction: Instruction {
switch self {
case .beginBorrow(let bbi): return bbi
case .storeBorrow(let sbi): return sbi
case .beginApply(let bai): return bai
case .partialApply(let pai): return pai
case .startAsyncLet(let bi): return bi
case .beginBorrow(let bbi):
return bbi
case .storeBorrow(let sbi):
return sbi
case .beginApply(let bai):
return bai
case .partialApply(let pai):
return pai
case .markDependence(let mdi):
return mdi
case .startAsyncLet(let bi):
return bi
}
}

Expand All @@ -188,9 +206,17 @@ enum BorrowingInstruction : CustomStringConvertible, Hashable {
/// incoming value dominates or is consumed by an outer adjacent
/// phi. See InteriorLiveness.
///
/// TODO: to hande reborrow-extended uses migrate ExtendedLiveness
/// TODO: to hande reborrow-extended uses, migrate ExtendedLiveness
/// to SwiftCompilerSources.
///
/// TODO: Handle .partialApply and .markDependence forwarded uses
/// that are phi operands. Currently, partial_apply [on_stack]
/// and mark_dependence [nonescaping] cannot be cloned, so walking
/// through the phi safely returns dominated scope-ending operands.
/// Instead, this could report the phi as a scope-ending use, and
/// the client could decide whether to walk through them or to
/// construct reborrow-extended liveness.
///
/// TODO: For instructions that are not a BeginBorrowValue, verify
/// that scope ending instructions exist on all paths. These
/// instructions should be complete after SILGen and never cloned to
Expand All @@ -206,8 +232,10 @@ enum BorrowingInstruction : CustomStringConvertible, Hashable {
}
case .beginApply(let bai):
return bai.token.uses.walk { return visitor($0) }
case .partialApply(let pai):
return visitForwardedUses(introducer: pai, context) {
case .partialApply, .markDependence:
let svi = instruction as! SingleValueInstruction
assert(svi.ownership == .owned)
return visitForwardedUses(introducer: svi, context) {
switch $0 {
case let .operand(operand):
if operand.endsLifetime {
Expand Down Expand Up @@ -306,7 +334,7 @@ enum BeginBorrowValue {
self = BeginBorrowValue(beginBorrow)!
case let .beginApply(beginApply):
self = BeginBorrowValue(beginApply.token)!
case .storeBorrow, .partialApply, .startAsyncLet:
case .storeBorrow, .partialApply, .markDependence, .startAsyncLet:
return nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

swift_compiler_sources(Optimizer
AddressUtils.swift
BorrowUtils.swift
DiagnosticEngine.swift
Devirtualization.swift
EscapeUtils.swift
ForwardingUtils.swift
LifetimeDependenceUtils.swift
OptUtils.swift
OwnershipLiveness.swift
SSAUpdater.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,32 +197,39 @@ enum ForwardingUseResult: CustomStringConvertible {
}
}

/// Visit all the uses in a forward-extended lifetime
/// Visit all the non-forwarding uses in a forward-extended lifetime
/// (LifetimeIntroducer -> Operand).
///
/// Minimal requirements:
/// needWalk(for value: Value) -> Bool
/// leafUse(_ operand: Operand) -> WalkResult
/// nonForwardingUse(_ operand: Operand) -> WalkResult
/// deadValue(_ value: Value, using operand: Operand?) -> WalkResult
///
/// Start walking:
/// walkDown(root: Value)
///
protocol ForwardingDefUseWalker {
// Minimally, check a ValueSet. This walker may traverse chains of
// aggregation and destructuring by default. Implementations may
// handle phis.
/// Minimally, check a ValueSet. This walker may traverse chains of
/// aggregation and destructuring by default. Implementations may
/// handle phis.
mutating func needWalk(for value: Value) -> Bool

mutating func leafUse(_ operand: Operand) -> WalkResult
/// A nonForwarding use does not forward ownership, but may
/// propagate the lifetime in other ways, such as an interior
/// pointer.
mutating func nonForwardingUse(_ operand: Operand) -> WalkResult

// Report any initial or forwarded value with no uses. Only relevant for
// guaranteed values or incomplete OSSA. This could be a dead
// instruction, a terminator in which the result is dead on one
// path, or a dead phi.
//
// \p operand is nil if \p value is the root.
/// Report any initial or forwarded value with no uses. Only relevant for
/// guaranteed values or incomplete OSSA. This could be a dead
/// instruction, a terminator in which the result is dead on one
/// path, or a dead phi.
///
/// \p operand is nil if \p value is the root.
mutating func deadValue(_ value: Value, using operand: Operand?) -> WalkResult

/// This is called for every forwarded value. If the root was an
/// owned value, then this identifies all OSSA lifetimes in the
/// forward-extendd lifetime.
mutating func walkDownUses(of: Value, using: Operand?) -> WalkResult

mutating func walkDown(operand: Operand) -> WalkResult
Expand All @@ -235,7 +242,7 @@ extension ForwardingDefUseWalker {
}

mutating func walkDownUses(of value: Value, using operand: Operand?)
-> WalkResult {
-> WalkResult {
return walkDownUsesDefault(forwarding: value, using: operand)
}

Expand All @@ -245,8 +252,8 @@ extension ForwardingDefUseWalker {
if !needWalk(for: value) { return .continueWalk }

var hasUse = false
for operand in value.uses where !operand.isTypeDependent {
if walkDown(operand: operand) == .abortWalk {
for use in value.uses where !use.isTypeDependent {
if walkDown(operand: use) == .abortWalk {
return .abortWalk
}
hasUse = true
Expand All @@ -263,12 +270,17 @@ extension ForwardingDefUseWalker {

mutating func walkDownDefault(forwarding operand: Operand) -> WalkResult {
if let inst = operand.instruction as? ForwardingInstruction {
return inst.forwardedResults.walk { walkDownUses(of: $0, using: operand) }
let singleOper = inst.singleForwardedOperand
if singleOper == nil || singleOper! == operand {
return inst.forwardedResults.walk {
walkDownUses(of: $0, using: operand)
}
}
}
if let phi = Phi(using: operand) {
return walkDownUses(of: phi.value, using: operand)
}
return leafUse(operand)
return nonForwardingUse(operand)
}
}

Expand Down Expand Up @@ -301,7 +313,7 @@ private struct VisitForwardedUses : ForwardingDefUseWalker {
visitedValues.insert(value)
}

mutating func leafUse(_ operand: Operand) -> WalkResult {
mutating func nonForwardingUse(_ operand: Operand) -> WalkResult {
return visitor(.operand(operand))
}

Expand Down
Loading