Skip to content

Commit 00a5848

Browse files
Apply Synchronization fixes
1 parent 3517f1d commit 00a5848

4 files changed

+220
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
From c6b128ca7b89d0c42563d4a1d53602fcfd48bea6 Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Sat, 10 Aug 2024 13:50:57 +0000
4+
Subject: [PATCH] [wasm] Annotate errno as SwiftPrivate by apinotes
5+
6+
This patch adds an apinotes file for SwiftWASILibc clang module to mark
7+
`errno` macro hidden from Swift code. This resolves ambiguity between
8+
the C macro definition and the Swift wrapper in WASILibc overlay module.
9+
10+
This change installs the apinotes file to the resource directories for
11+
both lib/swift/apinotes and lib/swift_static/apinotes.
12+
---
13+
stdlib/public/Platform/CMakeLists.txt | 28 +++++++++++++++++++
14+
stdlib/public/Platform/SwiftWASILibc.apinotes | 5 ++++
15+
test/stdlib/WASILibcAPI.swift | 19 +++++++++++++
16+
3 files changed, 52 insertions(+)
17+
create mode 100644 stdlib/public/Platform/SwiftWASILibc.apinotes
18+
create mode 100644 test/stdlib/WASILibcAPI.swift
19+
20+
diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt
21+
index 7e709923ffc..3d798e48304 100644
22+
--- a/stdlib/public/Platform/CMakeLists.txt
23+
+++ b/stdlib/public/Platform/CMakeLists.txt
24+
@@ -508,6 +508,34 @@ if("WASI" IN_LIST SWIFT_SDKS)
25+
DESTINATION "lib/swift_static/${arch_subdir}"
26+
COMPONENT sdk-overlay)
27+
endif()
28+
+
29+
+ set(wasilibc_apinotes_source "SwiftWASILibc.apinotes")
30+
+ add_custom_command_target(
31+
+ copy_wasilibc_apinotes_resource
32+
+ COMMAND
33+
+ "${CMAKE_COMMAND}" "-E" "make_directory" ${SWIFTLIB_DIR}/apinotes ${SWIFTSTATICLIB_DIR}/apinotes
34+
+ COMMAND
35+
+ "${CMAKE_COMMAND}" "-E" "copy_if_different"
36+
+ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTLIB_DIR}/apinotes
37+
+ COMMAND
38+
+ "${CMAKE_COMMAND}" "-E" "copy_if_different"
39+
+ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTSTATICLIB_DIR}/apinotes
40+
+ OUTPUT
41+
+ ${SWIFTLIB_DIR}/apinotes/${wasilibc_apinotes_source}
42+
+ ${SWIFTSTATICLIB_DIR}/apinotes/${wasilibc_apinotes_source}
43+
+ COMMENT "Copying WASILibc API notes to resource directories")
44+
+
45+
+ list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_apinotes_resource})
46+
+ add_dependencies(sdk-overlay ${copy_wasilibc_apinotes_resource})
47+
+ swift_install_in_component(FILES "${wasilibc_apinotes_source}"
48+
+ DESTINATION "lib/swift/apinotes"
49+
+ COMPONENT sdk-overlay)
50+
+ if(SWIFT_BUILD_STATIC_STDLIB)
51+
+ swift_install_in_component(FILES "${wasilibc_apinotes_source}"
52+
+ DESTINATION "lib/swift_static/apinotes"
53+
+ COMPONENT sdk-overlay)
54+
+ endif()
55+
+
56+
endforeach()
57+
endif()
58+
add_custom_target(wasilibc_modulemap DEPENDS ${wasilibc_modulemap_target_list})
59+
diff --git a/stdlib/public/Platform/SwiftWASILibc.apinotes b/stdlib/public/Platform/SwiftWASILibc.apinotes
60+
new file mode 100644
61+
index 00000000000..001acc7ebb5
62+
--- /dev/null
63+
+++ b/stdlib/public/Platform/SwiftWASILibc.apinotes
64+
@@ -0,0 +1,5 @@
65+
+Name: SwiftWASILibc
66+
+Globals:
67+
+ # errno macro is importable but we provide explicit Swift wrapper
68+
+ - Name: errno
69+
+ SwiftPrivate: true
70+
diff --git a/test/stdlib/WASILibcAPI.swift b/test/stdlib/WASILibcAPI.swift
71+
new file mode 100644
72+
index 00000000000..fe599bb9f38
73+
--- /dev/null
74+
+++ b/test/stdlib/WASILibcAPI.swift
75+
@@ -0,0 +1,19 @@
76+
+// RUN: %target-swift-frontend -typecheck -swift-version 6 %s -verify
77+
+// REQUIRES: executable_test
78+
+// REQUIRES: OS=wasi
79+
+
80+
+import WASILibc
81+
+
82+
+// errno is a global thread-local variable, so it should be accessible
83+
+// from any context.
84+
+
85+
+enum TestErrno {
86+
+ static func testSyncContext() {
87+
+ _ = errno
88+
+ errno = 0
89+
+ }
90+
+ static func testAsyncContext() async {
91+
+ _ = errno
92+
+ errno = 0
93+
+ }
94+
+}
95+
--
96+
2.43.2
97+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
From 261c0c6bf343aee915e5969e417c13e7bdd86912 Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Wed, 5 Jun 2024 17:15:43 +0000
4+
Subject: [PATCH] [Synchronization] Fix Wasm mutex build
5+
6+
This change fixes the following build error happening on Wasm:
7+
```
8+
error: referencing instance method '_wait(expected:)' on 'Atomic' requires the types '_MutexHandle.State' and 'UInt32' be equivalent
9+
```
10+
---
11+
stdlib/public/Synchronization/Mutex/WasmImpl.swift | 2 +-
12+
1 file changed, 1 insertion(+), 1 deletion(-)
13+
14+
diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift
15+
index 807eb3d8c64..8fa84b0ff48 100644
16+
--- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift
17+
+++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift
18+
@@ -23,7 +23,7 @@ internal func _swift_stdlib_wait(
19+
@_extern(c, "llvm.wasm32.memory.atomic.notify")
20+
internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32)
21+
22+
-extension Atomic where Value == UInt32 {
23+
+extension Atomic where Value == _MutexHandle.State {
24+
internal borrowing func _wait(expected: _MutexHandle.State) {
25+
_swift_stdlib_wait(
26+
on: .init(_rawAddress),
27+
--
28+
2.43.2
29+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
From bcbf0f86c459cb9b8ffefc7bea47cd4bea04fbeb Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Thu, 6 Jun 2024 04:13:08 +0000
4+
Subject: [PATCH] [Synchronization] Fix wasm atomic intrinsic declarations
5+
6+
Otherwise, isel will not be able to select the desired atomic
7+
instructions.
8+
---
9+
stdlib/public/Synchronization/Mutex/WasmImpl.swift | 10 +++++-----
10+
1 file changed, 5 insertions(+), 5 deletions(-)
11+
12+
diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift
13+
index 8fa84b0ff48..d2729f4d54c 100644
14+
--- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift
15+
+++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift
16+
@@ -13,19 +13,19 @@
17+
// Note: All atomic accesses on WASM are sequentially consistent regardless of
18+
// what ordering we tell LLVM to use.
19+
20+
-@_extern(c, "llvm.wasm32.memory.atomic.wait32")
21+
+@_extern(c, "llvm.wasm.memory.atomic.wait32")
22+
internal func _swift_stdlib_wait(
23+
on: UnsafePointer<UInt32>,
24+
expected: UInt32,
25+
timeout: Int64
26+
) -> UInt32
27+
28+
-@_extern(c, "llvm.wasm32.memory.atomic.notify")
29+
-internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32)
30+
+@_extern(c, "llvm.wasm.memory.atomic.notify")
31+
+internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32) -> UInt32
32+
33+
extension Atomic where Value == _MutexHandle.State {
34+
internal borrowing func _wait(expected: _MutexHandle.State) {
35+
- _swift_stdlib_wait(
36+
+ _ = _swift_stdlib_wait(
37+
on: .init(_rawAddress),
38+
expected: expected.rawValue,
39+
40+
@@ -36,7 +36,7 @@ extension Atomic where Value == _MutexHandle.State {
41+
42+
internal borrowing func _wake() {
43+
// Only wake up 1 thread
44+
- _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
45+
+ _ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
46+
}
47+
}
48+
49+
--
50+
2.43.2
51+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
From def82827e4bb3274e0ac3a86e761cb46b2a28a6d Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Thu, 6 Jun 2024 04:14:46 +0000
4+
Subject: [PATCH] [Synchronization] Skip atomic operations in single-threaded
5+
mode on WebAssembly
6+
7+
Use of atomics instructions requires the support of threads proposal and
8+
it's not widely supported yet. So we should enable actual atomic
9+
operations only when targeting wasm32-uknown-wasip1-threads.
10+
---
11+
stdlib/public/Synchronization/Mutex/WasmImpl.swift | 4 ++++
12+
1 file changed, 4 insertions(+)
13+
14+
diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift
15+
index d2729f4d54c..6c58628c40e 100644
16+
--- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift
17+
+++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift
18+
@@ -25,6 +25,7 @@ internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32) -> UI
19+
20+
extension Atomic where Value == _MutexHandle.State {
21+
internal borrowing func _wait(expected: _MutexHandle.State) {
22+
+ #if _runtime(_multithreaded)
23+
_ = _swift_stdlib_wait(
24+
on: .init(_rawAddress),
25+
expected: expected.rawValue,
26+
@@ -32,11 +33,14 @@ extension Atomic where Value == _MutexHandle.State {
27+
// A timeout of < 0 means indefinitely.
28+
timeout: -1
29+
)
30+
+ #endif
31+
}
32+
33+
internal borrowing func _wake() {
34+
+ #if _runtime(_multithreaded)
35+
// Only wake up 1 thread
36+
_ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
37+
+ #endif
38+
}
39+
}
40+
41+
--
42+
2.43.2
43+

0 commit comments

Comments
 (0)