Repository navigation
[wasm] Enable multi-threaded wasi-threads Concurrency - #92018
Conversation
|
@swift-ci Please test WebAssembly |
|
The consuming executor is up: swiftlang/swift-platform-executors#33 (issue #30). Its wasi-threads probe passes against a 6.3.2 build of this change; the description now links both. |
|
@ktoso could you trigger CI on this one ( |
|
@MaxDesiatov this is the concurrency-runtime piece of the It overlaps #92238 textually in |
|
This PR looks heavily AI-generated, and is quite verbose for what you're actually trying to do. At the very least I'd expect PR description and comments in the diff to be cleaned up for readability.
It's not supposed to reach the bot. There's no separate Wasm CI job, all Linux jobs cross-compile and test for Wasm by default to reuse the existing Linux host toolchain. |
|
Hi @MaxDesiatov sorry I can cut down on the verbosity and cleanup the PR description. Fundamentally I'm just trying to enable wasi-threads support in swift wasm, and that requires an actual multithreaded executor (swiftlang/swift-platform-executors#33) which I could alternately wire in with the @_spi hooks. But either way we do need a mechanism to compile wasm with multithreaded concurrency. I'm using swift wasm in a large project and currently using my own custom built swift toolchain to enable it, but I can't ship it on the App Store unless it's built with the Xcode toolchain so I'm trying to get this upstream. |
Fixes #30. On the wasi-threads triple wasi-libc maps `pthread_create` onto `wasi_thread_spawn`, so the pthread executors work there with three additions: a `ConditionSelector` (no epoll/kqueue on WASI; `wakeup()` raises a flag and signals, `whenReady` waits with a timed wait for the earliest deadline), C shims for thread creation with an explicit 4 MiB stack, an absolute `CLOCK_REALTIME` deadline for `pthread_cond_timedwait`, and no-op thread-name calls, and a `PlatformExecutorFactory` for WASI. WASI reports one processor, so the pool defaults to four executors; `SWIFT_PLATFORM_DEFAULT_EXECUTOR_POOL_SIZE` sizes it. `Examples/WASIThreads` is a probe that installs the executors as the defaults and checks parallelism, main-thread isolation and delayed scheduling; it exits non-zero on failure. Verified under wasmtime (`-W threads=y,shared-memory=y`) against a 6.3.2 threads stdlib built with `SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY=FALSE` (swiftlang/swift#92018): peak concurrency 4/4, detached hop off and back to the main thread, sleeps on both clocks on time. Native build and tests unchanged. Caveats, documented in the README: the `DefaultExecutorFactory` alias is only honored on WASI with `-disable-availability-checking` until `CustomGlobalExecutors` is enabled by default, and threads SDKs whose stdlib predates #92018 report every thread as the main thread.
ab149c4 to
b0a24b6
Compare
wasmstdlibhelpers.py sets SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY=TRUE for every wasm stdlib, including wasip1-threads, where it makes the runtime treat every thread as the main thread. Set it per product instead, keeping the cooperative global executor on wasip1-threads.
b0a24b6 to
03d70fc
Compare
|
Hi @MaxDesiatov ! I have removed all of the unnecessary comments and simplified the PR description. Also my PR into swift-platform-executors has merged, adding an actual multithreaded executor for wasi-threads. PTAL when you have a chance! 🙏 |
|
@swift-ci test |
We do have WasmKit 0.4.1 included in development snapshots off |
…t is carried today), and Swift in place of Python (what is Python today); the wasi-threads item brought up to date (swiftlang/swift#92018 merged) (#41) Co-authored-by: Claude <344224626+logan-shire-claude[bot]@users.noreply.github.com>
…sip1-threads) (#9) * sdk-check build-threads-runtime: the wasm32-unknown-wasip1-threads runtime from upstream's swift-6.4.2-RELEASE and sdk/patches Swift 6.4.2's stdlib, runtime and Concurrency for the threads triple, and its sysroot with libunwind, libc++abi and libc++ built with threads and WebAssembly exceptions (the standard exnref proposal), built by Swift's own build-script with swift.org's release toolchain as the host compiler (no compiler is built). Sources cloned by tag and checked against their commits: swift, llvm-project (sparse) and swift-experimental-string-processing at swift-6.4.2-RELEASE, wasi-libc at wasi-sdk-31. No fork. sdk/patches/swift (git am): - 0001: swiftlang/swift#92018 (multi-threaded wasi-threads Concurrency), backported to 6.4.2's wasistdlib.py; - 0002: the threads sysroot's C++ runtimes with exceptions, assembly compiled for the target, the llvm-project patches applied when missing; - 0003: no embedded stdlib for the threads triple; - 0004: build-script's --build-wasi-threads-stdlib-only (the sysroot and the threads stdlib alone), in place of a Python shim over build-script. sdk/patches/llvm-project (git apply): llvm/llvm-project#168449 and libunwind's assembly empty on wasm. The package: WASI.sdk, the stdlib's static resources with libswiftCxx.a beside them and -lunwind in static-executable-args.lnk, compiler-rt; checked (every object wasm, the EH and thread symbols, __config_site, no legacy EH instruction). A build from the same inputs is kept. Run.swift: a runner whose output streams to the log, and an environment for run(). * sdk/build.sh wasm-threads: a third wasm bundle, wasm32-unknown-wasip1-threads beside wasm32-unknown-wasip1, plain swift-6.4.2-RELEASE_wasm-threads.artifactbundle.tar.gz: swift.org's wasm bundle with the threads runtime (sdk-check build-threads-runtime) as a second target, Isomer's Foundation family built for both, plain only (no ICU for the threads triple). The threads target's swift-sdk.json and toolset.json: atomics, bulk memory, POSIX threads and local-exec TLS for every compile; -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false for C and C++, and -Xllvm -wasm-enable-eh -Xllvm -wasm-use-legacy-eh=false for Swift (SwiftPM hands C options to swiftc as -Xcc, and swift-frontend aborts on that -mllvm option unless its own LLVM has it); the link a shared memory, imported and exported, with a 1 GiB maximum, and -lunwind. rules_swift reads one swift-sdk.json, the threads target's (wasm_sdk threads = True), so the wasip1 target's is wasip1-swift-sdk.json here; SwiftPM takes either by id. The E2 C++ interop maps fix applies to the threads target's identical maps; its Foundation's CWasiThreadIdentity clang module goes in beside it. SwiftPM builds for the threads target run with the toolchain's clang first on PATH (the image's clang 18 knows no -wasm-use-legacy-eh). Its check, under WasmKit 0.5.0 (by sha256) with --wasi-threads for the threads target: the client, the C++ interop client, and sdk-check threads-clients (a TaskGroup of 64 tasks on a task executor over four pthreads with Foundation on each thread; C++ throwing and catching on four std::threads; the C++ object's EH instructions exnref's alone); then undefined, autolink and pairing; swift-testing on the wasip1 target only (the threads runtime has none). sdk-check proof names the threads target's item check-<bundle>-wasm32-threads; draft registers it with `threads = True`. sdk/README.md: the threads SDK. The pack step's times also follow sdk/patches and the runtime's build. * Isomer SDK: wasm-threads in the sdk matrix * The download check runs as the target's programs do (WasmKit 0.5.0 with --wasi-threads for the threads target) --------- Co-authored-by: Agent <344224964+logan-shire-agent[bot]@users.noreply.github.com> Co-authored-by: Logan Shire <[email protected]>
…, the threaded reactors and their tests again (#347) * Isomer's Swift SDKs isomer-6.4.2.4: a wasm threads bundle, and URLSession's downloads written to a file as they arrive * The wasm threads SDK back: Isomer's Swift SDKs isomer-6.4.2.4's threads bundle (Swift 6.4.2 with swiftlang/swift#92018, libc++ with threads and WebAssembly exceptions) registered beside the wasm SDK, the threaded reactors built and their tests run again (sqlite_file_threads_test, wasm_engine_tests' threaded cases, and wasmkit_threads_test on the Mac and Linux) * AGENTS.md: the threads toolset's -mllvm option reaches clang's links too (rules_swift#1860), a harmless warning --------- Co-authored-by: Agent <344224964+logan-shire-agent[bot]@users.noreply.github.com>
wasmstdlibhelpers.py sets SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY=TRUE for every wasm stdlib, including wasm32-unknown-wasip1-threads, where it makes the runtime treat every thread as the main thread. That rules out a multi-threaded executor such as swiftlang/swift-platform-executors#33. - Set the flag per product: TRUE for WASI and Emscripten, FALSE for WASI threads. - Keep the cooperative global executor on WASI threads, and allow it in stdlib/CMakeLists.txt when a threading package is present. - Add a wasi_threads lit feature and a test that MainActor.assumeIsolated traps on a worker thread. CI's WasmKit can't run wasi-threads, so the test was run locally under wasmtime. (cherry picked from commit 8ddc55c) 6.4.2 backport: use the existing WASI product hooks in wasistdlib.py; there is no shared helper or Emscripten product, and the worker trap uses the 6.4.2 CooperativeExecutor diagnostic.
…NCURRENCY=FALSE (swiftlang/swift#92018 backport, -8)
wasmstdlibhelpers.py sets SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY=TRUE
for every wasm stdlib, including wasm32-unknown-wasip1-threads, where it
makes the runtime treat every thread as the main thread. That rules out
a multi-threaded executor such as swiftlang/swift-platform-executors#33.
WASI threads.
stdlib/CMakeLists.txt when a threading package is present.
traps on a worker thread. CI's WasmKit can't run wasi-threads, so the
test was run locally under wasmtime.