Skip to content
Closed
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
9 changes: 9 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,15 @@ impl DataFlowGraph {
}
}

/// Mutates an instruction in-place.
pub(crate) fn mutate_instruction(
&mut self,
instruction_id: InstructionId,
mut f: impl FnMut(&mut Instruction),
) {
f(&mut self.instructions[instruction_id]);
}

/// Set the value of value_to_replace to refer to the value referred to by new_value.
///
/// This is the preferred method to call for optimizations simplifying
Expand Down
37 changes: 25 additions & 12 deletions compiler/noirc_evaluator/src/ssa/opt/remove_if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,17 @@ impl Context {
let instructions = function.dfg[block].take_instructions();
let one = FieldElement::one();
let mut current_conditional = function.dfg.make_constant(one, NumericType::bool());
let mut values_to_replace = HashMap::default();

for instruction_id in instructions {
// Before we process instructions, replace any values we previously determined we need to replace
if !values_to_replace.is_empty() {
function.dfg.mutate_instruction(instruction_id, |instruction| {
instruction.replace_values(&values_to_replace);
});
}

for instruction in instructions {
match &function.dfg[instruction] {
match &function.dfg[instruction_id] {
Instruction::IfElse { then_condition, then_value, else_condition, else_value } => {
let then_condition = *then_condition;
let else_condition = *else_condition;
Expand All @@ -74,7 +82,7 @@ impl Context {
let typ = function.dfg.type_of_value(then_value);
assert!(!matches!(typ, Type::Numeric(_)));

let call_stack = function.dfg.get_instruction_call_stack_id(instruction);
let call_stack = function.dfg.get_instruction_call_stack_id(instruction_id);
let mut value_merger = ValueMerger::new(
&mut function.dfg,
block,
Expand All @@ -92,20 +100,21 @@ impl Context {
);

let _typ = function.dfg.type_of_value(value);
let results = function.dfg.instruction_results(instruction);
let results = function.dfg.instruction_results(instruction_id);
let result = results[0];
// let result = match typ {
// Type::Array(..) => results[0],
// Type::Slice(..) => results[1],
// other => unreachable!("IfElse instructions should only have arrays or slices at this point. Found {other:?}"),
// };

function.dfg.set_value_from_id(result, value);
self.array_set_conditionals.insert(result, current_conditional);
values_to_replace.insert(result, value);

self.array_set_conditionals.insert(value, current_conditional);
}
Instruction::Call { func, arguments } => {
if let Value::Intrinsic(intrinsic) = function.dfg[*func] {
let results = function.dfg.instruction_results(instruction);
let results = function.dfg.instruction_results(instruction_id);

match slice_capacity_change(&function.dfg, intrinsic, arguments, results) {
SizeChange::None => (),
Expand All @@ -124,27 +133,31 @@ impl Context {
}
}
}
function.dfg[block].instructions_mut().push(instruction);
function.dfg[block].instructions_mut().push(instruction_id);
}
Instruction::ArraySet { array, .. } => {
let results = function.dfg.instruction_results(instruction);
let results = function.dfg.instruction_results(instruction_id);
let result = if results.len() == 2 { results[1] } else { results[0] };

self.array_set_conditionals.insert(result, current_conditional);

let old_capacity = self.get_or_find_capacity(&function.dfg, *array);
self.slice_sizes.insert(result, old_capacity);
function.dfg[block].instructions_mut().push(instruction);
function.dfg[block].instructions_mut().push(instruction_id);
}
Instruction::EnableSideEffectsIf { condition } => {
current_conditional = *condition;
function.dfg[block].instructions_mut().push(instruction);
function.dfg[block].instructions_mut().push(instruction_id);
}
_ => {
function.dfg[block].instructions_mut().push(instruction);
function.dfg[block].instructions_mut().push(instruction_id);
}
}
}

if !values_to_replace.is_empty() {
function.dfg.replace_values_in_block_terminator(block, &values_to_replace);
}
}

fn get_or_find_capacity(&mut self, dfg: &DataFlowGraph, value: ValueId) -> u32 {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading