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

feat: replace quadratic removal of rc instructions #6705

Merged
merged 1 commit into from
Dec 5, 2024
Merged
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
feat: replace quadratic removal of rc instructions
TomAFrench committed Dec 5, 2024
commit b7ce8388a413ea1e6a37cbbe406829fb758dfeb3
33 changes: 21 additions & 12 deletions compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Dead Instruction Elimination (DIE) pass: Removes any instruction without side-effects for
//! which the results are unused.
use fxhash::FxHashSet as HashSet;
use fxhash::{FxHashMap as HashMap, FxHashSet as HashSet};
use im::Vector;
use noirc_errors::Location;
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
@@ -193,19 +193,28 @@ impl Context {
}

fn remove_rc_instructions(self, dfg: &mut DataFlowGraph) {
for (rc, block) in self.rc_instructions {
let value = match &dfg[rc] {
Instruction::IncrementRc { value } => *value,
Instruction::DecrementRc { value } => *value,
other => {
unreachable!("Expected IncrementRc or DecrementRc instruction, found {other:?}")
let unused_rc_values_by_block: HashMap<BasicBlockId, HashSet<InstructionId>> =
self.rc_instructions.into_iter().fold(HashMap::default(), |mut acc, (rc, block)| {
let value = match &dfg[rc] {
Instruction::IncrementRc { value } => *value,
Instruction::DecrementRc { value } => *value,
other => {
unreachable!(
"Expected IncrementRc or DecrementRc instruction, found {other:?}"
)
}
};

if !self.used_values.contains(&value) {
acc.entry(block).or_default().insert(rc);
}
};
acc
});

// This could be more efficient if we have to remove multiple instructions in a single block
if !self.used_values.contains(&value) {
dfg[block].instructions_mut().retain(|instruction| *instruction != rc);
}
for (block, instructions_to_remove) in unused_rc_values_by_block {
dfg[block]
.instructions_mut()
.retain(|instruction| !instructions_to_remove.contains(instruction));
}
}