Skip to content

wasm32 target: With runtime over WASI preview1 plus an emitted JS host - #1210

Merged
ehartford merged 6 commits into
mainfrom
wasm-target
Sep 21, 2026
Merged

ehartford merged 6 commits into
mainfrom
wasm-target

Conversation

@ropoctl

@ropoctl ropoctl commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

What

--target=wasm32 compiles a pure-With program to a WebAssembly module whose only imports are WASI preview1 system calls, and emits <prog>.js, an emscripten-style host that runs it under node (real filesystem, argv, env, stdio) or in a browser (in-memory filesystem, console). Since the With runtime never uses libc there is no wasi-libc or emscripten musl underneath: rt/wasm.w implements the same rt_* platform contract the Linux/Darwin/Windows backends implement over libc, directly on WASI, and owns what a libc would (startup/exit, a page allocator behind rt_mmap on memory.grow, malloc/free/memcmp, compiler-rt's __multi3). Design note: docs/wasm-target.md; decision D45.

with build --target=wasm32 prog.w -o prog.wasm    # prog.wasm + prog.js
node prog.js a b c
with run  --target=wasm32 prog.w
with test --target=wasm32 test/wasm/*.w
with build :cross-rt-wasm && with build :wasm-tests

The module also runs directly under any WASI host (wasmtime, node's wasi module) because the imports are the standard ones.

Pieces

  • Target kind 7 wasm32 (TargetSpec, DriverOptions, BuildGraphKinds, std.build); wasm64 reserved as kind 8 and refused until its runtime exists.
  • Pointer width is a target property (target_spec_ptr_bytes) read by TypeLayout.
  • @[import_module("ns")] on an extern fn (clang's import_module): codegen attaches wasm-import-module / wasm-import-name.
  • _start entry wrapper on wasm (with_wasm_startup fills argc/argv, with_wasm_exit leaves via proc_exit).
  • Link stage: wasm-ld beside the SDK's lld, --stack-first -z stack-size=8MiB, --fatal-warnings, .wasm output, JS host emission, fiber_stubs.o instead of the fiber core.
  • with run / with test honour --target (they ignored it before) and execute wasm programs through node <prog>.js (WITH_NODE overrides).
  • build.w: :cross-rt-wasm builds out/lib/cross/wasm32/; :wasm-tests runs test/wasm/*.w (needs node; not in the standing battery).

Toolchain

The static LLVM must include the WebAssembly backend: LLVM_TARGETS_TO_BUILD="AArch64;X86;WebAssembly" tools/build-static-llvm.sh. The SDKs published today are AArch64+X86 only.

Two native ABI bugs wasm exposed, fixed for every target (ABI v5)

  • An explicit -> Unit lowered to an i32 result while an absent return type lowered to void, so a definition and a declaration of the same runtime symbol disagreed. Native ABIs ignore an unused return register; wasm traps on the signature mismatch. Return positions now lower Unit to void (resolve_return_type).
  • The HashMap/SlotMap runtime helpers were declared with placeholder prototypes (one pointer, or all-i64) and called with the real arguments; some sites also read an i32 result as i64. They now carry the runtime's real prototypes and an invariant check aborts on disagreement. docs/with-abi.md v5 entry, hashes re-recorded.

Verification (final build, pinned seed v0.15.2.0)

  • with build, with build :fixpoint: green.
  • :cross-rt-wasm, :wasm-tests: 6/6 fixtures pass under node (stdout/stderr/exit code, argv/env/sysinfo, Vec/str/HashMap/f64, checked and i128 multiply via __multi3, file I/O through the node host). The in-memory browser backend was exercised under node with memfs: true.
  • 120 test/behavior programs built for wasm32 and run under node: every failure is a designed refusal (async, c_import, host-built pcre2 bundle, mainless fixtures); no signature-mismatch thunks remain.
  • with build :test: 1060/1062 behavior, 770/771 compile-error, 209/210 spec. The reds are behav_error_handling, behav_tls_module_compiles, err_thread_spawn_closure_captures_ref, spec_ss13_6_comprehensions and build-helper-programs, all of which fail identically on the untouched base commit 43db527 with the same seed, plus spec-inventory-check (see below).

Needs a ruling (spec leads)

spec-inventory-check is red: attributes: implementation has unspec'd import_module. Not weakened. The surface additions that need wording are @[target("wasm32")] as a §16.13 guard, @[import_module("ns")] on extern fns, and --target=wasm32 in §18.5.

Not in scope

  • Async/fibers: no stack switching in WebAssembly; a program that spawns fails at wasm-ld with the fiber-core symbols undefined.
  • c_import, threads, processes, sockets: refused or report failure.
  • Host-built corpus bundles (std.regex, std.zlib) are not built for wasm32 yet; the link refuses with the existing target-mismatch message.
  • Found on the way: formatting an Option[&T] in an f-string segfaulted the compiler on every target. Fixed separately in codegen: formatting an Option[&T] no longer crashes the compiler #1216.

`--target=wasm32` compiles a pure-With program to a WebAssembly module
whose only imports are WASI preview1 system calls, and emits `<prog>.js`,
an emscripten-style host that runs it under node (real filesystem, argv,
env, stdio) or in a browser (in-memory filesystem, console). No wasi-libc
and no emscripten musl: rt/wasm.w implements the rt_* platform contract
the Linux/Darwin/Windows backends implement over libc, directly on WASI,
and owns what a libc would (startup/exit, the page allocator behind
rt_mmap on memory.grow, malloc/free/memcmp, compiler-rt's __multi3).

Compiler: target kind 7 (wasm32; wasm64 reserved and refused), pointer
width as a target property read by TypeLayout, `@[import_module]` for the
WASI namespace, a `_start` entry wrapper, wasm-ld in the link stage with
--fatal-warnings, `with run`/`with test --target=wasm32` through node,
`:cross-rt-wasm` and `:wasm-tests` build targets, test/wasm fixtures,
docs/wasm-target.md and decision D45.

Two native "works by luck" ABI inconsistencies became hard failures on
wasm and are fixed for every target (ABI v5): an explicit `-> Unit`
lowered to an i32 result while an absent return type lowered to void, and
the HashMap/SlotMap runtime helpers were declared with placeholder
prototypes and called with the real arguments.

Not covered: async/fibers (no stack switching; refused at link), c_import
(refused), threads/processes/sockets (report failure), host-built corpus
bundles such as std.regex (refused at link). spec-inventory-check stays
red until `@[import_module]` and the wasm32 guard get spec wording.
…target set

.github/workflows/wasm32.yml runs on ubuntu-latest: it builds the static
LLVM SDK with tools/build-static-llvm.sh (cached on the script's hash, since
no published SDK carries the WebAssembly backend yet), bootstraps the pinned
linux seed, builds the compiler and the wasm32 runtime, compiles
test/wasm/wasm_hello.w for wasm32, runs it under node and asserts its
output, then runs the wasm32 fixtures (:wasm-tests).

WebAssembly joins AArch64 and X86 in the default LLVM_TARGETS_TO_BUILD of
tools/build-static-llvm.sh, tools/build-static-llvm.ps1 and build/sdk.w,
so the next published SDKs can drive this lane without building LLVM.
@ehartford

Copy link
Copy Markdown
Contributor

Amazing!
the CI errors are my fault, i will fix it

The compiler references LLVM's five LLVMInitializeWebAssembly* entry points,
so every published SDK (AArch64+X86 only) failed the compiler link with
undefined symbols. When the SDK lib dir has no libLLVMWebAssemblyCodeGen, the
link-metadata actions (native and the linux/windows cross variants) now
alias those entry points to rt/cimport_stubs.w's no-op
with_llvm_wasm_backend_missing, the same way with_clang_main is aliased on
SDKs that predate `with cc`. The generated EmbeddedClangResourceData bakes
embedded_llvm_wasm_backend_linked() from the same SDK fact, and every
command's --target parse refuses wasm32 on such a compiler with a message
naming the fix, before any codegen.

Verified locally both ways: a :dev build against the old SDK links and
refuses --target=wasm32; against the WebAssembly-enabled SDK the response
file carries no alias and the wasm hello world runs under node.
ropoctl and others added 3 commits September 19, 2026 15:51
Require WebAssembly target archives and wasm-ld at SDK install/package boundaries, include CMake runtime modules, and support Windows ARM64 packaging and wasm-ld.exe lookup.

Build and cache SDK artifacts in each native CI lane, unpack them for compiler verification, and run wasm fixtures. Use bounded-memory With archive I/O, USTAR prefixes for long module paths, and portable stdio without mixing UCRT descriptors with runtime handles.

Root-cause evidence: lldb stopped in sdk_str_compare at the u8 subtraction underflow and in tool_tar_build_header with an empty filename from the old 100-byte path rejection. Real source extraction exposed the whole-archive read limit; SDK packaging exceeded the memory gate before streaming. A cold HTTPS source fetch exposed the missing explicit bigint helper return type.

Validation: source-built LLVM 22.1.6 SDK; graph-owned Ninja/CMake source builds and LLVM source download/extraction; deterministic archive contract with binary data, empty file, permissions, symlink and long paths; native tar round trip and SHA256; packaged SDK LLVM configure; final compiler build and all six wasm fixtures; actionlint and diff checks. Other native hosts require CI.
Preserve main decisions D45-D52 and retain the provisional wasm decision as D53. Recompute both ABI source hashes from the merged tree, including main file-prefix mapping and wasm target layout changes. Verify hashes with shasum, check the merged workflows with actionlint, and check the merge diff for whitespace errors.
@ehartford
ehartford merged commit 2435c7b into main Sep 21, 2026
0 of 5 checks passed
@ehartford

Copy link
Copy Markdown
Contributor

thank you!

ehartford pushed a commit that referenced this pull request Sep 21, 2026
… zero from the host

Every published SDK asset fails `sdk-ninja`'s bootstrap check (CI red on
all four non-mac lanes since #1210 added .github/actions/source-sdk):
linux x86_64 v0.15.1 keeps `bin/clang -> clang-22` and `bin/clang++ ->
clang` as links with no clang-22 in the archive and no share/ (cmake without
its modules); windows x86_64 ships cmake.exe without share/cmake-*
(CMAKE_ROOT); the linux/windows aarch64 assets are older builds with no
cmake or ninja. None was produced by build/sdk.w's packager (no With code or
workflow writes .tar.zst); they predate it.

build/sdk.w: the unix tool selection includes the driver the links resolve
to (`bin/clang-<major>`, from COMPILER_LLVM_VERSION), and
run_package_llvm_sdk_action refuses an archive whose manifest lacks any of
the bootstrap set (driver, links, cmake + share/cmake-X.Y/Modules, ninja)
— "SDK archive cannot bootstrap the next SDK build: missing …". The
contract test pins the selection on every platform.

.github/actions/source-sdk: before building, each bootstrap tool that is
missing or dangling in the fetched asset (cmake also needs its module tree)
is copied from the runner's toolchain with a loud line naming the source;
the package that run produces then bootstraps itself. Once such a package
is published on every platform the seeding never triggers.
ehartford added a commit that referenced this pull request Sep 21, 2026
…n fn's WebAssembly import namespace (#1263)

#1210 shipped the attribute with no spec text; spec-inventory-check has
been red on main since. Wording as proposed in the brief and landed at
Eric's "can you fix it"; §29.14's attribute index row is what the
inventory reads.

Co-authored-by: Eric Hartford <[email protected]>
ehartford added a commit that referenced this pull request Sep 21, 2026
… zero from the host (#1262)

Every published SDK asset fails `sdk-ninja`'s bootstrap check (CI red on
all four non-mac lanes since #1210 added .github/actions/source-sdk):
linux x86_64 v0.15.1 keeps `bin/clang -> clang-22` and `bin/clang++ ->
clang` as links with no clang-22 in the archive and no share/ (cmake without
its modules); windows x86_64 ships cmake.exe without share/cmake-*
(CMAKE_ROOT); the linux/windows aarch64 assets are older builds with no
cmake or ninja. None was produced by build/sdk.w's packager (no With code or
workflow writes .tar.zst); they predate it.

build/sdk.w: the unix tool selection includes the driver the links resolve
to (`bin/clang-<major>`, from COMPILER_LLVM_VERSION), and
run_package_llvm_sdk_action refuses an archive whose manifest lacks any of
the bootstrap set (driver, links, cmake + share/cmake-X.Y/Modules, ninja)
— "SDK archive cannot bootstrap the next SDK build: missing …". The
contract test pins the selection on every platform.

.github/actions/source-sdk: before building, each bootstrap tool that is
missing or dangling in the fetched asset (cmake also needs its module tree)
is copied from the runner's toolchain with a loud line naming the source;
the package that run produces then bootstraps itself. Once such a package
is published on every platform the seeding never triggers.

Co-authored-by: Eric Hartford <[email protected]>
ehartford pushed a commit that referenced this pull request Sep 22, 2026
… built only on release (Eric, 2026-09-22)

Remove the source-sdk step from the five lanes: each Build/Fixpoint/battery
now runs on the SDK its Fetch/Provision step already put at LLVM_PREFIX,
the env chain every lane had before #1210 added the action. The
WebAssembly fixtures step stays, unconditional, renamed; it is red until
the pinned SDK assets are republished from sdk-release.yml (lanes are not
a signal, D56).

The push trigger goes too (Eric: CI was overzealous in what it did on
every push): each lane runs on a nightly cron, staggered five minutes per
platform from 09:00 UTC, and on workflow_dispatch. paths-ignore has no
push to filter and is gone; cancel-in-progress stays false.
ehartford added a commit that referenced this pull request Sep 22, 2026
…ever on pull requests (#1304)

* CI is not a merge signal (D56): lanes run on push to main and on dispatch, never on pull requests

Eric, 2026-09-22: "We either reduce it to 20 minutes or we ignore it as any
kind of signal." Measured on the last green macOS lane: source-SDK 105 min,
compiler build 29 min, fixpoint 34 min, battery 71 min; even without the
SDK build a lane is ~2 h on a 4-core runner, and a lane that skips the
battery is not a signal. So: the seed-driven battery on the maintainer
machine gates a PR (draft until green, ready when posted); the five lane
workflows drop their pull_request trigger, keep push-to-main (queued, never
cancelled — they build and cache the SDK packages) and gain
workflow_dispatch. Branch protection already requires no status check.
decisions.md D56 records the measurements and the cost accepted; the agent
rules carry the sentence.

* Lanes run nightly and on dispatch against the fetched SDK; the SDK is built only on release (Eric, 2026-09-22)

Remove the source-sdk step from the five lanes: each Build/Fixpoint/battery
now runs on the SDK its Fetch/Provision step already put at LLVM_PREFIX,
the env chain every lane had before #1210 added the action. The
WebAssembly fixtures step stays, unconditional, renamed; it is red until
the pinned SDK assets are republished from sdk-release.yml (lanes are not
a signal, D56).

The push trigger goes too (Eric: CI was overzealous in what it did on
every push): each lane runs on a nightly cron, staggered five minutes per
platform from 09:00 UTC, and on workflow_dispatch. paths-ignore has no
push to filter and is gone; cancel-in-progress stays false.

* sdk-release.yml: build and publish the LLVM SDK packages on dispatch against a release tag

One job per platform, each the lane's preamble verbatim (checkout, seed,
pinned SDK asset, native toolchain env), then the source-sdk action, then
gh release upload --clobber of with-llvm-sdk-*.tar.gz, .sha256 and
.manifest to the release named by the tag input (GITHUB_TOKEN, contents:
write). The action already uploads the same files as the source-sdk-<host>
workflow artifact. Asset names are build/sdk.w's sdk_asset_for_platform,
the ones nightly-release.yml pins; that workflow does not call this one
because it republishes the digest-verified SDK the compiler was built
against, which a package built in the same run could not be.

* D56: lanes run nightly and on dispatch; the SDK is built only on release (Eric, 2026-09-22)

decisions.md D56, CLAUDE.md/AGENTS.md and the source-sdk action header say
so; the action runs only from sdk-release.yml.

---------

Co-authored-by: Eric Hartford <[email protected]>
@ehartford
ehartford deleted the wasm-target branch September 24, 2026 16:57
ehartford added a commit that referenced this pull request Sep 27, 2026
…e8defa (#1805)

* SDK: ship llvm-dwarfdump in the Unix package

stage2-debug-lines, a :fixpoint dependency, reads ELF and Mach-O line tables
with $LLVM_PREFIX/bin/llvm-dwarfdump (build/compiler.w
comp_llvm_dwarfdump_tool). The LLVM build installs it, but the package's bin/
selection did not keep it, so an SDK built from this recipe fails :fixpoint:

    error: stage2-debug-lines: missing llvm-dwarfdump: .deps/llvm-22.1.6-linux-x86_64/bin/llvm-dwarfdump

The published v0.15.1 linux SDK, which CI extracts, lacks it for the same
reason. Keep it in the package and require it when validating an SDK on
Linux and macOS. On PE the same target uses llvm-readobj, so the Windows
list is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* SDK: pin CMAKE_RC_COMPILER_INIT to the Kit's rc.exe on Windows

sdk.w names the Windows Kit's rc.exe as CMAKE_RC_COMPILER for the cmake and
LLVM builds, which compile with clang-cl. CMake's Platform/Windows-Clang
module picks its resource rule from CMAKE_RC_COMPILER_INIT as well: when
rc.exe is not on PATH it defaults that to llvm-rc and takes the llvm-rc rule
(clang preprocess, then `-clang:-MD -clang:-MF` depfile flags passed to the
resource compiler). rc.exe reads `-clang:-MD` as a code page:

    fatal error RC1205: invalid code page     (CMakeVersion.rc)

The build then depended on the host's PATH. Set CMAKE_RC_COMPILER_INIT to
the same rc.exe so the plain rc rule, with cmcldeps as its dependency
scanner, is chosen on every host.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* seed.lock: linux-x86_64 and windows-x86_64 seeds and SDKs from main 2e8defa

No published compiler builds main on linux-x86_64 or windows-x86_64: the
pinned v0.15.2.0 / v0.15.2.1 seeds fail on it, and the Linux lane has been
red at its build step since 09-22. Both seeds are main 2e8defa,
bootstrapped locally through the same ladder from v0.15.2.1 (8c859a7,
37f3a99, 8e55d67, fb6753b + the 4f00495 field-move fix, 8db10d1, and
the 607a52f hop), published as nightly-20260926-local-1-2e8defaa18ef. Each
binary builds main from a fresh checkout to a stage2 == stage3 fixpoint.

The same release carries LLVM 22.1.6 SDKs built from source with
:sdk-package (this branch's sdk.w). They include the WebAssembly backend
main has required since #1210 and, on Linux, llvm-dwarfdump for
stage2-debug-lines; the v0.15.1 SDKs the self-host lanes pinned have
neither. The lanes now fetch the .tar.gz assets, check them against their
.sha256 sidecars, and extract with tar -xzf. The seed pins in the
workflows were rewritten by tools/bump_seed_pins.w.

linux-aarch64, windows-aarch64 and darwin-aarch64 are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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