Skip to content

Commit

Permalink
feat: add support for overriding expression width (#4117)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #3854 

## Summary\*

This PR adds the `expression-width` option to the CLI which allows users
to specify the expression width they want to use. Passing zero will
result in an unbounded width.

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
TomAFrench and kevaundray authored Jan 25, 2024
1 parent 4eca52f commit c8026d5
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 16 deletions.
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

0 comments on commit c8026d5

Please sign in to comment.