-
Notifications
You must be signed in to change notification settings - Fork 373
feat: safe division has no side effects #11009
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
6ad4c86
d0afd9b
c749a05
7ac2f61
95cee42
c13d7f8
0391662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,8 +273,7 @@ | |
| }; | ||
|
|
||
| // Check if this operation is one that should only fail if the predicate is enabled. | ||
| let requires_acir_gen_predicate = | ||
| binary.requires_acir_gen_predicate(context.dfg); | ||
| let requires_acir_gen_predicate = binary.has_side_effects(context.dfg); | ||
|
|
||
| let fails_under_predicate = | ||
| requires_acir_gen_predicate && !is_predicate_constant_one; | ||
|
|
@@ -1363,9 +1362,7 @@ | |
| fn simplifies_instructions_following_conditional_failure() { | ||
| // In the following SSA we have: | ||
| // 1. v1 is a divide-by-zero which turns into an conditional-fail constraint under v0 | ||
| // 2. v2 is replaced by its default value (because division is considered side effecting) | ||
| // 3. v3 would be turned into a `truncate u64 0 to 32 bits` due to step 2, which is not expected to reach ACIR gen. | ||
| // We expect 3 to disappear as it can be simplified out. | ||
| // 2. v2 is kept, as it's not side-effectuful | ||
asterite marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let src = " | ||
| acir(inline) predicate_pure fn main f0 { | ||
| b0(v0: u1): | ||
|
|
@@ -1386,6 +1383,8 @@ | |
| b0(v0: u1): | ||
| enable_side_effects v0 | ||
| constrain u1 0 == v0, "attempt to divide by zero" | ||
| v3 = div u64 1, u64 1 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I still would expect this instruction to be removed.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Me too. I was being lazy here. Now I re-checked this and it seems the logic is that instructions that have side effects are replaced with instructions with default values, while ones that don't have side effects are left as-is. But... I don't understand why. I don't know why instructions are never removed in this case.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nevermind, instructions are removed, but only those that have side effects. Maybe it's because we under the effects of a predicate? But I still don't fully understand this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it doesn't make sense to me that an instruction without side effects wouldn't be removed...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it may even be a bug. I would expect instructions without side effects to just be removed.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just do #11009 (comment) to prevent these changes and we can determine why this is happening in follow-up work.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably wait for Akosh to come back, I think he did this part. |
||
| v4 = truncate v3 to 32 bits, max_bit_size: 254 | ||
| enable_side_effects u1 1 | ||
| return | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we still call
requires_acir_gen_predicatehere which callshas_side_effectsinternally? Do we maybe we need to override the Div/Mod cases inrequires_acir_gen_predicate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because it's gone. Or should we keep
requires_acir_gen_predicateas a synonym ofhas_side_effectsfor Binary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that is what I was thinking. And then before we call out to
has_side_effects(inside ofrequires_acir_gen_predicate) we can special case Div/Mod to return true like they currently do. In a follow-up we can then address #11009 (comment) as it is starting to leave the scope of this PR.