Skip to content

Commit 746cf2f

Browse files
committed
[BitwiseCopyable] Drop req from SIMD/Storage.
There are conformers to SIMDStorage (like that in the added test case) which involve an Array (a type that can't conform to BitwiseCopyable). So lift the constraint on SIMDStorage. This in turn requires lifting the constraint on SIMD (otherwise, e.g. `SIMD8<Scalar>` would fail to conform since it has as a member some SIMD8Storage which is only constrained to conform to `SIMDStorage`; the `SIMD8Storage` associatedtype also cannot be constrained to `BitwiseCopyable` because that storage may again not conform as in the test example). rdar://128661878
1 parent 478d8a6 commit 746cf2f

File tree

5 files changed

+223
-14
lines changed

5 files changed

+223
-14
lines changed

stdlib/public/core/SIMDVector.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ prefix operator .!
3131
/// elementwise accesses. Computational operations are defined on the `SIMD`
3232
/// protocol, which refines this protocol, and on the concrete types that
3333
/// conform to `SIMD`.
34-
public protocol SIMDStorage : BitwiseCopyable {
34+
public protocol SIMDStorage {
3535
/// The type of scalars in the vector space.
3636
#if $Embedded
3737
associatedtype Scalar: Hashable
@@ -80,8 +80,7 @@ public protocol SIMDScalar : BitwiseCopyable {
8080
public protocol SIMD<Scalar>:
8181
SIMDStorage,
8282
Hashable,
83-
ExpressibleByArrayLiteral,
84-
BitwiseCopyable
83+
ExpressibleByArrayLiteral
8584
{
8685
/// The mask type resulting from pointwise comparisons of this vector type.
8786
associatedtype MaskStorage: SIMD
@@ -96,8 +95,7 @@ public protocol SIMD<Scalar>:
9695
Codable,
9796
Hashable,
9897
CustomStringConvertible,
99-
ExpressibleByArrayLiteral,
100-
BitwiseCopyable
98+
ExpressibleByArrayLiteral
10199
{
102100
/// The mask type resulting from pointwise comparisons of this vector type.
103101
associatedtype MaskStorage: SIMD

stdlib/public/core/SIMDVectorTypes.swift.gyb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ extension SIMD${n}: Sendable
190190
where Scalar: Sendable,
191191
Scalar.SIMD${storageN}Storage: Sendable { }
192192

193+
extension SIMD${n}: BitwiseCopyable
194+
where Scalar: BitwiseCopyable,
195+
Scalar.SIMD${storageN}Storage: BitwiseCopyable { }
196+
193197
%end
194198

195199
extension SIMD3 {
@@ -221,7 +225,7 @@ extension ${Self}: SIMDScalar {
221225
/// Storage for a vector of ${spelledNumbers[n]} integers.
222226
@frozen
223227
@_alignment(${bytes if bytes <= 16 else 16})
224-
public struct SIMD${n}Storage: SIMDStorage, Sendable {
228+
public struct SIMD${n}Storage: SIMDStorage, Sendable, BitwiseCopyable {
225229

226230
public var _value: Builtin.Vec${n}x${BuiltinName}
227231

@@ -275,7 +279,7 @@ extension ${Self} : SIMDScalar {
275279
/// Storage for a vector of ${spelledNumbers[n]} floating-point values.
276280
@frozen
277281
@_alignment(${bytes if bytes <= 16 else 16})
278-
public struct SIMD${n}Storage: SIMDStorage, Sendable {
282+
public struct SIMD${n}Storage: SIMDStorage, Sendable, BitwiseCopyable {
279283

280284
public var _value: Builtin.Vec${n}xFPIEEE${bits}
281285

test/api-digester/Outputs/stability-stdlib-source-x86_64.swift.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ Func withoutActuallyEscaping(_:do:) has generic signature change from <ClosureTy
4848
Func withoutActuallyEscaping(_:do:) is now without @rethrows
4949

5050
Protocol SIMDScalar has generic signature change from <Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar> to <Self : Swift.BitwiseCopyable, Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMDMaskScalar == Self.SIMDMaskScalar.SIMDMaskScalar, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar>
51-
Protocol SIMDStorage has generic signature change from <Self.Scalar : Swift.Decodable, Self.Scalar : Swift.Encodable, Self.Scalar : Swift.Hashable> to <Self : Swift.BitwiseCopyable, Self.Scalar : Swift.Decodable, Self.Scalar : Swift.Encodable, Self.Scalar : Swift.Hashable>
52-
5351

5452
Protocol AdditiveArithmetic has added inherited protocol Copyable
5553
Protocol AdditiveArithmetic has added inherited protocol Escapable
@@ -148,13 +146,11 @@ Protocol RangeReplaceableCollection has added inherited protocol Copyable
148146
Protocol RangeReplaceableCollection has added inherited protocol Escapable
149147
Protocol RawRepresentable has added inherited protocol Copyable
150148
Protocol RawRepresentable has added inherited protocol Escapable
151-
Protocol SIMD has added inherited protocol BitwiseCopyable
152149
Protocol SIMD has added inherited protocol Copyable
153150
Protocol SIMD has added inherited protocol Escapable
154151
Protocol SIMDScalar has added inherited protocol BitwiseCopyable
155152
Protocol SIMDScalar has added inherited protocol Copyable
156153
Protocol SIMDScalar has added inherited protocol Escapable
157-
Protocol SIMDStorage has added inherited protocol BitwiseCopyable
158154
Protocol SIMDStorage has added inherited protocol Copyable
159155
Protocol SIMDStorage has added inherited protocol Escapable
160156
Protocol Sequence has added inherited protocol Copyable

test/api-digester/stability-stdlib-abi-without-asserts.test

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,8 @@ Func ContiguousArray._reserveCapacityImpl(minimumCapacity:growForAppend:) has ma
179179
// it as a removal with the addition of the RangeSet subscripts
180180
Subscript MutableCollection.subscript(_:) has been removed
181181

182-
Protocol SIMD has added inherited protocol BitwiseCopyable
183182
Protocol SIMDScalar has added inherited protocol BitwiseCopyable
184183
Protocol SIMDScalar has generic signature change from <Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar> to <Self : Swift.BitwiseCopyable, Self == Self.SIMD16Storage.Scalar, Self.SIMD16Storage : Swift.SIMDStorage, Self.SIMD2Storage : Swift.SIMDStorage, Self.SIMD32Storage : Swift.SIMDStorage, Self.SIMD4Storage : Swift.SIMDStorage, Self.SIMD64Storage : Swift.SIMDStorage, Self.SIMD8Storage : Swift.SIMDStorage, Self.SIMDMaskScalar : Swift.FixedWidthInteger, Self.SIMDMaskScalar : Swift.SIMDScalar, Self.SIMDMaskScalar : Swift.SignedInteger, Self.SIMDMaskScalar == Self.SIMDMaskScalar.SIMDMaskScalar, Self.SIMD16Storage.Scalar == Self.SIMD2Storage.Scalar, Self.SIMD2Storage.Scalar == Self.SIMD32Storage.Scalar, Self.SIMD32Storage.Scalar == Self.SIMD4Storage.Scalar, Self.SIMD4Storage.Scalar == Self.SIMD64Storage.Scalar, Self.SIMD64Storage.Scalar == Self.SIMD8Storage.Scalar>
185-
Protocol SIMDStorage has added inherited protocol BitwiseCopyable
186-
Protocol SIMDStorage has generic signature change from <Self.Scalar : Swift.Decodable, Self.Scalar : Swift.Encodable, Self.Scalar : Swift.Hashable> to <Self : Swift.BitwiseCopyable, Self.Scalar : Swift.Decodable, Self.Scalar : Swift.Encodable, Self.Scalar : Swift.Hashable>
187184
Protocol _Pointer has added inherited protocol BitwiseCopyable
188185

189186
Protocol AdditiveArithmetic has added inherited protocol Copyable

test/stdlib/SIMD-nontrivial.swift

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
struct SIMDArrayScalar: BinaryFloatingPoint, SIMDScalar, Codable {
4+
5+
typealias Impl = Float32
6+
7+
typealias RawExponent = Impl.RawExponent
8+
typealias FloatLiteralType = Impl.FloatLiteralType
9+
typealias Exponent = Impl.Exponent
10+
typealias Stride = Impl.Stride
11+
typealias IntegerLiteralType = Impl.IntegerLiteralType
12+
typealias RawSignificand = Impl.RawSignificand
13+
14+
typealias SIMDMaskScalar = Impl.SIMDMaskScalar
15+
typealias SIMD2Storage = SIMDArray<SIMDArrayCount2>
16+
typealias SIMD4Storage = SIMDArray<SIMDArrayCount4>
17+
typealias SIMD8Storage = SIMDArray<SIMDArrayCount8>
18+
typealias SIMD16Storage = SIMDArray<SIMDArrayCount16>
19+
typealias SIMD32Storage = SIMDArray<SIMDArrayCount32>
20+
typealias SIMD64Storage = SIMDArray<SIMDArrayCount64>
21+
22+
var impl: Impl
23+
24+
static var exponentBitCount: Int { Impl.exponentBitCount }
25+
26+
static var significandBitCount: Int { Impl.significandBitCount }
27+
28+
var exponentBitPattern: RawExponent { impl.exponentBitPattern }
29+
30+
var significandBitPattern: RawSignificand { impl.significandBitPattern }
31+
32+
var binade: SIMDArrayScalar { SIMDArrayScalar(impl.binade) }
33+
34+
var significandWidth: Int { impl.significandWidth }
35+
36+
static var nan: SIMDArrayScalar { SIMDArrayScalar(Impl.nan) }
37+
38+
static var signalingNaN: SIMDArrayScalar { SIMDArrayScalar(Impl.signalingNaN) }
39+
40+
static var infinity: SIMDArrayScalar { SIMDArrayScalar(Impl.infinity) }
41+
42+
static var greatestFiniteMagnitude: SIMDArrayScalar { SIMDArrayScalar(Impl.greatestFiniteMagnitude) }
43+
44+
static var pi: SIMDArrayScalar { SIMDArrayScalar(Impl.pi) }
45+
46+
var ulp: SIMDArrayScalar { SIMDArrayScalar(impl.ulp) }
47+
48+
static var leastNormalMagnitude: SIMDArrayScalar { SIMDArrayScalar(Impl.leastNormalMagnitude) }
49+
50+
static var leastNonzeroMagnitude: SIMDArrayScalar { SIMDArrayScalar(Impl.leastNonzeroMagnitude) }
51+
52+
var sign: FloatingPointSign { impl.sign }
53+
54+
var exponent: Exponent { impl.exponent }
55+
56+
var significand: SIMDArrayScalar { SIMDArrayScalar(impl.significand) }
57+
58+
var nextUp: SIMDArrayScalar { SIMDArrayScalar(impl.nextUp) }
59+
60+
var isNormal: Bool { impl.isNormal }
61+
62+
var isFinite: Bool { impl.isFinite }
63+
64+
var isZero: Bool { impl.isZero }
65+
66+
var isSubnormal: Bool { impl.isSubnormal }
67+
68+
var isInfinite: Bool { impl.isInfinite }
69+
70+
var isNaN: Bool { impl.isNaN }
71+
72+
var isSignalingNaN: Bool { impl.isSignalingNaN }
73+
74+
var isCanonical: Bool { impl.isCanonical }
75+
76+
init() {
77+
impl = Impl()
78+
}
79+
80+
init(_ impl: Impl) {
81+
self.impl = impl
82+
}
83+
84+
init(floatLiteral: FloatLiteralType) {
85+
impl = Impl(floatLiteral: floatLiteral)
86+
}
87+
88+
init?<T>(exactly source: T) where T: BinaryInteger {
89+
impl = Impl(source)
90+
}
91+
92+
init(sign: FloatingPointSign, exponent: Float32.Exponent, significand: SIMDArrayScalar) {
93+
impl = Impl(sign: sign, exponent: exponent, significand: significand.impl)
94+
}
95+
96+
init(integerLiteral value: Float32.IntegerLiteralType) {
97+
impl = Impl(integerLiteral: value)
98+
}
99+
100+
init(sign: FloatingPointSign, exponentBitPattern: RawExponent, significandBitPattern: RawSignificand) {
101+
impl = Impl(sign: sign, exponentBitPattern: exponentBitPattern, significandBitPattern: significandBitPattern)
102+
}
103+
104+
var magnitude: SIMDArrayScalar { SIMDArrayScalar(impl.magnitude) }
105+
106+
static func * (lhs: SIMDArrayScalar, rhs: SIMDArrayScalar) -> SIMDArrayScalar {
107+
SIMDArrayScalar(lhs.impl * rhs.impl)
108+
}
109+
110+
static func *= (lhs: inout SIMDArrayScalar, rhs: SIMDArrayScalar) {
111+
lhs.impl *= rhs.impl
112+
}
113+
114+
static func + (lhs: SIMDArrayScalar, rhs: SIMDArrayScalar) -> SIMDArrayScalar {
115+
SIMDArrayScalar(lhs.impl + rhs.impl)
116+
}
117+
118+
static func - (lhs: SIMDArrayScalar, rhs: SIMDArrayScalar) -> SIMDArrayScalar {
119+
SIMDArrayScalar(lhs.impl - rhs.impl)
120+
}
121+
122+
static func / (lhs: SIMDArrayScalar, rhs: SIMDArrayScalar) -> SIMDArrayScalar {
123+
SIMDArrayScalar(lhs.impl / rhs.impl)
124+
}
125+
126+
static func /= (lhs: inout SIMDArrayScalar, rhs: SIMDArrayScalar) {
127+
lhs.impl /= rhs.impl
128+
}
129+
130+
func distance(to other: SIMDArrayScalar) -> Float32.Stride {
131+
impl.distance(to: other.impl)
132+
}
133+
134+
func advanced(by n: Float32.Stride) -> SIMDArrayScalar {
135+
SIMDArrayScalar(impl.advanced(by: n))
136+
}
137+
138+
func isEqual(to other: SIMDArrayScalar) -> Bool {
139+
impl.isEqual(to: other.impl)
140+
}
141+
142+
func isLess(than other: SIMDArrayScalar) -> Bool {
143+
impl.isLess(than: other.impl)
144+
}
145+
146+
func isLessThanOrEqualTo(_ other: SIMDArrayScalar) -> Bool {
147+
impl.isLessThanOrEqualTo(other.impl)
148+
}
149+
150+
mutating func formRemainder(dividingBy other: SIMDArrayScalar) {
151+
impl.formRemainder(dividingBy: other.impl)
152+
}
153+
154+
mutating func formTruncatingRemainder(dividingBy other: SIMDArrayScalar) {
155+
impl.formTruncatingRemainder(dividingBy: other.impl)
156+
}
157+
158+
mutating func formSquareRoot() {
159+
impl.formSquareRoot()
160+
}
161+
162+
mutating func addProduct(_ lhs: SIMDArrayScalar, _ rhs: SIMDArrayScalar) {
163+
impl.addProduct(lhs.impl, rhs.impl)
164+
}
165+
166+
mutating func round(_ rule: FloatingPointRoundingRule) {
167+
impl.round(rule)
168+
}
169+
}
170+
171+
protocol SIMDArrayCount {
172+
static var value: Int { get }
173+
}
174+
175+
struct SIMDArrayCount2: SIMDArrayCount {
176+
static var value: Int { 2 }
177+
}
178+
179+
struct SIMDArrayCount4: SIMDArrayCount {
180+
static var value: Int { 4 }
181+
}
182+
183+
struct SIMDArrayCount8: SIMDArrayCount {
184+
static var value: Int { 8 }
185+
}
186+
187+
struct SIMDArrayCount16: SIMDArrayCount {
188+
static var value: Int { 16 }
189+
}
190+
191+
struct SIMDArrayCount32: SIMDArrayCount {
192+
static var value: Int { 32 }
193+
}
194+
195+
struct SIMDArrayCount64: SIMDArrayCount {
196+
static var value: Int { 64 }
197+
}
198+
199+
struct SIMDArray<Count: SIMDArrayCount>: SIMDStorage {
200+
typealias Scalar = SIMDArrayScalar
201+
202+
var scalarCount: Int { Count.value }
203+
204+
var storage: [Scalar]
205+
206+
init() {
207+
storage = [SIMDArrayScalar](repeating: 0, count: Count.value)
208+
}
209+
210+
subscript(index: Int) -> Scalar {
211+
get { storage[index] }
212+
set(newValue) { storage[index] = newValue }
213+
}
214+
}

0 commit comments

Comments
 (0)