runtime/wasm: enable cooperative safepoints and GC by default (based on #2204, #2207) - #2214
Draft
cpunion wants to merge 63 commits into
Draft
runtime/wasm: enable cooperative safepoints and GC by default (based on #2204, #2207)#2214cpunion wants to merge 63 commits into
cpunion wants to merge 63 commits into
Conversation
…orker-asyncify-scheduler # Conflicts: # internal/build/source_patch_test.go
…/wasm-wasi-single-worker
# Conflicts: # .github/workflows/llgo.yml
…orker-asyncify-scheduler # Conflicts: # runtime/internal/runtime/z_default.go
…/wasm-wasi-single-worker
…nts-roots-v2 # Conflicts: # cl/compile.go
# Conflicts: # .github/workflows/llgo.yml
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on #2204 and #2207.
Problem
The root frames from #2207 make running and suspended goroutine pointers visible to the wasm collector, but collection still requires an internal opt-in tag. A CPU-bound goroutine can also keep the single wasm worker indefinitely, preventing host timers, runnable goroutines, and timer-triggered GC from executing.
Implementation
internal/safepointplanCFG analysis. It inserts polls at declared Go function entries and DFS cycle-closing backedges, which intersects reducible and irreducible cycles without polling every loop block.Origin/Parentbefore deciding whether to instrument them. Runtime packages,//go:nosplit, cgo bodies, and package-less synthetic forwarding wrappers are not instrumented; this prevents scheduler helper re-entry while a G is already transitioning state.LLGO_WASI_THREADS=1keeps the historical pthread/nogc path, and explicitly combining it withllgo_wasm_gcremains an error.llgo_wasm_gcas an internal source-selection tag appended by the build; users no longer need to pass it.syscall/js.Valuenested in another struct.posix_memalignfrom the wasm collector allocator because default Emscripten GC selection uses-sMALLOC=none.Runtime flow
Runtime implementation packages remain outside instrumentation. The compiler analysis, root liveness, budget, scheduler slow path, host event queue, and collector remain separate modules.
Validation
All local work used
GOMAXPROCS=2,GOMEMLIMIT=3GiB, and-p=1where applicable.internal/safepointplan,internal/build,cl, andssapass.pollbudget,gcroot, andwasmeventtests pass.-O0and-O3; J64 and P1 ThinLTO also pass.-O3on J32/J64/P1 (12 combinations). Every P1 artifact passeswasm-tools validate --features all.llvm_gc_root_chain, collector, orCooperativeSafepointsymbol.Coverage highlights from local profiles:
internal/safepointplan: 100%runtime/internal/pollbudget: 100%configureWasmGC: 100%Cost
On Apple M4 Max,
BenchmarkBudgetPollis 1.795-1.799 ns/op with 0 B/op and 0 allocs/op.A target-level
-O3microbenchmark compared the same 20-million-iteration arithmetic loop with a normal poll at every backedge against a//go:nosplitno-poll control. Values are medians of five rounds:This is a deliberately minimal worst-case loop, so the relative increase is 41-47%; the absolute backedge cost is about 1.6-1.7 ns and does not allocate. The public fast wrapper is absent from the final wasm symbol strings after optimization; only the noinline slow path remains.
Against the direct #2204 + #2207 integration base, representative native Mach-O output remains 92,816 bytes and every section size is identical; only the linker UUID/hash differs. Local
cortex-m-qemucomparison is currently blocked by the same pre-existing runtime debug/pclntab compile errors on both the base and this branch, so the existing bounded Targets CI remains the embedded artifact gate.Review scope
Excluding the dependency merge, this PR changes 21 files with 775 insertions and 30 deletions. The commits separate nested type-patch correctness, compiler planning/emission, runtime polling, and default build/acceptance behavior.