(feat): Added tracking on enums as part of equality analysis#9645
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
💡 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".
orizi
left a comment
There was a problem hiding this comment.
@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.e5a4477 to
5050695
Compare
c70c62e to
c5cdb55
Compare
eytan-starkware
left a comment
There was a problem hiding this comment.
@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.
5050695 to
ad2d2df
Compare
59b0a37 to
aba39fd
Compare
58473a1 to
660af22
Compare
aba39fd to
c91c0a9
Compare
660af22 to
653055e
Compare
c91c0a9 to
adcb8be
Compare
orizi
left a comment
There was a problem hiding this comment.
@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?
653055e to
3a1f86f
Compare
eytan-starkware
left a comment
There was a problem hiding this comment.
@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.
orizi
left a comment
There was a problem hiding this comment.
@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;3a1f86f to
d61c1ca
Compare
eytan-starkware
left a comment
There was a problem hiding this comment.
@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.
orizi
left a comment
There was a problem hiding this comment.
@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)));d61c1ca to
c436bbe
Compare
eytan-starkware
left a comment
There was a problem hiding this comment.
@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.
orizi
left a comment
There was a problem hiding this comment.
@orizi reviewed 1 file and all commit messages, made 1 comment, and resolved 2 discussions.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

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