From 5f64c31a3a3ebfca7590f332b7918bcdb96bde76 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Thu, 10 Aug 2023 13:36:15 +0100 Subject: [PATCH 1/6] contract info --- crates/nargo_cli/src/cli/info_cmd.rs | 63 ++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index 213f494d7a9..b7915076bec 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -1,13 +1,13 @@ use acvm::Backend; use clap::Args; -use nargo::package::Package; +use nargo::{package::Package, prepare_package}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; -use noirc_driver::CompileOptions; +use noirc_driver::{compile_contracts, CompileOptions}; use noirc_frontend::graph::CrateName; use crate::{cli::compile_cmd::compile_package, errors::CliError}; -use super::NargoConfig; +use super::{compile_cmd::report_errors, NargoConfig}; /// Provides detailed information on a circuit /// @@ -33,7 +33,11 @@ pub(crate) fn run( let workspace = resolve_workspace_from_toml(&toml_path, args.package)?; for package in &workspace { - count_opcodes_and_gates_in_package(backend, package, &args.compile_options)?; + if package.is_contract() { + count_opcodes_and_gates_in_contract(backend, package, &args.compile_options)?; + } else { + count_opcodes_and_gates_in_package(backend, package, &args.compile_options)?; + } } Ok(()) @@ -62,3 +66,54 @@ fn count_opcodes_and_gates_in_package( Ok(()) } + +fn count_opcodes_and_gates_in_contract( + backend: &B, + package: &Package, + compile_options: &CompileOptions, +) -> Result<(), CliError> { + let (mut context, crate_id) = prepare_package(package); + let result = compile_contracts(&mut context, crate_id, &compile_options); + let contracts = report_errors(result, &context, compile_options.deny_warnings)?; + let mut total_num_opcodes_in_all_contracts = 0; + let mut total_num_circuit_size_in_all_contracts = 0; + + for contract in contracts { + let mut total_num_opcodes = 0; + let mut total_circuit_size = 0; + let mut function_info = Vec::new(); + for function in contract.functions { + let num_opcodes = function.bytecode.opcodes.len(); + let exact_circuit_size = backend + .get_exact_circuit_size(&function.bytecode) + .map_err(CliError::ProofSystemCompilerError)?; + total_num_opcodes += num_opcodes; + total_circuit_size += exact_circuit_size; + function_info.push((function.name, num_opcodes, exact_circuit_size)); + } + total_num_opcodes_in_all_contracts += total_num_opcodes; + total_num_circuit_size_in_all_contracts += total_circuit_size; + + println!( + "[{}] Total ACIR opcodes generated for language {:?}: {}", + contract.name, + backend.np_language(), + total_num_opcodes + ); + println!("[{}] Backend circuit size: {total_circuit_size} \n", contract.name); + + for info in function_info { + println!("[{}]({}) Total ACIR opcodes generated: {}", contract.name, info.0, info.1,); + println!("[{}]({}) Backend circuit size: {}", contract.name, info.0, info.2); + } + } + + println!( + "\n[All Contracts] Total ACIR opcodes generated for language {:?}: {}", + backend.np_language(), + total_num_opcodes_in_all_contracts + ); + println!("[All Contracts] Backend circuit size: {total_num_circuit_size_in_all_contracts}"); + + Ok(()) +} From 321f2b37ca6b3af11b5617b961b85a1a30d26ad1 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Thu, 10 Aug 2023 13:40:04 +0100 Subject: [PATCH 2/6] . --- crates/nargo_cli/src/cli/info_cmd.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index b7915076bec..a3624f285d2 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -34,7 +34,7 @@ pub(crate) fn run( for package in &workspace { if package.is_contract() { - count_opcodes_and_gates_in_contract(backend, package, &args.compile_options)?; + count_opcodes_and_gates_in_contracts(backend, package, &args.compile_options)?; } else { count_opcodes_and_gates_in_package(backend, package, &args.compile_options)?; } @@ -67,13 +67,13 @@ fn count_opcodes_and_gates_in_package( Ok(()) } -fn count_opcodes_and_gates_in_contract( +fn count_opcodes_and_gates_in_contracts( backend: &B, package: &Package, compile_options: &CompileOptions, ) -> Result<(), CliError> { let (mut context, crate_id) = prepare_package(package); - let result = compile_contracts(&mut context, crate_id, &compile_options); + let result = compile_contracts(&mut context, crate_id, compile_options); let contracts = report_errors(result, &context, compile_options.deny_warnings)?; let mut total_num_opcodes_in_all_contracts = 0; let mut total_num_circuit_size_in_all_contracts = 0; From ead28f2166eb2337fbb79d2b42e02a7262791110 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Thu, 10 Aug 2023 13:51:08 +0100 Subject: [PATCH 3/6] review --- crates/nargo_cli/src/cli/info_cmd.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index a3624f285d2..cd740f8b927 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -75,8 +75,6 @@ fn count_opcodes_and_gates_in_contracts( let (mut context, crate_id) = prepare_package(package); let result = compile_contracts(&mut context, crate_id, compile_options); let contracts = report_errors(result, &context, compile_options.deny_warnings)?; - let mut total_num_opcodes_in_all_contracts = 0; - let mut total_num_circuit_size_in_all_contracts = 0; for contract in contracts { let mut total_num_opcodes = 0; @@ -91,16 +89,6 @@ fn count_opcodes_and_gates_in_contracts( total_circuit_size += exact_circuit_size; function_info.push((function.name, num_opcodes, exact_circuit_size)); } - total_num_opcodes_in_all_contracts += total_num_opcodes; - total_num_circuit_size_in_all_contracts += total_circuit_size; - - println!( - "[{}] Total ACIR opcodes generated for language {:?}: {}", - contract.name, - backend.np_language(), - total_num_opcodes - ); - println!("[{}] Backend circuit size: {total_circuit_size} \n", contract.name); for info in function_info { println!("[{}]({}) Total ACIR opcodes generated: {}", contract.name, info.0, info.1,); @@ -108,12 +96,5 @@ fn count_opcodes_and_gates_in_contracts( } } - println!( - "\n[All Contracts] Total ACIR opcodes generated for language {:?}: {}", - backend.np_language(), - total_num_opcodes_in_all_contracts - ); - println!("[All Contracts] Backend circuit size: {total_num_circuit_size_in_all_contracts}"); - Ok(()) } From 695bb71ab445f21b856e3241a2acf97bafaedd19 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Thu, 10 Aug 2023 13:52:07 +0100 Subject: [PATCH 4/6] . --- crates/nargo_cli/src/cli/info_cmd.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index cd740f8b927..082cde7c6ca 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -77,8 +77,6 @@ fn count_opcodes_and_gates_in_contracts( let contracts = report_errors(result, &context, compile_options.deny_warnings)?; for contract in contracts { - let mut total_num_opcodes = 0; - let mut total_circuit_size = 0; let mut function_info = Vec::new(); for function in contract.functions { let num_opcodes = function.bytecode.opcodes.len(); From 904a409a7070c9c9f60693f47a82d4d3b8bd052e Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Thu, 10 Aug 2023 13:52:32 +0100 Subject: [PATCH 5/6] . --- crates/nargo_cli/src/cli/info_cmd.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index 082cde7c6ca..af3e3ccd4f3 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -83,8 +83,6 @@ fn count_opcodes_and_gates_in_contracts( let exact_circuit_size = backend .get_exact_circuit_size(&function.bytecode) .map_err(CliError::ProofSystemCompilerError)?; - total_num_opcodes += num_opcodes; - total_circuit_size += exact_circuit_size; function_info.push((function.name, num_opcodes, exact_circuit_size)); } From b825107bbc04244d2007fac6d6a3b903bd31093d Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 10 Aug 2023 14:00:56 +0100 Subject: [PATCH 6/6] chore: replace for-loop with `try_vecmap` --- crates/nargo_cli/src/cli/info_cmd.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index af3e3ccd4f3..2518c402605 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -1,5 +1,6 @@ use acvm::Backend; use clap::Args; +use iter_extended::try_vecmap; use nargo::{package::Package, prepare_package}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_driver::{compile_contracts, CompileOptions}; @@ -77,14 +78,13 @@ fn count_opcodes_and_gates_in_contracts( let contracts = report_errors(result, &context, compile_options.deny_warnings)?; for contract in contracts { - let mut function_info = Vec::new(); - for function in contract.functions { + let function_info: Vec<(String, usize, u32)> = try_vecmap(contract.functions, |function| { let num_opcodes = function.bytecode.opcodes.len(); - let exact_circuit_size = backend - .get_exact_circuit_size(&function.bytecode) - .map_err(CliError::ProofSystemCompilerError)?; - function_info.push((function.name, num_opcodes, exact_circuit_size)); - } + let exact_circuit_size = backend.get_exact_circuit_size(&function.bytecode)?; + + Ok((function.name, num_opcodes, exact_circuit_size)) + }) + .map_err(CliError::ProofSystemCompilerError)?; for info in function_info { println!("[{}]({}) Total ACIR opcodes generated: {}", contract.name, info.0, info.1,);