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

Flattening pass merges entire arrays even if only one index was changed #4692

Closed
jfecher opened this issue Apr 2, 2024 · 2 comments · Fixed by #4716
Closed

Flattening pass merges entire arrays even if only one index was changed #4692

jfecher opened this issue Apr 2, 2024 · 2 comments · Fixed by #4716
Assignees
Labels
bug Something isn't working

Comments

@jfecher
Copy link
Contributor

jfecher commented Apr 2, 2024

Aim

Mutating one index in an array inside a conditional:

if cond {
    array[3] = 4;
}

Expected Behavior

Only the one index that was changed needs to be merged from the implicit "else" case where it was unchanged.

Bug

The flattening pass instead merges every value inside array which can cause exponential blowup if the array is mutated inside a loop like this.

To Reproduce

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@jfecher jfecher added the bug Something isn't working label Apr 2, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Apr 2, 2024
@jfecher
Copy link
Contributor Author

jfecher commented Apr 2, 2024

@jfecher
Copy link
Contributor Author

jfecher commented Apr 2, 2024

Initial difficulties in working on fixing this: it is hard for the flattening pass to track these changes since the arrays are being loaded from and stored to with references. This is normally the domain of mem2reg which itself is already a fairly complex pass. For flattening to reason about which allocations are loaded to, modified at certain indices, then stored back to would likely require alias tracking for example.

@TomAFrench TomAFrench moved this from 📋 Backlog to 🏗 In progress in Noir May 2, 2024
github-merge-queue bot pushed a commit that referenced this issue May 3, 2024
# Description

## Problem\*

Alternate version of #4695

Resolves #4692

## Summary\*

- Flattening produces a new `IfElse` instruction to merge values instead
of immediately merging them
- `IfElse` instructions are removed by actually merging the values once
flattening and mem2reg is done. This is done in a new remove_if_else
pass.
- Since slice capacity tracking was done in flattening before and
required to merge values, I've had to move this to the remove_if_else
pass instead. It is somewhat simpler since we don't have to track
loads/stores.

## Additional Context

Currently only failing the nested_array_in_slice and
nested_array_dynamic tests.
The array being asserted somehow has the value `[1, 2, 3]` despite none
of these values being set in the program.

There are currently no actual optimizations here, this is just
structural changes so it should otherwise be equivalent.

## 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 <tom@tomfren.ch>
Co-authored-by: vezenovm <mvezenov@gmail.com>
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Noir May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: ✅ Done
2 participants