Skip to content

(feat): add array support to equality analysis#9802

Open
eytan-starkware wants to merge 1 commit intomainfrom
eytan_graphite/_feat_add_array_support_to_equality_analysis
Open

(feat): add array support to equality analysis#9802
eytan-starkware wants to merge 1 commit intomainfrom
eytan_graphite/_feat_add_array_support_to_equality_analysis

Conversation

@eytan-starkware
Copy link
Copy Markdown
Contributor

@eytan-starkware eytan-starkware commented Mar 30, 2026

Summary

Extends the equality analysis to track array operations (array_new, array_append, array_pop_front, array_snapshot_pop_front, array_snapshot_pop_back) by reusing the existing struct hashcons infrastructure. Arrays are now represented as (TypeId, Vec<VariableId>) mappings, and pop operations act as destructures that can recover original elements and remaining arrays.


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 only tracked snapshot/desnap, box/unbox, and struct construct relationships. Array operations were not analyzed, meaning the system couldn't detect when array elements could be recovered from pop operations or when two arrays with equivalent elements should be considered equivalent. This limited optimization opportunities for array-heavy code.


What was the behavior or documentation before?

The equality analysis ignored array operations entirely. Calls to array_new, array_append, and array pop functions were treated as opaque operations with no tracked relationships between inputs and outputs.


What is the behavior or documentation after?

The analysis now:

  • Tracks array_new() as creating an empty array construct
  • Tracks array_append(arr, elem) as extending the array's element list
  • Handles array_pop_front by recovering the first element (as boxed) and remaining array
  • Handles array_snapshot_pop_front and array_snapshot_pop_back by properly managing snapshot/box relationships
  • Detects when two arrays with identical element sequences are equivalent

Related issue or discussion (if any)

None specified.


Additional context

The implementation cleverly reuses the existing struct hashcons infrastructure since both structs and arrays map (TypeId, Vec<VariableId>). The analysis correctly handles the complex snapshot and boxing relationships that occur during array pop operations, ensuring that unboxing popped elements yields the correct snapshot types rather than falsely equating snapshot and non-snapshot values.


Note

Medium Risk
Extends core dataflow/equality analysis logic to model array construction and pop semantics, which can affect downstream optimizations and correctness if relationships are inferred incorrectly. Adds new handling paths for extern calls/matches (including snapshot/boxed elements), increasing surface area for edge cases.

Overview
Equality analysis now models arrays as structured values. It reuses the existing struct hashcons to track array_new/array_append chains as (TypeId, elements...) -> array constructs, allowing equivalence to propagate between arrays built from equivalent elements.

Extern match handling is extended for array pop APIs. On array_pop_front/array_pop_front_consume and snapshot variants (array_snapshot_pop_front/array_snapshot_pop_back), the analysis treats pops as destructures: it relates the popped element (boxed, or boxed-snapshot) to the corresponding element and records the remaining array construct.

Tests are updated and expanded. The equality analysis test harness now runs selected optimization phases (notably inlining) so ArrayTrait calls lower to the relevant externs, and new golden tests cover array new/append, pop-front merge behavior, snapshot pop-front/pop-back, and the None-arm empty-array case.

Written by Cursor Bugbot for commit 960b5dd. This will update automatically on new commits. Configure here.

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

Copy link
Copy Markdown
Contributor Author

eytan-starkware commented Mar 30, 2026

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_add_array_support_to_equality_analysis branch from bd180d8 to 1f639eb Compare March 31, 2026 12:52
@eytan-starkware eytan-starkware marked this pull request as ready for review March 31, 2026 12:52
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

let snap_ty = self.lowered.variables[old_snap_arr].ty;
info.set_struct_construct(snap_ty, vec![], old_snap_arr);
info.union(arm.var_ids[0], old_snap_arr);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unchecked index access may panic on empty var_ids

Medium Severity

The else branch for array_snapshot_pop_front/array_snapshot_pop_back unconditionally accesses arm.var_ids[0] without checking that the vector is non-empty. The parallel code for array_pop_front/array_pop_front_consume correctly guards this with if arm.var_ids.len() == 1 before indexing. If arm.var_ids is ever empty (e.g., due to a future "consume" variant of snapshot pop or unexpected IR), this will panic at runtime.

Additional Locations (1)
Fix in Cursor Fix in Web

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 resolved 1 discussion.
Reviewable status: 0 of 3 files reviewed, all discussions resolved (waiting on orizi and TomerStarkware).

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_add_array_support_to_equality_analysis branch from 1f639eb to 960b5dd Compare March 31, 2026 18:55
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.

2 participants