Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(perf): Follow array sets backwards in array set from get optimization #6208

Merged
merged 16 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
};
use acvm::acir::{brillig::MemoryAddress, AcirField};

pub(crate) const MAX_STACK_SIZE: usize = 32768;
pub(crate) const MAX_STACK_SIZE: usize = 2048;
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) const MAX_SCRATCH_SPACE: usize = 64;

impl<F: AcirField + DebugToString> BrilligContext<F, Stack> {
Expand Down Expand Up @@ -158,7 +158,7 @@
}

for (i, bit_size) in arguments.iter().flat_map(flat_bit_sizes).enumerate() {
// Calldatacopy tags everything with field type, so when downcast when necessary

Check warning on line 161 in compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Calldatacopy)
if bit_size < F::max_num_bits() {
self.cast_instruction(
SingleAddrVariable::new(
Expand Down
58 changes: 52 additions & 6 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,25 +818,71 @@ fn try_optimize_array_get_from_previous_set(
SimplifyResult::None
}

// If we have an array set whose value is from an array get on the same array at the same index,
// we can simplify that array set to the array we were looking to perform an array set upon.
//
// Simple case:
// v3 = array_get v1, index v2
// v5 = array_set v1, index v2, value v3
//
// If we could not immediately simplify the array set from its value, we can try to follow
// the array set backwards:
//
// v3 = array_get v1, index v2
// v5 = array_set v1, index v4, value [Field 100, Field 101, Field 102]
// v7 = array_set mut v5, index v2, value v3
//
// We want to optimize `v7` to `v5`. We see that `v3` comes from an array get to `v1`. We follow `v5` backwards and see an array set
// to `v1` and that array set occurs to a different index. We now know we can simplify `v7` to `v5` as it is unchanged.
fn try_optimize_array_set_from_previous_get(
dfg: &DataFlowGraph,
array_id: ValueId,
mut array_id: ValueId,
target_index: ValueId,
target_value: ValueId,
) -> SimplifyResult {
match &dfg[target_value] {
let array_from_get = match &dfg[target_value] {
Value::Instruction { instruction, .. } => match &dfg[*instruction] {
Instruction::ArrayGet { array, index } => {
if *array == array_id && *index == target_index {
SimplifyResult::SimplifiedTo(array_id)
// If array and index match from the value, we can immediately simplify
return SimplifyResult::SimplifiedTo(array_id);
} else if *index == target_index {
Some(*array)
} else {
SimplifyResult::None
None
}
}
_ => SimplifyResult::None,
_ => None,
},
_ => SimplifyResult::None,
_ => None,
};

let original_array_id = array_id;
// Arbitrary number of maximum tries just to prevent this optimization from taking too long.
let max_tries = 5;
for _ in 0..max_tries {
match &dfg[array_id] {
Value::Instruction { instruction, .. } => match &dfg[*instruction] {
Instruction::ArraySet { array, index, .. } => {
if *index == target_index {
return SimplifyResult::None;
}

if let Some(array_id) = array_from_get {
if *array == array_id {
return SimplifyResult::SimplifiedTo(original_array_id);
}
}

array_id = *array; // recur
}
_ => return SimplifyResult::None,
},
_ => return SimplifyResult::None,
}
}

SimplifyResult::None
}

pub(crate) type ErrorType = HirType;
Expand Down
Loading