Skip to content

refactor: share comparison-predicate selection across int libfuncs#1627

Open
TomerStarkware wants to merge 1 commit into
mainfrom
tomer/compare_predicate
Open

refactor: share comparison-predicate selection across int libfuncs#1627
TomerStarkware wants to merge 1 commit into
mainfrom
tomer/compare_predicate

Conversation

@TomerStarkware
Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware commented May 26, 2026

Summary

  • Extracts a compare_predicate helper in src/libfuncs.rs that picks
    the signed or unsigned cmpi variant from the operand's stored
    representation (i8..i128 → signed; u* and BoundedInt<L, U>
    unsigned, since bounded ints are biased to start at zero).
  • Routes int_range_try_new, int_range_pop_front, and
    bounded_int_constrain through the new helper, removing three
    near-identical inline ladders.
  • Adds a VM-equivalence integration test over BoundedInt<-10, 10>
    for int_range_try_new, checking 7 input pairs against cairo-vm.

This change is Reviewable

@TomerStarkware TomerStarkware requested a review from orizi May 26, 2026 12:20
Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 6 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on TomerStarkware).


src/libfuncs.rs line 558 at r1 (raw file):

/// biased to start at zero regardless of `L`'s sign — is stored as a
/// non-negative integer and uses the unsigned variant.
fn compare_predicate(

mix is strange - either return CmpiPredicate without getting it as input - or just let the function return a boolean asking if the value is stored as signed or as unsigned.

@TomerStarkware TomerStarkware force-pushed the tomer/compare_predicate branch from 481a3e5 to 22033f3 Compare May 27, 2026 11:24
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 27, 2026

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 8.614 ± 0.067 8.555 8.782 5.69 ± 0.07
cairo-native (embedded AOT) 1.513 ± 0.014 1.498 1.538 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.543 ± 0.039 1.503 1.639 1.02 ± 0.03

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 431.7 ± 2.9 427.8 439.1 1.00
cairo-native (embedded AOT) 1335.8 ± 12.4 1320.1 1362.7 3.09 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 1364.3 ± 32.2 1344.0 1452.2 3.16 ± 0.08

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 3.929 ± 0.063 3.860 4.054 2.23 ± 0.05
cairo-native (embedded AOT) 1.775 ± 0.020 1.749 1.808 1.01 ± 0.02
cairo-native (embedded JIT using LLVM's ORC Engine) 1.763 ± 0.029 1.733 1.823 1.00

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 3.836 ± 0.085 3.781 4.070 2.84 ± 0.07
cairo-native (embedded AOT) 1.353 ± 0.015 1.337 1.389 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.390 ± 0.024 1.346 1.426 1.03 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 473.0 ± 6.6 465.1 485.3 1.00
cairo-native (embedded AOT) 1370.4 ± 18.2 1348.7 1396.4 2.90 ± 0.06
cairo-native (embedded JIT using LLVM's ORC Engine) 1401.9 ± 23.2 1376.1 1432.7 2.96 ± 0.06

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 414.4 ± 4.7 404.6 419.8 1.00
cairo-native (embedded AOT) 1532.5 ± 29.1 1499.4 1598.5 3.70 ± 0.08
cairo-native (embedded JIT using LLVM's ORC Engine) 1650.7 ± 18.1 1629.7 1687.3 3.98 ± 0.06

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 27, 2026

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.860 ± 0.009 1.847 1.878 1.01 ± 0.01
base dict_insert.cairo (AOT) 1.844 ± 0.010 1.829 1.862 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.861 ± 0.028 1.845 1.939 1.01 ± 0.02
head dict_insert.cairo (AOT) 1.849 ± 0.017 1.823 1.874 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.688 ± 0.013 1.670 1.713 1.02 ± 0.01
base dict_snapshot.cairo (AOT) 1.655 ± 0.013 1.631 1.678 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.687 ± 0.008 1.677 1.705 1.02 ± 0.01
head dict_snapshot.cairo (AOT) 1.656 ± 0.011 1.643 1.672 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.170 ± 0.013 2.146 2.193 1.01 ± 0.01
base factorial_2M.cairo (AOT) 2.155 ± 0.012 2.142 2.176 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.171 ± 0.012 2.153 2.188 1.01 ± 0.01
head factorial_2M.cairo (AOT) 2.156 ± 0.010 2.144 2.173 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.690 ± 0.004 1.685 1.696 1.01 ± 0.01
base fib_2M.cairo (AOT) 1.666 ± 0.011 1.646 1.679 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.708 ± 0.017 1.687 1.744 1.02 ± 0.01
head fib_2M.cairo (AOT) 1.674 ± 0.011 1.657 1.693 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.718 ± 0.015 1.702 1.752 1.02 ± 0.01
base linear_search.cairo (AOT) 1.687 ± 0.007 1.677 1.701 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.719 ± 0.009 1.702 1.732 1.02 ± 0.01
head linear_search.cairo (AOT) 1.693 ± 0.014 1.669 1.724 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.949 ± 0.009 1.935 1.961 1.06 ± 0.01
base logistic_map.cairo (AOT) 1.843 ± 0.009 1.826 1.854 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.989 ± 0.018 1.960 2.020 1.08 ± 0.01
head logistic_map.cairo (AOT) 1.847 ± 0.009 1.836 1.861 1.00

@TomerStarkware TomerStarkware force-pushed the tomer/compare_predicate branch from 22033f3 to da05c0c Compare May 28, 2026 07:43
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

✅ Code is now correctly formatted.

@TomerStarkware TomerStarkware force-pushed the tomer/compare_predicate branch 2 times, most recently from aef451b to 2db1a69 Compare May 28, 2026 12:10
Copy link
Copy Markdown
Collaborator Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

@TomerStarkware made 1 comment.
Reviewable status: 3 of 6 files reviewed, 1 unresolved discussion (waiting on orizi).


src/libfuncs.rs line 558 at r1 (raw file):

Previously, orizi wrote…

mix is strange - either return CmpiPredicate without getting it as input - or just let the function return a boolean asking if the value is stored as signed or as unsigned.

Done.

Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

:lgtm:

@orizi reviewed 4 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

`int_range_try_new`, `int_range_pop_front`, and `bounded_int_constrain`
each picked between signed/unsigned cmpi predicates with near-identical
inline logic. Lift it into a `compare_predicate` helper on
`src/libfuncs.rs` that takes the operand's `CoreTypeConcrete`, the
registry, and the (signed, unsigned) predicate pair, and routes to
whichever variant matches the stored representation: `i8..i128` is
two's complement, while `u*` and `BoundedInt<L, U>` are stored as
non-negative integers (the latter biased to start at zero regardless
of `L`'s sign).

Adds a VM-equivalence test (`int_range_try_new_bounded_int_negative_lower_vm_equivalence`)
over `BoundedInt<-10, 10>` covering 7 input pairs around the 5-bit MSB
threshold.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@TomerStarkware TomerStarkware force-pushed the tomer/compare_predicate branch from 2db1a69 to ac4fb91 Compare May 28, 2026 14:32
Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: 5 of 6 files reviewed, 1 unresolved discussion (waiting on TomerStarkware).


src/libfuncs/int_range.rs line 113 at r4 (raw file):

    let x = entry.extract_value(context, location, range, inner_ty, 0)?;
    let k1 = entry.const_int_from_type(context, location, 1, inner_ty)?;
    let x_p_1 = entry.addi(x, k1, location)?;

delay addition and use Sle,Ule.

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.

2 participants