From a830ce5c3b247a7a60d7d4de4f8470e3227b8a47 Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Tue, 16 May 2023 15:41:57 +0200 Subject: [PATCH] chore(ssa): enable cse for assert (#1350) enable cse for assert --- crates/noirc_evaluator/src/ssa/node.rs | 30 +++++++++++++++++++ .../noirc_evaluator/src/ssa/optimizations.rs | 16 ++++++++++ 2 files changed, 46 insertions(+) diff --git a/crates/noirc_evaluator/src/ssa/node.rs b/crates/noirc_evaluator/src/ssa/node.rs index bec3c923a6d..4566d974813 100644 --- a/crates/noirc_evaluator/src/ssa/node.rs +++ b/crates/noirc_evaluator/src/ssa/node.rs @@ -506,6 +506,36 @@ impl Instruction { } } } + + pub(crate) fn get_location(&self) -> Option { + match &self.operation { + Operation::Binary(bin) => match bin.operator { + BinaryOp::Udiv(location) + | BinaryOp::Sdiv(location) + | BinaryOp::Urem(location) + | BinaryOp::Srem(location) + | BinaryOp::Div(location) + | BinaryOp::Shr(location) => Some(location), + _ => None, + }, + Operation::Call { location, .. } => Some(*location), + Operation::Load { location, .. } + | Operation::Store { location, .. } + | Operation::Constrain(_, location) => *location, + Operation::Cast(_) + | Operation::Truncate { .. } + | Operation::Not(_) + | Operation::Jne(_, _) + | Operation::Jeq(_, _) + | Operation::Jmp(_) + | Operation::Phi { .. } + | Operation::Return(_) + | Operation::Result { .. } + | Operation::Cond { .. } + | Operation::Intrinsic(_, _) + | Operation::Nop => None, + } + } } //adapted from LLVM IR diff --git a/crates/noirc_evaluator/src/ssa/optimizations.rs b/crates/noirc_evaluator/src/ssa/optimizations.rs index f1cfca9c243..d238ae7b0fe 100644 --- a/crates/noirc_evaluator/src/ssa/optimizations.rs +++ b/crates/noirc_evaluator/src/ssa/optimizations.rs @@ -507,6 +507,22 @@ fn cse_block_with_anchor( new_list.push(*ins_id); } } + Operation::Constrain(condition, location) => { + if let Some(similar) = anchor.find_similar_instruction(&operator) { + assert_ne!(similar, ins.id); + *modified = true; + let similar_ins = ctx + .try_get_mut_instruction(similar) + .expect("Similar instructions are instructions"); + if location.is_some() && similar_ins.get_location().is_none() { + similar_ins.operation = Operation::Constrain(*condition, *location); + } + new_mark = Mark::ReplaceWith(similar); + } else { + new_list.push(*ins_id); + anchor.push_front(&ins.operation, *ins_id); + } + } _ => { //TODO: checks we do not need to propagate res arguments new_list.push(*ins_id);