Skip to content

[stdlib] Adopt _pointerBitWidth conditional #65471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions stdlib/public/core/AtomicInt.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
object target: UnsafeMutablePointer<Int>,
expected: UnsafeMutablePointer<Int>,
desired: Int) -> Bool {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
target._rawValue, expected.pointee._value, desired._value)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
#if _pointerBitWidth(_64)
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int64(
target._rawValue, expected.pointee._value, desired._value)
#elseif _pointerBitWidth(_32)
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
target._rawValue, expected.pointee._value, desired._value)
#else
#error("Unknown platform")
#endif
expected.pointee._value = oldValue
return Bool(won)
Expand All @@ -82,23 +84,27 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
public // Existing uses outside stdlib
func _swift_stdlib_atomicLoadInt(
object target: UnsafeMutablePointer<Int>) -> Int {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
return Int(value)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
#if _pointerBitWidth(_64)
let value = Builtin.atomicload_seqcst_Int64(target._rawValue)
return Int(value)
#elseif _pointerBitWidth(_32)
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
return Int(value)
#else
#error("Unknown platform")
#endif
}

@usableFromInline // used by SwiftPrivate._stdlib_AtomicInt
internal func _swift_stdlib_atomicStoreInt(
object target: UnsafeMutablePointer<Int>,
desired: Int) {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
#if _pointerBitWidth(_64)
Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value)
#elseif _pointerBitWidth(_32)
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
#else
#error("Unknown platform")
#endif
}

Expand All @@ -111,14 +117,16 @@ func _swift_stdlib_atomicFetch${operation}Int(
object target: UnsafeMutablePointer<Int>,
operand: Int) -> Int {
let rawTarget = UnsafeMutableRawPointer(target)
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
let value = _swift_stdlib_atomicFetch${operation}Int32(
object: rawTarget.assumingMemoryBound(to: Int32.self),
operand: Int32(operand))
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
#if _pointerBitWidth(_64)
let value = _swift_stdlib_atomicFetch${operation}Int64(
object: rawTarget.assumingMemoryBound(to: Int64.self),
operand: Int64(operand))
#elseif _pointerBitWidth(_32)
let value = _swift_stdlib_atomicFetch${operation}Int32(
object: rawTarget.assumingMemoryBound(to: Int32.self),
operand: Int32(operand))
#else
#error("Unknown platform")
#endif
return Int(value)
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/BridgeStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal struct _BridgeStorage<NativeClass: AnyObject> {
rawValue = Builtin.reinterpretCast(native)
}

#if !(arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32))
#if _pointerBitWidth(_64)
@inlinable
@inline(__always)
internal init(taggedPayload: UInt) {
Expand Down
16 changes: 8 additions & 8 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,18 @@ internal var _objectPointerLowSpareBitShift: UInt {
}
}

#if arch(i386) || arch(arm) || arch(wasm32) || arch(powerpc) || arch(powerpc64) || arch(
powerpc64le) || arch(s390x) || arch(arm64_32)
@inlinable
internal var _objectPointerIsObjCBit: UInt {
@inline(__always) get { return 0x0000_0002 }
}
@inline(__always) get {
#if _pointerBitWidth(_64)
return 0x4000_0000_0000_0000
#elseif _pointerBitWidth(_32)
return 0x0000_0002
#else
@inlinable
internal var _objectPointerIsObjCBit: UInt {
@inline(__always) get { return 0x4000_0000_0000_0000 }
}
#error("Unknown platform")
#endif
}
}

/// Extract the raw bits of `x`.
@inlinable
Expand Down
6 changes: 4 additions & 2 deletions stdlib/public/core/DictionaryVariant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ extension Dictionary {
@inlinable
@inline(__always)
init(dummy: Void) {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
#if _pointerBitWidth(_64)
self.object = _BridgeStorage(taggedPayload: 0)
#elseif _pointerBitWidth(_32)
self.init(native: _NativeDictionary())
#else
self.object = _BridgeStorage(taggedPayload: 0)
#error("Unknown platform")
#endif
}

Expand Down
16 changes: 10 additions & 6 deletions stdlib/public/core/Hasher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ extension Hasher {

@inline(__always)
internal mutating func combine(_ value: UInt) {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
#if _pointerBitWidth(_64)
combine(UInt64(truncatingIfNeeded: value))
#elseif _pointerBitWidth(_32)
combine(UInt32(truncatingIfNeeded: value))
#else
combine(UInt64(truncatingIfNeeded: value))
#error("Unknown platform")
#endif
}

Expand Down Expand Up @@ -423,15 +425,17 @@ public struct Hasher {
@usableFromInline
internal static func _hash(seed: Int, _ value: UInt) -> Int {
var state = _State(seed: seed)
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
#if _pointerBitWidth(_64)
_internalInvariant(UInt.bitWidth == UInt64.bitWidth)
state.compress(UInt64(truncatingIfNeeded: value))
let tbc = _TailBuffer(tail: 0, byteCount: 8)
#elseif _pointerBitWidth(_32)
_internalInvariant(UInt.bitWidth < UInt64.bitWidth)
let tbc = _TailBuffer(
tail: UInt64(truncatingIfNeeded: value),
byteCount: UInt.bitWidth &>> 3)
#else
_internalInvariant(UInt.bitWidth == UInt64.bitWidth)
state.compress(UInt64(truncatingIfNeeded: value))
let tbc = _TailBuffer(tail: 0, byteCount: 8)
#error("Unknown platform")
#endif
return Int(truncatingIfNeeded: state.finalize(tailAndByteCount: tbc.value))
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/IntegerTypes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ ${assignmentOperatorComment(x.operator, True)}

% dbits = bits*2
% if bits == 64:
#if !(arch(arm) || arch(i386) || arch(wasm32))
#if _pointerBitWidth(_64) || arch(arm64_32)
// On 32b architectures we fall back on the generic implementation,
// because LLVM doesn't know how to codegen the 128b multiply we use.
//
Expand Down
6 changes: 4 additions & 2 deletions stdlib/public/core/SetVariant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ extension Set {
@inlinable
@inline(__always)
init(dummy: ()) {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
#if _pointerBitWidth(_64)
self.object = _BridgeStorage(taggedPayload: 0)
#elseif _pointerBitWidth(_32)
self.init(native: _NativeSet())
#else
self.object = _BridgeStorage(taggedPayload: 0)
#error("Unknown platform")
#endif
}

Expand Down
8 changes: 5 additions & 3 deletions stdlib/public/core/SmallString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ internal struct _SmallString {
extension _SmallString {
@inlinable @inline(__always)
internal static var capacity: Int {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
#if _pointerBitWidth(_32)
return 10
#elseif os(Android) && arch(arm64)
return 14
#else
#elseif _pointerBitWidth(_64)
return 15
#else
#error("Unknown platform")
#endif
}

Expand Down Expand Up @@ -346,7 +348,7 @@ extension _SmallString {
}
}

#if _runtime(_ObjC) && !(arch(i386) || arch(arm) || arch(arm64_32))
#if _runtime(_ObjC) && _pointerBitWidth(_64)
// Cocoa interop
extension _SmallString {
// Resiliently create from a tagged cocoa string
Expand Down
15 changes: 7 additions & 8 deletions stdlib/public/core/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ internal enum _KnownCocoaString {
case storage
case shared
case cocoa
#if !(arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32))
#if _pointerBitWidth(_64)
case tagged
#endif
#if arch(arm64)
Expand All @@ -323,7 +323,7 @@ internal enum _KnownCocoaString {
@inline(__always)
init(_ str: _CocoaString) {

#if !(arch(i386) || arch(arm) || arch(arm64_32))
#if _pointerBitWidth(_64)
if _isObjCTaggedPointer(str) {
#if arch(arm64)
if let _ = getConstantTaggedCocoaContents(str) {
Expand All @@ -349,8 +349,7 @@ internal enum _KnownCocoaString {
}
}

#if !(arch(i386) || arch(arm) || arch(arm64_32))

#if _pointerBitWidth(_64)
// Resiliently write a tagged _CocoaString's contents into a buffer.
// The Foundation overlay takes care of bridging tagged pointer strings before
// they reach us, but this may still be called by older code, or by strings
Expand Down Expand Up @@ -393,7 +392,7 @@ private func _withCocoaASCIIPointer<R>(
requireStableAddress: Bool,
work: (UnsafePointer<UInt8>) -> R?
) -> R? {
#if !(arch(i386) || arch(arm) || arch(arm64_32))
#if _pointerBitWidth(_64)
if _isObjCTaggedPointer(str) {
if let ptr = getConstantTaggedCocoaContents(str)?.asciiContentsPointer {
return work(ptr)
Expand Down Expand Up @@ -421,7 +420,7 @@ private func _withCocoaUTF8Pointer<R>(
requireStableAddress: Bool,
work: (UnsafePointer<UInt8>) -> R?
) -> R? {
#if !(arch(i386) || arch(arm) || arch(arm64_32))
#if _pointerBitWidth(_64)
if _isObjCTaggedPointer(str) {
if let ptr = getConstantTaggedCocoaContents(str)?.asciiContentsPointer {
return work(ptr)
Expand Down Expand Up @@ -577,7 +576,7 @@ internal func _bridgeCocoaString(_ cocoaString: _CocoaString) -> _StringGuts {
case .shared:
return _unsafeUncheckedDowncast(
cocoaString, to: __SharedStringStorage.self).asString._guts
#if !(arch(i386) || arch(arm) || arch(arm64_32))
#if _pointerBitWidth(_64)
case .tagged:
// Foundation should be taking care of tagged pointer strings before they
// reach here, so the only ones reaching this point should be back deployed,
Expand Down Expand Up @@ -608,7 +607,7 @@ internal func _bridgeCocoaString(_ cocoaString: _CocoaString) -> _StringGuts {
let immutableCopy
= _stdlib_binary_CFStringCreateCopy(cocoaString)

#if !(arch(i386) || arch(arm) || arch(arm64_32))
#if _pointerBitWidth(_64)
if _isObjCTaggedPointer(immutableCopy) {
// Copying a tagged pointer can produce a tagged pointer, but only if it's
// small enough to definitely fit in a _SmallString
Expand Down
10 changes: 6 additions & 4 deletions stdlib/public/core/StringGuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,18 @@ extension _StringGuts {
#else
@usableFromInline @inline(never) @_effects(releasenone)
internal func _invariantCheck() {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
_internalInvariant(MemoryLayout<String>.size == 12, """
#if _pointerBitWidth(_64)
_internalInvariant(MemoryLayout<String>.size == 16, """
the runtime is depending on this, update Reflection.mm and \
this if you change it
""")
#else
_internalInvariant(MemoryLayout<String>.size == 16, """
#elseif _pointerBitWidth(_32)
_internalInvariant(MemoryLayout<String>.size == 12, """
the runtime is depending on this, update Reflection.mm and \
this if you change it
""")
#else
#error("Unknown platform")
#endif
}
#endif // INTERNAL_CHECKS_ENABLED
Expand Down
Loading