Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Sep 9, 2024
1 parent 34b4a8a commit 4ae80ae
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 4ae80ae

Please sign in to comment.