diff --git a/acvm-repo/acvm/src/pwg/brillig.rs b/acvm-repo/acvm/src/pwg/brillig.rs index 9b0ecd87492..b35e227a77c 100644 --- a/acvm-repo/acvm/src/pwg/brillig.rs +++ b/acvm-repo/acvm/src/pwg/brillig.rs @@ -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, ); diff --git a/acvm-repo/brillig_vm/src/lib.rs b/acvm-repo/brillig_vm/src/lib.rs index ca8c52756d1..6da34c6a498 100644 --- a/acvm-repo/brillig_vm/src/lib.rs +++ b/acvm-repo/brillig_vm/src/lib.rs @@ -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 @@ -72,7 +72,7 @@ pub struct VM<'bb_solver, B: BlackBoxFunctionSolver> { /// List is appended onto by the caller upon reaching a [VMStatus::ForeignCallWait] foreign_call_results: Vec, /// Executable opcodes - bytecode: Vec, + bytecode: &'a [Opcode], /// Status of the VM status: VMStatus, /// Memory of the VM @@ -80,17 +80,17 @@ pub struct VM<'bb_solver, B: BlackBoxFunctionSolver> { /// Call stack call_stack: Vec, /// 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, - bytecode: Vec, + bytecode: &'a [Opcode], foreign_call_results: Vec, - black_box_solver: &'bb_solver B, + black_box_solver: &'a B, ) -> Self { Self { registers: inputs, @@ -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 // @@ -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); @@ -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); @@ -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); @@ -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); @@ -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() } @@ -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) } @@ -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() } @@ -883,8 +880,8 @@ mod tests { /// Helper to execute brillig code fn brillig_execute_and_get_vm( memory: Vec, - opcodes: Vec, - ) -> 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![]); @@ -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!( @@ -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!( @@ -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!( @@ -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!( @@ -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!( diff --git a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_slice_ops.rs b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_slice_ops.rs index 445f2769692..211d670e7d8 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_slice_ops.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_slice_ops.rs @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/compiler/noirc_evaluator/src/brillig/brillig_ir.rs b/compiler/noirc_evaluator/src/brillig/brillig_ir.rs index a5647c38b60..a4ea0362f06 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_ir.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_ir.rs @@ -1069,14 +1069,12 @@ pub(crate) mod tests { pub(crate) fn create_and_run_vm( memory: Vec, param_registers: Vec, - context: BrilligContext, - arguments: Vec, - returns: Vec, - ) -> 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, ); @@ -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, ); diff --git a/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs b/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs index 23c7b8a2c5b..fb426ad6876 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs @@ -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] @@ -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())); @@ -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!( diff --git a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index 8313aa3b9fe..6803c614f0b 100644 --- a/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs +++ b/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -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();