Skip to content

Conversation

@TomerStarkware
Copy link
Collaborator

@TomerStarkware TomerStarkware commented Dec 4, 2025

TL;DR

Improve function specialization in the lowering phase by tracking specialized function calls

What changed?

  • Enhanced the const folding mechanism to handle recursive specialization more intelligently

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator Author

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

@TomerStarkware TomerStarkware requested a review from orizi December 4, 2025 13:55
@TomerStarkware TomerStarkware marked this pull request as ready for review December 4, 2025 13:56
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 of 6 files at r1, all commit messages.
Reviewable status: 1 of 6 files reviewed, 1 unresolved discussion (waiting on @TomerStarkware)


crates/cairo-lang-lowering/src/lower/generated_test.rs line 69 at r1 (raw file):

        )
        .unwrap();
        let calls = lowering

doc what you are doing here.

@TomerStarkware TomerStarkware force-pushed the tomer/12-04-added_specialization_for_recursive_functions branch 3 times, most recently from 9be13c0 to b110587 Compare December 7, 2025 10:45
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 of 6 files at r1, 7 of 8 files at r2.
Reviewable status: 8 of 9 files reviewed, 1 unresolved discussion (waiting on @TomerStarkware)


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

                }
                _ => vec![None; call_stmt.inputs.len()],
            };

Suggestion:

        let self_specializition =
            if let ConcreteFunctionWithBodyLongId::Specialized(specialized) = self.caller_function &&
             caller_base == called_base {
                specialized.args.iter().map(Some).collect()
            } else {
                vec![None; call_stmt.inputs.len()]
            };

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.

Reviewable status: 8 of 9 files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


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

        ty: TypeId<'db>,
        unknown_vars: &mut Vec<VarUsage<'db>>,
        coerce: Option<&SpecializationArg<'db>>,

add doc about the coercion.

@TomerStarkware TomerStarkware force-pushed the tomer/12-04-added_specialization_for_recursive_functions branch from b110587 to fc53245 Compare December 7, 2025 11:08
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 of 8 files at r2, all commit messages.
Reviewable status: 9 of 11 files reviewed, 3 unresolved discussions (waiting on @TomerStarkware)


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

                            unreachable!("Expected an enum specialization argument");
                        };
                        assert_eq!(coercion_variant, &variant);

shouldn't this just return None?

Code quote:

                        assert_eq!(coercion_variant, &variant);

@TomerStarkware TomerStarkware force-pushed the tomer/12-04-added_specialization_for_recursive_functions branch from fc53245 to 334a183 Compare December 7, 2025 11:49
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 10 of 10 files at r4, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @TomerStarkware)

@TomerStarkware TomerStarkware force-pushed the tomer/12-04-added_specialization_for_recursive_functions branch from 334a183 to 1a346e4 Compare December 7, 2025 13:21
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 of 8 files at r5, all commit messages.
Reviewable status: 6 of 11 files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


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

                    Some(coerce) => {
                        let SpecializationArg::Struct(specialization_args) = coerce else {
                            unreachable!("Expected a struct specialization argument");

reachable ::Const as well.


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

                        let SpecializationArg::Enum { variant: coercion_variant, payload } = coerce
                        else {
                            unreachable!("Expected an enum specialization argument");

reachable ::Const as well.

@TomerStarkware TomerStarkware force-pushed the tomer/12-04-added_specialization_for_recursive_functions branch from 1a346e4 to 0ba4dcb Compare December 11, 2025 13:52
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.

:lgtm:

@orizi reviewed 4 of 8 files at r5, 8 of 8 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)

Copy link
Collaborator Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)


crates/cairo-lang-lowering/src/lower/generated_test.rs line 69 at r1 (raw file):

Previously, orizi wrote…

doc what you are doing here.

Done.


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

Previously, orizi wrote…

add doc about the coercion.

Done.


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

Previously, orizi wrote…

shouldn't this just return None?

Done.


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

Previously, orizi wrote…

reachable ::Const as well.

changed in previous pr


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

                }
                _ => vec![None; call_stmt.inputs.len()],
            };

Done.

@TomerStarkware TomerStarkware force-pushed the tomer/12-04-added_specialization_for_recursive_functions branch from 0ba4dcb to fdbbc36 Compare December 11, 2025 15:17
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 7 of 7 files at r7, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)

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