Skip to content
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
33 changes: 32 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ impl DataFlowGraph {
block: BasicBlockId,
ctrl_typevars: Option<Vec<Type>>,
call_stack: CallStackId,
) -> InsertInstructionResult {
self.insert_instruction_and_results_if_simplified(
instruction,
block,
ctrl_typevars,
call_stack,
None,
)
}

/// Simplifies a potentially existing instruction and inserts it only if it changed.
pub(crate) fn insert_instruction_and_results_if_simplified(
&mut self,
instruction: Instruction,
block: BasicBlockId,
ctrl_typevars: Option<Vec<Type>>,
call_stack: CallStackId,
existing_id: Option<InstructionId>,
) -> InsertInstructionResult {
if !self.is_handled_by_runtime(&instruction) {
panic!("Attempted to insert instruction not handled by runtime: {instruction:?}");
Expand All @@ -251,7 +269,20 @@ impl DataFlowGraph {
result @ (SimplifyResult::SimplifiedToInstruction(_)
| SimplifyResult::SimplifiedToInstructionMultiple(_)
| SimplifyResult::None) => {
let mut instructions = result.instructions().unwrap_or(vec![instruction]);
let instructions = result.instructions();
if instructions.is_none() {
if let Some(id) = existing_id {
if self[id] == instruction {
// Just (re)insert into the block, no need to redefine.
self.blocks[block].insert_instruction(id);
return InsertInstructionResult::Results(
id,
self.instruction_results(id),
);
}
}
}
let mut instructions = instructions.unwrap_or(vec![instruction]);
assert!(!instructions.is_empty(), "`SimplifyResult::SimplifiedToInstructionMultiple` must not return empty vector");

if instructions.len() > 1 {
Expand Down
20 changes: 12 additions & 8 deletions compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,18 @@ impl<'brillig> Context<'brillig> {
.then(|| vecmap(old_results, |result| dfg.type_of_value(*result)));

let call_stack = dfg.get_instruction_call_stack_id(id);
let new_results =
match dfg.insert_instruction_and_results(instruction, block, ctrl_typevars, call_stack)
{
InsertInstructionResult::SimplifiedTo(new_result) => vec![new_result],
InsertInstructionResult::SimplifiedToMultiple(new_results) => new_results,
InsertInstructionResult::Results(_, new_results) => new_results.to_vec(),
InsertInstructionResult::InstructionRemoved => vec![],
};
let new_results = match dfg.insert_instruction_and_results_if_simplified(
instruction,
block,
ctrl_typevars,
call_stack,
Some(id),
) {
InsertInstructionResult::SimplifiedTo(new_result) => vec![new_result],
InsertInstructionResult::SimplifiedToMultiple(new_results) => new_results,
InsertInstructionResult::Results(_, new_results) => new_results.to_vec(),
InsertInstructionResult::InstructionRemoved => vec![],
};
// Optimizations while inserting the instruction should not change the number of results.
assert_eq!(old_results.len(), new_results.len());

Expand Down