Skip to content

Commit

Permalink
refactor deallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant committed Mar 13, 2024
1 parent 096ead8 commit 13fbea3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
36 changes: 18 additions & 18 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<'block> BrilligBlock<'block> {
);

self.brillig_context.constrain_instruction(condition, assert_message);
self.brillig_context.deallocate_register(condition.address);
self.brillig_context.deallocate_single_addr(condition);
}
Instruction::Allocate => {
let result_value = dfg.instruction_results(instruction_id)[0];
Expand Down Expand Up @@ -545,7 +545,7 @@ impl<'block> BrilligBlock<'block> {
matches!(endianness, Endian::Big),
);

self.brillig_context.deallocate_register(radix.address);
self.brillig_context.deallocate_single_addr(radix);
}
_ => {
unreachable!("unsupported function call type {:?}", dfg[*func])
Expand Down Expand Up @@ -652,9 +652,9 @@ impl<'block> BrilligBlock<'block> {
);

self.brillig_context.constrain_instruction(condition, assert_message.clone());
self.brillig_context.deallocate_register(condition.address);
self.brillig_context.deallocate_register(left.address);
self.brillig_context.deallocate_register(right.address);
self.brillig_context.deallocate_single_addr(condition);
self.brillig_context.deallocate_single_addr(left);
self.brillig_context.deallocate_single_addr(right);
}
}
Instruction::IncrementRc { value } => {
Expand Down Expand Up @@ -768,9 +768,9 @@ impl<'block> BrilligBlock<'block> {
.constrain_instruction(condition, Some("Array index out of bounds".to_owned()));

if should_deallocate_size {
self.brillig_context.deallocate_register(size_as_register.address);
self.brillig_context.deallocate_single_addr(size_as_register);
}
self.brillig_context.deallocate_register(condition.address);
self.brillig_context.deallocate_single_addr(condition);
}

pub(crate) fn retrieve_variable_from_array(
Expand Down Expand Up @@ -1095,7 +1095,7 @@ impl<'block> BrilligBlock<'block> {
self.update_slice_length(target_len.address, arguments[0], dfg, BinaryIntOp::Add);

self.slice_insert_operation(target_vector, source_vector, converted_index, &items);
self.brillig_context.deallocate_register(converted_index.address);
self.brillig_context.deallocate_single_addr(converted_index);
}
Value::Intrinsic(Intrinsic::SliceRemove) => {
let target_len = match self.variables.define_variable(
Expand Down Expand Up @@ -1148,7 +1148,7 @@ impl<'block> BrilligBlock<'block> {
&removed_items,
);

self.brillig_context.deallocate_register(converted_index.address);
self.brillig_context.deallocate_single_addr(converted_index);
}
_ => unreachable!("ICE: Slice operation not supported"),
}
Expand Down Expand Up @@ -1236,7 +1236,7 @@ impl<'block> BrilligBlock<'block> {
condition,
Some("attempt to add with overflow".to_string()),
);
self.brillig_context.deallocate_register(condition.address);
self.brillig_context.deallocate_single_addr(condition);
}
(BinaryIntOp::Sub, false) => {
let condition =
Expand All @@ -1252,7 +1252,7 @@ impl<'block> BrilligBlock<'block> {
condition,
Some("attempt to subtract with overflow".to_string()),
);
self.brillig_context.deallocate_register(condition.address);
self.brillig_context.deallocate_single_addr(condition);
}
(BinaryIntOp::Mul, false) => {
// Multiplication overflow is only possible for bit sizes > 1
Expand Down Expand Up @@ -1286,11 +1286,11 @@ impl<'block> BrilligBlock<'block> {
condition,
Some("attempt to multiply with overflow".to_string()),
);
ctx.deallocate_register(condition.address);
ctx.deallocate_register(division.address);
ctx.deallocate_single_addr(condition);
ctx.deallocate_single_addr(division);
});
self.brillig_context.deallocate_register(is_right_zero.address);
self.brillig_context.deallocate_register(zero.address);
self.brillig_context.deallocate_single_addr(is_right_zero);
self.brillig_context.deallocate_single_addr(zero);
}
}
_ => {}
Expand All @@ -1316,9 +1316,9 @@ impl<'block> BrilligBlock<'block> {
} else {
let new_variable =
self.variables.allocate_constant(self.brillig_context, value_id, dfg);
let register_index = new_variable.extract_single_addr();

self.brillig_context.const_instruction(register_index, (*constant).into());
self.brillig_context
.const_instruction(new_variable.extract_single_addr(), (*constant).into());
new_variable
}
}
Expand Down Expand Up @@ -1369,7 +1369,7 @@ impl<'block> BrilligBlock<'block> {
);
}

self.brillig_context.deallocate_register(iterator_register.address);
self.brillig_context.deallocate_single_addr(iterator_register);

new_variable
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'block> BrilligBlock<'block> {
BinaryIntOp::Add,
);
self.store_variable_in_array(target_vector.pointer, target_index, *variable);
self.brillig_context.deallocate_register(target_index.address);
self.brillig_context.deallocate_single_addr(target_index);
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ impl<'block> BrilligBlock<'block> {
for (index, variable) in variables_to_insert.iter().enumerate() {
let target_index = self.brillig_context.make_usize_constant(index.into());
self.store_variable_in_array(target_vector.pointer, target_index, *variable);
self.brillig_context.deallocate_register(target_index.address);
self.brillig_context.deallocate_single_addr(target_index);
}

self.brillig_context.deallocate_register(destination_copy_pointer);
Expand Down Expand Up @@ -123,7 +123,7 @@ impl<'block> BrilligBlock<'block> {
for (index, variable) in removed_items.iter().enumerate() {
let target_index = self.brillig_context.make_usize_constant(index.into());
self.retrieve_variable_from_array(source_vector.pointer, target_index, *variable);
self.brillig_context.deallocate_register(target_index.address);
self.brillig_context.deallocate_single_addr(target_index);
}

self.brillig_context.deallocate_register(source_copy_pointer);
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<'block> BrilligBlock<'block> {
BinaryIntOp::Add,
);
self.retrieve_variable_from_array(source_vector.pointer, target_index, *variable);
self.brillig_context.deallocate_register(target_index.address);
self.brillig_context.deallocate_single_addr(target_index);
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'block> BrilligBlock<'block> {
BinaryIntOp::Add,
);
self.store_variable_in_array(target_vector.pointer, target_index, *variable);
self.brillig_context.deallocate_register(target_index.address);
self.brillig_context.deallocate_single_addr(target_index);
}

self.brillig_context.deallocate_register(source_pointer_at_index);
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<'block> BrilligBlock<'block> {
BinaryIntOp::Add,
);
self.retrieve_variable_from_array(source_vector.pointer, target_index, *variable);
self.brillig_context.deallocate_register(target_index.address);
self.brillig_context.deallocate_single_addr(target_index);
}

self.brillig_context.deallocate_register(source_pointer_after_index);
Expand Down
25 changes: 15 additions & 10 deletions compiler/noirc_evaluator/src/brillig/brillig_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl BrilligContext {
// debug_show handled by allocate_array_instruction
let size_register = self.make_usize_constant(size.into());
self.allocate_array_instruction(pointer_register, size_register.address);
self.deallocate_register(size_register.address);
self.deallocate_single_addr(size_register);
}

/// Allocates an array of size contained in size_register and stores the
Expand Down Expand Up @@ -176,7 +176,7 @@ impl BrilligContext {
ReservedRegisters::stack_pointer(),
BinaryIntOp::Add,
);
self.deallocate_register(size_register.address);
self.deallocate_single_addr(size_register);
}

pub(crate) fn allocate_single_addr_reference_instruction(
Expand Down Expand Up @@ -306,8 +306,8 @@ impl BrilligContext {
self.enter_section(exit_loop_section);

// Deallocate our temporary registers
self.deallocate_register(iterator_less_than_iterations.address);
self.deallocate_register(iterator_register.address);
self.deallocate_single_addr(iterator_less_than_iterations);
self.deallocate_single_addr(iterator_register);
}

/// This instruction will issue an if-then branch that will check if the condition is true
Expand Down Expand Up @@ -426,6 +426,11 @@ impl BrilligContext {
pub(crate) fn deallocate_register(&mut self, register_index: MemoryAddress) {
self.registers.deallocate_register(register_index);
}

/// Deallocates the address where the single address variable is stored
pub(crate) fn deallocate_single_addr(&mut self, var: SingleAddrVariable) {
self.deallocate_register(var.address);
}
}

impl BrilligContext {
Expand Down Expand Up @@ -621,7 +626,7 @@ impl BrilligContext {
rhs: input.address,
};
self.push_opcode(opcode);
self.deallocate_register(max.address);
self.deallocate_single_addr(max);
}

/// Processes a foreign call instruction.
Expand Down Expand Up @@ -786,7 +791,7 @@ impl BrilligContext {
BrilligBinaryOp::Integer(BinaryIntOp::And),
);

self.deallocate_register(mask_constant.address);
self.deallocate_single_addr(mask_constant);
}

/// Emits a stop instruction
Expand Down Expand Up @@ -947,7 +952,7 @@ impl BrilligContext {
let const_register = self.make_usize_constant(Value::from(constant));
self.memory_op(operand, const_register.address, destination, op);
// Mark as no longer used for this purpose, frees for reuse
self.deallocate_register(const_register.address);
self.deallocate_single_addr(const_register);
}

/// Utility method to perform a binary instruction with a memory address
Expand Down Expand Up @@ -1077,9 +1082,9 @@ impl BrilligContext {
});

// Deallocate our temporary registers
self.deallocate_register(shifted_field.address);
self.deallocate_register(modulus_field.address);
self.deallocate_register(radix_as_field.address);
self.deallocate_single_addr(shifted_field);
self.deallocate_single_addr(modulus_field);
self.deallocate_single_addr(radix_as_field);

if big_endian {
self.reverse_vector_in_place_instruction(target_vector);
Expand Down
12 changes: 6 additions & 6 deletions compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ impl BrilligContext {
BrilligParameter::Slice(..) => unreachable!("ICE: Cannot deflatten slices"),
}

self.deallocate_register(source_index.address);
self.deallocate_register(target_index.address);
self.deallocate_single_addr(source_index);
self.deallocate_single_addr(target_index);
}
}

Expand Down Expand Up @@ -327,7 +327,7 @@ impl BrilligContext {
returned_pointer,
);

self.deallocate_register(pointer_to_return_data.address);
self.deallocate_single_addr(pointer_to_return_data);
return_data_index += BrilligContext::flattened_size(return_param);
}
BrilligParameter::Slice(..) => {
Expand Down Expand Up @@ -436,8 +436,8 @@ impl BrilligContext {
BrilligParameter::Slice(..) => unreachable!("ICE: Cannot flatten slices"),
}

self.deallocate_register(source_index.address);
self.deallocate_register(target_index.address);
self.deallocate_single_addr(source_index);
self.deallocate_single_addr(target_index);
}
}

Expand All @@ -449,7 +449,7 @@ impl BrilligContext {
flattened_array_pointer,
item_count,
);
self.deallocate_register(item_count.address);
self.deallocate_single_addr(item_count);
}
}
}
Expand Down

0 comments on commit 13fbea3

Please sign in to comment.