|
| 1 | +//===--- LifetimeDependenceScopeFixup.swift ----------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===---------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// For an apply that returns a lifetime dependent value, |
| 14 | +// LifetimeDependenceInsertion inserts mark_dependence [unresolved] on parent |
| 15 | +// value's access scope. LifetimeDependenceScopeFixup then extends the access |
| 16 | +// scope to cover all uses of the dependent value. |
| 17 | + |
| 18 | +// This pass must run after LifetimeDependenceInsertion and before |
| 19 | +// LifetimeDependenceDiagnostics. |
| 20 | + |
| 21 | +import SIL |
| 22 | + |
| 23 | +private let verbose = false |
| 24 | + |
| 25 | +private func log(_ message: @autoclosure () -> String) { |
| 26 | + if verbose { |
| 27 | + print("### \(message())") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +let lifetimeDependenceScopeFixupPass = FunctionPass( |
| 32 | + name: "lifetime-dependence-scope-fixup") |
| 33 | +{ (function: Function, context: FunctionPassContext) in |
| 34 | + log("Scope fixup for lifetime dependence in \(function.name)") |
| 35 | + |
| 36 | + for instruction in function.instructions { |
| 37 | + guard let markDep = instruction as? MarkDependenceInst else { |
| 38 | + continue |
| 39 | + } |
| 40 | + if let lifetimeDep = LifetimeDependence(markDep, context) { |
| 41 | + fixup(dependence: lifetimeDep, context) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +private func fixup(dependence: LifetimeDependence, |
| 47 | + _ context: FunctionPassContext) { |
| 48 | + log("Scope fixup for lifetime dependent instructions: \(dependence)") |
| 49 | + |
| 50 | + guard case .access(let bai) = dependence.scope else { |
| 51 | + return |
| 52 | + } |
| 53 | + var range = InstructionRange(begin: bai, context) |
| 54 | + var walker = LifetimeDependenceScopeFixupWalker(bai.parentFunction, context) { |
| 55 | + range.insert($0.instruction) |
| 56 | + return .continueWalk |
| 57 | + } |
| 58 | + defer {walker.deinitialize()} |
| 59 | + _ = walker.walkDown(root: dependence.dependentValue) |
| 60 | + defer {range.deinitialize()} |
| 61 | + |
| 62 | + var beginAccess = bai |
| 63 | + while (true) { |
| 64 | + var endAcceses = [Instruction]() |
| 65 | + // Collect original end_access instructions |
| 66 | + for end in beginAccess.endInstructions { |
| 67 | + endAcceses.append(end) |
| 68 | + } |
| 69 | + |
| 70 | + // Insert original end_access instructions to prevent access scope shortening |
| 71 | + range.insert(contentsOf: endAcceses) |
| 72 | + assert(!range.ends.isEmpty) |
| 73 | + |
| 74 | + // Create new end_access at the end of extended uses |
| 75 | + for end in range.ends { |
| 76 | + let endBuilder = Builder(after: end, context) |
| 77 | + _ = endBuilder.createEndAccess(beginAccess: beginAccess) |
| 78 | + } |
| 79 | + |
| 80 | + // Delete original end_access instructions |
| 81 | + for endAccess in endAcceses { |
| 82 | + context.erase(instruction: endAccess) |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: Add SIL support for lifetime dependence and write unit test |
| 86 | + // for nested access scopes |
| 87 | + guard case let .scope(enclosingBeginAccess) = beginAccess.address.enclosingAccessScope else { |
| 88 | + break |
| 89 | + } |
| 90 | + beginAccess = enclosingBeginAccess |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +private struct LifetimeDependenceScopeFixupWalker : LifetimeDependenceDefUseWalker { |
| 95 | + let function: Function |
| 96 | + let context: Context |
| 97 | + let visitor: (Operand) -> WalkResult |
| 98 | + var visitedValues: ValueSet |
| 99 | + |
| 100 | + init(_ function: Function, _ context: Context, visitor: @escaping (Operand) -> WalkResult) { |
| 101 | + self.function = function |
| 102 | + self.context = context |
| 103 | + self.visitor = visitor |
| 104 | + self.visitedValues = ValueSet(context) |
| 105 | + } |
| 106 | + |
| 107 | + mutating func deinitialize() { |
| 108 | + visitedValues.deinitialize() |
| 109 | + } |
| 110 | + |
| 111 | + mutating func needWalk(for value: Value) -> Bool { |
| 112 | + visitedValues.insert(value) |
| 113 | + } |
| 114 | + |
| 115 | + mutating func deadValue(_ value: Value, using operand: Operand?) |
| 116 | + -> WalkResult { |
| 117 | + if let operand { |
| 118 | + return visitor(operand) |
| 119 | + } |
| 120 | + return .continueWalk |
| 121 | + } |
| 122 | + |
| 123 | + mutating func leafUse(of operand: Operand) -> WalkResult { |
| 124 | + return visitor(operand) |
| 125 | + } |
| 126 | + |
| 127 | + mutating func escapingDependence(on operand: Operand) -> WalkResult { |
| 128 | + _ = visitor(operand) |
| 129 | + return .abortWalk |
| 130 | + } |
| 131 | + |
| 132 | + mutating func returnedDependence(result: Operand) -> WalkResult { |
| 133 | + return .continueWalk |
| 134 | + } |
| 135 | + |
| 136 | + mutating func returnedDependence(address: FunctionArgument, |
| 137 | + using operand: Operand) -> WalkResult { |
| 138 | + return .continueWalk |
| 139 | + } |
| 140 | +} |
| 141 | + |
0 commit comments