Skip to content

Commit f83fb1b

Browse files
committed
Optimizer: add FunctionWorklist and CrossFunctionValueWorklist
Originally, `FunctionWorklist` was a private utility in MandatoryPerformanceOptimizations. Moved this to `Worklist.swift` to make it generally available. Also, simplify the `pop()` function which changes the popping order - therefore some test changes were necessary.
1 parent 40363d8 commit f83fb1b

File tree

4 files changed

+76
-34
lines changed

4 files changed

+76
-34
lines changed

SwiftCompilerSources/Sources/Optimizer/DataStructures/Worklist.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,63 @@ extension InstructionWorklist {
110110
}
111111
}
112112

113+
/// A worklist for `Function`s.
114+
struct FunctionWorklist {
115+
116+
// The current functions in the worklist.
117+
private(set) var functions = Array<Function>()
118+
119+
// All functions which were ever pushed to the worklist.
120+
private var pushedFunctions = Set<Function>()
121+
122+
mutating func pushIfNotVisited(_ function: Function) {
123+
if pushedFunctions.insert(function).inserted {
124+
functions.append(function)
125+
}
126+
}
127+
128+
mutating func pushIfNotVisited(contentsOf functions: [Function]) {
129+
for f in functions {
130+
pushIfNotVisited(f)
131+
}
132+
}
133+
134+
mutating func pop() -> Function? {
135+
return functions.popLast()
136+
}
137+
}
138+
139+
/// Like `ValueWorklist`, but allows pushing `Value`s from different functions -
140+
/// at the cost of a less efficient implementation.
141+
struct CrossFunctionValueWorklist {
142+
143+
// The current values in the worklist.
144+
private(set) var values = Array<Value>()
145+
146+
// All values which were ever pushed to the worklist.
147+
private var pushedValues = Set<ObjectIdentifier>(minimumCapacity: 8)
148+
149+
init() {
150+
values.reserveCapacity(8)
151+
}
152+
153+
mutating func pop() -> Value? {
154+
return values.popLast()
155+
}
156+
157+
mutating func pushIfNotVisited(_ value: Value) {
158+
if pushedValues.insert(ObjectIdentifier(value)).inserted {
159+
values.append(value)
160+
}
161+
}
162+
163+
mutating func pushIfNotVisited<S: Sequence>(contentsOf values: S) where S.Element == Value {
164+
for value in values {
165+
pushIfNotVisited(value)
166+
}
167+
}
168+
169+
func hasBeenPushed(_ value: Value) -> Bool {
170+
return pushedValues.contains(ObjectIdentifier(value))
171+
}
172+
}

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -439,20 +439,7 @@ private extension Value {
439439
}
440440
}
441441

442-
fileprivate struct FunctionWorklist {
443-
private(set) var functions = Array<Function>()
444-
private var pushedFunctions = Set<Function>()
445-
private var currentIndex = 0
446-
447-
mutating func pop() -> Function? {
448-
if currentIndex < functions.count {
449-
let f = functions[currentIndex]
450-
currentIndex += 1
451-
return f
452-
}
453-
return nil
454-
}
455-
442+
extension FunctionWorklist {
456443
mutating func addAllMandatoryRequiredFunctions(of moduleContext: ModulePassContext) {
457444
for f in moduleContext.functions {
458445
// Performance annotated functions
@@ -522,10 +509,4 @@ fileprivate struct FunctionWorklist {
522509
}
523510
}
524511
}
525-
526-
mutating func pushIfNotVisited(_ element: Function) {
527-
if pushedFunctions.insert(element).inserted {
528-
functions.append(element)
529-
}
530-
}
531512
}

test/SILOptimizer/mandatory_performance_optimizations.sil

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,17 @@ bb0(%0 : $Int, %1 : $@thick Int.Type, %2 : @owned $Builtin.NativeObject):
250250
return %2 : $Builtin.NativeObject
251251
}
252252

253-
// CHECK-LABEL: sil [perf_constraint] [ossa] @$s12metatype_argTf4dnn_n : $@convention(thin) (Int, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
253+
// CHECK-LABEL: sil [perf_constraint] [ossa] @$s19metatype_arg_throwsTf4ndn_n : $@convention(thin) (Int, @owned Builtin.NativeObject) -> (@owned Builtin.NativeObject, @error any Error) {
254254
// CHECK: bb0(%0 : $Int, %1 : @owned $Builtin.NativeObject):
255255
// CHECK: %2 = metatype $@thick Int.Type
256256
// CHECK: fix_lifetime %2 : $@thick Int.Type
257257
// CHECK: return %1 : $Builtin.NativeObject
258-
// CHECK: } // end sil function '$s12metatype_argTf4dnn_n'
258+
// CHECK: } // end sil function '$s19metatype_arg_throwsTf4ndn_n'
259259

260-
// CHECK-LABEL: sil [perf_constraint] [ossa] @$s19metatype_arg_throwsTf4dnn_n : $@convention(thin) (Int, @owned Builtin.NativeObject) -> (@owned Builtin.NativeObject, @error any Error) {
260+
// CHECK-LABEL: sil [perf_constraint] [ossa] @$s12metatype_argTf4ndn_n : $@convention(thin) (Int, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
261261
// CHECK: bb0(%0 : $Int, %1 : @owned $Builtin.NativeObject):
262262
// CHECK: %2 = metatype $@thick Int.Type
263263
// CHECK: fix_lifetime %2 : $@thick Int.Type
264264
// CHECK: return %1 : $Builtin.NativeObject
265-
// CHECK: } // end sil function '$s19metatype_arg_throwsTf4dnn_n'
265+
// CHECK: } // end sil function '$s12metatype_argTf4ndn_n'
266+

test/embedded/classes-generic-no-stdlib.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ public func bar(t: T2) -> MyClass<T2> {
3434
// CHECK-SIL-DAG: sil {{.*}}@$e4main7MyClassC1tACyxGx_tcfCAA2T2V_Tt0g5 {{.*}}{
3535
// CHECK-SIL-DAG: sil {{.*}}@$e4main7MyClassCfDAA2T2V_Tg5 {{.*}}{
3636

37-
// CHECK-SIL: sil_vtable MyClass {
38-
// CHECK-SIL: #MyClass.t!getter: <T> (MyClass<T>) -> () -> T : @$e4main7MyClassC1txvgAA2T1V_Tg5 // specialized MyClass.t.getter
39-
// CHECK-SIL: #MyClass.t!setter: <T> (MyClass<T>) -> (T) -> () : @$e4main7MyClassC1txvsAA2T1V_Tg5 // specialized MyClass.t.setter
40-
// CHECK-SIL: #MyClass.t!modify: <T> (MyClass<T>) -> () -> () : @$e4main7MyClassC1txvMAA2T1V_Tg5 // specialized MyClass.t.modify
41-
// CHECK-SIL: #MyClass.init!allocator: <T> (MyClass<T>.Type) -> (T) -> MyClass<T> : @$e4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5 // specialized MyClass.__allocating_init(t:)
42-
// CHECK-SIL: #MyClass.deinit!deallocator: @$e4main7MyClassCfDAA2T1V_Tg5 // specialized MyClass.__deallocating_deinit
43-
// CHECK-SIL: }
44-
4537
// CHECK-SIL: sil_vtable $MyClass<T2> {
4638
// CHECK-SIL: #MyClass.t!getter: <T> (MyClass<T>) -> () -> T : @$e4main7MyClassC1txvgAA2T2V_Tg5 // specialized MyClass.t.getter
4739
// CHECK-SIL: #MyClass.t!setter: <T> (MyClass<T>) -> (T) -> () : @$e4main7MyClassC1txvsAA2T2V_Tg5 // specialized MyClass.t.setter
@@ -50,9 +42,17 @@ public func bar(t: T2) -> MyClass<T2> {
5042
// CHECK-SIL: #MyClass.deinit!deallocator: @$e4main7MyClassCfDAA2T2V_Tg5 // specialized MyClass.__deallocating_deinit
5143
// CHECK-SIL: }
5244

45+
// CHECK-SIL: sil_vtable $MyClass<T1> {
46+
// CHECK-SIL: #MyClass.t!getter: <T> (MyClass<T>) -> () -> T : @$e4main7MyClassC1txvgAA2T1V_Tg5 // specialized MyClass.t.getter
47+
// CHECK-SIL: #MyClass.t!setter: <T> (MyClass<T>) -> (T) -> () : @$e4main7MyClassC1txvsAA2T1V_Tg5 // specialized MyClass.t.setter
48+
// CHECK-SIL: #MyClass.t!modify: <T> (MyClass<T>) -> () -> () : @$e4main7MyClassC1txvMAA2T1V_Tg5 // specialized MyClass.t.modify
49+
// CHECK-SIL: #MyClass.init!allocator: <T> (MyClass<T>.Type) -> (T) -> MyClass<T> : @$e4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5 // specialized MyClass.__allocating_init(t:)
50+
// CHECK-SIL: #MyClass.deinit!deallocator: @$e4main7MyClassCfDAA2T1V_Tg5 // specialized MyClass.__deallocating_deinit
51+
// CHECK-SIL: }
52+
5353

54-
// CHECK-IR: @"$e4main7MyClassCyAA2T2VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$e4main7MyClassCfDAA2T2V_Tg5", ptr null, ptr @"$e4main7MyClassC1txvgAA2T2V_Tg5", ptr @"$e4main7MyClassC1txvsAA2T2V_Tg5", ptr @"$e4main7MyClassC1txvMAA2T2V_Tg5", ptr @"$e4main7MyClassC1tACyxGx_tcfCAA2T2V_Tg5" }>
55-
// CHECK-IR: @"$e4main7MyClassCyAA2T1VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$e4main7MyClassCfDAA2T1V_Tg5", ptr null, ptr @"$e4main7MyClassC1txvgAA2T1V_Tg5", ptr @"$e4main7MyClassC1txvsAA2T1V_Tg5", ptr @"$e4main7MyClassC1txvMAA2T1V_Tg5", ptr @"$e4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5" }>
54+
// CHECK-IR-DAG: @"$e4main7MyClassCyAA2T2VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$e4main7MyClassCfDAA2T2V_Tg5", ptr null, ptr @"$e4main7MyClassC1txvgAA2T2V_Tg5", ptr @"$e4main7MyClassC1txvsAA2T2V_Tg5", ptr @"$e4main7MyClassC1txvMAA2T2V_Tg5", ptr @"$e4main7MyClassC1tACyxGx_tcfCAA2T2V_Tg5" }>
55+
// CHECK-IR-DAG: @"$e4main7MyClassCyAA2T1VGN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$e4main7MyClassCfDAA2T1V_Tg5", ptr null, ptr @"$e4main7MyClassC1txvgAA2T1V_Tg5", ptr @"$e4main7MyClassC1txvsAA2T1V_Tg5", ptr @"$e4main7MyClassC1txvMAA2T1V_Tg5", ptr @"$e4main7MyClassC1tACyxGx_tcfCAA2T1V_Tg5" }>
5656

5757
// CHECK-IR-DAG: define {{.*}}void @"$e4main7MyClassC1txvgAA2T1V_Tg5"(ptr swiftself %0)
5858
// CHECK-IR-DAG: define {{.*}}i1 @"$e4main7MyClassC1txvgAA2T2V_Tg5"(ptr swiftself %0)

0 commit comments

Comments
 (0)