From 58deb9f04338fc9c30fd21c1002cd762c62b657a Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Tue, 10 Jun 2025 20:15:38 +0000 Subject: [PATCH 1/2] simplify truncate when the bit size we are truncating to it greater than the maximum bit size a value can have --- compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs index 90407213023..db019d59f2b 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs @@ -141,7 +141,7 @@ pub(crate) fn simplify( try_optimize_array_set_from_previous_get(dfg, *array_id, *index_id, *value) } Instruction::Truncate { value, bit_size, max_bit_size } => { - if bit_size == max_bit_size { + if bit_size >= max_bit_size { return SimplifiedTo(*value); } if let Some((numeric_constant, typ)) = dfg.get_numeric_constant_with_type(*value) { From 24a3512adf2d60a55f5bdb11c244619645ebf2ee Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 11 Jun 2025 17:07:40 +0000 Subject: [PATCH 2/2] add test --- .../src/ssa/ir/dfg/simplify.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs index db019d59f2b..b0461c3b092 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs @@ -490,4 +490,25 @@ mod tests { assert_normalized_ssa_equals(ssa, &expected); } } + + #[test] + fn truncate_to_bit_size_bigger_than_value_max_bit_size() { + let src = " + acir(inline) fn main f0 { + b0(v0: u8): + v1 = truncate v0 to 16 bits, max_bit_size: 8 + v2 = cast v1 as u16 + return v2 + } + "; + let ssa = Ssa::from_str_simplifying(src).unwrap(); + + assert_ssa_snapshot!(ssa, @r" + acir(inline) fn main f0 { + b0(v0: u8): + v1 = cast v0 as u16 + return v1 + } + "); + } }