Skip to content

Commit 2aa5b49

Browse files
authored
Merge pull request #20159 from lorentey/bridgestorage-audit
[stdlib] ABI audit for _BridgeStorage
2 parents 25a4d1e + 6593817 commit 2aa5b49

File tree

6 files changed

+100
-122
lines changed

6 files changed

+100
-122
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
4949
return _ArrayBuffer<U>(storage: _storage)
5050
}
5151

52-
/// The spare bits that are set when a native array needs deferred
53-
/// element type checking.
54-
@inlinable
55-
internal var deferredTypeCheckMask: Int { return 1 }
56-
5752
/// Returns an `_ArrayBuffer<U>` containing the same elements,
5853
/// deferring checking each element's `U`-ness until it is accessed.
5954
///
@@ -71,8 +66,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
7166
// _sanityCheck(U.self is Element.Type)
7267

7368
return _ArrayBuffer<U>(
74-
storage: _ArrayBridgeStorage(
75-
native: _native._storage, bits: deferredTypeCheckMask))
69+
storage: _ArrayBridgeStorage(native: _native._storage, isFlagged: true))
7670
}
7771

7872
@inlinable
@@ -109,7 +103,7 @@ extension _ArrayBuffer {
109103
@inlinable
110104
internal mutating func isUniquelyReferenced() -> Bool {
111105
if !_isClassOrObjCExistential(Element.self) {
112-
return _storage.isUniquelyReferenced_native_noSpareBits()
106+
return _storage.isUniquelyReferencedUnflaggedNative()
113107
}
114108

115109
// This is a performance optimization. This code used to be:
@@ -532,7 +526,7 @@ extension _ArrayBuffer {
532526
if !_isClassOrObjCExistential(Element.self) {
533527
return true
534528
} else {
535-
return _storage.isNativeWithClearedSpareBits(deferredTypeCheckMask)
529+
return _storage.isUnflaggedNative
536530
}
537531
}
538532

@@ -543,15 +537,15 @@ extension _ArrayBuffer {
543537
internal var _native: NativeBuffer {
544538
return NativeBuffer(
545539
_isClassOrObjCExistential(Element.self)
546-
? _storage.nativeInstance : _storage.nativeInstance_noSpareBits)
540+
? _storage.nativeInstance : _storage.unflaggedNativeInstance)
547541
}
548542

549543
/// Fast access to the native representation.
550544
///
551545
/// - Precondition: `_isNativeTypeChecked`.
552546
@inlinable
553547
internal var _nativeTypeChecked: NativeBuffer {
554-
return NativeBuffer(_storage.nativeInstance_noSpareBits)
548+
return NativeBuffer(_storage.unflaggedNativeInstance)
555549
}
556550

557551
@inlinable

stdlib/public/core/BridgeStorage.swift

Lines changed: 60 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,45 @@
2121
import SwiftShims
2222

2323
@_fixed_layout
24-
public // @testable
25-
struct _BridgeStorage<
26-
NativeClass: AnyObject, ObjCClass: AnyObject
24+
@usableFromInline
25+
internal struct _BridgeStorage<
26+
NativeClass: AnyObject,
27+
ObjCClass: AnyObject
2728
> {
28-
public // @testable
29-
typealias Native = NativeClass
30-
31-
public // @testable
32-
typealias ObjC = ObjCClass
33-
34-
@inlinable // FIXME(sil-serialize-all)
29+
@usableFromInline
30+
internal typealias Native = NativeClass
31+
32+
@usableFromInline
33+
internal typealias ObjC = ObjCClass
34+
35+
// rawValue is passed inout to _isUnique. Although its value
36+
// is unchanged, it must appear mutable to the optimizer.
37+
@usableFromInline
38+
internal var rawValue: Builtin.BridgeObject
39+
40+
@inlinable
3541
@inline(__always)
36-
public // @testable
37-
init(native: Native, bits: Int) {
42+
internal init(native: Native, isFlagged flag: Bool) {
43+
// Note: Some platforms provide more than one spare bit, but the minimum is
44+
// a single bit.
45+
3846
_sanityCheck(_usesNativeSwiftReferenceCounting(NativeClass.self))
39-
40-
// More bits are available on some platforms, but it's not portable
41-
_sanityCheck(0...1 ~= bits,
42-
"BridgeStorage can't store bits outside the range 0...1")
4347

4448
rawValue = _makeNativeBridgeObject(
45-
native, UInt(bits) << _objectPointerLowSpareBitShift)
49+
native,
50+
flag ? (1 as UInt) << _objectPointerLowSpareBitShift : 0)
4651
}
47-
48-
@inlinable // FIXME(sil-serialize-all)
52+
53+
@inlinable
4954
@inline(__always)
50-
public // @testable
51-
init(objC: ObjC) {
55+
internal init(objC: ObjC) {
5256
_sanityCheck(_usesNativeSwiftReferenceCounting(NativeClass.self))
5357
rawValue = _makeObjCBridgeObject(objC)
5458
}
55-
56-
@inlinable // FIXME(sil-serialize-all)
59+
60+
@inlinable
5761
@inline(__always)
58-
public // @testable
59-
init(native: Native) {
62+
internal init(native: Native) {
6063
_sanityCheck(_usesNativeSwiftReferenceCounting(NativeClass.self))
6164
rawValue = Builtin.reinterpretCast(native)
6265
}
@@ -69,97 +72,73 @@ struct _BridgeStorage<
6972
}
7073
#endif
7174

72-
@inlinable // FIXME(sil-serialize-all)
73-
public // @testable
74-
var spareBits: Int {
75-
@inline(__always) get {
76-
_sanityCheck(isNative)
77-
return Int(
78-
_nonPointerBits(rawValue) >> _objectPointerLowSpareBitShift)
79-
}
80-
}
81-
82-
@inlinable // FIXME(sil-serialize-all)
75+
@inlinable
8376
@inline(__always)
84-
public // @testable
85-
mutating func isUniquelyReferencedNative() -> Bool {
77+
internal mutating func isUniquelyReferencedNative() -> Bool {
8678
return _isUnique(&rawValue)
8779
}
8880

89-
@inlinable // FIXME(sil-serialize-all)
90-
public // @testable
91-
var isNative: Bool {
81+
@inlinable
82+
internal var isNative: Bool {
9283
@inline(__always) get {
9384
let result = Builtin.classifyBridgeObject(rawValue)
9485
return !Bool(Builtin.or_Int1(result.isObjCObject,
9586
result.isObjCTaggedPointer))
9687
}
9788
}
98-
99-
@inlinable // FIXME(sil-serialize-all)
100-
@inline(__always)
101-
public // @testable
102-
func isNativeWithClearedSpareBits(_ bits: Int) -> Bool {
103-
return (_bitPattern(rawValue) &
104-
(_bridgeObjectTaggedPointerBits | _objCTaggedPointerBits |
105-
_objectPointerIsObjCBit |
106-
(UInt(bits)) << _objectPointerLowSpareBitShift)) == 0
89+
90+
@inlinable
91+
static var flagMask: UInt {
92+
@inline(__always) get {
93+
return (1 as UInt) << _objectPointerLowSpareBitShift
94+
}
95+
}
96+
97+
@inlinable
98+
internal var isUnflaggedNative: Bool {
99+
@inline(__always) get {
100+
return (_bitPattern(rawValue) &
101+
(_bridgeObjectTaggedPointerBits | _objCTaggedPointerBits |
102+
_objectPointerIsObjCBit | _BridgeStorage.flagMask)) == 0
103+
}
107104
}
108105

109-
@inlinable // FIXME(sil-serialize-all)
110-
public // @testable
111-
var isObjC: Bool {
106+
@inlinable
107+
internal var isObjC: Bool {
112108
@inline(__always) get {
113109
return !isNative
114110
}
115111
}
116-
117-
@inlinable // FIXME(sil-serialize-all)
118-
public // @testable
119-
var nativeInstance: Native {
112+
113+
@inlinable
114+
internal var nativeInstance: Native {
120115
@inline(__always) get {
121116
_sanityCheck(isNative)
122117
return Builtin.castReferenceFromBridgeObject(rawValue)
123118
}
124119
}
125-
126-
@inlinable // FIXME(sil-serialize-all)
127-
public // @testable
128-
var nativeInstance_noSpareBits: Native {
120+
121+
@inlinable
122+
internal var unflaggedNativeInstance: Native {
129123
@inline(__always) get {
130124
_sanityCheck(isNative)
131125
_sanityCheck(_nonPointerBits(rawValue) == 0)
132126
return Builtin.reinterpretCast(rawValue)
133127
}
134128
}
135-
136-
@inlinable // FIXME(sil-serialize-all)
129+
130+
@inlinable
137131
@inline(__always)
138-
public // @testable
139-
mutating func isUniquelyReferenced_native_noSpareBits() -> Bool {
132+
internal mutating func isUniquelyReferencedUnflaggedNative() -> Bool {
140133
_sanityCheck(isNative)
141134
return _isUnique_native(&rawValue)
142135
}
143136

144-
@inlinable // FIXME(sil-serialize-all)
145-
public // @testable
146-
var objCInstance: ObjC {
137+
@inlinable
138+
internal var objCInstance: ObjC {
147139
@inline(__always) get {
148140
_sanityCheck(isObjC)
149141
return Builtin.castReferenceFromBridgeObject(rawValue)
150142
}
151143
}
152-
153-
//===--- private --------------------------------------------------------===//
154-
@inlinable // FIXME(sil-serialize-all)
155-
internal var _isTagged: Bool {
156-
@inline(__always) get {
157-
return Bool(Builtin.classifyBridgeObject(rawValue).isObjCTaggedPointer)
158-
}
159-
}
160-
161-
// rawValue is passed inout to _isUnique. Although its value
162-
// is unchanged, it must appear mutable to the optimizer.
163-
@usableFromInline
164-
internal var rawValue: Builtin.BridgeObject
165144
}

stdlib/public/core/DictionaryVariant.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,27 @@ extension Dictionary._Variant {
7878

7979
@inlinable
8080
internal mutating func isUniquelyReferenced() -> Bool {
81-
return object.isUniquelyReferenced_native_noSpareBits()
81+
return object.isUniquelyReferencedUnflaggedNative()
8282
}
8383

8484
#if _runtime(_ObjC)
8585
@usableFromInline @_transparent
8686
internal var isNative: Bool {
8787
if guaranteedNative { return true }
88-
return object.isNativeWithClearedSpareBits(0)
88+
return object.isUnflaggedNative
8989
}
9090
#endif
9191

9292
@usableFromInline @_transparent
9393
internal var asNative: _NativeDictionary<Key, Value> {
9494
get {
95-
return _NativeDictionary<Key, Value>(object.nativeInstance_noSpareBits)
95+
return _NativeDictionary<Key, Value>(object.unflaggedNativeInstance)
9696
}
9797
set {
9898
self = .init(native: newValue)
9999
}
100100
_modify {
101-
var native = _NativeDictionary<Key, Value>(
102-
object.nativeInstance_noSpareBits)
101+
var native = _NativeDictionary<Key, Value>(object.unflaggedNativeInstance)
103102
self = .init(dummy: ())
104103
yield &native
105104
object = .init(native: native._storage)

stdlib/public/core/SetVariant.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,27 @@ extension Set._Variant {
7575

7676
@inlinable
7777
internal mutating func isUniquelyReferenced() -> Bool {
78-
return object.isUniquelyReferenced_native_noSpareBits()
78+
return object.isUniquelyReferencedUnflaggedNative()
7979
}
8080

8181
#if _runtime(_ObjC)
8282
@usableFromInline @_transparent
8383
internal var isNative: Bool {
8484
if guaranteedNative { return true }
85-
return object.isNativeWithClearedSpareBits(0)
85+
return object.isUnflaggedNative
8686
}
8787
#endif
8888

8989
@usableFromInline @_transparent
9090
internal var asNative: _NativeSet<Element> {
9191
get {
92-
return _NativeSet(object.nativeInstance_noSpareBits)
92+
return _NativeSet(object.unflaggedNativeInstance)
9393
}
9494
set {
9595
self = .init(native: newValue)
9696
}
9797
_modify {
98-
var native = _NativeSet<Element>(object.nativeInstance_noSpareBits)
98+
var native = _NativeSet<Element>(object.unflaggedNativeInstance)
9999
self = .init(dummy: ())
100100
yield &native
101101
object = .init(native: native._storage)

test/api-digester/Outputs/stability-stdlib-abi.swift.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ Constructor AnyHashable.init(_box:) has been removed
88
Constructor AnyHashable.init(_usingDefaultRepresentationOf:) has been removed
99
Constructor ManagedBufferPointer.init(_:_:_:) has been removed
1010
Constructor Zip2Sequence.init(_sequence1:_sequence2:) has been removed
11+
Constructor _BridgeStorage.init(native:bits:) has been removed
1112
Constructor _BridgeableMetatype.init(value:) has been removed
1213
Func AnyHashable._downCastConditional(into:) has been removed
14+
Func _BridgeStorage.isNativeWithClearedSpareBits(_:) has been removed
15+
Func _BridgeStorage.isUniquelyReferenced_native_noSpareBits() has been removed
1316
Func _CocoaDictionary.Index.copy() has been removed
1417
Func _ContiguousArrayStorage._getNonVerbatimBridgedHeapBuffer() has been removed
1518
Func _ContiguousArrayStorage._withVerbatimBridgedUnsafeBufferImpl(_:) has been removed
@@ -28,6 +31,10 @@ Var ManagedBufferPointer.baseAddress has been removed
2831
Var ManagedBufferPointer.storage has been removed
2932
Var ManagedBufferPointer.value has been removed
3033
Var Optional._nilSentinel has been removed
34+
Var _ArrayBuffer.deferredTypeCheckMask has been removed
35+
Var _BridgeStorage._isTagged has been removed
36+
Var _BridgeStorage.nativeInstance_noSpareBits has been removed
37+
Var _BridgeStorage.spareBits has been removed
3138
Var _BridgeableMetatype.value has been removed
3239
Var _CocoaDictionary.Index._object has been removed
3340
Var _CocoaSet.Index._object has been removed

0 commit comments

Comments
 (0)