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

chore: Optimize mutating arrays in if statements #4695

Closed
wants to merge 3 commits into from

Conversation

jfecher
Copy link
Contributor

@jfecher jfecher commented Apr 2, 2024

Description

Problem*

Resolves #4692

Summary*

Tries to make merging of array values during flattening smarter by keeping track of only which indices may change.

Note that pinning which indices actually change is much more difficult since these arrays are in references which are loaded from and stored to. This'd require mem2reg and alias analysis to do. So I instead opted for an approximation: we track which indices are changed for each array type within an if branch instead.

If the indices that may be stored to are known, and this list is less than the array's length, then we store to only these indices instead.

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@jfecher jfecher changed the title First pass of tracking which indices are changed by type chore: Optimize mutating arrays in if statements Apr 2, 2024
Copy link
Contributor

github-actions bot commented Apr 3, 2024

Changes to circuit sizes

Generated at commit: 19dbb05135aac3f6c97c0e18853db0fd3c6b365b, compared to commit: 463fb7712b74c3afaeb87597a700121619850386

🧾 Summary (10% most significant diffs)

Program ACIR opcodes (+/-) % Circuit size (+/-) %
regression_4449 +208,244 ❌ +3001.93% +1,102,985 ❌ +392.93%
sha2_byte +8,995 ❌ +51.52% +45,620 ❌ +51.56%

Full diff report 👇
Program ACIR opcodes (+/-) % Circuit size (+/-) %
regression_4449 215,181 (+208,244) +3001.93% 1,383,695 (+1,102,985) +392.93%
sha2_byte 26,455 (+8,995) +51.52% 134,107 (+45,620) +51.56%
poseidon_bn254_hash 4,922 (+3) +0.06% 8,494 (+2,142) +33.72%
conditional_1 7,425 (+254) +3.54% 38,799 (0) 0.00%
hashmap 215,913 (-2,956) -1.35% 488,308 (-3,014) -0.61%
regression 120 (-80) -40.00% 3,091 (-79) -2.49%

@jfecher
Copy link
Contributor Author

jfecher commented Apr 15, 2024

Closing this in favor of #4716

@jfecher jfecher closed this Apr 15, 2024
github-merge-queue bot pushed a commit that referenced this pull request 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 <[email protected]>
Co-authored-by: vezenovm <[email protected]>
@TomAFrench TomAFrench deleted the jf/opt-array-merging branch November 20, 2024 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flattening pass merges entire arrays even if only one index was changed
1 participant