Skip to content

fix: emit all allocas in the init block instead of the current block - #1664

Closed
TomerStarkware wants to merge 1 commit into
mainfrom
fix/alloca-in-loops
Closed

TomerStarkware wants to merge 1 commit into
mainfrom
fix/alloca-in-loops

Conversation

@TomerStarkware

Copy link
Copy Markdown
Collaborator

Summary

Several places emitted llvm.alloca into the libfunc's own block rather than the function's init (pre-entry) block. When that block sits inside a loop, the alloca re-executes on every iteration and the stack grows without bound.

All allocas now go through the init block, which runs exactly once per function call:

  • felt252_dict_squash: range-check and gas pointers.
  • squashed_dict into_entries: result array slot.
  • qm31 binary ops: lhs/rhs operand slots.
  • RuntimeBindingsMeta::libfunc_qm31_bin_op: result slot. Now takes the LibfuncHelper instead of a bare &Module (same as dict_get); callers already passed the helper via deref.
  • trace_dump::build_state_snapshot (with-trace-dump feature): per-variable snapshot slots emitted at every statement. Takes an explicit init block; both call sites in compiler.rs pass the pre-entry block.

Test plan

  • cargo check with and without with-trace-dump
  • cargo clippy --all-targets clean, cargo fmt applied
  • cargo test --lib -- qm31 felt252_dict squashed_dict (41 passed)

🤖 Generated with Claude Code

Several libfuncs (felt252_dict_squash, squashed_dict into_entries, qm31
binary ops), the qm31 runtime binding and the trace-dump state snapshot
emitted `llvm.alloca` into the libfunc's own block. When that block is
part of a loop, the alloca re-executes on every iteration and the stack
grows unboundedly. Route every alloca through the function's init block,
which runs exactly once per call.

`libfunc_qm31_bin_op` now takes the `LibfuncHelper` (like `dict_get`) so
it can reach the init block, and `build_state_snapshot` takes an explicit
init block argument.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.548 ± 0.029 1.512 1.601 1.03 ± 0.02
base dict_insert.cairo (AOT) 1.496 ± 0.012 1.472 1.509 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.766 ± 0.012 1.748 1.780 1.01 ± 0.01
head dict_insert.cairo (AOT) 1.748 ± 0.006 1.738 1.757 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.363 ± 0.017 1.346 1.394 1.03 ± 0.02
base dict_snapshot.cairo (AOT) 1.330 ± 0.017 1.298 1.356 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.593 ± 0.010 1.579 1.608 1.02 ± 0.01
head dict_snapshot.cairo (AOT) 1.565 ± 0.008 1.550 1.577 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 1.416 ± 0.016 1.391 1.444 1.00
base factorial_2M.cairo (AOT) 1.422 ± 0.029 1.384 1.484 1.00 ± 0.02

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 1.639 ± 0.009 1.624 1.649 1.01 ± 0.01
head factorial_2M.cairo (AOT) 1.620 ± 0.008 1.604 1.630 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.379 ± 0.024 1.350 1.438 1.03 ± 0.02
base fib_2M.cairo (AOT) 1.335 ± 0.012 1.314 1.354 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.593 ± 0.008 1.580 1.601 1.01 ± 0.01
head fib_2M.cairo (AOT) 1.572 ± 0.009 1.563 1.586 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.375 ± 0.017 1.354 1.406 1.04 ± 0.01
base linear_search.cairo (AOT) 1.327 ± 0.006 1.316 1.336 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.620 ± 0.005 1.611 1.628 1.02 ± 0.01
head linear_search.cairo (AOT) 1.587 ± 0.008 1.574 1.604 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.359 ± 0.021 1.333 1.394 1.01 ± 0.02
base logistic_map.cairo (AOT) 1.341 ± 0.025 1.309 1.394 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.597 ± 0.012 1.583 1.626 1.02 ± 0.01
head logistic_map.cairo (AOT) 1.562 ± 0.010 1.552 1.581 1.00

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 11.488 ± 0.058 11.363 11.555 5.99 ± 0.11
cairo-native (embedded AOT) 1.995 ± 0.036 1.910 2.030 1.04 ± 0.03
cairo-native (embedded JIT using LLVM's ORC Engine) 1.917 ± 0.034 1.874 1.969 1.00

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 561.7 ± 11.1 545.6 580.9 1.00
cairo-native (embedded AOT) 1755.1 ± 22.7 1725.9 1802.2 3.12 ± 0.07
cairo-native (embedded JIT using LLVM's ORC Engine) 1786.5 ± 18.0 1754.3 1810.3 3.18 ± 0.07

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 5.090 ± 0.031 5.046 5.143 2.77 ± 0.04
cairo-native (embedded AOT) 1.841 ± 0.021 1.796 1.879 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.871 ± 0.042 1.820 1.944 1.02 ± 0.03

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 5.043 ± 0.036 4.999 5.101 2.91 ± 0.04
cairo-native (embedded AOT) 1.732 ± 0.022 1.697 1.768 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.755 ± 0.023 1.712 1.800 1.01 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 622.0 ± 12.3 605.9 648.2 1.00
cairo-native (embedded AOT) 1776.8 ± 30.8 1736.2 1842.6 2.86 ± 0.07
cairo-native (embedded JIT using LLVM's ORC Engine) 1802.9 ± 23.4 1774.0 1853.7 2.90 ± 0.07

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 519.7 ± 17.3 492.0 545.7 1.00
cairo-native (embedded AOT) 1741.1 ± 14.3 1715.0 1763.5 3.35 ± 0.11
cairo-native (embedded JIT using LLVM's ORC Engine) 1748.2 ± 34.3 1694.5 1805.8 3.36 ± 0.13

@TomerStarkware
TomerStarkware deleted the fix/alloca-in-loops branch October 1, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant