Skip to content

Commit d065a3e

Browse files
authored
Merge pull request #75562 from atrick/nfc-lifedep
[nfc] LifetimeDiagnostics cleanup
2 parents 1869174 + 1113c75 commit d065a3e

14 files changed

+165
-68
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public func precondition(_ condition: Bool, _ message: @autoclosure () -> String
5858
// Debugging Utilities
5959
//===----------------------------------------------------------------------===//
6060

61+
public func debugLog(prefix: Bool = true, _ message: @autoclosure () -> String) {
62+
let formatted = (prefix ? "### " : "") + message()
63+
formatted._withBridgedStringRef { ref in
64+
Bridged_dbgs().write(ref)
65+
}
66+
Bridged_dbgs().newLine()
67+
}
68+
6169
/// Let's lldb's `po` command not print any "internal" properties of the conforming type.
6270
///
6371
/// This is useful if the `description` already contains all the information of a type instance.

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private let verbose = false
1616

1717
private func log(prefix: Bool = true, _ message: @autoclosure () -> String) {
1818
if verbose {
19-
print((prefix ? "### " : "") + message())
19+
debugLog(prefix: prefix, message())
2020
}
2121
}
2222

@@ -36,6 +36,7 @@ let lifetimeDependenceDiagnosticsPass = FunctionPass(
3636
#endif
3737
log(prefix: false, "\n--- Diagnosing lifetime dependence in \(function.name)")
3838
log("\(function)")
39+
log("\(function.convention)")
3940

4041
for argument in function.arguments
4142
where !argument.type.isEscapable(in: function)
@@ -54,8 +55,8 @@ let lifetimeDependenceDiagnosticsPass = FunctionPass(
5455
continue
5556
}
5657
if let apply = instruction as? FullApplySite {
57-
// Handle ~Escapable results that do not have a lifetime
58-
// dependence (@_unsafeNonescapableResult).
58+
// Handle ~Escapable results that do not have a lifetime dependence. This includes implicit initializers and
59+
// @_unsafeNonescapableResult.
5960
apply.resultOrYields.forEach {
6061
if let lifetimeDep = LifetimeDependence(unsafeApplyResult: $0,
6162
context) {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceInsertion.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ private func insertDependencies(for apply: LifetimeDependentApply,
116116
insertMarkDependencies(value: dependentValue, initializer: nil,
117117
bases: bases, builder: builder, context)
118118
}
119-
let builder = Builder(after: apply.applySite, context)
120119
for resultOper in apply.applySite.indirectResultOperands {
121120
let accessBase = resultOper.value.accessBase
122121
guard let (initialAddress, initializingStore) =
@@ -132,11 +131,11 @@ private func insertDependencies(for apply: LifetimeDependentApply,
132131
context) else {
133132
continue
134133
}
135-
assert(initializingStore == resultOper.instruction,
136-
"an indirect result is a store")
137-
insertMarkDependencies(value: initialAddress,
138-
initializer: initializingStore, bases: bases,
139-
builder: builder, context)
134+
assert(initializingStore == resultOper.instruction, "an indirect result is a store")
135+
Builder.insert(after: apply.applySite, context) { builder in
136+
insertMarkDependencies(value: initialAddress, initializer: initializingStore, bases: bases, builder: builder,
137+
context)
138+
}
140139
}
141140
}
142141

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private func optimizeNonOptionalBridging(_ apply: ApplyInst,
232232
private func removeBridgingCodeInPredecessors(of block: BasicBlock, _ context: FunctionPassContext) {
233233
for pred in block.predecessors {
234234
let branch = pred.terminator as! BranchInst
235-
let builder = Builder(after: branch, context)
235+
let builder = Builder(atEndOf: branch.parentBlock, location: branch.location, context)
236236
builder.createBranch(to: block)
237237

238238
let en = branch.operands[0].value as! EnumInst

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,19 +487,23 @@ extension Builder {
487487
}
488488

489489
/// Creates a builder which inserts _after_ `insPnt`, using a custom `location`.
490+
///
491+
/// TODO: this is usually incorrect for terminator instructions. Instead use
492+
/// `Builder.insert(after:location:_:insertFunc)` from OptUtils.swift. Rename this to afterNonTerminator.
490493
init(after insPnt: Instruction, location: Location, _ context: some MutatingContext) {
491494
context.verifyIsTransforming(function: insPnt.parentFunction)
492-
if let nextInst = insPnt.next {
493-
self.init(insertAt: .before(nextInst), location: location,
494-
context.notifyInstructionChanged, context._bridged.asNotificationHandler())
495-
} else {
496-
self.init(insertAt: .atEndOf(insPnt.parentBlock), location: location,
497-
context.notifyInstructionChanged, context._bridged.asNotificationHandler())
495+
guard let nextInst = insPnt.next else {
496+
fatalError("cannot insert an instruction after a block terminator.")
498497
}
498+
self.init(insertAt: .before(nextInst), location: location,
499+
context.notifyInstructionChanged, context._bridged.asNotificationHandler())
499500
}
500501

501502
/// Creates a builder which inserts _after_ `insPnt`, using `insPnt`'s next
502503
/// non-meta instruction's location.
504+
///
505+
/// TODO: this is incorrect for terminator instructions. Instead use `Builder.insert(after:location:_:insertFunc)`
506+
/// from OptUtils.swift. Rename this to afterNonTerminator.
503507
init(after insPnt: Instruction, _ context: some MutatingContext) {
504508
self.init(after: insPnt, location: insPnt.locationOfNextNonMetaInstruction, context)
505509
}

SwiftCompilerSources/Sources/Optimizer/Utilities/AddressUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private func log(_ message: @autoclosure () -> String) {
2525
///
2626
/// TODO: Integrate this with SIL verification to ensure completeness.
2727
///
28-
/// TODO: Convert AddressDefUseWalker to conform to AddressUtils after
28+
/// TODO: Convert AddressDefUseWalker to use AddressUseVisitor after
2929
/// checking that the additional instructions are handled correctly by
3030
/// escape analysis.
3131
///

SwiftCompilerSources/Sources/Optimizer/Utilities/FunctionSignatureTransforms.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ private func createForwardingApply(
179179
isNonThrowing: ai.isNonThrowing,
180180
isNonAsync: ai.isNonAsync,
181181
specializationInfo: ai.specializationInfo)
182-
let builder = Builder(after: newApply, context)
183182
builder.createReturn(of: newApply)
184183
case let tai as TryApplyInst:
185184
let normalBlock = thunk.appendNewBlock(context)

SwiftCompilerSources/Sources/Optimizer/Utilities/LifetimeDependenceUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ struct LifetimeDependence : CustomStringConvertible {
109109
case caller(Argument)
110110
/// An access scope.
111111
case access(BeginAccessInst)
112-
/// An coroutine.
112+
/// A coroutine.
113113
case yield(Value)
114114
/// An owned value whose OSSA lifetime encloses nonescapable values
115115
case owned(Value)

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ extension FullApplySite {
163163
}
164164

165165
extension Builder {
166+
static func insert(after inst: Instruction, _ context: some MutatingContext, insertFunc: (Builder) -> ()) {
167+
Builder.insert(after: inst, location: inst.location, context, insertFunc: insertFunc)
168+
}
169+
166170
static func insert(after inst: Instruction, location: Location,
167171
_ context: some MutatingContext, insertFunc: (Builder) -> ()) {
168172
if inst is TermInst {

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public struct ApplyOperandConventions : Collection {
7171
calleeArgumentIndex(ofOperandIndex: operandIndex)!]
7272
}
7373

74+
public subscript(parameterDependencies operandIndex: Int)
75+
-> FunctionConvention.LifetimeDependencies? {
76+
return calleeArgumentConventions[parameterDependencies:
77+
calleeArgumentIndex(ofOperandIndex: operandIndex)!]
78+
}
79+
7480
public var firstParameterOperandIndex: Int {
7581
return ApplyOperandConventions.firstArgumentIndex +
7682
calleeArgumentConventions.firstParameterIndex
@@ -222,6 +228,16 @@ extension ApplySite {
222228
functionConvention.resultDependencies != nil
223229
}
224230

231+
public var hasLifetimeDependence: Bool {
232+
functionConvention.hasLifetimeDependencies()
233+
}
234+
235+
public func parameterDependencies(target operand: Operand) -> FunctionConvention.LifetimeDependencies? {
236+
let idx = operand.index
237+
return idx < operandConventions.startIndex ? nil
238+
: operandConventions[parameterDependencies: idx]
239+
}
240+
225241
public var yieldConventions: YieldConventions {
226242
YieldConventions(convention: functionConvention)
227243
}

0 commit comments

Comments
 (0)