Skip to content

Commit

Permalink
Merge ae49571 into 8232bfa
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Oct 11, 2024
2 parents 8232bfa + ae49571 commit 9ba5f65
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
6 changes: 3 additions & 3 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ impl Intrinsic {
// These apply a constraint that the input must fit into a specified number of limbs.
Intrinsic::ToBits(_) | Intrinsic::ToRadix(_) => true,

// These imply a check that the slice is non-empty and should fail otherwise.
Intrinsic::SlicePopBack | Intrinsic::SlicePopFront | Intrinsic::SliceRemove => true,

Intrinsic::ArrayLen
| Intrinsic::ArrayAsStrUnchecked
| Intrinsic::AsSlice
| Intrinsic::SlicePushBack
| Intrinsic::SlicePushFront
| Intrinsic::SlicePopBack
| Intrinsic::SlicePopFront
| Intrinsic::SliceInsert
| Intrinsic::SliceRemove
| Intrinsic::StrAsBytes
| Intrinsic::FromField
| Intrinsic::AsField
Expand Down
21 changes: 21 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ pub(super) fn simplify_call(
}
}
Intrinsic::SlicePopBack => {
let length = dfg.get_numeric_constant(arguments[0]);
if length.map_or(true, |length| length.is_zero()) {
// If the length is zero then we're trying to pop the last element from an empty slice.
// Defer the error to acir_gen.
return SimplifyResult::None;
}

let slice = dfg.get_array_constant(arguments[1]);
if let Some((_, typ)) = slice {
simplify_slice_pop_back(typ, arguments, dfg, block, call_stack.clone())
Expand All @@ -174,6 +181,13 @@ pub(super) fn simplify_call(
}
}
Intrinsic::SlicePopFront => {
let length = dfg.get_numeric_constant(arguments[0]);
if length.map_or(true, |length| length.is_zero()) {
// If the length is zero then we're trying to pop the first element from an empty slice.
// Defer the error to acir_gen.
return SimplifyResult::None;
}

let slice = dfg.get_array_constant(arguments[1]);
if let Some((mut slice, typ)) = slice {
let element_count = typ.element_size();
Expand Down Expand Up @@ -225,6 +239,13 @@ pub(super) fn simplify_call(
}
}
Intrinsic::SliceRemove => {
let length = dfg.get_numeric_constant(arguments[0]);
if length.map_or(true, |length| length.is_zero()) {
// If the length is zero then we're trying to remove an element from an empty slice.
// Defer the error to acir_gen.
return SimplifyResult::None;
}

let slice = dfg.get_array_constant(arguments[1]);
let index = dfg.get_numeric_constant(arguments[2]);
if let (Some((mut slice, typ)), Some(index)) = (slice, index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ impl<'a> SliceCapacityTracker<'a> {
let slice_contents = arguments[argument_index];

if let Some(contents_capacity) = slice_sizes.get(&slice_contents) {
let new_capacity = *contents_capacity - 1;
// We use a saturating sub here as calling `pop_front` or `pop_back`
// on a zero-length slice would otherwise underflow.
let new_capacity = contents_capacity.saturating_sub(1);
slice_sizes.insert(result_slice, new_capacity);
}
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/remove_if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ impl Context {
}
SizeChange::Dec { old, new } => {
let old_capacity = self.get_or_find_capacity(&function.dfg, old);
self.slice_sizes.insert(new, old_capacity - 1);
// We use a saturating sub here as calling `pop_front` or `pop_back` on a zero-length slice
// would otherwise underflow.
self.slice_sizes.insert(new, old_capacity.saturating_sub(1));
}
}
}
Expand Down

0 comments on commit 9ba5f65

Please sign in to comment.