Skip to content

Commit

Permalink
feat: completely separate compilation and circuit narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jan 22, 2024
1 parent 0570f70 commit d7bd359
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 80 deletions.
4 changes: 2 additions & 2 deletions compiler/noirc_driver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum ContractFunctionType {
Unconstrained,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CompiledContract {
pub noir_version: String,

Expand All @@ -51,7 +51,7 @@ pub struct CompiledContract {
/// A contract function unlike a regular Noir program
/// however can have additional properties.
/// One of these being a function type.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractFunction {
pub name: String,

Expand Down
20 changes: 13 additions & 7 deletions tooling/lsp/src/requests/profile_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,32 @@ fn on_profile_run_request_inner(
&workspace_file_manager,
&parsed_files,
&workspace,
expression_width,
&CompileOptions::default(),
)
.map_err(|err| ResponseError::new(ErrorCode::REQUEST_FAILED, err))?;

let mut opcodes_counts: HashMap<Location, OpCodesCount> = HashMap::new();
let mut file_map: BTreeMap<FileId, DebugFile> = BTreeMap::new();
for compiled_program in &compiled_programs {
for compiled_program in compiled_programs {
let compiled_program =
nargo::ops::transform_program(compiled_program, expression_width);

let span_opcodes = compiled_program.debug.count_span_opcodes();
let debug_artifact: DebugArtifact = compiled_program.clone().into();
opcodes_counts.extend(span_opcodes);
file_map.extend(debug_artifact.file_map);
}

for compiled_contract in &compiled_contracts {
let functions = &compiled_contract.functions;
let debug_artifact: DebugArtifact = compiled_contract.clone().into();
for compiled_contract in compiled_contracts {
let compiled_contract =
nargo::ops::transform_contract(compiled_contract, expression_width);

let function_debug_info: Vec<_> =
compiled_contract.functions.iter().map(|func| &func.debug).cloned().collect();
let debug_artifact: DebugArtifact = compiled_contract.into();
file_map.extend(debug_artifact.file_map);
for contract_function in functions {
let span_opcodes = contract_function.debug.count_span_opcodes();
for contract_function_debug in function_debug_info {
let span_opcodes = contract_function_debug.count_span_opcodes();
opcodes_counts.extend(span_opcodes);
}
}
Expand Down
12 changes: 6 additions & 6 deletions tooling/nargo/src/artifacts/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ impl From<CompiledProgram> for DebugArtifact {
}
}

impl From<&CompiledContract> for DebugArtifact {
fn from(compiled_artifact: &CompiledContract) -> Self {
impl From<CompiledContract> for DebugArtifact {
fn from(compiled_artifact: CompiledContract) -> Self {
let all_functions_debug: Vec<DebugInfo> = compiled_artifact
.functions
.iter()
.map(|contract_function| contract_function.debug.clone())
.into_iter()
.map(|contract_function| contract_function.debug)
.collect();

DebugArtifact {
debug_symbols: all_functions_debug,
file_map: compiled_artifact.file_map.clone(),
warnings: compiled_artifact.warnings.clone(),
file_map: compiled_artifact.file_map,
warnings: compiled_artifact.warnings,
}
}
}
Expand Down
37 changes: 4 additions & 33 deletions tooling/nargo/src/ops/compile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use acvm::ExpressionWidth;
use fm::FileManager;
use noirc_driver::{CompilationResult, CompileOptions, CompiledContract, CompiledProgram};
use noirc_frontend::hir::ParsedFiles;
Expand All @@ -9,8 +8,6 @@ use crate::{package::Package, workspace::Workspace};

use rayon::prelude::*;

use super::{transform_contract, transform_program};

/// Compiles workspace.
///
/// # Errors
Expand All @@ -20,7 +17,6 @@ pub fn compile_workspace(
file_manager: &FileManager,
parsed_files: &ParsedFiles,
workspace: &Workspace,
expression_width: ExpressionWidth,
compile_options: &CompileOptions,
) -> Result<(Vec<CompiledProgram>, Vec<CompiledContract>), CompileError> {
let (binary_packages, contract_packages): (Vec<_>, Vec<_>) = workspace
Expand All @@ -32,22 +28,11 @@ pub fn compile_workspace(
// Compile all of the packages in parallel.
let program_results: Vec<CompilationResult<CompiledProgram>> = binary_packages
.par_iter()
.map(|package| {
compile_program(
file_manager,
parsed_files,
package,
compile_options,
expression_width,
None,
)
})
.map(|package| compile_program(file_manager, parsed_files, package, compile_options, None))
.collect();
let contract_results: Vec<CompilationResult<CompiledContract>> = contract_packages
.par_iter()
.map(|package| {
compile_contract(file_manager, parsed_files, package, compile_options, expression_width)
})
.map(|package| compile_contract(file_manager, parsed_files, package, compile_options))
.collect();

// Report any warnings/errors which were encountered during compilation.
Expand Down Expand Up @@ -82,34 +67,20 @@ pub fn compile_program(
parsed_files: &ParsedFiles,
package: &Package,
compile_options: &CompileOptions,
expression_width: ExpressionWidth,
cached_program: Option<CompiledProgram>,
) -> CompilationResult<CompiledProgram> {
let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);

let (program, warnings) =
noirc_driver::compile_main(&mut context, crate_id, compile_options, cached_program)?;

// Apply backend specific optimizations.
let optimized_program = transform_program(program, expression_width);

Ok((optimized_program, warnings))
noirc_driver::compile_main(&mut context, crate_id, compile_options, cached_program)
}

pub fn compile_contract(
file_manager: &FileManager,
parsed_files: &ParsedFiles,
package: &Package,
compile_options: &CompileOptions,
expression_width: ExpressionWidth,
) -> CompilationResult<CompiledContract> {
let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);
let (contract, warnings) =
noirc_driver::compile_contract(&mut context, crate_id, compile_options)?;

let optimized_contract = transform_contract(contract, expression_width);

Ok((optimized_contract, warnings))
noirc_driver::compile_contract(&mut context, crate_id, compile_options)
}

pub(crate) fn report_errors<T>(
Expand Down
3 changes: 2 additions & 1 deletion tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub(crate) fn run(
&parsed_files,
package,
&args.compile_options,
expression_width,
None,
);

Expand All @@ -64,6 +63,8 @@ pub(crate) fn run(
args.compile_options.silence_warnings,
)?;

let program = nargo::ops::transform_program(program, expression_width);

let smart_contract_string = backend.eth_contract(&program.circuit)?;

let contract_dir = workspace.contracts_directory_path(package);
Expand Down
19 changes: 4 additions & 15 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::path::Path;

use acvm::ExpressionWidth;

use fm::FileManager;
use nargo::artifacts::program::ProgramArtifact;
use nargo::errors::CompileError;
Expand Down Expand Up @@ -68,7 +66,6 @@ pub(crate) fn run(
&workspace_file_manager,
&parsed_files,
&workspace,
expression_width,
&args.compile_options,
)?;

Expand All @@ -81,9 +78,11 @@ pub(crate) fn run(
// Save build artifacts to disk.
let only_acir = args.compile_options.only_acir;
for (package, program) in binary_packages.into_iter().zip(compiled_program) {
let program = nargo::ops::transform_program(program, expression_width);
save_program(program.clone(), &package, &workspace.target_directory_path(), only_acir);
}
for (package, contract) in contract_packages.into_iter().zip(compiled_contracts) {
let contract = nargo::ops::transform_contract(contract, expression_width);
save_contract(contract, &package, &circuit_dir);
}

Expand All @@ -94,7 +93,6 @@ pub(super) fn compile_workspace(
file_manager: &FileManager,
parsed_files: &ParsedFiles,
workspace: &Workspace,
expression_width: ExpressionWidth,
compile_options: &CompileOptions,
) -> Result<(Vec<CompiledProgram>, Vec<CompiledContract>), CliError> {
let (binary_packages, contract_packages): (Vec<_>, Vec<_>) = workspace
Expand All @@ -114,21 +112,12 @@ pub(super) fn compile_workspace(
.filter(|p| p.noir_version == NOIR_ARTIFACT_VERSION_STRING)
.map(|p| p.into());

compile_program(
file_manager,
parsed_files,
package,
compile_options,
expression_width,
cached_program,
)
compile_program(file_manager, parsed_files, package, compile_options, cached_program)
})
.collect();
let contract_results: Vec<CompilationResult<CompiledContract>> = contract_packages
.par_iter()
.map(|package| {
compile_contract(file_manager, parsed_files, package, compile_options, expression_width)
})
.map(|package| compile_contract(file_manager, parsed_files, package, compile_options))
.collect();

// Report any warnings/errors which were encountered during compilation.
Expand Down
12 changes: 4 additions & 8 deletions tooling/nargo_cli/src/cli/dap_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,8 @@ fn load_and_compile_project(
let parsed_files = parse_all(&workspace_file_manager);

let compile_options = CompileOptions::default();
let compilation_result = compile_program(
&workspace_file_manager,
&parsed_files,
package,
&compile_options,
expression_width,
None,
);
let compilation_result =
compile_program(&workspace_file_manager, &parsed_files, package, &compile_options, None);

let compiled_program = report_errors(
compilation_result,
Expand All @@ -91,6 +85,8 @@ fn load_and_compile_project(
)
.map_err(|_| LoadError("Failed to compile project"))?;

let compiled_program = nargo::ops::transform_program(compiled_program, expression_width);

let (inputs_map, _) =
read_inputs_from_file(&package.root_dir, prover_name, Format::Toml, &compiled_program.abi)
.map_err(|_| LoadError("Failed to read program inputs"))?;
Expand Down
3 changes: 2 additions & 1 deletion tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub(crate) fn run(
&parsed_files,
package,
&args.compile_options,
expression_width,
None,
);

Expand All @@ -83,6 +82,8 @@ pub(crate) fn run(
args.compile_options.silence_warnings,
)?;

let compiled_program = nargo::ops::transform_program(compiled_program, expression_width);

run_async(package, compiled_program, &args.prover_name, &args.witness_name, target_dir)
}

Expand Down
3 changes: 2 additions & 1 deletion tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ pub(crate) fn run(
&parsed_files,
package,
&args.compile_options,
expression_width,
None,
);

Expand All @@ -87,6 +86,8 @@ pub(crate) fn run(
args.compile_options.silence_warnings,
)?;

let compiled_program = nargo::ops::transform_program(compiled_program, expression_width);

let (return_value, solved_witness) = execute_program_and_decode(
compiled_program,
package,
Expand Down
12 changes: 9 additions & 3 deletions tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,25 @@ pub(crate) fn run(
&workspace_file_manager,
&parsed_files,
&workspace,
expression_width,
&args.compile_options,
)?;

let compiled_programs = vecmap(compiled_programs, |program| {
nargo::ops::transform_program(program, expression_width)
});
let compiled_contracts = vecmap(compiled_contracts, |contract| {
nargo::ops::transform_contract(contract, expression_width)
});

if args.profile_info {
for compiled_program in &compiled_programs {
let span_opcodes = compiled_program.debug.count_span_opcodes();
let debug_artifact: DebugArtifact = compiled_program.clone().into();
let debug_artifact = DebugArtifact::from(compiled_program.clone());
print_span_opcodes(span_opcodes, &debug_artifact);
}

for compiled_contract in &compiled_contracts {
let debug_artifact: DebugArtifact = compiled_contract.clone().into();
let debug_artifact = DebugArtifact::from(compiled_contract.clone());
let functions = &compiled_contract.functions;
for contract_function in functions {
let span_opcodes = contract_function.debug.count_span_opcodes();
Expand Down
3 changes: 2 additions & 1 deletion tooling/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ pub(crate) fn run(
&parsed_files,
package,
&args.compile_options,
expression_width,
None,
);

Expand All @@ -88,6 +87,8 @@ pub(crate) fn run(
args.compile_options.silence_warnings,
)?;

let compiled_program = nargo::ops::transform_program(compiled_program, expression_width);

prove_package(
backend,
&workspace,
Expand Down
3 changes: 2 additions & 1 deletion tooling/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub(crate) fn run(
&parsed_files,
package,
&args.compile_options,
expression_width,
None,
);

Expand All @@ -73,6 +72,8 @@ pub(crate) fn run(
args.compile_options.silence_warnings,
)?;

let compiled_program = nargo::ops::transform_program(compiled_program, expression_width);

verify_package(backend, &workspace, package, compiled_program, &args.verifier_name)?;
}

Expand Down
2 changes: 1 addition & 1 deletion tooling/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn decode_string_value(field_elements: &[FieldElement]) -> String {
final_string.to_owned()
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContractEvent {
/// Event name
name: String,
Expand Down

0 comments on commit d7bd359

Please sign in to comment.