Skip to content

Conversation

@giladchase
Copy link
Contributor

@giladchase giladchase commented Nov 17, 2025

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

giladchase commented Nov 17, 2025

@giladchase giladchase marked this pull request as ready for review November 17, 2025 09:43
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: 0 of 13 files reviewed, 3 unresolved discussions (waiting on @TomerStarkware)


crates/cairo-lang-sierra-to-casm/src/invocations/boxing_test.rs line 2 at r1 (raw file):

use cairo_lang_casm::ap_change::ApChange;
use cairo_lang_casm::casm;

revert.


tests/e2e_test_data/libfuncs/box line 316 at r1 (raw file):

fn foo(x: felt252) -> Box<@felt252> {
    let y = x;
    core::box::into_repr_ptr_wrapper(@y)

tests should be only here - additionally define the extern fn and function making sure the var is local - just here.


crates/cairo-lang-sierra-ap-change/src/core_libfunc_ap_change.rs line 114 at r1 (raw file):

        Box(libfunc) => match libfunc {
            BoxConcreteLibfunc::Into(_) => vec![ApChange::Known(1)],
            BoxConcreteLibfunc::IntoReprPtr(_) => vec![ApChange::Known(1)],

Suggestion:

            BoxConcreteLibfunc::IntoFromLocal(_) => vec![ApChange::Known(1)],

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: 0 of 13 files reviewed, 6 unresolved discussions (waiting on @giladchase and @TomerStarkware)


crates/cairo-lang-sierra/src/extensions/modules/boxing.rs line 97 at r1 (raw file):

            SierraApChange::Known { new_vars_only: true },
        ))
    }

Suggestion:

    const STR_ID: &'static str = "into_repr_ptr";

    fn specialize_signature(
        &self,
        context: &dyn SignatureSpecializationContext,
        ty: ConcreteTypeId,
    ) -> Result<LibfuncSignature, SpecializationError> {
        Ok(LibfuncSignature::new_non_branch(
            vec![ty.clone()],
            vec![OutputVarInfo {
                ty: box_ty(context, ty)?,
                ref_info: OutputVarReferenceInfo::NewTempVar { idx: 0 },
            }],
            SierraApChange::Known { new_vars_only: true },
        ))
    }

corelib/src/box.cairo line 54 at r1 (raw file):

extern fn unbox<T>(box: Box<T>) -> T nopanic;
extern fn box_forward_snapshot<T>(value: @Box<T>) -> Box<@T> nopanic;
extern fn into_repr_ptr<T>(value: @T) -> Box<@T> nopanic;

revert


crates/cairo-lang-semantic/src/expr/compute.rs line 1082 at r1 (raw file):

            let snapshot_expr_id = ctx.arenas.exprs.alloc(Expr::Snapshot(snapshot_expr.clone()));

            // Call core::box::into_repr_ptr_wrapper(@x).

revert.

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: 0 of 13 files reviewed, 7 unresolved discussions (waiting on @giladchase and @TomerStarkware)


crates/cairo-lang-sierra/src/extensions/modules/boxing.rs line 77 at r1 (raw file):

pub type IntoBoxLibfunc = WrapSignatureAndTypeGenericLibfunc<IntoBoxLibfuncWrapped>;

/// Libfunc for wrapping a snapshot object of type T into a repr-ptr box.

rename and remove any other snapshot ref.

@giladchase giladchase force-pushed the gilad/11-17-feat_optimize_t_to_use_fp_offset_instead_of_box_alloc branch from 2773e0c to e010ca7 Compare November 18, 2025 06:49
@giladchase giladchase changed the title feat: optimize &T to use fp+offset instead of box alloc feat(sierra): Add local_into_box libfunc for stack-based boxing of variables. Nov 18, 2025
@giladchase giladchase changed the title feat(sierra): Add local_into_box libfunc for stack-based boxing of variables. feat: Add local_into_box libfunc for stack-based boxing of variables. Nov 18, 2025
Copy link
Contributor Author

@giladchase giladchase 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: 0 of 14 files reviewed, 7 unresolved discussions (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-sierra/src/extensions/modules/boxing.rs line 77 at r1 (raw file):

Previously, orizi wrote…

rename and remove any other snapshot ref.

Done.


tests/e2e_test_data/libfuncs/box line 316 at r1 (raw file):

Previously, orizi wrote…

tests should be only here - additionally define the extern fn and function making sure the var is local - just here.

Done.


corelib/src/box.cairo line 54 at r1 (raw file):

Previously, orizi wrote…

revert

Done.


crates/cairo-lang-semantic/src/expr/compute.rs line 1082 at r1 (raw file):

Previously, orizi wrote…

revert.

Done.


crates/cairo-lang-sierra-to-casm/src/invocations/boxing_test.rs line 2 at r1 (raw file):

Previously, orizi wrote…

revert.

Done.


crates/cairo-lang-sierra/src/extensions/modules/boxing.rs line 97 at r1 (raw file):

            SierraApChange::Known { new_vars_only: true },
        ))
    }

Done.


crates/cairo-lang-sierra-ap-change/src/core_libfunc_ap_change.rs line 114 at r1 (raw file):

        Box(libfunc) => match libfunc {
            BoxConcreteLibfunc::Into(_) => vec![ApChange::Known(1)],
            BoxConcreteLibfunc::IntoReprPtr(_) => vec![ApChange::Known(1)],

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 of 14 files at r2, all commit messages.
Reviewable status: 3 of 14 files reviewed, 8 unresolved discussions (waiting on @TomerStarkware)


crates/cairo-lang-sierra/src/extensions/modules/boxing.rs line 92 at r2 (raw file):

            vec![OutputVarInfo {
                ty: box_ty(context, ty)?,
                ref_info: OutputVarReferenceInfo::NewTempVar { idx: 0 },

Suggestion:

                ref_info: OutputVarReferenceInfo::Deferred(DeferredOutputKind::AddConst { param_idx: 0 }),

crates/cairo-lang-sierra-to-casm/src/invocations/boxing.rs line 83 at r2 (raw file):

        Default::default(),
        pre_instructions,
    ))

probably somewhat buggy - but something of this sort

Suggestion:

    let cell = CellRef { register: Register::AP, offset: -2 }
    let addr = CellExpression::BinOp {
        op: CellOperator::Add,
        a: cell,
        b: DerefOrImmediate::Immediate(base_offset),
    };
    builder.build(
        casm!(call rel 0;).instructions,
        vec![RelocationEntry { instruction_idx: 0, relocation: Relocation::EndOfProgram }],
        [[addr].into_iter()].into_iter(),
    )

crates/cairo-lang-sierra-to-casm/src/invocations/misc.rs line 264 at r2 (raw file):

/// Helper function to generate code that computes a pointer to a local based on its offset from fp.
pub fn get_fp_based_pointer(offset: i32) -> InstructionsWithRelocations {

use directly in the function for now.
not useful as a helper - used only once.


crates/cairo-lang-sierra-to-casm/src/invocations/misc.rs line 271 at r2 (raw file):

        // After calling the above empty function, `[ap - 2]` contains the callsite's `fp`.
        // Compute fp + offset (the address of the local) and push onto stack.
        [ap+0] = [ap + -2] + (offset), ap++;

this store isn't required - just return the non-store deref + const.
suggestion in the caller fucntion.

Code quote:

        [ap+0] = [ap + -2] + (offset), ap++;

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 14 files at r2.
Reviewable status: 10 of 14 files reviewed, 5 unresolved discussions (waiting on @giladchase and @TomerStarkware)

@giladchase giladchase force-pushed the gilad/11-17-feat_optimize_t_to_use_fp_offset_instead_of_box_alloc branch from e010ca7 to 065c8fc Compare November 18, 2025 09:39
Copy link
Contributor Author

@giladchase giladchase 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: 10 of 14 files reviewed, 5 unresolved discussions (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-sierra-to-casm/src/invocations/boxing.rs line 83 at r2 (raw file):

Previously, orizi wrote…

probably somewhat buggy - but something of this sort

Done.


crates/cairo-lang-sierra-to-casm/src/invocations/misc.rs line 264 at r2 (raw file):

Previously, orizi wrote…

use directly in the function for now.
not useful as a helper - used only once.

Done.


crates/cairo-lang-sierra-to-casm/src/invocations/misc.rs line 271 at r2 (raw file):

Previously, orizi wrote…

this store isn't required - just return the non-store deref + const.
suggestion in the caller fucntion.

Done.


crates/cairo-lang-sierra/src/extensions/modules/boxing.rs line 92 at r2 (raw file):

            vec![OutputVarInfo {
                ty: box_ty(context, ty)?,
                ref_info: OutputVarReferenceInfo::NewTempVar { idx: 0 },

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 1 of 14 files at r2, 3 of 5 files at r3, all commit messages.
Reviewable status: 12 of 14 files reviewed, 4 unresolved discussions (waiting on @TomerStarkware)


tests/e2e_test_data/libfuncs/box line 323 at r3 (raw file):

    let y = x;
    local_into_box_wrapper(@y)
}

remove snapshots from all tests.

Suggestion:

extern fn local_into_box<T>(value: T) -> Box<T> nopanic;
#[inline(never)]
pub fn local_into_box_wrapper<T>(value: T) -> Box<T> {
    local_into_box(value)
}

fn foo(x: felt252) -> Box<felt252> {
    let y = x;
    local_into_box_wrapper(y)
}

@giladchase giladchase force-pushed the gilad/11-17-feat_optimize_t_to_use_fp_offset_instead_of_box_alloc branch from 065c8fc to f16a1c3 Compare November 18, 2025 12:07
Copy link
Contributor Author

@giladchase giladchase 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: 12 of 14 files reviewed, 4 unresolved discussions (waiting on @orizi and @TomerStarkware)


tests/e2e_test_data/libfuncs/box line 323 at r3 (raw file):

Previously, orizi wrote…

remove snapshots from all tests.

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 all commit messages.
Reviewable status: 12 of 14 files reviewed, 8 unresolved discussions (waiting on @TomerStarkware)


tests/e2e_test_data/libfuncs/box line 323 at r4 (raw file):

    let y = x;
    local_into_box_wrapper(y)
}

Suggestion:

extern fn local_into_box<T>(value: T) -> Box<T> nopanic;

fn foo(x: felt252) -> Box<felt252> {
    local_into_box(x)
}

tests/e2e_test_data/libfuncs/box line 360 at r4 (raw file):

//! > ==========================================================================

//! > local_into_box with ap-based variable

this tests does nothing - as the function receives the value as an arg - so on fp.

delete.


tests/e2e_test_data/libfuncs/box line 442 at r4 (raw file):

    let x = ();
    local_into_box_wrapper(x)
}

Suggestion:

extern fn local_into_box<T>(value: T) -> Box<T> nopanic;

fn foo(x: ()) -> Box<()> {
    let x = ();
    local_into_box(x)
}

tests/e2e_test_data/libfuncs/box line 476 at r4 (raw file):

test::local_into_box_wrapper::<()>@F1([0]: Unit) -> (Box<Unit>);

//! > ==========================================================================

add

Suggestion:

//! > ==========================================================================

//! > reference operator non-local zero-sized type

//! > test_runner_name
SmallE2ETestRunner

//! > cairo_code
extern fn local_into_box<T>(value: T) -> Box<T> nopanic;

fn foo() -> Box<()> {
    local_into_box(())
}

//! > ==========================================================================

tests/e2e_test_data/libfuncs/box line 539 at r4 (raw file):

//! > ==========================================================================

//! > local_into_box with mixed fp and ap variables

similarly - doesn't really test anything.

@giladchase giladchase force-pushed the gilad/11-17-feat_optimize_t_to_use_fp_offset_instead_of_box_alloc branch from f16a1c3 to f770888 Compare November 18, 2025 12:38
Copy link
Contributor Author

@giladchase giladchase 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: 12 of 14 files reviewed, 8 unresolved discussions (waiting on @orizi and @TomerStarkware)


tests/e2e_test_data/libfuncs/box line 360 at r4 (raw file):

Previously, orizi wrote…

this tests does nothing - as the function receives the value as an arg - so on fp.

delete.

Done.


tests/e2e_test_data/libfuncs/box line 476 at r4 (raw file):

Previously, orizi wrote…

add

Done.


tests/e2e_test_data/libfuncs/box line 539 at r4 (raw file):

Previously, orizi wrote…

similarly - doesn't really test anything.

Done.


tests/e2e_test_data/libfuncs/box line 323 at r4 (raw file):

    let y = x;
    local_into_box_wrapper(y)
}

Done.


tests/e2e_test_data/libfuncs/box line 442 at r4 (raw file):

    let x = ();
    local_into_box_wrapper(x)
}

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 1 of 5 files at r3, 1 of 1 files at r5, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


tests/e2e_test_data/libfuncs/box line 356 at r5 (raw file):

//! > casm
call rel 5;
[ap + 0] = [ap + -2] + 0, ap++;

reduce code size to equivalent code.

Suggestion:

[ap + 0] = [ap + -2], ap++;

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