Skip to content

Commit

Permalink
refactor: extracted casting to its own methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant committed Jul 18, 2023
1 parent c145a4d commit 522feee
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,9 @@ impl<'block> BrilligBlock<'block> {
}

// Perform necessary castings after gathering the passed parameters
for (param_id, param_type) in block.parameters().iter().zip(&param_types) {
if let Type::Array(..) = param_type {
// Arrays are passed between blocks as vectors, so we have to cast them back to an array
let passed_variable = self.function_context.get_or_create_parameter_variable(
self.brillig_context,
*param_id,
dfg,
);
let size = compute_size_of_type(param_type);
let passed_vector = self.function_context.extract_heap_vector(passed_variable);

let array = self.brillig_context.vector_to_array(&passed_vector, size);
self.function_context
.replace_variable(*param_id, RegisterOrMemory::HeapArray(array));
}
}
block.parameters().iter().for_each(|param_id| {
self.cast_back_from_interface(*param_id, dfg);
});
}

/// Converts an SSA instruction into a sequence of Brillig opcodes.
Expand Down Expand Up @@ -341,6 +328,11 @@ impl<'block> BrilligBlock<'block> {
);
self.brillig_context
.post_call_prep_returns_load_registers(&result_registers, &saved_registers);

// Cast back received results
result_ids.iter().for_each(|result| {
self.cast_back_from_interface(*result, dfg);
});
}
Value::Intrinsic(Intrinsic::BlackBox(bb_func)) => {
let function_arguments =
Expand Down Expand Up @@ -773,7 +765,7 @@ impl<'block> BrilligBlock<'block> {
variable
}

/// Between blocks and functions, we cast arrays to vectors, since array is a subtype of slice.
/// Between blocks and functions, we pass arrays as vectors, since array is a subtype of slice.
fn cast_for_interface(&mut self, variable: RegisterOrMemory) -> RegisterOrMemory {
match variable {
RegisterOrMemory::HeapArray(array) => {
Expand All @@ -783,6 +775,34 @@ impl<'block> BrilligBlock<'block> {
}
}

/// Between blocks and functions, we receive arrays as vectors, since array is a subtype of slice.
fn cast_back_from_interface(
&mut self,
value_id: ValueId,
dfg: &DataFlowGraph,
) -> RegisterOrMemory {
let typ = dfg.type_of_value(value_id);
let passed_variable = self.function_context.get_or_create_parameter_variable(
self.brillig_context,
value_id,
dfg,
);
if let Type::Array(..) = typ {
// Arrays are passed between blocks as vectors, so we have to cast them back to an array

let size = compute_size_of_type(&typ);
let passed_vector = self.function_context.extract_heap_vector(passed_variable);

let array = self.brillig_context.vector_to_array(&passed_vector, size);

let new_variable = RegisterOrMemory::HeapArray(array);
self.function_context.replace_variable(value_id, new_variable);

return new_variable;
}
passed_variable
}

/// Converts an SSA `ValueId` into a `RegisterIndex`. Initializes if necessary.
fn convert_ssa_register_value(
&mut self,
Expand Down

0 comments on commit 522feee

Please sign in to comment.