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

feat: Apply optimizations to unconstrained code #2348

Merged
merged 34 commits into from
Sep 1, 2023
Merged

Conversation

jfecher
Copy link
Contributor

@jfecher jfecher commented Aug 16, 2023

Description

Problem*

Resolves #2066
Resolves #2336 - See note

Potentially addresses #2382 and #2355.

Summary*

#2336 was caused by us not removing old copies of functions if we ever failed to inline a function. While looking into it, performing a full reachability analysis to find which functions are still reachable was more difficult than just reworking inlining to count each unconstrained function as an entry point function along with main, and inline into all entry points. Once this was done I could move the generation of brillig to the end of ssa optimizations as it was no longer incompatible with inlining.

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

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

@jfecher jfecher marked this pull request as ready for review August 16, 2023 17:14
Copy link
Contributor

@vezenovm vezenovm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the code introduced here: #2190

crates/noirc_evaluator/src/ssa.rs Show resolved Hide resolved
@jfecher
Copy link
Contributor Author

jfecher commented Aug 16, 2023

Can we remove the code introduced here: #2190

I was thinking this could be a later PR, but sure

vezenovm
vezenovm previously approved these changes Aug 16, 2023
@TomAFrench
Copy link
Member

Can you use the rebuild.sh script to regenerate the commited ACIR once this is ready? You'll need to have nargo installed from this branch in order to do this (run noirup -p ./ from the repository root)

@jfecher
Copy link
Contributor Author

jfecher commented Aug 16, 2023

In a cruel twist of fate, this PR is also blocked on the mem2reg pass needing updates (and potentially more issues).

brillig_references for example contains a function

unconstrained fn add1(x: &mut Field) {
    *x += 1;
}

Which breaks mem2reg since the function is not inlined and it erroneously simplifies instructions after this function call with the old value of x. I was under the impression we didn't want to allow mutable references to be passed to Brillig functions? If we do then mem2reg needs to be updated not only to handle aliases but to handle function calls as well.

@jfecher
Copy link
Contributor Author

jfecher commented Aug 16, 2023

brillig_to_le_bytes also panics with Message: index out of bounds: the len is 31 but the index is 31. I'm not sure why for this one - the final generated SSA looks noticeably more optimized (loops are unrolled) but otherwise equivalent to the original (which also accesses index 31):

After Dead Instruction Elimination:
brillig fn main f0 {
  b0(v0: Field):
    v45 = call to_le_radix(v0, u32 2⁸, u32 31)
    v47 = array_get v45, index Field 0
    v49 = array_get v45, index Field 1
    v51 = array_get v45, index Field 2
    v55 = array_get v45, index Field 31
    return [v47, v49, v51, v55]
}

@vezenovm
Copy link
Contributor

vezenovm commented Aug 16, 2023

brillig_to_le_bytes also panics with Message: index out of bounds: the len is 31 but the index is 31. I'm not sure why for this one - the final generated SSA looks noticeably more optimized (loops are unrolled) but otherwise equivalent to the original (which also accesses index 31):

I think this may be related to an error in the codegen for Brillig radix_instruction. I had to do this extra command for merging of slices (https://github.com/noir-lang/noir/pull/2347/files#diff-77a15e4d5423fb81c916909fd0c5854c33725d6ede78c5694916d8f53f6e739eR862) in order to get the accurate slice len for brillig_to_le_bytes. It looks like I also need to update the target_vector.size as otherwise memory can be overwritten.

@jfecher
Copy link
Contributor Author

jfecher commented Aug 16, 2023

@vezenovm do you mind breaking that change into a separate PR?

@vezenovm
Copy link
Contributor

do you mind breaking that change into a separate PR?

Will do

@ludamad
Copy link
Collaborator

ludamad commented Aug 16, 2023

I was under the impression we didn't want to allow mutable references to be passed to Brillig functions?

It's probably not the most sensible to have a mutable reference and change a bunch of constrained data models in Brillig, but within Brillig I would imagine you would want to (especially if Brillig is the execution model for externally constrained e.g. Aztec public fn's)

jfecher and others added 4 commits August 22, 2023 15:23
* master: (34 commits)
  chore: Decouple `noirc_abi` from frontend by introducing `PrintableType` (#2373)
  feat(brillig): Added locations for brillig artifacts (#2415)
  feat: Report compilation warnings before errors (#2398)
  chore: Rework `CrateGraph` to only have one root crate (#2391)
  chore: clippy fix (#2408)
  chore(deps): bump rustls-webpki from 0.101.1 to 0.101.4 (#2404)
  fix(acir): Attach locations to MemoryOps in ACIR (#2389)
  feat: Use equivalence information from equality assertions to simplify circuit (#2378)
  chore: fix body expr span (#2402)
  feat(attributes): enable custom attributes (#2395)
  chore: Remove `serde` from `noirc_frontend` (#2390)
  chore: allow parenthesizing in two type locations  (#2388)
  chore(ci): automatically delete cache entries associated with closed PRs (#2342)
  feat: Perform more checks for compile-time arithmetic (#2380)
  chore: Remove `noirc_abi::FunctionSignature` and define in terms of HIR (#2372)
  feat: Update to `acvm` 0.22.0 (#2363)
  chore: Update committed ACIR artifacts (#2376)
  feat(ssa): Merge slices in if statements with witness conditions (#2347)
  chore: Separate frontend `Visibility` and `Distinctness` from the ABI (#2369)
  feat: add syntax for specifying function type environments (#2357)
  ...
@jfecher jfecher force-pushed the jf/optimize-brillig branch from df467e2 to 080d4b4 Compare August 25, 2023 19:21
@jfecher
Copy link
Contributor Author

jfecher commented Aug 25, 2023

Was using this PR to experiment with some mem2reg changes on top of the current PR, hence the additional changes here. Optimizing brillig_references necessitated some additional changes. I'll open another PR for those changes when ready, although this PR is failing without them.

@jfecher
Copy link
Contributor Author

jfecher commented Aug 30, 2023

This PR is ready for review now

vezenovm
vezenovm previously approved these changes Sep 1, 2023
Copy link
Contributor

@vezenovm vezenovm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some non-blocking nits, LGTM

crates/noirc_evaluator/src/ssa/opt/inlining.rs Outdated Show resolved Hide resolved
crates/noirc_evaluator/src/ssa/opt/mem2reg.rs Outdated Show resolved Hide resolved
crates/noirc_evaluator/src/ssa/opt/mem2reg.rs Outdated Show resolved Hide resolved
Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
jfecher and others added 2 commits September 1, 2023 09:50
Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
@vezenovm
Copy link
Contributor

vezenovm commented Sep 1, 2023

Just need the ACIR artifacts to be reconciled and I can approve

@jfecher
Copy link
Contributor Author

jfecher commented Sep 1, 2023

@vezenovm should be good now

@vezenovm vezenovm enabled auto-merge September 1, 2023 15:27
@vezenovm vezenovm added this pull request to the merge queue Sep 1, 2023
Merged via the queue into master with commit 8e0f6c4 Sep 1, 2023
@vezenovm vezenovm deleted the jf/optimize-brillig branch September 1, 2023 15:57
TomAFrench added a commit that referenced this pull request Sep 1, 2023
* master: (47 commits)
  fix: Initialize structs during def collection, not name resolution (#2528)
  feat: Apply optimizations to unconstrained code (#2348)
  chore(ci): Distinguish between expected failures and compiler panics in `compile_failure` tests. (#2518)
  chore: improve types in `acvm-backend-barretenberg` (#2516)
  feat(aztec_noir): abstract kernel return types (#2521)
  chore: remove usage of `Backend` trait (#2514)
  chore: delete `ProveAndVerifyCommand` (#2520)
  chore: Remove dead code from `acvm_backend_barretenberg` (#2512)
  chore: only install `tokio-util` dependency on windows (#2425)
  chore(aztec_noir):  imply the open keyword (#2508)
  chore: pull `acvm-backend-barretenberg` into main Noir repo (#2495)
  chore: clippy fix (#2507)
  chore: check if the noir aztec library is installed (#2505)
  chore: update ACIR artifacts (#2503)
  chore!: Update to `acvm-backend-barretenberg` v0.12.0 (#2377)
  fix: Bring back accidentally deleted double_verify_proof test. (#2501)
  chore(aztec_noir): import aztec library if not found yet (#2492)
  chore(abi)!: Replace struct name with fully qualified struct path (#2374)
  chore!: Remove keys from preprocessed artifacts (#2283)
  chore(noir): Release 0.10.5 (#2482)
  ...
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.

Cannot iterate on slices Apply ssa optimizations to brillig functions
5 participants