Skip to content

filter unused remappings. #7631

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 21 additions & 6 deletions crates/cairo-lang-lowering/src/optimizations/reorder_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod test;

use std::cmp::Reverse;
use std::collections::HashSet;

use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::unordered_hash_map::{Entry, UnorderedHashMap};
Expand All @@ -13,15 +14,16 @@ use crate::borrow_check::analysis::{Analyzer, BackAnalysis, StatementLocation};
use crate::db::LoweringGroup;
use crate::ids::FunctionId;
use crate::{
BlockId, FlatLowered, MatchInfo, Statement, StatementCall, VarRemapping, VarUsage, VariableId,
BlockId, FlatBlockEnd, FlatLowered, MatchInfo, Statement, StatementCall, VarRemapping,
VarUsage, VariableId,
};

/// Reorder the statements in the lowering in order to move variable definitions closer to their
/// usage. Statement with no side effects and unused outputs are removed.
///
/// The list of call statements that can be moved is currently hardcoded.
///
/// Removing unnecessary remapping before this optimization will result in better code.
/// Removes unnecessary remapping as it allows more statements to be removed.
pub fn reorder_statements(db: &dyn LoweringGroup, lowered: &mut FlatLowered) {
if lowered.blocks.is_empty() {
return;
Expand All @@ -30,15 +32,17 @@ pub fn reorder_statements(db: &dyn LoweringGroup, lowered: &mut FlatLowered) {
lowered: &*lowered,
moveable_functions: &db.priv_movable_function_ids(),
statement_to_move: vec![],
unused_remapping_dsts: HashSet::new(),
};
let mut analysis = BackAnalysis::new(lowered, ctx);
analysis.get_root_info();
let ctx = analysis.analyzer;
let ReorderStatementsContext { statement_to_move, unused_remapping_dsts, .. } =
analysis.analyzer;

let mut changes_by_block =
OrderedHashMap::<BlockId, Vec<(usize, Option<Statement>)>>::default();

for (src, opt_dst) in ctx.statement_to_move {
for (src, opt_dst) in statement_to_move {
changes_by_block.entry(src.0).or_insert_with(Vec::new).push((src.1, None));

if let Some(dst) = opt_dst {
Expand All @@ -47,6 +51,12 @@ pub fn reorder_statements(db: &dyn LoweringGroup, lowered: &mut FlatLowered) {
}
}

for block in lowered.blocks.iter_mut() {
if let FlatBlockEnd::Goto(_, remappings) = &mut block.end {
remappings.retain(|dst, _| !unused_remapping_dsts.contains(dst));
}
}

for (block_id, block_changes) in changes_by_block {
let statements = &mut lowered.blocks[block_id].statements;
let block_len = statements.len();
Expand Down Expand Up @@ -85,6 +95,7 @@ pub struct ReorderStatementsContext<'a> {
// A list of function that can be moved.
moveable_functions: &'a UnorderedHashSet<FunctionId>,
statement_to_move: Vec<(StatementLocation, Option<StatementLocation>)>,
unused_remapping_dsts: HashSet<VariableId>,
}
impl ReorderStatementsContext<'_> {
fn call_can_be_moved(&mut self, stmt: &StatementCall) -> bool {
Expand Down Expand Up @@ -152,8 +163,12 @@ impl Analyzer<'_> for ReorderStatementsContext<'_> {
_target_block_id: BlockId,
remapping: &VarRemapping,
) {
for VarUsage { var_id, .. } in remapping.values() {
info.next_use.insert(*var_id, statement_location);
for (dst, VarUsage { var_id: src, .. }) in remapping.iter() {
if info.next_use.get(dst).is_some() {
info.next_use.insert(*src, statement_location);
} else {
self.unused_remapping_dsts.insert(*dst);
}
}
}

Expand Down