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

Avoid store_temp in remapping when the target is a local. #7089

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -97,15 +97,14 @@ enum_match<core::bool>([0]) { fallthrough([5]) label_test::foo::1([6]) }
branch_align() -> ()
drop<Unit>([5]) -> ()
function_call<user@test::revoke_ap>() -> ([7])
rename<felt252>([7]) -> ([1])
store_local<felt252>([2], [7]) -> ([1])
jump() { label_test::foo::3() }
label_test::foo::1:
branch_align() -> ()
drop<Unit>([6]) -> ()
const_as_immediate<Const<felt252, 1>>() -> ([8])
store_temp<felt252>([8]) -> ([1])
store_local<felt252>([2], [8]) -> ([1])
label_test::foo::3:
store_local<felt252>([2], [1]) -> ([1])
function_call<user@test::revoke_ap>() -> ([9])
drop<felt252>([9]) -> ()
felt252_add([3], [1]) -> ([10])
Expand Down Expand Up @@ -386,3 +385,57 @@ felt252_add([2], [9]) -> ([10])
store_temp<felt252>([10]) -> ([10])
return([10])
label_test::foo::2:

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

//! > store local instead of push

//! > test_runner_name
test_function_generator

//! > function
fn foo(x: felt252) -> felt252 {
let a = if x == 0 {
1
} else {
2
};
revoke_ap();
a
}

//! > function_name
foo

//! > module_code
fn revoke_ap() -> felt252 {
revoke_ap()
}

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > sierra_gen_diagnostics

//! > sierra_code
label_test::foo::0:
alloc_local<felt252>() -> ([2])
finalize_locals() -> ()
disable_ap_tracking() -> ()
felt252_is_zero([0]) { fallthrough() label_test::foo::1([3]) }
branch_align() -> ()
const_as_immediate<Const<felt252, 1>>() -> ([4])
store_local<felt252>([2], [4]) -> ([1])
jump() { label_test::foo::3() }
label_test::foo::1:
branch_align() -> ()
drop<NonZero<felt252>>([3]) -> ()
const_as_immediate<Const<felt252, 2>>() -> ([5])
store_local<felt252>([2], [5]) -> ([1])
label_test::foo::3:
function_call<user@test::revoke_ap>() -> ([6])
drop<felt252>([6]) -> ()
store_temp<felt252>([1]) -> ([1])
return([1])
label_test::foo::2:
35 changes: 35 additions & 0 deletions crates/cairo-lang-sierra-generator/src/store_variables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,25 @@ impl<'a> AddStoreVariableStatements<'a> {
for (i, pre_sierra::PushValue { var, var_on_stack, ty, dup }) in
push_values.iter().enumerate()
{

if !dup && var != var_on_stack{
if let Some(uninitialized_local_var_id) =
self.local_variables.get(var_on_stack)
{
self.store_local_ex(
var,
&uninitialized_local_var_id.clone(),
var_on_stack,
ty,
);

state.pop_var_state(var);
state.variables.insert(var_on_stack.clone(), VarState::LocalVar );
continue;
}
}


let (is_on_stack, var_state) = match state.pop_var_state(var) {
VarState::Deferred { info: deferred_info } => {
if let DeferredVariableKind::Const = deferred_info.kind {
Expand Down Expand Up @@ -512,6 +531,22 @@ impl<'a> AddStoreVariableStatements<'a> {
));
}

/// Adds a `store_local` command storing `var` into itself using the preallocated
/// `uninitialized_local_var_id`.
fn store_local_ex(
&mut self,
var: &sierra::ids::VarId,
uninitialized_local_var_id: &sierra::ids::VarId,
output: &sierra::ids::VarId,
ty: &sierra::ids::ConcreteTypeId,
) {
self.result.push(simple_statement(
store_local_libfunc_id(self.db, ty.clone()),
&[uninitialized_local_var_id.clone(), var.clone()],
&[output.clone()],
));
}

/// Adds a call to the dup() libfunc, duplicating `var` into `dup_var`.
fn dup(
&mut self,
Expand Down
11 changes: 5 additions & 6 deletions crates/cairo-lang-sierra-generator/src/store_variables/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ fn store_local_result_of_if() {
let statements: Vec<pre_sierra::StatementWithLocation> = vec![
dummy_simple_branch(&db, "branch", &[], 0),
// If part.
dummy_simple_statement(&db, "store_temp<felt252>", &["100"], &["100"]),
dummy_push_values(&db, &[("100", "200")]),
dummy_jump_statement(&db, 1),
// Else part.
dummy_label(&db, 0),
dummy_push_values(&db, &[("0", "100")]),
dummy_push_values(&db, &[("0", "200")]),
// Post-if.
dummy_label(&db, 1),
dummy_simple_statement(&db, "nope", &[], &[]),
Expand All @@ -437,18 +437,17 @@ fn store_local_result_of_if() {
test_add_store_statements(
&db,
statements,
OrderedHashMap::from_iter(vec![("100".into(), "200".into()),],),
OrderedHashMap::from_iter(vec![("200".into(), "300".into()),],),
&["0", "100"],
),
vec![
"branch() { label_test::test::0() fallthrough() }",
"store_temp<felt252>(100) -> (100)",
"store_local<felt252>(300, 100) -> (200)",
"jump() { label_test::test::1() }",
"label_test::test::0:",
"store_temp<felt252>(0) -> (100)",
"store_local<felt252>(300, 0) -> (200)",
"label_test::test::1:",
"nope() -> ()",
"store_local<felt252>(200, 100) -> (100)",
"revoke_ap() -> ()",
"return()",
],
Expand Down
Loading