Skip to content

Commit

Permalink
Don't remove necessary RC instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Nov 21, 2024
1 parent efa5cc4 commit 6a4be88
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ impl Context {
.push(instructions_len - instruction_index - 1);
}
} else {
use Instruction::*;
if matches!(instruction, IncrementRc { .. } | DecrementRc { .. }) {
// We can't remove rc instructions if they're loaded from a reference
// since we'd have no way of knowing whether the reference is still used.
if Self::is_inc_dec_instruction_on_known_array(instruction, &function.dfg) {
self.rc_instructions.push((*instruction_id, block_id));
} else {
instruction.for_each_value(|value| {
Expand Down Expand Up @@ -337,6 +338,21 @@ impl Context {

inserted_check
}

/// True if this is a `Instruction::IncrementRc` or `Instruction::DecrementRc`
/// operating on an array directly from a `Instruction::MakeArray`.
fn is_inc_dec_instruction_on_known_array(
instruction: &Instruction,
dfg: &DataFlowGraph,
) -> bool {
use Instruction::*;
if let IncrementRc { value } | DecrementRc { value } = instruction {
if let Value::Instruction { instruction, .. } = &dfg[*value] {
return matches!(&dfg[*instruction], MakeArray { .. });
}
}
false
}
}

fn instruction_might_result_in_out_of_bounds(
Expand Down

0 comments on commit 6a4be88

Please sign in to comment.