Skip to content

Commit

Permalink
Exit after optimisation if it hasn't changed anything
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Dec 4, 2024
1 parent b163c20 commit 371678e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion acvm-repo/acvm/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use transformers::transform_internal;
pub use transformers::MIN_EXPRESSION_WIDTH;

/// We need multiple passes to stabilize the output.
/// The value was determined by running tests.
const MAX_OPTIMIZER_PASSES: usize = 3;

/// This module moves and decomposes acir opcodes. The transformation map allows consumers of this module to map
Expand Down Expand Up @@ -89,14 +90,22 @@ pub fn compile<F: AcirField>(
// but some of them don't stabilize unless we also repeat the backend agnostic optimizations.
let (mut acir, acir_opcode_positions) = loop {
let (acir, acir_opcode_positions) = optimize_internal(prev_acir);

let opcodes_hash = fxhash::hash64(&acir.opcodes);
// Stop if we have already done at least one transform and an extra optimization changed nothing.
if pass > 0 && prev_opcodes_hash == opcodes_hash {
break (acir, acir_opcode_positions);
}

let (acir, acir_opcode_positions) =
transform_internal(acir, expression_width, acir_opcode_positions);

let opcodes_hash = fxhash::hash64(&acir.opcodes);

// Stop if the output hasn't change in this loop or we went too long.
if pass == MAX_OPTIMIZER_PASSES || prev_opcodes_hash == opcodes_hash {
break (acir, acir_opcode_positions);
}

pass += 1;
prev_acir = acir;
prev_opcodes_hash = opcodes_hash
Expand Down

0 comments on commit 371678e

Please sign in to comment.