Skip to content

Commit

Permalink
feat: Avoid incrementing reference counts in some cases (#6568)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Nov 21, 2024
1 parent 7a077de commit 01c4a9f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 8 additions & 3 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl<'a> FunctionContext<'a> {
/// Always returns a Value::Mutable wrapping the allocate instruction.
pub(super) fn new_mutable_variable(&mut self, value_to_store: ValueId) -> Value {
let element_type = self.builder.current_function.dfg.type_of_value(value_to_store);
self.builder.increment_array_reference_count(value_to_store);
let alloc = self.builder.insert_allocate(element_type);
self.builder.insert_store(alloc, value_to_store);
let typ = self.builder.type_of_value(value_to_store);
Expand Down Expand Up @@ -735,7 +736,6 @@ impl<'a> FunctionContext<'a> {
// Reference counting in brillig relies on us incrementing reference
// counts when arrays/slices are constructed or indexed.
// Thus, if we dereference an lvalue which happens to be array/slice we should increment its reference counter.
self.builder.increment_array_reference_count(reference);
self.builder.insert_load(reference, element_type).into()
})
}
Expand Down Expand Up @@ -916,7 +916,10 @@ impl<'a> FunctionContext<'a> {
let parameters = self.builder.current_function.dfg.block_parameters(entry).to_vec();

for parameter in parameters {
self.builder.increment_array_reference_count(parameter);
// Avoid reference counts for immutable arrays that aren't behind references.
if self.builder.current_function.dfg.value_is_reference(parameter) {
self.builder.increment_array_reference_count(parameter);
}
}

entry
Expand All @@ -933,7 +936,9 @@ impl<'a> FunctionContext<'a> {
dropped_parameters.retain(|parameter| !terminator_args.contains(parameter));

for parameter in dropped_parameters {
self.builder.decrement_array_reference_count(parameter);
if self.builder.current_function.dfg.value_is_reference(parameter) {
self.builder.decrement_array_reference_count(parameter);
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,11 @@ impl<'a> FunctionContext<'a> {
values = values.map(|value| {
let value = value.eval(self);

// Make sure to increment array reference counts on each let binding
self.builder.increment_array_reference_count(value);

Tree::Leaf(if let_expr.mutable {
self.new_mutable_variable(value)
} else {
// `new_mutable_variable` already increments rcs internally
self.builder.increment_array_reference_count(value);
value::Value::Normal(value)
})
});
Expand Down

0 comments on commit 01c4a9f

Please sign in to comment.