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

chore: refactor DataFlowGraph.insert_instruction_and_results #6823

Merged
merged 1 commit into from
Dec 16, 2024
Merged
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
56 changes: 46 additions & 10 deletions compiler/noirc_evaluator/src/ssa/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ impl DataFlowGraph {
id
}

fn insert_instruction_without_simplification(
&mut self,
instruction_data: Instruction,
block: BasicBlockId,
ctrl_typevars: Option<Vec<Type>>,
call_stack: CallStackId,
) -> InstructionId {
let id = self.make_instruction(instruction_data, ctrl_typevars);
self.blocks[block].insert_instruction(id);
self.locations.insert(id, call_stack);
id
}

pub(crate) fn insert_instruction_and_results_without_simplification(
&mut self,
instruction_data: Instruction,
block: BasicBlockId,
ctrl_typevars: Option<Vec<Type>>,
call_stack: CallStackId,
) -> InsertInstructionResult {
let id = self.insert_instruction_without_simplification(
instruction_data,
block,
ctrl_typevars,
call_stack,
);

InsertInstructionResult::Results(id, self.instruction_results(id))
}

/// Inserts a new instruction at the end of the given block and returns its results
pub(crate) fn insert_instruction_and_results(
&mut self,
Expand All @@ -184,7 +214,8 @@ impl DataFlowGraph {
result @ (SimplifyResult::SimplifiedToInstruction(_)
| SimplifyResult::SimplifiedToInstructionMultiple(_)
| SimplifyResult::None) => {
let instructions = result.instructions().unwrap_or(vec![instruction]);
let mut instructions = result.instructions().unwrap_or(vec![instruction]);
assert!(!instructions.is_empty(), "`SimplifyResult::SimplifiedToInstructionMultiple` must not return empty vector");

if instructions.len() > 1 {
// There's currently no way to pass results from one instruction in `instructions` on to the next.
Expand All @@ -196,17 +227,22 @@ impl DataFlowGraph {
);
}

let mut last_id = None;

// Pull off the last instruction as we want to return its results.
let last_instruction = instructions.pop().expect("`instructions` can't be empty");
for instruction in instructions {
let id = self.make_instruction(instruction, ctrl_typevars.clone());
self.blocks[block].insert_instruction(id);
self.locations.insert(id, call_stack);
last_id = Some(id);
self.insert_instruction_without_simplification(
instruction,
block,
ctrl_typevars.clone(),
call_stack,
);
}

let id = last_id.expect("There should be at least 1 simplified instruction");
InsertInstructionResult::Results(id, self.instruction_results(id))
self.insert_instruction_and_results_without_simplification(
last_instruction,
block,
ctrl_typevars,
call_stack,
)
}
}
}
Expand Down
Loading