Skip to content

Commit c10c545

Browse files
committed
OwnershipUtils review feedback: code formatting.
1 parent d445754 commit c10c545

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/BorrowUtils.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,12 @@ enum BeginBorrowValue {
289289
let inst = operand.instruction as! SingleValueInstruction
290290
self = BeginBorrowValue(inst)!
291291
case is BranchInst:
292-
guard let phi = Phi(using: operand) else { return nil }
293-
guard phi.isReborrow else { return nil }
292+
guard let phi = Phi(using: operand) else {
293+
return nil
294+
}
295+
guard phi.isReborrow else {
296+
return nil
297+
}
294298
self = .reborrow(phi)
295299
default:
296300
return nil
@@ -404,16 +408,16 @@ private struct BorrowIntroducers {
404408
}
405409

406410
private mutating func push(_ introducer: Value,
407-
introducers: inout Stack<Value>) {
411+
in introducers: inout Stack<Value>) {
408412
if visitedIntroducers.insert(introducer.hashable).inserted {
409413
introducers.push(introducer)
410414
}
411415
}
412416

413417
private mutating func push<S: Sequence>(contentsOf other: S,
414-
introducers: inout Stack<Value>) where S.Element == Value {
418+
in introducers: inout Stack<Value>) where S.Element == Value {
415419
for elem in other {
416-
push(elem, introducers: &introducers)
420+
push(elem, in: &introducers)
417421
}
418422
}
419423

@@ -430,7 +434,7 @@ private struct BorrowIntroducers {
430434
// 'introducers' to avoid duplicates and avoid exponential
431435
// recursion on aggregates.
432436
if let cachedIntroducers = cache.valueIntroducers[value.hashable] {
433-
cachedIntroducers.forEach { push($0, introducers: &introducers) }
437+
push(contentsOf: cachedIntroducers, in: &introducers)
434438
return
435439
}
436440
introducers.withMarker(
@@ -452,7 +456,7 @@ private struct BorrowIntroducers {
452456
return
453457

454458
case .owned:
455-
push(value, introducers: &introducers);
459+
push(value, in: &introducers);
456460
return
457461

458462
case .guaranteed:
@@ -461,7 +465,7 @@ private struct BorrowIntroducers {
461465
// BeginBorrowedValue handles the initial scope introducers: begin_borrow,
462466
// load_borrow, & reborrow.
463467
if BeginBorrowValue(value) != nil {
464-
push(value, introducers: &introducers)
468+
push(value, in: &introducers)
465469
return
466470
}
467471
// Handle guaranteed forwarding phis
@@ -531,7 +535,7 @@ private struct BorrowIntroducers {
531535
// Map the incoming introducers to an outer-adjacent phi if one exists.
532536
push(contentsOf: mapToPhi(predecessor: pred,
533537
incomingValues: incomingIntroducers),
534-
introducers: &introducers)
538+
in: &introducers)
535539
}
536540
// Remove this phi from the pending set. This phi may be visited
537541
// again at a different level of phi recursion. In that case, we
@@ -724,8 +728,9 @@ private struct EnclosingValues {
724728
in enclosingValues: inout Stack<Value>,
725729
_ cache: inout BorrowIntroducers.Cache) {
726730

727-
guard visitedReborrows.insert(reborrow.value) else { return }
728-
731+
guard visitedReborrows.insert(reborrow.value) else {
732+
return
733+
}
729734
// avoid duplicates in the enclosingValues set.
730735
var pushedEnclosingValues = ValueSet(context)
731736
defer { pushedEnclosingValues.deinitialize() }

SwiftCompilerSources/Sources/Optimizer/Utilities/ForwardingUtils.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,15 @@ func gatherLifetimeIntroducers(for value: Value, _ context: Context) -> [Value]
145145
introducers.append($0)
146146
return .continueWalk
147147
}
148-
defer { walker.visitedValues.deinitialize() }
148+
defer { walker.deinitialize() }
149149
_ = walker.walkUp(value: value)
150150
return introducers
151151
}
152152

153153
// TODO: visitor can be nonescaping when we have borrowed properties.
154154
func visitLifetimeIntroducers(for value: Value, _ context: Context,
155-
visitor: @escaping (Value) -> WalkResult) -> WalkResult {
155+
visitor: @escaping (Value) -> WalkResult)
156+
-> WalkResult {
156157
var walker = VisitLifetimeIntroducers(context, visitor: visitor)
157158
defer { walker.visitedValues.deinitialize() }
158159
return walker.walkUp(value: value)
@@ -167,6 +168,8 @@ private struct VisitLifetimeIntroducers : ForwardingUseDefWalker {
167168
self.visitedValues = ValueSet(context)
168169
}
169170

171+
mutating func deinitialize() { visitedValues.deinitialize() }
172+
170173
mutating func needWalk(for value: Value) -> Bool {
171174
visitedValues.insert(value)
172175
}

SwiftCompilerSources/Sources/Optimizer/Utilities/OwnershipLiveness.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func computeInteriorLiveness(for definingValue: Value,
9595

9696
var range = InstructionRange(for: definingValue, context)
9797

98-
var visitor = InteriorUseVisitor(definingValue: definingValue, context) {
98+
var visitor = InteriorUseWalker(definingValue: definingValue, context) {
9999
range.insert($0.instruction)
100100
return .continueWalk
101101
}
@@ -159,18 +159,19 @@ func computeInteriorLiveness(for definingValue: Value,
159159
///
160160
/// TODO: Change the operandOwnership of MarkDependenceInst base operand.
161161
/// It should be a borrowing operand, not a pointer escape.
162-
struct InteriorUseVisitor: OwnershipUseVisitor, AddressUseVisitor {
162+
struct InteriorUseWalker: OwnershipUseVisitor, AddressUseVisitor {
163163
let context: FunctionPassContext
164164
var _context: Context { context }
165165

166-
let function: Function
167166
let definingValue: Value
168167
let useVisitor: (Operand) -> WalkResult
169168

170169
var innerScopeHandler: InnerScopeHandler? = nil
171170

172171
var unenclosedPhis: [Phi] = []
173172

173+
var function: Function { definingValue.parentFunction }
174+
174175
/// If any interior pointer may escape, then record the first instance
175176
/// here. This immediately aborts the walk, so further instances are
176177
/// unavailable.
@@ -197,7 +198,6 @@ struct InteriorUseVisitor: OwnershipUseVisitor, AddressUseVisitor {
197198
visitor: @escaping (Operand) -> WalkResult) {
198199
assert(!definingValue.type.isAddress, "address values have no ownership")
199200
self.context = context
200-
self.function = definingValue.parentFunction
201201
self.definingValue = definingValue
202202
self.useVisitor = visitor
203203
self.visited = ValueSet(context)
@@ -312,18 +312,22 @@ struct InteriorUseVisitor: OwnershipUseVisitor, AddressUseVisitor {
312312
}
313313

314314
func handleInner(borrow: BeginBorrowValue) -> WalkResult {
315-
guard let innerScopeHandler else { return .continueWalk }
315+
guard let innerScopeHandler else {
316+
return .continueWalk
317+
}
316318
return innerScopeHandler(borrow.value)
317319
}
318320

319321
func handleAccess(address: BeginAccessInst) -> WalkResult {
320-
guard let innerScopeHandler else { return .continueWalk }
322+
guard let innerScopeHandler else {
323+
return .continueWalk
324+
}
321325
return innerScopeHandler(address)
322326
}
323327
}
324328

325329
// Helpers to walk down forwarding operations.
326-
extension InteriorUseVisitor {
330+
extension InteriorUseWalker {
327331
// Walk down forwarding operands
328332
private mutating func walkDown(operand: Operand) -> WalkResult {
329333
// Record all uses
@@ -345,10 +349,12 @@ extension InteriorUseVisitor {
345349
}
346350

347351
private mutating func walkDownUses(of value: Value) -> WalkResult {
348-
guard value.ownership.hasLifetime else { return .continueWalk }
349-
350-
guard visited.insert(value) else { return .continueWalk }
351-
352+
guard value.ownership.hasLifetime else {
353+
return .continueWalk
354+
}
355+
guard visited.insert(value) else {
356+
return .continueWalk
357+
}
352358
switch value.ownership {
353359
case .owned:
354360
return visitUsesOfInner(value: value)
@@ -985,7 +991,7 @@ let interiorLivenessTest = FunctionTest("interior_liveness_swift") {
985991
var range = InstructionRange(for: value, context)
986992
defer { range.deinitialize() }
987993

988-
var visitor = InteriorUseVisitor(definingValue: value, context) {
994+
var visitor = InteriorUseWalker(definingValue: value, context) {
989995
range.insert($0.instruction)
990996
return .continueWalk
991997
}
@@ -1047,5 +1053,5 @@ let addressUseTest = FunctionTest("address_use_test") {
10471053
print(function)
10481054
print("Uses of address: \(address)")
10491055
var printer = AddressUsePrinter(_context: context)
1050-
printer.walkDownUses(ofAddress: address)
1056+
_ = printer.walkDownUses(ofAddress: address)
10511057
}

0 commit comments

Comments
 (0)