Repository navigation
Cranelift: rematerialize colocated func_addr in each block where it is used - #15456
Merged
fitzgen merged 1 commit intoNov 25, 2026
Merged
Conversation
… 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
force-pushed
the
cranelift-remat-func-addr
branch
from
November 25, 2026 11:24
91a46aa to
eace0ce
Compare
Contributor
Subscribe to Label Actioncc @avanhatt, @cfallin, @fitzgen, @mmcloughlin DetailsThis issue or pull request has been labeled: "cranelift", "isle"Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
fitzgen
approved these changes
Nov 25, 2026
fitzgen
left a comment
Member
There was a problem hiding this comment.
Seems reasonable to me, thanks!
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.
Cranelift's optimizer already recomputes
iconstin each block where it is used, instead of keeping a single value live across the function. This PR does the same forfunc_addrof 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_addrinstructions, 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.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.