-
Notifications
You must be signed in to change notification settings - Fork 200
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
feat(optimization): Perform array gets as early as possible #5762
Conversation
Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
…into jf/move-array-gets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requesting changes just for some discussion.
I'm a little concerned about this PR as it's similar to my "bubble up" optimisation which does the same operation but for constrain instructions. This resulted in situations where we ended up getting tests failing due to the noir code not being executed in the order which a user would expect.
I'm unsure how long we can maintain this guarantee however so we may need to open discussions on breaking this.
@TomAFrench I'm going to try changing the array merge optimization directly for now to delay the array sets there instead of moving the array gets later. This is the first approach I tried and thought it didn't work initially but I think now that the noir program was just getting cached from a previous version of the compiler. So I'll try again and hopefully we'll see similar improvements. I'll also note that the conditional_1 test failure did not occur when I used a smaller constant value for that array merge optimization, like the previous 2. Not sure why that'd be causing the failure. |
Changes to circuit sizes
🧾 Summary (10% most significant diffs)
Full diff report 👇
|
Running into some difficulties experimenting with this PR. Most notably is a difference in semantics between SSA Pre and Post-Flattening. When optimizing array sets we optimize them if the array and index are known, but we never check the current enable side effects value to see if the array set may be disabled. This is fine Pre-flattening since control flow prevents referring to these directly if it is not what is wanted. Post flattening however it should be valid to optimize an array merge of the following:
to:
Since the then branch should be disabled in the else case, we should expect |
Going to pause work on this PR until #5782 is fixed. I'm also running into it with the current approach in this PR which is fairly similar to what we currently use. The nested dynamic arrays test is failing with this panic currently. |
Hmm I'm surprised changing the array merge optimization to use |
I'm closing this for now since the results are currently negative. We can retry similar optimizations in another PR later if wanted. |
Description
Problem*
Resolves #5027
(#5501 is a separate issue)
Summary*
Due to other optimizations out of the user's control, programs can often end up with sequences resembling the following:
Where we have an
array_set
that cannot be made mutable because there is still anarray_get
later to the unmutated array.To fix this, I've added another pass to move array gets as early as possible in the program. The earliest they can be moved is always just after their last ValueId dependency was inserted. This usually frees up any array_sets to be made mutable afterward.
Additional Context
The program
as well as the new test in
execution_success/array_get_optimization
now have 0 non-mutable array sets.Not much change from most tests. I think this pattern was mostly a result of the array merge optimization to only merge changed indices since this is the only optimization that inserts array sets.
Documentation*
Check one:
PR Checklist*
cargo fmt
on default settings.