From 704d9b8ee097f4722e2e6140eb80691fbf9c4f22 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 28 Jul 2023 07:27:01 +0000 Subject: [PATCH 1/2] fix: Initialize Value::Array of type Slice --- .../src/brillig/brillig_gen/brillig_block.rs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs index 4de052aad9d..c7779533a8a 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs @@ -800,10 +800,29 @@ impl<'block> BrilligBlock<'block> { value_id, dfg, ); - let heap_array = self.function_context.extract_heap_array(new_variable); - self.brillig_context - .allocate_fixed_length_array(heap_array.pointer, heap_array.size); + // Initialize the variable + let pointer = match new_variable { + RegisterOrMemory::HeapArray(heap_array) => { + self.brillig_context + .allocate_fixed_length_array(heap_array.pointer, array.len()); + + heap_array.pointer + } + RegisterOrMemory::HeapVector(heap_vector) => { + self.brillig_context + .const_instruction(heap_vector.size, array.len().into()); + self.brillig_context + .allocate_array_instruction(heap_vector.pointer, heap_vector.size); + + heap_vector.pointer + } + _ => unreachable!( + "ICE: Cannot initialize array value created as {new_variable:?}" + ), + }; + + // Write the items // Allocate a register for the iterator let iterator_register = self.brillig_context.make_constant(0_usize.into()); @@ -811,11 +830,7 @@ impl<'block> BrilligBlock<'block> { for element_id in array.iter() { let element_variable = self.convert_ssa_value(*element_id, dfg); // Store the item in memory - self.store_variable_in_array( - heap_array.pointer, - iterator_register, - element_variable, - ); + self.store_variable_in_array(pointer, iterator_register, element_variable); // Increment the iterator self.brillig_context.usize_op_in_place(iterator_register, BinaryIntOp::Add, 1); } From 5c10ddb522ae44a0127fa248722668e69445e2ab Mon Sep 17 00:00:00 2001 From: sirasistant Date: Fri, 28 Jul 2023 07:34:43 +0000 Subject: [PATCH 2/2] test: improved brillig test after bug fix --- .../brillig_slices/src/main.nr | 98 ++++++++++--------- 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_slices/src/main.nr b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_slices/src/main.nr index 7e4e8729199..34a9afcd515 100644 --- a/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_slices/src/main.nr +++ b/crates/nargo_cli/tests/test_data_ssa_refactor/brillig_slices/src/main.nr @@ -2,71 +2,75 @@ use dep::std::slice; use dep::std; unconstrained fn main(x: Field, y: Field) { - // Mark it as mut so the compiler doesn't simplify the following operations - // But don't reuse the mut slice variable until this is fixed https://github.com/noir-lang/noir/issues/1931 - let slice: [Field] = [y, x]; + let mut slice: [Field] = [y, x]; assert(slice.len() == 2); - let mut pushed_back_slice = slice.push_back(7); - assert(pushed_back_slice.len() == 3); - assert(pushed_back_slice[0] == y); - assert(pushed_back_slice[1] == x); - assert(pushed_back_slice[2] == 7); + slice = slice.push_back(7); + assert(slice.len() == 3); + assert(slice[0] == y); + assert(slice[1] == x); + assert(slice[2] == 7); // Array set on slice target - pushed_back_slice[0] = x; - pushed_back_slice[1] = y; - pushed_back_slice[2] = 1; - - assert(pushed_back_slice[0] == x); - assert(pushed_back_slice[1] == y); - assert(pushed_back_slice[2] == 1); - - assert(slice.len() == 2); - - let pushed_front_slice = pushed_back_slice.push_front(2); - assert(pushed_front_slice.len() == 4); - assert(pushed_front_slice[0] == 2); - assert(pushed_front_slice[1] == x); - assert(pushed_front_slice[2] == y); - assert(pushed_front_slice[3] == 1); - - let (item, popped_front_slice) = pushed_front_slice.pop_front(); + slice[0] = x; + slice[1] = y; + slice[2] = 1; + + assert(slice[0] == x); + assert(slice[1] == y); + assert(slice[2] == 1); + + slice = push_front_to_slice(slice, 2); + assert(slice.len() == 4); + assert(slice[0] == 2); + assert(slice[1] == x); + assert(slice[2] == y); + assert(slice[3] == 1); + + let (item, popped_front_slice) = slice.pop_front(); + slice = popped_front_slice; assert(item == 2); - assert(popped_front_slice.len() == 3); - assert(popped_front_slice[0] == x); - assert(popped_front_slice[1] == y); - assert(popped_front_slice[2] == 1); + assert(slice.len() == 3); + assert(slice[0] == x); + assert(slice[1] == y); + assert(slice[2] == 1); - let (popped_back_slice, another_item) = popped_front_slice.pop_back(); + let (popped_back_slice, another_item) = slice.pop_back(); + slice = popped_back_slice; assert(another_item == 1); - assert(popped_back_slice.len() == 2); - assert(popped_back_slice[0] == x); - assert(popped_back_slice[1] == y); + assert(slice.len() == 2); + assert(slice[0] == x); + assert(slice[1] == y); - let inserted_slice = popped_back_slice.insert(1, 2); - assert(inserted_slice.len() == 3); - assert(inserted_slice[0] == x); - assert(inserted_slice[1] == 2); - assert(inserted_slice[2] == y); + slice = slice.insert(1, 2); + assert(slice.len() == 3); + assert(slice[0] == x); + assert(slice[1] == 2); + assert(slice[2] == y); - let (removed_slice, should_be_2) = inserted_slice.remove(1); + let (removed_slice, should_be_2) = slice.remove(1); + slice = removed_slice; assert(should_be_2 == 2); - assert(removed_slice.len() == 2); - assert(removed_slice[0] == x); - assert(removed_slice[1] == y); + assert(slice.len() == 2); + assert(slice[0] == x); + assert(slice[1] == y); - let (slice_with_only_x, should_be_y) = removed_slice.remove(1); + let (slice_with_only_x, should_be_y) = slice.remove(1); + slice = slice_with_only_x; assert(should_be_y == y); - assert(slice_with_only_x.len() == 1); - assert(removed_slice[0] == x); + assert(slice.len() == 1); + assert(slice[0] == x); - let (empty_slice, should_be_x) = slice_with_only_x.remove(0); + let (empty_slice, should_be_x) = slice.remove(0); assert(should_be_x == x); assert(empty_slice.len() == 0); } +// Tests slice passing to/from functions +unconstrained fn push_front_to_slice(slice: [T], item: T) -> [T] { + slice.push_front(item) +} \ No newline at end of file