Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(perf): Follow array sets backwards in array set from get optimiz…
…ation (noir-lang#6208) # Description ## Problem\* Part of general effort reduce Brillig bytecode sizes ## Summary\* Follow-up to noir-lang#6207 From comments inside the PR: ``` /// If we have an array set whose value is from an array get on the same array at the same index, /// we can simplify that array set to the array we were looking to perform an array set upon. /// /// Simple case: /// v3 = array_get v1, index v2 /// v5 = array_set v1, index v2, value v3 /// /// If we could not immediately simplify the array set from its value, we can try to follow /// the array set backwards in the case we have constant indices: /// /// v3 = array_get v1, index 1 /// v5 = array_set v1, index 2, value [Field 100, Field 101, Field 102] /// v7 = array_set mut v5, index 1, value v3 /// /// We want to optimize `v7` to `v5`. We see that `v3` comes from an array get to `v1`. We follow `v5` backwards and see an array set /// to `v1` and see that the previous array set occurs to a different constant index. /// /// For each array set: /// - If the index is non-constant we fail the optimization since any index may be changed. /// - If the index is constant and is our target index, we conservatively fail the optimization. /// - Otherwise, we check the array value of the `array_set``. We will refer to this array as array'. /// In the case above, array' is `v1` from `v5 = array set ...` /// - If the original `array_set` value comes from an `array_get`, check the array in that `array_get` against array'. /// - If the two values are equal we can simplify. /// - Continuing the example above, as we have `v3 = array_get v1, index 1`, `v1` is /// what we want to check against array'. We now know we can simplify `v7` to `v5` as it is unchanged. /// - If they are not equal, recur marking the current `array_set` array as the new array id to use in the checks ``` ## 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. --------- Co-authored-by: Tom French <[email protected]>
- Loading branch information