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

feat: add support for overriding expression width #4117

Merged
merged 3 commits into from
Jan 25, 2024
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: 10 additions & 0 deletions acvm-repo/acvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ pub enum ExpressionWidth {
Unbounded,
Bounded { width: usize },
}

impl From<usize> for ExpressionWidth {
fn from(width: usize) -> ExpressionWidth {
if width == 0 {
ExpressionWidth::Unbounded
} else {
ExpressionWidth::Bounded { width }
}
}
}
18 changes: 16 additions & 2 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]

use acvm::ExpressionWidth;
use clap::Args;
use fm::{FileId, FileManager};
use iter_extended::vecmap;
Expand All @@ -16,7 +17,6 @@ use noirc_frontend::hir::Context;
use noirc_frontend::macros_api::MacroProcessor;
use noirc_frontend::monomorphization::monomorphize;
use noirc_frontend::node_interner::FuncId;
use serde::{Deserialize, Serialize};
use std::path::Path;
use tracing::info;

Expand All @@ -43,8 +43,12 @@ pub const NOIRC_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const NOIR_ARTIFACT_VERSION_STRING: &str =
concat!(env!("CARGO_PKG_VERSION"), "+", env!("GIT_COMMIT"));

#[derive(Args, Clone, Debug, Default, Serialize, Deserialize)]
#[derive(Args, Clone, Debug, Default)]
pub struct CompileOptions {
/// Override the expression width requested by the backend.
#[arg(long, value_parser = parse_expression_width)]
pub expression_width: Option<ExpressionWidth>,

/// Force a full recompilation.
#[arg(long = "force")]
pub force_compile: bool,
Expand Down Expand Up @@ -81,6 +85,16 @@ pub struct CompileOptions {
pub show_monomorphized: bool,
}

fn parse_expression_width(input: &str) -> Result<ExpressionWidth, std::io::Error> {
use std::io::{Error, ErrorKind};

let width = input
.parse::<usize>()
.map_err(|err| Error::new(ErrorKind::InvalidInput, err.to_string()))?;

Ok(ExpressionWidth::from(width))
}

/// Helper type used to signify where only warnings are expected in file diagnostics
pub type Warnings = Vec<FileDiagnostic>;

Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ pub(crate) fn run(
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);

let expression_width = backend.get_backend_info_or_default();
let expression_width = args
.compile_options
.expression_width
.unwrap_or_else(|| backend.get_backend_info_or_default());
let (compiled_program, compiled_contracts) = compile_workspace(
&workspace_file_manager,
&parsed_files,
Expand Down
37 changes: 29 additions & 8 deletions tooling/nargo_cli/src/cli/dap_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use acvm::acir::native_types::WitnessMap;
use acvm::ExpressionWidth;
use backend_interface::Backend;
use clap::Args;
use nargo::constants::PROVER_INPUT_FILE;
Expand Down Expand Up @@ -29,7 +30,21 @@ use crate::errors::CliError;
use super::NargoConfig;

#[derive(Debug, Clone, Args)]
pub(crate) struct DapCommand;
pub(crate) struct DapCommand {
/// Override the expression width requested by the backend.
#[arg(long, value_parser = parse_expression_width)]
expression_width: Option<ExpressionWidth>,
}

fn parse_expression_width(input: &str) -> Result<ExpressionWidth, std::io::Error> {
use std::io::{Error, ErrorKind};

let width = input
.parse::<usize>()
.map_err(|err| Error::new(ErrorKind::InvalidInput, err.to_string()))?;

Ok(ExpressionWidth::from(width))
}

struct LoadError(&'static str);

Expand All @@ -54,16 +69,14 @@ fn find_workspace(project_folder: &str, package: Option<&str>) -> Option<Workspa
}

fn load_and_compile_project(
backend: &Backend,
project_folder: &str,
package: Option<&str>,
prover_name: &str,
expression_width: ExpressionWidth,
) -> Result<(CompiledProgram, WitnessMap), LoadError> {
let workspace =
find_workspace(project_folder, package).ok_or(LoadError("Cannot open workspace"))?;

let expression_width =
backend.get_backend_info().map_err(|_| LoadError("Failed to get backend info"))?;
let package = workspace
.into_iter()
.find(|p| p.is_binary())
Expand Down Expand Up @@ -100,7 +113,7 @@ fn load_and_compile_project(

fn loop_uninitialized_dap<R: Read, W: Write>(
mut server: Server<R, W>,
backend: &Backend,
expression_width: ExpressionWidth,
) -> Result<(), ServerError> {
loop {
let req = match server.poll_request()? {
Expand Down Expand Up @@ -140,7 +153,12 @@ fn loop_uninitialized_dap<R: Read, W: Write>(
eprintln!("Package: {}", package.unwrap_or("(default)"));
eprintln!("Prover name: {}", prover_name);

match load_and_compile_project(backend, project_folder, package, prover_name) {
match load_and_compile_project(
project_folder,
package,
prover_name,
expression_width,
) {
Ok((compiled_program, initial_witness)) => {
server.respond(req.ack()?)?;

Expand Down Expand Up @@ -176,12 +194,15 @@ fn loop_uninitialized_dap<R: Read, W: Write>(

pub(crate) fn run(
backend: &Backend,
_args: DapCommand,
args: DapCommand,
_config: NargoConfig,
) -> Result<(), CliError> {
let output = BufWriter::new(std::io::stdout());
let input = BufReader::new(std::io::stdin());
let server = Server::new(input, output);

loop_uninitialized_dap(server, backend).map_err(CliError::DapError)
let expression_width =
args.expression_width.unwrap_or_else(|| backend.get_backend_info_or_default());

loop_uninitialized_dap(server, expression_width).map_err(CliError::DapError)
}
5 changes: 4 additions & 1 deletion tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pub(crate) fn run(
Some(NOIR_ARTIFACT_VERSION_STRING.to_string()),
)?;
let target_dir = &workspace.target_directory_path();
let expression_width = backend.get_backend_info()?;
let expression_width = args
.compile_options
.expression_width
.unwrap_or_else(|| backend.get_backend_info_or_default());

let mut workspace_file_manager = file_manager_with_stdlib(std::path::Path::new(""));
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ pub(crate) fn run(
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);

let expression_width = backend.get_backend_info_or_default();
let expression_width = args
.compile_options
.expression_width
.unwrap_or_else(|| backend.get_backend_info_or_default());
let binary_packages = workspace.into_iter().filter(|package| package.is_binary());
for package in binary_packages {
let compilation_result = compile_program(
Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub(crate) fn run(
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);

let expression_width = backend.get_backend_info_or_default();
let expression_width = args
.compile_options
.expression_width
.unwrap_or_else(|| backend.get_backend_info_or_default());
let (compiled_programs, compiled_contracts) = compile_workspace(
&workspace_file_manager,
&parsed_files,
Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub(crate) fn run(
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);

let expression_width = backend.get_backend_info()?;
let expression_width = args
.compile_options
.expression_width
.unwrap_or_else(|| backend.get_backend_info_or_default());
let binary_packages = workspace.into_iter().filter(|package| package.is_binary());
for package in binary_packages {
let compilation_result = compile_program(
Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo_cli/src/cli/verify_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pub(crate) fn run(
insert_all_files_for_workspace_into_file_manager(&workspace, &mut workspace_file_manager);
let parsed_files = parse_all(&workspace_file_manager);

let expression_width = backend.get_backend_info()?;
let expression_width = args
.compile_options
.expression_width
.unwrap_or_else(|| backend.get_backend_info_or_default());
let binary_packages = workspace.into_iter().filter(|package| package.is_binary());
for package in binary_packages {
let compilation_result = compile_program(
Expand Down
Loading