Skip to content

Commit e74a7ec

Browse files
authored
Merge pull request #84979 from eeckstein/global-init-optimizations
Optimizer: some small (mandatory) optimization improvements for static initialization of globals
2 parents a42fb1e + 62e2871 commit e74a7ec

File tree

9 files changed

+62
-4
lines changed

9 files changed

+62
-4
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ struct AliasAnalysis {
220220
is StrongCopyUnmanagedValueInst,
221221
is StrongCopyWeakValueInst,
222222
is BeginBorrowInst,
223-
is BeginCOWMutationInst:
223+
is BeginCOWMutationInst,
224+
is DebugStepInst:
224225
return .noEffects
225226

226227
case let load as LoadInst:

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/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private extension LoadingInstruction {
210210
return false
211211
}
212212
switch address.accessBase {
213-
case .box, .stack:
213+
case .box, .stack, .global:
214214
break
215215
default:
216216
return false

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>,

SwiftCompilerSources/Sources/Optimizer/TestPasses/MemBehaviorDumper.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ private extension Instruction {
7777
is StoreBorrowInst,
7878
is MarkDependenceInst,
7979
is MarkDependenceAddrInst,
80-
is DebugValueInst:
80+
is DebugValueInst,
81+
is DebugStepInst:
8182
return true
8283
default:
8384
return false

test/SILOptimizer/mandatory-redundant-load-elim.sil

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,3 +1922,17 @@ bb0(%0 : $MyInt, %1 : @guaranteed $Foo, %2 : @guaranteed $Baz):
19221922
dealloc_stack %3 : $*MyInt
19231923
return %38 : $MyInt
19241924
} // end sil function '$s27capture_promotion_ownership05test_a1_B0SiycyFSiycfU_Tf2iii_n'
1925+
1926+
sil_global @gi : $Int
1927+
1928+
// CHECK: sil [ossa] @test_global :
1929+
// CHECK-NOT: load
1930+
// CHECK: return %0
1931+
// CHECK: } // end sil function 'test_global'
1932+
sil [ossa] @test_global : $@convention(thin) (Int) -> Int {
1933+
bb0(%0 : $Int):
1934+
%1 = global_addr @gi : $*Int
1935+
store %0 to [trivial] %1
1936+
%3 = load [trivial] %1
1937+
return %3
1938+
}

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/mem-behavior.sil

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,18 @@ bb0:
12661266
return %r : $()
12671267
}
12681268

1269+
// CHECK-LABEL: @test_debug_step
1270+
// CHECK: PAIR #0.
1271+
// CHECK-NEXT: debug_step
1272+
// CHECK-NEXT: %0 = argument of bb0
1273+
// CHECK-NEXT: r=0,w=0
1274+
sil [ossa] @test_debug_step : $@convention(thin) (@inout_aliasable Int) -> () {
1275+
bb0(%0 : $*Int):
1276+
debug_step
1277+
%99 = tuple ()
1278+
return %99 : $()
1279+
}
1280+
12691281
// CHECK-LABEL: @test_store_assign
12701282
// CHECK: PAIR #0.
12711283
// CHECK-NEXT: store %2 to [assign] %0 : $*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)