From 4786ddcca256d8786e3ba4c52d76be36375ddf06 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 3 Sep 2024 12:58:02 +0000 Subject: [PATCH] fix: temporary register leaks --- .../brillig/brillig_gen/brillig_black_box.rs | 67 ++++++++++++++---- .../src/brillig/brillig_gen/brillig_block.rs | 69 ++++++++++--------- 2 files changed, 91 insertions(+), 45 deletions(-) diff --git a/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs b/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs index 802d442885f..bd9190c1cfe 100644 --- a/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs +++ b/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs @@ -29,6 +29,7 @@ pub(crate) fn convert_black_box_call( + brillig_context: &mut BrilligContext, + original_array_or_vector: &BrilligVariable, + converted_vector: BrilligVector, + bb_func: &BlackBoxFunc, +) { + match original_array_or_vector { + BrilligVariable::BrilligArray(_) => { + brillig_context.deallocate_register(converted_vector.size); + } + BrilligVariable::BrilligVector(_) => {} + _ => unreachable!( + "ICE: {} expected an array or a vector, but got {:?}", + bb_func.name(), + original_array_or_vector + ), + } +} diff --git a/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs b/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs index a7cb1571e34..4f678303542 100644 --- a/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs +++ b/noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs @@ -810,6 +810,12 @@ impl<'block> BrilligBlock<'block> { // puts the returns into the returned_registers and restores saved_registers self.brillig_context .codegen_post_call_prep_returns_load_registers(&returned_registers, &saved_registers); + + // Reset the register state to the one needed to hold the current available variables + let variables = self.variables.get_available_variables(self.function_context); + let registers = + variables.into_iter().flat_map(|variable| variable.extract_registers()).collect(); + self.brillig_context.set_allocated_registers(registers); } fn validate_array_index( @@ -1794,7 +1800,7 @@ impl<'block> BrilligBlock<'block> { dfg, ); let array = variable.extract_array(); - self.allocate_nested_array(typ, Some(array)); + self.allocate_foreign_call_result_array(typ, array); variable } @@ -1821,40 +1827,39 @@ impl<'block> BrilligBlock<'block> { } } - fn allocate_nested_array( - &mut self, - typ: &Type, - array: Option, - ) -> BrilligVariable { - match typ { - Type::Array(types, size) => { - let array = array.unwrap_or(BrilligArray { - pointer: self.brillig_context.allocate_register(), - size: *size, - rc: self.brillig_context.allocate_register(), - }); - self.brillig_context.codegen_allocate_fixed_length_array(array.pointer, array.size); - self.brillig_context.usize_const_instruction(array.rc, 1_usize.into()); - - let mut index = 0_usize; - for _ in 0..*size { - for element_type in types.iter() { - match element_type { - Type::Array(_, _) => { - let inner_array = self.allocate_nested_array(element_type, None); - let idx = - self.brillig_context.make_usize_constant_instruction(index.into()); - self.brillig_context.codegen_store_variable_in_array(array.pointer, idx, inner_array); - } - Type::Slice(_) => unreachable!("ICE: unsupported slice type in allocate_nested_array(), expects an array or a numeric type"), - _ => (), - } - index += 1; + fn allocate_foreign_call_result_array(&mut self, typ: &Type, array: BrilligArray) { + let Type::Array(types, size) = typ else { + unreachable!("ICE: allocate_foreign_call_array() expects an array, got {typ:?}") + }; + + self.brillig_context.codegen_allocate_fixed_length_array(array.pointer, array.size); + self.brillig_context.usize_const_instruction(array.rc, 1_usize.into()); + + let mut index = 0_usize; + for _ in 0..*size { + for element_type in types.iter() { + match element_type { + Type::Array(_, nested_size) => { + let inner_array = BrilligArray { + pointer: self.brillig_context.allocate_register(), + rc: self.brillig_context.allocate_register(), + size: *nested_size, + }; + self.allocate_foreign_call_result_array(element_type, inner_array); + + let idx = + self.brillig_context.make_usize_constant_instruction(index.into()); + self.brillig_context.codegen_store_variable_in_array(array.pointer, idx, BrilligVariable::BrilligArray(inner_array)); + + self.brillig_context.deallocate_single_addr(idx); + self.brillig_context.deallocate_register(inner_array.pointer); + self.brillig_context.deallocate_register(inner_array.rc); } + Type::Slice(_) => unreachable!("ICE: unsupported slice type in allocate_nested_array(), expects an array or a numeric type"), + _ => (), } - BrilligVariable::BrilligArray(array) + index += 1; } - _ => unreachable!("ICE: allocate_nested_array() expects an array, got {typ:?}"), } }