Stream: git-wasmtime

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


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

kestred opened issue #15263:

.clif Test Case

A chain of N blocks, each dominated by the one before it. Each block divides its value by the constant 3 and either
returns or passes the value plus 1 to the next block. Here N = 2:

function %chain(i64) -> i64 {
block0(v0: i64):
    v1 = iconst.i64 3
    v2 = iconst.i64 1
    jump block1(v0)

block1(v3: i64):
    v4 = srem v3, v1
    v5 = iadd v3, v2
    brif v4, block3(v5), block2

block2:
    return v3

block3(v6: i64):
    v7 = srem v6, v1
    v8 = iadd v6, v2
    brif v7, block5(v8), block4

block4:
    return v6

block5(v9: i64):
    return v9
}

The following script writes the same function at any N:

#!/bin/sh
# Writes a chain of N blocks, each returning its value if the value is divisible by 3 and otherwise passing on the
# value plus 1.
n=${1:-4}
printf 'function %%chain(i64) -> i64 {\nblock0(v0: i64):\n    v1 = iconst.i64 3\n    v2 = iconst.i64 1\n    jump block1(v0)\n'
i=1
while [ $i -le $n ]; do
  v=$((3 * i))
  printf '\nblock%d(v%d: i64):\n    v%d = srem v%d, v1\n    v%d = iadd v%d, v2\n    brif v%d, block%d(v%d), block%d\n\nblock%d:\n    return v%d\n' \
    $((2 * i - 1)) $v $((v + 1)) $v $((v + 2)) $v $((v + 1)) $((2 * i + 1)) $((v + 2)) $((2 * i)) $((2 * i)) $v
  i=$((i + 1))
done
printf '\nblock%d(v%d: i64):\n    return v%d\n}\n' $((2 * n + 1)) $((3 * n + 3)) $((3 * n + 3))

Steps to Reproduce

Stock 0.137.0 printed:

N=2048
   0.042    0.040  Egraph based optimizations
            35274752  maximum resident set size
          1266600151  instructions retired
N=4096
   0.114    0.110  Egraph based optimizations
            64110592  maximum resident set size
          3262324004  instructions retired
N=8192
   0.368    0.360  Egraph based optimizations
           122896384  maximum resident set size
          9827252833  instructions retired
N=16384
   1.283    1.266  Egraph based optimizations
           246464512  maximum resident set size
         32726829030  instructions retired

With the patch:

N=2048
   0.025    0.023  Egraph based optimizations
            35389440  maximum resident set size
           862344272  instructions retired
N=4096
   0.046    0.042  Egraph based optimizations
            64126976  maximum resident set size
          1651218000  instructions retired
N=8192
   0.102    0.094  Egraph based optimizations
           122994688  maximum resident set size
          3384001793  instructions retired
N=16384
   0.227    0.211  Egraph based optimizations
           246579200  maximum resident set size
          6953866487  instructions retired

The patch leaves the output unchanged: clif-util compile -D printed identical disassembly with and without it at
N = 1,024.

Expected Results

Compile time grows linearly with N. With the patch, the instructions retired by clif-util double with each doubling
of N, and at N = 16,384 the egraph pass takes 0.23 s:

N Egraph pass Instructions retired Peak memory
2,048 0.025 s 0.86G 34 MB
4,096 0.046 s 1.65G 61 MB
8,192 0.102 s 3.38G 117 MB
16,384 0.227 s 6.95G 235 MB

Actual Results

Compile time grows quadratically with N: at N = 16,384 the egraph pass takes 1.28 s, and with the patch 0.23 s. The
instructions retired by clif-util grow 2.6, 3.0 and 3.3 times per doubling of N, while peak memory only doubles:

N Egraph pass Instructions retired Peak memory
2,048 0.042 s 1.27G 34 MB
4,096 0.114 s 3.26G 61 MB
8,192 0.368 s 9.83G 117 MB
16,384 1.283 s 32.73G 235 MB

The pass times are the first column of -T. The instructions retired include parsing, verification and every other
pass. Each figure is from one run, on a machine whose load average was about 7 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

OptimizeCtx::depth_of_block_in_gvn_map finds a block's level on the GVN map's scope stack by scanning
gvn_map_blocks from the top. insert_pure_enode calls it for every new pure node, at the block where the node is
available. A node available near the root scans nearly the whole stack, and the stack is as deep as the dominator
tree. Here, each srem by a constant is rewritten into a multiply by a magic number, whose constants are available at
the entry block, so each of the N blocks scans a stack about as deep as its own position.

The patch records each block's level as the block is pushed, and looks it up instead.

--- a/cranelift/codegen/src/egraph/mod.rs
+++ b/cranelift/codegen/src/egraph/mod.rs
@@ -148,6 +148,8 @@
     eclass_size: &'opt mut SecondaryMap<Value, u8>,
     pub(crate) gvn_map: &'opt mut ScopedHashMap<(Type, InstructionData), Option<Value>>,
     pub(crate) gvn_map_blocks: &'opt Vec<Block>,
+    /// The index of each block of `gvn_map_blocks` in it.
+    gvn_map_depth: &'opt SecondaryMap<Block, u32>,
     pub(crate) remat_values: &'opt mut FxHashSet<Value>,
     pub(crate) stats: &'opt mut Stats,
     domtree: &'opt DominatorTree,
@@ -339,13 +341,11 @@
             block,
             self.gvn_map_blocks
         );
-        self.gvn_map_blocks
-            .iter()
-            .enumerate()
-            .rev()
-            .find(|&(_, b)| *b == block)
-            .unwrap()
-            .0
+        // A lookup rather than a scan of the stack, which would be
+        // quadratic where the domtree is deep, as along a chain of loops.
+        let depth = self.gvn_map_depth[block] as usize;
+        debug_assert_eq!(self.gvn_map_blocks[depth], block);
+        depth
     }

     /// Optimizes an enode by applying any matching mid-end rewrite
@@ -884,6 +884,7 @@
         // The block in the domtree preorder traversal at each level
         // of the GVN map.
         let mut gvn_map_blocks: Vec<Block> = vec![];
+        let mut gvn_map_depth: SecondaryMap<Block, u32> = SecondaryMap::new();

         // To get the best possible merging and canonicalization, we
         // track where a value is "available" at: this is the
@@ -927,6 +928,7 @@
             }

             gvn_map.increment_depth();
+            gvn_map_depth[block] = gvn_map_blocks.len() as u32;
             gvn_map_blocks.push(block);

             let mut alias_analysis_state = self.alias_analysis.push_scope(&*self.cfg, block);
@@ -983,6 +985,7 @@
                     value_to_opt_value: &mut value_to_opt_value,
                     gvn_map: &mut gvn_map,
                     gvn_map_blocks: &mut gvn_map_blocks,
+                    gvn_map_depth: &gvn_map_depth,
                     available_block: &mut available_block,
                     eclass_size: &mut eclass_size,
                     rewrite_depth: 0,

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

kestred added the bug label to Issue #15263.

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

kestred added the cranelift label to Issue #15263.

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

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

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

kestred edited issue #15263:

.clif Test Case

A chain of N blocks, each dominated by the one before it. Each block divides its value by the constant 3 and either
returns or passes the value plus 1 to the next block. Here N = 2:

function %chain(i64) -> i64 {
block0(v0: i64):
    v1 = iconst.i64 3
    v2 = iconst.i64 1
    jump block1(v0)

block1(v3: i64):
    v4 = srem v3, v1
    v5 = iadd v3, v2
    brif v4, block3(v5), block2

block2:
    return v3

block3(v6: i64):
    v7 = srem v6, v1
    v8 = iadd v6, v2
    brif v7, block5(v8), block4

block4:
    return v6

block5(v9: i64):
    return v9
}

The following script writes the same function at any N:

#!/bin/sh
# Writes a chain of N blocks, each returning its value if the value is divisible by 3 and otherwise passing on the
# value plus 1.
n=${1:-4}
printf 'function %%chain(i64) -> i64 {\nblock0(v0: i64):\n    v1 = iconst.i64 3\n    v2 = iconst.i64 1\n    jump block1(v0)\n'
i=1
while [ $i -le $n ]; do
  v=$((3 * i))
  printf '\nblock%d(v%d: i64):\n    v%d = srem v%d, v1\n    v%d = iadd v%d, v2\n    brif v%d, block%d(v%d), block%d\n\nblock%d:\n    return v%d\n' \
    $((2 * i - 1)) $v $((v + 1)) $v $((v + 2)) $v $((v + 1)) $((2 * i + 1)) $((v + 2)) $((2 * i)) $((2 * i)) $v
  i=$((i + 1))
done
printf '\nblock%d(v%d: i64):\n    return v%d\n}\n' $((2 * n + 1)) $((3 * n + 3)) $((3 * n + 3))

Steps to Reproduce

Stock 0.137.0 printed:

N=2048
   0.042    0.040  Egraph based optimizations
            35274752  maximum resident set size
          1266600151  instructions retired
N=4096
   0.114    0.110  Egraph based optimizations
            64110592  maximum resident set size
          3262324004  instructions retired
N=8192
   0.368    0.360  Egraph based optimizations
           122896384  maximum resident set size
          9827252833  instructions retired
N=16384
   1.283    1.266  Egraph based optimizations
           246464512  maximum resident set size
         32726829030  instructions retired

With the patch:

N=2048
   0.025    0.023  Egraph based optimizations
            35389440  maximum resident set size
           862344272  instructions retired
N=4096
   0.046    0.042  Egraph based optimizations
            64126976  maximum resident set size
          1651218000  instructions retired
N=8192
   0.102    0.094  Egraph based optimizations
           122994688  maximum resident set size
          3384001793  instructions retired
N=16384
   0.227    0.211  Egraph based optimizations
           246579200  maximum resident set size
          6953866487  instructions retired

The patch leaves the output unchanged: clif-util compile -D printed identical disassembly with and without it at
N = 1,024.

Expected Results

Compile time grows linearly with N. With the patch, the instructions retired by clif-util double with each doubling
of N, and at N = 16,384 the egraph pass takes 0.23 s:

N Egraph pass Instructions retired Peak memory
2,048 0.025 s 0.86G 34 MB
4,096 0.046 s 1.65G 61 MB
8,192 0.102 s 3.38G 117 MB
16,384 0.227 s 6.95G 235 MB

Actual Results

Compile time grows quadratically with N: at N = 16,384 the egraph pass takes 1.28 s, and with the patch 0.23 s. The
instructions retired by clif-util grow 2.6, 3.0 and 3.3 times per doubling of N, while peak memory only doubles:

N Egraph pass Instructions retired Peak memory
2,048 0.042 s 1.27G 34 MB
4,096 0.114 s 3.26G 61 MB
8,192 0.368 s 9.83G 117 MB
16,384 1.283 s 32.73G 235 MB

The pass times are the first column of -T. The instructions retired include parsing, verification and every other
pass. Each figure is from one run, on a machine whose load average was about 7 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

OptimizeCtx::depth_of_block_in_gvn_map finds a block's level on the GVN map's scope stack by scanning
gvn_map_blocks from the top. insert_pure_enode calls it for every new pure node, at the block where the node is
available. A node available near the root scans nearly the whole stack, and the stack is as deep as the dominator
tree. Here, each srem by a constant is rewritten into a multiply by a magic number, whose constants are available at
the entry block, so each of the N blocks scans a stack about as deep as its own position.

The patch records each block's level as the block is pushed, and looks it up instead.

--- a/cranelift/codegen/src/egraph/mod.rs
+++ b/cranelift/codegen/src/egraph/mod.rs
@@ -148,6 +148,8 @@
     eclass_size: &'opt mut SecondaryMap<Value, u8>,
     pub(crate) gvn_map: &'opt mut ScopedHashMap<(Type, InstructionData), Option<Value>>,
     pub(crate) gvn_map_blocks: &'opt Vec<Block>,
+    /// The index of each block of `gvn_map_blocks` in it.
+    gvn_map_depth: &'opt SecondaryMap<Block, u32>,
     pub(crate) remat_values: &'opt mut FxHashSet<Value>,
     pub(crate) stats: &'opt mut Stats,
     domtree: &'opt DominatorTree,
@@ -339,13 +341,11 @@
             block,
             self.gvn_map_blocks
         );
-        self.gvn_map_blocks
-            .iter()
-            .enumerate()
-            .rev()
-            .find(|&(_, b)| *b == block)
-            .unwrap()
-            .0
+        // A lookup rather than a scan of the stack, which would be
+        // quadratic where the domtree is deep, as along a chain of loops.
+        let depth = self.gvn_map_depth[block] as usize;
+        debug_assert_eq!(self.gvn_map_blocks[depth], block);
+        depth
     }

     /// Optimizes an enode by applying any matching mid-end rewrite
@@ -884,6 +884,7 @@
         // The block in the domtree preorder traversal at each level
         // of the GVN map.
         let mut gvn_map_blocks: Vec<Block> = vec![];
+        let mut gvn_map_depth: SecondaryMap<Block, u32> = SecondaryMap::new();

         // To get the best possible merging and canonicalization, we
         // track where a value is "available" at: this is the
@@ -927,6 +928,7 @@
             }

             gvn_map.increment_depth();
+            gvn_map_depth[block] = gvn_map_blocks.len() as u32;
             gvn_map_blocks.push(block);

             let mut alias_analysis_state = self.alias_analysis.push_scope(&*self.cfg, block);
@@ -983,6 +985,7 @@
                     value_to_opt_value: &mut value_to_opt_value,
                     gvn_map: &mut gvn_map,
                     gvn_map_blocks: &mut gvn_map_blocks,
+                    gvn_map_depth: &gvn_map_depth,
                     available_block: &mut available_block,
                     eclass_size: &mut eclass_size,
                     rewrite_depth: 0,

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

cfallin commented on issue #15263:

@kestred I'd be happy to review a PR if you'd like to submit one.

Also, a few high-level thoughts:

Thanks!

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

kestred closed issue #15263:

.clif Test Case

A chain of N blocks, each dominated by the one before it. Each block divides its value by the constant 3 and either
returns or passes the value plus 1 to the next block. Here N = 2:

function %chain(i64) -> i64 {
block0(v0: i64):
    v1 = iconst.i64 3
    v2 = iconst.i64 1
    jump block1(v0)

block1(v3: i64):
    v4 = srem v3, v1
    v5 = iadd v3, v2
    brif v4, block3(v5), block2

block2:
    return v3

block3(v6: i64):
    v7 = srem v6, v1
    v8 = iadd v6, v2
    brif v7, block5(v8), block4

block4:
    return v6

block5(v9: i64):
    return v9
}

The following script writes the same function at any N:

#!/bin/sh
# Writes a chain of N blocks, each returning its value if the value is divisible by 3 and otherwise passing on the
# value plus 1.
n=${1:-4}
printf 'function %%chain(i64) -> i64 {\nblock0(v0: i64):\n    v1 = iconst.i64 3\n    v2 = iconst.i64 1\n    jump block1(v0)\n'
i=1
while [ $i -le $n ]; do
  v=$((3 * i))
  printf '\nblock%d(v%d: i64):\n    v%d = srem v%d, v1\n    v%d = iadd v%d, v2\n    brif v%d, block%d(v%d), block%d\n\nblock%d:\n    return v%d\n' \
    $((2 * i - 1)) $v $((v + 1)) $v $((v + 2)) $v $((v + 1)) $((2 * i + 1)) $((v + 2)) $((2 * i)) $((2 * i)) $v
  i=$((i + 1))
done
printf '\nblock%d(v%d: i64):\n    return v%d\n}\n' $((2 * n + 1)) $((3 * n + 3)) $((3 * n + 3))

Steps to Reproduce

Stock 0.137.0 printed:

N=2048
   0.042    0.040  Egraph based optimizations
            35274752  maximum resident set size
          1266600151  instructions retired
N=4096
   0.114    0.110  Egraph based optimizations
            64110592  maximum resident set size
          3262324004  instructions retired
N=8192
   0.368    0.360  Egraph based optimizations
           122896384  maximum resident set size
          9827252833  instructions retired
N=16384
   1.283    1.266  Egraph based optimizations
           246464512  maximum resident set size
         32726829030  instructions retired

With the patch:

N=2048
   0.025    0.023  Egraph based optimizations
            35389440  maximum resident set size
           862344272  instructions retired
N=4096
   0.046    0.042  Egraph based optimizations
            64126976  maximum resident set size
          1651218000  instructions retired
N=8192
   0.102    0.094  Egraph based optimizations
           122994688  maximum resident set size
          3384001793  instructions retired
N=16384
   0.227    0.211  Egraph based optimizations
           246579200  maximum resident set size
          6953866487  instructions retired

The patch leaves the output unchanged: clif-util compile -D printed identical disassembly with and without it at
N = 1,024.

Expected Results

Compile time grows linearly with N. With the patch, the instructions retired by clif-util double with each doubling
of N, and at N = 16,384 the egraph pass takes 0.23 s:

N Egraph pass Instructions retired Peak memory
2,048 0.025 s 0.86G 34 MB
4,096 0.046 s 1.65G 61 MB
8,192 0.102 s 3.38G 117 MB
16,384 0.227 s 6.95G 235 MB

Actual Results

Compile time grows quadratically with N: at N = 16,384 the egraph pass takes 1.28 s, and with the patch 0.23 s. The
instructions retired by clif-util grow 2.6, 3.0 and 3.3 times per doubling of N, while peak memory only doubles:

N Egraph pass Instructions retired Peak memory
2,048 0.042 s 1.27G 34 MB
4,096 0.114 s 3.26G 61 MB
8,192 0.368 s 9.83G 117 MB
16,384 1.283 s 32.73G 235 MB

The pass times are the first column of -T. The instructions retired include parsing, verification and every other
pass. Each figure is from one run, on a machine whose load average was about 7 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

OptimizeCtx::depth_of_block_in_gvn_map finds a block's level on the GVN map's scope stack by scanning
gvn_map_blocks from the top. insert_pure_enode calls it for every new pure node, at the block where the node is
available. A node available near the root scans nearly the whole stack, and the stack is as deep as the dominator
tree. Here, each srem by a constant is rewritten into a multiply by a magic number, whose constants are available at
the entry block, so each of the N blocks scans a stack about as deep as its own position.

The patch records each block's level as the block is pushed, and looks it up instead.

--- a/cranelift/codegen/src/egraph/mod.rs
+++ b/cranelift/codegen/src/egraph/mod.rs
@@ -148,6 +148,8 @@
     eclass_size: &'opt mut SecondaryMap<Value, u8>,
     pub(crate) gvn_map: &'opt mut ScopedHashMap<(Type, InstructionData), Option<Value>>,
     pub(crate) gvn_map_blocks: &'opt Vec<Block>,
+    /// The index of each block of `gvn_map_blocks` in it.
+    gvn_map_depth: &'opt SecondaryMap<Block, u32>,
     pub(crate) remat_values: &'opt mut FxHashSet<Value>,
     pub(crate) stats: &'opt mut Stats,
     domtree: &'opt DominatorTree,
@@ -339,13 +341,11 @@
             block,
             self.gvn_map_blocks
         );
-        self.gvn_map_blocks
-            .iter()
-            .enumerate()
-            .rev()
-            .find(|&(_, b)| *b == block)
-            .unwrap()
-            .0
+        // A lookup rather than a scan of the stack, which would be
+        // quadratic where the domtree is deep, as along a chain of loops.
+        let depth = self.gvn_map_depth[block] as usize;
+        debug_assert_eq!(self.gvn_map_blocks[depth], block);
+        depth
     }

     /// Optimizes an enode by applying any matching mid-end rewrite
@@ -884,6 +884,7 @@
         // The block in the domtree preorder traversal at each level
         // of the GVN map.
         let mut gvn_map_blocks: Vec<Block> = vec![];
+        let mut gvn_map_depth: SecondaryMap<Block, u32> = SecondaryMap::new();

         // To get the best possible merging and canonicalization, we
         // track where a value is "available" at: this is the
@@ -927,6 +928,7 @@
             }

             gvn_map.increment_depth();
+            gvn_map_depth[block] = gvn_map_blocks.len() as u32;
             gvn_map_blocks.push(block);

             let mut alias_analysis_state = self.alias_analysis.push_scope(&*self.cfg, block);
@@ -983,6 +985,7 @@
                     value_to_opt_value: &mut value_to_opt_value,
                     gvn_map: &mut gvn_map,
                     gvn_map_blocks: &mut gvn_map_blocks,
+                    gvn_map_depth: &gvn_map_depth,
                     available_block: &mut available_block,
                     eclass_size: &mut eclass_size,
                     rewrite_depth: 0,

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

kestred commented on issue #15263:

Thank you @cfallin, I've been working on trying to mitigate possible malicious inputs in a project I'm working on.
For these inputs, I'm switching to limiting function size in my project before it reaches Cranelift instead.

I'll focus on narrowing down the other issues to the most useful contributions.
Will make sure that both normal programs and worst case programs both improve.


Last updated: Nov 19 2026 at 20:05 UTC