Skip to content

Commit d0f5f79

Browse files
authored
Merge pull request #83636 from eeckstein/optionset-insert
stdlib: specialize `OptionSet.insert` for FixedWidthInteger raw-values
2 parents ab5fc57 + e28125b commit d0f5f79

File tree

3 files changed

+964
-860
lines changed

3 files changed

+964
-860
lines changed

stdlib/public/core/OptionSet.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,15 @@ extension OptionSet where RawValue: FixedWidthInteger {
374374
self = Self(rawValue: self.rawValue ^ other.rawValue)
375375
}
376376
}
377+
378+
extension OptionSet where RawValue: FixedWidthInteger, Element == Self {
379+
@_alwaysEmitIntoClient
380+
@discardableResult
381+
public mutating func insert(
382+
_ newMember: Element
383+
) -> (inserted: Bool, memberAfterInsert: Element) {
384+
let inserted = !self.contains(newMember)
385+
self = Self(rawValue: self.rawValue | newMember.rawValue)
386+
return (inserted, newMember)
387+
}
388+
}

test/SILOptimizer/optionset.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -O -sil-verify-all -module-name=test -Xllvm -sil-print-types -emit-sil | grep -v debug_value | %FileCheck %s
2-
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -Osize -sil-verify-all -module-name=test -Xllvm -sil-print-types -emit-sil | grep -v debug_value | %FileCheck %s
1+
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -O -sil-verify-all -module-name=test -emit-sil | grep -v debug_value | %FileCheck %s
2+
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -Osize -sil-verify-all -module-name=test -emit-sil | grep -v debug_value | %FileCheck %s
33
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
44
// REQUIRES: swift_in_compiler
55

@@ -15,8 +15,8 @@ public struct TestOptions: OptionSet {
1515

1616
// CHECK-LABEL: sil_global hidden [let] @$s4test17globalTestOptionsAA0cD0Vvp : $TestOptions = {
1717
// CHECK: [[CONST:%.*]] = integer_literal $Builtin.Int{{32|64}}, 15
18-
// CHECK: [[INT:%.*]] = struct $Int (%0 : $Builtin.Int{{32|64}})
19-
// CHECK: %initval = struct $TestOptions ([[INT]] : $Int)
18+
// CHECK: [[INT:%.*]] = struct $Int (%0)
19+
// CHECK: %initval = struct $TestOptions ([[INT]])
2020
let globalTestOptions: TestOptions = [.first, .second, .third, .fourth]
2121

2222
// CHECK-LABEL: sil @$s4test17returnTestOptionsAA0cD0VyF
@@ -33,12 +33,22 @@ public func returnTestOptions() -> TestOptions {
3333

3434
// CHECK-LABEL: sil @$s4test22returnEmptyTestOptionsAA0dE0VyF
3535
// CHECK: bb0:
36-
// CHECK-NEXT: integer_literal {{.*}}, 0
37-
// CHECK-NEXT: struct $Int
38-
// CHECK: builtin "onFastPath"() : $()
39-
// CHECK-NEXT: struct $TestOptions
40-
// CHECK-NEXT: return
36+
// CHECK-NEXT: [[ZERO:%.*]] = integer_literal {{.*}}, 0
37+
// CHECK: [[I:%.*]] = struct $Int ([[ZERO]]
38+
// CHECK: [[T:%.*]] = struct $TestOptions
39+
// CHECK: return [[T]]
4140
// CHECK: } // end sil function '$s4test22returnEmptyTestOptionsAA0dE0VyF'
4241
public func returnEmptyTestOptions() -> TestOptions {
4342
return []
4443
}
44+
45+
extension TestOptions {
46+
// CHECK-LABEL: sil @$s4test11TestOptionsV12insertSecondyyF :
47+
// CHECK-NOT: bb1
48+
// CHECK: builtin "or
49+
// CHECK-NOT: bb1
50+
// CHECK: } // end sil function '$s4test11TestOptionsV12insertSecondyyF'
51+
public mutating func insertSecond() {
52+
insert(.second)
53+
}
54+
}

0 commit comments

Comments
 (0)