Skip to content

Conversation

@eytan-starkware
Copy link
Contributor

@eytan-starkware eytan-starkware commented Dec 31, 2025

Summary

Added support for enum_boxed_match libfunc that allows matching on boxed enums, returning boxes of the variant types. This complements the existing struct_boxed_deconstruct functionality and enables more efficient handling of boxed enum types.


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?

When working with boxed enums, there was no direct way to match on the enum and get boxed variants without unboxing and reboxing. This implementation allows for more efficient handling of boxed enum types by providing a direct way to match on a boxed enum and get boxes of the variant types.


What was the behavior or documentation before?

Previously, to match on a boxed enum and work with its variants as boxes, you would need to unbox the enum, match on it, and then box each variant again. This was inefficient and required more code.


What is the behavior or documentation after?

Now, the enum_boxed_match libfunc allows directly matching on a boxed enum and getting boxes of the variant types. This is similar to how struct_boxed_deconstruct works for structs. The implementation handles different variant sizes correctly, including padding for right-aligned variants.


Additional context

The implementation includes refactoring of the box unpacking functionality to reduce code duplication between struct and enum implementations. The BoxUnpackLibfunc trait was introduced to share common logic between struct_boxed_deconstruct and enum_boxed_match.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

eytan-starkware commented Dec 31, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

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

@eytan-starkware eytan-starkware marked this pull request as ready for review December 31, 2025 10:05
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from c5d3e57 to 27ca24b Compare December 31, 2025 10:08
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 9 files and all commit messages, and made 1 comment.
Reviewable status: 9 of 10 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.

@orizi reviewed all commit messages and made 1 comment.
Reviewable status: 9 of 10 files reviewed, 1 unresolved discussion (waiting on @eytan-starkware and @ilyalesokhin-starkware).


crates/cairo-lang-sierra-ap-change/src/core_libfunc_ap_change.rs line 301 at r1 (raw file):

                signature, ..
            })
            | EnumConcreteLibfunc::BoxedMatch(EnumBoxedMatchConcreteLibfunc {

this sounds incorrect - as box match should include a deref before the match.

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.

@orizi made 1 comment.
Reviewable status: 9 of 10 files reviewed, 2 unresolved discussions (waiting on @eytan-starkware and @ilyalesokhin-starkware).


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


//! > casm
jmp rel 7 if [fp + -3] != 0;

[fp + -3] is a pointer

Suggestion:

[ap + 0] = [[fp + -3]], ap++;
jmp rel 7 if [fp + -3] != 0;

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch 2 times, most recently from ead4620 to 082b6c3 Compare January 11, 2026 14:59
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

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

@eytan-starkware made 2 comments.
Reviewable status: 5 of 10 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware, @orizi, and @TomerStarkware).


crates/cairo-lang-sierra-ap-change/src/core_libfunc_ap_change.rs line 301 at r1 (raw file):

Previously, orizi wrote…

this sounds incorrect - as box match should include a deref before the match.

Done


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

Previously, orizi wrote…

[fp + -3] is a pointer

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.

@orizi reviewed all commit messages and made 2 comments.
Reviewable status: 5 of 10 files reviewed, 4 unresolved discussions (waiting on @eytan-starkware, @ilyalesokhin-starkware, and @TomerStarkware).


tests/e2e_test_data/libfuncs/enum line 524 at r2 (raw file):

        BoxedOption::None(_) => BoxTrait::new(0),
    }
}

and so on.

Suggestion:

fn foo(e: Box<Option>) -> BoxedOption {
    enum_boxed_match(e)
}

tests/e2e_test_data/libfuncs/enum line 676 at r2 (raw file):

        },
    }
}

Suggestion:

fn foo(e: Box<Data>) -> BoxedData {
    enum_boxed_match(e)
}

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from 082b6c3 to 1c24515 Compare January 11, 2026 15:52
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

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

@eytan-starkware made 2 comments.
Reviewable status: 5 of 10 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware, @orizi, and @TomerStarkware).


tests/e2e_test_data/libfuncs/enum line 524 at r2 (raw file):

Previously, orizi wrote…

and so on.

Done.


tests/e2e_test_data/libfuncs/enum line 676 at r2 (raw file):

        },
    }
}

Done.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from 1c24515 to 2875ad7 Compare January 12, 2026 10:35
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 partially reviewed 4 files and made 1 comment.
Reviewable status: 8 of 10 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi).

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from 2875ad7 to b8ec7a5 Compare January 12, 2026 15:11
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.

@orizi reviewed all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: 5 of 10 files reviewed, 4 unresolved discussions (waiting on @eytan-starkware, @ilyalesokhin-starkware, and @TomerStarkware).


a discussion (no related file):
this PR now requires an actual audit - as it already touches too much existing code.

requires major simplification or splitting.

@eytan-starkware eytan-starkware changed the base branch from main to graphite-base/9378 January 13, 2026 08:57
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from b8ec7a5 to 09abae4 Compare January 13, 2026 08:57
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9378 to eytan_graphite/_refactor_build_jump_and_jump_table_to_support_prepended_instructions January 13, 2026 08:57
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_build_jump_and_jump_table_to_support_prepended_instructions branch from 254407f to 7de9b91 Compare January 13, 2026 13:10
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch 3 times, most recently from c9f6067 to 38c5207 Compare January 14, 2026 13:18
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_build_jump_and_jump_table_to_support_prepended_instructions branch from 7de9b91 to 2f62226 Compare January 14, 2026 13:18
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from 38c5207 to 8581230 Compare January 14, 2026 15:17
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_build_jump_and_jump_table_to_support_prepended_instructions branch 2 times, most recently from 68b0a7d to c4c9d0f Compare January 15, 2026 13:42
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from 8581230 to 3b5d56b Compare January 15, 2026 13:42
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from 3b5d56b to 0298e08 Compare January 15, 2026 13:54
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_build_jump_and_jump_table_to_support_prepended_instructions branch from c4c9d0f to 7638bf8 Compare January 18, 2026 13:59
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch 2 times, most recently from 56b2dd5 to a5e79e9 Compare January 18, 2026 14:03
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_build_jump_and_jump_table_to_support_prepended_instructions branch from 7638bf8 to 902d2d9 Compare January 18, 2026 14:03
SIERRA_UPDATE_MINOR_CHANGE_TAG=Added enum_boxed_match libfunc for matching boxed enums
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

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

@eytan-starkware made 1 comment.
Reviewable status: 3 of 10 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware, @orizi, and @TomerStarkware).


a discussion (no related file):

Previously, orizi wrote…

this PR now requires an actual audit - as it already touches too much existing code.

requires major simplification or splitting.

Now?

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_opt_libfunc_for_matching_on_a_boxed_enum_for_boxed_variants_results branch from a5e79e9 to 45aef2e Compare January 18, 2026 14:44
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