Skip to content

Commit 32811ba

Browse files
authored
Merge pull request #65471 from lorentey/adopt-pointerBitwidth
[stdlib] Adopt _pointerBitWidth conditional
2 parents 3eaeafa + 298ab20 commit 32811ba

33 files changed

+305
-229
lines changed

stdlib/public/core/AtomicInt.swift.gyb

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
6565
object target: UnsafeMutablePointer<Int>,
6666
expected: UnsafeMutablePointer<Int>,
6767
desired: Int) -> Bool {
68-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
69-
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
70-
target._rawValue, expected.pointee._value, desired._value)
71-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
68+
#if _pointerBitWidth(_64)
7269
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int64(
7370
target._rawValue, expected.pointee._value, desired._value)
71+
#elseif _pointerBitWidth(_32)
72+
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
73+
target._rawValue, expected.pointee._value, desired._value)
74+
#else
75+
#error("Unknown platform")
7476
#endif
7577
expected.pointee._value = oldValue
7678
return Bool(won)
@@ -82,23 +84,27 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
8284
public // Existing uses outside stdlib
8385
func _swift_stdlib_atomicLoadInt(
8486
object target: UnsafeMutablePointer<Int>) -> Int {
85-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
86-
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
87-
return Int(value)
88-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
87+
#if _pointerBitWidth(_64)
8988
let value = Builtin.atomicload_seqcst_Int64(target._rawValue)
9089
return Int(value)
90+
#elseif _pointerBitWidth(_32)
91+
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
92+
return Int(value)
93+
#else
94+
#error("Unknown platform")
9195
#endif
9296
}
9397

9498
@usableFromInline // used by SwiftPrivate._stdlib_AtomicInt
9599
internal func _swift_stdlib_atomicStoreInt(
96100
object target: UnsafeMutablePointer<Int>,
97101
desired: Int) {
98-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
99-
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
100-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
102+
#if _pointerBitWidth(_64)
101103
Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value)
104+
#elseif _pointerBitWidth(_32)
105+
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
106+
#else
107+
#error("Unknown platform")
102108
#endif
103109
}
104110

@@ -111,14 +117,16 @@ func _swift_stdlib_atomicFetch${operation}Int(
111117
object target: UnsafeMutablePointer<Int>,
112118
operand: Int) -> Int {
113119
let rawTarget = UnsafeMutableRawPointer(target)
114-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
115-
let value = _swift_stdlib_atomicFetch${operation}Int32(
116-
object: rawTarget.assumingMemoryBound(to: Int32.self),
117-
operand: Int32(operand))
118-
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
120+
#if _pointerBitWidth(_64)
119121
let value = _swift_stdlib_atomicFetch${operation}Int64(
120122
object: rawTarget.assumingMemoryBound(to: Int64.self),
121123
operand: Int64(operand))
124+
#elseif _pointerBitWidth(_32)
125+
let value = _swift_stdlib_atomicFetch${operation}Int32(
126+
object: rawTarget.assumingMemoryBound(to: Int32.self),
127+
operand: Int32(operand))
128+
#else
129+
#error("Unknown platform")
122130
#endif
123131
return Int(value)
124132
}

stdlib/public/core/BridgeStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
6161
rawValue = Builtin.reinterpretCast(native)
6262
}
6363

64-
#if !(arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32))
64+
#if _pointerBitWidth(_64)
6565
@inlinable
6666
@inline(__always)
6767
internal init(taggedPayload: UInt) {

stdlib/public/core/Builtin.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,18 @@ internal var _objectPointerLowSpareBitShift: UInt {
406406
}
407407
}
408408

409-
#if arch(i386) || arch(arm) || arch(wasm32) || arch(powerpc) || arch(powerpc64) || arch(
410-
powerpc64le) || arch(s390x) || arch(arm64_32)
411409
@inlinable
412410
internal var _objectPointerIsObjCBit: UInt {
413-
@inline(__always) get { return 0x0000_0002 }
414-
}
411+
@inline(__always) get {
412+
#if _pointerBitWidth(_64)
413+
return 0x4000_0000_0000_0000
414+
#elseif _pointerBitWidth(_32)
415+
return 0x0000_0002
415416
#else
416-
@inlinable
417-
internal var _objectPointerIsObjCBit: UInt {
418-
@inline(__always) get { return 0x4000_0000_0000_0000 }
419-
}
417+
#error("Unknown platform")
420418
#endif
419+
}
420+
}
421421

422422
/// Extract the raw bits of `x`.
423423
@inlinable

stdlib/public/core/DictionaryVariant.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ extension Dictionary {
4646
@inlinable
4747
@inline(__always)
4848
init(dummy: Void) {
49-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
49+
#if _pointerBitWidth(_64)
50+
self.object = _BridgeStorage(taggedPayload: 0)
51+
#elseif _pointerBitWidth(_32)
5052
self.init(native: _NativeDictionary())
5153
#else
52-
self.object = _BridgeStorage(taggedPayload: 0)
54+
#error("Unknown platform")
5355
#endif
5456
}
5557

stdlib/public/core/Hasher.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,12 @@ extension Hasher {
160160

161161
@inline(__always)
162162
internal mutating func combine(_ value: UInt) {
163-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
163+
#if _pointerBitWidth(_64)
164+
combine(UInt64(truncatingIfNeeded: value))
165+
#elseif _pointerBitWidth(_32)
164166
combine(UInt32(truncatingIfNeeded: value))
165167
#else
166-
combine(UInt64(truncatingIfNeeded: value))
168+
#error("Unknown platform")
167169
#endif
168170
}
169171

@@ -423,15 +425,17 @@ public struct Hasher {
423425
@usableFromInline
424426
internal static func _hash(seed: Int, _ value: UInt) -> Int {
425427
var state = _State(seed: seed)
426-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
428+
#if _pointerBitWidth(_64)
429+
_internalInvariant(UInt.bitWidth == UInt64.bitWidth)
430+
state.compress(UInt64(truncatingIfNeeded: value))
431+
let tbc = _TailBuffer(tail: 0, byteCount: 8)
432+
#elseif _pointerBitWidth(_32)
427433
_internalInvariant(UInt.bitWidth < UInt64.bitWidth)
428434
let tbc = _TailBuffer(
429435
tail: UInt64(truncatingIfNeeded: value),
430436
byteCount: UInt.bitWidth &>> 3)
431437
#else
432-
_internalInvariant(UInt.bitWidth == UInt64.bitWidth)
433-
state.compress(UInt64(truncatingIfNeeded: value))
434-
let tbc = _TailBuffer(tail: 0, byteCount: 8)
438+
#error("Unknown platform")
435439
#endif
436440
return Int(truncatingIfNeeded: state.finalize(tailAndByteCount: tbc.value))
437441
}

stdlib/public/core/IntegerTypes.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ ${assignmentOperatorComment(x.operator, True)}
15151515

15161516
% dbits = bits*2
15171517
% if bits == 64:
1518-
#if !(arch(arm) || arch(i386) || arch(wasm32))
1518+
#if _pointerBitWidth(_64) || arch(arm64_32)
15191519
// On 32b architectures we fall back on the generic implementation,
15201520
// because LLVM doesn't know how to codegen the 128b multiply we use.
15211521
//

stdlib/public/core/SetVariant.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ extension Set {
3636
@inlinable
3737
@inline(__always)
3838
init(dummy: ()) {
39-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
39+
#if _pointerBitWidth(_64)
40+
self.object = _BridgeStorage(taggedPayload: 0)
41+
#elseif _pointerBitWidth(_32)
4042
self.init(native: _NativeSet())
4143
#else
42-
self.object = _BridgeStorage(taggedPayload: 0)
44+
#error("Unknown platform")
4345
#endif
4446
}
4547

stdlib/public/core/SmallString.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ internal struct _SmallString {
7979
extension _SmallString {
8080
@inlinable @inline(__always)
8181
internal static var capacity: Int {
82-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
82+
#if _pointerBitWidth(_32)
8383
return 10
8484
#elseif os(Android) && arch(arm64)
8585
return 14
86-
#else
86+
#elseif _pointerBitWidth(_64)
8787
return 15
88+
#else
89+
#error("Unknown platform")
8890
#endif
8991
}
9092

@@ -346,7 +348,7 @@ extension _SmallString {
346348
}
347349
}
348350

349-
#if _runtime(_ObjC) && !(arch(i386) || arch(arm) || arch(arm64_32))
351+
#if _runtime(_ObjC) && _pointerBitWidth(_64)
350352
// Cocoa interop
351353
extension _SmallString {
352354
// Resiliently create from a tagged cocoa string

stdlib/public/core/StringBridge.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ internal enum _KnownCocoaString {
313313
case storage
314314
case shared
315315
case cocoa
316-
#if !(arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32))
316+
#if _pointerBitWidth(_64)
317317
case tagged
318318
#endif
319319
#if arch(arm64)
@@ -323,7 +323,7 @@ internal enum _KnownCocoaString {
323323
@inline(__always)
324324
init(_ str: _CocoaString) {
325325

326-
#if !(arch(i386) || arch(arm) || arch(arm64_32))
326+
#if _pointerBitWidth(_64)
327327
if _isObjCTaggedPointer(str) {
328328
#if arch(arm64)
329329
if let _ = getConstantTaggedCocoaContents(str) {
@@ -349,8 +349,7 @@ internal enum _KnownCocoaString {
349349
}
350350
}
351351

352-
#if !(arch(i386) || arch(arm) || arch(arm64_32))
353-
352+
#if _pointerBitWidth(_64)
354353
// Resiliently write a tagged _CocoaString's contents into a buffer.
355354
// The Foundation overlay takes care of bridging tagged pointer strings before
356355
// they reach us, but this may still be called by older code, or by strings
@@ -393,7 +392,7 @@ private func _withCocoaASCIIPointer<R>(
393392
requireStableAddress: Bool,
394393
work: (UnsafePointer<UInt8>) -> R?
395394
) -> R? {
396-
#if !(arch(i386) || arch(arm) || arch(arm64_32))
395+
#if _pointerBitWidth(_64)
397396
if _isObjCTaggedPointer(str) {
398397
if let ptr = getConstantTaggedCocoaContents(str)?.asciiContentsPointer {
399398
return work(ptr)
@@ -421,7 +420,7 @@ private func _withCocoaUTF8Pointer<R>(
421420
requireStableAddress: Bool,
422421
work: (UnsafePointer<UInt8>) -> R?
423422
) -> R? {
424-
#if !(arch(i386) || arch(arm) || arch(arm64_32))
423+
#if _pointerBitWidth(_64)
425424
if _isObjCTaggedPointer(str) {
426425
if let ptr = getConstantTaggedCocoaContents(str)?.asciiContentsPointer {
427426
return work(ptr)
@@ -577,7 +576,7 @@ internal func _bridgeCocoaString(_ cocoaString: _CocoaString) -> _StringGuts {
577576
case .shared:
578577
return _unsafeUncheckedDowncast(
579578
cocoaString, to: __SharedStringStorage.self).asString._guts
580-
#if !(arch(i386) || arch(arm) || arch(arm64_32))
579+
#if _pointerBitWidth(_64)
581580
case .tagged:
582581
// Foundation should be taking care of tagged pointer strings before they
583582
// reach here, so the only ones reaching this point should be back deployed,
@@ -608,7 +607,7 @@ internal func _bridgeCocoaString(_ cocoaString: _CocoaString) -> _StringGuts {
608607
let immutableCopy
609608
= _stdlib_binary_CFStringCreateCopy(cocoaString)
610609

611-
#if !(arch(i386) || arch(arm) || arch(arm64_32))
610+
#if _pointerBitWidth(_64)
612611
if _isObjCTaggedPointer(immutableCopy) {
613612
// Copying a tagged pointer can produce a tagged pointer, but only if it's
614613
// small enough to definitely fit in a _SmallString

stdlib/public/core/StringGuts.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,18 @@ extension _StringGuts {
179179
#else
180180
@usableFromInline @inline(never) @_effects(releasenone)
181181
internal func _invariantCheck() {
182-
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
183-
_internalInvariant(MemoryLayout<String>.size == 12, """
182+
#if _pointerBitWidth(_64)
183+
_internalInvariant(MemoryLayout<String>.size == 16, """
184184
the runtime is depending on this, update Reflection.mm and \
185185
this if you change it
186186
""")
187-
#else
188-
_internalInvariant(MemoryLayout<String>.size == 16, """
187+
#elseif _pointerBitWidth(_32)
188+
_internalInvariant(MemoryLayout<String>.size == 12, """
189189
the runtime is depending on this, update Reflection.mm and \
190190
this if you change it
191191
""")
192+
#else
193+
#error("Unknown platform")
192194
#endif
193195
}
194196
#endif // INTERNAL_CHECKS_ENABLED

0 commit comments

Comments
 (0)