-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into jf/make-for-a-statement
- Loading branch information
Showing
177 changed files
with
982 additions
and
2,165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,45 @@ | ||
use acir::circuit::{Circuit, Opcode}; | ||
|
||
mod general; | ||
mod redundant_range; | ||
mod unused_memory; | ||
|
||
pub(crate) use general::GeneralOptimizer; | ||
pub(crate) use redundant_range::RangeOptimizer; | ||
|
||
use self::unused_memory::UnusedMemoryOptimizer; | ||
|
||
use super::AcirTransformationMap; | ||
|
||
/// Applies [`ProofSystemCompiler`][crate::ProofSystemCompiler] independent optimizations to a [`Circuit`]. | ||
pub fn optimize(acir: Circuit) -> (Circuit, AcirTransformationMap) { | ||
// General optimizer pass | ||
let mut opcodes: Vec<Opcode> = Vec::new(); | ||
for opcode in acir.opcodes { | ||
match opcode { | ||
Opcode::Arithmetic(arith_expr) => { | ||
opcodes.push(Opcode::Arithmetic(GeneralOptimizer::optimize(arith_expr))); | ||
} | ||
other_opcode => opcodes.push(other_opcode), | ||
}; | ||
} | ||
let acir = Circuit { opcodes, ..acir }; | ||
|
||
// Track original acir opcode positions throughout the transformation passes of the compilation | ||
// by applying the modifications done to the circuit opcodes and also to the opcode_positions (delete and insert) | ||
let acir_opcode_positions = acir.opcodes.iter().enumerate().map(|(i, _)| i).collect(); | ||
|
||
// Unused memory optimization pass | ||
let memory_optimizer = UnusedMemoryOptimizer::new(acir); | ||
let (acir, acir_opcode_positions) = | ||
memory_optimizer.remove_unused_memory_initializations(acir_opcode_positions); | ||
|
||
// Range optimization pass | ||
let range_optimizer = RangeOptimizer::new(acir); | ||
let (acir, acir_opcode_positions) = | ||
range_optimizer.replace_redundant_ranges(acir_opcode_positions); | ||
|
||
let transformation_map = AcirTransformationMap { acir_opcode_positions }; | ||
|
||
(acir, transformation_map) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use acir::circuit::{opcodes::BlockId, Circuit, Opcode}; | ||
use std::collections::HashSet; | ||
|
||
/// `UnusedMemoryOptimizer` will remove initializations of memory blocks which are unused. | ||
pub(crate) struct UnusedMemoryOptimizer { | ||
unused_memory_initializations: HashSet<BlockId>, | ||
circuit: Circuit, | ||
} | ||
|
||
impl UnusedMemoryOptimizer { | ||
/// Creates a new `UnusedMemoryOptimizer ` by collecting unused memory init | ||
/// opcodes from `Circuit`. | ||
pub(crate) fn new(circuit: Circuit) -> Self { | ||
let unused_memory_initializations = Self::collect_unused_memory_initializations(&circuit); | ||
Self { circuit, unused_memory_initializations } | ||
} | ||
|
||
/// Creates a set of ids for memory blocks for which no [`Opcode::MemoryOp`]s exist. | ||
/// | ||
/// These memory blocks can be safely removed. | ||
fn collect_unused_memory_initializations(circuit: &Circuit) -> HashSet<BlockId> { | ||
let mut unused_memory_initialization = HashSet::new(); | ||
|
||
for opcode in &circuit.opcodes { | ||
match opcode { | ||
Opcode::MemoryInit { block_id, .. } => { | ||
unused_memory_initialization.insert(*block_id); | ||
} | ||
Opcode::MemoryOp { block_id, .. } => { | ||
unused_memory_initialization.remove(block_id); | ||
} | ||
_ => (), | ||
} | ||
} | ||
unused_memory_initialization | ||
} | ||
|
||
/// Returns a `Circuit` where [`Opcode::MemoryInit`]s for unused memory blocks are dropped. | ||
pub(crate) fn remove_unused_memory_initializations( | ||
self, | ||
order_list: Vec<usize>, | ||
) -> (Circuit, Vec<usize>) { | ||
let mut new_order_list = Vec::with_capacity(order_list.len()); | ||
let mut optimized_opcodes = Vec::with_capacity(self.circuit.opcodes.len()); | ||
for (idx, opcode) in self.circuit.opcodes.iter().enumerate() { | ||
match opcode { | ||
Opcode::MemoryInit { block_id, .. } | ||
if self.unused_memory_initializations.contains(block_id) => | ||
{ | ||
// Drop opcode | ||
} | ||
_ => { | ||
new_order_list.push(order_list[idx]); | ||
optimized_opcodes.push(opcode.clone()); | ||
} | ||
} | ||
} | ||
|
||
(Circuit { opcodes: optimized_opcodes, ..self.circuit }, new_order_list) | ||
} | ||
} |
Oops, something went wrong.