Skip to content

Commit 65343ad

Browse files
authored
[Embedded] Enable SIMD types in embedded Swift (#70674)
* [Embedded] Enable SIMD types in embedded Swift * Incorporate feedback and add test * Update simd.swift
1 parent a566c10 commit 65343ad

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ split_embedded_sources(
252252
if(SWIFT_STDLIB_ENABLE_VECTOR_TYPES)
253253
list(APPEND SWIFTLIB_SOURCES SIMDVector.swift)
254254
list(APPEND SWIFTLIB_GYB_SOURCES SIMDConcreteOperations.swift.gyb SIMDVectorTypes.swift.gyb)
255+
256+
list(APPEND SWIFTLIB_EMBEDDED_SOURCES SIMDVector.swift)
257+
list(APPEND SWIFTLIB_EMBEDDED_GYB_SOURCES SIMDConcreteOperations.swift.gyb SIMDVectorTypes.swift.gyb)
255258
endif()
256259

257260
list(APPEND SWIFTLIB_EMBEDDED_SOURCES

stdlib/public/core/SIMDVector.swift

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ prefix operator .!
3333
/// conform to `SIMD`.
3434
public protocol SIMDStorage {
3535
/// The type of scalars in the vector space.
36+
#if $Embedded
37+
associatedtype Scalar: Hashable
38+
#else
3639
associatedtype Scalar: Codable, Hashable
37-
40+
#endif
41+
3842
/// The number of scalars, or elements, in the vector.
3943
var scalarCount: Int { get }
4044

@@ -71,6 +75,20 @@ public protocol SIMDScalar {
7175
associatedtype SIMD64Storage: SIMDStorage where SIMD64Storage.Scalar == Self
7276
}
7377

78+
#if $Embedded
79+
/// A SIMD vector of a fixed number of elements.
80+
public protocol SIMD<Scalar>:
81+
SIMDStorage,
82+
Hashable,
83+
ExpressibleByArrayLiteral
84+
{
85+
/// The mask type resulting from pointwise comparisons of this vector type.
86+
associatedtype MaskStorage: SIMD
87+
where MaskStorage.Scalar: FixedWidthInteger & SignedInteger
88+
}
89+
90+
#else
91+
7492
/// A SIMD vector of a fixed number of elements.
7593
public protocol SIMD<Scalar>:
7694
SIMDStorage,
@@ -84,6 +102,8 @@ public protocol SIMD<Scalar>:
84102
where MaskStorage.Scalar: FixedWidthInteger & SignedInteger
85103
}
86104

105+
#endif
106+
87107
extension SIMD {
88108
/// The valid indices for subscripting the vector.
89109
@_transparent
@@ -111,7 +131,9 @@ extension SIMD {
111131
public func hash(into hasher: inout Hasher) {
112132
for i in indices { hasher.combine(self[i]) }
113133
}
114-
134+
135+
#if !$Embedded
136+
115137
/// Encodes the scalars of this vector into the given encoder in an unkeyed
116138
/// container.
117139
///
@@ -125,7 +147,7 @@ extension SIMD {
125147
try container.encode(self[i])
126148
}
127149
}
128-
150+
129151
/// Creates a new vector by decoding scalars from the given decoder.
130152
///
131153
/// This initializer throws an error if reading from the decoder fails, or
@@ -147,14 +169,16 @@ extension SIMD {
147169
self[i] = try container.decode(Scalar.self)
148170
}
149171
}
150-
172+
151173
/// A textual description of the vector.
152174
public var description: String {
153175
get {
154176
return "\(Self.self)(" + indices.map({"\(self[$0])"}).joined(separator: ", ") + ")"
155177
}
156178
}
157-
179+
180+
#endif
181+
158182
/// A vector mask with the result of a pointwise equality comparison.
159183
///
160184
/// Equivalent to:
@@ -541,6 +565,7 @@ extension SIMD where Scalar: FixedWidthInteger {
541565

542566
/// Returns a vector with random values from within the specified range in
543567
/// all lanes, using the given generator as a source for randomness.
568+
@_unavailableInEmbedded
544569
@inlinable
545570
public static func random<T: RandomNumberGenerator>(
546571
in range: Range<Scalar>,
@@ -555,14 +580,16 @@ extension SIMD where Scalar: FixedWidthInteger {
555580

556581
/// Returns a vector with random values from within the specified range in
557582
/// all lanes.
583+
@_unavailableInEmbedded
558584
@inlinable
559585
public static func random(in range: Range<Scalar>) -> Self {
560586
var g = SystemRandomNumberGenerator()
561587
return Self.random(in: range, using: &g)
562588
}
563-
589+
564590
/// Returns a vector with random values from within the specified range in
565591
/// all lanes, using the given generator as a source for randomness.
592+
@_unavailableInEmbedded
566593
@inlinable
567594
public static func random<T: RandomNumberGenerator>(
568595
in range: ClosedRange<Scalar>,
@@ -577,11 +604,13 @@ extension SIMD where Scalar: FixedWidthInteger {
577604

578605
/// Returns a vector with random values from within the specified range in
579606
/// all lanes.
607+
@_unavailableInEmbedded
580608
@inlinable
581609
public static func random(in range: ClosedRange<Scalar>) -> Self {
582610
var g = SystemRandomNumberGenerator()
583611
return Self.random(in: range, using: &g)
584612
}
613+
585614
}
586615

587616
extension SIMD where Scalar: FloatingPoint {
@@ -608,6 +637,7 @@ extension SIMD where Scalar: FloatingPoint {
608637
}
609638
}
610639

640+
@_unavailableInEmbedded
611641
extension SIMD
612642
where Scalar: BinaryFloatingPoint, Scalar.RawSignificand: FixedWidthInteger {
613643
/// Returns a vector with random values from within the specified range in
@@ -695,6 +725,7 @@ public struct SIMDMask<Storage>: SIMD
695725
}
696726
}
697727

728+
@_unavailableInEmbedded
698729
extension SIMDMask {
699730
/// Returns a vector mask with `true` or `false` randomly assigned in each
700731
/// lane, using the given generator as a source for randomness.

stdlib/public/core/SIMDVectorTypes.swift.gyb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ extension SIMD${n} where Scalar: FixedWidthInteger {
155155
}
156156
}
157157

158+
@_unavailableInEmbedded
158159
extension SIMD${n}: CustomDebugStringConvertible {
159160
public var debugDescription: String {
160161
return "SIMD${n}<\(Scalar.self)>(${', '.join(map(lambda c:

test/embedded/simd.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -parse-as-library -enable-experimental-feature Embedded -c -o %t/main.o
4+
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
5+
// RUN: %target-run %t/a.out | %FileCheck %s
6+
7+
// REQUIRES: swift_in_compiler
8+
// REQUIRES: executable_test
9+
// REQUIRES: optimized_stdlib
10+
// REQUIRES: OS=macosx || OS=linux-gnu
11+
12+
@inline(never)
13+
public func sum(_ v: SIMD4<Float>) -> Float {
14+
return v.x + v.y + v.z + v.w
15+
}
16+
17+
@main
18+
struct Main {
19+
static func main() {
20+
let v = SIMD4<Float>(1.0, 2.0, 3.0, 4.0)
21+
guard v.sum() == sum(v) else {
22+
fatalError()
23+
}
24+
25+
// CHECK: success
26+
print("success")
27+
}
28+
}

0 commit comments

Comments
 (0)