Skip to content

(feat): def site analysis based on forward dataflow#9870

Merged
eytan-starkware merged 1 commit intomainfrom
eytan_graphite/_feat_def_site_analysis_based_on_forward_dataflow
Apr 30, 2026
Merged

(feat): def site analysis based on forward dataflow#9870
eytan-starkware merged 1 commit intomainfrom
eytan_graphite/_feat_def_site_analysis_based_on_forward_dataflow

Conversation

@eytan-starkware
Copy link
Copy Markdown
Contributor

@eytan-starkware eytan-starkware commented Apr 23, 2026

Summary

Adds a DefLocation enum and a DefSiteAnalysis pass to the lowering analysis framework. DefLocation distinguishes between variables defined at block entry (function parameters, goto remapping destinations, match arm bindings) and variables defined as outputs of a statement. DefSiteAnalysis implements the DataflowAnalyzer trait using the existing forward dataflow framework and produces a DefSites result mapping each variable (by arena index) to its DefLocation.


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?

Downstream optimization passes and semantic checks need to know where each variable in the lowered IR is first defined. Previously there was no dedicated analysis for this; callers had to reconstruct def-site information ad hoc. A shared, tested analysis avoids duplicated logic and provides a consistent representation.


What was the behavior or documentation before?

There was no DefLocation type and no DefSiteAnalysis pass. Consumers of the lowered IR had no standard way to query where a variable was defined.


What is the behavior or documentation after?

Calling DefSiteAnalysis::analyze(lowered) returns a DefSites value (a Vec<DefLocation> indexed by variable arena index). Each entry is either:

  • DefLocation::BlockEntry(block) — the variable is a function parameter, a goto remapping destination, or a match arm binding introduced at the entry of block.
  • DefLocation::Statement((block, stmt_idx)) — the variable is produced as an output of statement stmt_idx in block.

File-based tests covering linear functions, match arms, and goto remappings are included under src/analysis/test_data/def_site.


Related issue or discussion (if any)


Additional context

The DefSiteAnalysis state is accumulated globally (not per-block) because each variable has exactly one definition site in SSA-form lowered IR, so no per-block lattice state is needed. The Info type is () and merge is a no-op.

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 23, 2026

PR Summary

Low Risk
Adds an isolated new analysis pass and tests without changing existing lowering or optimization behavior; main risk is incorrect def-site attribution affecting future consumers.

Overview
Adds a new DefLocation enum plus a DefSiteAnalysis forward dataflow pass that computes, for each variable arena index, whether it is defined at a BlockEntry (params, match-arm bindings, goto remap destinations) or by a specific Statement output.

Introduces file-based tests and fixtures for linear code, match arms, goto remappings, multi-output statements, and a regression test that ensures the analysis panics if any variable remains UNRESOLVED.

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

@eytan-starkware eytan-starkware changed the base branch from eytan_graphite/_refactor_preserve_partial_struct_construct_info_during_merge_with_fieldvar_placeholders to graphite-base/9870 April 23, 2026 12:27
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.

Reviewed by Cursor Bugbot for commit 21bf47f. Configure here.

Comment thread crates/cairo-lang-lowering/src/analysis/mod.rs
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: 21bf47fb36

ℹ️ 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/def_site.rs Outdated
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, and made 2 comments.
Reviewable status: 1 of 4 files reviewed, 4 unresolved discussions (waiting on eytan-starkware and TomerStarkware).


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

}

impl fmt::Display for DefSites {

implement Debug for DefLocation and use the debug string of the derive here.
format!("{:#?}")


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

    ) {
        for &output in stmt.outputs() {
            self.def_sites[output.index()] = (block_id, stmt_idx + 1);

just use the DefLocation to begin with.

Code quote:

        for &output in stmt.outputs() {
            self.def_sites[output.index()] = (block_id, stmt_idx + 1);

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_def_site_analysis_based_on_forward_dataflow branch from 21bf47f to 4cf78fb Compare April 28, 2026 15:01
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 3 comments.
Reviewable status: 1 of 4 files reviewed, 4 unresolved discussions (waiting on orizi and TomerStarkware).


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

Previously, orizi wrote…

implement Debug for DefLocation and use the debug string of the derive here.
format!("{:#?}")

Done.


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

Previously, orizi wrote…

just use the DefLocation to begin with.

Done.

Comment thread crates/cairo-lang-lowering/src/analysis/def_site.rs Outdated
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9870 to main April 28, 2026 15:01
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 2 files, made 2 comments, and resolved 1 discussion.
Reviewable status: 2 of 4 files reviewed, 3 unresolved discussions (waiting on eytan-starkware and TomerStarkware).


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

Previously, eytan-starkware wrote…

Done.

i think you can just remove this entire impl and do 1 other suggestion I added instead


crates/cairo-lang-lowering/src/analysis/def_site_test.rs line 37 at r2 (raw file):

        let lowering_str = formatted_lowered(db, Some(&lowered));
        let result_str = DefSiteAnalysis::analyze(&lowered).to_string();
        (lowering_str, result_str)

Suggestion:

        (
            formatted_lowered(db, Some(&lowered)),
            format!("{:#?}", DefSiteAnalysis::analyze(&lowered).0
        )

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_def_site_analysis_based_on_forward_dataflow branch from 4cf78fb to 27b79d7 Compare April 30, 2026 11:16
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: 2 of 4 files reviewed, 2 unresolved discussions (waiting on orizi and TomerStarkware).


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

Previously, orizi wrote…

i think you can just remove this entire impl and do 1 other suggestion I added instead

Agreed


crates/cairo-lang-lowering/src/analysis/def_site_test.rs line 37 at r2 (raw file):

        let lowering_str = formatted_lowered(db, Some(&lowered));
        let result_str = DefSiteAnalysis::analyze(&lowered).to_string();
        (lowering_str, result_str)

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 3 files 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 force-pushed the eytan_graphite/_feat_def_site_analysis_based_on_forward_dataflow branch from 27b79d7 to e0a55cc Compare April 30, 2026 12:08
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 reviewed 1 file and all commit messages.
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 Apr 30, 2026
Merged via the queue into main with commit e4d7e3f Apr 30, 2026
54 checks passed
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