Skip to content

Commit

Permalink
feat: replace quadratic removal of rc instructions (#10416)
Browse files Browse the repository at this point in the history
Copy of noir-lang/noir#6705. This PR cuts the
time to build `rollup-base-public` by 75%.
  • Loading branch information
TomAFrench authored Dec 5, 2024
1 parent 427cf59 commit 9d833c5
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions noir/noir-repo/compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,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));
}
}

Expand Down

0 comments on commit 9d833c5

Please sign in to comment.