Skip to content

Commit 76bfd3b

Browse files
committed
[embedded] Add Set to the embedded stdlib
1 parent 1c54f4b commit 76bfd3b

15 files changed

+183
-15
lines changed

stdlib/public/core/BridgeStorage.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
3131

3232
// rawValue is passed inout to _isUnique. Although its value
3333
// is unchanged, it must appear mutable to the optimizer.
34+
#if !$Embedded
3435
@usableFromInline
3536
internal var rawValue: Builtin.BridgeObject
37+
#else
38+
@usableFromInline
39+
internal var rawValue: NativeClass
40+
#endif
3641

42+
#if !$Embedded
3743
@inlinable
3844
@inline(__always)
3945
internal init(native: Native, isFlagged flag: Bool) {
@@ -46,13 +52,16 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
4652
native,
4753
flag ? (1 as UInt) << _objectPointerLowSpareBitShift : 0)
4854
}
55+
#endif
4956

57+
#if _runtime(_ObjC)
5058
@inlinable
5159
@inline(__always)
5260
internal init(objC: ObjC) {
5361
_internalInvariant(_usesNativeSwiftReferenceCounting(NativeClass.self))
5462
rawValue = _makeObjCBridgeObject(objC)
5563
}
64+
#endif
5665

5766
@inlinable
5867
@inline(__always)
@@ -65,7 +74,7 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
6574
@inlinable
6675
@inline(__always)
6776
internal init(taggedPayload: UInt) {
68-
rawValue = _bridgeObject(taggingPayload: taggedPayload)
77+
rawValue = Builtin.reinterpretCast(taggedPayload)
6978
}
7079
#endif
7180

@@ -84,9 +93,13 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
8493
@inlinable
8594
internal var isNative: Bool {
8695
@inline(__always) get {
96+
#if !$Embedded
8797
let result = Builtin.classifyBridgeObject(rawValue)
8898
return !Bool(Builtin.or_Int1(result.isObjCObject,
8999
result.isObjCTaggedPointer))
100+
#else
101+
return true
102+
#endif
90103
}
91104
}
92105

@@ -100,9 +113,13 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
100113
@inlinable
101114
internal var isUnflaggedNative: Bool {
102115
@inline(__always) get {
116+
#if !$Embedded
103117
return (_bitPattern(rawValue) &
104118
(_bridgeObjectTaggedPointerBits | _objCTaggedPointerBits |
105119
_objectPointerIsObjCBit | _BridgeStorage.flagMask)) == 0
120+
#else
121+
return true
122+
#endif
106123
}
107124
}
108125

@@ -117,16 +134,18 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
117134
internal var nativeInstance: Native {
118135
@inline(__always) get {
119136
_internalInvariant(isNative)
120-
return Builtin.castReferenceFromBridgeObject(rawValue)
137+
return rawValue
121138
}
122139
}
123140

124141
@inlinable
125142
internal var unflaggedNativeInstance: Native {
126143
@inline(__always) get {
127144
_internalInvariant(isNative)
145+
#if !$Embedded
128146
_internalInvariant(_nonPointerBits(rawValue) == 0)
129-
return Builtin.reinterpretCast(rawValue)
147+
#endif
148+
return rawValue
130149
}
131150
}
132151

@@ -151,11 +170,13 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
151170
Builtin.endCOWMutation(&rawValue)
152171
}
153172

173+
#if _runtime(_ObjC)
154174
@inlinable
155175
internal var objCInstance: ObjC {
156176
@inline(__always) get {
157177
_internalInvariant(isObjC)
158178
return Builtin.castReferenceFromBridgeObject(rawValue)
159179
}
160180
}
181+
#endif
161182
}

stdlib/public/core/Builtin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,11 @@ func _isUnique_native<T>(_ object: inout T) -> Bool {
725725
// This could be a bridge object, single payload enum, or plain old
726726
// reference. Any case it's non pointer bits must be zero, so
727727
// force cast it to BridgeObject and check the spare bits.
728+
#if !$Embedded
728729
_internalInvariant(
729730
(_bitPattern(Builtin.reinterpretCast(object)) & _objectPointerSpareBits)
730731
== 0)
732+
#endif
731733
_internalInvariant(_usesNativeSwiftReferenceCounting(
732734
type(of: Builtin.reinterpretCast(object) as AnyObject)))
733735
return Bool(Builtin.isUnique_native(&object))

stdlib/public/core/CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ split_embedded_sources(
4343
EMBEDDED Assert.swift
4444
EMBEDDED AssertCommon.swift
4545
EMBEDDED BidirectionalCollection.swift
46-
NORMAL Bitset.swift
46+
EMBEDDED Bitset.swift
4747
EMBEDDED Bool.swift
4848
NORMAL BridgeObjectiveC.swift
49-
NORMAL BridgeStorage.swift
49+
EMBEDDED BridgeStorage.swift
5050
NORMAL BridgingBuffer.swift
5151
EMBEDDED Builtin.swift
5252
EMBEDDED BuiltinMath.swift
@@ -89,7 +89,7 @@ split_embedded_sources(
8989
# END WORKAROUND
9090
EMBEDDED Hasher.swift
9191
NORMAL Hashing.swift
92-
NORMAL HashTable.swift
92+
EMBEDDED HashTable.swift
9393
EMBEDDED Identifiable.swift
9494
EMBEDDED Indices.swift
9595
NORMAL InputStream.swift
@@ -111,7 +111,7 @@ split_embedded_sources(
111111
EMBEDDED Misc.swift
112112
EMBEDDED MutableCollection.swift
113113
NORMAL NativeDictionary.swift
114-
NORMAL NativeSet.swift
114+
EMBEDDED NativeSet.swift
115115
NORMAL NewtypeWrapper.swift
116116
NORMAL NFC.swift
117117
NORMAL NFD.swift
@@ -139,15 +139,15 @@ split_embedded_sources(
139139
EMBEDDED SipHash.swift
140140
EMBEDDED Sequence.swift
141141
EMBEDDED SequenceAlgorithms.swift
142-
NORMAL Set.swift
142+
EMBEDDED Set.swift
143143
EMBEDDED SetAlgebra.swift
144144
NORMAL SetAnyHashableExtensions.swift
145145
NORMAL SetBridging.swift
146-
NORMAL SetBuilder.swift
147-
NORMAL SetCasting.swift
148-
NORMAL SetStorage.swift
149-
NORMAL SetVariant.swift
150-
NORMAL ShadowProtocols.swift
146+
EMBEDDED SetBuilder.swift
147+
EMBEDDED SetCasting.swift
148+
EMBEDDED SetStorage.swift
149+
EMBEDDED SetVariant.swift
150+
EMBEDDED ShadowProtocols.swift
151151
NORMAL Shims.swift
152152
EMBEDDED Slice.swift
153153
NORMAL SmallString.swift

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,10 @@ public func swift_deletedMethodError() -> Never {
275275
public func swift_willThrow() throws {
276276
}
277277

278+
@_silgen_name("arc4random_buf")
279+
public func arc4random_buf(buf: UnsafeMutableRawPointer, nbytes: Int)
280+
281+
@_silgen_name("swift_stdlib_random")
282+
public func swift_stdlib_random(_ buf: UnsafeMutableRawPointer, _ nbytes: Int) {
283+
arc4random_buf(buf: buf, nbytes: nbytes)
284+
}

stdlib/public/core/HashTable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extension _HashTable {
102102
}
103103

104104
internal static func hashSeed(
105-
for object: AnyObject,
105+
for object: Builtin.NativeObject,
106106
scale: Int8
107107
) -> Int {
108108
// We generate a new hash seed whenever a new hash table is allocated and

stdlib/public/core/Hasher.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ extension Hasher {
251251
}
252252
}
253253

254+
#if $Embedded
255+
@usableFromInline
256+
var _swift_stdlib_Hashing_parameters: _SwiftHashingParameters = {
257+
var seed0: UInt64 = 0, seed1: UInt64 = 0
258+
swift_stdlib_random(&seed0, MemoryLayout<UInt64>.size)
259+
swift_stdlib_random(&seed1, MemoryLayout<UInt64>.size)
260+
return .init(seed0: seed0, seed1: seed1, deterministic: false)
261+
}()
262+
#endif
263+
254264
/// The universal hash function used by `Set` and `Dictionary`.
255265
///
256266
/// `Hasher` can be used to map an arbitrary sequence of bytes to an integer

stdlib/public/core/NativeSet.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ extension _NativeSet: _SetBuffer {
351351
// This function has a highly visible name to make it stand out in stack traces.
352352
@usableFromInline
353353
@inline(never)
354+
@_unavailableInEmbedded
354355
internal func ELEMENT_TYPE_OF_SET_VIOLATES_HASHABLE_REQUIREMENTS(
355356
_ elementType: Any.Type
356357
) -> Never {
@@ -379,7 +380,11 @@ extension _NativeSet { // Insertions
379380
// because we'll need to compare elements in case of hash collisions.
380381
let (bucket, found) = find(element, hashValue: hashValue)
381382
guard !found else {
383+
#if !$Embedded
382384
ELEMENT_TYPE_OF_SET_VIOLATES_HASHABLE_REQUIREMENTS(Element.self)
385+
#else
386+
fatalError("duplicate elements in a Set")
387+
#endif
383388
}
384389
hashTable.insert(bucket)
385390
uncheckedInitialize(at: bucket, to: element)
@@ -418,7 +423,11 @@ extension _NativeSet { // Insertions
418423
if rehashed {
419424
let (b, f) = find(element)
420425
if f {
426+
#if !$Embedded
421427
ELEMENT_TYPE_OF_SET_VIOLATES_HASHABLE_REQUIREMENTS(Element.self)
428+
#else
429+
fatalError("duplicate elements in a Set")
430+
#endif
422431
}
423432
bucket = b
424433
}
@@ -437,7 +446,11 @@ extension _NativeSet { // Insertions
437446
if rehashed {
438447
let (b, f) = find(element)
439448
if f != found {
449+
#if !$Embedded
440450
ELEMENT_TYPE_OF_SET_VIOLATES_HASHABLE_REQUIREMENTS(Element.self)
451+
#else
452+
fatalError("duplicate elements in a Set")
453+
#endif
441454
}
442455
bucket = b
443456
}

stdlib/public/core/Set.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,14 @@ extension Set: Hashable {
448448
}
449449
}
450450

451+
@_unavailableInEmbedded
451452
extension Set: _HasCustomAnyHashableRepresentation {
452453
public __consuming func _toCustomAnyHashable() -> AnyHashable? {
453454
return AnyHashable(_box: _SetAnyHashableBox(self))
454455
}
455456
}
456457

458+
@_unavailableInEmbedded
457459
internal struct _SetAnyHashableBox<Element: Hashable>: _AnyHashableBox {
458460
internal let _value: Set<Element>
459461
internal let _canonical: Set<AnyHashable>
@@ -1034,6 +1036,7 @@ extension Set: SetAlgebra {
10341036
}
10351037
}
10361038

1039+
@_unavailableInEmbedded
10371040
extension Set: CustomStringConvertible, CustomDebugStringConvertible {
10381041
/// A string that represents the contents of the set.
10391042
public var description: String {

stdlib/public/core/SetCasting.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extension Set {
4141
/// - Precondition: `BaseValue` is a base class or base `@objc`
4242
/// protocol (such as `AnyObject`) of `DerivedValue`.
4343
@inlinable
44+
@_unavailableInEmbedded
4445
public func _setUpCast<DerivedValue, BaseValue>(
4546
_ source: Set<DerivedValue>
4647
) -> Set<BaseValue> {
@@ -57,6 +58,7 @@ public func _setUpCast<DerivedValue, BaseValue>(
5758

5859
/// Called by the casting machinery.
5960
@_silgen_name("_swift_setDownCastIndirect")
61+
@_unavailableInEmbedded
6062
internal func _setDownCastIndirect<SourceValue, TargetValue>(
6163
_ source: UnsafePointer<Set<SourceValue>>,
6264
_ target: UnsafeMutablePointer<Set<TargetValue>>) {
@@ -71,6 +73,7 @@ internal func _setDownCastIndirect<SourceValue, TargetValue>(
7173
/// - Precondition: `DerivedValue` is a subtype of `BaseValue` and both
7274
/// are reference types.
7375
@inlinable
76+
@_unavailableInEmbedded
7477
public func _setDownCast<BaseValue, DerivedValue>(_ source: Set<BaseValue>)
7578
-> Set<DerivedValue> {
7679

@@ -99,6 +102,7 @@ public func _setDownCast<BaseValue, DerivedValue>(_ source: Set<BaseValue>)
99102

100103
/// Called by the casting machinery.
101104
@_silgen_name("_swift_setDownCastConditionalIndirect")
105+
@_unavailableInEmbedded
102106
internal func _setDownCastConditionalIndirect<SourceValue, TargetValue>(
103107
_ source: UnsafePointer<Set<SourceValue>>,
104108
_ target: UnsafeMutablePointer<Set<TargetValue>>
@@ -118,6 +122,7 @@ internal func _setDownCastConditionalIndirect<SourceValue, TargetValue>(
118122
/// - Precondition: `DerivedValue` is a subtype of `BaseValue` and both
119123
/// are reference types.
120124
@inlinable
125+
@_unavailableInEmbedded
121126
public func _setDownCastConditional<BaseValue, DerivedValue>(
122127
_ source: Set<BaseValue>
123128
) -> Set<DerivedValue>? {

stdlib/public/core/SetStorage.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ internal class __EmptySetSingleton: __RawSetStorage {
127127
#endif
128128
}
129129

130+
#if $Embedded
131+
public var _swiftEmptySetSingleton: (Int, Int, Int, Int, UInt8, UInt8, UInt16, UInt32, Int, Int, Int) =
132+
(
133+
/*isa*/0, /*refcount*/-1, // HeapObject header
134+
/*count*/0,
135+
/*capacity*/0,
136+
/*scale*/0,
137+
/*reservedScale*/0,
138+
/*extra*/0,
139+
/*age*/0,
140+
/*seed*/0,
141+
/*rawElements*/1,
142+
/*metadata*/-1
143+
)
144+
#endif
145+
130146
extension __RawSetStorage {
131147
/// The empty singleton that is used for every single Set that is created
132148
/// without any elements. The contents of the storage must never be mutated.
@@ -364,7 +380,7 @@ extension _SetStorage {
364380
truncatingIfNeeded: ObjectIdentifier(storage).hashValue)
365381
}
366382

367-
storage._seed = seed ?? _HashTable.hashSeed(for: storage, scale: scale)
383+
storage._seed = seed ?? _HashTable.hashSeed(for: Builtin.castToNativeObject(storage), scale: scale)
368384
storage._rawElements = UnsafeMutableRawPointer(elementsAddr)
369385

370386
// Initialize hash table metadata.

stdlib/public/core/TemporaryAllocation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,18 @@ internal func _isStackAllocationSafe(byteCount: Int, alignment: Int) -> Bool {
8888
return true
8989
}
9090

91+
#if !$Embedded
9192
// Finally, take a slow path through the standard library to see if the
9293
// current environment can accept a larger stack allocation.
9394
guard #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) //SwiftStdlib 5.6
9495
else {
9596
return false
9697
}
9798
return swift_stdlib_isStackAllocationSafe(byteCount, alignment)
99+
#else
100+
return false
101+
#endif
102+
98103
#else
99104
fatalError("unsupported compiler")
100105
#endif

test/embedded/set-runtime.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-run-simple-swift(%S/Inputs/print.swift -enable-experimental-feature Embedded -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
2+
// RUN: %target-run-simple-swift(%S/Inputs/print.swift -O -enable-experimental-feature Embedded -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
3+
// RUN: %target-run-simple-swift(%S/Inputs/print.swift -Osize -enable-experimental-feature Embedded -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
4+
5+
// REQUIRES: swift_in_compiler
6+
// REQUIRES: executable_test
7+
// REQUIRES: optimized_stdlib
8+
// REQUIRES: OS=macosx || OS=linux-gnu
9+
10+
@main
11+
struct Main {
12+
static func main() {
13+
var s: Set<Int> = [1, 2, 3]
14+
s.insert(42)
15+
s.sorted()
16+
s.allSatisfy { $0 > 0 }
17+
s.contains { $0 > 0 }
18+
s.map { $0 * 2 }
19+
s.filter { $0 > 0 }
20+
s.firstIndex(of: 42)
21+
s.min()
22+
s.max()
23+
s.reduce(0, +)
24+
// s.shuffled()
25+
// s.randomElement()
26+
print("OK!")
27+
}
28+
}
29+
30+
// CHECK: OK!

0 commit comments

Comments
 (0)