Skip to content

Commit a57c97b

Browse files
authored
Merge pull request #67717 from eeckstein/reformat-passes
Swift Optimizer: some reformatting in the optimization passes
2 parents 81cab33 + 799bd0a commit a57c97b

12 files changed

+61
-64
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AssumeSingleThreaded.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727

2828
import SIL
2929

30-
let assumeSingleThreadedPass = FunctionPass(
31-
name: "sil-assume-single-threaded", { function, context in
32-
for inst in function.instructions {
33-
guard let rcInst = inst as? RefCountingInst else { continue }
30+
let assumeSingleThreadedPass = FunctionPass(name: "sil-assume-single-threaded") {
31+
(function: Function, context: FunctionPassContext) in
3432

35-
rcInst.setAtomicity(isAtomic: false, context)
36-
}
33+
for inst in function.instructions {
34+
guard let rcInst = inst as? RefCountingInst else { continue }
35+
36+
rcInst.setAtomicity(isAtomic: false, context)
3737
}
38-
)
38+
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeEscapeEffects.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import SIL
2525
/// ```
2626
/// The pass does not try to change or re-compute _defined_ effects.
2727
///
28-
let computeEscapeEffects = FunctionPass(name: "compute-escape-effects", {
28+
let computeEscapeEffects = FunctionPass(name: "compute-escape-effects") {
2929
(function: Function, context: FunctionPassContext) in
3030

3131
var newEffects = function.effects.escapeEffects.arguments.filter {!$0.isDerived }
@@ -73,8 +73,7 @@ let computeEscapeEffects = FunctionPass(name: "compute-escape-effects", {
7373
context.modifyEffects(in: function) { (effects: inout FunctionEffects) in
7474
effects.escapeEffects.arguments = newEffects
7575
}
76-
})
77-
76+
}
7877

7978
/// Returns true if an argument effect was added.
8079
private

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import SIL
2424
/// ```
2525
/// are computed.
2626
///
27-
let computeSideEffects = FunctionPass(name: "compute-side-effects", {
27+
let computeSideEffects = FunctionPass(name: "compute-side-effects") {
2828
(function: Function, context: FunctionPassContext) in
2929

3030
if function.isAvailableExternally {
@@ -71,7 +71,7 @@ let computeSideEffects = FunctionPass(name: "compute-side-effects", {
7171
context.modifyEffects(in: function) { (effects: inout FunctionEffects) in
7272
effects.sideEffects = SideEffects(arguments: collectedEffects.argumentEffects, global: collectedEffects.globalEffects)
7373
}
74-
})
74+
}
7575

7676
/// The collected argument and global side effects of the function.
7777
private struct CollectedEffects {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import SIL
3131
/// br continue_bb(%5)
3232
/// continue_bb(%bridgedOptionalSwiftValue):
3333
/// ```
34-
let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt", {
34+
let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt") {
3535
(function: Function, context: FunctionPassContext) in
3636

3737
if !function.hasOwnership { return }
@@ -54,7 +54,7 @@ let objCBridgingOptimization = FunctionPass(name: "objc-bridging-opt", {
5454
}
5555
}
5656
}
57-
})
57+
}
5858

5959
//===----------------------------------------------------------------------===//
6060
// Top-level optimization functions

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ReleaseDevirtualizer.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,42 @@ import SIL
3030
/// The optimization is only done for stack promoted objects because they are
3131
/// known to have no associated objects (which are not explicitly released
3232
/// in the deinit method).
33-
let releaseDevirtualizerPass = FunctionPass(
34-
name: "release-devirtualizer", { function, context in
35-
for block in function.blocks {
36-
// The last `release_value`` or `strong_release`` instruction before the
37-
// deallocation.
38-
var lastRelease: RefCountingInst?
39-
40-
for instruction in block.instructions {
41-
if let release = lastRelease {
42-
// We only do the optimization for stack promoted object, because for
43-
// these we know that they don't have associated objects, which are
44-
// _not_ released by the deinit method.
45-
if let deallocStackRef = instruction as? DeallocStackRefInst {
46-
if !context.continueWithNextSubpassRun(for: release) {
47-
return
48-
}
49-
tryDevirtualizeReleaseOfObject(context, release, deallocStackRef)
50-
lastRelease = nil
51-
continue
33+
let releaseDevirtualizerPass = FunctionPass(name: "release-devirtualizer") {
34+
(function: Function, context: FunctionPassContext) in
35+
36+
for block in function.blocks {
37+
// The last `release_value`` or `strong_release`` instruction before the
38+
// deallocation.
39+
var lastRelease: RefCountingInst?
40+
41+
for instruction in block.instructions {
42+
if let release = lastRelease {
43+
// We only do the optimization for stack promoted object, because for
44+
// these we know that they don't have associated objects, which are
45+
// _not_ released by the deinit method.
46+
if let deallocStackRef = instruction as? DeallocStackRefInst {
47+
if !context.continueWithNextSubpassRun(for: release) {
48+
return
5249
}
50+
tryDevirtualizeReleaseOfObject(context, release, deallocStackRef)
51+
lastRelease = nil
52+
continue
5353
}
54+
}
5455

55-
switch instruction {
56-
case is ReleaseValueInst, is StrongReleaseInst:
57-
lastRelease = instruction as? RefCountingInst
58-
case is DeallocRefInst, is SetDeallocatingInst:
56+
switch instruction {
57+
case is ReleaseValueInst, is StrongReleaseInst:
58+
lastRelease = instruction as? RefCountingInst
59+
case is DeallocRefInst, is SetDeallocatingInst:
60+
lastRelease = nil
61+
default:
62+
if instruction.mayRelease {
5963
lastRelease = nil
60-
default:
61-
if instruction.mayRelease {
62-
lastRelease = nil
63-
}
64-
}
64+
}
6565
}
6666
}
6767
}
68-
)
68+
}
6969

7070
/// Tries to de-virtualize the final release of a stack-promoted object.
7171
private func tryDevirtualizeReleaseOfObject(

SwiftCompilerSources/Sources/Optimizer/ModulePasses/StackProtection.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ private func log(_ message: @autoclosure () -> String) {
3838
/// inter-procedural analysis. If this is not possible and the `enableMoveInoutStackProtection`
3939
/// option is set, the fallback is to move the argument into a temporary `alloc_stack`
4040
/// and do the unsafe pointer operations on the temporary.
41-
let stackProtection = ModulePass(name: "stack-protection", {
42-
(context: ModulePassContext) in
41+
let stackProtection = ModulePass(name: "stack-protection") {
42+
(context: ModulePassContext) in
4343

4444
if !context.options.enableStackProtection {
4545
return
4646
}
4747

4848
var optimization = StackProtectionOptimization(enableMoveInout: context.options.enableMoveInoutStackProtection)
4949
optimization.processModule(context)
50-
})
50+
}
5151

5252
/// The stack-protection optimization on function-level.
5353
///
5454
/// In contrast to the `stack-protection` pass, this pass doesn't do any inter-procedural
5555
/// analysis. It runs at Onone.
56-
let functionStackProtection = FunctionPass(name: "function-stack-protection", {
56+
let functionStackProtection = FunctionPass(name: "function-stack-protection") {
5757
(function: Function, context: FunctionPassContext) in
5858

5959
if !context.options.enableStackProtection {
@@ -62,7 +62,7 @@ let functionStackProtection = FunctionPass(name: "function-stack-protection", {
6262

6363
var optimization = StackProtectionOptimization(enableMoveInout: context.options.enableMoveInoutStackProtection)
6464
optimization.process(function: function, context)
65-
})
65+
}
6666

6767
/// The optimization algorithm.
6868
private struct StackProtectionOptimization {

SwiftCompilerSources/Sources/Optimizer/TestPasses/AccessDumper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SIL
1919
/// dumps anything, but aborts if the result is wrong.
2020
///
2121
/// This pass is used for testing `AccessUtils`.
22-
let accessDumper = FunctionPass(name: "dump-access", {
22+
let accessDumper = FunctionPass(name: "dump-access") {
2323
(function: Function, context: FunctionPassContext) in
2424
print("Accesses for \(function.name)")
2525

@@ -46,7 +46,7 @@ let accessDumper = FunctionPass(name: "dump-access", {
4646
}
4747

4848
print("End accesses for \(function.name)")
49-
})
49+
}
5050

5151
private struct AccessStoragePathVisitor : ValueUseDefWalker {
5252
var walkUpCache = WalkerCache<Path>()

SwiftCompilerSources/Sources/Optimizer/TestPasses/DeadEndBlockDumper.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212

1313
import SIL
1414

15-
let deadEndBlockDumper = FunctionPass(name: "dump-deadendblocks", {
15+
let deadEndBlockDumper = FunctionPass(name: "dump-deadendblocks") {
1616
(function: Function, context: FunctionPassContext) in
1717

18-
1918
print("Function \(function.name)")
2019

2120
var deadEndBlocks = DeadEndBlocks(function: function, context)
2221
print(deadEndBlocks)
2322
defer { deadEndBlocks.deinitialize() }
2423

2524
print("end function \(function.name)")
26-
})
25+
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/EscapeInfoDumper.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SIL
1717
/// Dumps the EscapeInfo query results for all `alloc_stack` instructions in a function.
1818
///
1919
/// This pass is used for testing EscapeInfo.
20-
let escapeInfoDumper = FunctionPass(name: "dump-escape-info", {
20+
let escapeInfoDumper = FunctionPass(name: "dump-escape-info") {
2121
(function: Function, context: FunctionPassContext) in
2222

2323
print("Escape information for \(function.name):")
@@ -60,16 +60,15 @@ let escapeInfoDumper = FunctionPass(name: "dump-escape-info", {
6060
}
6161
}
6262
print("End function \(function.name)\n")
63-
})
64-
63+
}
6564

6665
/// Dumps the results of address-related escape analysis.
6766
///
6867
/// Dumps the EscapeInfo query results for addresses escaping to function calls.
6968
/// The `fix_lifetime` instruction is used as marker for addresses and values to query.
7069
///
7170
/// This pass is used for testing EscapeInfo.
72-
let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info", {
71+
let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info") {
7372
(function: Function, context: FunctionPassContext) in
7473

7574
print("Address escape information for \(function.name):")
@@ -159,4 +158,4 @@ let addressEscapeInfoDumper = FunctionPass(name: "dump-addr-escape-info", {
159158
}
160159

161160
print("End function \(function.name)\n")
162-
})
161+
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/FunctionUsesDumper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SIL
1414

15-
let functionUsesDumper = ModulePass(name: "dump-function-uses", {
15+
let functionUsesDumper = ModulePass(name: "dump-function-uses") {
1616
(context: ModulePassContext) in
1717

1818
var functionUses = FunctionUses()
@@ -25,4 +25,4 @@ let functionUsesDumper = ModulePass(name: "dump-function-uses", {
2525
print(uses)
2626
print("End function \(function.name)\n")
2727
}
28-
})
28+
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/RangeDumper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SIL
1414

15-
let rangeDumper = FunctionPass(name: "dump-ranges", {
15+
let rangeDumper = FunctionPass(name: "dump-ranges") {
1616
(function: Function, context: FunctionPassContext) in
1717

1818
var begin: Instruction?
@@ -73,7 +73,7 @@ let rangeDumper = FunctionPass(name: "dump-ranges", {
7373
assert(!instRange.contains(o))
7474
assert(!instRange.inclusiveRangeContains(o))
7575
}
76-
})
76+
}
7777

7878
private func verify(_ blockRange: BasicBlockRange, _ context: FunctionPassContext) {
7979
var inRange = BasicBlockSet(context)

SwiftCompilerSources/Sources/Optimizer/TestPasses/RunUnitTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import SIL
1414

1515
/// This pass should only be used by sil-opt to run all the unit tests.
1616
///
17-
let runUnitTests = ModulePass(name: "run-unit-tests", {
17+
let runUnitTests = ModulePass(name: "run-unit-tests") {
1818
(context: ModulePassContext) in
1919

2020
print("--- Run unit tests ---")
2121

2222
print("test ProjectionPath")
2323
SmallProjectionPath.runUnitTests()
24-
})
24+
}

0 commit comments

Comments
 (0)