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): Optimize array set from get #6207

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
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
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 = 2048;
pub(crate) const MAX_STACK_SIZE: usize = 32768;
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
30 changes: 26 additions & 4 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,9 @@ impl Instruction {
}
}
Instruction::ArraySet { array, index, value, .. } => {
let array = dfg.get_array_constant(*array);
let index = dfg.get_numeric_constant(*index);
if let (Some((array, element_type)), Some(index)) = (array, index) {
let array_const = dfg.get_array_constant(*array);
let index_const = dfg.get_numeric_constant(*index);
if let (Some((array, element_type)), Some(index)) = (array_const, index_const) {
let index =
index.try_to_u32().expect("Expected array index to fit in u32") as usize;

Expand All @@ -641,7 +641,8 @@ impl Instruction {
return SimplifiedTo(new_array);
}
}
None

try_optimize_array_set_from_previous_get(dfg, *array, *index, *value)
}
Instruction::Truncate { value, bit_size, max_bit_size } => {
if bit_size == max_bit_size {
Expand Down Expand Up @@ -817,6 +818,27 @@ fn try_optimize_array_get_from_previous_set(
SimplifyResult::None
}

fn try_optimize_array_set_from_previous_get(
dfg: &DataFlowGraph,
array_id: ValueId,
target_index: ValueId,
target_value: ValueId,
) -> SimplifyResult {
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)
} else {
SimplifyResult::None
}
}
_ => SimplifyResult::None,
},
_ => SimplifyResult::None,
}
}

pub(crate) type ErrorType = HirType;

pub(crate) fn error_selector_from_type(typ: &ErrorType) -> ErrorSelector {
Expand Down
Loading