Skip to content

fix(ssa): Check cast bit size when fetching the max num bits for a value #4699

Closed
vezenovm wants to merge 4 commits intomasterfrom
mv/ssa-simplify-bit-fix
Closed

fix(ssa): Check cast bit size when fetching the max num bits for a value #4699
vezenovm wants to merge 4 commits intomasterfrom
mv/ssa-simplify-bit-fix

Conversation

@vezenovm
Copy link
Contributor

@vezenovm vezenovm commented Apr 2, 2024

Description

Problem*

No issue as this was found by @jfecher while testing in debug mode so pushed a quick fix

Summary*

We check for Cast instructions in get_value_max_num_bits as per this PR (#4039) in order to determine a more restrictive upper bound on values. However, if we cast from a larger data type we are returning the larger bit size.

Additional Context

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.

@vezenovm vezenovm requested a review from jfecher April 2, 2024 21:16
@github-actions
Copy link
Contributor

github-actions bot commented Apr 2, 2024

Changes to circuit sizes

Generated at commit: 4e8dfa53db321658d61f62ac35f71f8521cf80a0, compared to commit: 24f6d85f2467a109399d21729f8bb0f97c5ba6db

🧾 Summary (10% most significant diffs)

Program ACIR opcodes (+/-) % Circuit size (+/-) %
bit_shifts_runtime 0 ➖ 0.00% -5 ✅ -0.09%

Full diff report 👇
Program ACIR opcodes (+/-) % Circuit size (+/-) %
bit_shifts_runtime 2,802 (0) 0.00% 5,861 (-5) -0.09%

if let Some(constant) = constant {
let max_possible_value =
2u128.pow(dfg.get_value_max_num_bits(non_constant)) - 1;
2u128.pow(dfg.type_of_value(non_constant).bit_size()) - 1;
Copy link
Contributor Author

@vezenovm vezenovm Apr 2, 2024

Choose a reason for hiding this comment

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

There is actually two issues. The Cast logic was incorrect and also that in #4691 we should be checking against the bit_size of the variable's type. According to the comments for get_value_max_num_bits it is attempting to the following, which is not what we want:

Returns the maximum possible number of bits that `value` can potentially be

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll pull this out into its own fix and we can wait for @TomAFrench to take a look at the Cast fix as those are his changes.

Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why we should make this change? The motivation for the current implementation is that get_value_max_num_bits will return the max number of bits which value can take, i.e. non_constant < 2^max_bits. This allows us to reason how about when the constant is greater than this upper bound then it's impossible for the equality to hold.

This relies on casts up from a smaller type as otherwise the constant will always be less than the max potential value of the variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This relies on casts up from a smaller type as otherwise the constant will always be less than the max potential value of the variable.

Ok I understand thank you for elaborating. I was thinking that using get_value_max_num_bits was a typo due to the comments above about variable types. I can update those to discuss that we want to check how for casts from a lower type as well as bringing usage back of get_value_max_num_bits.

github-merge-queue bot pushed a commit that referenced this pull request Apr 3, 2024
…formation (#4700)

# Description

## Problem\*

No issue as this was found by @jfecher while testing in debug mode where
we get overflow errors after #4691

Resolves
#4699 (comment)

## Summary\*



## Additional Context



## 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](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Tom French <tom@tomfren.ch>
@jfecher jfecher closed this in #4700 Apr 3, 2024
TomAFrench added a commit that referenced this pull request Apr 3, 2024
…formation (#4700)

# Description

## Problem\*

No issue as this was found by @jfecher while testing in debug mode where
we get overflow errors after #4691

Resolves
#4699 (comment)

## Summary\*



## Additional Context



## 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](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Tom French <tom@tomfren.ch>
@TomAFrench TomAFrench deleted the mv/ssa-simplify-bit-fix branch November 20, 2024 12:03
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.

3 participants