Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multi-pop for span. #5588

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub fn core_libfunc_ap_change<InfoProvider: InvocationApChangeInfoProvider>(
| ArrayConcreteLibfunc::SnapshotPopBack(_) => {
vec![ApChange::Known(1), ApChange::Known(1)]
}
ArrayConcreteLibfunc::SnapshotMultiPopFront(_)
| ArrayConcreteLibfunc::SnapshotMultiPopBack(_) => {
vec![ApChange::Known(3), ApChange::Known(3)]
}
ArrayConcreteLibfunc::Get(libfunc) => {
if info_provider.type_size(&libfunc.ty) == 1 { [4, 3] } else { [5, 4] }
.map(ApChange::Known)
Expand Down
7 changes: 7 additions & 0 deletions crates/cairo-lang-sierra-gas/src/core_libfunc_cost_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ pub fn core_libfunc_cost(
| ArrayConcreteLibfunc::SnapshotPopBack(_) => {
vec![ConstCost::steps(2).into(), ConstCost::steps(3).into()]
}
ArrayConcreteLibfunc::SnapshotMultiPopFront(_)
| ArrayConcreteLibfunc::SnapshotMultiPopBack(_) => {
vec![
(ConstCost::steps(4) + ConstCost::range_checks(1)).into(),
(ConstCost::steps(5) + ConstCost::range_checks(1)).into(),
]
}
ArrayConcreteLibfunc::Get(libfunc) => {
if info_provider.type_size(&libfunc.ty) == 1 {
vec![
Expand Down
131 changes: 129 additions & 2 deletions crates/cairo-lang-sierra-to-casm/src/invocations/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cairo_lang_casm::builder::CasmBuilder;
use cairo_lang_casm::builder::{CasmBuilder, Var};
use cairo_lang_casm::casm_build_extend;
use cairo_lang_sierra::extensions::array::ArrayConcreteLibfunc;
use cairo_lang_sierra::extensions::array::{ArrayConcreteLibfunc, ConcreteMultiPopLibfunc};
use cairo_lang_sierra::ids::ConcreteTypeId;

use super::{CompiledInvocation, CompiledInvocationBuilder, InvocationError};
Expand Down Expand Up @@ -28,6 +28,12 @@ pub fn build(
ArrayConcreteLibfunc::SnapshotPopBack(libfunc) => {
build_pop_back(&libfunc.ty, builder, false)
}
ArrayConcreteLibfunc::SnapshotMultiPopFront(libfunc) => {
build_multi_pop_front(libfunc, builder)
}
ArrayConcreteLibfunc::SnapshotMultiPopBack(libfunc) => {
build_multi_pop_back(libfunc, builder)
}
ArrayConcreteLibfunc::Get(libfunc) => build_array_get(&libfunc.ty, builder),
ArrayConcreteLibfunc::Slice(libfunc) => build_array_slice(&libfunc.ty, builder),
ArrayConcreteLibfunc::Len(libfunc) => build_array_len(&libfunc.ty, builder),
Expand Down Expand Up @@ -365,3 +371,124 @@ fn build_array_len(
Default::default(),
))
}

/// Handles a Sierra statement for popping elements from the beginning of an array.
fn build_multi_pop_front(
libfunc: &ConcreteMultiPopLibfunc,
builder: CompiledInvocationBuilder<'_>,
) -> Result<CompiledInvocation, InvocationError> {
let [expr_range_check, expr_arr] = builder.try_get_refs()?;
let range_check = expr_range_check.try_unpack_single()?;
let [arr_start, arr_end] = expr_arr.try_unpack()?;
let element_size = builder.program_info.type_sizes[&libfunc.ty];

let mut casm_builder = CasmBuilder::default();
add_input_variables! {casm_builder,
buffer(1) range_check;
deref arr_start;
deref arr_end;
};
let popped_size: i16 = libfunc.popped_values * element_size;
casm_build_extend!(casm_builder, let orig_range_check = range_check;);
extend_multi_pop_failure_checks(
&mut casm_builder,
range_check,
arr_start,
arr_end,
popped_size,
);
casm_build_extend! {casm_builder,
// Success case.
const popped_size = popped_size;
tempvar rc;
// Calculating the new start, as it is required for calculating the range checked value.
tempvar new_start = arr_start + popped_size;
assert rc = arr_end - new_start;
assert rc = *(range_check++);
};
let failure_handle = get_non_fallthrough_statement_id(&builder);
Ok(builder.build_from_casm_builder(
casm_builder,
[
("Fallthrough", &[&[range_check], &[new_start, arr_end], &[arr_start]], None),
("Failure", &[&[range_check], &[arr_start, arr_end]], Some(failure_handle)),
],
CostValidationInfo {
range_check_info: Some((orig_range_check, range_check)),
extra_costs: None,
},
))
}

/// Handles a Sierra statement for popping elements from the end of an array.
fn build_multi_pop_back(
libfunc: &ConcreteMultiPopLibfunc,
builder: CompiledInvocationBuilder<'_>,
) -> Result<CompiledInvocation, InvocationError> {
let [expr_range_check, expr_arr] = builder.try_get_refs()?;
let range_check = expr_range_check.try_unpack_single()?;
let [arr_start, arr_end] = expr_arr.try_unpack()?;
let element_size = builder.program_info.type_sizes[&libfunc.ty];

let mut casm_builder = CasmBuilder::default();
add_input_variables! {casm_builder,
buffer(1) range_check;
deref arr_start;
deref arr_end;
};
let popped_size: i16 = libfunc.popped_values * element_size;
casm_build_extend!(casm_builder, let orig_range_check = range_check;);
extend_multi_pop_failure_checks(
&mut casm_builder,
range_check,
arr_start,
arr_end,
popped_size,
);
casm_build_extend! {casm_builder,
// Success case.
const popped_size = popped_size;
tempvar rc;
// Calculating the new end, as it is required for calculating the range checked value.
tempvar new_end = arr_end - popped_size;
assert rc = new_end - arr_start;
assert rc = *(range_check++);
};
let failure_handle = get_non_fallthrough_statement_id(&builder);
Ok(builder.build_from_casm_builder(
casm_builder,
[
("Fallthrough", &[&[range_check], &[arr_start, new_end], &[new_end]], None),
("Failure", &[&[range_check], &[arr_start, arr_end]], Some(failure_handle)),
],
CostValidationInfo {
range_check_info: Some((orig_range_check, range_check)),
extra_costs: None,
},
))
}

/// Extends the casm builder with the common part of the multi-pop front and multi-pop back.
fn extend_multi_pop_failure_checks(
casm_builder: &mut CasmBuilder,
range_check: Var,
arr_start: Var,
arr_end: Var,
popped_size: i16,
) {
casm_build_extend! {casm_builder,
const popped_size_plus_1 = popped_size + 1;
const popped_size = popped_size;
let arr_start_popped = arr_start + popped_size;
tempvar has_enough_elements;
hint TestLessThanOrEqual {lhs: arr_start_popped, rhs: arr_end} into {dst: has_enough_elements};
jump HasEnoughElements if has_enough_elements != 0;
// Proving that `arr_start - arr_end + popped_size + 1 >= 0` and therefore
// `popped_size + 1 >= arr_end - arr_start == arr.len()` ==> `arr.len() < popopped_size`.
tempvar minus_length = arr_start - arr_end;
tempvar rc = minus_length + popped_size_plus_1;
assert rc = *(range_check++);
jump Failure;
HasEnoughElements:
};
}
11 changes: 11 additions & 0 deletions crates/cairo-lang-sierra/src/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ fn args_as_two_types(
}
}

/// Helper for extracting a type and a value from the template arguments.
fn args_as_type_and_value(
args: &[GenericArg],
) -> Result<(ConcreteTypeId, BigInt), SpecializationError> {
match args {
[GenericArg::Type(ty), GenericArg::Value(value)] => Ok((ty.clone(), value.clone())),
[_, _] => Err(SpecializationError::UnsupportedGenericArg),
_ => Err(SpecializationError::WrongNumberOfGenericArgs),
}
}

/// Helper for extracting the type from the template arguments.
fn args_as_single_user_func(args: &[GenericArg]) -> Result<FunctionId, SpecializationError> {
match args {
Expand Down
Loading
Loading