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(perf): Remove inc_rc/dec_rc instructions that follow a removed load in mem2reg #6092

Closed
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
40 changes: 35 additions & 5 deletions compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
//!
//! Repeating this algorithm for each block in the function in program order should result in
//! optimizing out most known loads. However, identifying all aliases correctly has been proven
//! undecidable in general (Landi, 1992). So this pass will not always optimize out all loads

Check warning on line 69 in compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Landi)
//! that could theoretically be optimized out. This pass can be performed at any time in the
//! SSA optimization pipeline, although it will be more successful the simpler the program's CFG is.
//! This pass is currently performed several times to enable other passes - most notably being
Expand Down Expand Up @@ -154,7 +154,7 @@
/// Load and Store instructions that should be removed at the end of the pass.
///
/// We avoid removing individual instructions as we go since removing elements
/// from the middle of Vecs many times will be slower than a single call to `retain`.

Check warning on line 157 in compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Vecs)
instructions_to_remove: HashSet<InstructionId>,

/// Track a value's last load across all blocks.
Expand Down Expand Up @@ -364,10 +364,12 @@
let result = self.inserter.function.dfg.instruction_results(instruction)[0];
references.remember_dereference(self.inserter.function, address, result);

let mut known_value_exists = false;
// If the load is known, replace it with the known value and remove the load
if let Some(value) = references.get_known_value(address) {
self.inserter.map_value(result, value);
self.instructions_to_remove.insert(instruction);
known_value_exists = true;
} else {
references.mark_value_used(address, self.inserter.function);

Expand Down Expand Up @@ -420,12 +422,13 @@
self.inserter.map_value(result, previous_result);
self.instructions_to_remove.insert(instruction);
}
} else if !known_value_exists {
// We want to set the load for every load even if the address has a known value
// and the previous load instruction was removed.
// We are safe to still remove a repeat load in this case as we are mapping from the current load's
// result to the previous load, which if it was removed should already have a mapping to the known value.
references.set_last_load(address, instruction);
}
// We want to set the load for every load even if the address has a known value
// and the previous load instruction was removed.
// We are safe to still remove a repeat load in this case as we are mapping from the current load's
// result to the previous load, which if it was removed should already have a mapping to the known value.
references.set_last_load(address, instruction);
}
Instruction::Store { address, value } => {
let address = self.inserter.function.dfg.resolve(*address);
Expand Down Expand Up @@ -547,6 +550,33 @@
}
self.mark_all_unknown(arguments, references);
}
Instruction::IncrementRc { value } | Instruction::DecrementRc { value } => {
// Account for whether a load has been removed.
// If a load has been removed, there may potentially be an inc_rc/dec_rc instruction
// that follows the load instruction. With the load gone, we can also remove the inc_rc/dec_rc.
let instructions = self.inserter.function.dfg[block_id].instructions();
if instructions.len() < 2 {
return;
}
// We subtract two as we always push an instruction before analyzing it and we want the instruction
// before this increment or decrement rc instruction.
let last_instruction = instructions[instructions.len() - 2];
let value = self.inserter.function.dfg.resolve(*value);
if self.instructions_to_remove.contains(&last_instruction) {
if let Instruction::Load { address } =
self.inserter.function.dfg[last_instruction]
{
let result =
self.inserter.function.dfg.instruction_results(last_instruction)[0];
let result = self.inserter.resolve(result);
// We only want to remove the inc/dec rc if the removed load came from a repeated load
if result == value && references.last_loads.get(&address).is_some() {
self.instructions_to_remove.insert(instruction);
}
}
}
}

_ => (),
}
}
Expand Down
Loading