Skip to content

Conversation

@eytan-starkware
Copy link
Contributor

@eytan-starkware eytan-starkware commented Jan 13, 2026

Summary

Refactored the struct boxed deconstruct libfunc to improve handling of output variable references. This PR:

  1. Extracted common utility functions to a new utils.rs module:

    • Added unwrap_snapshot_type to handle snapshot type detection and unwrapping
    • Added boxed_output_var_info to create boxed output variables consistently
  2. Improved the struct deconstruction logic:

    • Replaced the manual snapshot detection with the new utility function
    • Restructured the output variable creation to properly handle zero-sized types
    • Made the code more maintainable by separating analysis and signature creation

Type of change

Please check one:

  • Performance improvement

Why is this change needed?

The previous implementation of struct boxed deconstruction had complex logic for handling output variable references, especially when dealing with zero-sized types. This refactoring makes the code more maintainable and consistent by extracting common patterns into utility functions and improving the handling of output variable references.


What was the behavior or documentation before?

The struct boxed deconstruct libfunc had complex inline logic for handling snapshots and determining output variable references. The code was less maintainable and had a more complex approach to handling zero-sized types.


What is the behavior or documentation after?

The functionality remains the same, but the code is now more maintainable with:

  • Clear separation between type analysis and signature creation
  • Utility functions for common operations
  • Improved handling of output variable references for zero-sized types
  • More consistent approach to boxed output variables

Additional context

This refactoring is part of ongoing efforts to improve the maintainability of the Sierra codebase, particularly around struct operations and boxed types.

Copy link
Contributor Author

eytan-starkware commented Jan 13, 2026

@reviewable-StarkWare
Copy link

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.

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


crates/cairo-lang-sierra/src/extensions/modules/structure.rs line 284 at r1 (raw file):

impl StructBoxedDeconstructLibfunc {
    /// Analyzes a struct type to extract member types and snapshot information.

restore doc.


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 135 at r1 (raw file):

/// Returns the inner type and whether the provided type is a snapshot.
pub fn unwrap_snapshot_type(

Suggestion:

pub fn peel_snapshot(

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: 0 of 2 files reviewed, 3 unresolved discussions (waiting on @eytan-starkware, @ilyalesokhin-starkware, and @TomerStarkware).


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 155 at r1 (raw file):

) -> Result<OutputVarInfo, SpecializationError> {
    let inner_type = if is_snapshot { snapshot_ty(context, component_ty)? } else { component_ty };
    Ok(OutputVarInfo { ty: box_ty(context, inner_type)?, ref_info })

the ref_info part is only confusing.

just handle the typing.

Code quote:

/// Helper to create a boxed output variable for unpack operations.
pub fn boxed_output_var_info(
    context: &dyn SignatureSpecializationContext,
    component_ty: ConcreteTypeId,
    is_snapshot: bool,
    ref_info: OutputVarReferenceInfo,
) -> Result<OutputVarInfo, SpecializationError> {
    let inner_type = if is_snapshot { snapshot_ty(context, component_ty)? } else { component_ty };
    Ok(OutputVarInfo { ty: box_ty(context, inner_type)?, ref_info })

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 1 file and all commit messages, and made 1 comment.
Reviewable status: 1 of 2 files reviewed, 3 unresolved discussions (waiting on @eytan-starkware and @ilyalesokhin-starkware).

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from f4ea7ec to 36c875a Compare January 13, 2026 13:10
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 3 comments.
Reviewable status: 1 of 2 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware and @orizi).


crates/cairo-lang-sierra/src/extensions/modules/structure.rs line 284 at r1 (raw file):

Previously, orizi wrote…

restore doc.

Done.


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 155 at r1 (raw file):

Previously, orizi wrote…

the ref_info part is only confusing.

just handle the typing.

Done.


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 135 at r1 (raw file):

/// Returns the inner type and whether the provided type is a snapshot.
pub fn unwrap_snapshot_type(

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: 0 of 2 files reviewed, 5 unresolved discussions (waiting on @eytan-starkware, @ilyalesokhin-starkware, and @TomerStarkware).


crates/cairo-lang-sierra/src/extensions/modules/structure.rs line 321 at r2 (raw file):

        let mut outputs = Vec::with_capacity(member_types.len());

        for member_ty in member_types.by_ref() {

this change broke the current logic.
would probably change tests after rebase as well.


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 152 at r2 (raw file):

    component_ty: ConcreteTypeId,
    is_snapshot: bool,
) -> Result<ConcreteTypeId, SpecializationError> {

or something better - as the output isn't relevant now.

Suggestion:

/// Helper to create a boxed output variable for unpack operations.
pub fn boxed_ty_with_snapshot(
    context: &dyn SignatureSpecializationContext,
    component_ty: ConcreteTypeId,
    is_snapshot: bool,
) -> Result<ConcreteTypeId, SpecializationError> {

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch 2 times, most recently from 612b112 to a0f42e0 Compare January 14, 2026 15:17
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: 0 of 2 files reviewed, 5 unresolved discussions (waiting on @ilyalesokhin-starkware, @orizi, and @TomerStarkware).


crates/cairo-lang-sierra/src/extensions/modules/structure.rs line 321 at r2 (raw file):

Previously, orizi wrote…

this change broke the current logic.
would probably change tests after rebase as well.

f2f


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 152 at r2 (raw file):

Previously, orizi wrote…

or something better - as the output isn't relevant now.

Done.

@eytan-starkware eytan-starkware changed the base branch from main to graphite-base/9462 January 15, 2026 13:42
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from a0f42e0 to 4e956b9 Compare January 15, 2026 13:42
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9462 to eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests January 15, 2026 13:43
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 2 files and all commit messages, made 1 comment, and resolved 5 discussions.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @eytan-starkware and @ilyalesokhin-starkware).


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 145 at r3 (raw file):

        Ok((ty, false))
    }
}

Suggestion:

pub fn peel_snapshot(
    context: &dyn SignatureSpecializationContext,
    ty: &ConcreteTypeId,
) -> Result<(&ConcreteTypeId, bool), SpecializationError> {
    let type_info = context.get_type_info(ty)?;
    if type_info.long_id.generic_id == SnapshotType::id() {
        Ok((args_as_single_type(&type_info.long_id.generic_args)?, true))
    } else {
        Ok((ty, false))
    }
}

@eytan-starkware eytan-starkware changed the base branch from eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests to graphite-base/9462 January 18, 2026 13:32
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from 4e956b9 to 7b6d2af Compare January 18, 2026 13: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 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @ilyalesokhin-starkware and @orizi).


crates/cairo-lang-sierra/src/extensions/modules/utils.rs line 145 at r3 (raw file):

        Ok((ty, false))
    }
}

Done

@eytan-starkware eytan-starkware changed the base branch from graphite-base/9462 to eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests January 18, 2026 13:59
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from 7b6d2af to a793aa3 Compare January 18, 2026 14:03
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests branch from bf6b4e1 to 44b56da Compare January 18, 2026 14:03
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:

@orizi reviewed 2 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware).

@eytan-starkware eytan-starkware changed the base branch from eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests to graphite-base/9462 January 19, 2026 14:14
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from a793aa3 to 2ba5194 Compare January 19, 2026 15:31
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9462 to eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests January 19, 2026 15:32
@eytan-starkware eytan-starkware changed the base branch from eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests to graphite-base/9462 January 20, 2026 10:46
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from 2ba5194 to 0e998a4 Compare January 20, 2026 11:20
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9462 to eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests January 20, 2026 11:20
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests branch from f07d02a to 9825250 Compare January 20, 2026 12:08
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from 0e998a4 to 442b9c0 Compare January 20, 2026 12:08
@eytan-starkware eytan-starkware changed the base branch from eytan_graphite/_test_adding_casm_trace_to_boxed_enum_e2e_tests to main January 20, 2026 12:25
@graphite-app
Copy link

graphite-app bot commented Jan 20, 2026

Merge activity

  • Jan 20, 12:58 PM UTC: This pull request can not be added to the Graphite merge queue. Please try rebasing and resubmitting to merge when ready.
  • Jan 20, 12:58 PM UTC: Graphite disabled "merge when ready" on this PR due to: a merge conflict with the target branch; resolve the conflict and try again..

SIERRA_UPDATE_MINOR_CHANGE_TAG=Refactored boaxed deconstruct to be more reusable
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/refactored_boxed_deconstruct_signature_code_creating_reusable_functions branch from 442b9c0 to de22370 Compare January 20, 2026 13:38
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 1 file and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware).

@eytan-starkware eytan-starkware added this pull request to the merge queue Jan 20, 2026
Merged via the queue into main with commit cabd233 Jan 20, 2026
55 checks passed
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