From e08ad97132337835d1433c271388fbc538495bf0 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 22 Sep 2021 12:54:07 -0700 Subject: [PATCH 1/2] Implement double-wide loads with compare-exchange on arm64 in debug builds with the Swift 5.5 commpiler Resolves https://github.com/apple/swift-atomics/issues/41. --- Sources/Atomics/IntegerConformances.swift.gyb | 15 +++++++++++++++ .../autogenerated/IntegerConformances.swift | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/Sources/Atomics/IntegerConformances.swift.gyb b/Sources/Atomics/IntegerConformances.swift.gyb index 72f209f..555913e 100644 --- a/Sources/Atomics/IntegerConformances.swift.gyb +++ b/Sources/Atomics/IntegerConformances.swift.gyb @@ -66,6 +66,18 @@ extension ${swiftType}.AtomicRepresentation: AtomicStorage { at pointer: UnsafeMutablePointer, ordering: AtomicLoadOrdering ) -> Value { +% if swiftType == "DoubleWord": + // Work around https://github.com/apple/swift-atomics/issues/41 + #if compiler(>=5.5) && arch(arm64) && DEBUG + let (_, original) = atomicCompareExchange( + expected: DoubleWord(high: 0, low: 0), + desired: DoubleWord(high: 0, low: 0), + at: pointer, + successOrdering: .relaxed, // Note: this relies on the FIXME below. + failureOrdering: ordering) + return original + #else +% end switch ordering { % for (swiftOrder, shimOrder) in loadOrderings: case .${swiftOrder}: @@ -74,6 +86,9 @@ extension ${swiftType}.AtomicRepresentation: AtomicStorage { default: fatalError("Unsupported ordering") } +% if swiftType == "DoubleWord": + #endif +% end } @_semantics("atomics.requires_constant_orderings") diff --git a/Sources/Atomics/autogenerated/IntegerConformances.swift b/Sources/Atomics/autogenerated/IntegerConformances.swift index 0684fbd..f124044 100644 --- a/Sources/Atomics/autogenerated/IntegerConformances.swift +++ b/Sources/Atomics/autogenerated/IntegerConformances.swift @@ -4989,6 +4989,16 @@ extension DoubleWord.AtomicRepresentation: AtomicStorage { at pointer: UnsafeMutablePointer, ordering: AtomicLoadOrdering ) -> Value { + // Work around https://github.com/apple/swift-atomics/issues/41 + #if compiler(>=5.5) && arch(arm64) && DEBUG + let (_, original) = atomicCompareExchange( + expected: DoubleWord(high: 0, low: 0), + desired: DoubleWord(high: 0, low: 0), + at: pointer, + successOrdering: .relaxed, // Note: this relies on the FIXME below. + failureOrdering: ordering) + return original + #else switch ordering { case .relaxed: return _sa_load_relaxed_DoubleWord(pointer._extract) @@ -4999,6 +5009,7 @@ extension DoubleWord.AtomicRepresentation: AtomicStorage { default: fatalError("Unsupported ordering") } + #endif } @_semantics("atomics.requires_constant_orderings") From 2468fa3eac88d73dcdbc71402e352fd16c302d5c Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 22 Sep 2021 13:16:38 -0700 Subject: [PATCH 2/2] Also use compare-exchange in `dispose()` --- Sources/Atomics/IntegerConformances.swift.gyb | 19 +++++++++- .../autogenerated/IntegerConformances.swift | 35 +++++++++++++------ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/Sources/Atomics/IntegerConformances.swift.gyb b/Sources/Atomics/IntegerConformances.swift.gyb index 555913e..f62d122 100644 --- a/Sources/Atomics/IntegerConformances.swift.gyb +++ b/Sources/Atomics/IntegerConformances.swift.gyb @@ -44,7 +44,24 @@ extension ${swiftType}: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_${swiftType}(_storage) +% if swiftType == "DoubleWord": + // Work around https://github.com/apple/swift-atomics/issues/41 + #if compiler(>=5.5) && arch(arm64) && DEBUG + var copy = self // This is not great + var expected = DoubleWord(high: 0, low: 0) + withUnsafeMutablePointer(to: ©) { pointer in + _ = _sa_cmpxchg_strong_relaxed_relaxed_DoubleWord( + pointer._extract, + &expected, + DoubleWord(high: 0, low: 0)) + } + return expected + #else +% end + return _sa_dispose_${swiftType}(_storage) +% if swiftType == "DoubleWord": + #endif +% end } } } diff --git a/Sources/Atomics/autogenerated/IntegerConformances.swift b/Sources/Atomics/autogenerated/IntegerConformances.swift index f124044..27aa235 100644 --- a/Sources/Atomics/autogenerated/IntegerConformances.swift +++ b/Sources/Atomics/autogenerated/IntegerConformances.swift @@ -34,7 +34,7 @@ extension Int: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_Int(_storage) + return _sa_dispose_Int(_storage) } } } @@ -527,7 +527,7 @@ extension Int64: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_Int64(_storage) + return _sa_dispose_Int64(_storage) } } } @@ -1020,7 +1020,7 @@ extension Int32: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_Int32(_storage) + return _sa_dispose_Int32(_storage) } } } @@ -1513,7 +1513,7 @@ extension Int16: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_Int16(_storage) + return _sa_dispose_Int16(_storage) } } } @@ -2006,7 +2006,7 @@ extension Int8: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_Int8(_storage) + return _sa_dispose_Int8(_storage) } } } @@ -2499,7 +2499,7 @@ extension UInt: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_UInt(_storage) + return _sa_dispose_UInt(_storage) } } } @@ -2992,7 +2992,7 @@ extension UInt64: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_UInt64(_storage) + return _sa_dispose_UInt64(_storage) } } } @@ -3485,7 +3485,7 @@ extension UInt32: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_UInt32(_storage) + return _sa_dispose_UInt32(_storage) } } } @@ -3978,7 +3978,7 @@ extension UInt16: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_UInt16(_storage) + return _sa_dispose_UInt16(_storage) } } } @@ -4471,7 +4471,7 @@ extension UInt8: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_UInt8(_storage) + return _sa_dispose_UInt8(_storage) } } } @@ -4967,7 +4967,20 @@ extension DoubleWord: AtomicValue { @inline(__always) @_alwaysEmitIntoClient public func dispose() -> Value { - _sa_dispose_DoubleWord(_storage) + // Work around https://github.com/apple/swift-atomics/issues/41 + #if compiler(>=5.5) && arch(arm64) && DEBUG + var copy = self // This is not great + var expected = DoubleWord(high: 0, low: 0) + withUnsafeMutablePointer(to: ©) { pointer in + _ = _sa_cmpxchg_strong_relaxed_relaxed_DoubleWord( + pointer._extract, + &expected, + DoubleWord(high: 0, low: 0)) + } + return expected + #else + return _sa_dispose_DoubleWord(_storage) + #endif } } }