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: Use equivalence information from equality assertions to simplify circuit #2378

Merged
merged 3 commits into from
Aug 22, 2023

Conversation

TomAFrench
Copy link
Member

@TomAFrench TomAFrench commented Aug 20, 2023

Description

Problem*

Partially resolves #1431

Summary*

When we assert two AcirVars are equal, we now use this equivalence to use the simpler of the two values in future.

This PR will be a lot more useful once #2137 is merged as currently all of our assertions are against 1.

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.

@TomAFrench TomAFrench changed the base branch from assert-eq-builtin to master August 21, 2023 18:38
@TomAFrench TomAFrench marked this pull request as ready for review August 21, 2023 18:40
@jfecher
Copy link
Contributor

jfecher commented Aug 22, 2023

Should this be an SSA pass instead? It seems our acir-gen pass is getting increasingly complex as we add more optimizations to it.

@TomAFrench
Copy link
Member Author

Hmm. I agree it would be best to make this an SSA pass as we're be able to use the information on the two ValueIds being equivalent from the beginning of the circuit. I'd need to think more on how you would do this.

@kevaundray kevaundray added this pull request to the merge queue Aug 22, 2023
@TomAFrench
Copy link
Member Author

TomAFrench commented Aug 22, 2023

It's a bit more difficult to do this in SSA as it doesn't know the difference between a value which is a single witness and a value which is a complicated expression.

I think that making an SSA pass to correctly handle this program may be difficult.

fn main (x: u32, y: u32, z: u32) {
    let yz = y * z;
    assert_eq(x, yz);
    assert_eq(x * yz, 25);
}

If we do it in acir-gen then we can easily see that x is simpler than yz so we can replace the last line with assert_eq(x*x, 25). SSA has no information on the internal structure of x and yz (except for if we rebuild this from scratch by backtracking to params and constants) so this is more difficult.

@jfecher
Copy link
Contributor

jfecher commented Aug 22, 2023

SSA has no information on the internal structure of x and yz (except for if we rebuild this from scratch by backtracking to params and constants) so this is more difficult.

I'd think checking whether x is a block param and whether yz is an instruction result could be sufficient, but it doesn't need to block this PR. An SSA pass would be a larger code change as well.

Merged via the queue into master with commit ec5b021 Aug 22, 2023
16 checks passed
@kevaundray kevaundray deleted the use-equivalence-from-asserts branch August 22, 2023 18:37
TomAFrench added a commit that referenced this pull request Aug 22, 2023
* master:
  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)
TomAFrench added a commit that referenced this pull request Aug 23, 2023
* 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)
  ...
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.

Store equality sets in AcirContext
3 participants