Stream: git-wasmtime

Topic: wasmtime / issue #15264 Cranelift: egraph: performance: e...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 13 2026 at 18:11):

kestred opened issue #15264:

.clif Test Case

A function that adds up the results of N calls, and returns early through a cold block where a call reports an error.
Here N = 3:

function %sum(f64) -> f64 {
    sig0 = (f64) -> f64, i8
    fn0 = %g sig0

block0(v0: f64):
    v1 = f64const 0.0
    jump block1

block1:
    v3, v4 = call fn0(v0)
    v5 = fadd v1, v3
    brif v4, block5, block2

block2:
    v6, v7 = call fn0(v0)
    v8 = fadd v5, v6
    brif v7, block5, block3

block3:
    v9, v10 = call fn0(v0)
    v11 = fadd v8, v9
    brif v10, block5, block4

block4:
    return v11

block5 cold:
    return v1
}

This script writes the same function at any N:

#!/bin/sh
# Writes a function that adds up the results of N calls, returning early where a call reports an error.
n=${1:-4}
printf 'function %%sum(f64) -> f64 {\n    sig0 = (f64) -> f64, i8\n    fn0 = %%g sig0\n\nblock0(v0: f64):\n    v1 = f64const 0.0\n    jump block1\n'
i=1
while [ $i -le $n ]; do
  v=$((3 * i))
  printf '\nblock%d:\n    v%d, v%d = call fn0(v0)\n    v%d = fadd v%d, v%d\n    brif v%d, block%d, block%d\n' \
    $i $v $((v + 1)) $((v + 2)) $([ $i = 1 ] && echo 1 || echo $((v - 1))) $v $((v + 1)) $((n + 2)) $((i + 1))
  i=$((i + 1))
done
printf '\nblock%d:\n    return v%d\n\nblock%d cold:\n    return v1\n}\n' $((n + 1)) $((3 * n + 2)) $((n + 2))

Steps to Reproduce

Expected Results

Compile time grows linearly with N, as it does with opt_level=none. With the patch, at N = 8,192 register
allocation takes 0.051 s and clif-util retires 1.93G instructions in 68 MB:

N Register allocation Instructions retired Peak memory
1,024 0.007 s 0.28G 15 MB
2,048 0.015 s 0.49G 23 MB
4,096 0.029 s 0.98G 39 MB
8,192 0.051 s 1.93G 68 MB

Actual Results

Compile time grows at least quadratically with N: at N = 8,192 register allocation takes 24.5 s and 4.4 GB, and with
the patch 0.051 s and 68 MB. The instructions retired by clif-util grow 4.7, 5.7 and 6.5 times per doubling of N,
and peak memory 3.6 to 3.9 times:

N Register allocation Instructions retired Peak memory
1,024 0.080 s 1.55G 83 MB
2,048 0.419 s 7.29G 298 MB
4,096 3.232 s 41.58G 1,142 MB
8,192 24.520 s 268.71G 4,476 MB

"Register allocation" is the -T line for Cranelift's call into regalloc2, so its time is regalloc2's. The
instructions retired include parsing, verification and every other pass. Each figure is from one run, on a machine
whose load average was about 10 at the time.

Versions and Environment

Cranelift version or commit: cranelift-codegen 0.137.0, with regalloc2 0.15.2, built into clif-util from wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0, --release and default features.

Operating system: macOS 26.6.2 (build 25G83).

Architecture: arm64 (Apple M5 Pro), compiling for --target aarch64.

Extra Info

Elaboration places a pure instruction just before its first use in the skeleton. Each fadd of the sum is used only
by the next fadd, and the last only by the return, so every fadd is placed in the last block. Each call's
result is then live from its call to the end of the function, across every later call and block. With N calls that
is N live ranges, each spanning up to N blocks, and the work of regalloc2 grows with their product.

The patch moves a pure instruction that is the last use of one of its operands to just after its latest operand.
That ends the operand's range and starts its own, so the sum interleaves with the calls. It applies these limits:

Because of the second limit, the early exits must be cold. With cold removed from the test case, the last block no
longer post-dominates the others, and at N = 2,048 the patched and stock clif-util both retired 7.3G instructions.

The patch adds egraph/early.rs, called after Elaborator::elaborate.

<details><summary>Patch: new <code>cranelift/codegen/src/egraph/early.rs</code> and its call after elaboration</summary>

--- a/cranelift/codegen/src/egraph/mod.rs
+++ b/cranelift/codegen/src/egraph/mod.rs
@@ -27,6 +27,7 @@
 use smallvec::SmallVec;

 mod cost;
+mod early;
 mod elaborate;

 /// An iterator that yields blocks in a depth-first pre-order traversal of the
@@ -1256,6 +1257,13 @@
             self.ctrl_plane,
         );
         elaborator.elaborate();
+        early::place_early(
+            self.func,
+            self.cfg,
+            &self.domtree,
+            self.loop_analysis,
+            &mut self.stats,
+        );

         self.check_post_egraph();
     }
@@ -1341,6 +1349,7 @@
     pub(crate) elaborate_memoize_miss: u64,
     pub(crate) elaborate_remat: u64,
     pub(crate) elaborate_licm_hoist: u64,
+    pub(crate) elaborate_place_early: u64,
     pub(crate) elaborate_func: u64,
     pub(crate) elaborate_func_pre_insts: u64,
     pub(crate) elaborate_func_post_insts: u64,
--- /dev/null
+++ b/cranelift/codegen/src/egraph/early.rs
@@ -0,0 +1,312 @@
+//! Placing a pure instruction early where it ends one of its
+//! operands' live ranges.
+//!
+//! Elaboration places a pure instruction just before its first use in
+//! the skeleton, which keeps every operand live until then. A running
+//! sum, `acc2 = fadd acc1, v2`, whose only skeleton use is a return,
+//! would keep each `v` live from its call to the return, and register
+//! allocation would then cost the square of the calls. Moved to just
+//! after its latest operand, such an instruction ends the range of the
+//! operand that it kills and begins its own, so no range grows.
+
+use super::Stats;
+use crate::FxHashMap;
+use crate::dominator_tree::DominatorTree;
+use crate::flowgraph::ControlFlowGraph;
+use crate::inst_predicates::is_pure_for_egraph;
+use crate::ir::{Block, Function, Inst, ProgramPoint, Value, ValueDef};
[message truncated]

view this post on Zulip Wasmtime GitHub notifications bot (Nov 13 2026 at 18:11):

kestred added the bug label to Issue #15264.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 13 2026 at 18:11):

kestred added the cranelift label to Issue #15264.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 13 2026 at 18:18):

alexcrichton added the cranelift:goal:compile-time label to Issue #15264.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 00:34):

cfallin closed issue #15264:

.clif Test Case

A function that adds up the results of N calls, and returns early through a cold block where a call reports an error.
Here N = 3:

function %sum(f64) -> f64 {
    sig0 = (f64) -> f64, i8
    fn0 = %g sig0

block0(v0: f64):
    v1 = f64const 0.0
    jump block1

block1:
    v3, v4 = call fn0(v0)
    v5 = fadd v1, v3
    brif v4, block5, block2

block2:
    v6, v7 = call fn0(v0)
    v8 = fadd v5, v6
    brif v7, block5, block3

block3:
    v9, v10 = call fn0(v0)
    v11 = fadd v8, v9
    brif v10, block5, block4

block4:
    return v11

block5 cold:
    return v1
}

This script writes the same function at any N:

#!/bin/sh
# Writes a function that adds up the results of N calls, returning early where a call reports an error.
n=${1:-4}
printf 'function %%sum(f64) -> f64 {\n    sig0 = (f64) -> f64, i8\n    fn0 = %%g sig0\n\nblock0(v0: f64):\n    v1 = f64const 0.0\n    jump block1\n'
i=1
while [ $i -le $n ]; do
  v=$((3 * i))
  printf '\nblock%d:\n    v%d, v%d = call fn0(v0)\n    v%d = fadd v%d, v%d\n    brif v%d, block%d, block%d\n' \
    $i $v $((v + 1)) $((v + 2)) $([ $i = 1 ] && echo 1 || echo $((v - 1))) $v $((v + 1)) $((n + 2)) $((i + 1))
  i=$((i + 1))
done
printf '\nblock%d:\n    return v%d\n\nblock%d cold:\n    return v1\n}\n' $((n + 1)) $((3 * n + 2)) $((n + 2))

Steps to Reproduce

Expected Results

Compile time grows linearly with N, as it does with opt_level=none. With the patch, at N = 8,192 register
allocation takes 0.051 s and clif-util retires 1.93G instructions in 68 MB:

N Register allocation Instructions retired Peak memory
1,024 0.007 s 0.28G 15 MB
2,048 0.015 s 0.49G 23 MB
4,096 0.029 s 0.98G 39 MB
8,192 0.051 s 1.93G 68 MB

Actual Results

Compile time grows at least quadratically with N: at N = 8,192 register allocation takes 24.5 s and 4.4 GB, and with
the patch 0.051 s and 68 MB. The instructions retired by clif-util grow 4.7, 5.7 and 6.5 times per doubling of N,
and peak memory 3.6 to 3.9 times:

N Register allocation Instructions retired Peak memory
1,024 0.080 s 1.55G 83 MB
2,048 0.419 s 7.29G 298 MB
4,096 3.232 s 41.58G 1,142 MB
8,192 24.520 s 268.71G 4,476 MB

"Register allocation" is the -T line for Cranelift's call into regalloc2, so its time is regalloc2's. The
instructions retired include parsing, verification and every other pass. Each figure is from one run, on a machine
whose load average was about 10 at the time.

Versions and Environment

Cranelift version or commit: cranelift-codegen 0.137.0, with regalloc2 0.15.2, built into clif-util from wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0, --release and default features.

Operating system: macOS 26.6.2 (build 25G83).

Architecture: arm64 (Apple M5 Pro), compiling for --target aarch64.

Extra Info

Elaboration places a pure instruction just before its first use in the skeleton. Each fadd of the sum is used only
by the next fadd, and the last only by the return, so every fadd is placed in the last block. Each call's
result is then live from its call to the end of the function, across every later call and block. With N calls that
is N live ranges, each spanning up to N blocks, and the work of regalloc2 grows with their product.

The patch moves a pure instruction that is the last use of one of its operands to just after its latest operand.
That ends the operand's range and starts its own, so the sum interleaves with the calls. It applies these limits:

Because of the second limit, the early exits must be cold. With cold removed from the test case, the last block no
longer post-dominates the others, and at N = 2,048 the patched and stock clif-util both retired 7.3G instructions.

The patch adds egraph/early.rs, called after Elaborator::elaborate.

<details><summary>Patch: new <code>cranelift/codegen/src/egraph/early.rs</code> and its call after elaboration</summary>

--- a/cranelift/codegen/src/egraph/mod.rs
+++ b/cranelift/codegen/src/egraph/mod.rs
@@ -27,6 +27,7 @@
 use smallvec::SmallVec;

 mod cost;
+mod early;
 mod elaborate;

 /// An iterator that yields blocks in a depth-first pre-order traversal of the
@@ -1256,6 +1257,13 @@
             self.ctrl_plane,
         );
         elaborator.elaborate();
+        early::place_early(
+            self.func,
+            self.cfg,
+            &self.domtree,
+            self.loop_analysis,
+            &mut self.stats,
+        );

         self.check_post_egraph();
     }
@@ -1341,6 +1349,7 @@
     pub(crate) elaborate_memoize_miss: u64,
     pub(crate) elaborate_remat: u64,
     pub(crate) elaborate_licm_hoist: u64,
+    pub(crate) elaborate_place_early: u64,
     pub(crate) elaborate_func: u64,
     pub(crate) elaborate_func_pre_insts: u64,
     pub(crate) elaborate_func_post_insts: u64,
--- /dev/null
+++ b/cranelift/codegen/src/egraph/early.rs
@@ -0,0 +1,312 @@
+//! Placing a pure instruction early where it ends one of its
+//! operands' live ranges.
+//!
+//! Elaboration places a pure instruction just before its first use in
+//! the skeleton, which keeps every operand live until then. A running
+//! sum, `acc2 = fadd acc1, v2`, whose only skeleton use is a return,
+//! would keep each `v` live from its call to the return, and register
+//! allocation would then cost the square of the calls. Moved to just
+//! after its latest operand, such an instruction ends the range of the
+//! operand that it kills and begins its own, so no range grows.
+
+use super::Stats;
+use crate::FxHashMap;
+use crate::dominator_tree::DominatorTree;
+use crate::flowgraph::ControlFlowGraph;
+use crate::inst_predicates::is_pure_for_egraph;
+use crate::ir::{Block, Function, Inst, ProgramPoint, Value, ValueDef};
[message truncated]

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 00:34):

cfallin commented on issue #15264:

Please see the comment here, where @xixi-box submitted your patch as their own work (!!) and I reviewed it before realizing you included the patch here, too. Based on that reasoning, this fix is out-of-scope and the quadratic behavior is expected/acceptable (as an optimizing compiler does not guarantee any particular asymptotic runtime).


Last updated: Nov 19 2026 at 20:05 UTC