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

chore: defer reporting of errors until after compilation and optimization is finished #2659

Merged
merged 1 commit into from
Sep 12, 2023
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
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use super::NargoConfig;
use super::{
compile_cmd::compile_package,
compile_cmd::compile_bin_package,
fs::{create_named_dir, program::read_program_from_file, write_to_file},
};
use crate::backends::Backend;
Expand Down Expand Up @@ -82,7 +82,7 @@ fn smart_contract_for_package(
read_program_from_file(circuit_build_path)?
} else {
let (program, _) =
compile_package(package, compile_options, np_language, &is_opcode_supported)?;
compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?;

PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
Expand Down
82 changes: 66 additions & 16 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use nargo::artifacts::program::PreprocessedProgram;
use nargo::package::Package;
use nargo::prepare_package;
use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection};
use noirc_driver::{
compile_main, CompilationResult, CompileOptions, CompiledContract, CompiledProgram,
};
use noirc_driver::{CompilationResult, CompileOptions, CompiledContract, CompiledProgram};
use noirc_frontend::graph::CrateName;

use clap::Args;
Expand Down Expand Up @@ -68,29 +66,41 @@ pub(crate) fn run(
for package in &workspace {
// If `contract` package type, we're compiling every function in a 'contract' rather than just 'main'.
if package.is_contract() {
let contracts_with_debug_artifacts = compile_contracts(
let (file_manager, compilation_result) = compile_contracts(
package,
&args.compile_options,
np_language,
&is_opcode_supported,
);
let contracts_with_debug_artifacts = report_errors(
compilation_result,
&file_manager,
args.compile_options.deny_warnings,
)?;

save_contracts(
contracts_with_debug_artifacts,
package,
&circuit_dir,
args.output_debug,
);
} else {
let (program, debug_artifact) =
compile_package(package, &args.compile_options, np_language, &is_opcode_supported)?;
let (file_manager, compilation_result) =
compile_program(package, &args.compile_options, np_language, &is_opcode_supported);

let (program, debug_artifact) = report_errors(
compilation_result,
&file_manager,
args.compile_options.deny_warnings,
)?;
save_program(debug_artifact, program, package, &circuit_dir, args.output_debug);
}
}

Ok(())
}

pub(crate) fn compile_package(
pub(crate) fn compile_bin_package(
package: &Package,
compile_options: &CompileOptions,
np_language: Language,
Expand All @@ -100,9 +110,43 @@ pub(crate) fn compile_package(
return Err(CompileError::LibraryCrate(package.name.clone()).into());
}

let (file_manager, compilation_result) =
compile_program(package, compile_options, np_language, &is_opcode_supported);

let (program, debug_artifact) =
report_errors(compilation_result, &file_manager, compile_options.deny_warnings)?;

Ok((program, debug_artifact))
}

pub(crate) fn compile_contract_package(
package: &Package,
compile_options: &CompileOptions,
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
) -> Result<Vec<(CompiledContract, DebugArtifact)>, CliError> {
let (file_manager, compilation_result) =
compile_contracts(package, compile_options, np_language, &is_opcode_supported);
let contracts_with_debug_artifacts =
report_errors(compilation_result, &file_manager, compile_options.deny_warnings)?;
Ok(contracts_with_debug_artifacts)
}

fn compile_program(
package: &Package,
compile_options: &CompileOptions,
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
) -> (FileManager, CompilationResult<(CompiledProgram, DebugArtifact)>) {
let (mut context, crate_id) = prepare_package(package);
let result = compile_main(&mut context, crate_id, compile_options);
let program = report_errors(result, &context.file_manager, compile_options.deny_warnings)?;

let (program, warnings) =
match noirc_driver::compile_main(&mut context, crate_id, compile_options) {
Ok(program_and_warnings) => program_and_warnings,
Err(errors) => {
return (context.file_manager, Err(errors));
}
};

// Apply backend specific optimizations.
let optimized_program =
Expand All @@ -112,22 +156,28 @@ pub(crate) fn compile_package(
let debug_artifact =
DebugArtifact::new(vec![optimized_program.debug.clone()], &context.file_manager);

Ok((optimized_program, debug_artifact))
(context.file_manager, Ok(((optimized_program, debug_artifact), warnings)))
}

pub(crate) fn compile_contracts(
fn compile_contracts(
package: &Package,
compile_options: &CompileOptions,
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
) -> Result<Vec<(CompiledContract, DebugArtifact)>, CliError> {
) -> (FileManager, CompilationResult<Vec<(CompiledContract, DebugArtifact)>>) {
let (mut context, crate_id) = prepare_package(package);
let result = noirc_driver::compile_contracts(&mut context, crate_id, compile_options);
let contracts = report_errors(result, &context.file_manager, compile_options.deny_warnings)?;
let (contracts, warnings) =
match noirc_driver::compile_contracts(&mut context, crate_id, compile_options) {
Ok(contracts_and_warnings) => contracts_and_warnings,
Err(errors) => {
return (context.file_manager, Err(errors));
}
};

let optimized_contracts = try_vecmap(contracts, |contract| {
nargo::ops::optimize_contract(contract, np_language, &is_opcode_supported)
})?;
})
.expect("Backend does not support an opcode that is in the IR");

let contracts_with_debug_artifacts = vecmap(optimized_contracts, |contract| {
let debug_infos = vecmap(&contract.functions, |func| func.debug.clone());
Expand All @@ -136,7 +186,7 @@ pub(crate) fn compile_contracts(
(contract, debug_artifact)
});

Ok(contracts_with_debug_artifacts)
(context.file_manager, Ok((contracts_with_debug_artifacts, warnings)))
}

fn save_program(
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use noirc_driver::{CompileOptions, CompiledProgram};
use noirc_errors::CustomDiagnostic;
use noirc_frontend::graph::CrateName;

use super::compile_cmd::compile_package;
use super::compile_cmd::compile_bin_package;
use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir};
use super::NargoConfig;
use crate::backends::Backend;
Expand Down Expand Up @@ -88,7 +88,7 @@ fn execute_package(
is_opcode_supported: &impl Fn(&Opcode) -> bool,
) -> Result<(Option<InputValue>, WitnessMap), CliError> {
let (compiled_program, debug_artifact) =
compile_package(package, compile_options, np_language, &is_opcode_supported)?;
compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?;
let CompiledProgram { abi, circuit, .. } = compiled_program;

// Parse the initial witness values from Prover.toml
Expand Down
12 changes: 8 additions & 4 deletions tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ use prettytable::{row, table, Row};
use serde::Serialize;

use crate::backends::Backend;
use crate::{cli::compile_cmd::compile_package, errors::CliError};
use crate::errors::CliError;

use super::{compile_cmd::compile_contracts, NargoConfig};
use super::{
compile_cmd::{compile_bin_package, compile_contract_package},
NargoConfig,
};

/// Provides detailed information on a circuit
///
Expand Down Expand Up @@ -172,7 +175,7 @@ fn count_opcodes_and_gates_in_program(
is_opcode_supported: &impl Fn(&Opcode) -> bool,
) -> Result<ProgramInfo, CliError> {
let (compiled_program, _) =
compile_package(package, compile_options, np_language, &is_opcode_supported)?;
compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?;
let (language, _) = backend.get_backend_info()?;

Ok(ProgramInfo {
Expand All @@ -190,7 +193,8 @@ fn count_opcodes_and_gates_in_contracts(
np_language: Language,
is_opcode_supported: &impl Fn(&Opcode) -> bool,
) -> Result<Vec<ContractInfo>, CliError> {
let contracts = compile_contracts(package, compile_options, np_language, &is_opcode_supported)?;
let contracts =
compile_contract_package(package, compile_options, np_language, &is_opcode_supported)?;
let (language, _) = backend.get_backend_info()?;

try_vecmap(contracts, |(contract, _)| {
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use noirc_abi::input_parser::Format;
use noirc_driver::CompileOptions;
use noirc_frontend::graph::CrateName;

use super::compile_cmd::compile_package;
use super::compile_cmd::compile_bin_package;
use super::fs::{
inputs::{read_inputs_from_file, write_inputs_to_file},
program::read_program_from_file,
Expand Down Expand Up @@ -102,7 +102,7 @@ pub(crate) fn prove_package(
(program, None)
} else {
let (program, debug_artifact) =
compile_package(package, compile_options, np_language, &is_opcode_supported)?;
compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?;
let preprocessed_program = PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
abi: program.abi,
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::NargoConfig;
use super::{
compile_cmd::compile_package,
compile_cmd::compile_bin_package,
fs::{inputs::read_inputs_from_file, load_hex_data, program::read_program_from_file},
};
use crate::{backends::Backend, errors::CliError};
Expand Down Expand Up @@ -86,7 +86,7 @@ fn verify_package(
read_program_from_file(circuit_build_path)?
} else {
let (program, _) =
compile_package(package, compile_options, np_language, &is_opcode_supported)?;
compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?;

PreprocessedProgram {
backend: String::from(BACKEND_IDENTIFIER),
Expand Down