Skip to content

Commit

Permalink
fix: do not simply divisions (#3664)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #3607 

## Summary\*
Identical divisions happening in both 'else' and 'then' should not be
simplified, because the non-taken branch will output (0,0) instead of
the division result.


## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [X] I have tested the changes locally.
- [X] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
guipublic authored Dec 1, 2023
1 parent 36844fa commit e5b981b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ impl Instruction {
use Instruction::*;

match self {
Binary(_) | Cast(_, _) | Not(_) | ArrayGet { .. } | ArraySet { .. } => true,
Binary(bin) => {
// In ACIR, a division with a false predicate outputs (0,0), so it cannot replace another instruction unless they have the same predicate
bin.operator != BinaryOp::Div
}
Cast(_, _) | Not(_) | ArrayGet { .. } | ArraySet { .. } => true,

// Unclear why this instruction causes problems.
Truncate { .. } => false,
Expand Down
6 changes: 6 additions & 0 deletions test_programs/execution_success/regression_3607/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "regression_3607"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "1"
8 changes: 8 additions & 0 deletions test_programs/execution_success/regression_3607/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main(mut x: u32) {
if x == 0 {
x = (x+1) / x;
} else {
x = (x+1) / x;
}
assert(x != 0);
}

0 comments on commit e5b981b

Please sign in to comment.