Skip to content

Commit e50daa6

Browse files
committed
Shuffle around some of the optimization passes.
The end goal here is to end up with a good pass ordering that will allow us to only run one set of these passes, rather than running them twice. This is a start in that direction. No real impact measured on compile times as of this change. On benchmarks I see a mix of regressions and improvements. -O improvements: Calculator -17.6% 1.21x Chars -54.4% 2.19x PolymorphicCalls -14.7% 1.17x SetIsSubsetOf -14.1% 1.16x Sim2DArray -14.1% 1.16x StrToInt -30.4% 1.44x -O regressions: CaptureProp +32.9% 0.75x DictionarySwap +36.0% 0.74x XorLoop +39.8% 0.72x -Ounchecked improvements: Chars -58.0% 2.38x -Ounchecked regressions: CaptureProp +33.3% 0.75x -Onone improvements: StrToInt -14.9% 1.18x StringWalk -47.6% 1.91x StringWithCString -17.2% 1.21x (many more smaller improvements) -Onone regressions: Calculator +21.5% 0.82x OpenClose +10.1% 0.91x
1 parent 0a893c1 commit e50daa6

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

lib/SILOptimizer/PassManager/Passes.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -172,36 +172,25 @@ void AddHighLevelLoopOptPasses(SILPassManager &PM) {
172172
PM.addSwiftArrayOpts();
173173
}
174174

175+
// Perform classic SSA optimizations.
175176
void AddSSAPasses(SILPassManager &PM, OptimizationLevelKind OpLevel) {
176-
AddSimplifyCFGSILCombine(PM);
177+
// Promote box allocations to stack allocations.
177178
PM.addAllocBoxToStack();
179+
180+
// Propagate copies through stack locations. Should run after
181+
// box-to-stack promotion since it is limited to propagating through
182+
// stack locations. Should run before aggregate lowering since that
183+
// splits up copy_addr.
178184
PM.addCopyForwarding();
179-
PM.addLowerAggregateInstrs();
180-
PM.addSILCombine();
181-
PM.addSROA();
182-
PM.addMem2Reg();
183185

184-
// Perform classic SSA optimizations.
185-
PM.addGlobalOpt();
186-
PM.addLetPropertiesOpt();
187-
PM.addPerformanceConstantPropagation();
188-
PM.addDCE();
189-
PM.addCSE();
190-
PM.addSILCombine();
191-
PM.addJumpThreadSimplifyCFG();
192-
// Jump threading can expose opportunity for silcombine (enum -> is_enum_tag->
193-
// cond_br).
194-
PM.addSILCombine();
195-
// Which can expose opportunity for simplifcfg.
196-
PM.addSimplifyCFG();
186+
// Split up opaque operations (copy_addr, retain_value, etc.).
187+
PM.addLowerAggregateInstrs();
197188

198-
// Perform retain/release code motion and run the first ARC optimizer.
199-
PM.addRedundantLoadElimination();
200-
PM.addCSE();
201-
PM.addEarlyCodeMotion();
202-
PM.addARCSequenceOpts();
189+
// Split up operations on stack-allocated aggregates (struct, tuple).
190+
PM.addSROA();
203191

204-
PM.addSILLinker();
192+
// Promote stack allocations to values.
193+
PM.addMem2Reg();
205194

206195
// Run the devirtualizer, specializer, and inliner. If any of these
207196
// makes a change we'll end up restarting the function passes on the
@@ -217,12 +206,37 @@ void AddSSAPasses(SILPassManager &PM, OptimizationLevelKind OpLevel) {
217206
// Does inline semantics-functions (except "availability"), but not
218207
// global-init functions.
219208
PM.addPerfInliner();
209+
PM.addGlobalOpt();
210+
PM.addLetPropertiesOpt();
220211
break;
221212
case OptimizationLevelKind::LowLevel:
222213
// Inlines everything
223214
PM.addLateInliner();
224215
break;
225216
}
217+
218+
PM.addMem2Reg();
219+
AddSimplifyCFGSILCombine(PM);
220+
221+
PM.addPerformanceConstantPropagation();
222+
PM.addDCE();
223+
PM.addCSE();
224+
PM.addSILCombine();
225+
PM.addJumpThreadSimplifyCFG();
226+
// Jump threading can expose opportunity for silcombine (enum -> is_enum_tag->
227+
// cond_br).
228+
PM.addSILCombine();
229+
// Which can expose opportunity for simplifcfg.
230+
PM.addSimplifyCFG();
231+
232+
// Perform retain/release code motion and run the first ARC optimizer.
233+
PM.addRedundantLoadElimination();
234+
PM.addCSE();
235+
PM.addEarlyCodeMotion();
236+
PM.addARCSequenceOpts();
237+
238+
PM.addSILLinker();
239+
226240
PM.addSimplifyCFG();
227241
// Only hoist releases very late.
228242
if (OpLevel == OptimizationLevelKind::LowLevel)

test/SILOptimizer/devirt_single_module_in_multiple_files.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public func test() {
66
e.evaluate(1)
77
}
88

9-
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTSf3cpfr69_TFFC38devirt_single_module_in_multiple_files9EvaluatorcFT_S0_U_FT_Si_n_n___TTRXFo__dSi_XFo_iT__iSi_
9+
// CHECK-LABEL: sil shared @_TFFC38devirt_single_module_in_multiple_files9Evaluatorc
1010
// CHECK: %{{.*}} = class_method %{{.*}} : $Problem1, #Problem1.run!1 : (Problem1) -> () -> Int , $@convention(method) (@guaranteed Problem1) -> Int
1111
// CHECK-NEXT: apply
1212
// CHECK: return
1313

14-
// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTSf3cpfr70_TFFC38devirt_single_module_in_multiple_files9EvaluatorcFT_S0_U0_FT_Si_n_n___TTRXFo__dSi_XFo_iT__iSi_
14+
// CHECK-LABEL: sil shared @_TFFC38devirt_single_module_in_multiple_files9Evaluatorc
1515
// CHECK: %{{.*}} = class_method %{{.*}} : $Problem2, #Problem2.run!1 : (Problem2) -> () -> Int , $@convention(method) (@guaranteed Problem2) -> Int
1616
// CHECK-NEXT: apply
1717
// CHECK: return

0 commit comments

Comments
 (0)