Skip to content

Commit

Permalink
fix: Consider constants as used values to keep their rc ops (#6122)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #6121

## Summary\*

Die was not considering constants as used. As a result, the reference
counter operations on constants were removed.
This created a correctness issue.

## 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
sirasistant authored Sep 23, 2024
1 parent 5598059 commit 1217005
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,17 @@ impl Context {
self.used_values.insert(value_id);
}
Value::Array { array, .. } => {
self.used_values.insert(value_id);
for elem in array {
self.mark_used_instruction_results(dfg, *elem);
}
}
Value::Param { .. } => {
self.used_values.insert(value_id);
}
Value::NumericConstant { .. } => {
self.used_values.insert(value_id);
}
_ => {
// Does not comprise of any instruction results
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "brillig_constant_reference_regression"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sorted_index = ["1", "0"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
unconstrained fn main(sorted_index: [u32; 2]) {
let original = [
55,
11
];

let mut sorted = original; // Stores the constant "original" into the sorted reference

for i in 0..2 {
let index = sorted_index[i];
let value = original[index];
sorted[i] = value; // On first iteration, we should not mutate the original constant array, RC should be > 1
}

assert_eq(sorted[1], 55);
}

0 comments on commit 1217005

Please sign in to comment.