Skip to content

Commit

Permalink
feat: improve optimisations on range constraints (#4690)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

We're currently not making full use of type information when optimizing
out range constraints in SSA. I don't think makes a huge difference as
they should be removed in ACIR gen but it's good to catch these early.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** 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
TomAFrench authored Apr 2, 2024
1 parent 656665e commit 96b8110
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,12 @@ impl Instruction {
Instruction::IncrementRc { .. } => None,
Instruction::DecrementRc { .. } => None,
Instruction::RangeCheck { value, max_bit_size, .. } => {
if let Some(numeric_constant) = dfg.get_numeric_constant(*value) {
if numeric_constant.num_bits() < *max_bit_size {
return Remove;
}
let max_potential_bits = dfg.get_value_max_num_bits(*value);
if max_potential_bits < *max_bit_size {
Remove
} else {
None
}
None
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,16 @@ pub(super) fn simplify_call(
let max_bit_size = dfg.get_numeric_constant(arguments[1]);
if let Some(max_bit_size) = max_bit_size {
let max_bit_size = max_bit_size.to_u128() as u32;
SimplifyResult::SimplifiedToInstruction(Instruction::RangeCheck {
value,
max_bit_size,
assert_message: Some("call to assert_max_bit_size".to_owned()),
})
let max_potential_bits = dfg.get_value_max_num_bits(value);
if max_potential_bits < max_bit_size {
SimplifyResult::Remove
} else {
SimplifyResult::SimplifiedToInstruction(Instruction::RangeCheck {
value,
max_bit_size,
assert_message: Some("call to assert_max_bit_size".to_owned()),
})
}
} else {
SimplifyResult::None
}
Expand Down

0 comments on commit 96b8110

Please sign in to comment.