Skip to content

(feat): Added tracking on enums as part of equality analysis#9645

Merged
eytan-starkware merged 1 commit intomainfrom
eytan_graphite/_feat_equality_analysis_enum_support
Feb 19, 2026
Merged

(feat): Added tracking on enums as part of equality analysis#9645
eytan-starkware merged 1 commit intomainfrom
eytan_graphite/_feat_equality_analysis_enum_support

Conversation

@eytan-starkware
Copy link
Copy Markdown
Contributor

@eytan-starkware eytan-starkware commented Feb 12, 2026

Summary

Enhances the equality analysis to track enum constructs and match relationships. The analysis now maintains a hashcons for enum variants, allowing it to detect when two enum constructs with the same variant and equivalent inputs should produce equivalent outputs. This enables more precise tracking of enum values across branches and through match statements.


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 equality analysis previously didn't track relationships between enum variants and their inner values. This limited the optimizer's ability to detect equivalent enum values and perform optimizations across match statements and branches. By tracking these relationships, we can enable more aggressive optimizations for code that uses enums.


What was the behavior or documentation before?

The equality analysis would not track relationships between enum variants and their inner values. When an enum was constructed or matched against, this information was lost, preventing optimizations that depend on knowing the relationship between the enum and its contents.


What is the behavior or documentation after?

The equality analysis now:

  • Tracks enum constructs via a hashcons mechanism
  • Preserves enum relationships across branches when the same variant is used
  • Recognizes when a match arm extracts a value that was previously used to construct the enum
  • Properly handles the merging of enum information at control flow join points

Additional context

This is part of ongoing work to improve the optimizer's ability to reason about higher-level constructs in Cairo. The implementation uses both forward and reverse mappings to efficiently track enum relationships in both directions.

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

Copy link
Copy Markdown
Contributor Author

eytan-starkware commented Feb 12, 2026

Copy link
Copy Markdown

@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: e5a447708d

ℹ️ 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".

Comment thread crates/cairo-lang-lowering/src/analysis/equality_analysis.rs
Copy link
Copy Markdown
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: 0 of 3 files reviewed, 2 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware).


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 91 at r1 (raw file):

    enum_hashcons: OrderedHashMap<(ConcreteVariant<'db>, VariableId), VariableId>,

    /// Reverse hashcons for enum constructs: maps (variant, output_rep) -> input_rep.

the maps seem the opposite.

Code quote:

(variant, output_rep) -> input_rep.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_equality_analysis_enum_support branch from e5a4477 to 5050695 Compare February 15, 2026 14:07
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/feat_dataflow_forward_equality_analysis branch from c70c62e to c5cdb55 Compare February 15, 2026 14:07
Copy link
Copy Markdown
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 3 files reviewed, 2 unresolved discussions (waiting on @orizi and @TomerStarkware).


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 91 at r1 (raw file):

Previously, orizi wrote…

the maps seem the opposite.

Bad comment. Fixed.

Comment thread crates/cairo-lang-lowering/src/analysis/equality_analysis.rs
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_equality_analysis_enum_support branch from 5050695 to ad2d2df Compare February 15, 2026 14:55
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/feat_dataflow_forward_equality_analysis branch 2 times, most recently from 59b0a37 to aba39fd Compare February 15, 2026 15:28
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_equality_analysis_enum_support branch 2 times, most recently from 58473a1 to 660af22 Compare February 16, 2026 07:48
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/feat_dataflow_forward_equality_analysis branch from aba39fd to c91c0a9 Compare February 16, 2026 07:48
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_equality_analysis_enum_support branch from 660af22 to 653055e Compare February 16, 2026 12:40
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/feat_dataflow_forward_equality_analysis branch from c91c0a9 to adcb8be Compare February 16, 2026 12:40
Base automatically changed from eytan_graphite/feat_dataflow_forward_equality_analysis to main February 17, 2026 07:48
Copy link
Copy Markdown
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: 0 of 4 files reviewed, 2 unresolved discussions (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 1 at r3 (raw file):

//! Equality analysis for lowered IR.

this entire file already existed - right?

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_equality_analysis_enum_support branch from 653055e to 3a1f86f Compare February 18, 2026 12:20
Copy link
Copy Markdown
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: 0 of 4 files reviewed, 2 unresolved discussions (waiting on orizi and TomerStarkware).


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 1 at r3 (raw file):

Previously, orizi wrote…

this entire file already existed - right?

Correct. Rebased.

Copy link
Copy Markdown
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, made 1 comment, and resolved 1 discussion.
Reviewable status: 1 of 4 files reviewed, 2 unresolved discussions (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 511 at r4 (raw file):

                {
                    let matched_var = enum_info.input.var_id;
                    let arm_var = arm.var_ids[0];

Suggestion:

                    && let MatchArmSelector::VariantId(variant) = arm.arm_selector
                    && let [arm_var] = &arm.var_ids
                {
                    let matched_var = enum_info.input.var_id;

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_equality_analysis_enum_support branch from 3a1f86f to d61c1ca Compare February 19, 2026 08:54
Copy link
Copy Markdown
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 and resolved 1 discussion.
Reviewable status: 1 of 4 files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 511 at r4 (raw file):

                {
                    let matched_var = enum_info.input.var_id;
                    let arm_var = arm.var_ids[0];

Done.

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


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 263 at r5 (raw file):

        for (&(variant, input), &output) in self.enum_hashcons.iter() {
            let name = variant.id.name(db).to_string(db);
            lines.push(format!("{}({}) = {}", name, v(input), v(output)));

Suggestion:

            let name = variant.id.name(db).to_string(db);
            lines.push(format!("{name}({}) = {}", v(input), v(output)));

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_equality_analysis_enum_support branch from d61c1ca to c436bbe Compare February 19, 2026 09:54
Copy link
Copy Markdown
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 4 files reviewed, 2 unresolved discussions (waiting on orizi and TomerStarkware).


crates/cairo-lang-lowering/src/analysis/equality_analysis.rs line 263 at r5 (raw file):

        for (&(variant, input), &output) in self.enum_hashcons.iter() {
            let name = variant.id.name(db).to_string(db);
            lines.push(format!("{}({}) = {}", name, v(input), v(output)));

Done.

Copy link
Copy Markdown
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 file and all commit messages, made 1 comment, and resolved 2 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@eytan-starkware eytan-starkware added this pull request to the merge queue Feb 19, 2026
Merged via the queue into main with commit 777da4c Feb 19, 2026
53 checks passed
@eytan-starkware eytan-starkware deleted the eytan_graphite/_feat_equality_analysis_enum_support branch April 6, 2026 06:58
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.

3 participants