Skip to content

Conversation

@eytan-starkware
Copy link
Contributor

@eytan-starkware eytan-starkware commented Nov 4, 2025

What changed?

  • Added constant folding for the felt252_div libfunc with three optimization cases:
    1. Division by 1 returns the original value
    2. Division of 0 by any non-zero value returns 0
    3. Division of two constants is computed at compile time
  • Improved error handling in the test runner to provide better diagnostics when lowering fails
  • Added test cases for all three optimization scenarios

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

eytan-starkware commented Nov 4, 2025

@eytan-starkware eytan-starkware marked this pull request as ready for review November 4, 2025 10:49
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/adding_support_to_felt_add_mul_and_sub_in_constant_propogation branch from 4b9630b to bc86fb3 Compare November 4, 2025 11:25
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_felt252_div_constant_propogation_support branch from 4982bd9 to ad3f92b Compare November 4, 2025 11:25
Copy link
Collaborator

@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.

:lgtm:

@TomerStarkware reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware and @orizi)

Copy link
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.

Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/optimizations/const_folding.rs line 556 at r1 (raw file):

            // There is no need to check if divisor is 0 and throw error, because casting to NonZero
            // will add a panic that will always be thrown. TODO(eytan-starkware): Make
            // sure that NonZero on a const zero leads to a compile error.

It does.
shorten comment.

Suggestion:

            // Note that divisor is never 0, due to NonZero type always being the divisor.

crates/cairo-lang-lowering/src/optimizations/const_folding.rs line 560 at r1 (raw file):

                && rhs.is_one()
            {
                // Check if dividing by 1 (returns the original value)

Suggestion:

                // Returns the original value when dividing by 1.

crates/cairo-lang-lowering/src/optimizations/const_folding.rs line 566 at r1 (raw file):

                && lhs.is_zero()
            {
                // Check if 0 is being divided (returns 0)

Suggestion:

// If the value is 0, result is 0 regardless of the divisor.

crates/cairo-lang-lowering/src/optimizations/const_folding.rs line 569 at r1 (raw file):

                Some(self.propagate_zero_and_get_statement(stmt.outputs[0]))
            } else if let (Some(lhs), Some(rhs)) =
                (self.as_int(stmt.inputs[0].var_id), self.as_int(stmt.inputs[1].var_id))

maybe better now when let-chains works - no need for an extra tuple structuring and destructuring.

Suggestion:

            } else if let Some(lhs) = self.as_int(stmt.inputs[0].var_id)
              && let Some(rhs)) = self.as_int(stmt.inputs[1].var_id)

crates/cairo-lang-lowering/src/optimizations/const_folding.rs line 570 at r1 (raw file):

            } else if let (Some(lhs), Some(rhs)) =
                (self.as_int(stmt.inputs[0].var_id), self.as_int(stmt.inputs[1].var_id))
                && !rhs.is_zero()

Suggestion:

            } else if let (Some(lhs), Some(rhs)) =
                (self.as_int(stmt.inputs[0].var_id), self.as_int(stmt.inputs[1].var_id))

crates/cairo-lang-lowering/src/optimizations/const_folding.rs line 578 at r1 (raw file):

                let rhs_felt = Felt252::from(rhs);
                // For non-zero divisor, use field_div; the libfunc should handle zero checks
                let rhs_nonzero = rhs_felt.try_into().expect("Non-zero divisor");

if you want to do that - you can do it as part of the initial let-chain.
&& let Some(rhs_nonzero) = Felt252::from(rhs).try_into() {

Code quote:

let rhs_nonzero = rhs_felt.try_into().expect("Non-zero divisor");

Copy link
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.

Reviewable status: all files reviewed, 8 unresolved discussions (waiting on @eytan-starkware and @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/optimizations/test_data/const_folding line 6514 at r1 (raw file):


//! > module_code
use core::felt252;

remove all around - already in prelude.

Code quote:

use core::felt252;

crates/cairo-lang-lowering/src/optimizations/test_data/const_folding line 6578 at r1 (raw file):

Statements:
  (v1: core::zeroable::NonZero::<core::felt252>) <- NonZero(1)
  (v2: core::felt252) <- core::felt252_div(v0, v1)

nothing happened.

Code quote:

  (v2: core::felt252) <- core::felt252_div(v0, v1)

@orizi orizi deleted the branch eytan_graphite/adding_support_to_felt_add_mul_and_sub_in_constant_propogation November 5, 2025 14:45
@orizi orizi closed this Nov 5, 2025
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.

5 participants