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

Optimization: Check whether binary ops can be unchecked based upon the induction variable #7343

Closed
vezenovm opened this issue Feb 10, 2025 · 0 comments · Fixed by #7344
Closed
Assignees
Labels
brillig Unconstrained functions / brillig IR enhancement New feature or request ssa

Comments

@vezenovm
Copy link
Contributor

vezenovm commented Feb 10, 2025

Problem

When performing binary operations we default to checked operations. However, when we know we have a known upper loop bound we can determine whether there will be an overflow at compilation time.
For example:

fn main(six_as_u32: u32) {
    for i in 0..64 {
        let z: u32 = i * 64;
        assert(z != six_as_u32);
    }
}

We know the maximum loop bound is going to be 63 and the 63*64 will fit into a u32. This operation should be unchecked.

However, let's look at the output from noir-profiler execution-opcodes:
Image

45.3% (448 samples) of the executed opcodes comes from i * 64, while only 6.47% (64 samples as expected) are actual Brillig operations. The rest of the operations for i * 64 are from overflow checks.

Happy Case

Get rid of unnecessary overflow checks when we know from the induction variable that the operation will never overflow.
e.g. The above example should only see 64 opcode samples from i * 64.

This PR #6910 is of similar style but only checks induction variables from outer loops and hoists binary ops into the outer loop based upon the induction variable upper bound.

Workaround

None

Workaround Description

There is no ability to workaround this at the moment, this requires compiler changes.

Additional Context

No response

Project Impact

Blocker

Blocker Context

I am marking this as a blocker as improving unconstrained execution is of general benefit to all Noir devs and this this specific optimization is only possible through compile changesr.

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@vezenovm vezenovm added brillig Unconstrained functions / brillig IR enhancement New feature or request ssa labels Feb 10, 2025
@vezenovm vezenovm self-assigned this Feb 10, 2025
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Feb 10, 2025
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
brillig Unconstrained functions / brillig IR enhancement New feature or request ssa
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant