Stream: git-wasmtime

Topic: wasmtime / issue #15266 Cranelift: egraph: performance: f...


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

kestred opened issue #15266:

.clif Test Case

A 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 an optimize filetest, so that clif-util test runs
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.sh writes 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

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 retired

With 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 retired

The patch leaves the output unchanged. clif-util compile --target aarch64 --set opt_level=speed -p -D printed
identical IR and disassembly with and without it at N = 1, 2, 16, 1,024 and 4,096, and clif-util test passed every
filetest in cranelift/filetests/filetests with it.

Expected Results

Compile time grows linearly with N. With the patch, the instructions retired by clif-util nearly 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 by clif-util grow 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-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

When elaboration places a pure instruction, it looks for the outermost loop that the instruction can be hoisted out
of. For each argument, it scans loop_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_stack when 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_depth returns 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 test at N = 32,768 shows 516 of the egraph pass's 531 samples in this scan, a loop of
DominatorTree::block_dominates calls 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_point returns the first entry where it is true, and the length of the slice where it never is, as the
scan's unwrap_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

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

kestred added the bug label to Issue #15266.

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

kestred added the cranelift label to Issue #15266.

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

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

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

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.

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

kestred commented on issue #15266:

Thanks, as with: https://github.com/bytecodealliance/wasmtime/issues/15263#issuecomment-6954906616.

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

kestred closed issue #15266:

.clif Test Case

A 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 an optimize filetest, so that clif-util test runs
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.sh writes 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

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 retired

With 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 retired

The patch leaves the output unchanged. clif-util compile --target aarch64 --set opt_level=speed -p -D printed
identical IR and disassembly with and without it at N = 1, 2, 16, 1,024 and 4,096, and clif-util test passed every
filetest in cranelift/filetests/filetests with it.

Expected Results

Compile time grows linearly with N. With the patch, the instructions retired by clif-util nearly 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 by clif-util grow 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-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

When elaboration places a pure instruction, it looks for the outermost loop that the instruction can be hoisted out
of. For each argument, it scans loop_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_stack when 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_depth returns 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 test at N = 32,768 shows 516 of the egraph pass's 531 samples in this scan, a loop of
DominatorTree::block_dominates calls 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_point returns the first entry where it is true, and the length of the slice where it never is, as the
scan's unwrap_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