Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(avm-transpiler): optionally count opcode types #8439

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion avm-transpiler/src/opcodes.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion avm-transpiler/src/transpile_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl From<CompiledAcirContractArtifact> 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);
Expand All @@ -118,7 +119,7 @@ impl From<CompiledAcirContractArtifact> 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(),
Expand Down
20 changes: 15 additions & 5 deletions avm-transpiler/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,16 +68,25 @@ pub fn extract_static_assert_messages(program: &Program<FieldElement>) -> HashMa

/// Print inputs, outputs, and instructions in a Brillig program
pub fn dbg_print_brillig_program(brillig_bytecode: &[BrilligOpcode<FieldElement>]) {
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::<AvmOpcode, usize>::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);
}
}