Skip to content

Commit 93ff8b0

Browse files
committed
stdlib: make sure that SetAlgebra.init(sequence) is on the fast path.
In order to fully optimize OptionSet literals, it's important that this function is inlined and fully optimized. So far this was done by chance, but with COW representation it needs a hint to the optimizer.
1 parent 98522b0 commit 93ff8b0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

stdlib/public/core/SetAlgebra.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ extension SetAlgebra {
409409
public init<S: Sequence>(_ sequence: __owned S)
410410
where S.Element == Element {
411411
self.init()
412+
// Needed to fully optimize OptionSet literals.
413+
_onFastPath()
412414
for e in sequence { insert(e) }
413415
}
414416

test/SILOptimizer/optionset.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public struct TestOptions: OptionSet {
1414

1515
// CHECK: sil @{{.*}}returnTestOptions{{.*}}
1616
// CHECK-NEXT: bb0:
17+
// CHECK-NEXT: builtin
1718
// CHECK-NEXT: integer_literal {{.*}}, 15
1819
// CHECK-NEXT: struct $Int
1920
// CHECK-NEXT: struct $TestOptions
@@ -22,18 +23,19 @@ public func returnTestOptions() -> TestOptions {
2223
return [.first, .second, .third, .fourth]
2324
}
2425

25-
// CHECK: sil @{{.*}}returnEmptyTestOptions{{.*}}
26-
// CHECK-NEXT: bb0:
27-
// CHECK-NEXT: integer_literal {{.*}}, 0
28-
// CHECK-NEXT: struct $Int
29-
// CHECK-NEXT: struct $TestOptions
30-
// CHECK-NEXT: return
26+
// CHECK: sil @{{.*}}returnEmptyTestOptions{{.*}}
27+
// CHECK: [[ZERO:%[0-9]+]] = integer_literal {{.*}}, 0
28+
// CHECK: [[ZEROINT:%[0-9]+]] = struct $Int ([[ZERO]]
29+
// CHECK: [[TO:%[0-9]+]] = struct $TestOptions ([[ZEROINT]]
30+
// CHECK: return [[TO]]
31+
// CHECK: } // end sil function {{.*}}returnEmptyTestOptions{{.*}}
3132
public func returnEmptyTestOptions() -> TestOptions {
3233
return []
3334
}
3435

3536
// CHECK: alloc_global @{{.*}}globalTestOptions{{.*}}
3637
// CHECK-NEXT: global_addr
38+
// CHECK-NEXT: builtin
3739
// CHECK-NEXT: integer_literal {{.*}}, 15
3840
// CHECK-NEXT: struct $Int
3941
// CHECK-NEXT: struct $TestOptions

0 commit comments

Comments
 (0)