Skip to content

Cranelift: rematerialize colocated func_addr in each block where it is used - #15456

Merged
fitzgen merged 1 commit into
bytecodealliance:mainfrom
vouillon:cranelift-remat-func-addr
Nov 25, 2026
Merged

fitzgen merged 1 commit into
bytecodealliance:mainfrom
vouillon:cranelift-remat-func-addr

Conversation

@vouillon

@vouillon vouillon commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Cranelift's optimizer already recomputes iconst in each block where it is used, instead of keeping a single value live across the function. This PR does the same for func_addr of colocated functions. A colocated function is one that is known to stay close to the code referencing it after linking, such as another function of the same Wasm module; its address is computed relative to the program counter in one or two instructions. Other function addresses may need a load (from the GOT, for instance), so they are left alone.

The optimizer merges identical func_addr instructions, so a function address used throughout a large function becomes a single value that is live across all of it. This happens in Wasmtime's module startup function for modules produced by wasm_of_ocaml, which reference the same functions from many global initializers (#15382). In the startup function of a wasm_of_ocaml build of herd (9.4 MB of Wasm, 24,560 globals, about 125,000 blocks), 287 function addresses were live across most of the function, and register allocation split 572 values at boundaries between hot and cold blocks.

Measurements, with bytecodealliance/regalloc2#285 (without it, compiling herd fails with an 11 GiB memory limit). Memory is the median of three runs. The "startup function" rows are measured on a copy of herd whose function bodies are replaced by unreachable.

before after
herd startup function: values split at hot/cold boundaries 572 15
herd startup function: live ranges 17.1 M 3.0 M
herd startup function: compile memory 4.9 GiB 2.5 GiB
herd, whole module: compile memory 5.9 GiB 3.5 GiB
js_of_ocaml, whole module: compile memory 1.24 GiB 1.13 GiB
ocamlformat, whole module: compile memory 1.33 GiB 1.09 GiB

Code size changes by less than 0.5% on these modules. The generated code is identical for SpiderMonkey, a .NET runtime and the ONNX runtime.

@vouillon
vouillon requested a review from a team as a code owner November 25, 2026 08:44
@vouillon
vouillon requested review from cfallin and removed request for a team November 25, 2026 08:44
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels Nov 25, 2026
… is used

The address of a colocated function (one that is known to stay close to
the code referencing it after linking, such as another function of the
same Wasm module) is cheap to compute: one or two instructions, relative
to the program counter. But the optimizer merges identical `func_addr`
instructions, so an address used in many places of a large function
becomes a single value that is live across all of them.

This shows up in Wasmtime's module startup function for modules that
reference the same functions from many global initializers, such as the
ones produced by wasm_of_ocaml: each `ref.func` stores the address of
the function. In the startup function of one such module (a
wasm_of_ocaml build of herd, with 24,560 globals), 287 function
addresses were live across most of the function's 125,000 blocks.
Register allocation then split 572 values at boundaries between hot and
cold blocks.

Treat the `func_addr` of a colocated function like `iconst`, and
recompute it in each block where it is used. Other function addresses
may need a load (from the GOT, for instance), so they are left alone.
This also adds a spec for `func_addr` and a model for `FuncRef`, so that
the ISLE verifier can check the new rule.

For herd, compiling the startup function now uses 2.5 GiB instead of
4.9 GiB, and compiling the whole module 3.5 GiB instead of 5.9 GiB.
These numbers are with the regalloc2 fix from
bytecodealliance/regalloc2#285; without it, the module does not compile
within 12 GB. The generated code is unchanged for the other modules I
tried (SpiderMonkey, .NET and the ONNX runtime).
@vouillon
vouillon force-pushed the cranelift-remat-func-addr branch from 91a46aa to eace0ce Compare November 25, 2026 11:24
@vouillon vouillon changed the title Cranelift: rematerialize func_addr in each block where it is used Cranelift: rematerialize colocated func_addr in each block where it is used Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Subscribe to Label Action

cc @avanhatt, @cfallin, @fitzgen, @mmcloughlin

Details This issue or pull request has been labeled: "cranelift", "isle"

Thus the following users have been cc'd because of the following labels:

  • avanhatt: isle
  • cfallin: isle
  • fitzgen: isle
  • mmcloughlin: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@fitzgen fitzgen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems reasonable to me, thanks!

@fitzgen
fitzgen added this pull request to the merge queue Nov 25, 2026
Merged via the queue into bytecodealliance:main with commit 49fa381 Nov 25, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants