Skip to content

Conversation

@orizi
Copy link
Collaborator

@orizi orizi commented Feb 3, 2026

Summary

Improved the implementation of enum_boxed_match libfunc to handle single-variant enums more efficiently. This PR fixes the code generation for boxed enum matching, particularly for the case of enums with a single variant where no branching is needed.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The current implementation of enum_boxed_match was not correctly handling single-variant enums. It was generating unnecessary code for the single-variant case, which should be a simple pass-through operation without branching logic.


What was the behavior or documentation before?

Previously, the enum_boxed_match libfunc was generating unnecessary code for single-variant enums, treating them similarly to multi-variant enums which require branching logic.


What is the behavior or documentation after?

Now, for single-variant enums, the enum_boxed_match libfunc generates simpler, more efficient code that directly returns the variant box without any branching. The implementation also includes better handling of offsets and cleaner code for the multi-variant cases.


Additional context

Added several test cases to verify the correct behavior for single-variant and multi-variant boxed enum matching. The changes also include some refactoring to improve code clarity and maintainability, particularly in the utility functions for type handling.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator Author

orizi commented Feb 3, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi orizi marked this pull request as ready for review February 3, 2026 16:56
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6e9942c64c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@orizi orizi force-pushed the orizi/02-03-updating_enum_boxed_match_according_to_audit branch from 6e9942c to 46e06c6 Compare February 3, 2026 18:46
Copy link
Collaborator Author

@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 made 1 comment.
Reviewable status: 0 of 7 files reviewed, 1 unresolved discussion (waiting on @ilyalesokhin-starkware and @liorgold2).

Copy link
Collaborator Author

@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 resolved 1 discussion.
Reviewable status: 0 of 7 files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware and @liorgold2).

@orizi orizi force-pushed the orizi/02-03-updating_enum_boxed_match_according_to_audit branch from 46e06c6 to 40ff896 Compare February 4, 2026 12:50
@ilyalesokhin-starkware
Copy link
Contributor

crates/cairo-lang-sierra-to-casm/src/invocations/enm.rs line 471 at r3 (raw file):

    }];

    assert!(boxed_enum_ptr.apply_known_ap_change(1));

Suggestion:

// Fix boxed_enum_ptr to account for the `inc_ap` above.
assert!(boxed_enum_ptr.apply_known_ap_change(1));

@ilyalesokhin-starkware
Copy link
Contributor

tests/e2e_test_data/libfuncs/enum line 770 at r3 (raw file):

}

extern fn enum_boxed_match<T>(e: Box<T>) -> BoxedSingle nopanic;

this also works, right?

Suggestion:

Box<felt252>

Copy link
Contributor

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

@ilyalesokhin-starkware reviewed 8 files and all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @liorgold2 and @orizi).

@orizi orizi force-pushed the orizi/02-03-updating_enum_boxed_match_according_to_audit branch from 40ff896 to d961458 Compare February 4, 2026 15:43
Copy link
Collaborator Author

@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 made 2 comments.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware and @liorgold2).


tests/e2e_test_data/libfuncs/enum line 770 at r3 (raw file):

Previously, ilyalesokhin-starkware wrote…

this also works, right?

yes it does.


crates/cairo-lang-sierra-to-casm/src/invocations/enm.rs line 471 at r3 (raw file):

    }];

    assert!(boxed_enum_ptr.apply_known_ap_change(1));

Done.

@orizi orizi enabled auto-merge February 4, 2026 15:48
@orizi orizi disabled auto-merge February 4, 2026 15:48
@ilyalesokhin-starkware
Copy link
Contributor

tests/e2e_test_data/libfuncs/enum line 770 at r3 (raw file):

Previously, orizi wrote…

yes it does.

Lior said he prefers that syntax.

@orizi orizi force-pushed the orizi/02-03-updating_enum_boxed_match_according_to_audit branch 2 times, most recently from 728d02f to ec182e0 Compare February 4, 2026 16:10
Copy link
Collaborator Author

@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 made 1 comment.
Reviewable status: 7 of 8 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware and @liorgold2).


tests/e2e_test_data/libfuncs/enum line 770 at r3 (raw file):

Previously, ilyalesokhin-starkware wrote…

Lior said he prefers that syntax.

Done.

@ilyalesokhin-starkware
Copy link
Contributor

tests/e2e_test_data/libfuncs/enum line 862 at r1 (raw file):

branch_align() -> ();
enum_boxed_match<test::A>([1]) -> ([2]);
branch_align() -> ();

Why did the branch_align disappear?

Code quote:

branch_align() -> ();

Copy link
Collaborator Author

@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 made 1 comment.
Reviewable status: 6 of 8 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @liorgold2).


tests/e2e_test_data/libfuncs/enum line 862 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

Why did the branch_align disappear?

lior's suggested change made this function not be considered as a branch on sierra-gen - so no branch-align was caused.

in general we may want to consider this for "single-branch-matches" but not for this PR.

@orizi orizi force-pushed the orizi/02-03-updating_enum_boxed_match_according_to_audit branch from ec182e0 to 94fce36 Compare February 9, 2026 07:18
Copy link
Collaborator Author

@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 made 1 comment.
Reviewable status: 6 of 8 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @liorgold2).


tests/e2e_test_data/libfuncs/enum line 862 at r1 (raw file):

Previously, orizi wrote…

lior's suggested change made this function not be considered as a branch on sierra-gen - so no branch-align was caused.

in general we may want to consider this for "single-branch-matches" but not for this PR.

fixed underlying issue in another PR.

@orizi orizi force-pushed the orizi/02-03-updating_enum_boxed_match_according_to_audit branch from 94fce36 to ef10664 Compare February 10, 2026 09:14
Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

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

@liorgold2 reviewed 3 files and all commit messages, and made 1 comment.
Reviewable status: 5 of 8 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi).


tests/e2e_test_data/libfuncs/enum line 812 at r8 (raw file):

enum B {
    Value1: A,
    Value2: A,

Actually, let's have the second item larger, which will put the inner selectors in different locations due to padding. E.g.

enum B {
Value1: A,
Value2: (A, felt252, felt252)

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

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

@liorgold2 reviewed 3 files and made 3 comments.
Reviewable status: 6 of 8 files reviewed, 7 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi).


crates/cairo-lang-sierra-gas/src/core_libfunc_cost_base.rs line 412 at r8 (raw file):

                // plus the regular match cost - but only when branching is actually needed.
                let n = signature.branch_signatures.len();
                match n {

Suggestion:

                match signature.branch_signatures.len() {

crates/cairo-lang-sierra/src/extensions/modules/enm.rs line 397 at r8 (raw file):

    ) -> Result<(Vec<ConcreteTypeId>, bool), SpecializationError> {
        let type_info = context.get_type_info(ty)?;
        let (inner_ty, is_snapshot) = peel_snapshot(ty, type_info)?;

There was a question whether it's ok to peel at most one snapshot. What happens with Box<@@T>?


crates/cairo-lang-sierra/src/extensions/modules/enm.rs line 461 at r8 (raw file):

            context,
            enum_type.clone(),
            variants.iter().cloned(),

Is this efficient? Does it create an interator that clones all items when necessary?

Code quote:

         variants.iter().cloned(),

SIERRA_UPDATE_PATCH_CHANGE_TAG=Updating not yet allowed function.
@orizi orizi force-pushed the orizi/02-03-updating_enum_boxed_match_according_to_audit branch from ef10664 to a64c3d3 Compare February 10, 2026 12:52
Copy link
Collaborator Author

@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 made 4 comments.
Reviewable status: 6 of 8 files reviewed, 7 unresolved discussions (waiting on @ilyalesokhin-starkware and @liorgold2).


crates/cairo-lang-sierra/src/extensions/modules/enm.rs line 397 at r8 (raw file):

Previously, liorgold2 wrote…

There was a question whether it's ok to peel at most one snapshot. What happens with Box<@@T>?

there's no such thing at the sierra level - there's either one or none.


crates/cairo-lang-sierra/src/extensions/modules/enm.rs line 461 at r8 (raw file):

Previously, liorgold2 wrote…

Is this efficient? Does it create an interator that clones all items when necessary?

it only clones on "next" calls - no cloning of the vector is performed.


tests/e2e_test_data/libfuncs/enum line 812 at r8 (raw file):

Previously, liorgold2 wrote…

Actually, let's have the second item larger, which will put the inner selectors in different locations due to padding. E.g.

enum B {
Value1: A,
Value2: (A, felt252, felt252)

Done.


crates/cairo-lang-sierra-gas/src/core_libfunc_cost_base.rs line 412 at r8 (raw file):

                // plus the regular match cost - but only when branching is actually needed.
                let n = signature.branch_signatures.len();
                match n {

Done.

Copy link
Contributor

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

@ilyalesokhin-starkware partially reviewed 3 files and resolved 3 discussions.
Reviewable status: 7 of 8 files reviewed, 4 unresolved discussions (waiting on @liorgold2).

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

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

@liorgold2 partially reviewed 6 files and all commit messages, made 1 comment, and resolved 4 discussions.
Reviewable status: 7 of 8 files reviewed, 1 unresolved discussion (waiting on @orizi).


crates/cairo-lang-sierra-to-casm/src/invocations/enm.rs line 444 at r9 (raw file):

    if num_branches == 1 {
        // The box should point to: base_addr + 1 (selector).
        let variant_box = CellExpression::add_with_const(boxed_enum_ptr, orig_offset + 1);

Consider checked\_add or explicitly checking orig_offset < ... before.

Code quote:

orig_offset + 1

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

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

@liorgold2 reviewed 3 files.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @orizi).

Copy link
Collaborator

@liorgold2 liorgold2 left a comment

Choose a reason for hiding this comment

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

:lgtm: (see comment below)

@liorgold2 made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @orizi).

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.

4 participants