Skip to content

Commit 973467f

Browse files
committed
SIL, Optimizer: update and handle borrowed-from instructions
Compute, update and handle borrowed-from instruction in various utilities and passes. Also, used borrowed-from to simplify `gatherBorrowIntroducers` and `gatherEnclosingValues`. Replace those utilities by `Value.getBorrowIntroducers` and `Value.getEnclosingValues`, which return a lazily computed Sequence of borrowed/enclosing values.
1 parent 70583fd commit 973467f

File tree

99 files changed

+1156
-788
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1156
-788
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,24 +236,16 @@ private struct LifetimeVariable {
236236
self = Self(accessBase: value.accessBase, context)
237237
return
238238
}
239-
if let firstIntroducer = getFirstBorrowIntroducer(of: value, context) {
239+
// FUTURE: consider diagnosing multiple variable introducers. It's
240+
// unclear how more than one can happen.
241+
if let firstIntroducer = value.getBorrowIntroducers(context).first {
240242
self = Self(introducer: firstIntroducer)
241243
return
242244
}
243245
self.varDecl = nil
244246
self.sourceLoc = nil
245247
}
246248

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-
257249
private func getFirstLifetimeIntroducer(of value: Value,
258250
_ context: some Context)
259251
-> Value? {

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBranch.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ private extension BranchInst {
4949
let parentBB = parentBlock
5050

5151
for (argIdx, op) in operands.enumerated() {
52-
targetBB.arguments[argIdx].uses.replaceAll(with: op.value, context)
52+
let arg = targetBB.arguments[argIdx]
53+
if let phi = Phi(arg),
54+
let bfi = phi.borrowedFrom
55+
{
56+
bfi.uses.replaceAll(with: op.value, context)
57+
context.erase(instruction: bfi)
58+
} else {
59+
arg.uses.replaceAll(with: op.value, context)
60+
}
5361
}
5462
targetBB.eraseAllArguments(context)
5563

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifySwitchEnum.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ extension SwitchEnumInst : OnoneSimplifyable {
4848
precondition(enumInst.payload == nil || !parentFunction.hasOwnership,
4949
"missing payload argument in switch_enum case block")
5050
builder.createBranch(to: caseBlock)
51+
context.erase(instruction: self)
5152
case 1:
5253
builder.createBranch(to: caseBlock, arguments: [enumInst.payload!])
54+
context.erase(instruction: self)
55+
updateBorrowedFrom(for: [Phi(caseBlock.arguments[0])!], context)
5356
default:
5457
fatalError("case block of switch_enum cannot have more than 1 argument")
5558
}
56-
context.erase(instruction: self)
5759

5860
if canEraseEnumInst {
5961
context.erase(instructionIncludingDebugUses: enumInst)

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ private func registerSwiftPasses() {
117117
registerPass(rangeDumper, { rangeDumper.run($0) })
118118
registerPass(runUnitTests, { runUnitTests.run($0) })
119119
registerPass(testInstructionIteration, { testInstructionIteration.run($0) })
120+
registerPass(updateBorrowedFromPass, { updateBorrowedFromPass.run($0) })
120121
}
121122

122123
private func registerSwiftAnalyses() {
@@ -126,4 +127,5 @@ private func registerSwiftAnalyses() {
126127

127128
private func registerUtilities() {
128129
registerVerifier()
130+
registerBorrowedFromUpdater()
129131
}

0 commit comments

Comments
 (0)