Skip to content

[wasm] Enable multi-threaded wasi-threads Concurrency - #92018

Merged
MaxDesiatov merged 1 commit into
swiftlang:mainfrom
AttilaTheFun:wasi-threads-single-threaded-flag
Oct 9, 2026
Merged

MaxDesiatov merged 1 commit into
swiftlang:mainfrom
AttilaTheFun:wasi-threads-single-threaded-flag

Conversation

@AttilaTheFun

@AttilaTheFun AttilaTheFun commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

@AttilaTheFun

Copy link
Copy Markdown
Contributor Author

@swift-ci Please test WebAssembly

@AttilaTheFun

Copy link
Copy Markdown
Contributor Author

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.

@AttilaTheFun

Copy link
Copy Markdown
Contributor Author

@ktoso could you trigger CI on this one (@swift-ci Please test WebAssembly from me doesn't reach the bot) and take a look when you have a moment? It's a two-line build-script fix plus a lit test; the executor that depends on it is under review in swiftlang/swift-platform-executors#33.

@AttilaTheFun

Copy link
Copy Markdown
Contributor Author

@MaxDesiatov this is the concurrency-runtime piece of the wasip1-threads track you've been landing: #87283 puts the threads slice into the Swift SDK, and #92238 runs its tests under WasmKit — but the threads stdlib is still built with SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY=TRUE (the shared block in wasmstdlibhelpers.py wins under CMake last-wins), so TaskGroup/async let never fan out on it. This PR makes the define per-product, sets the cooperative executor explicitly on the single-threaded build, relaxes the stdlib guard, and adds a wasi_threads lit feature with a main-thread-isolation test.

It overlaps #92238 textually in wasistdlib.py and test/lit.cfg (not semantically — it doesn't touch what you're changing there). Happy to rebase onto #92238 once it lands, or to fold the flip into your series if you'd rather carry it. Would you be able to review, and trigger CI? The consuming executor is swiftlang/swift-platform-executors#33.

@MaxDesiatov

Copy link
Copy Markdown
Contributor

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.

@swift-ci Please test WebAssembly from me doesn't reach the bot

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.

@AttilaTheFun

Copy link
Copy Markdown
Contributor Author

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.

FranzBusch pushed a commit to swiftlang/swift-platform-executors that referenced this pull request Sep 22, 2026
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.
@AttilaTheFun
AttilaTheFun force-pushed the wasi-threads-single-threaded-flag branch from ab149c4 to b0a24b6 Compare September 27, 2026 19:02
@AttilaTheFun
AttilaTheFun requested a review from a team as a code owner September 27, 2026 19:02
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.
@AttilaTheFun
AttilaTheFun force-pushed the wasi-threads-single-threaded-flag branch from b0a24b6 to 03d70fc Compare September 27, 2026 19:09
@AttilaTheFun AttilaTheFun changed the title [stdlib] Do not compile the wasi-threads Concurrency runtime single-threaded [wasm] Enable multi-threaded wasi-threads Concurrency Sep 27, 2026
@AttilaTheFun

Copy link
Copy Markdown
Contributor Author

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! 🙏

swiftlang/swift-platform-executors@015c70a

@MaxDesiatov MaxDesiatov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MaxDesiatov

Copy link
Copy Markdown
Contributor

@swift-ci test

@MaxDesiatov

Copy link
Copy Markdown
Contributor
  • WasmKit can't run wasi-threads, so the test was run locally under wasmtime.

We do have WasmKit 0.4.1 included in development snapshots off main, which does support wasip1-threads.

@MaxDesiatov
MaxDesiatov enabled auto-merge (squash) October 9, 2026 15:18
@MaxDesiatov
MaxDesiatov merged commit 8ddc55c into swiftlang:main Oct 9, 2026
4 checks passed
AttilaTheFun pushed a commit to AttilaTheFun/isomer that referenced this pull request Oct 27, 2026
…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>
AttilaTheFun added a commit to AttilaTheFun/isomer_hermetic_toolchains that referenced this pull request Nov 26, 2026
…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]>
AttilaTheFun pushed a commit to AttilaTheFun/isomer that referenced this pull request Nov 27, 2026
…, 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>
Hyp3rSon1X pushed a commit to Hyp3rSon1X/swift that referenced this pull request Nov 28, 2026
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.
Hyp3rSon1X added a commit to Hyp3rSon1X/swift-toolchains that referenced this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants