From 6dcc8c6848cd0bccd0e7510e07e11c0caffaab35 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 12:32:48 +0100 Subject: [PATCH 1/9] count contract gates --- crates/nargo_cli/src/cli/info_cmd.rs | 102 ++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 17 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index 7ad0a2caf8c..ca42d920e74 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -1,10 +1,14 @@ use acvm::Backend; use clap::Args; -use noirc_driver::CompileOptions; + +use noirc_driver::{compile_contracts, CompileOptions}; use std::path::Path; use crate::cli::compile_cmd::compile_circuit; use crate::errors::CliError; +use crate::resolver::resolve_root_manifest; + +use super::compile_cmd::report_errors; use super::NargoConfig; @@ -14,6 +18,10 @@ use super::NargoConfig; /// 2. Counts the final number gates in the circuit used by a backend #[derive(Debug, Clone, Args)] pub(crate) struct InfoCommand { + /// Get information of each contract function used within the program + #[arg(short, long)] + contracts: bool, + #[clap(flatten)] compile_options: CompileOptions, } @@ -23,28 +31,88 @@ pub(crate) fn run( args: InfoCommand, config: NargoConfig, ) -> Result<(), CliError> { - count_opcodes_and_gates_with_path(backend, config.program_dir, &args.compile_options) + count_opcodes_and_gates_with_path(backend, config.program_dir, &args) } fn count_opcodes_and_gates_with_path>( backend: &B, program_dir: P, - compile_options: &CompileOptions, + args: &InfoCommand, ) -> Result<(), CliError> { - let (compiled_program, _) = - compile_circuit(backend, None, program_dir.as_ref(), compile_options)?; - let num_opcodes = compiled_program.circuit.opcodes.len(); - - println!( - "Total ACIR opcodes generated for language {:?}: {}", - backend.np_language(), - num_opcodes - ); - - let exact_circuit_size = backend - .get_exact_circuit_size(&compiled_program.circuit) - .map_err(CliError::ProofSystemCompilerError)?; - println!("Backend circuit size: {exact_circuit_size}"); + if args.contracts { + let (mut context, crate_id) = resolve_root_manifest(program_dir.as_ref(), None)?; + + let result = compile_contracts(&mut context, crate_id, &args.compile_options); + let contracts = report_errors(result, &context, args.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 {:?} in contract {}: {}", + backend.np_language(), + contract.name, + total_num_opcodes + ); + println!("Backend circuit size for contract {}: {total_circuit_size}", contract.name); + println!(""); + + for info in function_info { + println!( + "Total ACIR opcodes generated of function {} for language {:?} in contract {}: {}", + info.0, + backend.np_language(), + contract.name, + info.1, + ); + println!( + "Backend circuit size for function {} in contract {}: {}", + info.0, contract.name, info.2 + ); + } + } + + println!(""); + println!( + "Total ACIR opcodes generated for language {:?} in all contracts: {}", + backend.np_language(), + total_num_opcodes_in_all_contracts + ); + println!( + "Backend circuit size for all contracts: {total_num_circuit_size_in_all_contracts}" + ); + } else { + let (compiled_program, _) = + compile_circuit(backend, None, program_dir.as_ref(), &args.compile_options)?; + let num_opcodes = compiled_program.circuit.opcodes.len(); + + println!( + "Total ACIR opcodes generated for language {:?}: {}", + backend.np_language(), + num_opcodes + ); + + let exact_circuit_size = backend + .get_exact_circuit_size(&compiled_program.circuit) + .map_err(CliError::ProofSystemCompilerError)?; + println!("Backend circuit size: {exact_circuit_size}"); + } Ok(()) } From d9f591d91186b3569668534334dc9d3981148e58 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 13:17:15 +0100 Subject: [PATCH 2/9] . --- crates/nargo_cli/src/cli/info_cmd.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index ca42d920e74..f725e4253a2 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -71,7 +71,7 @@ fn count_opcodes_and_gates_with_path>( total_num_opcodes ); println!("Backend circuit size for contract {}: {total_circuit_size}", contract.name); - println!(""); + println!(); for info in function_info { println!( @@ -88,7 +88,7 @@ fn count_opcodes_and_gates_with_path>( } } - println!(""); + println!(); println!( "Total ACIR opcodes generated for language {:?} in all contracts: {}", backend.np_language(), From af6210d2e4d82bc459197920509e8a13ea479aa3 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 13:23:08 +0100 Subject: [PATCH 3/9] :.: --- crates/nargo_cli/src/cli/info_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index f725e4253a2..ffc553366e3 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -18,7 +18,7 @@ use super::NargoConfig; /// 2. Counts the final number gates in the circuit used by a backend #[derive(Debug, Clone, Args)] pub(crate) struct InfoCommand { - /// Get information of each contract function used within the program + /// Get information of all contracts used within the program #[arg(short, long)] contracts: bool, From 05262b4ee915b20e730ea2308da64de69791cfc3 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 13:41:49 +0100 Subject: [PATCH 4/9] implement contract flag --- crates/nargo_cli/src/cli/info_cmd.rs | 60 +++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index ffc553366e3..fddda893359 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -22,6 +22,10 @@ pub(crate) struct InfoCommand { #[arg(short, long)] contracts: bool, + /// Get information of a contract used within the program + #[arg(short, long)] + contract: Option, + #[clap(flatten)] compile_options: CompileOptions, } @@ -31,13 +35,13 @@ pub(crate) fn run( args: InfoCommand, config: NargoConfig, ) -> Result<(), CliError> { - count_opcodes_and_gates_with_path(backend, config.program_dir, &args) + count_opcodes_and_gates_with_path(backend, config.program_dir, args) } fn count_opcodes_and_gates_with_path>( backend: &B, program_dir: P, - args: &InfoCommand, + args: InfoCommand, ) -> Result<(), CliError> { if args.contracts { let (mut context, crate_id) = resolve_root_manifest(program_dir.as_ref(), None)?; @@ -97,6 +101,58 @@ fn count_opcodes_and_gates_with_path>( println!( "Backend circuit size for all contracts: {total_num_circuit_size_in_all_contracts}" ); + } else if args.contract.is_some() { + let contract_name = args.contract.unwrap(); + let (mut context, crate_id) = resolve_root_manifest(program_dir.as_ref(), None)?; + let result = compile_contracts(&mut context, crate_id, &args.compile_options); + let contracts = report_errors(result, &context, args.compile_options.deny_warnings)?; + + for contract in contracts { + if contract.name == contract_name { + 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)); + } + + println!( + "Total ACIR opcodes generated for language {:?} in contract {}: {}", + backend.np_language(), + contract.name, + total_num_opcodes + ); + println!( + "Backend circuit size for contract {}: {total_circuit_size}", + contract.name + ); + println!(); + + for info in function_info { + println!( + "Total ACIR opcodes generated of function {} for language {:?} in contract {}: {}", + info.0, + backend.np_language(), + contract.name, + info.1, + ); + println!( + "Backend circuit size for function {} in contract {}: {}", + info.0, contract.name, info.2 + ); + } + + break; + } + + println!("Cannot find contract.") + } } else { let (compiled_program, _) = compile_circuit(backend, None, program_dir.as_ref(), &args.compile_options)?; From 0a26943f914e0501618ebfddec368d3362b153dc Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 13:43:04 +0100 Subject: [PATCH 5/9] . --- crates/nargo_cli/src/cli/info_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index fddda893359..5201d957ccb 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -151,7 +151,7 @@ fn count_opcodes_and_gates_with_path>( break; } - println!("Cannot find contract.") + println!("Cannot find contract."); } } else { let (compiled_program, _) = From d365fdbe2abca52143fe951a7c45eda584161a00 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 13:44:55 +0100 Subject: [PATCH 6/9] . --- crates/nargo_cli/src/cli/info_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index 5201d957ccb..c321aaae6dd 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -151,7 +151,7 @@ fn count_opcodes_and_gates_with_path>( break; } - println!("Cannot find contract."); + println!("Cannot find contract"); } } else { let (compiled_program, _) = From 978cc5c6e9c663a3003cb19bc1d0134eb2adc583 Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 13:48:42 +0100 Subject: [PATCH 7/9] . --- crates/nargo_cli/src/cli/info_cmd.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index c321aaae6dd..f564bd15451 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -79,11 +79,8 @@ fn count_opcodes_and_gates_with_path>( for info in function_info { println!( - "Total ACIR opcodes generated of function {} for language {:?} in contract {}: {}", - info.0, - backend.np_language(), - contract.name, - info.1, + "Total ACIR opcodes generated for function {} in contract {}: {}", + info.0, contract.name, info.1, ); println!( "Backend circuit size for function {} in contract {}: {}", @@ -94,12 +91,12 @@ fn count_opcodes_and_gates_with_path>( println!(); println!( - "Total ACIR opcodes generated for language {:?} in all contracts: {}", + "Total ACIR opcodes generated for language {:?} in All Contracts: {}", backend.np_language(), total_num_opcodes_in_all_contracts ); println!( - "Backend circuit size for all contracts: {total_num_circuit_size_in_all_contracts}" + "Backend circuit size for All Contracts: {total_num_circuit_size_in_all_contracts}" ); } else if args.contract.is_some() { let contract_name = args.contract.unwrap(); @@ -136,11 +133,8 @@ fn count_opcodes_and_gates_with_path>( for info in function_info { println!( - "Total ACIR opcodes generated of function {} for language {:?} in contract {}: {}", - info.0, - backend.np_language(), - contract.name, - info.1, + "Total ACIR opcodes generated for function {} in contract {}: {}", + info.0, contract.name, info.1, ); println!( "Backend circuit size for function {} in contract {}: {}", From 95d36412c93ce4936f55e0aecba7960d815c79fd Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Sat, 29 Jul 2023 15:31:20 +0100 Subject: [PATCH 8/9] . --- crates/nargo_cli/src/cli/info_cmd.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index f564bd15451..14a6823a6ea 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -1,18 +1,14 @@ +use super::compile_cmd::report_errors; +use super::NargoConfig; +use crate::cli::compile_cmd::compile_circuit; +use crate::errors::CliError; +use crate::resolver::resolve_root_manifest; use acvm::Backend; use clap::Args; - use noirc_driver::{compile_contracts, CompileOptions}; use std::path::Path; -use crate::cli::compile_cmd::compile_circuit; -use crate::errors::CliError; -use crate::resolver::resolve_root_manifest; - -use super::compile_cmd::report_errors; - -use super::NargoConfig; - -/// Provides detailed informaton on a circuit +/// Provides detailed information on a circuit /// Current information provided: /// 1. The number of ACIR opcodes /// 2. Counts the final number gates in the circuit used by a backend From e3e911d4ce484b46a78ab6f015d495ec2a8a685a Mon Sep 17 00:00:00 2001 From: ethan-000 Date: Mon, 7 Aug 2023 19:18:29 +0100 Subject: [PATCH 9/9] remove println --- crates/nargo_cli/src/cli/info_cmd.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index 14a6823a6ea..519ecff9f0b 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -70,8 +70,10 @@ fn count_opcodes_and_gates_with_path>( contract.name, total_num_opcodes ); - println!("Backend circuit size for contract {}: {total_circuit_size}", contract.name); - println!(); + println!( + "Backend circuit size for contract {}: {total_circuit_size} \n", + contract.name + ); for info in function_info { println!( @@ -85,9 +87,8 @@ fn count_opcodes_and_gates_with_path>( } } - println!(); println!( - "Total ACIR opcodes generated for language {:?} in All Contracts: {}", + "\nTotal ACIR opcodes generated for language {:?} in All Contracts: {}", backend.np_language(), total_num_opcodes_in_all_contracts );