kestred opened issue #15263:
.clifTest CaseA 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
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, 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 2048 4096 8192 16384; do sh 4-gen.sh $n > chain.clif echo "N=$n" /usr/bin/time -l target/release/clif-util compile --target aarch64 --set opt_level=speed -T chain.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.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 retiredWith 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 retiredThe patch leaves the output unchanged:
clif-util compile -Dprinted 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-utildouble 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 byclif-utilgrow 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-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
OptimizeCtx::depth_of_block_in_gvn_mapfinds a block's level on the GVN map's scope stack by scanning
gvn_map_blocksfrom the top.insert_pure_enodecalls 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, eachsremby 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,
kestred added the bug label to Issue #15263.
kestred added the cranelift label to Issue #15263.
alexcrichton added the cranelift:goal:compile-time label to Issue #15263.
kestred edited issue #15263:
.clifTest CaseA 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
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, 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 2048 4096 8192 16384; do sh gen.sh $n > chain.clif echo "N=$n" /usr/bin/time -l target/release/clif-util compile --target aarch64 --set opt_level=speed -T chain.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.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 retiredWith 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 retiredThe patch leaves the output unchanged:
clif-util compile -Dprinted 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-utildouble 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 byclif-utilgrow 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-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
OptimizeCtx::depth_of_block_in_gvn_mapfinds a block's level on the GVN map's scope stack by scanning
gvn_map_blocksfrom the top.insert_pure_enodecalls 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, eachsremby 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,
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:
- We do have an AI policy that specifies that GitHub communications (and other human communication media we have, like Zulip) are primarily meant to have human-written text. You can include an LLM-produced audit or bug report in
<details>...</details>if you want, but please summarize much more briefly and concisely first, as maintainer time is valuable (and stretched thin) here.- I see a bunch of "quadratic behavior" issues you filed both here on Cranelift, and on regalloc2, over the weekend. We can certainly fix issues where there's an easy fix (like here). But also in general, since Cranelift is an optimizing compiler, we don't have any general guarantee about asymptotic behavior, because many of the underlying algorithms (e.g. iterative dataflow analysis) have bad worst cases (e.g. cubic) even if the common case is usually fast. So I wouldn't call "run-time is quadratic" a bug per-se, though again we're happy to fix it where there are better solutions. Just wanted to clarify in case you're prompting an LLM to audit for this kind of thing.
Thanks!
kestred closed issue #15263:
.clifTest CaseA 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
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, 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 2048 4096 8192 16384; do sh gen.sh $n > chain.clif echo "N=$n" /usr/bin/time -l target/release/clif-util compile --target aarch64 --set opt_level=speed -T chain.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.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 retiredWith 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 retiredThe patch leaves the output unchanged:
clif-util compile -Dprinted 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-utildouble 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 byclif-utilgrow 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-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
OptimizeCtx::depth_of_block_in_gvn_mapfinds a block's level on the GVN map's scope stack by scanning
gvn_map_blocksfrom the top.insert_pure_enodecalls 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, eachsremby 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,
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