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

fix: don't deduplicate constraints in blocks that are not dominated #6628

Closed
wants to merge 5 commits into from

Conversation

asterite
Copy link
Collaborator

@asterite asterite commented Nov 26, 2024

Description

Problem

Resolves #6588

Summary

Alternative to the approach in #6627. Here we keep track of all the simplifications a value could have (one per block) while #6627 only kept one simplification... which works, but this PR should result in more simplifications 🤞

Additional Context

There are a lot of nested structures in this file and it would be nice to somehow refactor it... but maybe it can be done in a separate PR, now that we have a regression test for the bug.

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.

Copy link
Contributor

github-actions bot commented Nov 26, 2024

Changes to Brillig bytecode sizes

Generated at commit: 4fe6653df717dd423e639058ab444dc30d3e5a45, compared to commit: 7b56904e56d95b88cefcbf3862e822fd3b1c8730

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
fold_call_witness_condition +4 ❌ +3.64%
slice_dynamic_index +12 ❌ +0.48%

Full diff report 👇
Program Brillig opcodes (+/-) %
fold_call_witness_condition 114 (+4) +3.64%
slice_dynamic_index 2,532 (+12) +0.48%

Copy link
Contributor

Changes to number of Brillig opcodes executed

Generated at commit: 4fe6653df717dd423e639058ab444dc30d3e5a45, compared to commit: 7b56904e56d95b88cefcbf3862e822fd3b1c8730

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
fold_call_witness_condition +1 ❌ +1.45%
slice_dynamic_index +4 ❌ +0.09%

Full diff report 👇
Program Brillig opcodes (+/-) %
fold_call_witness_condition 70 (+1) +1.45%
slice_dynamic_index 4,335 (+4) +0.09%
uhashmap 146,340 (-3) -0.00%
hashmap 52,936 (-3) -0.01%

) -> bool {
// These two can never be hoisted as they have a side-effect
// (though it's fine to de-duplicate them, just not fine to hoist them)
if matches!(instruction, Instruction::Constrain(..) | Instruction::RangeCheck { .. }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be enough to do this for Constrain, because that's the one that interacts with simplifications that can replace other instructions' results? Other instructions can have side effects too, but Constrain has special role in constant_folding. I don't see RangeCheck have the same.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think any instruction can be simplified. At least instruction here comes from resolve_instruction where any Instruction type can be returned.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I meant was that we only have simplifications that come from Constrain, and I thought the hoisting of Constrain in the dominator is what's causing trouble further on. Just not sure what RangeCheck would do.

@TomAFrench
Copy link
Member

I'd like to avoid having 2 PRs with 99% of the same code. Especially as #6627 is getting refactorings which disguise the meaningful differences in approach being taken.

Let's park this PR and get the other one merged to fix the correctness bug. We can then revisit how we can maintain more optimizations.

@TomAFrench TomAFrench marked this pull request as draft November 27, 2024 10:33
@asterite
Copy link
Collaborator Author

Sounds good. The other PR also has more refactors and the code is a bit more readable 👍

@asterite asterite closed this Nov 27, 2024
@asterite asterite deleted the ab/constant_folding_bug_alternative branch November 27, 2024 10:40
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.

Cross-block deduplication in constant folding has correctness bugs.
3 participants