Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for subtractions contenting references as right hand side operands #1898

Merged
merged 17 commits into from
Jan 3, 2025

Conversation

FrancoGiachetta
Copy link
Contributor

@FrancoGiachetta FrancoGiachetta commented Dec 20, 2024

Add support for subtractions contenting references as right hand side operands

Description

The VM reads let expressions like c - tmp as an addition instead of a subtraction, where c is a constant and tmp is a local variable. This is because currently only additions are supported. This is not an issue with variables declared with the let keyword because those are resolved at compile time.

This PR introduces a little refactor to allow subtractions with references as the rhs operand. It does it by saving the sign with which the reference is defined.

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 Dec 20, 2024

**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 ± σ):     31.650 s ±  0.134 s    [User: 30.869 s, System: 0.780 s]
  Range (min … max):   31.555 s … 31.744 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     31.825 s ±  0.008 s    [User: 31.094 s, System: 0.729 s]
  Range (min … max):   31.819 s … 31.830 s    2 runs
 
Summary
  hyper_threading_main threads: 1 ran
    1.01 ± 0.00 times faster than hyper_threading_pr 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 ± σ):     17.634 s ±  0.039 s    [User: 30.800 s, System: 0.818 s]
  Range (min … max):   17.607 s … 17.661 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     17.715 s ±  0.008 s    [User: 30.963 s, System: 0.790 s]
  Range (min … max):   17.709 s … 17.720 s    2 runs
 
Summary
  hyper_threading_main threads: 2 ran
    1.00 ± 0.00 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 ± σ):     12.770 s ±  0.096 s    [User: 43.259 s, System: 0.922 s]
  Range (min … max):   12.702 s … 12.839 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     12.282 s ±  0.664 s    [User: 43.648 s, System: 0.932 s]
  Range (min … max):   11.812 s … 12.751 s    2 runs
 
Summary
  hyper_threading_pr threads: 4 ran
    1.04 ± 0.06 times faster than hyper_threading_main 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 ± σ):     12.274 s ±  0.163 s    [User: 43.174 s, System: 0.995 s]
  Range (min … max):   12.158 s … 12.389 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     11.801 s ±  0.122 s    [User: 44.241 s, System: 1.055 s]
  Range (min … max):   11.715 s … 11.888 s    2 runs
 
Summary
  hyper_threading_pr threads: 6 ran
    1.04 ± 0.02 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.952 s ±  0.356 s    [User: 44.304 s, System: 1.149 s]
  Range (min … max):   11.700 s … 12.204 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     12.116 s ±  0.101 s    [User: 44.296 s, System: 1.060 s]
  Range (min … max):   12.045 s … 12.188 s    2 runs
 
Summary
  hyper_threading_main threads: 8 ran
    1.01 ± 0.03 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 ± σ):     12.069 s ±  0.214 s    [User: 44.257 s, System: 1.113 s]
  Range (min … max):   11.918 s … 12.220 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     12.133 s ±  0.076 s    [User: 44.219 s, System: 1.101 s]
  Range (min … max):   12.080 s … 12.187 s    2 runs
 
Summary
  hyper_threading_main threads: 16 ran
    1.01 ± 0.02 times faster than hyper_threading_pr threads: 16


Copy link

github-actions bot commented Dec 20, 2024

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.496 ± 0.042 2.453 2.575 1.00 ± 0.02
head big_factorial 2.486 ± 0.035 2.444 2.553 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.457 ± 0.027 2.402 2.503 1.01 ± 0.02
head big_fibonacci 2.445 ± 0.044 2.382 2.537 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 9.240 ± 0.142 9.102 9.597 1.00
head blake2s_integration_benchmark 9.308 ± 0.318 9.004 9.755 1.01 ± 0.04
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.583 ± 0.025 2.555 2.647 1.01 ± 0.01
head compare_arrays_200000 2.554 ± 0.027 2.522 2.599 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.725 ± 0.012 1.704 1.745 1.01 ± 0.03
head dict_integration_benchmark 1.706 ± 0.049 1.669 1.816 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.444 ± 0.018 1.428 1.493 1.01 ± 0.02
head field_arithmetic_get_square_benchmark 1.428 ± 0.025 1.401 1.479 1.00
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 9.285 ± 0.059 9.200 9.358 1.01 ± 0.01
head integration_builtins 9.193 ± 0.077 9.104 9.309 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 9.536 ± 0.132 9.417 9.870 1.00 ± 0.02
head keccak_integration_benchmark 9.492 ± 0.070 9.348 9.562 1.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.611 ± 0.059 2.548 2.751 1.01 ± 0.04
head linear_search 2.595 ± 0.082 2.518 2.756 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.766 ± 0.020 1.745 1.804 1.00 ± 0.03
head math_cmp_and_pow_integration_benchmark 1.759 ± 0.044 1.720 1.861 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.715 ± 0.027 1.689 1.764 1.02 ± 0.03
head math_integration_benchmark 1.686 ± 0.043 1.655 1.801 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.454 ± 0.034 1.419 1.534 1.01 ± 0.04
head memory_integration_benchmark 1.435 ± 0.038 1.406 1.520 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.836 ± 0.026 1.815 1.905 1.01 ± 0.02
head operations_with_data_structures_benchmarks 1.810 ± 0.027 1.781 1.866 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 593.1 ± 5.8 585.7 605.4 1.01 ± 0.02
head pedersen 587.8 ± 7.2 581.1 603.9 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 708.4 ± 7.0 695.6 721.6 1.00 ± 0.02
head poseidon_integration_benchmark 707.5 ± 13.2 691.6 731.3 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 2.089 ± 0.024 2.063 2.124 1.01 ± 0.01
head secp_integration_benchmark 2.060 ± 0.018 2.038 2.099 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 685.5 ± 7.4 677.8 703.8 1.00
head set_integration_benchmark 696.9 ± 3.2 692.7 702.1 1.02 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 5.067 ± 0.020 5.040 5.098 1.01 ± 0.02
head uint256_integration_benchmark 5.010 ± 0.111 4.923 5.241 1.00

Copy link

codecov bot commented Dec 23, 2024

Codecov Report

Attention: Patch coverage is 98.57143% with 2 lines in your changes missing coverage. Please review.

Project coverage is 96.34%. Comparing base (052e7ce) to head (921e633).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
vm/src/hint_processor/hint_processor_utils.rs 94.11% 1 Missing ⚠️
vm/src/serde/deserialize_utils.rs 98.11% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1898   +/-   ##
=======================================
  Coverage   96.33%   96.34%           
=======================================
  Files         102      102           
  Lines       40751    40830   +79     
=======================================
+ Hits        39259    39336   +77     
- Misses       1492     1494    +2     

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

@FrancoGiachetta FrancoGiachetta marked this pull request as ready for review December 23, 2024 17:11
@JulianGCalderon
Copy link
Contributor

I think this is OK as it's a simple and fast fix, but we should create an issue to tackle the underlying problem later on: supporting arbitrary expressions like cairo-lang does. I think we would eventually need to define an AST for the expression.

@gabrielbosio
Copy link
Collaborator

Since the compiler simplifies the expression whenever it can, there would be no problem in let expressions which only use constants or even variables defined with the let keyword. However, when we use variables defined with the local keyword this is not the case anymore because in doing so we are introducing the use of references.

PR looks good but the description could be rephrased, I think it's not clear what's the issue. It can be simplified to something like:

The VM reads let expressions as c - tmp as an addition instead of a subtraction, where c is a constant and tmp is a local variable.

Then it can describe why this happens.
Also:

Thus, only supporting additions would make expressiones like this one: 17 - [fp] to equivalent to 17 + [fp] because the sign of the reference is never taken into account.

Is [fp] interpreted as a local variable?

@FrancoGiachetta
Copy link
Contributor Author

The fp points to the first local declared, fp + 1 to the second and so on. I'll change the description to make it clearer.

CHANGELOG.md Outdated Show resolved Hide resolved
FrancoGiachetta and others added 2 commits January 2, 2025 17:16
Co-authored-by: Gabriel Bosio <38794644+gabrielbosio@users.noreply.github.com>
typo

Co-authored-by: Gabriel Bosio <38794644+gabrielbosio@users.noreply.github.com>
@FrancoGiachetta FrancoGiachetta added this pull request to the merge queue Jan 3, 2025
Merged via the queue into main with commit 87c10b3 Jan 3, 2025
91 checks passed
@FrancoGiachetta FrancoGiachetta deleted the references-substraction branch January 3, 2025 21:27
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.

3 participants