Skip to content

Commit 5a0182f

Browse files
authored
chore(ssa): Simplify truncate when the bit size we are truncating to is greater than the value's max bit size (#8875)
1 parent 229d57b commit 5a0182f

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub(crate) fn simplify(
141141
try_optimize_array_set_from_previous_get(dfg, *array_id, *index_id, *value)
142142
}
143143
Instruction::Truncate { value, bit_size, max_bit_size } => {
144-
if bit_size == max_bit_size {
144+
if bit_size >= max_bit_size {
145145
return SimplifiedTo(*value);
146146
}
147147
if let Some((numeric_constant, typ)) = dfg.get_numeric_constant_with_type(*value) {
@@ -490,4 +490,25 @@ mod tests {
490490
assert_normalized_ssa_equals(ssa, &expected);
491491
}
492492
}
493+
494+
#[test]
495+
fn truncate_to_bit_size_bigger_than_value_max_bit_size() {
496+
let src = "
497+
acir(inline) fn main f0 {
498+
b0(v0: u8):
499+
v1 = truncate v0 to 16 bits, max_bit_size: 8
500+
v2 = cast v1 as u16
501+
return v2
502+
}
503+
";
504+
let ssa = Ssa::from_str_simplifying(src).unwrap();
505+
506+
assert_ssa_snapshot!(ssa, @r"
507+
acir(inline) fn main f0 {
508+
b0(v0: u8):
509+
v1 = cast v0 as u16
510+
return v1
511+
}
512+
");
513+
}
493514
}

0 commit comments

Comments
 (0)