Skip to content

Commit

Permalink
feat: pass brillig bytecode to VM by reference (#3030)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
TomAFrench and kevaundray authored Oct 9, 2023
1 parent 0bc2e4d commit 4ee290b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 78 deletions.
2 changes: 1 addition & 1 deletion acvm-repo/acvm/src/pwg/brillig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl BrilligSolver {
let mut vm = VM::new(
input_registers,
input_memory,
brillig.bytecode.clone(),
&brillig.bytecode,
brillig.foreign_call_results.clone(),
bb_solver,
);
Expand Down
65 changes: 31 additions & 34 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub enum VMStatus {

#[derive(Debug, PartialEq, Eq, Clone)]
/// VM encapsulates the state of the Brillig VM during execution.
pub struct VM<'bb_solver, B: BlackBoxFunctionSolver> {
pub struct VM<'a, B: BlackBoxFunctionSolver> {
/// Register storage
registers: Registers,
/// Instruction pointer
Expand All @@ -72,25 +72,25 @@ pub struct VM<'bb_solver, B: BlackBoxFunctionSolver> {
/// List is appended onto by the caller upon reaching a [VMStatus::ForeignCallWait]
foreign_call_results: Vec<ForeignCallResult>,
/// Executable opcodes
bytecode: Vec<Opcode>,
bytecode: &'a [Opcode],
/// Status of the VM
status: VMStatus,
/// Memory of the VM
memory: Memory,
/// Call stack
call_stack: Vec<Value>,
/// The solver for blackbox functions
black_box_solver: &'bb_solver B,
black_box_solver: &'a B,
}

impl<'bb_solver, B: BlackBoxFunctionSolver> VM<'bb_solver, B> {
impl<'a, B: BlackBoxFunctionSolver> VM<'a, B> {
/// Constructs a new VM instance
pub fn new(
inputs: Registers,
memory: Vec<Value>,
bytecode: Vec<Opcode>,
bytecode: &'a [Opcode],
foreign_call_results: Vec<ForeignCallResult>,
black_box_solver: &'bb_solver B,
black_box_solver: &'a B,
) -> Self {
Self {
registers: inputs,
Expand Down Expand Up @@ -443,7 +443,8 @@ mod tests {
};

// Start VM
let mut vm = VM::new(input_registers, vec![], vec![opcode], vec![], &DummyBlackBoxSolver);
let opcodes = [opcode];
let mut vm = VM::new(input_registers, vec![], &opcodes, vec![], &DummyBlackBoxSolver);

// Process a single VM opcode
//
Expand Down Expand Up @@ -487,7 +488,7 @@ mod tests {
opcodes.push(Opcode::JumpIf { condition: RegisterIndex::from(2), location: 3 });

let mut vm =
VM::new(Registers::load(registers), vec![], opcodes, vec![], &DummyBlackBoxSolver);
VM::new(Registers::load(registers), vec![], &opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
Expand Down Expand Up @@ -528,13 +529,9 @@ mod tests {
destination: RegisterIndex::from(2),
};

let mut vm = VM::new(
input_registers,
vec![],
vec![jump_opcode, trap_opcode, not_equal_cmp_opcode, jump_if_not_opcode, add_opcode],
vec![],
&DummyBlackBoxSolver,
);
let opcodes =
[jump_opcode, trap_opcode, not_equal_cmp_opcode, jump_if_not_opcode, add_opcode];
let mut vm = VM::new(input_registers, vec![], &opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
Expand Down Expand Up @@ -571,8 +568,8 @@ mod tests {
let mov_opcode =
Opcode::Mov { destination: RegisterIndex::from(2), source: RegisterIndex::from(0) };

let mut vm =
VM::new(input_registers, vec![], vec![mov_opcode], vec![], &DummyBlackBoxSolver);
let opcodes = &[mov_opcode];
let mut vm = VM::new(input_registers, vec![], opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::Finished);
Expand Down Expand Up @@ -629,13 +626,8 @@ mod tests {
destination: RegisterIndex::from(2),
};

let mut vm = VM::new(
input_registers,
vec![],
vec![equal_opcode, not_equal_opcode, less_than_opcode, less_than_equal_opcode],
vec![],
&DummyBlackBoxSolver,
);
let opcodes = [equal_opcode, not_equal_opcode, less_than_opcode, less_than_equal_opcode];
let mut vm = VM::new(input_registers, vec![], &opcodes, vec![], &DummyBlackBoxSolver);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::InProgress);
Expand Down Expand Up @@ -705,7 +697,9 @@ mod tests {
// if tmp != 0 goto loop_body
Opcode::JumpIf { condition: r_tmp, location: start.len() },
];
let vm = brillig_execute_and_get_vm(memory, [&start[..], &loop_body[..]].concat());

let opcodes = [&start[..], &loop_body[..]].concat();
let vm = brillig_execute_and_get_vm(memory, &opcodes);
vm.get_memory().clone()
}

Expand Down Expand Up @@ -780,7 +774,9 @@ mod tests {
// if tmp != 0 goto loop_body
Opcode::JumpIf { condition: r_tmp, location: start.len() },
];
let vm = brillig_execute_and_get_vm(memory, [&start[..], &loop_body[..]].concat());

let opcodes = [&start[..], &loop_body[..]].concat();
let vm = brillig_execute_and_get_vm(memory, &opcodes);
vm.registers.get(r_sum)
}

Expand Down Expand Up @@ -858,7 +854,8 @@ mod tests {
Opcode::Return {},
];

let vm = brillig_execute_and_get_vm(memory, [&start[..], &recursive_fn[..]].concat());
let opcodes = [&start[..], &recursive_fn[..]].concat();
let vm = brillig_execute_and_get_vm(memory, &opcodes);
vm.get_memory().clone()
}

Expand All @@ -883,8 +880,8 @@ mod tests {
/// Helper to execute brillig code
fn brillig_execute_and_get_vm(
memory: Vec<Value>,
opcodes: Vec<Opcode>,
) -> VM<'static, DummyBlackBoxSolver> {
opcodes: &[Opcode],
) -> VM<'_, DummyBlackBoxSolver> {
let mut vm = VM::new(empty_registers(), memory, opcodes, vec![], &DummyBlackBoxSolver);
brillig_execute(&mut vm);
assert_eq!(vm.call_stack, vec![]);
Expand Down Expand Up @@ -917,7 +914,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(vec![], double_program);
let mut vm = brillig_execute_and_get_vm(vec![], &double_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -978,7 +975,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), invert_program);
let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), &invert_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -1051,7 +1048,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(input_string.clone(), string_double_program);
let mut vm = brillig_execute_and_get_vm(input_string.clone(), &string_double_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -1113,7 +1110,7 @@ mod tests {
},
];

let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), invert_program);
let mut vm = brillig_execute_and_get_vm(initial_matrix.clone(), &invert_program);

// Check that VM is waiting
assert_eq!(
Expand Down Expand Up @@ -1198,7 +1195,7 @@ mod tests {
];
let mut initial_memory = matrix_a.clone();
initial_memory.extend(matrix_b.clone());
let mut vm = brillig_execute_and_get_vm(initial_memory, matrix_mul_program);
let mut vm = brillig_execute_and_get_vm(initial_memory, &matrix_mul_program);

// Check that VM is waiting
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ mod tests {
use crate::brillig::brillig_gen::brillig_block_variables::BlockVariables;
use crate::brillig::brillig_gen::brillig_fn::FunctionContext;
use crate::brillig::brillig_ir::artifact::BrilligParameter;
use crate::brillig::brillig_ir::tests::{create_and_run_vm, create_context};
use crate::brillig::brillig_ir::tests::{
create_and_run_vm, create_context, create_entry_point_bytecode,
};
use crate::brillig::brillig_ir::BrilligContext;
use crate::ssa::function_builder::FunctionBuilder;
use crate::ssa::ir::function::RuntimeType;
Expand Down Expand Up @@ -399,12 +401,11 @@ mod tests {

context.return_instruction(&[copied_array_pointer, copied_array_size]);

let bytecode = create_entry_point_bytecode(context, arguments, returns).byte_code;
let vm = create_and_run_vm(
array.clone(),
vec![Value::from(0_usize), item_to_push],
context,
arguments,
returns,
&bytecode,
);

assert_eq!(vm.get_memory(), &expected_mem);
Expand Down Expand Up @@ -493,13 +494,8 @@ mod tests {

context.return_instruction(&[copied_array_pointer, copied_array_size, removed_item]);

let vm = create_and_run_vm(
array.clone(),
vec![Value::from(0_usize)],
context,
arguments,
returns,
);
let bytecode = create_entry_point_bytecode(context, arguments, returns).byte_code;
let vm = create_and_run_vm(array.clone(), vec![Value::from(0_usize)], &bytecode);

assert_eq!(vm.get_memory(), &expected_mem);

Expand Down Expand Up @@ -584,12 +580,11 @@ mod tests {

context.return_instruction(&[copied_array_pointer, copied_array_size]);

let bytecode = create_entry_point_bytecode(context, arguments, returns).byte_code;
let vm = create_and_run_vm(
array.clone(),
vec![Value::from(0_usize), item, index],
context,
arguments,
returns,
&bytecode,
);

assert_eq!(vm.get_memory(), &expected_mem);
Expand Down Expand Up @@ -707,13 +702,8 @@ mod tests {

context.return_instruction(&[copied_array_pointer, copied_array_size, removed_item]);

let vm = create_and_run_vm(
array.clone(),
vec![Value::from(0_usize), index],
context,
arguments,
returns,
);
let bytecode = create_entry_point_bytecode(context, arguments, returns).byte_code;
let vm = create_and_run_vm(array.clone(), vec![Value::from(0_usize), index], &bytecode);

assert_eq!(vm.get_memory(), &expected_mem);

Expand Down
10 changes: 4 additions & 6 deletions compiler/noirc_evaluator/src/brillig/brillig_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,14 +1069,12 @@ pub(crate) mod tests {
pub(crate) fn create_and_run_vm(
memory: Vec<Value>,
param_registers: Vec<Value>,
context: BrilligContext,
arguments: Vec<BrilligParameter>,
returns: Vec<BrilligParameter>,
) -> VM<'static, DummyBlackBoxSolver> {
bytecode: &[BrilligOpcode],
) -> VM<'_, DummyBlackBoxSolver> {
let mut vm = VM::new(
Registers { inner: param_registers },
memory,
create_entry_point_bytecode(context, arguments, returns).byte_code,
bytecode,
vec![],
&DummyBlackBoxSolver,
);
Expand Down Expand Up @@ -1132,7 +1130,7 @@ pub(crate) mod tests {
let mut vm = VM::new(
Registers { inner: vec![] },
vec![],
bytecode,
&bytecode,
vec![ForeignCallResult { values: vec![ForeignCallParam::Array(number_sequence)] }],
&DummyBlackBoxSolver,
);
Expand Down
20 changes: 5 additions & 15 deletions compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ mod tests {

use crate::brillig::brillig_ir::{
artifact::BrilligParameter,
tests::{create_and_run_vm, create_context},
tests::{create_and_run_vm, create_context, create_entry_point_bytecode},
};

#[test]
Expand Down Expand Up @@ -317,13 +317,8 @@ mod tests {

context.return_instruction(&[array_pointer]);

let vm = create_and_run_vm(
flattened_array.clone(),
vec![Value::from(0_usize)],
context,
arguments,
returns,
);
let bytecode = create_entry_point_bytecode(context, arguments, returns).byte_code;
let vm = create_and_run_vm(flattened_array.clone(), vec![Value::from(0_usize)], &bytecode);
let memory = vm.get_memory();

assert_eq!(vm.get_registers().get(RegisterIndex(0)), Value::from(flattened_array.len()));
Expand Down Expand Up @@ -382,13 +377,8 @@ mod tests {

context.return_instruction(&[array_pointer]);

let vm = create_and_run_vm(
flattened_array.clone(),
vec![Value::from(0_usize)],
context,
arguments,
returns,
);
let bytecode = create_entry_point_bytecode(context, arguments, returns).byte_code;
let vm = create_and_run_vm(flattened_array.clone(), vec![Value::from(0_usize)], &bytecode);
let memory = vm.get_memory();

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ fn execute_brillig(

// Instantiate a Brillig VM given the solved input registers and memory, along with the Brillig bytecode.
let input_registers = Registers::load(input_register_values);
let mut vm = VM::new(input_registers, input_memory, code, Vec::new(), &NullBbSolver);
let mut vm = VM::new(input_registers, input_memory, &code, Vec::new(), &NullBbSolver);

// Run the Brillig VM on these inputs, bytecode, etc!
let vm_status = vm.process_opcodes();
Expand Down

0 comments on commit 4ee290b

Please sign in to comment.