Skip to content

runtime: stack switching on wasm #81963

Open

Description

@ezrakilty

Go Wasm Stack Switching

As an experiment in the WebAssembly backend, under a GOWASM feature-flag, retarget the compilation of goroutine switching to use WebAssembly's experimental suspend and resume instructions.

Why

The existing method for switching stacks (goroutines) in Go Wasm is to insert code to manage the stack explicitly and step by step. To this end, every function call is checked for a switching boolean return and
stack frames are returned one by one. Every function has a jump table at the top which allows explicit representation of resume-points within the function, but which muddies the debuggability of the generated Wasm code, and has a run-time cost.

The experimental suspend/resume instructions in Wasm are designed precisely to give a direct way to compile things like thread (goroutine) switching. "Suspend" captures a current stack, up to some point, and "resume" installs a given stack that was captured earlier.

Behavior and compatibility

A prototype has been implemented that shows how the suspend/resume instructions can integrate into the Go runtime and compiler. Using suspend/resume should make it possible to remove the checks on every call routine and the top-of-function jump table, making for much more straightforward compilation and code reading.

The change is intended to be invisible to programmers and only to streamline the compiler implementation. However, the WebAssembly feature is not universally deployed, so the new work needs to be kept in an
experiment.

Activity

  1. gabyhelp commented on Oct 2, 2026

    @gabyhelp

    Related Issues

    (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

  2. added this to the Go1.28 milestone on Oct 2, 2026
  3. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Oct 2, 2026
  4. cagedmantis commented on Oct 2, 2026

    @cagedmantis
    Contributor

    @golang/compiler @golang/runtime

  5. changed the title [-]Go Wasm Stack Switching[/-] [+]runtime: stack switching on wasm[/+] on Oct 2, 2026
  6. gopherbot commented on Oct 12, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/845786 mentions this issue: cmd/internal/obj/wasm, cmd/link/internal/wasm: Define opcodes for WasmFX suspend/resume extention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.arch-wasmWebAssembly issuescompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions