Skip to content

Commit 62e2871

Browse files
committed
MandatoryPerformanceOptimizations: run DeadStoreElimination for global init functions.
Cleanup leftovers from simplification. The InitializeStaticGlobals pass cannot deal with dead stores in global init functions.
1 parent 794964a commit 62e2871

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/DeadStoreElimination.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ import SIL
5353
let deadStoreElimination = FunctionPass(name: "dead-store-elimination") {
5454
(function: Function, context: FunctionPassContext) in
5555

56+
eliminateDeadStores(in: function, context)
57+
}
58+
59+
func eliminateDeadStores(in function: Function, _ context: FunctionPassContext) {
5660
// Avoid quadratic complexity by limiting the number of visited instructions.
5761
// This limit is sufficient for most "real-world" functions, by far.
5862
var complexityBudget = 10_000

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
219219

220220
changed = context.eliminateDeadAllocations(in: function) || changed
221221
}
222+
223+
if function.isGlobalInitOnceFunction {
224+
// Cleanup leftovers from simplification. The InitializeStaticGlobals pass cannot deal with dead
225+
// stores in global init functions.
226+
eliminateDeadStores(in: function, context)
227+
}
222228
}
223229

224230
private func inlineAndDevirtualize(apply: FullApplySite, alreadyInlinedFunctions: inout Set<PathFunctionTuple>,

test/SILOptimizer/mandatory_performance_optimizations.sil

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct NonTrivialStruct {
1515

1616
sil_global [let] @g1 : $Int32
1717
sil_global [let] @g2 : $Int32
18+
sil_global [let] @g3 : $Int32
1819

1920
sil @paable : $@convention(thin) (Builtin.Int64) -> ()
2021
sil @moved_pai_callee : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
@@ -161,6 +162,25 @@ bb0:
161162
return %6 : $()
162163
}
163164

165+
// CHECK-LABEL: sil [global_init_once_fn] [no_locks] [perf_constraint] [ossa] @remove_loads_and_stores_in_globalinit :
166+
// CHECK-NOT: store
167+
// CHECK: debug_step
168+
// CHECK-NOT: load
169+
// CHECK: } // end sil function 'remove_loads_and_stores_in_globalinit'
170+
sil [global_init_once_fn] [no_locks] [ossa] @remove_loads_and_stores_in_globalinit : $@convention(c) () -> () {
171+
bb0:
172+
alloc_global @g3
173+
%1 = global_addr @g3 : $*Int32
174+
%2 = integer_literal $Builtin.Int32, 10
175+
%3 = struct $Int32 (%2)
176+
store %3 to [trivial] %1
177+
debug_step
178+
%6 = load [trivial] %1
179+
store %6 to [trivial] %1
180+
%r = tuple ()
181+
return %r
182+
}
183+
164184
// Check that we don't crash on global init-once declarations.
165185

166186
// CHECK-LABEL: sil [global_init_once_fn] [no_locks] @external_global_init_once : $@convention(c) () -> ()

test/SILOptimizer/performance-annotations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct Str : P {
2222
return a + x
2323
}
2424

25-
static let s = 27
25+
static let s = 27 << 0
2626
static var s2 = 10 + s
2727
static var s3 = initFunc() // expected-error {{global/static variable initialization can cause locking}}
2828
}

0 commit comments

Comments
 (0)