Skip to content

Commit

Permalink
fix: Replace unused ArrayGet/Set with constrain if possibly out of bo…
Browse files Browse the repository at this point in the history
…unds (#5691)

# Description

## Problem

For #5296

## Summary

In DIE, when we find an `ArrayGet/Set` that we could remove, if that
instruction might be an index out of bounds we still remove it but leave
a constrain check too.

## Additional Context

For now the code is a bit messy, I just wanted to get it working first.

Also, it's not working for slices yet because we can't insert out of
bounds checks if we don't know the slice length. For that we'll need to
add the checks like we do for `ArrayGet` for slice (this could be done
in a separate PR, though).

## 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
asterite authored Aug 13, 2024
1 parent eb33d1c commit a87d926
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 10 deletions.
356 changes: 346 additions & 10 deletions compiler/noirc_evaluator/src/ssa/opt/die.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unused_array_get_known_index_out_of_bounds"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let array = [1, 2, 3];
let _ = array[10]; // Index out of bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unused_array_get_unknown_index_out_of_bounds"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "10"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main(x: Field) {
let array = [1, 2, 3];
let _ = array[x]; // Index out of bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unused_array_set_known_index_out_of_bounds"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let mut array = [1, 2, 3];
array[10] = 1; // Index out of bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unused_array_set_unknown_index_out_of_bounds"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "10"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main(x: Field) {
let mut array = [1, 2, 3];
array[x] = 1; // Index out of bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unused_slice_get_known_index_out_of_bounds"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let slice = &[1, 2, 3];
let _ = slice[10]; // Index out of bounds
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unused_slice_get_unknown_index_out_of_bounds"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "10"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main(x: Field) {
let slice = &[1, 2, 3];
let _ = slice[x]; // Index out of bounds
}

0 comments on commit a87d926

Please sign in to comment.