kestred opened issue #15264:
.clifTest CaseA 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
First, build
clif-utilfrom wasmtime v50.0.0, the release that ships cranelift-codegen 0.137.0:
curl -L https://github.com/bytecodealliance/wasmtime/archive/refs/tags/v50.0.0.tar.gz | tar xz cd wasmtime-50.0.0 cargo build --release -p cranelift-tools --bin clif-utilThen, print the optimized IR of the case above:
target/release/clif-util compile --target aarch64 --set opt_level=speed -p 5-test.clif
Stock 0.137.0 places everyfaddin the last block:
```
block0(v0: f64):
jump block1block1:
v3, v4 = call fn0(v0)
brif v4, block5, block2block2:
v6, v7 = call fn0(v0)
brif v7, block5, block3block3:
v9, v10 = call fn0(v0)
brif v10, block5, block4block4:
v1 = f64const 0.0
v5 = fadd v1, v3 ; v1 = 0.0
v8 = fadd v5, v6
v11 = fadd v8, v9
return v11block5 cold:
v12 = f64const 0.0
return v12 ; v12 = 0.0
}
```Then, compile the function at four sizes, printing the pass timings with
-T, and the instructions retired and
peak memory of the wholeclif-utilprocess with macOS's/usr/bin/time -l:
for n in 1024 2048 4096 8192; do sh 5-gen.sh $n > sum.clif echo "N=$n" /usr/bin/time -l target/release/clif-util compile --target aarch64 --set opt_level=speed -T sum.clif 2>&1 | grep -E 'Register allocation|instructions retired|maximum resident' done
Stock 0.137.0 printed:
N=1024 0.080 0.080 Register allocation 87490560 maximum resident set size 1550473981 instructions retired N=2048 0.419 0.419 Register allocation 312688640 maximum resident set size 7286855404 instructions retired N=4096 3.232 3.232 Register allocation 1197965312 maximum resident set size 41582387980 instructions retired N=8192 24.520 24.520 Register allocation 4693360640 maximum resident set size 268712923627 instructions retiredThen, run the same loop with
--set opt_level=none, which skips the egraph pass. At N = 2,048, 4,096 and 8,192 it
retired 0.46G, 0.86G and 1.74G instructions, with register allocation taking 0.012 s, 0.023 s and 0.067 s.Then, apply the patch under Extra Info, build again, and repeat the steps with
opt_level=speed. The IR now
interleaves the sum with the calls:
```
block0(v0: f64):
jump block1block1:
v3, v4 = call fn0(v0)
v1 = f64const 0.0
v5 = fadd v1, v3 ; v1 = 0.0
brif v4, block5, block2block2:
v6, v7 = call fn0(v0)
v8 = fadd.f64 v5, v6
brif v7, block5, block3block3:
v9, v10 = call fn0(v0)
brif v10, block5, block4block4:
v11 = fadd.f64 v8, v9
return v11block5 cold:
v12 = f64const 0.0
return v12 ; v12 = 0.0
}
and the loop printed:
N=1024
0.007 0.007 Register allocation
15466496 maximum resident set size
276224699 instructions retired
N=2048
0.015 0.015 Register allocation
24363008 maximum resident set size
485154515 instructions retired
N=4096
0.029 0.029 Register allocation
40402944 maximum resident set size
980167829 instructions retired
N=8192
0.051 0.051 Register allocation
71319552 maximum resident set size
1928848457 instructions retired
```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 andclif-utilretires 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 byclif-utilgrow 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
-Tline 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-utilfrom wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0,--releaseand 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
faddof the sum is used only
by the nextfadd, and the last only by thereturn, so everyfaddis 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:
- it moves an instruction only past a skeleton instruction;
it never makes an instruction run anywhere new, so a move into another block needs the instruction's block to
post-dominate that block among warm blocks, and to be in every loop that contains it;it leaves in place an instruction whose result a skeleton instruction uses, so that lowering can still merge the
two, as anicmpinto abrif;it copies a constant once per block where the constant no longer dominates its user.
Because of the second limit, the early exits must be cold. With
coldremoved from the test case, the last block no
longer post-dominates the others, and at N = 2,048 the patched and stockclif-utilboth retired 7.3G instructions.The patch adds
egraph/early.rs, called afterElaborator::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]
kestred added the bug label to Issue #15264.
kestred added the cranelift label to Issue #15264.
alexcrichton added the cranelift:goal:compile-time label to Issue #15264.
cfallin closed issue #15264:
.clifTest CaseA 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
First, build
clif-utilfrom wasmtime v50.0.0, the release that ships cranelift-codegen 0.137.0:
curl -L https://github.com/bytecodealliance/wasmtime/archive/refs/tags/v50.0.0.tar.gz | tar xz cd wasmtime-50.0.0 cargo build --release -p cranelift-tools --bin clif-utilThen, print the optimized IR of the case above:
target/release/clif-util compile --target aarch64 --set opt_level=speed -p 5-test.clif
Stock 0.137.0 places everyfaddin the last block:
```
block0(v0: f64):
jump block1block1:
v3, v4 = call fn0(v0)
brif v4, block5, block2block2:
v6, v7 = call fn0(v0)
brif v7, block5, block3block3:
v9, v10 = call fn0(v0)
brif v10, block5, block4block4:
v1 = f64const 0.0
v5 = fadd v1, v3 ; v1 = 0.0
v8 = fadd v5, v6
v11 = fadd v8, v9
return v11block5 cold:
v12 = f64const 0.0
return v12 ; v12 = 0.0
}
```Then, compile the function at four sizes, printing the pass timings with
-T, and the instructions retired and
peak memory of the wholeclif-utilprocess with macOS's/usr/bin/time -l:
for n in 1024 2048 4096 8192; do sh 5-gen.sh $n > sum.clif echo "N=$n" /usr/bin/time -l target/release/clif-util compile --target aarch64 --set opt_level=speed -T sum.clif 2>&1 | grep -E 'Register allocation|instructions retired|maximum resident' done
Stock 0.137.0 printed:
N=1024 0.080 0.080 Register allocation 87490560 maximum resident set size 1550473981 instructions retired N=2048 0.419 0.419 Register allocation 312688640 maximum resident set size 7286855404 instructions retired N=4096 3.232 3.232 Register allocation 1197965312 maximum resident set size 41582387980 instructions retired N=8192 24.520 24.520 Register allocation 4693360640 maximum resident set size 268712923627 instructions retiredThen, run the same loop with
--set opt_level=none, which skips the egraph pass. At N = 2,048, 4,096 and 8,192 it
retired 0.46G, 0.86G and 1.74G instructions, with register allocation taking 0.012 s, 0.023 s and 0.067 s.Then, apply the patch under Extra Info, build again, and repeat the steps with
opt_level=speed. The IR now
interleaves the sum with the calls:
```
block0(v0: f64):
jump block1block1:
v3, v4 = call fn0(v0)
v1 = f64const 0.0
v5 = fadd v1, v3 ; v1 = 0.0
brif v4, block5, block2block2:
v6, v7 = call fn0(v0)
v8 = fadd.f64 v5, v6
brif v7, block5, block3block3:
v9, v10 = call fn0(v0)
brif v10, block5, block4block4:
v11 = fadd.f64 v8, v9
return v11block5 cold:
v12 = f64const 0.0
return v12 ; v12 = 0.0
}
and the loop printed:
N=1024
0.007 0.007 Register allocation
15466496 maximum resident set size
276224699 instructions retired
N=2048
0.015 0.015 Register allocation
24363008 maximum resident set size
485154515 instructions retired
N=4096
0.029 0.029 Register allocation
40402944 maximum resident set size
980167829 instructions retired
N=8192
0.051 0.051 Register allocation
71319552 maximum resident set size
1928848457 instructions retired
```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 andclif-utilretires 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 byclif-utilgrow 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
-Tline 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-utilfrom wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0,--releaseand 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
faddof the sum is used only
by the nextfadd, and the last only by thereturn, so everyfaddis 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:
- it moves an instruction only past a skeleton instruction;
it never makes an instruction run anywhere new, so a move into another block needs the instruction's block to
post-dominate that block among warm blocks, and to be in every loop that contains it;it leaves in place an instruction whose result a skeleton instruction uses, so that lowering can still merge the
two, as anicmpinto abrif;it copies a constant once per block where the constant no longer dominates its user.
Because of the second limit, the early exits must be cold. With
coldremoved from the test case, the last block no
longer post-dominates the others, and at N = 2,048 the patched and stockclif-utilboth retired 7.3G instructions.The patch adds
egraph/early.rs, called afterElaborator::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]
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