Skip to content

Commit

Permalink
feat: optimize out range checks on limiting cases (noir-lang#7510)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Feb 25, 2025
1 parent 3bc52e4 commit 6f78848
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ impl Instruction {
Instruction::DecrementRc { .. } => None,
Instruction::RangeCheck { value, max_bit_size, .. } => {
let max_potential_bits = dfg.get_value_max_num_bits(*value);
if max_potential_bits < *max_bit_size {
if max_potential_bits <= *max_bit_size {
Remove
} else {
None
Expand Down Expand Up @@ -1472,3 +1472,32 @@ impl SimplifyResult {
}
}
}

#[cfg(test)]
mod tests {
use crate::ssa::{opt::assert_normalized_ssa_equals, ssa_gen::Ssa};

#[test]
fn removes_range_constraints_on_constants() {
let src = "
acir(inline) fn main f0 {
b0(v0: Field):
range_check Field 0 to 1 bits
range_check Field 1 to 1 bits
range_check Field 255 to 8 bits
range_check Field 256 to 8 bits
return
}
";
let ssa = Ssa::from_str_simplifying(src).unwrap();

let expected = "
acir(inline) fn main f0 {
b0(v0: Field):
range_check Field 256 to 8 bits
return
}
";
assert_normalized_ssa_equals(ssa, expected);
}
}

0 comments on commit 6f78848

Please sign in to comment.