Skip to content

Commit 55e2f06

Browse files
authored
Merge pull request #81133 from eeckstein/sil-instructions
SIL: some API improvements for the `ScopedInstruction` and `BorrowIntroducingInstruction` protocols
2 parents d26da38 + d80f7a3 commit 55e2f06

File tree

6 files changed

+31
-38
lines changed

6 files changed

+31
-38
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ struct AliasAnalysis {
358358
return defaultEffects(of: endBorrow, on: memLoc)
359359
case .box, .class, .tail:
360360
// Check if the memLoc is "derived" from the begin_borrow, i.e. is an interior pointer.
361-
var walker = FindBeginBorrowWalker(beginBorrow: endBorrow.borrow as! BorrowIntroducingInstruction)
361+
var walker = FindBeginBorrowWalker(beginBorrow: endBorrow.borrow as! BeginBorrowInstruction)
362362
return walker.visitAccessStorageRoots(of: accessPath) ? .noEffects : .worstEffects
363363
}
364364
}
@@ -704,7 +704,7 @@ private enum ImmutableScope {
704704
}
705705

706706
private struct FindBeginBorrowWalker : ValueUseDefWalker {
707-
let beginBorrow: BorrowIntroducingInstruction
707+
let beginBorrow: BeginBorrowInstruction
708708
var walkUpCache = WalkerCache<Path>()
709709

710710
mutating func walkUp(value: Value, path: SmallProjectionPath) -> WalkResult {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LetPropertyLowering.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private func constructLetInitRegion(
136136
// root-class initializer).
137137
initRegion.insert(markUninitialized)
138138

139-
var borrows = Stack<BorrowIntroducingInstruction>(context)
139+
var borrows = Stack<BeginBorrowInstruction>(context)
140140
defer { borrows.deinitialize() }
141141

142142
for inst in markUninitialized.parentFunction.instructions {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceScopeFixup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ private struct ExtendableScope {
287287
var firstInstruction: Instruction {
288288
switch introducer {
289289
case let .scoped(scopedInst):
290-
return scopedInst.instruction
290+
return scopedInst
291291
case let .owned(value):
292292
if let definingInst = value.definingInstructionOrTerminator {
293293
return definingInst
@@ -298,7 +298,7 @@ private struct ExtendableScope {
298298
var endInstructions: LazyMapSequence<LazyFilterSequence<UseList>, Instruction> {
299299
switch introducer {
300300
case let .scoped(scopedInst):
301-
return scopedInst.endOperands.users
301+
return scopedInst.scopeEndingOperands.users
302302
case let .owned(value):
303303
return value.uses.endingLifetime.users
304304
}

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ extension GlobalVariable {
826826

827827
extension InstructionRange {
828828
/// Adds the instruction range of a borrow-scope by transitively visiting all (potential) re-borrows.
829-
mutating func insert(borrowScopeOf borrow: BorrowIntroducingInstruction, _ context: some Context) {
829+
mutating func insert(borrowScopeOf borrow: BeginBorrowInstruction, _ context: some Context) {
830830
var worklist = ValueWorklist(context)
831831
defer { worklist.deinitialize() }
832832

SwiftCompilerSources/Sources/Optimizer/Utilities/OwnershipLiveness.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ extension InteriorUseWalker: AddressUseVisitor {
685685
if handleInner(borrowed: ba) == .abortWalk {
686686
return .abortWalk
687687
}
688-
return ba.endOperands.walk { useVisitor($0) }
688+
return ba.scopeEndingOperands.walk { useVisitor($0) }
689689
case let ba as BeginApplyInst:
690690
if handleInner(borrowed: ba.token) == .abortWalk {
691691
return .abortWalk

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ final public class BridgeObjectToRefInst : SingleValueInstruction, UnaryInstruct
11691169

11701170
final public class BridgeObjectToWordInst : SingleValueInstruction, UnaryInstruction {}
11711171

1172-
final public class BorrowedFromInst : SingleValueInstruction, BorrowIntroducingInstruction {
1172+
final public class BorrowedFromInst : SingleValueInstruction, BeginBorrowInstruction {
11731173
public var borrowedValue: Value { operands[0].value }
11741174
public var borrowedPhi: Phi { Phi(borrowedValue)! }
11751175
public var enclosingOperands: OperandArray {
@@ -1179,6 +1179,8 @@ final public class BorrowedFromInst : SingleValueInstruction, BorrowIntroducingI
11791179
public var enclosingValues: LazyMapSequence<LazySequence<OperandArray>.Elements, Value> {
11801180
enclosingOperands.values
11811181
}
1182+
1183+
public var scopeEndingOperands: LazyFilterSequence<UseList> { uses.endingLifetime }
11821184
}
11831185

11841186
final public class ProjectBoxInst : SingleValueInstruction, UnaryInstruction {
@@ -1415,10 +1417,8 @@ final public class AllocExistentialBoxInst : SingleValueInstruction, Allocation
14151417
/// An instruction whose side effects extend across a scope including other instructions. These are always paired with a
14161418
/// scope ending instruction such as `begin_access` (ending with `end_access`) and `begin_borrow` (ending with
14171419
/// `end_borrow`).
1418-
public protocol ScopedInstruction {
1419-
var instruction: Instruction { get }
1420-
1421-
var endOperands: LazyFilterSequence<UseList> { get }
1420+
public protocol ScopedInstruction: Instruction {
1421+
var scopeEndingOperands: LazyFilterSequence<UseList> { get }
14221422

14231423
var endInstructions: EndInstructions { get }
14241424
}
@@ -1427,53 +1427,44 @@ extension Instruction {
14271427
/// Return the sequence of use points of any instruction.
14281428
public var endInstructions: EndInstructions {
14291429
if let scopedInst = self as? ScopedInstruction {
1430-
return .scoped(scopedInst.endOperands.users)
1430+
return .scoped(scopedInst.scopeEndingOperands.users)
14311431
}
14321432
return .single(self)
14331433
}
14341434
}
14351435

1436-
/// Instructions beginning a borrow-scope which must be ended by `end_borrow`.
1437-
public protocol BorrowIntroducingInstruction : SingleValueInstruction, ScopedInstruction {
1438-
}
1439-
1440-
extension BorrowIntroducingInstruction {
1441-
public var instruction: Instruction { get { self } }
1436+
/// Single-value instructions beginning a borrow-scope which end with an `end_borrow` or a branch to a re-borrow phi.
1437+
/// See also `BeginBorrowValue` which represents all kind of `Value`s which begin a borrow scope.
1438+
public protocol BeginBorrowInstruction : SingleValueInstruction, ScopedInstruction {
14421439
}
14431440

14441441
final public class EndBorrowInst : Instruction, UnaryInstruction {
14451442
public var borrow: Value { operand.value }
14461443
}
14471444

1448-
extension BorrowIntroducingInstruction {
1449-
public var endOperands: LazyFilterSequence<UseList> {
1450-
return uses.lazy.filter { $0.instruction is EndBorrowInst }
1451-
}
1452-
}
1453-
1454-
final public class BeginBorrowInst : SingleValueInstruction, UnaryInstruction, BorrowIntroducingInstruction {
1445+
final public class BeginBorrowInst : SingleValueInstruction, UnaryInstruction, BeginBorrowInstruction {
14551446
public var borrowedValue: Value { operand.value }
14561447

14571448
public override var isLexical: Bool { bridged.BeginBorrow_isLexical() }
14581449
public var hasPointerEscape: Bool { bridged.BeginBorrow_hasPointerEscape() }
14591450
public var isFromVarDecl: Bool { bridged.BeginBorrow_isFromVarDecl() }
14601451

1461-
public var endOperands: LazyFilterSequence<UseList> {
1462-
return uses.endingLifetime
1463-
}
1452+
public var scopeEndingOperands: LazyFilterSequence<UseList> { uses.endingLifetime }
14641453
}
14651454

1466-
final public class LoadBorrowInst : SingleValueInstruction, LoadInstruction, BorrowIntroducingInstruction {
1455+
final public class LoadBorrowInst : SingleValueInstruction, LoadInstruction, BeginBorrowInstruction {
14671456

14681457
// True if the invariants on `load_borrow` have not been checked and should not be strictly enforced.
14691458
//
14701459
// This can only occur during raw SIL before move-only checking occurs. Developers can write incorrect
14711460
// code using noncopyable types that consumes or mutates a memory location while that location is borrowed,
14721461
// but the move-only checker must diagnose those problems before canonical SIL is formed.
14731462
public var isUnchecked: Bool { bridged.LoadBorrowInst_isUnchecked() }
1463+
1464+
public var scopeEndingOperands: LazyFilterSequence<UseList> { uses.endingLifetime }
14741465
}
14751466

1476-
final public class StoreBorrowInst : SingleValueInstruction, StoringInstruction, BorrowIntroducingInstruction {
1467+
final public class StoreBorrowInst : SingleValueInstruction, StoringInstruction, BeginBorrowInstruction {
14771468
public var allocStack: AllocStackInst {
14781469
var dest = destination
14791470
if let mark = dest as? MarkUnresolvedNonCopyableValueInst {
@@ -1482,7 +1473,13 @@ final public class StoreBorrowInst : SingleValueInstruction, StoringInstruction,
14821473
return dest as! AllocStackInst
14831474
}
14841475

1476+
public var scopeEndingOperands: LazyFilterSequence<UseList> {
1477+
return self.uses.lazy.filter { $0.instruction is EndBorrowInst }
1478+
}
1479+
14851480
public var endBorrows: LazyMapSequence<LazyFilterSequence<UseList>, Instruction> {
1481+
// A `store_borrow` is an address value.
1482+
// Only `end_borrow`s (with this address operand) can end such a borrow scope.
14861483
uses.users(ofType: EndBorrowInst.self)
14871484
}
14881485
}
@@ -1507,7 +1504,7 @@ final public class BeginAccessInst : SingleValueInstruction, UnaryInstruction {
15071504
public typealias EndAccessInstructions = LazyMapSequence<LazyFilterSequence<UseList>, EndAccessInst>
15081505

15091506
public var endAccessInstructions: EndAccessInstructions {
1510-
endOperands.map { $0.instruction as! EndAccessInst }
1507+
scopeEndingOperands.map { $0.instruction as! EndAccessInst }
15111508
}
15121509
}
15131510

@@ -1518,9 +1515,7 @@ final public class EndAccessInst : Instruction, UnaryInstruction {
15181515
}
15191516

15201517
extension BeginAccessInst : ScopedInstruction {
1521-
public var instruction: Instruction { get { self } }
1522-
1523-
public var endOperands: LazyFilterSequence<UseList> {
1518+
public var scopeEndingOperands: LazyFilterSequence<UseList> {
15241519
return uses.lazy.filter { $0.instruction is EndAccessInst }
15251520
}
15261521
}
@@ -1556,9 +1551,7 @@ final public class AbortApplyInst : Instruction, UnaryInstruction {
15561551
}
15571552

15581553
extension BeginApplyInst : ScopedInstruction {
1559-
public var instruction: Instruction { get { self } }
1560-
1561-
public var endOperands: LazyFilterSequence<UseList> {
1554+
public var scopeEndingOperands: LazyFilterSequence<UseList> {
15621555
return token.uses.lazy.filter { $0.isScopeEndingUse }
15631556
}
15641557
}

0 commit comments

Comments
 (0)