Skip to content

Commit

Permalink
Merge 02ef8b7 into e973397
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Dec 11, 2024
2 parents e973397 + 02ef8b7 commit d281663
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ pub(super) fn compile_array_copy_procedure<F: AcirField + DebugToString>(
BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
1_usize.into(),
);
// Decrease the original ref count now that this copy is no longer pointing to it
ctx.codegen_usize_op(rc.address, rc.address, BrilligBinaryOp::Sub, 1);
}
});
}
24 changes: 7 additions & 17 deletions compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,38 +442,29 @@ impl FunctionBuilder {
/// Insert instructions to increment the reference count of any array(s) stored
/// within the given value. If the given value is not an array and does not contain
/// any arrays, this does nothing.
///
/// Returns whether a reference count instruction was issued.
pub(crate) fn increment_array_reference_count(&mut self, value: ValueId) -> bool {
self.update_array_reference_count(value, true)
pub(crate) fn increment_array_reference_count(&mut self, value: ValueId) {
self.update_array_reference_count(value, true);
}

/// Insert instructions to decrement the reference count of any array(s) stored
/// within the given value. If the given value is not an array and does not contain
/// any arrays, this does nothing.
///
/// Returns whether a reference count instruction was issued.
pub(crate) fn decrement_array_reference_count(&mut self, value: ValueId) -> bool {
self.update_array_reference_count(value, false)
pub(crate) fn decrement_array_reference_count(&mut self, value: ValueId) {
self.update_array_reference_count(value, false);
}

/// Increment or decrement the given value's reference count if it is an array.
/// If it is not an array, this does nothing. Note that inc_rc and dec_rc instructions
/// are ignored outside of unconstrained code.
///
/// Returns whether a reference count instruction was issued.
fn update_array_reference_count(&mut self, value: ValueId, increment: bool) -> bool {
fn update_array_reference_count(&mut self, value: ValueId, increment: bool) {
match self.type_of_value(value) {
Type::Numeric(_) => false,
Type::Function => false,
Type::Numeric(_) => (),
Type::Function => (),
Type::Reference(element) => {
if element.contains_an_array() {
let reference = value;
let value = self.insert_load(reference, element.as_ref().clone());
self.update_array_reference_count(value, increment);
true
} else {
false
}
}
Type::Array(..) | Type::Slice(..) => {
Expand All @@ -484,7 +475,6 @@ impl FunctionBuilder {
} else {
self.insert_dec_rc(value);
}
true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/function_inserter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'f> FunctionInserter<'f> {
// another MakeArray instruction. Note that this assumes the function inserter is inserting
// in control-flow order. Otherwise we could refer to ValueIds defined later in the program.
let make_array = if let Instruction::MakeArray { elements, typ } = &instruction {
if self.array_is_constant(elements) && self.function.runtime().is_acir() {
if self.array_is_constant(elements) {
if let Some(fetched_value) = self.get_cached_array(elements, typ) {
assert_eq!(results.len(), 1);
self.values.insert(results[0], fetched_value);
Expand Down
13 changes: 5 additions & 8 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ impl Instruction {
/// conditional on whether the caller wants the predicate to be taken into account or not.
pub(crate) fn can_be_deduplicated(
&self,
function: &Function,
dfg: &DataFlowGraph,
deduplicate_with_predicate: bool,
) -> bool {
use Instruction::*;
Expand All @@ -458,7 +458,7 @@ impl Instruction {
| IncrementRc { .. }
| DecrementRc { .. } => false,

Call { func, .. } => match function.dfg[*func] {
Call { func, .. } => match dfg[*func] {
Value::Intrinsic(intrinsic) => {
intrinsic.can_be_deduplicated(deduplicate_with_predicate)
}
Expand All @@ -468,11 +468,8 @@ impl Instruction {
// We can deduplicate these instructions if we know the predicate is also the same.
Constrain(..) | RangeCheck { .. } => deduplicate_with_predicate,

// Arrays can be mutated in unconstrained code so code that handles this case must
// take care to track whether the array was possibly mutated or not before
// deduplicating. Since we don't know if the containing pass checks for this, we
// can only assume these are safe to deduplicate in constrained code.
MakeArray { .. } => function.runtime().is_acir(),
// This should never be side-effectful
MakeArray { .. } => true,

// These can have different behavior depending on the EnableSideEffectsIf context.
// Replacing them with a similar instruction potentially enables replacing an instruction
Expand All @@ -485,7 +482,7 @@ impl Instruction {
| IfElse { .. }
| ArrayGet { .. }
| ArraySet { .. } => {
deduplicate_with_predicate || !self.requires_acir_gen_predicate(&function.dfg)
deduplicate_with_predicate || !self.requires_acir_gen_predicate(dfg)
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions compiler/noirc_evaluator/src/ssa/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,6 @@ impl Type {
}
}

/// Retrieves the array or slice type within this type, or panics if there is none.
pub(crate) fn get_contained_array(&self) -> &Type {
match self {
Type::Numeric(_) | Type::Function => panic!("Expected an array type"),
Type::Array(_, _) | Type::Slice(_) => self,
Type::Reference(element) => element.get_contained_array(),
}
}

pub(crate) fn element_types(self) -> Arc<Vec<Type>> {
match self {
Type::Array(element_types, _) | Type::Slice(element_types) => element_types,
Expand Down
81 changes: 25 additions & 56 deletions compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Function {
}

context.visited_blocks.insert(block);
context.fold_constants_in_block(self, &mut dom, block);
context.fold_constants_in_block(&mut self.dfg, &mut dom, block);
}
}
}
Expand Down Expand Up @@ -268,38 +268,37 @@ impl<'brillig> Context<'brillig> {

fn fold_constants_in_block(
&mut self,
function: &mut Function,
dfg: &mut DataFlowGraph,
dom: &mut DominatorTree,
block: BasicBlockId,
) {
let instructions = function.dfg[block].take_instructions();
let instructions = dfg[block].take_instructions();

// Default side effect condition variable with an enabled state.
let mut side_effects_enabled_var =
function.dfg.make_constant(FieldElement::one(), NumericType::bool());
dfg.make_constant(FieldElement::one(), NumericType::bool());

for instruction_id in instructions {
self.fold_constants_into_instruction(
function,
dfg,
dom,
block,
instruction_id,
&mut side_effects_enabled_var,
);
}
self.block_queue.extend(function.dfg[block].successors());
self.block_queue.extend(dfg[block].successors());
}

fn fold_constants_into_instruction(
&mut self,
function: &mut Function,
dfg: &mut DataFlowGraph,
dom: &mut DominatorTree,
mut block: BasicBlockId,
id: InstructionId,
side_effects_enabled_var: &mut ValueId,
) {
let constraint_simplification_mapping = self.get_constraint_map(*side_effects_enabled_var);
let dfg = &mut function.dfg;

let instruction =
Self::resolve_instruction(id, block, dfg, dom, constraint_simplification_mapping);
Expand All @@ -312,15 +311,6 @@ impl<'brillig> Context<'brillig> {
{
match cache_result {
CacheResult::Cached(cached) => {
// We track whether we may mutate MakeArray instructions before we deduplicate
// them but we still need to issue an extra inc_rc in case they're mutated afterward.
if matches!(instruction, Instruction::MakeArray { .. }) {
let value = *cached.last().unwrap();
let inc_rc = Instruction::IncrementRc { value };
let call_stack = dfg.get_call_stack(id);
dfg.insert_instruction_and_results(inc_rc, block, None, call_stack);
}

Self::replace_result_ids(dfg, &old_results, cached);
return;
}
Expand All @@ -334,25 +324,32 @@ impl<'brillig> Context<'brillig> {
}
};

let new_results =
// First try to inline a call to a brillig function with all constant arguments.
let new_results = Self::try_inline_brillig_call_with_all_constants(
Self::try_inline_brillig_call_with_all_constants(
&instruction,
&old_results,
block,
dfg,
self.brillig_info,
)
// Otherwise, try inserting the instruction again to apply any optimizations using the newly resolved inputs.
.unwrap_or_else(|| {
Self::push_instruction(id, instruction.clone(), &old_results, block, dfg)
// Otherwise, try inserting the instruction again to apply any optimizations using the newly resolved inputs.
Self::push_instruction(
id,
instruction.clone(),
&old_results,
block,
dfg,
)
});

Self::replace_result_ids(dfg, &old_results, &new_results);

self.cache_instruction(
instruction.clone(),
new_results,
function,
dfg,
*side_effects_enabled_var,
block,
);
Expand Down Expand Up @@ -439,7 +436,7 @@ impl<'brillig> Context<'brillig> {
&mut self,
instruction: Instruction,
instruction_results: Vec<ValueId>,
function: &Function,
dfg: &DataFlowGraph,
side_effects_enabled_var: ValueId,
block: BasicBlockId,
) {
Expand All @@ -448,11 +445,11 @@ impl<'brillig> Context<'brillig> {
// to map from the more complex to the simpler value.
if let Instruction::Constrain(lhs, rhs, _) = instruction {
// These `ValueId`s should be fully resolved now.
if let Some((complex, simple)) = simplify(&function.dfg, lhs, rhs) {
if let Some((complex, simple)) = simplify(dfg, lhs, rhs) {
self.get_constraint_map(side_effects_enabled_var)
.entry(complex)
.or_default()
.add(&function.dfg, simple, block);
.add(dfg, simple, block);
}
}
}
Expand All @@ -469,7 +466,7 @@ impl<'brillig> Context<'brillig> {
// 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(&function.dfg);
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 };
Expand All @@ -482,19 +479,12 @@ impl<'brillig> Context<'brillig> {
.cache(block, vec![*value]);
}

self.remove_possibly_mutated_cached_make_arrays(&instruction, function);

// 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.
let can_be_deduplicated =
instruction.can_be_deduplicated(function, self.use_constraint_info);

// We also allow deduplicating MakeArray instructions that we have tracked which haven't
// been mutated.
if can_be_deduplicated || matches!(instruction, Instruction::MakeArray { .. }) {
if instruction.can_be_deduplicated(dfg, self.use_constraint_info) {
let use_predicate =
self.use_constraint_info && instruction.requires_acir_gen_predicate(&function.dfg);
self.use_constraint_info && instruction.requires_acir_gen_predicate(dfg);
let predicate = use_predicate.then_some(side_effects_enabled_var);

self.cached_instruction_results
Expand Down Expand Up @@ -701,26 +691,6 @@ impl<'brillig> Context<'brillig> {
}
}
}

fn remove_possibly_mutated_cached_make_arrays(
&mut self,
instruction: &Instruction,
function: &Function,
) {
use Instruction::{ArraySet, Store};

// Should we consider calls to slice_push_back and similar to be mutating operations as well?
if let Store { value: array, .. } | ArraySet { array, .. } = instruction {
let instruction = match &function.dfg[*array] {
Value::Instruction { instruction, .. } => &function.dfg[*instruction],
_ => return,
};

if matches!(instruction, Instruction::MakeArray { .. }) {
self.cached_instruction_results.remove(instruction);
}
}
}
}

impl ResultCache {
Expand Down Expand Up @@ -1184,7 +1154,6 @@ mod test {
// fn main f0 {
// b0(v0: u64):
// v1 = make_array [v0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0, u64 0]
// inc_rc v1
// v5 = call keccakf1600(v1)
// }
let ssa = ssa.fold_constants();
Expand All @@ -1194,7 +1163,7 @@ mod test {
let main = ssa.main();
let instructions = main.dfg[main.entry_block()].instructions();
let ending_instruction_count = instructions.len();
assert_eq!(ending_instruction_count, 3);
assert_eq!(ending_instruction_count, 2);
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ impl<'f> LoopInvariantContext<'f> {
!self.defined_in_loop.contains(&value) || self.loop_invariants.contains(&value);
});

let can_be_deduplicated = instruction.can_be_deduplicated(self.inserter.function, false)
let can_be_deduplicated = instruction
.can_be_deduplicated(&self.inserter.function.dfg, false)
|| self.can_be_deduplicated_from_upper_bound(&instruction);

is_loop_invariant && can_be_deduplicated
Expand Down
Loading

0 comments on commit d281663

Please sign in to comment.