Skip to content

(refactor): moving unsafe panic optimization into new dataflow format#9534

Open
eytan-starkware wants to merge 2 commits intomainfrom
eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format
Open

(refactor): moving unsafe panic optimization into new dataflow format#9534
eytan-starkware wants to merge 2 commits intomainfrom
eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format

Conversation

@eytan-starkware
Copy link
Contributor

@eytan-starkware eytan-starkware commented Jan 25, 2026

Summary

Implement a new DataflowBackAnalysis adapter that bridges between the new DataflowAnalyzer trait and the legacy Analyzer trait for backward analysis. This enables a cleaner API for dataflow analysis while maintaining backward compatibility. The PR also refactors the early_unsafe_panic optimization to use this new adapter, improving code organization and clarity.


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 existing analysis framework required implementing the legacy Analyzer trait directly, which had a less intuitive API for dataflow analysis. This change introduces a more structured approach through the DataflowAnalyzer trait and an adapter pattern, making it easier to write new analyses while maintaining compatibility with existing code.


What was the behavior or documentation before?

Previously, all backward analyses had to implement the legacy Analyzer trait directly, which mixed traversal logic with analysis logic and required manual handling of statement processing.


What is the behavior or documentation after?

Now, analyses can implement the more focused DataflowAnalyzer trait, which separates traversal from analysis logic. The new DataflowBackAnalysis adapter handles the translation between the new and legacy interfaces. The early_unsafe_panic optimization has been refactored to use this new approach, resulting in cleaner, more maintainable code.


Additional context

This change is part of a larger effort to modernize the dataflow analysis framework in the compiler. It maintains backward compatibility while introducing a cleaner API for new analyses. The refactoring of early_unsafe_panic serves as an example of how to migrate existing analyses to the new framework.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

eytan-starkware commented Jan 25, 2026

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

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


crates/cairo-lang-lowering/src/optimizations/early_unsafe_panic.rs line 99 at r1 (raw file):

    pub reachability: Reachability<'db>,
    /// Locations where we need to insert unsafe_panic.
    pub fixes: Vec<(StatementLocation, LocationId<'db>)>,

holding multiple vectors sounds less efficient (in general) - any way to keep a way to do the original concept?

Code quote:

    pub fixes: Vec<(StatementLocation, LocationId<'db>)>,

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

pub mod core;

// Re-export commonly used types at the module level for convenience.

remove at the original PR.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from e116334 to 4e2fa7b Compare January 27, 2026 06:44
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_creating_a_dataflow_analysis_framework_and_migrating_backward_analysis branch from db28b67 to 8a6f9a7 Compare January 27, 2026 06:45
Copy link
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 3 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware, @orizi, and @TomerStarkware).


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

Previously, orizi wrote…

remove at the original PR.

Done.


crates/cairo-lang-lowering/src/optimizations/early_unsafe_panic.rs line 99 at r1 (raw file):

Previously, orizi wrote…

holding multiple vectors sounds less efficient (in general) - any way to keep a way to do the original concept?

You are correct it should have been in the context. Done

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


crates/cairo-lang-lowering/src/optimizations/early_unsafe_panic.rs line 96 at r2 (raw file):

}

/// Analysis info containing reachability state and accumulated fixes.

it no longer does - also, does it still need to exist?

Suggestion:

/// Analysis info containing reachability state.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_feat_creating_a_dataflow_analysis_framework_and_migrating_backward_analysis branch from 8a6f9a7 to 90cfcf3 Compare January 27, 2026 08:02
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from 4e2fa7b to 710d10b Compare January 27, 2026 08:02
Copy link
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, 1 unresolved discussion (waiting on @ilyalesokhin-starkware, @orizi, and @TomerStarkware).


crates/cairo-lang-lowering/src/optimizations/early_unsafe_panic.rs line 96 at r2 (raw file):

Previously, orizi wrote…

it no longer does - also, does it still need to exist?

Actually unneeded, the info is just reachability

@eytan-starkware eytan-starkware marked this pull request as ready for review January 27, 2026 08:03
@graphite-app graphite-app bot changed the base branch from eytan_graphite/_feat_creating_a_dataflow_analysis_framework_and_migrating_backward_analysis to graphite-base/9534 January 27, 2026 08:11
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from 710d10b to 51ee583 Compare January 27, 2026 08:55
@graphite-app graphite-app bot changed the base branch from graphite-base/9534 to main January 27, 2026 08:56
@graphite-app
Copy link

graphite-app bot commented Jan 27, 2026

Merge activity

  • Jan 27, 8:56 AM UTC: Graphite rebased this pull request, because this pull request is set to merge when ready.
  • Jan 27, 2:09 PM UTC: Graphite rebased this pull request, because this pull request is set to merge when ready.
  • Jan 28, 11:59 AM UTC: Graphite rebased this pull request, because this pull request is set to merge when ready.
  • Jan 29, 10:59 AM UTC: Graphite rebased this pull request, because this pull request is set to merge when ready.

Copy link
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: 3 of 4 files reviewed, 1 unresolved discussion (waiting on @eytan-starkware, @ilyalesokhin-starkware, and @TomerStarkware).


crates/cairo-lang-lowering/src/optimizations/early_unsafe_panic.rs line 104 at r3 (raw file):

    }

    fn merge(

why is the merge duplicated?

@eytan-starkware eytan-starkware changed the base branch from main to graphite-base/9534 January 27, 2026 14:08
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from 51ee583 to 874a15a Compare January 27, 2026 14:08
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9534 to eytan_graphite/_feat_improved_analyzer_trait_to_better_deal_with_intra_block_flow January 27, 2026 14:08
@graphite-app graphite-app bot changed the base branch from eytan_graphite/_feat_improved_analyzer_trait_to_better_deal_with_intra_block_flow to main January 27, 2026 14:09
Copy link
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, 1 unresolved discussion (waiting on @ilyalesokhin-starkware, @orizi, and @TomerStarkware).


crates/cairo-lang-lowering/src/optimizations/early_unsafe_panic.rs line 104 at r3 (raw file):

Previously, orizi wrote…

why is the merge duplicated?

Done.

@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from 874a15a to c52dc05 Compare January 28, 2026 11:58
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from c52dc05 to 90720f5 Compare January 28, 2026 14:03
Copy link
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, made 4 comments, and resolved 1 discussion.
Reviewable status: 3 of 4 files reviewed, 4 unresolved discussions (waiting on @eytan-starkware, @ilyalesokhin-starkware, and @TomerStarkware).


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

            match_info.location(),
            transferred_infos.into_iter(),
        )

any reason not to keep as iterator?

Suggestion:

        let transferred_infos = match_info
            .arms()
            .iter()
            .zip(infos)
            .map(|(arm, info)| {
                let edge = Edge::MatchArm { arm, match_info };
                let transferred = self.analyzer.transfer_edge(&info, &edge);
                (arm.block_id, transferred)
            });
        self.analyzer.merge(
            statement_location,
            match_info.location(),
            transferred_infos,
        )

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

        _vars: &'a [VarUsage<'db>],
    ) -> Self::Info {
        let block_end = &self.lowered.blocks[statement_location.0].end;

don't you need edges here as well?


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

        _var: &VarUsage<'db>,
    ) -> Self::Info {
        let block_end = &self.lowered.blocks[statement_location.0].end;

don't you need edges here as well?


crates/cairo-lang-lowering/src/optimizations/early_unsafe_panic.rs line 100 at r5 (raw file):

    const DIRECTION: Direction = Direction::Backward;

    fn initial_info(&mut self, _block_id: BlockId, _block_end: &'a BlockEnd<'db>) -> Self::Info {

can you re-order the functions to make it easier to review there are no change?

@eytan-starkware eytan-starkware changed the base branch from main to graphite-base/9534 January 29, 2026 10:58
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from 90720f5 to 7e5e8ee Compare January 29, 2026 10:58
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9534 to eytan_graphite/_refactor_updating_analysis_framework_to_be_easier_to_use January 29, 2026 10:58
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from 7e5e8ee to 168adb3 Compare January 29, 2026 10:58
@graphite-app graphite-app bot changed the base branch from eytan_graphite/_refactor_updating_analysis_framework_to_be_easier_to_use to main January 29, 2026 10:59
@eytan-starkware eytan-starkware changed the base branch from main to graphite-base/9534 February 1, 2026 11:12
@eytan-starkware eytan-starkware force-pushed the eytan_graphite/_refactor_moving_unsafe_panic_optimization_into_new_dataflow_format branch from 168adb3 to a92b937 Compare February 1, 2026 14:23
@eytan-starkware eytan-starkware changed the base branch from graphite-base/9534 to eytan_graphite/_refactor_updating_analysis_framework_to_be_easier_to_use February 1, 2026 14:23
@eytan-starkware eytan-starkware changed the base branch from eytan_graphite/_refactor_updating_analysis_framework_to_be_easier_to_use to main February 3, 2026 12: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.

3 participants