Sitelet https://github.com/starkware-libs/cairo/pull/10056
Skip to content

bugfix(return-opt): Fixed issue with identical variants on return building. - #10056

Merged
orizi merged 1 commit into
mainfrom
orizi/06-07-bugfix_return-opt_fixed_issue_with_identical_variants_on_return_building
Jun 7, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-07-bugfix_return-opt_fixed_issue_with_identical_variants_on_return_building

Conversation

@orizi

@orizi orizi commented Jun 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Replaces the (TypeId, Vec<VariableId>) tuple key used in EarlyReturnContext::constructed with a dedicated Construction enum that has distinct Struct and Enum variants. The Enum variant stores a semantic::ConcreteVariant and a single VariableId, rather than a TypeId and a Vec<VariableId>, ensuring that different enum variants with the same input variable cannot collide in the memoization map.

A regression test is added covering the case where two different variants of the same enum (MyEnum::A(x) and MyEnum::B(x)) are constructed from the same input variable, verifying they produce distinct output variables.


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 previous key (TypeId, Vec<VariableId>) for enum constructions used the enum's type ID and the input variable wrapped in a Vec. Because two different variants of the same enum share the same TypeId, constructing MyEnum::A(x) and MyEnum::B(x) would produce identical keys, causing the second construction to incorrectly reuse the variable allocated for the first. This led to wrong lowered IR when both variants appeared in the same early-return path.


What was the behavior or documentation before?

Constructing two different variants of the same enum from the same input variable during return optimization would collide in the constructed map, causing the second enum construct statement to reuse the output variable of the first variant instead of allocating a new one.


What is the behavior or documentation after?

Each enum construction is keyed by its ConcreteVariant (which encodes the specific variant, not just the enum type) and its input VariableId. Struct constructions continue to be keyed by TypeId and the full list of input VariableIds. The two cases are now structurally separate and cannot collide.


Related issue or discussion (if any)

None.


Additional context

The new Construction enum derives Debug, Clone, PartialEq, Eq, and Hash, making it a well-typed, self-documenting replacement for the ad-hoc tuple key.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

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

@orizi
orizi marked this pull request as ready for review June 7, 2026 12:59
@cursor

cursor Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes compiler lowering for early-return IR generation; wrong memoization could have produced incorrect programs before, but the fix is localized with a regression test.

Overview
Fixes return optimization incorrectly deduplicating EnumConstruct IR when rebuilding early returns.

EarlyReturnContext's memo map no longer uses (enum type, input var) for enums. It now keys constructions with a Construction enum: structs stay (type, field vars); enums use (concrete variant, input var), so variants like MyEnum::A(x) and MyEnum::B(x) cannot share one cached construct.

Adds a return_optimization test for (MyEnum::A(x), MyEnum::B(x)) expecting two distinct enum constructs in the optimized lowering.

Reviewed by Cursor Bugbot for commit 0089e45. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

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

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

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

@orizi
orizi added this pull request to the merge queue Jun 7, 2026
Merged via the queue into main with commit 9b4db3c Jun 7, 2026
54 checks passed
@orizi
orizi deleted the orizi/06-07-bugfix_return-opt_fixed_issue_with_identical_variants_on_return_building branch June 7, 2026 13:16
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