Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct result when assigning shared arrays in unconstrained code #4210

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ impl FunctionBuilder {
}
}
Type::Array(..) | Type::Slice(..) => {
self.insert_instruction(Instruction::IncrementRc { value }, None);
// If there are nested arrays or slices, we wait until ArrayGet
// is issued to increment the count of that array.
self.insert_instruction(Instruction::IncrementRc { value }, None);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ impl<'a> FunctionContext<'a> {
new_value.for_each(|value| {
let value = value.eval(self);
array = self.builder.insert_array_set(array, index, value);
self.builder.increment_array_reference_count(array);
index = self.builder.insert_binary(index, BinaryOp::Add, one);
});
array
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
/// loop_end():
/// ... This is the current insert point after codegen_for finishes ...
fn codegen_for(&mut self, for_expr: &ast::For) -> Result<Values, RuntimeError> {
let loop_entry = self.builder.insert_block();

Check warning on line 473 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
let loop_body = self.builder.insert_block();
let loop_end = self.builder.insert_block();

Expand Down Expand Up @@ -524,7 +524,7 @@
/// v2 = ... codegen b ...
/// br end_if(v2)
/// end_if(v3: ?): // Type of v3 matches the type of a and b
/// ... This is the current insert point after codegen_if finishes ...

Check warning on line 527 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
///
/// As another example, the expression `if cond { a }` is codegen'd as:
///
Expand All @@ -537,7 +537,7 @@
/// ... This is the current insert point after codegen_if finishes ...
fn codegen_if(&mut self, if_expr: &ast::If) -> Result<Values, RuntimeError> {
let condition = self.codegen_non_tuple_expression(&if_expr.condition)?;

Check warning on line 540 in compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (brif)
let then_block = self.builder.insert_block();
let else_block = self.builder.insert_block();

Expand Down Expand Up @@ -678,6 +678,11 @@
let lhs = self.extract_current_value(&assign.lvalue)?;
let rhs = self.codegen_expression(&assign.expression)?;

rhs.clone().for_each(|value| {
let value = value.eval(self);
self.builder.increment_array_reference_count(value);
});

self.assign_new_value(lhs, rhs);
Ok(Self::unit_value())
}
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/brillig_cow_assign/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "brillig_cow_assign"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
items_to_update = 10
index = 6
23 changes: 23 additions & 0 deletions test_programs/execution_success/brillig_cow_assign/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
global N = 10;

unconstrained fn main() {
let mut arr = [0; N];
let mut mid_change = arr;

for i in 0..N {
if i == N / 2 {
mid_change = arr;
}
arr[i] = 27;
}

// Expect:
// arr = [27, 27, 27, 27, 27, 27, 27, 27, 27, 27]
// mid_change = [27, 27, 27, 27, 27, 0, 0, 0, 0, 0]

let modified_i = N / 2 + 1;
assert_eq(arr[modified_i], 27);

// Fail here!
assert(mid_change[modified_i] != 27);
}
Loading