From 6d0f86ba389a5b59b1d7fdcadcbce3e40eecaa48 Mon Sep 17 00:00:00 2001
From: guipublic <47281315+guipublic@users.noreply.github.com>
Date: Fri, 6 Dec 2024 20:14:57 +0100
Subject: [PATCH] chore: simplify MSM with constant folding (#6650)
---
compiler/noirc_evaluator/src/lib.rs | 6 +-
.../src/ssa/ir/instruction/call/blackbox.rs | 240 +++++++++++++++---
.../embedded_curve_ops/src/main.nr | 18 ++
3 files changed, 222 insertions(+), 42 deletions(-)
diff --git a/compiler/noirc_evaluator/src/lib.rs b/compiler/noirc_evaluator/src/lib.rs
index 8127e3d03ef..75ea557d3de 100644
--- a/compiler/noirc_evaluator/src/lib.rs
+++ b/compiler/noirc_evaluator/src/lib.rs
@@ -12,8 +12,7 @@ pub mod ssa;
pub use ssa::create_program;
pub use ssa::ir::instruction::ErrorType;
-/// Trims leading whitespace from each line of the input string, according to
-/// how much leading whitespace there is on the first non-empty line.
+/// Trims leading whitespace from each line of the input string
#[cfg(test)]
pub(crate) fn trim_leading_whitespace_from_lines(src: &str) -> String {
let mut lines = src.trim_end().lines();
@@ -21,11 +20,10 @@ pub(crate) fn trim_leading_whitespace_from_lines(src: &str) -> String {
while first_line.is_empty() {
first_line = lines.next().unwrap();
}
- let indent = first_line.len() - first_line.trim_start().len();
let mut result = first_line.trim_start().to_string();
for line in lines {
result.push('\n');
- result.push_str(&line[indent..]);
+ result.push_str(line.trim_start());
}
result
}
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 301b75e0bd4..db085bd762f 100644
--- a/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs
+++ b/compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs
@@ -2,10 +2,11 @@ use std::sync::Arc;
use acvm::{acir::AcirField, BlackBoxFunctionSolver, BlackBoxResolutionError, FieldElement};
+use crate::ssa::ir::instruction::BlackBoxFunc;
use crate::ssa::ir::{
basic_block::BasicBlockId,
dfg::{CallStack, DataFlowGraph},
- instruction::{Instruction, SimplifyResult},
+ instruction::{Instruction, Intrinsic, SimplifyResult},
types::Type,
value::ValueId,
};
@@ -70,52 +71,125 @@ pub(super) fn simplify_msm(
block: BasicBlockId,
call_stack: &CallStack,
) -> SimplifyResult {
- // TODO: Handle MSMs where a subset of the terms are constant.
+ let mut is_constant;
+
match (dfg.get_array_constant(arguments[0]), dfg.get_array_constant(arguments[1])) {
(Some((points, _)), Some((scalars, _))) => {
- let Some(points) = points
- .into_iter()
- .map(|id| dfg.get_numeric_constant(id))
- .collect::