Skip to content

Commit d01d74e

Browse files
committed
LifetimeDependenceDiagnostics: find the correct variable introducer.
1 parent 73f7f8f commit d01d74e

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -236,32 +236,22 @@ private struct LifetimeVariable {
236236
self = Self(accessBase: value.accessBase, context)
237237
return
238238
}
239-
if let firstIntroducer = getFirstBorrowIntroducer(of: value, context) {
239+
if let firstIntroducer = getFirstVariableIntroducer(of: value, context) {
240240
self = Self(introducer: firstIntroducer)
241241
return
242242
}
243243
self.varDecl = nil
244244
self.sourceLoc = nil
245245
}
246246

247-
// FUTURE: consider diagnosing multiple variable introducers. It's
248-
// unclear how more than one can happen.
249-
private func getFirstBorrowIntroducer(of value: Value,
250-
_ context: some Context)
251-
-> Value? {
252-
var introducers = Stack<Value>(context)
253-
gatherBorrowIntroducers(for: value, in: &introducers, context)
254-
return introducers.pop()
255-
}
256-
257-
private func getFirstLifetimeIntroducer(of value: Value,
258-
_ context: some Context)
259-
-> Value? {
247+
private func getFirstVariableIntroducer(of value: Value, _ context: some Context) -> Value? {
260248
var introducer: Value?
261-
_ = visitLifetimeIntroducers(for: value, context) {
249+
var useDefVisitor = VariableIntroducerUseDefWalker(context) {
262250
introducer = $0
263251
return .abortWalk
264252
}
253+
defer { useDefVisitor.deinitialize() }
254+
_ = useDefVisitor.walkUp(valueOrAddress: value)
265255
return introducer
266256
}
267257

@@ -283,12 +273,11 @@ private struct LifetimeVariable {
283273
private init(accessBase: AccessBase, _ context: some Context) {
284274
switch accessBase {
285275
case .box(let projectBox):
286-
if let box = getFirstLifetimeIntroducer(of: projectBox.box, context) {
287-
self = Self(introducer: box)
288-
}
289-
// We should always find an introducer since boxes are nontrivial.
290-
self.varDecl = nil
291-
self.sourceLoc = nil
276+
// Note: referenceRoot looks through `begin_borrow [var_decl]` and `move_value [var_decl]`. But the box should
277+
// never be produced by one of these, except when it is redundant with the `alloc_box` VarDecl. It does not seem
278+
// possible for a box to be moved/borrowed directly into another variable's box. Reassignment always loads/stores
279+
// the value.
280+
self = Self(introducer: projectBox.box.referenceRoot)
292281
case .stack(let allocStack):
293282
self = Self(introducer: allocStack)
294283
case .global(let globalVar):

0 commit comments

Comments
 (0)