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

Introduce a consteval_int macro to compute integers at compile-time #3132

Merged
merged 5 commits into from
Jun 13, 2023

Conversation

wraitii
Copy link
Contributor

@wraitii wraitii commented May 12, 2023

Fixes #3130

This introduces a Cairo plugin that converts constant expressions in constant into their actual value, so that we can write code such as the below without requiring further compiler changes. I think it's sufficient to do this in constants.

const some_value = consteval_int!((256 * 1080) & 0xfaff + 1234); // should become 14336
const some_other_value: felt252 = consteval_int!(256 - 1); // should become 255

I have a separate diff on a 'pow operator' (2**128) that I can push next if that is wanted.


This change is Reviewable

@wraitii wraitii force-pushed the feat/arithmetic_plugin branch from a2174eb to a240473 Compare May 16, 2023 11:49
@wraitii wraitii force-pushed the feat/arithmetic_plugin branch from a240473 to 1551c72 Compare May 24, 2023 09:18
Copy link
Contributor

@spapinistarkware spapinistarkware 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: 0 of 7 files reviewed, 1 unresolved discussion (waiting on @wraitii)


tests/bug_samples/issue2964.cairo line 23 at r1 (raw file):

    // This assumes that Drop implies Destruct and Copy implies Clone
    let mut a = GenericStruct { x: SimpleStruct { x: 1, y: 2 }, y: SimpleStruct { x: 1, y: 2 } };
    a.x.x = 24;

What is this change?

Copy link
Contributor

@spapinistarkware spapinistarkware left a comment

Choose a reason for hiding this comment

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

This alters the behaviour of the code. for example 255_u8+1-1 will work with your plugin, but fail without.
I'd rather this be explicit with an inline macro, or at the very least an annotation above the const item, that explains this does operations in the integers realm.

Reviewable status: 0 of 7 files reviewed, 3 unresolved discussions (waiting on @wraitii)


crates/cairo-lang-plugins/src/plugins/arithmetic.rs line 73 at r1 (raw file):

                Some(rec_compute(db, &bin_expr.lhs(db))? - rec_compute(db, &bin_expr.rhs(db))?)
            }
            ast::BinaryOperator::Div(_) => {

This might be wrong. For example, felt division is incorrect here. Are u sure u wanna support div?


crates/cairo-lang-plugins/src/plugins/arithmetic.rs line 76 at r1 (raw file):

                Some(rec_compute(db, &bin_expr.lhs(db))? / rec_compute(db, &bin_expr.rhs(db))?)
            }
            ast::BinaryOperator::Mod(_) => {

Same as for Div. Felts won't like this.

@wraitii wraitii force-pushed the feat/arithmetic_plugin branch from 1551c72 to 3751e87 Compare June 7, 2023 15:30
@wraitii wraitii changed the title Arithmetics in constants Introduce a consteval_int macro to compute integers at compile-time Jun 7, 2023
@wraitii wraitii force-pushed the feat/arithmetic_plugin branch from 3751e87 to cd89bc0 Compare June 8, 2023 12:11
Copy link
Contributor

@spapinistarkware spapinistarkware left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 7 files at r1, 9 of 9 files at r2, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @wraitii)

Copy link
Contributor

@spapinistarkware spapinistarkware left a comment

Choose a reason for hiding this comment

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

Looks good to me. Let's wait for Orizi to also take a look.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @wraitii)

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.

Reviewed 2 of 9 files at r2, all commit messages.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @wraitii)


crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 34 at r2 (raw file):

}

fn handle_constant(db: &dyn SyntaxGroup, constant_ast: &ast::ItemConstant) -> PluginResult {

doc


crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 68 at r2 (raw file):

}

fn extract_consteval_macro_expression(

doc


crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 93 at r2 (raw file):

}

fn rec_compute(

doc
and rename to 'compute_constant_expr'

@wraitii wraitii force-pushed the feat/arithmetic_plugin branch from cd89bc0 to 6748e25 Compare June 12, 2023 12:40
@wraitii
Copy link
Contributor Author

wraitii commented Jun 12, 2023

Done all inlines.

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.

Reviewed 4 of 4 files at r3, all commit messages.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @spapinistarkware and @wraitii)


crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 36 at r3 (raw file):

// Rewrite a constant declaration that contains a consteval_int macro
// into a constant declaration with the computed value,
// e.g. `const a: felt252 = consteval_int!(2 * 2 * 2);` into `const a: felt252 = 8;`.

Suggestion:

/// Rewrite a constant declaration that contains a consteval_int macro
/// into a constant declaration with the computed value,
/// e.g. `const a: felt252 = consteval_int!(2 * 2 * 2);` into `const a: felt252 = 8;`.

crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 71 at r3 (raw file):

}

// Extract the actual expression from the consteval_int macro, or fail with diagnostics.

Suggestion:

/// Extract the actual expression from the consteval_int macro, or fail with diagnostics.

crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 98 at r3 (raw file):

// Compute the actual value of an integer expression, or fail with diagnostics.
// This computation handles arbitrary integers, unlike regular Cairo math.

Suggestion:

/// Compute the actual value of an integer expression, or fail with diagnostics.
/// This computation handles arbitrary integers, unlike regular Cairo math.

tests/bug_samples/issue3130.cairo line 7 at r3 (raw file):

#[test]
fn main() {}

add to bug_samples/lib.cairo

Code quote:

const a: felt252 = (4 + 2 * 3) * 256;
const b: felt252 = 0xff & (24 + 5 * 2);
const c: felt252 = -0xff & (24 + 5 * 2);
const d: felt252 = 0xff | (24 + 5 * 2);

#[test]
fn main() {}

@wraitii wraitii requested a review from orizi June 12, 2023 13:56
Copy link
Contributor Author

@wraitii wraitii 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: 8 of 11 files reviewed, 6 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 36 at r3 (raw file):

// Rewrite a constant declaration that contains a consteval_int macro
// into a constant declaration with the computed value,
// e.g. `const a: felt252 = consteval_int!(2 * 2 * 2);` into `const a: felt252 = 8;`.

Done.


crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 71 at r3 (raw file):

}

// Extract the actual expression from the consteval_int macro, or fail with diagnostics.

Done.


crates/cairo-lang-plugins/src/plugins/consteval_int.rs line 98 at r3 (raw file):

// Compute the actual value of an integer expression, or fail with diagnostics.
// This computation handles arbitrary integers, unlike regular Cairo math.

Done.


tests/bug_samples/issue3130.cairo line 7 at r3 (raw file):

Previously, orizi wrote…

add to bug_samples/lib.cairo

Done, and fixed

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.

:lgtm:

Reviewed 3 of 3 files at r4, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @spapinistarkware and @wraitii)

Copy link
Contributor

@spapinistarkware spapinistarkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 3 of 4 files at r3, 3 of 3 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @wraitii)

auto-merge was automatically disabled June 12, 2023 15:40

Head branch was pushed to by a user without write access

@wraitii wraitii force-pushed the feat/arithmetic_plugin branch 2 times, most recently from a9fffbc to c10f79a Compare June 12, 2023 15:58
@wraitii
Copy link
Contributor Author

wraitii commented Jun 12, 2023

@orizi @spapinistarkware Cairo_fmt wants to add a space before the 'consteval_int', but that suggestion actually crashes the Cairo compiler.

I'm not entirely sure how to fix this, it's not obvious to me where this space gets added in the formatter.

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.

We probably need to fix something in the formatter. (As this is the first macro usage)
Will fix tomorrow and ping here.

Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @wraitii)

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.

can you check again?

Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @wraitii)

@wraitii wraitii force-pushed the feat/arithmetic_plugin branch from c10f79a to 787c9b1 Compare June 13, 2023 13:04
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.

Reviewed 1 of 9 files at r2, 1 of 1 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @wraitii)

@orizi orizi added this pull request to the merge queue Jun 13, 2023
Merged via the queue into starkware-libs:main with commit 8e4e319 Jun 13, 2023
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.

feat: compile-time arithmetic for constants
3 participants