From 660658acb2073d97f83c4b0ed7648e79e7030895 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 12 Dec 2024 11:36:05 -0600 Subject: [PATCH 1/7] Fix possible type error --- .../src/ssa/ir/instruction/binary.rs | 4 ++++ .../src/ssa/ir/instruction/call.rs | 19 ++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs index 0f52168a30..81f2f3b1e0 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs @@ -256,6 +256,10 @@ impl Binary { if rhs_is_zero { return SimplifyResult::SimplifiedTo(self.lhs); } + if operand_type == NumericType::bool() && (lhs_is_one || rhs_is_one) { + let one = dfg.make_constant(FieldElement::one(), operand_type); + return SimplifyResult::SimplifiedTo(one); + } if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) { return SimplifyResult::SimplifiedTo(self.lhs); } diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs index 1daa1af790..02be0910a1 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs @@ -480,20 +480,14 @@ fn simplify_slice_push_back( } fn simplify_slice_pop_back( - element_type: Type, + slice_type: Type, arguments: &[ValueId], dfg: &mut DataFlowGraph, block: BasicBlockId, call_stack: CallStack, ) -> SimplifyResult { - let element_types = match element_type.clone() { - Type::Slice(element_types) | Type::Array(element_types, _) => element_types, - _ => { - unreachable!("ICE: Expected slice or array, but got {element_type}"); - } - }; - - let element_count = element_type.element_size(); + let element_types = slice_type.element_types(); + let element_count = element_types.len(); let mut results = VecDeque::with_capacity(element_count + 1); let new_slice_length = update_slice_length(arguments[0], dfg, BinaryOp::Sub, block); @@ -507,14 +501,17 @@ fn simplify_slice_pop_back( flattened_len = update_slice_length(flattened_len, dfg, BinaryOp::Sub, block); // We must pop multiple elements in the case of a slice of tuples - for _ in 0..element_count { + // Iterating through element types in reverse here since we're popping from the end + for element_type in element_types.iter().rev() { let get_last_elem_instr = Instruction::ArrayGet { array: arguments[1], index: flattened_len }; + + let element_type = Some(vec![element_type.clone()]); let get_last_elem = dfg .insert_instruction_and_results( get_last_elem_instr, block, - Some(element_types.to_vec()), + element_type, call_stack.clone(), ) .first(); From fd24b758b35dd831865b82ece17c5abff160662c Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 12 Dec 2024 11:43:00 -0600 Subject: [PATCH 2/7] Fix var_points --- .../noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs index c58264dbe8..efe874d41e 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs @@ -173,7 +173,8 @@ pub(super) fn simplify_msm( var_scalars.push(zero); let result_x = dfg.make_constant(result_x, NumericType::NativeField); let result_y = dfg.make_constant(result_y, NumericType::NativeField); - let result_is_infinity = dfg.make_constant(result_is_infinity, NumericType::bool()); + let result_is_infinity = + dfg.make_constant(result_is_infinity, NumericType::NativeField); var_points.push(result_x); var_points.push(result_y); var_points.push(result_is_infinity); From f4d878929b66a91e62f87174d3e2939cd4434394 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 12 Dec 2024 12:50:12 -0600 Subject: [PATCH 3/7] Fix unit test --- .../noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs index efe874d41e..eaa3cba58f 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs @@ -388,9 +388,9 @@ mod test { v5 = make_array [Field 1, Field 0, v0, Field 0, Field 2, Field 0] : [Field; 6] v7 = make_array [Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0, v0, v1, Field 0, Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0] : [Field; 9] v8 = make_array [v0, Field 0, Field 1, Field 0] : [Field; 4] - v12 = make_array [v0, v1, Field 0, Field -3227352362257037263902424173275354266044964400219754872043023745437788450996, Field 8902249110305491597038405103722863701255802573786510474664632793109847672620, u1 0] : [Field; 6] - v14 = call multi_scalar_mul(v12, v8) -> [Field; 3] - return v14 + v11 = make_array [v0, v1, Field 0, Field -3227352362257037263902424173275354266044964400219754872043023745437788450996, Field 8902249110305491597038405103722863701255802573786510474664632793109847672620, Field 0] : [Field; 6] + v13 = call multi_scalar_mul(v12, v8) -> [Field; 3] + return v13 } "#; assert_normalized_ssa_equals(ssa, expected_src); From 0b7207613cd194ad4b65e12f2c9820c4003238b7 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 12 Dec 2024 13:27:36 -0600 Subject: [PATCH 4/7] Fix test --- .../noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs index eaa3cba58f..7b111d351a 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs @@ -389,7 +389,7 @@ mod test { v7 = make_array [Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0, v0, v1, Field 0, Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0] : [Field; 9] v8 = make_array [v0, Field 0, Field 1, Field 0] : [Field; 4] v11 = make_array [v0, v1, Field 0, Field -3227352362257037263902424173275354266044964400219754872043023745437788450996, Field 8902249110305491597038405103722863701255802573786510474664632793109847672620, Field 0] : [Field; 6] - v13 = call multi_scalar_mul(v12, v8) -> [Field; 3] + v13 = call multi_scalar_mul(v11, v8) -> [Field; 3] return v13 } "#; From ee7548b02f14f5734e73c5adb60f4ac4a18a5e1d Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 12 Dec 2024 13:41:46 -0600 Subject: [PATCH 5/7] Revert bool -> field change --- .../noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs index 7b111d351a..0e9bb98931 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs @@ -173,8 +173,12 @@ pub(super) fn simplify_msm( var_scalars.push(zero); let result_x = dfg.make_constant(result_x, NumericType::NativeField); let result_y = dfg.make_constant(result_y, NumericType::NativeField); + + // Pushing a bool here is intentional, multi_scalar_mul takes two arguments: + // `points: [(Field, Field, bool); N]` and `scalars: [(Field, Field); N]`. let result_is_infinity = - dfg.make_constant(result_is_infinity, NumericType::NativeField); + dfg.make_constant(result_is_infinity, NumericType::bool()); + var_points.push(result_x); var_points.push(result_y); var_points.push(result_is_infinity); From 6853918ab77be011af7c0addd65c96bb3a982b6d Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 12 Dec 2024 13:43:41 -0600 Subject: [PATCH 6/7] Fix test --- .../src/ssa/ir/instruction/call/blackbox.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs index 0e9bb98931..82a55a718b 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs @@ -176,8 +176,7 @@ pub(super) fn simplify_msm( // Pushing a bool here is intentional, multi_scalar_mul takes two arguments: // `points: [(Field, Field, bool); N]` and `scalars: [(Field, Field); N]`. - let result_is_infinity = - dfg.make_constant(result_is_infinity, NumericType::bool()); + let result_is_infinity = dfg.make_constant(result_is_infinity, NumericType::bool()); var_points.push(result_x); var_points.push(result_y); @@ -392,8 +391,8 @@ mod test { v5 = make_array [Field 1, Field 0, v0, Field 0, Field 2, Field 0] : [Field; 6] v7 = make_array [Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0, v0, v1, Field 0, Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0] : [Field; 9] v8 = make_array [v0, Field 0, Field 1, Field 0] : [Field; 4] - v11 = make_array [v0, v1, Field 0, Field -3227352362257037263902424173275354266044964400219754872043023745437788450996, Field 8902249110305491597038405103722863701255802573786510474664632793109847672620, Field 0] : [Field; 6] - v13 = call multi_scalar_mul(v11, v8) -> [Field; 3] + v12 = make_array [v0, v1, Field 0, Field -3227352362257037263902424173275354266044964400219754872043023745437788450996, Field 8902249110305491597038405103722863701255802573786510474664632793109847672620, u1 0] : [Field; 6] + v14 = call multi_scalar_mul(v12, v8) -> [Field; 3] return v13 } "#; From 2e8b5fe61e1dd539b611bd0b1962effcd406d07f Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Thu, 12 Dec 2024 13:44:11 -0600 Subject: [PATCH 7/7] Fix unit test --- .../noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs index 82a55a718b..a5de98cec7 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs @@ -393,7 +393,7 @@ mod test { v8 = make_array [v0, Field 0, Field 1, Field 0] : [Field; 4] v12 = make_array [v0, v1, Field 0, Field -3227352362257037263902424173275354266044964400219754872043023745437788450996, Field 8902249110305491597038405103722863701255802573786510474664632793109847672620, u1 0] : [Field; 6] v14 = call multi_scalar_mul(v12, v8) -> [Field; 3] - return v13 + return v14 } "#; assert_normalized_ssa_equals(ssa, expected_src);