Skip to content

Conversation

DiegoCivi
Copy link
Contributor

@DiegoCivi DiegoCivi commented Sep 24, 2025

Fix system builtin as implicit for cairo1 programs

Description

When executing a cairo1 program that had the system builtin as an implicit and it received input arguments, a memory error would stop execution. This was happening because it was trying to use memory that had been already used.

To execute a program, first an entry code is added (done in create_entry_code()). In the case of the System builtin we are adding the following CASM:

casm_build_extend! {ctx,
    tempvar system;
    hint AllocSegment {} into {dst: system};
    ap += 1;
};

This allocs one value into memory (because of the tempvar instruction).

After creating the corresponding entry code we load the input arguments, if any, into the memory (done in load_arguments()). However this values are loaded after the ones of the entry code. As the entry code is executed after all of this, its values are still not present so we need to leave gaps in memory for them. The error we were seeing, happened because we were not leaving a gap for the value created in the entry code for the System builtin.

Something similar happened when we wanted to use proof_mode. With this mode, the arguments also need to be on the output segment, so again if we do not leave the corresponding gaps, when trying to load them we would be trying to use unavailable memory.

Added tests

There are different cases where we need to be careful with memory when loading tha arguments:

  • When we have input arguments
  • When we have the System builtin
  • When we have the SegmentArena builtin
  • When we have the Gas builtin
  • When we want use the proof mode

All these cases can create different combinations of situations were we would need to leave different gaps in the memory. I have added some tests that check some of these combinations:

  • with_input/implicit_gas_builtin.cairo: Uses input arguments and the gas builtin as an implicit argument.
  • with_input/implicit_system_builtin.cairo: Uses input arguments and the system builtin as an implicit argument
  • with_input/system_segment_gas.cairo: Uses input arguments and the gas, system and segment arena builtins.

Closes #1850

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.
    • CHANGELOG has been updated.

Copy link

github-actions bot commented Sep 24, 2025

**Hyper Thereading Benchmark results**




hyperfine -r 2 -n "hyper_threading_main threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_main' -n "hyper_threading_pr threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 1
  Time (mean ± σ):     24.133 s ±  0.027 s    [User: 21.975 s, System: 2.153 s]
  Range (min … max):   24.114 s … 24.152 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     24.016 s ±  0.006 s    [User: 22.111 s, System: 1.900 s]
  Range (min … max):   24.012 s … 24.021 s    2 runs
 
Summary
  hyper_threading_pr threads: 1 ran
    1.00 ± 0.00 times faster than hyper_threading_main threads: 1




hyperfine -r 2 -n "hyper_threading_main threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_main' -n "hyper_threading_pr threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 2
  Time (mean ± σ):     13.247 s ±  0.023 s    [User: 21.999 s, System: 2.203 s]
  Range (min … max):   13.231 s … 13.264 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     13.406 s ±  0.122 s    [User: 22.268 s, System: 2.150 s]
  Range (min … max):   13.320 s … 13.493 s    2 runs
 
Summary
  hyper_threading_main threads: 2 ran
    1.01 ± 0.01 times faster than hyper_threading_pr threads: 2




hyperfine -r 2 -n "hyper_threading_main threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_main' -n "hyper_threading_pr threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 4
  Time (mean ± σ):     11.357 s ±  0.171 s    [User: 40.776 s, System: 2.460 s]
  Range (min … max):   11.236 s … 11.478 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     11.837 s ±  0.056 s    [User: 40.156 s, System: 2.464 s]
  Range (min … max):   11.797 s … 11.876 s    2 runs
 
Summary
  hyper_threading_main threads: 4 ran
    1.04 ± 0.02 times faster than hyper_threading_pr threads: 4




hyperfine -r 2 -n "hyper_threading_main threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_main' -n "hyper_threading_pr threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 6
  Time (mean ± σ):     11.712 s ±  0.043 s    [User: 40.389 s, System: 2.575 s]
  Range (min … max):   11.682 s … 11.742 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     11.540 s ±  0.036 s    [User: 39.959 s, System: 2.543 s]
  Range (min … max):   11.515 s … 11.566 s    2 runs
 
Summary
  hyper_threading_pr threads: 6 ran
    1.01 ± 0.00 times faster than hyper_threading_main threads: 6




hyperfine -r 2 -n "hyper_threading_main threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_main' -n "hyper_threading_pr threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 8
  Time (mean ± σ):     11.504 s ±  0.179 s    [User: 40.890 s, System: 2.714 s]
  Range (min … max):   11.378 s … 11.630 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     11.534 s ±  0.063 s    [User: 41.218 s, System: 2.721 s]
  Range (min … max):   11.489 s … 11.578 s    2 runs
 
Summary
  hyper_threading_main threads: 8 ran
    1.00 ± 0.02 times faster than hyper_threading_pr threads: 8




hyperfine -r 2 -n "hyper_threading_main threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_main' -n "hyper_threading_pr threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 16
  Time (mean ± σ):     11.583 s ±  0.090 s    [User: 41.824 s, System: 2.437 s]
  Range (min … max):   11.520 s … 11.646 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     11.495 s ±  0.007 s    [User: 41.843 s, System: 2.302 s]
  Range (min … max):   11.491 s … 11.500 s    2 runs
 
Summary
  hyper_threading_pr threads: 16 ran
    1.01 ± 0.01 times faster than hyper_threading_main threads: 16


Copy link

github-actions bot commented Sep 24, 2025

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.107 ± 0.019 2.066 2.124 1.01 ± 0.01
head big_factorial 2.095 ± 0.020 2.069 2.142 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.036 ± 0.025 2.011 2.084 1.01 ± 0.01
head big_fibonacci 2.022 ± 0.015 2.001 2.053 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 7.268 ± 0.140 7.083 7.597 1.00 ± 0.02
head blake2s_integration_benchmark 7.238 ± 0.105 7.058 7.370 1.00
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.159 ± 0.029 2.124 2.213 1.01 ± 0.01
head compare_arrays_200000 2.148 ± 0.013 2.131 2.170 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.436 ± 0.007 1.427 1.449 1.01 ± 0.01
head dict_integration_benchmark 1.416 ± 0.017 1.396 1.456 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.202 ± 0.007 1.193 1.218 1.00 ± 0.01
head field_arithmetic_get_square_benchmark 1.196 ± 0.008 1.185 1.210 1.00
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 7.358 ± 0.075 7.207 7.483 1.00 ± 0.02
head integration_builtins 7.340 ± 0.093 7.179 7.485 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 7.465 ± 0.144 7.265 7.769 1.01 ± 0.02
head keccak_integration_benchmark 7.424 ± 0.091 7.286 7.549 1.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.159 ± 0.039 2.135 2.257 1.00
head linear_search 2.166 ± 0.023 2.126 2.208 1.00 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.494 ± 0.013 1.479 1.513 1.00
head math_cmp_and_pow_integration_benchmark 1.506 ± 0.021 1.482 1.553 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.446 ± 0.012 1.432 1.463 1.00 ± 0.01
head math_integration_benchmark 1.439 ± 0.012 1.421 1.453 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.209 ± 0.015 1.193 1.247 1.00 ± 0.01
head memory_integration_benchmark 1.207 ± 0.010 1.191 1.217 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.526 ± 0.007 1.517 1.543 1.00 ± 0.01
head operations_with_data_structures_benchmarks 1.520 ± 0.010 1.509 1.543 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 523.7 ± 1.7 521.3 527.0 1.00
head pedersen 523.8 ± 1.6 520.3 526.0 1.00 ± 0.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 603.1 ± 5.7 595.8 613.3 1.00
head poseidon_integration_benchmark 605.6 ± 5.6 599.8 615.9 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.760 ± 0.009 1.747 1.778 1.00
head secp_integration_benchmark 1.764 ± 0.010 1.749 1.783 1.00 ± 0.01
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 648.3 ± 4.7 643.8 657.5 1.00 ± 0.01
head set_integration_benchmark 645.6 ± 2.4 641.9 650.2 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.121 ± 0.035 4.066 4.156 1.00
head uint256_integration_benchmark 4.122 ± 0.031 4.072 4.168 1.00 ± 0.01

Copy link

codecov bot commented Sep 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.69%. Comparing base (958ae7e) to head (283bd7c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2207      +/-   ##
==========================================
+ Coverage   96.68%   96.69%   +0.01%     
==========================================
  Files         104      104              
  Lines       44027    44023       -4     
==========================================
+ Hits        42566    42567       +1     
+ Misses       1461     1456       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@DiegoCivi DiegoCivi marked this pull request as ready for review September 25, 2025 14:50
Copy link
Contributor

@JulianGCalderon JulianGCalderon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Looks good

Comment on lines +1007 to +1011
fn got_implicit_builtin(param_types: &[ConcreteTypeId], builtin_name: &str) -> bool {
param_types
.iter()
.any(|ty| ty.debug_name.as_ref().is_some_and(|n| n == builtin_name))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use BuiltinName here instead of a &str, just to be safe. You can convert a ty.debug_name with BuiltinName::from(s)

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.

Invalid allocation when using the System implicit
3 participants