Skip to content

Commit

Permalink
do not deduplicate division by a zero const
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm committed Feb 14, 2025
1 parent 3878037 commit 653a9a4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ impl Instruction {
BinaryOp::Div | BinaryOp::Mod => {
// Div and Mod require a predicate if the RHS may be zero.
dfg.get_numeric_constant(binary.rhs)
.map(|rhs| !rhs.is_zero())
.map(|rhs| rhs.is_zero())
.unwrap_or(true)
}
BinaryOp::Add { unchecked: true }
Expand Down
51 changes: 51 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,4 +1785,55 @@ mod test {
let ssa = ssa.fold_constants();
assert_normalized_ssa_equals(ssa, src);
}

#[test]
fn does_not_deduplicate_unsigned_division_by_zero_constant() {
// Regression test for https://github.com/noir-lang/noir/issues/7283
let src = "
acir(inline) fn main f0 {
b0(v0: u32, v1: u32, v2: u1):
enable_side_effects v2
v4 = div v1, u32 0
v5 = not v2
enable_side_effects v5
v6 = div v1, u32 0
return
}
";

let ssa = Ssa::from_str(src).unwrap();
let ssa = ssa.fold_constants();
assert_normalized_ssa_equals(ssa, src);
}

#[test]
fn does_duplicate_unsigned_division_by_non_zero_constant() {
// Regression test for https://github.com/noir-lang/noir/issues/7283
let src = "
acir(inline) fn main f0 {
b0(v0: u32, v1: u32, v2: u1):
enable_side_effects v2
v4 = div v1, u32 2
v5 = not v2
enable_side_effects v5
v6 = div v1, u32 2
return
}
";

let ssa = Ssa::from_str(src).unwrap();
let ssa = ssa.fold_constants();

let expected = "
acir(inline) fn main f0 {
b0(v0: u32, v1: u32, v2: u1):
enable_side_effects v2
v4 = div v1, u32 2
v5 = not v2
enable_side_effects v5
return
}
";
assert_normalized_ssa_equals(ssa, expected);
}
}

0 comments on commit 653a9a4

Please sign in to comment.