Skip to content

Commit

Permalink
feat(ssa): Simplify array get from set that writes to the same dynami…
Browse files Browse the repository at this point in the history
…c index (noir-lang/noir#6684)

feat: Reduce memory consumption by storing array length as `u32` during SSA (noir-lang/noir#6606)
chore: add `ram_blowup_regression` to memory report (noir-lang/noir#6683)
chore: update noir-bench-report version (noir-lang/noir#6675)
fix: Prevent hoisting binary instructions which can overflow (noir-lang/noir#6672)
feat(ssa): Hoisting of array get using known induction variable maximum (noir-lang/noir#6639)
feat: better error message when trying to invoke struct function field (noir-lang/noir#6661)
feat: add memory report into the CI (noir-lang/noir#6630)
feat: allow ignoring test failures from foreign calls (noir-lang/noir#6660)
chore: refactor foreign call executors (noir-lang/noir#6659)
fix: correct signed integer handling in `noirc_abi` (noir-lang/noir#6638)
fix: allow multiple `_` parameters, and disallow `_` as an expression you can read from (noir-lang/noir#6657)
feat: allow filtering which SSA passes are printed (noir-lang/noir#6636)
fix: use correct type for attribute arguments (noir-lang/noir#6640)
fix: always return an array of `u8`s when simplifying `Intrinsic::ToRadix` calls (noir-lang/noir#6663)
feat(ssa): Option to set the maximum acceptable Brillig bytecode increase in unrolling (noir-lang/noir#6641)
feat: Sync from aztec-packages (noir-lang/noir#6656)
chore: refactor poseidon2 (noir-lang/noir#6655)
fix: correct types returned by constant EC operations simplified within SSA (noir-lang/noir#6652)
feat: Sync from aztec-packages (noir-lang/noir#6634)
fix: used signed division for signed modulo (noir-lang/noir#6635)
fix(ssa): don't deduplicate constraints in blocks that are not dominated (noir-lang/noir#6627)
chore: pin foundry version in CI (noir-lang/noir#6642)
feat(ssa): Deduplicate intrinsics with predicates (noir-lang/noir#6615)
chore: improve error message of `&T` (noir-lang/noir#6633)
fix: LSP code action wasn't triggering on beginning or end of identifier (noir-lang/noir#6616)
chore!: remove `ec` module from stdlib (noir-lang/noir#6612)
fix(LSP): use generic self type to narrow down methods to complete (noir-lang/noir#6617)
fix!: Disallow `#[export]` on associated methods (noir-lang/noir#6626)
chore: redo typo PR by donatik27 (noir-lang/noir#6575)
chore: redo typo PR by Dimitrolito (noir-lang/noir#6614)
feat: simplify `jmpif`s by reversing branches if condition is negated (noir-lang/noir#5891)
fix: Do not warn on unused functions marked with #[export] (noir-lang/noir#6625)
chore: Add panic for compiler error described in #6620 (noir-lang/noir#6621)
feat(perf): Track last loads per block in mem2reg and remove them if possible (noir-lang/noir#6088)
fix(ssa): Track all local allocations during flattening (noir-lang/noir#6619)
feat(comptime): Implement blackbox functions in comptime interpreter (noir-lang/noir#6551)
chore: derive PartialEq and Hash for FieldElement (noir-lang/noir#6610)
chore: ignore almost-empty directories in nargo_cli tests (noir-lang/noir#6611)
chore: remove temporary allocations from `num_bits` (noir-lang/noir#6600)
chore: Release Noir(1.0.0-beta.0) (noir-lang/noir#6562)
feat: Add `array_refcount` and `slice_refcount` builtins for debugging (noir-lang/noir#6584)
chore!: Require types of globals to be specified (noir-lang/noir#6592)
fix: don't report visibility errors when elaborating comptime value (noir-lang/noir#6498)
fix: preserve newlines between comments when formatting statements (noir-lang/noir#6601)
fix: parse a bit more SSA stuff (noir-lang/noir#6599)
chore!: remove eddsa from stdlib (noir-lang/noir#6591)
chore: Typo in oracles how to (noir-lang/noir#6598)
feat(ssa): Loop invariant code motion (noir-lang/noir#6563)
fix: remove `compiler_version` from new `Nargo.toml` (noir-lang/noir#6590)
feat: Avoid incrementing reference counts in some cases (noir-lang/noir#6568)
chore: fix typo in test name (noir-lang/noir#6589)
fix: consider prereleases to be compatible with pre-1.0.0 releases (noir-lang/noir#6580)
feat: try to inline brillig calls with all constant arguments  (noir-lang/noir#6548)
fix: correct type when simplifying `derive_pedersen_generators` (noir-lang/noir#6579)
feat: Sync from aztec-packages (noir-lang/noir#6576)
  • Loading branch information
AztecBot committed Dec 3, 2024
2 parents 30daa73 + 6bc6f32 commit a92b956
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6196d05bcb7ecd0b84fcc5ccc20d8dab99bc8052
304403f24e2a15b57bb054c4402a8d7f8d275668
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,31 @@ impl<'brillig> Context<'brillig> {
}
}

// If we have an array get whose value is from an array set on the same array at the same index,
// we can simplify that array get to the value of the previous array set.
//
// For example:
// v3 = array_set v0, index v1, value v2
// v4 = array_get v3, index v1 -> Field
//
// We know that `v4` can be simplified to `v2`.
// Thus, even if the index is dynamic (meaning the array get would have side effects),
// we can simplify the operation when we take into account the predicate.
if let Instruction::ArraySet { index, value, .. } = &instruction {
let use_predicate =
self.use_constraint_info && instruction.requires_acir_gen_predicate(dfg);
let predicate = use_predicate.then_some(side_effects_enabled_var);

let array_get = Instruction::ArrayGet { array: instruction_results[0], index: *index };

self.cached_instruction_results
.entry(array_get)
.or_default()
.entry(predicate)
.or_default()
.cache(block, vec![*value]);
}

// If the instruction doesn't have side-effects and if it won't interact with enable_side_effects during acir_gen,
// we cache the results so we can reuse them if the same instruction appears again later in the block.
// Others have side effects representing failure, which are implicit in the ACIR code and can also be deduplicated.
Expand Down Expand Up @@ -1521,4 +1546,49 @@ mod test {
let ssa = ssa.fold_constants_using_constraints();
assert_normalized_ssa_equals(ssa, expected);
}

#[test]
fn array_get_from_array_set_with_different_predicates() {
let src = "
acir(inline) fn main f0 {
b0(v0: [Field; 3], v1: u32, v2: Field):
enable_side_effects u1 0
v4 = array_set v0, index v1, value v2
enable_side_effects u1 1
v6 = array_get v4, index v1 -> Field
return v6
}
";

let ssa = Ssa::from_str(src).unwrap();

let ssa = ssa.fold_constants_using_constraints();
// We expect the code to be unchanged
assert_normalized_ssa_equals(ssa, src);
}

#[test]
fn array_get_from_array_set_same_predicates() {
let src = "
acir(inline) fn main f0 {
b0(v0: [Field; 3], v1: u32, v2: Field):
enable_side_effects u1 1
v4 = array_set v0, index v1, value v2
v6 = array_get v4, index v1 -> Field
return v6
}
";
let ssa = Ssa::from_str(src).unwrap();

let expected = "
acir(inline) fn main f0 {
b0(v0: [Field; 3], v1: u32, v2: Field):
enable_side_effects u1 1
v4 = array_set v0, index v1, value v2
return v2
}
";
let ssa = ssa.fold_constants_using_constraints();
assert_normalized_ssa_equals(ssa, expected);
}
}

0 comments on commit a92b956

Please sign in to comment.