Skip to content

Conversation

@eytan-starkware
Copy link
Contributor

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

Enhances the reboxing optimization to support whole variable reboxing, eliminating unnecessary box/unbox operations.

What changed?

  • Refactored the apply_reboxing_candidate function to support for the ReboxingValue::Unboxed case, which represents whole variable reboxing
  • Implemented a new replace_variable_usages function that replaces all usages of the reboxed with the original box variable throughout the lowered function
  • Extracted the existing member-of-unboxed logic into a separate replace_into_box_call function
  • Updated tests to verify the new functionality

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

eytan-starkware commented Dec 9, 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 9, 2025 10:08
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_now_also_works_on_whole_var_reboxing branch from 65c6092 to ffde167 Compare December 9, 2025 10:39
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.
Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @eytan-starkware, @giladchase, and @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/optimizations/reboxing.rs line 268 at r1 (raw file):

/// Replaces all usages of old_var with new_var throughout the lowered function.
fn replace_variable_usages(lowered: &mut Lowered<'_>, old_var: VariableId, new_var: VariableId) {

can't you use VarRemapper to do all candidate replacements in one go?

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_now_also_works_on_whole_var_reboxing branch from ffde167 to e3b6a1f Compare December 10, 2025 07:18
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.

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @giladchase, @ilyalesokhin-starkware, and @orizi)


crates/cairo-lang-lowering/src/optimizations/reboxing.rs line 268 at r1 (raw file):

Previously, orizi wrote…

can't you use VarRemapper to do all candidate replacements in one go?

Yes, much better

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_now_also_works_on_whole_var_reboxing branch from e3b6a1f to 0496ffe Compare December 10, 2025 07:28
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 of 2 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @eytan-starkware, @giladchase, and @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/optimizations/reboxing.rs line 205 at r2 (raw file):

    // We sort the candidates such that removal of statements can be done in reverse order.
    stmts_to_remove.sort_by_key(|(block_id, stmt_idx)| (block_id.0, *stmt_idx));

was there any order to the original candidates? or these are basically random?

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.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @giladchase, @ilyalesokhin-starkware, and @orizi)


crates/cairo-lang-lowering/src/optimizations/reboxing.rs line 205 at r2 (raw file):

Previously, orizi wrote…

was there any order to the original candidates? or these are basically random?

I think it is safe to assume they were ordered correctly ahead of time, so reversing directly should be safe, but I dont want to add that assumption. I can use unstable sort if performance is an issue, but I expect this to be a small amount of statements to remove

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: all files reviewed, 1 unresolved discussion (waiting on @eytan-starkware, @giladchase, and @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/optimizations/reboxing.rs line 205 at r2 (raw file):

Previously, eytan-starkware wrote…

I think it is safe to assume they were ordered correctly ahead of time, so reversing directly should be safe, but I dont want to add that assumption. I can use unstable sort if performance is an issue, but I expect this to be a small amount of statements to remove

do stable - and doc that it should be sorted already.
i just don't want it to overly kill large auto-generated codes.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_also_applied_on_box_of_snapshots branch from ef335a5 to 630ce29 Compare December 10, 2025 08:42
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_now_also_works_on_whole_var_reboxing branch from 0496ffe to 4370c99 Compare December 10, 2025 08:42
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.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @giladchase, @ilyalesokhin-starkware, and @orizi)


crates/cairo-lang-lowering/src/optimizations/reboxing.rs line 205 at r2 (raw file):

Previously, orizi wrote…

do stable - and doc that it should be sorted already.
i just don't want it to overly kill large auto-generated codes.

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:

@orizi reviewed 1 of 1 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @giladchase and @ilyalesokhin-starkware)

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_now_also_works_on_whole_var_reboxing branch from 4370c99 to f5eada9 Compare December 10, 2025 10:32
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_also_applied_on_box_of_snapshots branch from 630ce29 to 1dd5922 Compare December 10, 2025 10:32
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_now_also_works_on_whole_var_reboxing branch from f5eada9 to 7e430d6 Compare December 10, 2025 12:08
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_optimization_reboxing_also_applied_on_box_of_snapshots branch from 1dd5922 to 2e496a0 Compare December 10, 2025 12:08
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 of 1 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @giladchase and @ilyalesokhin-starkware)

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