From e5b981b08c2b345f00426acafe47b76d5262254d Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Fri, 1 Dec 2023 19:00:31 +0100 Subject: [PATCH] fix: do not simply divisions (#3664) # 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. --- compiler/noirc_evaluator/src/ssa/ir/instruction.rs | 6 +++++- .../execution_success/regression_3607/Nargo.toml | 6 ++++++ .../execution_success/regression_3607/Prover.toml | 1 + .../execution_success/regression_3607/src/main.nr | 8 ++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test_programs/execution_success/regression_3607/Nargo.toml create mode 100644 test_programs/execution_success/regression_3607/Prover.toml create mode 100644 test_programs/execution_success/regression_3607/src/main.nr diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction.rs index 63b32766f62..e940b719cb6 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction.rs @@ -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, diff --git a/test_programs/execution_success/regression_3607/Nargo.toml b/test_programs/execution_success/regression_3607/Nargo.toml new file mode 100644 index 00000000000..8757f88b90e --- /dev/null +++ b/test_programs/execution_success/regression_3607/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "regression_3607" +type = "bin" +authors = [""] + +[dependencies] diff --git a/test_programs/execution_success/regression_3607/Prover.toml b/test_programs/execution_success/regression_3607/Prover.toml new file mode 100644 index 00000000000..4dd6b405159 --- /dev/null +++ b/test_programs/execution_success/regression_3607/Prover.toml @@ -0,0 +1 @@ +x = "1" diff --git a/test_programs/execution_success/regression_3607/src/main.nr b/test_programs/execution_success/regression_3607/src/main.nr new file mode 100644 index 00000000000..c09211c2810 --- /dev/null +++ b/test_programs/execution_success/regression_3607/src/main.nr @@ -0,0 +1,8 @@ +fn main(mut x: u32) { + if x == 0 { + x = (x+1) / x; + } else { + x = (x+1) / x; + } + assert(x != 0); +} \ No newline at end of file