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(ssa refactor): hookup alloc + store instructions #1464

Merged
merged 17 commits into from
Jun 1, 2023
Merged
Changes from 14 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
38 changes: 29 additions & 9 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ impl Context {
let result_ids = dfg.instruction_results(instruction_id);
if Self::value_is_array_address(result_ids[0], dfg) {
self.track_array_address(result_ids[0], binary, dfg);
return;
(Vec::new(), Vec::new())
} else {
let result_acir_var = self.convert_ssa_binary(binary, dfg);
self.ssa_value_to_acir_var.insert(result_ids[0], result_acir_var);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
(vec![result_ids[0]], vec![result_acir_var])
}
let result_acir_var = self.convert_ssa_binary(binary, dfg);
self.ssa_value_to_acir_var.insert(result_ids[0], result_acir_var);
(vec![result_ids[0]], vec![result_acir_var])
}
Instruction::Constrain(value_id) => {
let constrain_condition = self.convert_ssa_value(*value_id, dfg);
Expand All @@ -159,11 +160,6 @@ impl Context {
let result_ids = dfg.instruction_results(instruction_id);
(vec![result_ids[0]], vec![result_acir_var])
}
Instruction::Load { address } => {
let result_acir_var = self.convert_ssa_load(address);
let result_ids = dfg.instruction_results(instruction_id);
(vec![result_ids[0]], vec![result_acir_var])
}
Instruction::Call { func, arguments } => {
let intrinsic = Self::id_to_intrinsic(*func, dfg);
let black_box = match intrinsic {
Expand All @@ -188,6 +184,21 @@ impl Context {
assert_eq!(result_ids.len(), 1, "Not ops have a single result");
(vec![result_ids[0]], vec![result_acir_var])
}
Instruction::Allocate { size } => {
let array_id = self.acir_context.allocate_array(*size as usize);
let result_ids = dfg.instruction_results(instruction_id);
self.ssa_value_to_array_address.insert(result_ids[0], (array_id, 0));
(Vec::new(), Vec::new())
}
Instruction::Store { address, value } => {
self.convert_ssa_store(address, value, dfg);
(Vec::new(), Vec::new())
}
Instruction::Load { address } => {
let result_acir_var = self.convert_ssa_load(address);
let result_ids = dfg.instruction_results(instruction_id);
(vec![result_ids[0]], vec![result_acir_var])
}
_ => todo!("{instruction:?}"),
};

Expand Down Expand Up @@ -291,6 +302,15 @@ impl Context {
}
}

/// Stores the `AcirVar` corresponding to `value` at the `ArrayId` and index corresponding to
/// `address`.
fn convert_ssa_store(&mut self, address: &ValueId, value: &ValueId, dfg: &DataFlowGraph) {
let element_var = self.convert_ssa_value(*value, dfg);
let (array_id, index) =
self.ssa_value_to_array_address.get(address).expect("ICE: Load from undeclared array");
self.acir_context.array_store(*array_id, *index, element_var).expect("invalid array load");
}

/// Returns the `AcirVar` that was previously stored at the given address.
fn convert_ssa_load(&mut self, address: &ValueId) -> AcirVar {
let (array_id, index) =
Expand Down