Skip to content

Commit

Permalink
fix(ssa): RC correctness issue (#6134)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #6123 

## Summary\*

Still a draft as I want to test it a bit more and am not fully sure if
it is the best solution.

## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [X] I have tested the changes locally.
- [X] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
vezenovm authored Sep 24, 2024
1 parent 684b6cc commit 5b1c896
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ impl<'a> FunctionContext<'a> {
let element_types = Self::convert_type(element_type);
values.map_both(element_types, |value, element_type| {
let reference = value.eval_reference();
// 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "brillig_rc_regression_6123"
type = "bin"
authors = [""]
compiler_version = ">=0.34.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
struct Builder {
note_hashes: BoundedVec<Field, 2>,
nullifiers: BoundedVec<Field, 2>,
}

impl Builder {
fn append_note_hashes_with_logs(&mut self, num_note_hashes: u32) {
let index_offset = self.note_hashes.len();
for i in 0..self.note_hashes.max_len() {
if i < num_note_hashes {
self.add_new_note_hash((index_offset + i) as Field);
}
}
}

fn add_new_note_hash(&mut self, value: Field) {
self.note_hashes.push(value);
}
}

fn swap_items<T, let N: u32>(vec: &mut BoundedVec<T, N>, from_index: u32, to_index: u32) {
let tmp = vec.storage[from_index];
vec.storage[from_index] = vec.storage[to_index];
vec.storage[to_index] = tmp;
}

unconstrained fn main() {
let mut builder = Builder { note_hashes: BoundedVec::new(), nullifiers: BoundedVec::new() };

builder.append_note_hashes_with_logs(2);
builder.nullifiers.storage[1] = 27;
// Get ordered items before shuffling.
let note_hashes = builder.note_hashes.storage;
let original_first_note_hash = note_hashes[0];
// Shuffle.
swap_items(&mut builder.note_hashes, 1, 0);

for i in 0..1 {
assert_eq(note_hashes[i], original_first_note_hash);
}
}
3 changes: 2 additions & 1 deletion tooling/debugger/ignored-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ is_unconstrained
macros
references
regression_4709
reference_only_used_as_alias
reference_only_used_as_alias
brillig_rc_regression_6123

0 comments on commit 5b1c896

Please sign in to comment.