-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: strip initialization of unused memory blocks from ACIR (#2985)
Co-authored-by: Maxim Vezenov <[email protected]>
- Loading branch information
1 parent
269e490
commit fde5daa
Showing
54 changed files
with
69 additions
and
0 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
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) | ||
} | ||
} |
Binary file not shown.
Binary file modified
BIN
-22 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/6_array/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
-30 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/7_function/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-17 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/array_dynamic/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+20 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/array_eq/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-22 Bytes
(93%)
tooling/nargo_cli/tests/acir_artifacts/array_len/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-151 Bytes
(93%)
tooling/nargo_cli/tests/acir_artifacts/array_neq/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-15 Bytes
(98%)
tooling/nargo_cli/tests/acir_artifacts/array_sort/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-35 Bytes
(99%)
tooling/nargo_cli/tests/acir_artifacts/bit_shifts_runtime/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-6 Bytes
(99%)
tooling/nargo_cli/tests/acir_artifacts/brillig_arrays/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+4 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/brillig_blake2s/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-6 Bytes
(99%)
tooling/nargo_cli/tests/acir_artifacts/brillig_calls_array/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-7 Bytes
(99%)
tooling/nargo_cli/tests/acir_artifacts/brillig_calls_conditionals/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+66 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/brillig_ecdsa/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-18 Bytes
(94%)
tooling/nargo_cli/tests/acir_artifacts/brillig_identity_function/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-228 Bytes
(87%)
tooling/nargo_cli/tests/acir_artifacts/brillig_keccak/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+14 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/brillig_sha256/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-74 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/conditional_1/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-5 Bytes
(98%)
tooling/nargo_cli/tests/acir_artifacts/conditional_2/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-8 Bytes
(98%)
tooling/nargo_cli/tests/acir_artifacts/conditional_regression_421/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-6 Bytes
(99%)
tooling/nargo_cli/tests/acir_artifacts/conditional_regression_661/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+3 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/conditional_regression_short_circuit/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-1 Byte
(100%)
tooling/nargo_cli/tests/acir_artifacts/double_verify_proof/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-25 Bytes
(99%)
tooling/nargo_cli/tests/acir_artifacts/ecdsa_secp256k1/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-72 Bytes
(94%)
tooling/nargo_cli/tests/acir_artifacts/ecdsa_secp256r1/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-176 Bytes
(92%)
tooling/nargo_cli/tests/acir_artifacts/global_consts/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-9 Bytes
(98%)
tooling/nargo_cli/tests/acir_artifacts/if_else_chain/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-6 Bytes
(96%)
tooling/nargo_cli/tests/acir_artifacts/integer_array_indexing/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+11 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/keccak256/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-6 Bytes
(97%)
tooling/nargo_cli/tests/acir_artifacts/main_bool_arg/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-77 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/merkle_insert/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-630 Bytes
(80%)
tooling/nargo_cli/tests/acir_artifacts/modulus/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-6 Bytes
(99%)
tooling/nargo_cli/tests/acir_artifacts/nested_arrays_from_brillig/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-17 Bytes
(93%)
tooling/nargo_cli/tests/acir_artifacts/pedersen_check/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-25 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/poseidon_bn254_hash/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-26 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/poseidonsponge_x5_254/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-10 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/regression/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-7 Bytes
(98%)
tooling/nargo_cli/tests/acir_artifacts/scalar_mul/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-157 Bytes
(96%)
tooling/nargo_cli/tests/acir_artifacts/schnorr/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+12 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/sha256/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+1 Byte
(100%)
tooling/nargo_cli/tests/acir_artifacts/sha2_blocks/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-207 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/sha2_byte/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-5 Bytes
(97%)
tooling/nargo_cli/tests/acir_artifacts/simple_2d_array/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-4 Bytes
(91%)
tooling/nargo_cli/tests/acir_artifacts/simple_array_param/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-55 Bytes
(97%)
tooling/nargo_cli/tests/acir_artifacts/simple_shield/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-63 Bytes
(100%)
tooling/nargo_cli/tests/acir_artifacts/slice_dynamic_index/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-36 Bytes
(98%)
tooling/nargo_cli/tests/acir_artifacts/strings/target/acir.gz
Binary file not shown.
Binary file modified
BIN
+1 Byte
(100%)
tooling/nargo_cli/tests/acir_artifacts/struct_array_inputs/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-20 Bytes
(95%)
tooling/nargo_cli/tests/acir_artifacts/struct_inputs/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-21 Bytes
(98%)
tooling/nargo_cli/tests/acir_artifacts/tuple_inputs/target/acir.gz
Binary file not shown.
Binary file modified
BIN
-5 Bytes
(97%)
tooling/nargo_cli/tests/acir_artifacts/type_aliases/target/acir.gz
Binary file not shown.