Skip to content

Commit

Permalink
Decouple acir blokid from ssa valueid
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic committed Aug 1, 2023
1 parent a484a31 commit 355b357
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ struct Context {
/// This set is used to ensure that a MemoryOp opcode is only pushed to the circuit
/// if there is already a MemoryInit opcode.
initialized_arrays: HashSet<BlockId>,

/// Maps SSA values to BlockId
/// A BlockId is an ACIR structure which identifies a memory block
/// Each acir memory block corresponds to a different SSA array.
memory_blocks: HashMap<Id<Value>, BlockId>,

/// Number of the next BlockId, it is used to construct
/// a new BlockId
max_block_id: u32,
}

#[derive(Clone)]
Expand Down Expand Up @@ -140,6 +149,8 @@ impl Context {
current_side_effects_enabled_var,
acir_context,
initialized_arrays: HashSet::new(),
memory_blocks: HashMap::new(),
max_block_id: 0,
}
}

Expand Down Expand Up @@ -228,7 +239,7 @@ impl Context {
match &value {
AcirValue::Var(_, _) => (),
AcirValue::Array(values) => {
let block_id = BlockId(param_id.to_usize() as u32);
let block_id = self.block_id(param_id);
let v = vecmap(values, |v| v.clone());
self.initialize_array(block_id, values.len(), Some(&v))?;
}
Expand Down Expand Up @@ -271,6 +282,18 @@ impl Context {
}
}

/// Get the BlockId corresponding to the ValueId
/// If there is no matching BlockId, we create a new one.
fn block_id(&mut self, value: &ValueId) -> BlockId {
if let Some(block_id) = self.memory_blocks.get(value) {
return *block_id;
}
let block_id = BlockId(self.max_block_id);
self.max_block_id += 1;
self.memory_blocks.insert(*value, block_id);
block_id
}

/// Creates an `AcirVar` corresponding to a parameter witness to appears in the abi. A range
/// constraint is added if the numeric type requires it.
///
Expand Down Expand Up @@ -513,7 +536,7 @@ impl Context {
dfg: &DataFlowGraph,
) -> Result<(), RuntimeError> {
let array = dfg.resolve(array);
let block_id = BlockId(array.to_usize() as u32);
let block_id = self.block_id(&array);
if !self.initialized_arrays.contains(&block_id) {
match &dfg[array] {
Value::Array { array, .. } => {
Expand Down Expand Up @@ -561,11 +584,9 @@ impl Context {
) -> Result<(), InternalError> {
// Fetch the internal SSA ID for the array
let array = dfg.resolve(array);
let array_ssa_id = array.to_usize() as u32;

// Use the SSA ID to create a block ID
// There is currently a 1-1 mapping from array SSA ID to block ID
let block_id = BlockId(array_ssa_id);
// Use the SSA ID to get or create its block ID
let block_id = self.block_id(&array);

// Every array has a length in its type, so we fetch that from
// the SSA IR.
Expand Down Expand Up @@ -599,8 +620,7 @@ impl Context {
.instruction_results(instruction)
.first()
.expect("Array set does not have one result");
let result_array_id = result_id.to_usize() as u32;
let result_block_id = BlockId(result_array_id);
let result_block_id = self.block_id(result_id);

// Initialize the new array with zero values
self.initialize_array(result_block_id, len, None)?;
Expand Down

0 comments on commit 355b357

Please sign in to comment.