Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions compiler/noirc_evaluator/src/ssa/ir/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,39 @@ impl IntegerConstant {
}
}

/// Increment the value by 1, saturating at the maximum value.
/// Increment the value by 1
///
/// # Panics
///
/// Panics if the increment causes an overflow.
pub(crate) fn inc(self) -> Self {
match self {
Self::Signed { value, bit_size } => {
Self::Signed { value: value.saturating_add(1), bit_size }
}
Self::Unsigned { value, bit_size } => {
Self::Unsigned { value: value.saturating_add(1), bit_size }
}
Self::Signed { value, bit_size } => Self::Signed {
value: value.checked_add(1).expect("ICE: overflow while incrementing constant"),
bit_size,
},
Self::Unsigned { value, bit_size } => Self::Unsigned {
value: value.checked_add(1).expect("ICE: overflow while incrementing constant"),
bit_size,
},
}
}

/// Decrement the value by 1, saturating at the minimum value.
///
/// # panics
///
/// Panics if the decrement causes an overflow.
pub(crate) fn dec(self) -> Self {
match self {
Self::Signed { value, bit_size } => {
Self::Signed { value: value.saturating_sub(1), bit_size }
}
Self::Unsigned { value, bit_size } => {
Self::Unsigned { value: value.saturating_sub(1), bit_size }
}
Self::Signed { value, bit_size } => Self::Signed {
value: value.checked_sub(1).expect("ICE: overflow while decrementing constant"),
bit_size,
},
Self::Unsigned { value, bit_size } => Self::Unsigned {
value: value.checked_sub(1).expect("ICE: overflow while decrementing constant"),
bit_size,
},
}
}

Expand Down
36 changes: 36 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/unrolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1829,4 +1829,40 @@ mod tests {
assert!(loop0.get_const_lower_bound(&function.dfg, pre_header).is_none());
assert!(loop0.get_const_upper_bound(&function.dfg, pre_header).is_none());
}

#[test]
#[should_panic(expected = "ICE: overflow while incrementing constant")]
fn unroll_loop_upper_bound_saturated() {
let ssa = format!(
r#"
acir(inline) fn main f0 {{
b0():
jmp b1(u128 {0})
b1(v0: u128):
v3 = eq v0, u128 {0}
jmpif v3 then: b3, else: b2
b2():
v2 = make_array b""
call print(u1 1, v0, v2, u1 0)
v6 = unchecked_add v0, u128 1
jmp b1(v6)
b3():
return
}}"#,
u128::MAX
);

let ssa = Ssa::from_str(&ssa).unwrap();
let function = ssa.main();

let loops = Loops::find_all(function);
assert_eq!(loops.yet_to_unroll.len(), 1);

let loop_ = &loops.yet_to_unroll[0];
let pre_header =
loop_.get_pre_header(function, &loops.cfg).expect("Should have a pre_header");
let (lower, upper) =
loop_.get_const_bounds(&function.dfg, pre_header).expect("bounds are numeric const");
assert_ne!(lower, upper);
}
}
Loading