Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/array_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,59 @@
#[tracing::instrument(level = "trace", skip(self))]
pub(crate) fn array_set_optimization(mut self) -> Self {
for func in self.functions.values_mut() {
#[cfg(debug_assertions)]
array_set_optimization_pre_check(func);

func.array_set_optimization();

#[cfg(debug_assertions)]
array_set_optimization_post_check(func);
}
self
}
}

/// Pre-check condition for [Function::array_set_optimization].
///
/// Panics if:
/// - there already exists a mutable array set instruction.
#[cfg(debug_assertions)]
fn array_set_optimization_pre_check(func: &Function) {
// There should be no mutable array sets.
for block_id in func.reachable_blocks() {
let instruction_ids = func.dfg[block_id].instructions();
for instruction_id in instruction_ids {
if matches!(func.dfg[*instruction_id], Instruction::ArraySet { mutable: true, .. }) {
panic!("mutable ArraySet instruction exists before `array_set_optimization` pass");
}
}
}
}

/// Post-check condition for [Function::array_set_optimization].
///
/// Panics if:
/// - Mutable array_set optimization has been applied to Brillig function
#[cfg(debug_assertions)]
fn array_set_optimization_post_check(func: &Function) {
// Brillig functions should be not have any mutable array sets.
if func.runtime().is_brillig() {
for block_id in func.reachable_blocks() {
let instruction_ids = func.dfg[block_id].instructions();
for instruction_id in instruction_ids {
if matches!(func.dfg[*instruction_id], Instruction::ArraySet { mutable: true, .. })
{
panic!("Mutable array set instruction in Brillig function");
}
}
}
}
}

impl Function {
pub(crate) fn array_set_optimization(&mut self) {
if matches!(self.runtime(), RuntimeType::Brillig(_)) {
// Brillig is supposed to use refcounting to decide whether to mutate an array;

Check warning on line 75 in compiler/noirc_evaluator/src/ssa/opt/array_set.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (refcounting)
// array mutation was only meant for ACIR. We could use it with Brillig as well,
// but then some of the optimizations that we can do in ACIR around shared
// references have to be skipped, which makes it more cumbersome.
Expand Down
Loading