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

Allow renaming a variable twice as it might happen in some cases. #4967

Merged
merged 1 commit into from
Jan 31, 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
18 changes: 9 additions & 9 deletions crates/cairo-lang-lowering/src/optimizations/cancel_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ pub fn cancel_ops(lowered: &mut FlatLowered) {
BackAnalysis { lowered: &*lowered, block_info: Default::default(), analyzer: ctx };
analysis.get_root_info();

let CancelOpsContext { mut var_remapper, mut stmts_to_remove, .. } = analysis.analyzer;
let CancelOpsContext { mut var_remapper, stmts_to_remove, .. } = analysis.analyzer;

// Remove no-longer needed statements.
stmts_to_remove.sort_by_key(|(block_id, stmt_id)| (block_id.0, *stmt_id));
for (block_id, stmt_id) in stmts_to_remove.into_iter().rev() {
// Note that dedup() is used since a statement might be marked for removal more then once.
for (block_id, stmt_id) in stmts_to_remove
.into_iter()
.sorted_by_key(|(block_id, stmt_id)| (block_id.0, *stmt_id))
.rev()
.dedup()
{
lowered.blocks[block_id].statements.remove(stmt_id);
}

Expand Down Expand Up @@ -77,12 +82,7 @@ fn get_use_sites<'a>(

impl<'a> CancelOpsContext<'a> {
fn rename_var(&mut self, from: VariableId, to: VariableId) {
assert!(
self.var_remapper.renamed_vars.insert(from, to).is_none(),
"Variable {:?} was already renamed",
from
);

self.var_remapper.renamed_vars.insert(from, to);
// Move `from` used sites to `to` to allow the optimization to be applied to them.
if let Some(from_use_sites) = self.use_sites.remove(&from) {
self.use_sites.entry(to).or_default().extend(from_use_sites);
Expand Down
49 changes: 49 additions & 0 deletions crates/cairo-lang-lowering/src/optimizations/test_data/cancel_ops
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,52 @@ blk3:
Statements:
End:
Return(v5)

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

//! > Test variable renamed twice.

//! > test_runner_name
test_cancel_ops

//! > function
fn foo(a: (felt252, felt252)) -> (felt252, felt252) {
let (b, c) = a;
let a = (b, c);
let (d, e) = a;
use_tuple((d, e));
(e, d)
}

//! > function_name
foo

//! > module_code
extern fn use_tuple(a: (felt252, felt252)) nopanic;

//! > semantic_diagnostics

//! > lowering_diagnostics

//! > before
Parameters: v0: (core::felt252, core::felt252)
blk0 (root):
Statements:
(v1: core::felt252, v2: core::felt252) <- struct_destructure(v0)
(v3: (core::felt252, core::felt252)) <- struct_construct(v1, v2)
(v4: core::felt252, v5: core::felt252) <- struct_destructure(v3)
(v6: (core::felt252, core::felt252)) <- struct_construct(v4, v5)
() <- test::use_tuple(v6)
(v7: (core::felt252, core::felt252)) <- struct_construct(v5, v4)
End:
Return(v7)

//! > after
Parameters: v0: (core::felt252, core::felt252)
blk0 (root):
Statements:
(v1: core::felt252, v2: core::felt252) <- struct_destructure(v0)
() <- test::use_tuple(v0)
(v7: (core::felt252, core::felt252)) <- struct_construct(v2, v1)
End:
Return(v7)
Loading