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 logical operators. #3323

Merged
merged 1 commit into from
Jun 8, 2023
Merged

Conversation

ilyalesokhin-starkware
Copy link
Contributor

@ilyalesokhin-starkware ilyalesokhin-starkware commented Jun 7, 2023

This change is Reviewable

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 3 of 10 files at r1, all commit messages.
Reviewable status: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)


crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r1 (raw file):

        semantic::Expr::Desnap(expr) => lower_expr_desnap(ctx, expr, builder),
        semantic::Expr::Assignment(expr) => lower_expr_assignment(ctx, expr, builder),
        semantic::Expr::BinaryOperator(expr) => lower_binary_op(ctx, builder, expr),

Isn't it kinda wierd that binary op is only && and ||?


crates/cairo-lang-semantic/src/expr/compute.rs line 379 at r1 (raw file):

            let bool_ty = core_bool_ty(db);
            if lexpr.expr.ty() != bool_ty {

If we always expect bool, conform_ty instead (look at if lowering)


crates/cairo-lang-semantic/src/expr/compute.rs line 386 at r1 (raw file):

            }

            if rexpr.expr.ty() != bool_ty {

Here as well

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r1 (raw file):

Previously, orizi wrote…

Isn't it kinda wierd that binary op is only && and ||?

I can rename it, but I think (and shachar agreed) that long term we want to use it for all binary ops.
In the current implementation, the semantic module does not know if the original code had a binary op or a function call.

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

I can rename it, but I think (and shachar agreed) that long term we want to use it for all binary ops.
In the current implementation, the semantic module does not know if the original code had a binary op or a function call.

Though I'm not sure how it will work with the inference in the semantic.

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-starkware 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: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

Though I'm not sure how it will work with the inference in the semantic.

Do you want me to rename it to LogicalOperator?

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: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)


crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

Do you want me to rename it to LogicalOperator?

i think we should rename - we might want to add the Call function some information about this being an operator for better diagnostics - but && and || are very different from other operators IMO (as these actually control the flow of the run)

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-semantic/src/expr/compute.rs line 379 at r1 (raw file):

Previously, orizi wrote…

If we always expect bool, conform_ty instead (look at if lowering)

Done, thanks.
Not sure how to test it though.

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-starkware 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: 3 of 10 files reviewed, 3 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r1 (raw file):

Previously, orizi wrote…

i think we should rename - we might want to add the Call function some information about this being an operator for better diagnostics - but && and || are very different from other operators IMO (as these actually control the flow of the run)

Done.


crates/cairo-lang-semantic/src/expr/compute.rs line 386 at r1 (raw file):

Previously, orizi wrote…

Here as well

Done.

@ilyalesokhin-starkware ilyalesokhin-starkware force-pushed the ilya/lazy_bool2 branch 2 times, most recently from 17e1c0c to fb40b13 Compare June 8, 2023 06:17
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 10 files at r1, 1 of 2 files at r2, 1 of 7 files at r3, 1 of 5 files at r4.
Reviewable status: 5 of 11 files reviewed, 6 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)


crates/cairo-lang-semantic/src/expr/objects.rs line 356 at r4 (raw file):

    pub op: LogicalOperator,
    pub rhs: semantic::ExprId,
    pub ty: semantic::TypeId,

Suggestion:

// ExprLogicalOperator is always of bool type.
pub ty: semantic::TypeId,

crates/cairo-lang-semantic/src/expr/test_data/logical_operator line 41 at r2 (raw file):

//! > function
fn foo(a: u128, b: bool, c: u128) -> bool {
    bar(a.into(), b, c.into());

why the intos?

Code quote:

   bar(a.into(), b, c.into());

crates/cairo-lang-lowering/src/diagnostic.rs line 122 at r2 (raw file):

    UnsupportedMatchArmNotAVariant,
    UnsupportedMatchArmOutOfOrder,
    LogicalOperatorsNotSupported,

you add it but not use it.

Code quote:

    LogicalOperatorsNotSupported,

crates/cairo-lang-lowering/src/test_data/lazy_bool_expr line 24 at r3 (raw file):

 --> lib.cairo:2:10
    if a && (x == 0) {
         ^^

why?

Code quote:

error: Unknown binary operator.
 --> lib.cairo:2:10
    if a && (x == 0) {
         ^^

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/test_data/lazy_bool_expr line 24 at r3 (raw file):

Previously, orizi wrote…

why?

sorry this file is not relevent.

Copy link
Contributor Author

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


crates/cairo-lang-lowering/src/diagnostic.rs line 122 at r2 (raw file):

Previously, orizi wrote…

you add it but not use it.

Done.

Copy link
Contributor Author

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


crates/cairo-lang-semantic/src/expr/test_data/logical_operator line 41 at r2 (raw file):

Previously, orizi wrote…

why the intos?

Done.

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 10 files at r1, 1 of 2 files at r2, 2 of 7 files at r3, all commit messages.
Reviewable status: 5 of 11 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)


crates/cairo-lang-lowering/src/lower/binary_op.rs line 138 at r2 (raw file):

        semantic::BinaryOperator::OrOr => lower_or_or(ctx, builder, location, expr.lhs, expr.rhs),
    }
}

or something similar?
this seems very duplicated

Suggestion:

/// Lowers an expression of type [semantic::ExprBinaryOperator].
pub fn lower_binary_op(
    ctx: &mut LoweringContext<'_, '_>,
    builder: &mut BlockBuilder,
    expr: &semantic::ExprBinaryOperator,
) -> LoweringResult<LoweredExpr> {
    let location = ctx.get_location(expr.stable_ptr.untyped());    let semantic_db = ctx.db.upcast();
    let unit_ty = corelib::unit_ty(semantic_db);
    let lhs_var = lower_expr(ctx, builder, lhs)?.var(ctx, builder)?;

    let mut subscope_lhs_true = create_subscope_with_bound_refs(ctx, builder);
    let lhs_true_block_id = subscope_lhs_true.block_id;

    let rhs_var = lower_expr(ctx, &mut subscope_lhs_true, rhs)?.var(ctx, &mut subscope_lhs_true)?;
    let mut subscope_lhs_true = create_subscope_with_bound_refs(ctx, builder);
    let lhs_true_block_id = subscope_lhs_true.block_id;

    let (sealed_block_lhs_true, lhs_false_block_id, sealed_block_lhs_false) = match expr.op {
        // Lowers `lhs && rhs` to `if lhs { rhs } else { false }`.
        semantic::BinaryOperator::AndAnd => {
            let rhs_var = lower_expr(ctx, &mut subscope_lhs_true, rhs)?.var(ctx, &mut subscope_lhs_true)?;
        
            let sealed_block_lhs_true = subscope_lhs_true.goto_callsite(Some(rhs_var));
            let mut subscope_lhs_false = create_subscope_with_bound_refs(ctx, builder);
            let lhs_false_block_id = subscope_lhs_false.block_id;
            let false_var =
                create_bool(ctx, &mut subscope_lhs_false, corelib::false_variant(semantic_db), location);
            let sealed_block_lhs_false = subscope_lhs_false.goto_callsite(Some(false_var));
            (sealed_block_lhs_true, lhs_false_block_id, sealed_block_lhs_false)
        }
        // Lowers `lhs || rhs` to `if lhs { true } else { rhs }`.
        semantic::BinaryOperator::OrOr => {
            let true_var =
                create_bool(ctx, &mut subscope_lhs_true, corelib::true_variant(semantic_db), location);
        
            let sealed_block_lhs_true = subscope_lhs_true.goto_callsite(Some(true_var));
            let mut subscope_lhs_false = create_subscope_with_bound_refs(ctx, builder);
            let lhs_false_block_id = subscope_lhs_false.block_id;
            let rhs_var =
                lower_expr(ctx, &mut subscope_lhs_false, rhs)?.var(ctx, &mut subscope_lhs_false)?;
        
            let sealed_block_lhs_false = subscope_lhs_false.goto_callsite(Some(rhs_var));
            (sealed_block_lhs_true, lhs_false_block_id, sealed_block_lhs_false)or 
        },
    }

    let match_info = MatchInfo::Enum(MatchEnumInfo {
        concrete_enum_id: corelib::core_bool_enum(semantic_db),
        input: lhs_var,
        arms: vec![
            MatchArm {
                variant_id: corelib::false_variant(semantic_db),
                block_id: lhs_true_block_id,
                var_ids: vec![ctx.new_var(VarRequest { ty: unit_ty, location })],
            },
            MatchArm {
                variant_id: corelib::true_variant(semantic_db),
                block_id: lhs_false_block_id,
                var_ids: vec![ctx.new_var(VarRequest { ty: unit_ty, location })],
            },
        ],
    });
    builder.merge_and_end_with_match(
        ctx,
        match_info,
        vec![sealed_block_lhs_true, sealed_block_lhs_false],
        location,
    )
}

crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r5 (raw file):

        semantic::Expr::Desnap(expr) => lower_expr_desnap(ctx, expr, builder),
        semantic::Expr::Assignment(expr) => lower_expr_assignment(ctx, expr, builder),
        semantic::Expr::LogicalOperator(expr) => lower_binary_op(ctx, builder, expr),

Suggestion:

ower_logical_op

crates/cairo-lang-semantic/src/expr/compute.rs line 379 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

Done, thanks.
Not sure how to test it though.

this already tests if it is the correct type for now - so sgtm.

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 2 files at r5.
Reviewable status: 6 of 11 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-semantic/src/expr/test_data/logical_operator line 56 at r5 (raw file):

 --> lib.cairo:2:5
    a && b || c
    ^

Is this supposed to work?

Code quote:

error: Expected type "core::bool", found: "S".
 --> lib.cairo:2:5
    a && b || c
    ^

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 7 files at r3, 2 of 5 files at r4, 2 of 3 files at r6, all commit messages.
Reviewable status: 9 of 11 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @spapinistarkware)

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-starkware 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, 3 unresolved discussions (waiting on @orizi and @spapinistarkware)


crates/cairo-lang-lowering/src/lower/binary_op.rs line 138 at r2 (raw file):

Previously, orizi wrote…

or something similar?
this seems very duplicated

Done.


crates/cairo-lang-lowering/src/lower/mod.rs line 455 at r5 (raw file):

        semantic::Expr::Desnap(expr) => lower_expr_desnap(ctx, expr, builder),
        semantic::Expr::Assignment(expr) => lower_expr_assignment(ctx, expr, builder),
        semantic::Expr::LogicalOperator(expr) => lower_binary_op(ctx, builder, expr),

Done.


crates/cairo-lang-semantic/src/expr/compute.rs line 379 at r1 (raw file):

Previously, orizi wrote…

this already tests if it is the correct type for now - so sgtm.

Done.


crates/cairo-lang-semantic/src/expr/objects.rs line 356 at r4 (raw file):

    pub op: LogicalOperator,
    pub rhs: semantic::ExprId,
    pub ty: semantic::TypeId,

Done.

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 1 of 3 files at r6, 2 of 2 files at r7, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @spapinistarkware)

@ilyalesokhin-starkware ilyalesokhin-starkware added this pull request to the merge queue Jun 8, 2023
Merged via the queue into main with commit 9276bd5 Jun 8, 2023
@orizi orizi deleted the ilya/lazy_bool2 branch June 28, 2023 05:24
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