From 6a871960ecb4b08de8891a2e676064d0c95e382d Mon Sep 17 00:00:00 2001 From: fcarreiro Date: Fri, 6 Sep 2024 22:03:52 +0000 Subject: [PATCH] wip --- avm-transpiler/src/opcodes.rs | 2 +- avm-transpiler/src/transpile_contract.rs | 3 ++- avm-transpiler/src/utils.rs | 20 +++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/avm-transpiler/src/opcodes.rs b/avm-transpiler/src/opcodes.rs index a33abd4855ec..36bcd8b8268a 100644 --- a/avm-transpiler/src/opcodes.rs +++ b/avm-transpiler/src/opcodes.rs @@ -1,7 +1,7 @@ /// All AVM opcodes /// Keep updated with TS, cpp, and docs protocol specs! #[allow(clippy::upper_case_acronyms, dead_code)] -#[derive(PartialEq, Copy, Clone, Debug)] +#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)] pub enum AvmOpcode { // Compute ADD, diff --git a/avm-transpiler/src/transpile_contract.rs b/avm-transpiler/src/transpile_contract.rs index e684a541d849..fff7ad27522e 100644 --- a/avm-transpiler/src/transpile_contract.rs +++ b/avm-transpiler/src/transpile_contract.rs @@ -100,6 +100,7 @@ impl From for TranspiledContractArtifact { let acir_program = function.bytecode; let brillig_bytecode = extract_brillig_from_acir_program(&acir_program); let assert_messages = extract_static_assert_messages(&acir_program); + info!("Extracted Brillig program has {} instructions", brillig_bytecode.len()); // Map Brillig pcs to AVM pcs (index is Brillig PC, value is AVM PC) let brillig_pcs_to_avm_pcs = map_brillig_pcs_to_avm_pcs(brillig_bytecode); @@ -118,7 +119,7 @@ impl From for TranspiledContractArtifact { let _ = encoder.read_to_end(&mut compressed_avm_bytecode); log::info!( - "{}::{}: compressed {} to {} bytes ({}% reduction)", + "{}::{}: bytecode size of {} was compressed to {} ({}% reduction)", contract.name, function.name, avm_bytecode.len(), diff --git a/avm-transpiler/src/utils.rs b/avm-transpiler/src/utils.rs index 19f9468e15c0..982a84795464 100644 --- a/avm-transpiler/src/utils.rs +++ b/avm-transpiler/src/utils.rs @@ -2,12 +2,13 @@ use fxhash::FxHashMap as HashMap; use acvm::acir::circuit::brillig::BrilligFunctionId; use acvm::FieldElement; -use log::debug; +use log::{debug, info, trace}; use acvm::acir::brillig::Opcode as BrilligOpcode; use acvm::acir::circuit::{AssertionPayload, Opcode, Program}; use crate::instructions::AvmInstruction; +use crate::opcodes::AvmOpcode; /// Extract the Brillig program from its `Program` wrapper. /// Noir entry point unconstrained functions are compiled to their own list contained @@ -67,16 +68,25 @@ pub fn extract_static_assert_messages(program: &Program) -> HashMa /// Print inputs, outputs, and instructions in a Brillig program pub fn dbg_print_brillig_program(brillig_bytecode: &[BrilligOpcode]) { - debug!("Printing Brillig program..."); + trace!("Printing Brillig program..."); for (i, instruction) in brillig_bytecode.iter().enumerate() { - debug!("\tPC:{0} {1:?}", i, instruction); + trace!("\tPC:{0} {1:?}", i, instruction); } } /// Print each instruction in an AVM program pub fn dbg_print_avm_program(avm_program: &[AvmInstruction]) { - debug!("Printing AVM program..."); + info!("Transpiled AVM program has {} instructions", avm_program.len()); + trace!("Printing AVM program..."); + let mut counts = std::collections::HashMap::::new(); for (i, instruction) in avm_program.iter().enumerate() { - debug!("\tPC:{0}: {1}", i, &instruction.to_string()); + trace!("\tPC:{0}: {1}", i, &instruction.to_string()); + *counts.entry(instruction.opcode).or_insert(0) += 1; + } + debug!("AVM opcode counts:"); + let mut sorted_counts: Vec<_> = counts.into_iter().collect(); + sorted_counts.sort_by_key(|(_, count)| -(*count as isize)); + for (opcode, count) in sorted_counts { + debug!("\t{0:?}: {1}", opcode, count); } }