Skip to content

Commit d0c42b0

Browse files
committed
SwiftCompilerSources: move PhiUpdater.swift from the Optimizer to the SIL module
1 parent f3b67d8 commit d0c42b0

File tree

9 files changed

+50
-36
lines changed

9 files changed

+50
-36
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public func initializeSwiftModules() {
1919
registerAST()
2020
registerSIL()
2121
registerSwiftAnalyses()
22-
registerUtilities()
2322
registerSwiftPasses()
2423
registerOptimizerTests()
2524
}
@@ -158,7 +157,3 @@ private func registerSwiftAnalyses() {
158157
AliasAnalysis.register()
159158
CalleeAnalysis.register()
160159
}
161-
162-
private func registerUtilities() {
163-
registerPhiUpdater()
164-
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ swift_compiler_sources(Optimizer
1616
SILPrinter.swift
1717
RangeDumper.swift
1818
RunUnitTests.swift
19+
UpdateBorrowedFrom.swift
1920
TestInstructionIteration.swift
2021
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===--- PhiUpdater.swift -------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
/// This pass is only used for testing.
16+
/// In the regular pipeline it's not needed because optimization passes must make sure that borrowed-from
17+
/// instructions are updated once the pass finishes.
18+
let updateBorrowedFromPass = FunctionPass(name: "update-borrowed-from") {
19+
(function: Function, context: FunctionPassContext) in
20+
21+
updateBorrowedFrom(in: function, context)
22+
}

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ swift_compiler_sources(Optimizer
1818
LocalVariableUtils.swift
1919
OptUtils.swift
2020
OwnershipLiveness.swift
21-
PhiUpdater.swift
2221
StaticInitCloner.swift
2322
)

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,5 @@ private func registerSILClasses() {
270270

271271
private func registerUtilities() {
272272
registerVerifier()
273+
registerPhiUpdater()
273274
}

SwiftCompilerSources/Sources/SIL/Utilities/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ swift_compiler_sources(SIL
1010
AccessUtils.swift
1111
BorrowUtils.swift
1212
ForwardingUtils.swift
13+
PhiUpdater.swift
1314
SequenceUtilities.swift
1415
SmallProjectionPath.swift
1516
SSAUpdater.swift

SwiftCompilerSources/Sources/Optimizer/Utilities/PhiUpdater.swift renamed to SwiftCompilerSources/Sources/SIL/Utilities/PhiUpdater.swift

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,22 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SIL
14-
import OptimizerBridging
13+
import SILBridging
1514

1615
/// Updates the reborrow flags and the borrowed-from instructions for all guaranteed phis in `function`.
17-
func updateGuaranteedPhis(in function: Function, _ context: some MutatingContext) {
16+
public func updateGuaranteedPhis(in function: Function, _ context: some MutatingContext) {
1817
updateReborrowFlags(in: function, context)
1918
updateBorrowedFrom(in: function, context)
2019
}
2120

2221
/// Updates the reborrow flags and the borrowed-from instructions for all `phis`.
23-
func updateGuaranteedPhis(phis: some Sequence<Phi>, _ context: some MutatingContext) {
22+
public func updateGuaranteedPhis(phis: some Sequence<Phi>, _ context: some MutatingContext) {
2423
updateReborrowFlags(for: phis, context)
2524
updateBorrowedFrom(for: phis, context)
2625
}
2726

2827
/// Update all borrowed-from instructions in the `function`
29-
func updateBorrowedFrom(in function: Function, _ context: some MutatingContext) {
28+
public func updateBorrowedFrom(in function: Function, _ context: some MutatingContext) {
3029
if !function.hasOwnership {
3130
return
3231
}
@@ -44,7 +43,7 @@ func updateBorrowedFrom(in function: Function, _ context: some MutatingContext)
4443
}
4544

4645
/// Update borrowed-from instructions for a set of phi arguments.
47-
func updateBorrowedFrom(for phis: some Sequence<Phi>, _ context: some MutatingContext) {
46+
public func updateBorrowedFrom(for phis: some Sequence<Phi>, _ context: some MutatingContext) {
4847
for phi in phis {
4948
if !phi.value.parentFunction.hasOwnership {
5049
return
@@ -67,7 +66,7 @@ func updateBorrowedFrom(for phis: some Sequence<Phi>, _ context: some MutatingCo
6766
}
6867

6968
/// Updates the reborrow flags for all guaranteed phis in `function`.
70-
func updateReborrowFlags(in function: Function, _ context: some MutatingContext) {
69+
public func updateReborrowFlags(in function: Function, _ context: some MutatingContext) {
7170
if !function.hasOwnership {
7271
return
7372
}
@@ -90,7 +89,7 @@ func updateReborrowFlags(in function: Function, _ context: some MutatingContext)
9089
/// by cutting off the control flow before an `end_borrow`, the re-borrow flags still have to remain
9190
/// without the possibility to re-calculate them from the (now missing) `end_borrow`.
9291
///
93-
func updateReborrowFlags(for phis: some Sequence<Phi>, _ context: some MutatingContext) {
92+
public func updateReborrowFlags(for phis: some Sequence<Phi>, _ context: some MutatingContext) {
9493
if let phi = phis.first(where: { phi in true }), !phi.value.parentFunction.hasOwnership {
9594
return
9695
}
@@ -160,7 +159,7 @@ private func createEmptyBorrowedFrom(for phi: Phi, _ context: some MutatingConte
160159
/// use(%1)
161160
/// ```
162161
///
163-
func replacePhiWithIncomingValue(phi: Phi, _ context: some MutatingContext) -> Bool {
162+
public func replacePhiWithIncomingValue(phi: Phi, _ context: some MutatingContext) -> Bool {
164163
if phi.predecessors.isEmpty {
165164
return false
166165
}
@@ -207,7 +206,7 @@ func replacePhiWithIncomingValue(phi: Phi, _ context: some MutatingContext) -> B
207206
///
208207
/// It's not needed to run this utility if SSAUpdater is used to create a _new_ OSSA liverange.
209208
///
210-
func replacePhisWithIncomingValues(phis: [Phi], _ context: some MutatingContext) {
209+
public func replacePhisWithIncomingValues(phis: [Phi], _ context: some MutatingContext) {
211210
var currentPhis = phis
212211
// Do this in a loop because replacing one phi might open up the opportunity for another phi
213212
// and the order of phis in the array can be arbitrary.
@@ -229,13 +228,13 @@ func registerPhiUpdater() {
229228
BridgedUtilities.registerPhiUpdater(
230229
// updateAllGuaranteedPhis
231230
{ (bridgedCtxt: BridgedContext, bridgedFunction: BridgedFunction) in
232-
let context = FunctionPassContext(_bridged: bridgedCtxt)
231+
let context = PhiUpdaterContext(_bridged: bridgedCtxt)
233232
let function = bridgedFunction.function;
234233
updateGuaranteedPhis(in: function, context)
235234
},
236235
// updateGuaranteedPhis
237236
{ (bridgedCtxt: BridgedContext, bridgedPhiArray: BridgedArrayRef) in
238-
let context = FunctionPassContext(_bridged: bridgedCtxt)
237+
let context = PhiUpdaterContext(_bridged: bridgedCtxt)
239238
var guaranteedPhis = Stack<Phi>(context)
240239
defer { guaranteedPhis.deinitialize() }
241240
bridgedPhiArray.withElements(ofType: BridgedValue.self) {
@@ -250,21 +249,17 @@ func registerPhiUpdater() {
250249
},
251250
// replacePhisWithIncomingValues
252251
{ (bridgedCtxt: BridgedContext, bridgedPhiArray: BridgedArrayRef) in
253-
let context = FunctionPassContext(_bridged: bridgedCtxt)
252+
let context = PhiUpdaterContext(_bridged: bridgedCtxt)
254253
var phis = [Phi]()
255254
bridgedPhiArray.withElements(ofType: BridgedValue.self) {
256255
phis = $0.map { Phi($0.value)! }
257256
}
258257
replacePhisWithIncomingValues(phis: phis, context)
259258
}
260259
)
261-
}
262-
263-
/// This pass is only used for testing.
264-
/// In the regular pipeline it's not needed because optimization passes must make sure that borrowed-from
265-
/// instructions are updated once the pass finishes.
266-
let updateBorrowedFromPass = FunctionPass(name: "update-borrowed-from") {
267-
(function: Function, context: FunctionPassContext) in
268260

269-
updateBorrowedFrom(in: function, context)
261+
struct PhiUpdaterContext: MutatingContext {
262+
let _bridged: BridgedContext
263+
public let notifyInstructionChanged: (Instruction) -> () = { inst in }
264+
}
270265
}

include/swift/SIL/SILBridging.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,15 @@ struct BridgedVerifier {
15041504
OptionalBridgedArgument atArgument);
15051505
};
15061506

1507+
struct BridgedUtilities {
1508+
typedef void (* _Nonnull UpdateFunctionFn)(BridgedContext, BridgedFunction);
1509+
typedef void (* _Nonnull UpdatePhisFn)(BridgedContext, BridgedArrayRef);
1510+
1511+
static void registerPhiUpdater(UpdateFunctionFn updateBorrowedFromFn,
1512+
UpdatePhisFn updateBorrowedFromPhisFn,
1513+
UpdatePhisFn replacePhisWithIncomingValuesFn);
1514+
};
1515+
15071516
namespace swift::test {
15081517
struct Arguments;
15091518
class FunctionTest;

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,6 @@ struct BridgedPostDomTree {
119119
BRIDGED_INLINE bool postDominates(BridgedBasicBlock dominating, BridgedBasicBlock dominated) const;
120120
};
121121

122-
struct BridgedUtilities {
123-
typedef void (* _Nonnull UpdateFunctionFn)(BridgedContext, BridgedFunction);
124-
typedef void (* _Nonnull UpdatePhisFn)(BridgedContext, BridgedArrayRef);
125-
126-
static void registerPhiUpdater(UpdateFunctionFn updateBorrowedFromFn,
127-
UpdatePhisFn updateBorrowedFromPhisFn,
128-
UpdatePhisFn replacePhisWithIncomingValuesFn);
129-
};
130-
131122
struct BridgedSpecializationCloner {
132123
swift::SpecializationCloner * _Nonnull cloner;
133124

0 commit comments

Comments
 (0)