Skip to content

runtime/wasm: add WASI single-worker scheduler (based on #2192, #2208) - #2197

Draft
cpunion wants to merge 19 commits into
xgo-dev:mainfrom
cpunion:codex/wasm-wasi-single-worker
Draft

runtime/wasm: add WASI single-worker scheduler (based on #2192, #2208)#2197
cpunion wants to merge 19 commits into
xgo-dev:mainfrom
cpunion:codex/wasm-wasi-single-worker

Conversation

@cpunion

@cpunion cpunion commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Depends on #2192 and #2208.

Part of #2152.

Problem

GOOS=wasip1 GOARCH=wasm llgo build can produce a module after #2192, but goroutines still select the pthread runtime. The resulting executable imports shared host memory and WASI pthread facilities, so ordinary single-worker Wasmtime execution cannot run Go scheduler behavior.

Implementation

  • Make WASI pthread scheduling an explicit LLGO_WASI_THREADS=1 compatibility mode.
  • Add a raw wasm32 execution-context backend and a one-M/one-P FIFO scheduler for default WASI Preview1 builds.
  • Run package initialization and main.main as the first schedulable task while keeping the host entry on the system stack.
  • Declare Asyncify as a typed target capability; executable linking writes a temporary core module, runs pinned Binaryen instrumentation, and atomically publishes the final module. Archives and shared libraries are unchanged.
  • Compile LLVM 19 SjLj with legacy wasm EH, apply Asyncify, then translate the final module to standardized exnref EH.
  • Keep the existing JS wasm scheduler behavior behind the same internal context boundary.
  • Depend on ssa/wasm: use static defer continuation dispatch #2208 for static wasm defer continuation dispatch; without it, LLVM 19 crashes while selecting runtime.EnsureLocalInitializer after compiler/runtime: add //llgo:tls and //llgo:gls package variables #2079.

The implementation is intentionally limited to the scheduler/context foundation. Channel blocking, timers and host async integration, wasm GC integration, root/safepoint work, and optional WASI Preview2 support remain follow-up work tracked by #2152.

Validation

Bounded locally with GOMAXPROCS=2, -p=1, and a 15 GiB ceiling:

  • macOS: J32, J64, and P1 scheduler fixtures pass, including deadlock behavior and 5,000 serial goroutine lifecycles.
  • macOS: P1 passes -O0, -O3, and ThinLTO builds; the final module validates and has no default pthread/shared-memory imports.
  • macOS: explicit LLGO_WASI_THREADS=1 still builds the historical pthread/shared-memory artifact.
  • macOS: executable and archive post-link guards, the external-module _demo/c/helloc P1 build/execution path, Go 1.26.5 tests, and Go 1.24.2 host tests pass.
  • Ubuntu 24.04 amd64 container: focused build/crosscompile/runtime tests pass; P1 builds, validates, runs under Wasmtime 39.0.1, and reports the expected deadlock.
  • Full LTO still hits the existing LLVM 19 SelectionDAG crash in runtime.printany; the same failure reproduces on the unmodified runtime/wasm: add single-worker Asyncify scheduler #2192 base.
  • The updated compiler/runtime: add //llgo:tls and //llgo:gls package variables #2079/ssa/wasm: use static defer continuation dispatch #2208 integration passes the full GitHub Ubuntu/macOS matrix and Codecov; the reproduced LLVM 19 WASI build, validation, and execution also pass locally.

Independent diff over #2192 and #2208: 26 files, +1,199/-35. This includes the focused post-link lifecycle coverage added after the initial review.

@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.93252% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/build/wasm_postlink.go 95.23% 2 Missing and 1 partial ⚠️
internal/build/build.go 85.71% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@cpunion cpunion changed the title runtime/wasm: add WASI single-worker scheduler (based on #2192) runtime/wasm: add WASI single-worker scheduler (based on #2192, #2208) Jul 29, 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.

1 participant