diff --git a/compiler/noirc_driver/src/lib.rs b/compiler/noirc_driver/src/lib.rs index 381a29ffcd..8b4fe6abee 100644 --- a/compiler/noirc_driver/src/lib.rs +++ b/compiler/noirc_driver/src/lib.rs @@ -58,7 +58,7 @@ pub type CompilationResult = Result<(T, Warnings), ErrorsAndWarnings>; // with the restricted version which only uses one file pub fn compile_file(context: &mut Context, root_file: &Path) -> CompilationResult { let crate_id = prepare_crate(context, root_file); - compile_main(context, crate_id, &CompileOptions::default(), None) + compile_main(context, crate_id, &CompileOptions::default(), None, true) } /// Adds the file from the file system at `Path` to the crate graph as a root file @@ -148,6 +148,7 @@ pub fn compile_main( crate_id: CrateId, options: &CompileOptions, cached_program: Option, + force_compile: bool, ) -> CompilationResult { let (_, warnings) = check_crate(context, crate_id, options.deny_warnings)?; @@ -163,7 +164,7 @@ pub fn compile_main( } }; - let compiled_program = compile_no_check(context, options, main, cached_program)?; + let compiled_program = compile_no_check(context, options, main, cached_program, force_compile)?; if options.print_acir { println!("Compiled ACIR for main (unoptimized):"); @@ -257,7 +258,7 @@ fn compile_contract_inner( continue; } - let function = match compile_no_check(context, options, function_id, None) { + let function = match compile_no_check(context, options, function_id, None, true) { Ok(function) => function, Err(new_error) => { errors.push(new_error); @@ -314,6 +315,7 @@ pub fn compile_no_check( options: &CompileOptions, main_function: FuncId, cached_program: Option, + force_compile: bool, ) -> Result { let program = monomorphize(main_function, &context.def_interner); @@ -321,7 +323,7 @@ pub fn compile_no_check( // If user has specified that they want to see intermediate steps printed then we should // force compilation even if the program hasn't changed. - if !(options.print_acir || options.show_brillig || options.show_ssa) { + if !(force_compile || options.print_acir || options.show_brillig || options.show_ssa) { if let Some(cached_program) = cached_program { if hash == cached_program.hash { return Ok(cached_program); diff --git a/compiler/wasm/src/compile.rs b/compiler/wasm/src/compile.rs index 99051a9840..6e756325ba 100644 --- a/compiler/wasm/src/compile.rs +++ b/compiler/wasm/src/compile.rs @@ -125,9 +125,10 @@ pub fn compile(args: JsValue) -> JsValue { ::from_serde(&optimized_contract).unwrap() } else { - let compiled_program = compile_main(&mut context, crate_id, &options.compile_options, None) - .expect("Compilation failed") - .0; + let compiled_program = + compile_main(&mut context, crate_id, &options.compile_options, None, true) + .expect("Compilation failed") + .0; let optimized_program = nargo::ops::optimize_program(compiled_program, np_language, &is_opcode_supported) diff --git a/tooling/nargo/src/ops/test.rs b/tooling/nargo/src/ops/test.rs index 993ceb0f21..c94fd25696 100644 --- a/tooling/nargo/src/ops/test.rs +++ b/tooling/nargo/src/ops/test.rs @@ -20,7 +20,7 @@ pub fn run_test( show_output: bool, config: &CompileOptions, ) -> TestStatus { - let program = compile_no_check(context, config, test_function.get_id(), None); + let program = compile_no_check(context, config, test_function.get_id(), None, false); match program { Ok(program) => { // Run the backend to ensure the PWG evaluates functions like std::hash::pedersen, diff --git a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs index 856970544b..35538ef1a8 100644 --- a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -76,6 +76,7 @@ fn smart_contract_for_package( workspace, package, compile_options, + false, np_language, &is_opcode_supported, )?; diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index 7ac7fe5850..737d3c6298 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -82,6 +82,7 @@ pub(crate) fn run( np_language, &opcode_support, &args.compile_options, + args.output_debug, )?; // Save build artifacts to disk. @@ -99,6 +100,7 @@ pub(super) fn compile_workspace( np_language: Language, opcode_support: &BackendOpcodeSupport, compile_options: &CompileOptions, + output_debug: bool, ) -> Result<(Vec, Vec), CliError> { let is_opcode_supported = |opcode: &_| opcode_support.is_opcode_supported(opcode); @@ -106,7 +108,14 @@ pub(super) fn compile_workspace( let program_results: Vec<(FileManager, CompilationResult)> = binary_packages .par_iter() .map(|package| { - compile_program(workspace, package, compile_options, np_language, &is_opcode_supported) + compile_program( + workspace, + package, + compile_options, + output_debug, + np_language, + &is_opcode_supported, + ) }) .collect(); let contract_results: Vec<(FileManager, CompilationResult)> = @@ -138,6 +147,7 @@ pub(crate) fn compile_bin_package( workspace: &Workspace, package: &Package, compile_options: &CompileOptions, + output_debug: bool, np_language: Language, is_opcode_supported: &impl Fn(&Opcode) -> bool, ) -> Result { @@ -145,8 +155,14 @@ pub(crate) fn compile_bin_package( return Err(CompileError::LibraryCrate(package.name.clone()).into()); } - let (file_manager, compilation_result) = - compile_program(workspace, package, compile_options, np_language, &is_opcode_supported); + let (file_manager, compilation_result) = compile_program( + workspace, + package, + compile_options, + output_debug, + np_language, + &is_opcode_supported, + ); let program = report_errors(compilation_result, &file_manager, compile_options.deny_warnings)?; @@ -157,6 +173,7 @@ fn compile_program( workspace: &Workspace, package: &Package, compile_options: &CompileOptions, + output_debug: bool, np_language: Language, is_opcode_supported: &impl Fn(&Opcode) -> bool, ) -> (FileManager, CompilationResult) { @@ -177,20 +194,33 @@ fn compile_program( None }; - let (program, warnings) = - match noirc_driver::compile_main(&mut context, crate_id, compile_options, cached_program) { - Ok(program_and_warnings) => program_and_warnings, - Err(errors) => { - return (context.file_manager, Err(errors)); - } - }; + // If we want to output the debug information then we need to perform a full recompilation of the ACIR. + let force_recompile = output_debug; + + let (program, warnings) = match noirc_driver::compile_main( + &mut context, + crate_id, + compile_options, + cached_program, + force_recompile, + ) { + Ok(program_and_warnings) => program_and_warnings, + Err(errors) => { + return (context.file_manager, Err(errors)); + } + }; // Apply backend specific optimizations. let optimized_program = nargo::ops::optimize_program(program, np_language, &is_opcode_supported) .expect("Backend does not support an opcode that is in the IR"); - save_program(optimized_program.clone(), package, &workspace.target_directory_path(), false); + save_program( + optimized_program.clone(), + package, + &workspace.target_directory_path(), + output_debug, + ); (context.file_manager, Ok((optimized_program, warnings))) } diff --git a/tooling/nargo_cli/src/cli/execute_cmd.rs b/tooling/nargo_cli/src/cli/execute_cmd.rs index 7d8cf6de39..ffc3ae65f4 100644 --- a/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -60,6 +60,7 @@ pub(crate) fn run( &workspace, package, &args.compile_options, + false, np_language, &|opcode| opcode_support.is_opcode_supported(opcode), )?; diff --git a/tooling/nargo_cli/src/cli/info_cmd.rs b/tooling/nargo_cli/src/cli/info_cmd.rs index 50021e842c..e55b1b7886 100644 --- a/tooling/nargo_cli/src/cli/info_cmd.rs +++ b/tooling/nargo_cli/src/cli/info_cmd.rs @@ -63,6 +63,7 @@ pub(crate) fn run( np_language, &opcode_support, &args.compile_options, + false, )?; let program_info = binary_packages diff --git a/tooling/nargo_cli/src/cli/prove_cmd.rs b/tooling/nargo_cli/src/cli/prove_cmd.rs index af300b7ebe..5571117e2d 100644 --- a/tooling/nargo_cli/src/cli/prove_cmd.rs +++ b/tooling/nargo_cli/src/cli/prove_cmd.rs @@ -59,6 +59,7 @@ pub(crate) fn run( &workspace, package, &args.compile_options, + false, np_language, &|opcode| opcode_support.is_opcode_supported(opcode), )?; diff --git a/tooling/nargo_cli/src/cli/verify_cmd.rs b/tooling/nargo_cli/src/cli/verify_cmd.rs index 6ae2b78fd0..a5a39e9aef 100644 --- a/tooling/nargo_cli/src/cli/verify_cmd.rs +++ b/tooling/nargo_cli/src/cli/verify_cmd.rs @@ -50,6 +50,7 @@ pub(crate) fn run( &workspace, package, &args.compile_options, + false, np_language, &|opcode| opcode_support.is_opcode_supported(opcode), )?;