kestred opened issue #15266:
.clifTest CaseA function with N loops in a row. Each loop adds its counter to a sum until the counter reaches the second argument,
then stores the sum and starts the next loop. It is written as anoptimizefiletest, so thatclif-util testruns
the egraph pass alone. Here N = 2:test optimize set opt_level=speed target aarch64 function %loops(i64, i64) { block0(v0: i64, v1: i64): v2 = iconst.i64 0 v3 = iconst.i64 1 jump block1(v2, v2) block1(v4: i64, v5: i64): v6 = iadd v5, v4 v7 = iadd v4, v3 v8 = icmp ult v7, v1 brif v8, block1(v7, v6), block2 block2: store v6, v0+8 jump block3(v2, v2) block3(v9: i64, v10: i64): v11 = iadd v10, v9 v12 = iadd v9, v3 v13 = icmp ult v12, v1 brif v13, block3(v12, v11), block4 block4: store v11, v0+16 jump block5(v2, v2) block5(v14: i64, v15: i64): return }
gen.shwrites the same test at any N:#!/bin/sh # Writes a test of the egraph pass on N loops in a row, each summing its counter until the counter reaches n and # storing the sum at p + 8 * i. n=${1:-4} printf 'test optimize\nset opt_level=speed\ntarget aarch64\n\nfunction %%loops(i64, i64) {\nblock0(v0: i64, v1: i64):\n v2 = iconst.i64 0\n v3 = iconst.i64 1\n jump block1(v2, v2)\n' i=1 while [ $i -le $n ]; do v=$((5 * i - 1)) printf '\nblock%d(v%d: i64, v%d: i64):\n v%d = iadd v%d, v%d\n v%d = iadd v%d, v3\n v%d = icmp ult v%d, v1\n brif v%d, block%d(v%d, v%d), block%d\n\nblock%d:\n store v%d, v0+%d\n jump block%d(v2, v2)\n' \ $((2 * i - 1)) $v $((v + 1)) $((v + 2)) $((v + 1)) $v $((v + 3)) $v $((v + 4)) $((v + 3)) $((v + 4)) $((2 * i - 1)) $((v + 3)) $((v + 2)) $((2 * i)) $((2 * i)) $((v + 2)) $((8 * i)) $((2 * i + 1)) i=$((i + 1)) done printf '\nblock%d(v%d: i64, v%d: i64):\n return\n}\n' $((2 * n + 1)) $((5 * n + 4)) $((5 * n + 5))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, run the test 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 2048 4096 8192 16384; do sh gen.sh $n > loops.clif echo "N=$n" /usr/bin/time -l target/release/clif-util test -T loops.clif 2>&1 | grep -E 'Egraph|instructions retired|maximum resident' doneThen, apply the patch under Extra Info, build again, and run the same loop.
Stock 0.137.0 printed:
N=2048 0.013 0.010 Egraph based optimizations 16334848 maximum resident set size 560108476 instructions retired N=4096 0.025 0.021 Egraph based optimizations 25624576 maximum resident set size 1213169521 instructions retired N=8192 0.086 0.078 Egraph based optimizations 42975232 maximum resident set size 3108413903 instructions retired N=16384 0.302 0.287 Egraph based optimizations 72073216 maximum resident set size 9081268638 instructions retiredWith the patch:
N=2048 0.008 0.006 Egraph based optimizations 17416192 maximum resident set size 471986712 instructions retired N=4096 0.010 0.006 Egraph based optimizations 25182208 maximum resident set size 856319878 instructions retired N=8192 0.022 0.014 Egraph based optimizations 42795008 maximum resident set size 1682183177 instructions retired N=16384 0.046 0.031 Egraph based optimizations 73809920 maximum resident set size 3338495358 instructions retiredThe patch leaves the output unchanged.
clif-util compile --target aarch64 --set opt_level=speed -p -Dprinted
identical IR and disassembly with and without it at N = 1, 2, 16, 1,024 and 4,096, andclif-util testpassed every
filetest incranelift/filetests/filetestswith it.Expected Results
Compile time grows linearly with N. With the patch, the instructions retired by
clif-utilnearly double with each
doubling of N, and at N = 16,384 the egraph pass takes 0.046 s:
N Egraph pass Instructions retired Peak memory 2,048 0.008 s 0.47G 17 MB 4,096 0.010 s 0.86G 24 MB 8,192 0.022 s 1.68G 41 MB 16,384 0.046 s 3.34G 70 MB Actual Results
Compile time grows quadratically with N: at N = 16,384 the egraph pass takes 0.30 s, and with the patch 0.046 s. The
instructions retired byclif-utilgrow 2.2, 2.6 and 2.9 times per doubling of N, and those beyond the patched run's
grow 4.0 times, while peak memory does not quite double:
N Egraph pass Instructions retired Peak memory 2,048 0.013 s 0.56G 16 MB 4,096 0.025 s 1.21G 24 MB 8,192 0.086 s 3.11G 41 MB 16,384 0.302 s 9.08G 69 MB The pass times are the first column of
-T. The instructions retired include parsing and verification. Each figure is
from one run, on a machine whose load average was about 47 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
When elaboration places a pure instruction, it looks for the outermost loop that the instruction can be hoisted out
of. For each argument, it scansloop_stack[..active_loop_depth]from the outside in, for the first loop whose hoist
block is dominated by the block where the argument is available. An argument computed inside the current loop, such
as a counter or a sum, is not available at any hoist block, so the scan reads the whole slice.The slice is not only the loops that contain the current block. A loop is popped from
loop_stackwhen the walk of
the dominator tree leaves its header's subtree, and in the test case every block after a loop is in that subtree, so
every earlier loop in the row stays on the stack.find_active_loop_depthreturns the position just past the
innermost loop on the stack that contains the block, so the slice includes every earlier sibling too. In the k-th loop
of the test case the slice has k entries, and the scan reads all of them for every argument computed in the loop.A profile of stock
clif-util testat N = 32,768 shows 516 of the egraph pass's 531 samples in this scan, a loop of
DominatorTree::block_dominatescalls over the stack's entries.The patch finds the same level by a binary search. Each loop header on the stack is dominated by the one before it,
so each hoist block strictly dominates the next. A value available at one hoist block is therefore available at
every later one, and the predicate of the scan is false for a prefix of the stack and true for the rest.
partition_pointreturns the first entry where it is true, and the length of the slice where it never is, as the
scan'sunwrap_or(active_loop_depth)did.--- a/cranelift/codegen/src/egraph/elaborate.rs +++ b/cranelift/codegen/src/egraph/elaborate.rs @@ -627,13 +627,19 @@ impl<'a> Elaborator<'a> { // hoist-block this value is already // available at. This is the loop-nest // level whose hoist-block we hoist to. - let hoist_level = self.loop_stack[..active_loop_depth] - .iter() - .position(|loop_entry| { - self.domtree + // + // Each hoist-block on the stack dominates the next, + // so once the value is available at one, it is + // available at all later ones. That lets a binary + // search find the level in log time, even when + // many loops in a row are on the stack. + let hoist_level = self.loop_stack[..active_loop_depth].partition_point( + |loop_entry| { + !self + .domtree .block_dominates(value.in_block, loop_entry.hoist_block) - }) - .unwrap_or(active_loop_depth); + }, + ); trace!( " -> arg: elab_value {:?} hoist level {:?}", value, hoist_level
kestred added the bug label to Issue #15266.
kestred added the cranelift label to Issue #15266.
alexcrichton added the cranelift:goal:compile-time label to Issue #15266.
cfallin commented on issue #15266:
Please feel free to submit your patch as PR rather than an inlined diff in an issue; I can then review it. Please also see our AI policy and try to keep descriptions concise; thanks.
kestred commented on issue #15266:
Thanks, as with: https://github.com/bytecodealliance/wasmtime/issues/15263#issuecomment-6954906616.
kestred closed issue #15266:
.clifTest CaseA function with N loops in a row. Each loop adds its counter to a sum until the counter reaches the second argument,
then stores the sum and starts the next loop. It is written as anoptimizefiletest, so thatclif-util testruns
the egraph pass alone. Here N = 2:test optimize set opt_level=speed target aarch64 function %loops(i64, i64) { block0(v0: i64, v1: i64): v2 = iconst.i64 0 v3 = iconst.i64 1 jump block1(v2, v2) block1(v4: i64, v5: i64): v6 = iadd v5, v4 v7 = iadd v4, v3 v8 = icmp ult v7, v1 brif v8, block1(v7, v6), block2 block2: store v6, v0+8 jump block3(v2, v2) block3(v9: i64, v10: i64): v11 = iadd v10, v9 v12 = iadd v9, v3 v13 = icmp ult v12, v1 brif v13, block3(v12, v11), block4 block4: store v11, v0+16 jump block5(v2, v2) block5(v14: i64, v15: i64): return }
gen.shwrites the same test at any N:#!/bin/sh # Writes a test of the egraph pass on N loops in a row, each summing its counter until the counter reaches n and # storing the sum at p + 8 * i. n=${1:-4} printf 'test optimize\nset opt_level=speed\ntarget aarch64\n\nfunction %%loops(i64, i64) {\nblock0(v0: i64, v1: i64):\n v2 = iconst.i64 0\n v3 = iconst.i64 1\n jump block1(v2, v2)\n' i=1 while [ $i -le $n ]; do v=$((5 * i - 1)) printf '\nblock%d(v%d: i64, v%d: i64):\n v%d = iadd v%d, v%d\n v%d = iadd v%d, v3\n v%d = icmp ult v%d, v1\n brif v%d, block%d(v%d, v%d), block%d\n\nblock%d:\n store v%d, v0+%d\n jump block%d(v2, v2)\n' \ $((2 * i - 1)) $v $((v + 1)) $((v + 2)) $((v + 1)) $v $((v + 3)) $v $((v + 4)) $((v + 3)) $((v + 4)) $((2 * i - 1)) $((v + 3)) $((v + 2)) $((2 * i)) $((2 * i)) $((v + 2)) $((8 * i)) $((2 * i + 1)) i=$((i + 1)) done printf '\nblock%d(v%d: i64, v%d: i64):\n return\n}\n' $((2 * n + 1)) $((5 * n + 4)) $((5 * n + 5))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, run the test 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 2048 4096 8192 16384; do sh gen.sh $n > loops.clif echo "N=$n" /usr/bin/time -l target/release/clif-util test -T loops.clif 2>&1 | grep -E 'Egraph|instructions retired|maximum resident' doneThen, apply the patch under Extra Info, build again, and run the same loop.
Stock 0.137.0 printed:
N=2048 0.013 0.010 Egraph based optimizations 16334848 maximum resident set size 560108476 instructions retired N=4096 0.025 0.021 Egraph based optimizations 25624576 maximum resident set size 1213169521 instructions retired N=8192 0.086 0.078 Egraph based optimizations 42975232 maximum resident set size 3108413903 instructions retired N=16384 0.302 0.287 Egraph based optimizations 72073216 maximum resident set size 9081268638 instructions retiredWith the patch:
N=2048 0.008 0.006 Egraph based optimizations 17416192 maximum resident set size 471986712 instructions retired N=4096 0.010 0.006 Egraph based optimizations 25182208 maximum resident set size 856319878 instructions retired N=8192 0.022 0.014 Egraph based optimizations 42795008 maximum resident set size 1682183177 instructions retired N=16384 0.046 0.031 Egraph based optimizations 73809920 maximum resident set size 3338495358 instructions retiredThe patch leaves the output unchanged.
clif-util compile --target aarch64 --set opt_level=speed -p -Dprinted
identical IR and disassembly with and without it at N = 1, 2, 16, 1,024 and 4,096, andclif-util testpassed every
filetest incranelift/filetests/filetestswith it.Expected Results
Compile time grows linearly with N. With the patch, the instructions retired by
clif-utilnearly double with each
doubling of N, and at N = 16,384 the egraph pass takes 0.046 s:
N Egraph pass Instructions retired Peak memory 2,048 0.008 s 0.47G 17 MB 4,096 0.010 s 0.86G 24 MB 8,192 0.022 s 1.68G 41 MB 16,384 0.046 s 3.34G 70 MB Actual Results
Compile time grows quadratically with N: at N = 16,384 the egraph pass takes 0.30 s, and with the patch 0.046 s. The
instructions retired byclif-utilgrow 2.2, 2.6 and 2.9 times per doubling of N, and those beyond the patched run's
grow 4.0 times, while peak memory does not quite double:
N Egraph pass Instructions retired Peak memory 2,048 0.013 s 0.56G 16 MB 4,096 0.025 s 1.21G 24 MB 8,192 0.086 s 3.11G 41 MB 16,384 0.302 s 9.08G 69 MB The pass times are the first column of
-T. The instructions retired include parsing and verification. Each figure is
from one run, on a machine whose load average was about 47 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
When elaboration places a pure instruction, it looks for the outermost loop that the instruction can be hoisted out
of. For each argument, it scansloop_stack[..active_loop_depth]from the outside in, for the first loop whose hoist
block is dominated by the block where the argument is available. An argument computed inside the current loop, such
as a counter or a sum, is not available at any hoist block, so the scan reads the whole slice.The slice is not only the loops that contain the current block. A loop is popped from
loop_stackwhen the walk of
the dominator tree leaves its header's subtree, and in the test case every block after a loop is in that subtree, so
every earlier loop in the row stays on the stack.find_active_loop_depthreturns the position just past the
innermost loop on the stack that contains the block, so the slice includes every earlier sibling too. In the k-th loop
of the test case the slice has k entries, and the scan reads all of them for every argument computed in the loop.A profile of stock
clif-util testat N = 32,768 shows 516 of the egraph pass's 531 samples in this scan, a loop of
DominatorTree::block_dominatescalls over the stack's entries.The patch finds the same level by a binary search. Each loop header on the stack is dominated by the one before it,
so each hoist block strictly dominates the next. A value available at one hoist block is therefore available at
every later one, and the predicate of the scan is false for a prefix of the stack and true for the rest.
partition_pointreturns the first entry where it is true, and the length of the slice where it never is, as the
scan'sunwrap_or(active_loop_depth)did.--- a/cranelift/codegen/src/egraph/elaborate.rs +++ b/cranelift/codegen/src/egraph/elaborate.rs @@ -627,13 +627,19 @@ impl<'a> Elaborator<'a> { // hoist-block this value is already // available at. This is the loop-nest // level whose hoist-block we hoist to. - let hoist_level = self.loop_stack[..active_loop_depth] - .iter() - .position(|loop_entry| { - self.domtree + // + // Each hoist-block on the stack dominates the next, + // so once the value is available at one, it is + // available at all later ones. That lets a binary + // search find the level in log time, even when + // many loops in a row are on the stack. + let hoist_level = self.loop_stack[..active_loop_depth].partition_point( + |loop_entry| { + !self + .domtree .block_dominates(value.in_block, loop_entry.hoist_block) - }) - .unwrap_or(active_loop_depth); + }, + ); trace!( " -> arg: elab_value {:?} hoist level {:?}", value, hoist_level
Last updated: Nov 19 2026 at 20:05 UTC