Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
158 changes: 80 additions & 78 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ use fxhash::FxHashMap as HashMap;
use im::HashSet;
use iter_extended::vecmap;
use noirc_errors::{Located, Location};
pub(crate) use options::ElaboratorOptions;
pub use options::{FrontendOptions, UnstableFeature};
pub use options::{ElaboratorOptions, FrontendOptions, UnstableFeature};
pub use path_resolution::Turbofish;
use path_resolution::{
PathResolution, PathResolutionItem, PathResolutionMode, PathResolutionTarget,
Expand Down Expand Up @@ -119,9 +118,9 @@ pub struct Loop {
pub struct Elaborator<'context> {
scopes: ScopeForest,

pub(crate) errors: Vec<CompilationError>,
pub errors: Vec<CompilationError>,

pub(crate) interner: &'context mut NodeInterner,
pub interner: &'context mut NodeInterner,
pub(crate) def_maps: &'context mut DefMaps,
pub(crate) usage_tracker: &'context mut UsageTracker,
pub(crate) crate_graph: &'context CrateGraph,
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct GenericOptions<'a, T> {
}

/// Options from nargo_cli that need to be passed down to the elaborator
pub(crate) type ElaboratorOptions<'a> = GenericOptions<'a, fm::FileId>;
pub type ElaboratorOptions<'a> = GenericOptions<'a, fm::FileId>;

/// This is the unresolved version of `ElaboratorOptions`
/// CLI options that need to be passed to the compiler frontend (the elaborator).
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
}
}

pub(crate) fn call_function(
pub fn call_function(
&mut self,
function: FuncId,
arguments: Vec<(Value, Location)>,
Expand Down Expand Up @@ -266,7 +266,7 @@
}
} else {
let name = self.elaborator.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 269 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
Value::Expr(Box::new(ExprValue::Statement(statement)))
}

pub(crate) fn lvalue(lvaue: LValue) -> Self {

Check warning on line 114 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
Value::Expr(Box::new(ExprValue::LValue(lvaue)))

Check warning on line 115 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
}

pub(crate) fn pattern(pattern: Pattern) -> Self {
Expand Down Expand Up @@ -174,7 +174,7 @@
})
}

pub(crate) fn into_expression(
pub fn into_expression(
self,
elaborator: &mut Elaborator,
location: Location,
Expand Down Expand Up @@ -339,7 +339,7 @@
Ok(Expression::new(kind, location))
}

pub(crate) fn into_hir_expression(
pub fn into_hir_expression(
self,
interner: &mut NodeInterner,
location: Location,
Expand Down
8 changes: 4 additions & 4 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ pub struct ModuleAttribute {

/// Given a Crate root, collect all definitions in that crate
pub struct DefCollector {
pub(crate) def_map: CrateDefMap,
pub(crate) imports: Vec<ImportDirective>,
pub(crate) items: CollectedItems,
pub def_map: CrateDefMap,
pub imports: Vec<ImportDirective>,
pub items: CollectedItems,
}

#[derive(Default)]
Expand Down Expand Up @@ -209,7 +209,7 @@ impl CompilationError {
}
}

pub(crate) fn is_error(&self) -> bool {
pub fn is_error(&self) -> bool {
// This is a bit expensive but not all error types have a `is_warning` method
// and it'd lead to code duplication to add them. `CompilationError::is_error`
// also isn't expected to be called too often.
Expand Down
9 changes: 6 additions & 3 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct LambdaContext {
///
/// This struct holds the FIFO queue of functions to monomorphize, which is added to
/// whenever a new (function, type) combination is encountered.
pub(super) struct Monomorphizer<'interner> {
pub struct Monomorphizer<'interner> {
/// Functions are keyed by their unique ID, whether they're unconstrained, their expected type,
/// and any generics they have so that we can monomorphize a new version of the function for each type.
///
Expand Down Expand Up @@ -205,7 +205,10 @@ pub fn monomorphize_debug(
}

impl<'interner> Monomorphizer<'interner> {
fn new(interner: &'interner mut NodeInterner, debug_type_tracker: DebugTypeTracker) -> Self {
pub fn new(
interner: &'interner mut NodeInterner,
debug_type_tracker: DebugTypeTracker,
) -> Self {
Monomorphizer {
functions: HashMap::default(),
locals: HashMap::default(),
Expand Down Expand Up @@ -481,7 +484,7 @@ impl<'interner> Monomorphizer<'interner> {
Ok(())
}

fn expr(
pub fn expr(
&mut self,
expr: node_interner::ExprId,
) -> Result<ast::Expression, MonomorphizationError> {
Expand Down
4 changes: 3 additions & 1 deletion tooling/ast_fuzzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ log.workspace = true
acir.workspace = true
acvm.workspace = true
bn254_blackbox_solver.workspace = true
fm.workspace = true
iter-extended.workspace = true
nargo.workspace = true
noirc_abi.workspace = true
noirc_driver.workspace = true
noirc_errors.workspace = true
noirc_evaluator.workspace = true
noirc_frontend.workspace = true
noirc_frontend = { workspace = true, features = ["test_utils"] }
noir_greybox_fuzzer.workspace = true

[dev-dependencies]
Expand Down
11 changes: 9 additions & 2 deletions tooling/ast_fuzzer/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ doc = false
bench = false

[[bin]]
name = "comptime_vs_brillig"
path = "fuzz_targets/comptime_vs_brillig.rs"
name = "comptime_vs_brillig_nargo"
path = "fuzz_targets/comptime_vs_brillig_nargo.rs"
test = false
doc = false
bench = false

[[bin]]
name = "comptime_vs_brillig_direct"
path = "fuzz_targets/comptime_vs_brillig_direct.rs"
test = false
doc = false
bench = false
Expand Down
12 changes: 0 additions & 12 deletions tooling/ast_fuzzer/fuzz/fuzz_targets/comptime_vs_brillig.rs

This file was deleted.

12 changes: 12 additions & 0 deletions tooling/ast_fuzzer/fuzz/fuzz_targets/comptime_vs_brillig_direct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! ```text
//! cargo +nightly fuzz run comptime_vs_brillig_direct
//! ```
#![no_main]

use libfuzzer_sys::arbitrary::Unstructured;

Check warning on line 6 in tooling/ast_fuzzer/fuzz/fuzz_targets/comptime_vs_brillig_direct.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (libfuzzer)
use libfuzzer_sys::fuzz_target;

Check warning on line 7 in tooling/ast_fuzzer/fuzz/fuzz_targets/comptime_vs_brillig_direct.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (libfuzzer)
use noir_ast_fuzzer_fuzz::targets::comptime_vs_brillig_direct;

fuzz_target!(|data: &[u8]| {
comptime_vs_brillig_direct::fuzz(&mut Unstructured::new(data)).unwrap();
});
12 changes: 12 additions & 0 deletions tooling/ast_fuzzer/fuzz/fuzz_targets/comptime_vs_brillig_nargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! ```text
//! cargo +nightly fuzz run comptime_vs_brillig_nargo
//! ```
#![no_main]

use libfuzzer_sys::arbitrary::Unstructured;

Check warning on line 6 in tooling/ast_fuzzer/fuzz/fuzz_targets/comptime_vs_brillig_nargo.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (libfuzzer)
use libfuzzer_sys::fuzz_target;

Check warning on line 7 in tooling/ast_fuzzer/fuzz/fuzz_targets/comptime_vs_brillig_nargo.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (libfuzzer)
use noir_ast_fuzzer_fuzz::targets::comptime_vs_brillig_nargo;

fuzz_target!(|data: &[u8]| {
comptime_vs_brillig_nargo::fuzz(&mut Unstructured::new(data)).unwrap();
});
83 changes: 83 additions & 0 deletions tooling/ast_fuzzer/fuzz/src/targets/comptime_vs_brillig_direct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//! Compare the execution of random ASTs between the comptime execution
//! (after converting the AST to Noir and running it through the comptime
//! interpreter) vs when everything is forced to be Brillig.
//! We choose Brillig here because it mostly matches comptime feature set
//! (e.g. `loop`, `while`, `break` and `continue` are possible)
//! This variant accesses the interpreter directly instead of going
//! through nargo, which speeds up execution but also currently
//! has some issues (inability to use prints among others).
use crate::{compare_results_comptime, create_ssa_or_die, default_ssa_options};
use arbitrary::Unstructured;
use color_eyre::eyre;
use noir_ast_fuzzer::Config;
use noir_ast_fuzzer::compare::CompareComptime;
use noir_ast_fuzzer::compare::CompareOptions;
use noir_ast_fuzzer::rewrite::change_all_functions_into_unconstrained;

pub fn fuzz(u: &mut Unstructured) -> eyre::Result<()> {
let config = Config {
// Avoid using large integers in for loops that the frontend would reject.
avoid_large_int_literals: true,
// Also avoid negative integers, because the frontend rejects them for loops.
avoid_negative_int_literals: true,
// Avoid break/continue
avoid_loop_control: true,
// Has to only use expressions valid in comptime
comptime_friendly: true,
// Force brillig, to generate loops that the interpreter can do but ACIR cannot.
force_brillig: true,
// Avoid overflows, divisions by zero and constraints for now, as we currently
// don't catch errors issued by the elaborator
avoid_overflow: true,
avoid_err_by_zero: true,
avoid_constrain: true,
// At the moment prints aren't recognized by elaborator
avoid_print: true,
// Use lower limits because of the interpreter, to avoid stack overflow
max_loop_size: 5,
max_recursive_calls: 5,
..Default::default()
};

let inputs = CompareComptime::arb_direct(
u,
config,
|program| {
let options = CompareOptions::default();
let ssa = create_ssa_or_die(
change_all_functions_into_unconstrained(program),
&options.onto(default_ssa_options()),
Some("brillig"),
);
Ok((ssa, options))
},
|program| {
let options = CompareOptions::default();
let ssa = create_ssa_or_die(
program,
&options.onto(default_ssa_options()),
Some("comptime_result_wrapper"),
);
Ok((ssa, options))
},
)?;

let result = inputs.exec_direct()?;

compare_results_comptime(&inputs, &result)
}

#[cfg(test)]
mod tests {

/// ```ignore
/// NOIR_ARBTEST_SEED=0x6819c61400001000 \
/// NOIR_AST_FUZZER_SHOW_AST=1 \
/// cargo test -p noir_ast_fuzzer_fuzz comptime_vs_brillig_direct
/// ```
#[test]
fn fuzz_with_arbtest() {
// TODO(#8870): Allow more tests when the bug is fixed (fails in the 128th case).
crate::targets::tests::fuzz_with_arbtest(super::fuzz, 127);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//! interpreter) vs when everything is forced to be Brillig.
//! We choose Brillig here because it mostly matches comptime feature set
//! (e.g. `loop`, `while`, `break` and `continue` are possible)
//! This variant lets nargo parse the resulting source code which is slow
//! but at the moment is more feature complete than using the interpreter
//! directly.
use crate::{compare_results_comptime, create_ssa_or_die, default_ssa_options};
use arbitrary::Unstructured;
use color_eyre::eyre;
Expand Down Expand Up @@ -31,7 +34,7 @@ pub fn fuzz(u: &mut Unstructured) -> eyre::Result<()> {
..Default::default()
};

let inputs = CompareComptime::arb(u, config, |_, program| {
let inputs = CompareComptime::arb(u, config, |program| {
let options = CompareOptions::default();
let ssa = create_ssa_or_die(
change_all_functions_into_unconstrained(program),
Expand Down
3 changes: 2 additions & 1 deletion tooling/ast_fuzzer/fuzz/src/targets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod acir_vs_brillig;
pub mod comptime_vs_brillig;
pub mod comptime_vs_brillig_direct;
pub mod comptime_vs_brillig_nargo;
pub mod min_vs_full;
pub mod orig_vs_morph;
pub mod pass_vs_prev;
Expand Down
2 changes: 1 addition & 1 deletion tooling/ast_fuzzer/src/compare/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl From<(SsaProgramArtifact, CompareOptions)> for CompareArtifact {
type SsaErrorTypes = BTreeMap<acir::circuit::ErrorSelector, ErrorType>;

/// The execution result is the value returned from the circuit and any output from `println`.
type ExecResult = (Result<WitnessStack<FieldElement>, NargoError<FieldElement>>, String);
pub(crate) type ExecResult = (Result<WitnessStack<FieldElement>, NargoError<FieldElement>>, String);

pub struct NargoErrorWithTypes(NargoError<FieldElement>, SsaErrorTypes);

Expand Down
Loading
Loading