From 0536c9ac651161b1b2afddfa2d834291be5366f3 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 6 Jun 2024 04:13:08 +0000 Subject: [PATCH 1/2] [Synchronization] Fix wasm atomic intrinsic declarations Otherwise, isel will not be able to select the desired atomic instructions. --- stdlib/public/Synchronization/Mutex/WasmImpl.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift index 8fa84b0ff4828..d2729f4d54cef 100644 --- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift +++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift @@ -13,19 +13,19 @@ // Note: All atomic accesses on WASM are sequentially consistent regardless of // what ordering we tell LLVM to use. -@_extern(c, "llvm.wasm32.memory.atomic.wait32") +@_extern(c, "llvm.wasm.memory.atomic.wait32") internal func _swift_stdlib_wait( on: UnsafePointer, expected: UInt32, timeout: Int64 ) -> UInt32 -@_extern(c, "llvm.wasm32.memory.atomic.notify") -internal func _swift_stdlib_wake(on: UnsafePointer, count: UInt32) +@_extern(c, "llvm.wasm.memory.atomic.notify") +internal func _swift_stdlib_wake(on: UnsafePointer, count: UInt32) -> UInt32 extension Atomic where Value == _MutexHandle.State { internal borrowing func _wait(expected: _MutexHandle.State) { - _swift_stdlib_wait( + _ = _swift_stdlib_wait( on: .init(_rawAddress), expected: expected.rawValue, @@ -36,7 +36,7 @@ extension Atomic where Value == _MutexHandle.State { internal borrowing func _wake() { // Only wake up 1 thread - _swift_stdlib_wake(on: .init(_rawAddress), count: 1) + _ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1) } } From 2dbbbcf64656072d5b43d7a5139c5efd3a4e9296 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 6 Jun 2024 04:14:46 +0000 Subject: [PATCH 2/2] [Synchronization] Skip atomic operations in single-threaded mode on WebAssembly Use of atomics instructions requires the support of threads proposal and it's not widely supported yet. So we should enable actual atomic operations only when targeting wasm32-uknown-wasip1-threads. --- stdlib/public/Synchronization/Mutex/WasmImpl.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift index d2729f4d54cef..6c58628c40eeb 100644 --- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift +++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift @@ -25,6 +25,7 @@ internal func _swift_stdlib_wake(on: UnsafePointer, count: UInt32) -> UI extension Atomic where Value == _MutexHandle.State { internal borrowing func _wait(expected: _MutexHandle.State) { + #if _runtime(_multithreaded) _ = _swift_stdlib_wait( on: .init(_rawAddress), expected: expected.rawValue, @@ -32,11 +33,14 @@ extension Atomic where Value == _MutexHandle.State { // A timeout of < 0 means indefinitely. timeout: -1 ) + #endif } internal borrowing func _wake() { + #if _runtime(_multithreaded) // Only wake up 1 thread _ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1) + #endif } }