Skip to content

Commit

Permalink
feat(avm-transpiler): optionally count opcode types (#8439)
Browse files Browse the repository at this point in the history
```
[2024-09-07T13:52:42Z INFO  avm_transpiler::transpile_contract] Transpiling AVM function transfer_public on contract Token
[2024-09-07T13:52:42Z INFO  avm_transpiler::transpile_contract] Extracted Brillig program has 746 instructions
[2024-09-07T13:52:42Z INFO  avm_transpiler::utils] Transpiled AVM program has 765 instructions
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils] AVM opcode counts:
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      MOV_8: 234
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      ADD: 136
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      SET: 97
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      JUMPI: 48
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      CAST: 43
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      JUMP: 36
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      REVERT: 35
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      EQ: 29
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      MOV_16: 22
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      LT: 18
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      SUB: 17
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      LTE: 11
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      INTERNALCALL: 7
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      MUL: 6
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      PEDERSEN: 4
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      FDIV: 4
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      ADDRESS: 2
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      SENDER: 2
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      INTERNALRETURN: 2
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      POSEIDON2: 2
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      SSTORE: 2
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      SLOAD: 2
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      TORADIXLE: 1
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      FUNCTIONSELECTOR: 1
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      NULLIFIEREXISTS: 1
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      CALLDATACOPY: 1
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      RETURN: 1
[2024-09-07T13:52:42Z DEBUG avm_transpiler::utils]      CALL: 1
[2024-09-07T13:52:42Z INFO  avm_transpiler::transpile_contract] Token::transfer_public: bytecode size of 7401 was compressed to 2342 (69% reduction)
```
  • Loading branch information
fcarreiro authored Sep 9, 2024
1 parent ede16d3 commit 21c06b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
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);
}
}

0 comments on commit 21c06b5

Please sign in to comment.