From eace0ceb23029592d4ebcde3dd219017f50f8fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Wed, 25 Nov 2026 09:26:38 +0100 Subject: [PATCH] Cranelift: rematerialize colocated `func_addr` in each block where it 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). --- cranelift/codegen/src/opts.rs | 8 +++++ cranelift/codegen/src/opts/remat.isle | 8 +++++ cranelift/codegen/src/prelude_opt.isle | 9 +++++ cranelift/codegen/src/spec/inst_specs.isle | 5 +++ .../filetests/egraph/remat-func-addr.clif | 33 +++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 cranelift/filetests/filetests/egraph/remat-func-addr.clif diff --git a/cranelift/codegen/src/opts.rs b/cranelift/codegen/src/opts.rs index 0fbd1f09d63c..43c3eab13e5f 100644 --- a/cranelift/codegen/src/opts.rs +++ b/cranelift/codegen/src/opts.rs @@ -285,6 +285,14 @@ impl<'a, 'b, 'c> generated_code::Context for IsleContext<'a, 'b, 'c> { block_call.block(&self.ctx.func.dfg.value_lists) } + fn colocated_func_ref(&mut self, func_ref: FuncRef) -> Option { + if self.ctx.func.dfg.ext_funcs[func_ref].colocated { + Some(func_ref) + } else { + None + } + } + fn just_trap_block(&mut self, block: &Block) -> Option { self.ctx .branch_to_trap_analysis diff --git a/cranelift/codegen/src/opts/remat.isle b/cranelift/codegen/src/opts/remat.isle index 00796fc3a253..c7b7a48d506e 100644 --- a/cranelift/codegen/src/opts/remat.isle +++ b/cranelift/codegen/src/opts/remat.isle @@ -29,3 +29,11 @@ (remat x)) (rule (simplify x @ (f64const _ _)) (remat x)) + +;; Rematerialize the addresses of colocated functions in each block where +;; they're used. They are computed relative to the program counter in one or +;; two instructions, and a single address used throughout a large function +;; would otherwise be live across all of it. Other function addresses may need +;; a load (from the GOT, for instance), so they are left alone. +(rule (simplify x @ (func_addr _ (colocated_func_ref _))) + (remat x)) diff --git a/cranelift/codegen/src/prelude_opt.isle b/cranelift/codegen/src/prelude_opt.isle index d8eb08cf4057..72db199c04a3 100644 --- a/cranelift/codegen/src/prelude_opt.isle +++ b/cranelift/codegen/src/prelude_opt.isle @@ -65,6 +65,15 @@ (decl pure block_call_block (BlockCall) Block) (extern constructor block_call_block block_call_block) +;; The verifier does not model what a `FuncRef` contains. +(model FuncRef (type !)) + +;; Matches a reference to a function in the same compilation unit +;; (`colocated`), whose address is computed relative to the program counter. +(spec (colocated_func_ref func_ref) (provide (= result func_ref))) +(decl colocated_func_ref (FuncRef) FuncRef) +(extern extractor colocated_func_ref colocated_func_ref) + ;; If `block` is a "just trap" block (its terminator is an unconditional `trap` ;; and all of its other instructions are pure) return the trap code of that ;; terminating `trap`. diff --git a/cranelift/codegen/src/spec/inst_specs.isle b/cranelift/codegen/src/spec/inst_specs.isle index 654839e38333..055920d29da4 100644 --- a/cranelift/codegen/src/spec/inst_specs.isle +++ b/cranelift/codegen/src/spec/inst_specs.isle @@ -375,6 +375,11 @@ (modifies clif_trap) (provide (= clif_trap (bv_is_zero! val)))) +;; The address of a function: only its width is known. +(spec (func_addr ty func_ref) + (require (= (:lanes ty) 1)) + (provide (= (:bits ty) (widthof result)))) + (spec (iconst ty arg) (require (= (:lanes ty) 1)) (provide (= arg (zero_ext 64 result)) diff --git a/cranelift/filetests/filetests/egraph/remat-func-addr.clif b/cranelift/filetests/filetests/egraph/remat-func-addr.clif new file mode 100644 index 000000000000..9f603e52804c --- /dev/null +++ b/cranelift/filetests/filetests/egraph/remat-func-addr.clif @@ -0,0 +1,33 @@ +test optimize +set opt_level=speed +target x86_64 + +;; The address of the colocated function `%g` is recomputed in `block1`. The +;; address of `%h`, which may need a load, is reused from `block0`. + +function %f(i64, i32) { + fn0 = colocated %g() + fn1 = %h() + +block0(v0: i64, v1: i32): + v2 = func_addr.i64 fn0 + v3 = func_addr.i64 fn1 + store v2, v0 + store v3, v0+8 + brif v1, block1, block2 + +block1: + v4 = func_addr.i64 fn0 + v5 = func_addr.i64 fn1 + store v4, v0+16 + store v5, v0+24 + return + +block2: + return +} + +; check: block1: +; nextln: v6 = func_addr.i64 fn0 +; nextln: store v6, v0+16 +; nextln: store.i64 v3, v0+24