Skip to content

Commit 48b8228

Browse files
committed
[embedded] Start building Synchronization in embedded Swift mode
1 parent 2ebae40 commit 48b8228

File tree

8 files changed

+98
-19
lines changed

8 files changed

+98
-19
lines changed

docs/EmbeddedSwift/EmbeddedSwiftStatus.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This status table describes which of the following Swift features can be used in
4848

4949
| **Swift Feature** | **Currently Supported In Embedded Swift?** |
5050
|------------------------------------------------------------|-----------------------------------------------------|
51+
| Synchronization module | Yes |
5152
| Swift Concurrency | Partial, experimental (basics of actors and tasks work in single-threaded concurrency mode) |
5253
| C interop | Yes |
5354
| C++ interop | Yes |

docs/EmbeddedSwift/UserManual.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ For (2), external dependencies are also triggered by specific code needing them,
197197
- **atomics instrinsics**
198198
- on CPU architectures that don't have direct load-acquire/store-release support in the ISA, LLVM calls helper functions for atomic operations
199199
- needed by refcounting in the Embedded Swift runtime (so any class usage will trigger this dependency)
200+
- also needed when using atomics from the Synchronization module
200201
- **multiplication/division/modulo instrinsics**
201202
- on CPU architectures that don't have direct support for the math operations in the ISA
202203
- dependency (on Mach-O): `__divti3`

stdlib/public/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
206206
"x86_64 x86_64-unknown-none-elf x86_64-unknown-none-elf"
207207
# Without specifying the `windows` component LLVM refuses to
208208
# generate valid COFF object files.
209-
"i686 i686-unknown-windows-coff i686-unknown-windows-coff"
210-
"x86_64 x86_64-unknown-windows-coff x86_64-unknown-windows-coff"
209+
"i686 i686-unknown-windows-msvc i686-unknown-windows-msvc"
210+
"x86_64 x86_64-unknown-windows-msvc x86_64-unknown-windows-msvc"
211211
)
212212
endif()
213213

stdlib/public/Synchronization/AtomicMemoryOrderings.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ extension AtomicLoadOrdering: Hashable {
9696
}
9797

9898
@available(SwiftStdlib 6.0, *)
99+
@_unavailableInEmbedded
99100
extension AtomicLoadOrdering: CustomStringConvertible {
100101
@available(SwiftStdlib 6.0, *)
101102
public var description: String {
@@ -191,6 +192,7 @@ extension AtomicStoreOrdering: Hashable {
191192
}
192193

193194
@available(SwiftStdlib 6.0, *)
195+
@_unavailableInEmbedded
194196
extension AtomicStoreOrdering: CustomStringConvertible {
195197
@available(SwiftStdlib 6.0, *)
196198
public var description: String {
@@ -315,6 +317,7 @@ extension AtomicUpdateOrdering: Hashable {
315317
}
316318

317319
@available(SwiftStdlib 6.0, *)
320+
@_unavailableInEmbedded
318321
extension AtomicUpdateOrdering: CustomStringConvertible {
319322
@available(SwiftStdlib 6.0, *)
320323
public var description: String {

stdlib/public/Synchronization/CMakeLists.txt

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#
1111
#===----------------------------------------------------------------------===#
1212

13-
add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
14-
# List of files here:
13+
set(swift_synchronization_sources
1514
Atomic.swift
1615
AtomicBool.swift
1716
AtomicFloats.swift
@@ -21,23 +20,67 @@ add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES
2120
AtomicPointers.swift
2221
AtomicRepresentable.swift
2322
WordPair.swift
23+
)
24+
set(swift_synchronization_gyb_sources
25+
AtomicIntegers.swift.gyb
26+
AtomicStorage.swift.gyb
27+
)
28+
set(swift_synchronization_swift_compile_flags
29+
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
30+
"-enable-builtin-module"
31+
"-enable-experimental-feature" "RawLayout"
32+
"-enable-experimental-feature" "StaticExclusiveOnly"
33+
)
2434

25-
GYB_SOURCES
26-
AtomicIntegers.swift.gyb
27-
AtomicStorage.swift.gyb
28-
29-
SWIFT_COMPILE_FLAGS
30-
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
31-
"-enable-builtin-module"
32-
"-enable-experimental-feature" "RawLayout"
33-
"-enable-experimental-feature" "StaticExclusiveOnly"
34-
35+
add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
36+
${swift_synchronization_sources}
37+
GYB_SOURCES ${swift_synchronization_gyb_sources}
38+
SWIFT_COMPILE_FLAGS ${swift_synchronization_swift_compile_flags}
3539
LINK_FLAGS
3640
"${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
37-
3841
INSTALL_IN_COMPONENT
3942
stdlib
40-
4143
MACCATALYST_BUILD_FLAVOR
4244
"zippered"
4345
)
46+
47+
# Embedded Synchronization - embedded libraries are built as .swiftmodule only,
48+
# i.e. there is no .o or .a file produced (no binary code is actually produced)
49+
# and only users of a library are going to actually compile any needed code.
50+
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
51+
add_custom_target(embedded-synchronization ALL)
52+
53+
foreach(entry ${EMBEDDED_STDLIB_TARGET_TRIPLES})
54+
string(REGEX REPLACE "[ \t]+" ";" list "${entry}")
55+
list(GET list 0 arch)
56+
list(GET list 1 mod)
57+
list(GET list 2 triple)
58+
59+
set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${mod}")
60+
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")
61+
set(SWIFT_SDK_embedded_ARCH_${arch}_TRIPLE "${triple}")
62+
set(SWIFT_SDK_embedded_PATH ${SWIFT_SDK_OSX_PATH})
63+
set(SWIFT_SDK_embedded_ARCH_${arch}_PATH ${SWIFT_SDK_OSX_PATH})
64+
set(SWIFT_SDK_embedded_USE_ISYSROOT TRUE)
65+
add_swift_target_library_single(
66+
embedded-synchronization-${mod}
67+
swiftSynchronization
68+
ONLY_SWIFTMODULE
69+
IS_FRAGILE
70+
71+
${swift_synchronization_sources}
72+
GYB_SOURCES ${swift_synchronization_gyb_sources}
73+
74+
SWIFT_COMPILE_FLAGS
75+
${swift_synchronization_swift_compile_flags}
76+
-Xcc -D__MACH__ -Xcc -D__APPLE__ -Xcc -ffreestanding -enable-experimental-feature Embedded
77+
78+
MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded"
79+
SDK "embedded"
80+
ARCHITECTURE "${arch}"
81+
DEPENDS embedded-stdlib-${mod}
82+
INSTALL_IN_COMPONENT stdlib
83+
)
84+
add_dependencies(embedded-synchronization embedded-synchronization-${mod})
85+
endforeach()
86+
endif()

stdlib/public/Synchronization/WordPair.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ extension WordPair: Hashable {
184184
}
185185

186186
@available(SwiftStdlib 6.0, *)
187+
@_unavailableInEmbedded
187188
extension WordPair: CustomStringConvertible {
188189
/// A string that represents the contents of the word pair.
189190
@available(SwiftStdlib 6.0, *)
@@ -193,6 +194,7 @@ extension WordPair: CustomStringConvertible {
193194
}
194195

195196
@available(SwiftStdlib 6.0, *)
197+
@_unavailableInEmbedded
196198
extension WordPair: CustomDebugStringConvertible {
197199
/// A string that represents the contents of the word pair, suitable for
198200
/// debugging.

stdlib/public/core/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,6 @@ add_swift_target_library(swiftCore
411411
# Embedded standard library - embedded libraries are built as .swiftmodule only,
412412
# i.e. there is no .o or .a file produced (no binary code is actually produced)
413413
# and only users of a library are going to actually compile any needed code.
414-
#
415-
# For now, we build a hardcoded list of target triples of the embedded stdlib,
416-
# and only when building Swift on macOS.
417414
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
418415
add_custom_target(embedded-stdlib ALL)
419416

test/embedded/synchronization.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -parse-as-library -enable-experimental-feature Embedded -disable-availability-checking -c -o %t/main.o
3+
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
4+
// RUN: %target-run %t/a.out | %FileCheck %s
5+
6+
// REQUIRES: swift_in_compiler
7+
// REQUIRES: executable_test
8+
// REQUIRES: optimized_stdlib
9+
// REQUIRES: OS=macosx || OS=linux-gnu
10+
11+
import Synchronization
12+
13+
@main
14+
struct Main {
15+
static let x = Atomic(128)
16+
17+
static func main() {
18+
let old = x.load(ordering: .relaxed)
19+
x.store(42, ordering: .relaxed)
20+
let new = x.load(ordering: .relaxed)
21+
print(old) // CHECK: 128
22+
print(new) // CHECK: 42
23+
let old2 = x.exchange(12, ordering: .acquiring)
24+
print(old2) // CHECK: 42
25+
let (exchanged, original) = x.compareExchange(expected: 128, desired: 316, ordering: .sequentiallyConsistent)
26+
print(exchanged) // CHECK: false
27+
print(original) // CHECK: 12
28+
let (exchanged2, original2) = x.compareExchange(expected: 12, desired: 316, ordering: .sequentiallyConsistent)
29+
print(exchanged2) // CHECK: true
30+
print(original2) // CHECK: 12
31+
}
32+
}

0 commit comments

Comments
 (0)