Skip to content

Commit

Permalink
Avoid issuing inc & dec_rc outside of unconstrained code
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jan 3, 2025
1 parent 54ca620 commit a34c46b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
44 changes: 24 additions & 20 deletions compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,29 +467,33 @@ impl FunctionBuilder {
///
/// Returns whether a reference count instruction was issued.
fn update_array_reference_count(&mut self, value: ValueId, increment: bool) -> bool {
match self.type_of_value(value) {
Type::Numeric(_) => false,
Type::Function => false,
Type::Reference(element) => {
if element.contains_an_array() {
let reference = value;
let value = self.insert_load(reference, element.as_ref().clone());
self.update_array_reference_count(value, increment);
true
} else {
false
if self.current_function.runtime().is_brillig() {
match self.type_of_value(value) {
Type::Numeric(_) => false,
Type::Function => false,
Type::Reference(element) => {
if element.contains_an_array() {
let reference = value;
let value = self.insert_load(reference, element.as_ref().clone());
self.update_array_reference_count(value, increment);
true
} else {
false
}
}
}
Type::Array(..) | Type::Slice(..) => {
// If there are nested arrays or slices, we wait until ArrayGet
// is issued to increment the count of that array.
if increment {
self.insert_inc_rc(value);
} else {
self.insert_dec_rc(value);
Type::Array(..) | Type::Slice(..) => {
// If there are nested arrays or slices, we wait until ArrayGet
// is issued to increment the count of that array.
if increment {
self.insert_inc_rc(value);
} else {
self.insert_dec_rc(value);
}
true
}
true
}
} else {
false
}
}

Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/remove_unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use fxhash::FxHashSet as HashSet;
use crate::ssa::{
ir::{
function::{Function, FunctionId},
instruction::Instruction,
value::Value,
},
ssa_gen::Ssa,
Expand Down Expand Up @@ -60,7 +61,11 @@ fn used_functions(func: &Function) -> BTreeSet<FunctionId> {
let block = &func.dfg[block_id];

for instruction_id in block.instructions() {
func.dfg[*instruction_id].for_each_value(&mut find_functions);
let instruction = &func.dfg[*instruction_id];

if matches!(instruction, Instruction::Store { .. } | Instruction::Call { .. }) {
instruction.for_each_value(&mut find_functions);
}
}

block.unwrap_terminator().for_each_value(&mut find_functions);
Expand Down

0 comments on commit a34c46b

Please sign in to comment.