Skip to content

Commit

Permalink
look at renamed var when matching deconstruct construct statements. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalesokhin-starkware authored Jan 31, 2024
1 parent 58a9145 commit 35e0268
Show file tree
Hide file tree
Showing 4 changed files with 1,632 additions and 1,573 deletions.
20 changes: 11 additions & 9 deletions crates/cairo-lang-lowering/src/optimizations/cancel_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,24 @@ pub fn cancel_ops(lowered: &mut FlatLowered) {
let ctx = CancelOpsContext {
lowered,
use_sites: Default::default(),
renamed_vars: Default::default(),
var_remapper: Default::default(),
stmts_to_remove: vec![],
};
let mut analysis =
BackAnalysis { lowered: &*lowered, block_info: Default::default(), analyzer: ctx };
analysis.get_root_info();
let mut ctx = analysis.analyzer;

let mut rebuilder = CancelOpsRebuilder { renamed_vars: ctx.renamed_vars };
let CancelOpsContext { mut var_remapper, mut stmts_to_remove, .. } = analysis.analyzer;

// Remove no-longer needed statements.
ctx.stmts_to_remove.sort_by_key(|(block_id, stmt_id)| (block_id.0, *stmt_id));
for (block_id, stmt_id) in ctx.stmts_to_remove.into_iter().rev() {
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() {
lowered.blocks[block_id].statements.remove(stmt_id);
}

// Rebuild the blocks with the new variable names.
for block in lowered.blocks.iter_mut() {
*block = rebuilder.rebuild_block(block);
*block = var_remapper.rebuild_block(block);
}
}

Expand All @@ -57,7 +56,7 @@ pub struct CancelOpsContext<'a> {
use_sites: UnorderedHashMap<VariableId, Vec<StatementLocation>>,

/// Maps a variable to the variable that it was renamed to.
renamed_vars: UnorderedHashMap<VariableId, VariableId>,
var_remapper: CancelOpsRebuilder,

/// Statements that can be be removed.
stmts_to_remove: Vec<StatementLocation>,
Expand All @@ -79,7 +78,7 @@ fn get_use_sites<'a>(
impl<'a> CancelOpsContext<'a> {
fn rename_var(&mut self, from: VariableId, to: VariableId) {
assert!(
self.renamed_vars.insert(from, to).is_none(),
self.var_remapper.renamed_vars.insert(from, to).is_none(),
"Variable {:?} was already renamed",
from
);
Expand Down Expand Up @@ -119,7 +118,9 @@ impl<'a> CancelOpsContext<'a> {
stmt.outputs.iter(),
construct_stmt.inputs.iter(),
)
.all(|(output, input)| output == &input.var_id) =>
.all(|(output, input)| {
output == &self.var_remapper.map_var_id(input.var_id)
}) =>
{
self.stmts_to_remove.push(**location);
Some(construct_stmt)
Expand Down Expand Up @@ -285,6 +286,7 @@ impl<'a> Analyzer<'a> for CancelOpsContext<'a> {
}
}

#[derive(Default)]
pub struct CancelOpsRebuilder {
renamed_vars: UnorderedHashMap<VariableId, VariableId>,
}
Expand Down
41 changes: 41 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 @@ -41,6 +41,47 @@ End:

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

//! > Test destruct folding.

//! > test_runner_name
test_cancel_ops

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

//! > function_name
foo

//! > module_code

//! > semantic_diagnostics

//! > lowering_diagnostics

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

//! > after
Parameters: v0: ((core::felt252,),)
blk0 (root):
Statements:
End:
Return(v0)

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

//! > Test destruct after merge (optimization not possible).

//! > test_runner_name
Expand Down
Loading

0 comments on commit 35e0268

Please sign in to comment.