Skip to content

Commit 3f8a35e

Browse files
committed
ForwardingUtils comments and cleanup.
1 parent 3bf7e71 commit 3f8a35e

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/ForwardingUtils.swift

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,32 +197,39 @@ enum ForwardingUseResult: CustomStringConvertible {
197197
}
198198
}
199199

200-
/// Visit all the uses in a forward-extended lifetime
200+
/// Visit all the non-forwarding uses in a forward-extended lifetime
201201
/// (LifetimeIntroducer -> Operand).
202202
///
203203
/// Minimal requirements:
204204
/// needWalk(for value: Value) -> Bool
205-
/// leafUse(_ operand: Operand) -> WalkResult
205+
/// nonForwardingUse(_ operand: Operand) -> WalkResult
206206
/// deadValue(_ value: Value, using operand: Operand?) -> WalkResult
207207
///
208208
/// Start walking:
209209
/// walkDown(root: Value)
210+
///
210211
protocol ForwardingDefUseWalker {
211-
// Minimally, check a ValueSet. This walker may traverse chains of
212-
// aggregation and destructuring by default. Implementations may
213-
// handle phis.
212+
/// Minimally, check a ValueSet. This walker may traverse chains of
213+
/// aggregation and destructuring by default. Implementations may
214+
/// handle phis.
214215
mutating func needWalk(for value: Value) -> Bool
215216

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

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

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

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

237244
mutating func walkDownUses(of value: Value, using operand: Operand?)
238-
-> WalkResult {
245+
-> WalkResult {
239246
return walkDownUsesDefault(forwarding: value, using: operand)
240247
}
241248

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

247254
var hasUse = false
248-
for operand in value.uses where !operand.isTypeDependent {
249-
if walkDown(operand: operand) == .abortWalk {
255+
for use in value.uses where !use.isTypeDependent {
256+
if walkDown(operand: use) == .abortWalk {
250257
return .abortWalk
251258
}
252259
hasUse = true
@@ -263,12 +270,17 @@ extension ForwardingDefUseWalker {
263270

264271
mutating func walkDownDefault(forwarding operand: Operand) -> WalkResult {
265272
if let inst = operand.instruction as? ForwardingInstruction {
266-
return inst.forwardedResults.walk { walkDownUses(of: $0, using: operand) }
273+
let singleOper = inst.singleForwardedOperand
274+
if singleOper == nil || singleOper! == operand {
275+
return inst.forwardedResults.walk {
276+
walkDownUses(of: $0, using: operand)
277+
}
278+
}
267279
}
268280
if let phi = Phi(using: operand) {
269281
return walkDownUses(of: phi.value, using: operand)
270282
}
271-
return leafUse(operand)
283+
return nonForwardingUse(operand)
272284
}
273285
}
274286

@@ -301,7 +313,7 @@ private struct VisitForwardedUses : ForwardingDefUseWalker {
301313
visitedValues.insert(value)
302314
}
303315

304-
mutating func leafUse(_ operand: Operand) -> WalkResult {
316+
mutating func nonForwardingUse(_ operand: Operand) -> WalkResult {
305317
return visitor(.operand(operand))
306318
}
307319

SwiftCompilerSources/Sources/SIL/ForwardingInstruction.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ extension ForwardingInstruction {
6363

6464
// An instruction that forwards a single value to a single result.
6565
//
66-
// For legacy reasons, some ForwardingInstructions that fit the SingleValueInstruction and UnaryInstruction requirements are not considered ConversionInstructions because certain routines do not want to see through them (InitExistentialValueInst, InitExistentialValueInst, OpenExistentialValueInst, OpenExistentialValueInst). This most likely has to do with type-dependent operands, although any ConversionInstruction should support type-dependent operands.
66+
// For legacy reasons, some ForwardingInstructions that fit the
67+
// SingleValueInstruction and UnaryInstruction requirements are not
68+
// considered ConversionInstructions because certain routines do not
69+
// want to see through them (InitExistentialValueInst,
70+
// DeinitExistentialValueInst, OpenExistentialValueInst,
71+
// OpenExistentialValueInst). This most likely has to do with
72+
// type-dependent operands, although any ConversionInstruction should
73+
// support type-dependent operands.
6774
public protocol ConversionInstruction : SingleValueInstruction,
6875
UnaryInstruction,
6976
ForwardingInstruction

0 commit comments

Comments
 (0)