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

fix: force recompilation when output_debug flag is set. #2898

Merged
merged 5 commits into from
Sep 29, 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
10 changes: 6 additions & 4 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
// with the restricted version which only uses one file
pub fn compile_file(context: &mut Context, root_file: &Path) -> CompilationResult<CompiledProgram> {
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
Expand Down Expand Up @@ -148,6 +148,7 @@
crate_id: CrateId,
options: &CompileOptions,
cached_program: Option<CompiledProgram>,
force_compile: bool,
) -> CompilationResult<CompiledProgram> {
let (_, warnings) = check_crate(context, crate_id, options.deny_warnings)?;

Expand All @@ -163,10 +164,10 @@
}
};

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):");

Check warning on line 170 in compiler/noirc_driver/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (unoptimized)
println!("{}", compiled_program.circuit);
}

Expand Down Expand Up @@ -215,7 +216,7 @@
if options.print_acir {
for contract_function in &compiled_contract.functions {
println!(
"Compiled ACIR for {}::{} (unoptimized):",

Check warning on line 219 in compiler/noirc_driver/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (unoptimized)
compiled_contract.name, contract_function.name
);
println!("{}", contract_function.bytecode);
Expand Down Expand Up @@ -257,7 +258,7 @@
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);
Expand Down Expand Up @@ -314,14 +315,15 @@
options: &CompileOptions,
main_function: FuncId,
cached_program: Option<CompiledProgram>,
force_compile: bool,
) -> Result<CompiledProgram, FileDiagnostic> {
let program = monomorphize(main_function, &context.def_interner);

let hash = fxhash::hash64(&program);

Check warning on line 322 in compiler/noirc_driver/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (fxhash)

// 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);
Expand Down
7 changes: 4 additions & 3 deletions compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ pub fn compile(args: JsValue) -> JsValue {

<JsValue as JsValueSerdeExt>::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)
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo/src/ops/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn run_test<B: BlackBoxFunctionSolver>(
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,
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn smart_contract_for_package(
workspace,
package,
compile_options,
false,
np_language,
&is_opcode_supported,
)?;
Expand Down
52 changes: 41 additions & 11 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub(crate) fn run(
np_language,
&opcode_support,
&args.compile_options,
args.output_debug,
)?;

// Save build artifacts to disk.
Expand All @@ -99,14 +100,22 @@ pub(super) fn compile_workspace(
np_language: Language,
opcode_support: &BackendOpcodeSupport,
compile_options: &CompileOptions,
output_debug: bool,
) -> Result<(Vec<CompiledProgram>, Vec<CompiledContract>), CliError> {
let is_opcode_supported = |opcode: &_| opcode_support.is_opcode_supported(opcode);

// Compile all of the packages in parallel.
let program_results: Vec<(FileManager, CompilationResult<CompiledProgram>)> = 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<CompiledContract>)> =
Expand Down Expand Up @@ -138,15 +147,22 @@ 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<CompiledProgram, CliError> {
if package.is_library() {
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)?;

Expand All @@ -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<CompiledProgram>) {
Expand All @@ -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;
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

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)))
}
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub(crate) fn run(
&workspace,
package,
&args.compile_options,
false,
np_language,
&|opcode| opcode_support.is_opcode_supported(opcode),
)?;
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub(crate) fn run(
np_language,
&opcode_support,
&args.compile_options,
false,
)?;

let program_info = binary_packages
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub(crate) fn run(
&workspace,
package,
&args.compile_options,
false,
np_language,
&|opcode| opcode_support.is_opcode_supported(opcode),
)?;
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub(crate) fn run(
&workspace,
package,
&args.compile_options,
false,
np_language,
&|opcode| opcode_support.is_opcode_supported(opcode),
)?;
Expand Down