From 39610af5b3cc8de7e3aa963a2cbff3083179cbf4 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 2 Aug 2023 01:28:09 -0700 Subject: [PATCH 01/11] chore(noirc_driver): Unify crate preparation (#2119) --- crates/lsp/src/lib.rs | 6 +++--- crates/lsp/src/lib_hacky.rs | 7 +++---- crates/nargo_cli/src/cli/mod.rs | 4 ++-- crates/nargo_cli/src/lib.rs | 7 +++---- crates/noirc_driver/src/lib.rs | 29 +++-------------------------- crates/wasm/src/compile.rs | 8 ++++---- 6 files changed, 18 insertions(+), 43 deletions(-) diff --git a/crates/lsp/src/lib.rs b/crates/lsp/src/lib.rs index bd4112218e4..1c02c802808 100644 --- a/crates/lsp/src/lib.rs +++ b/crates/lsp/src/lib.rs @@ -22,7 +22,7 @@ use lsp_types::{ InitializeParams, InitializeResult, InitializedParams, Position, PublishDiagnosticsParams, Range, ServerCapabilities, TextDocumentSyncOptions, }; -use noirc_driver::{check_crate, create_local_crate}; +use noirc_driver::{check_crate, prepare_crate}; use noirc_errors::{DiagnosticKind, FileDiagnostic}; use noirc_frontend::{ graph::{CrateGraph, CrateType}, @@ -190,7 +190,7 @@ fn on_code_lens_request( } }; - let crate_id = create_local_crate(&mut context, file_path, CrateType::Binary); + let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary); // We ignore the warnings and errors produced by compilation for producing codelenses // because we can still get the test functions even if compilation fails @@ -283,7 +283,7 @@ fn on_did_save_text_document( } }; - let crate_id = create_local_crate(&mut context, file_path, CrateType::Binary); + let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary); let mut diagnostics = Vec::new(); diff --git a/crates/lsp/src/lib_hacky.rs b/crates/lsp/src/lib_hacky.rs index 72a2625fcac..13bb2b82847 100644 --- a/crates/lsp/src/lib_hacky.rs +++ b/crates/lsp/src/lib_hacky.rs @@ -19,7 +19,7 @@ use lsp_types::{ InitializedParams, Position, PublishDiagnosticsParams, Range, ServerCapabilities, TextDocumentSyncOptions, }; -use noirc_driver::{check_crate, create_local_crate, create_non_local_crate, propagate_dep}; +use noirc_driver::{check_crate, prepare_crate, propagate_dep}; use noirc_errors::{DiagnosticKind, FileDiagnostic}; use noirc_frontend::{ graph::{CrateGraph, CrateId, CrateType}, @@ -286,7 +286,7 @@ fn create_context_at_path( } let nargo_toml_path = find_nearest_parent_file(&file_path, &["Nargo.toml"]); - let current_crate_id = create_local_crate(&mut context, &file_path, CrateType::Binary); + let current_crate_id = prepare_crate(&mut context, &file_path, CrateType::Binary); // TODO(AD): undo hacky dependency resolution if let Some(nargo_toml_path) = nargo_toml_path { @@ -297,8 +297,7 @@ fn create_context_at_path( .parent() .unwrap() // TODO .join(PathBuf::from(&dependency_path).join("src").join("lib.nr")); - let library_crate = - create_non_local_crate(&mut context, &path_to_lib, CrateType::Library); + let library_crate = prepare_crate(&mut context, &path_to_lib, CrateType::Library); propagate_dep(&mut context, library_crate, &crate_name.parse().unwrap()); } } diff --git a/crates/nargo_cli/src/cli/mod.rs b/crates/nargo_cli/src/cli/mod.rs index 8ce66db1b7b..9d494b21e6a 100644 --- a/crates/nargo_cli/src/cli/mod.rs +++ b/crates/nargo_cli/src/cli/mod.rs @@ -92,7 +92,7 @@ pub fn start_cli() -> eyre::Result<()> { #[cfg(test)] mod tests { use fm::FileManager; - use noirc_driver::{check_crate, create_local_crate}; + use noirc_driver::{check_crate, prepare_crate}; use noirc_errors::reporter; use noirc_frontend::{ graph::{CrateGraph, CrateType}, @@ -110,7 +110,7 @@ mod tests { let fm = FileManager::new(root_dir); let graph = CrateGraph::default(); let mut context = Context::new(fm, graph); - let crate_id = create_local_crate(&mut context, root_file, CrateType::Binary); + let crate_id = prepare_crate(&mut context, root_file, CrateType::Binary); let result = check_crate(&mut context, crate_id, false); let success = result.is_ok(); diff --git a/crates/nargo_cli/src/lib.rs b/crates/nargo_cli/src/lib.rs index b456d31c0ca..05753f7f3d8 100644 --- a/crates/nargo_cli/src/lib.rs +++ b/crates/nargo_cli/src/lib.rs @@ -9,7 +9,7 @@ use fm::FileManager; use nargo::package::{Dependency, Package}; -use noirc_driver::{add_dep, create_local_crate, create_non_local_crate}; +use noirc_driver::{add_dep, prepare_crate}; use noirc_frontend::{ graph::{CrateGraph, CrateId, CrateName, CrateType}, hir::Context, @@ -107,8 +107,7 @@ fn prepare_dependencies( for (dep_name, dep) in dependencies.into_iter() { match dep { Dependency::Remote { package } | Dependency::Local { package } => { - let crate_id = - create_non_local_crate(context, &package.entry_path, package.crate_type); + let crate_id = prepare_crate(context, &package.entry_path, package.crate_type); add_dep(context, parent_crate, crate_id, dep_name); prepare_dependencies(context, crate_id, package.dependencies.to_owned()); } @@ -121,7 +120,7 @@ fn prepare_package(package: &Package) -> (Context, CrateId) { let graph = CrateGraph::default(); let mut context = Context::new(fm, graph); - let crate_id = create_local_crate(&mut context, &package.entry_path, package.crate_type); + let crate_id = prepare_crate(&mut context, &package.entry_path, package.crate_type); prepare_dependencies(&mut context, crate_id, package.dependencies.to_owned()); diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index c0957313f69..4d1b7fe2675 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -52,40 +52,17 @@ pub fn compile_file( context: &mut Context, root_file: &Path, ) -> Result<(CompiledProgram, Warnings), ErrorsAndWarnings> { - let crate_id = create_local_crate(context, root_file, CrateType::Binary); + let crate_id = prepare_crate(context, root_file, CrateType::Binary); compile_main(context, crate_id, &CompileOptions::default()) } -/// Adds the File with the local crate root to the file system -/// and adds the local crate to the graph -/// XXX: This may pose a problem with workspaces, where you can change the local crate and where -/// we have multiple binaries in one workspace -/// A Fix would be for the driver instance to store the local crate id. -// Granted that this is the only place which relies on the local crate being first -pub fn create_local_crate( - context: &mut Context, - file_name: &Path, - crate_type: CrateType, -) -> CrateId { +/// Adds the file from the file system at `Path` to the crate graph +pub fn prepare_crate(context: &mut Context, file_name: &Path, crate_type: CrateType) -> CrateId { let root_file_id = context.file_manager.add_file(file_name).unwrap(); context.crate_graph.add_crate_root(crate_type, root_file_id) } -/// Creates a Non Local Crate. A Non Local Crate is any crate which is the not the crate that -/// the compiler is compiling. -pub fn create_non_local_crate( - context: &mut Context, - file_name: &Path, - crate_type: CrateType, -) -> CrateId { - let root_file_id = context.file_manager.add_file(file_name).unwrap(); - - // You can add any crate type to the crate graph - // but you cannot depend on Binaries - context.crate_graph.add_crate_root(crate_type, root_file_id) -} - /// Adds a edge in the crate graph for two crates pub fn add_dep( context: &mut Context, diff --git a/crates/wasm/src/compile.rs b/crates/wasm/src/compile.rs index c940f0ce246..15d8d5107ea 100644 --- a/crates/wasm/src/compile.rs +++ b/crates/wasm/src/compile.rs @@ -3,8 +3,8 @@ use fm::FileManager; use gloo_utils::format::JsValueSerdeExt; use log::debug; use noirc_driver::{ - check_crate, compile_contracts, compile_no_check, create_local_crate, create_non_local_crate, - propagate_dep, CompileOptions, CompiledContract, + check_crate, compile_contracts, compile_no_check, prepare_crate, propagate_dep, CompileOptions, + CompiledContract, }; use noirc_frontend::{ graph::{CrateGraph, CrateType}, @@ -63,7 +63,7 @@ impl Default for WASMCompileOptions { fn add_noir_lib(context: &mut Context, crate_name: &str) { let path_to_lib = Path::new(&crate_name).join("lib.nr"); - let library_crate = create_non_local_crate(context, &path_to_lib, CrateType::Library); + let library_crate = prepare_crate(context, &path_to_lib, CrateType::Library); propagate_dep(context, library_crate, &crate_name.parse().unwrap()); } @@ -87,7 +87,7 @@ pub fn compile(args: JsValue) -> JsValue { let mut context = Context::new(fm, graph); let path = Path::new(&options.entry_point); - let crate_id = create_local_crate(&mut context, path, CrateType::Binary); + let crate_id = prepare_crate(&mut context, path, CrateType::Binary); for dependency in options.optional_dependencies_set { add_noir_lib(&mut context, dependency.as_str()); From f7742ab026092f129bd4ec4f122bcd3249100529 Mon Sep 17 00:00:00 2001 From: jfecher Date: Wed, 2 Aug 2023 03:59:08 -0500 Subject: [PATCH 02/11] fix: flattening pass no longer overwrites previously mapped condition values (#2117) * Fix flattening pass overwriting previously mapped values * chore: add backticks to variable names in comment --------- Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> --- .../test_data/regression_2099/Nargo.toml | 6 +++ .../test_data/regression_2099/src/main.nr | 37 +++++++++++++++++++ .../src/ssa_refactor/ir/function_inserter.rs | 1 - .../src/ssa_refactor/opt/flatten_cfg.rs | 5 ++- 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 crates/nargo_cli/tests/test_data/regression_2099/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data/regression_2099/src/main.nr diff --git a/crates/nargo_cli/tests/test_data/regression_2099/Nargo.toml b/crates/nargo_cli/tests/test_data/regression_2099/Nargo.toml new file mode 100644 index 00000000000..ca96e7164a5 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/regression_2099/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "regression_2099" +authors = [""] +compiler_version = "0.9.0" + +[dependencies] \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data/regression_2099/src/main.nr b/crates/nargo_cli/tests/test_data/regression_2099/src/main.nr new file mode 100644 index 00000000000..b96e664dedf --- /dev/null +++ b/crates/nargo_cli/tests/test_data/regression_2099/src/main.nr @@ -0,0 +1,37 @@ +use dep::std::ec::tecurve::affine::Curve as AffineCurve; +use dep::std::ec::tecurve::affine::Point as Gaffine; +use dep::std::ec::tecurve::curvegroup::Curve; +use dep::std::ec::tecurve::curvegroup::Point as G; + +use dep::std::ec::swcurve::affine::Point as SWGaffine; +use dep::std::ec::swcurve::curvegroup::Point as SWG; + +use dep::std::ec::montcurve::affine::Point as MGaffine; +use dep::std::ec::montcurve::curvegroup::Point as MG; + +fn main() { + // Define Baby Jubjub (ERC-2494) parameters in affine representation + let bjj_affine = AffineCurve::new(168700, 168696, Gaffine::new(995203441582195749578291179787384436505546430278305826713579947235728471134,5472060717959818805561601436314318772137091100104008585924551046643952123905)); + + // Test addition + let p1_affine = Gaffine::new(17777552123799933955779906779655732241715742912184938656739573121738514868268, 2626589144620713026669568689430873010625803728049924121243784502389097019475); + let p2_affine = Gaffine::new(16540640123574156134436876038791482806971768689494387082833631921987005038935, 20819045374670962167435360035096875258406992893633759881276124905556507972311); + let _p3_affine = bjj_affine.add(p1_affine, p2_affine); + + // Test SWCurve equivalents of the above + // First the affine representation + let bjj_swcurve_affine = bjj_affine.into_swcurve(); + + let p1_swcurve_affine = bjj_affine.map_into_swcurve(p1_affine); + let p2_swcurve_affine = bjj_affine.map_into_swcurve(p2_affine); + + let _p3_swcurve_affine_from_add = bjj_swcurve_affine.add( + p1_swcurve_affine, + p2_swcurve_affine + ); + + // Check that these points are on the curve + assert( + bjj_swcurve_affine.contains(p1_swcurve_affine) + ); +} diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/function_inserter.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/function_inserter.rs index 38dcfbbb168..15c755f40c2 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/function_inserter.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/function_inserter.rs @@ -124,7 +124,6 @@ impl<'f> FunctionInserter<'f> { let old_parameters = self.function.dfg.block_parameters(block); for (param, new_param) in old_parameters.iter().zip(new_values) { - // Don't overwrite any existing entries to avoid overwriting the induction variable self.values.entry(*param).or_insert(*new_param); } } diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs b/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs index 4ff857f942f..fdc4be085d7 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs @@ -274,7 +274,10 @@ impl<'f> Context<'f> { // end, in addition to resetting the value of old_condition since it is set to // known to be true/false within the then/else branch respectively. self.insert_current_side_effects_enabled(); - self.inserter.map_value(old_condition, old_condition); + + // We must map back to `then_condition` here. Mapping `old_condition` to itself would + // lose any previous mappings. + self.inserter.map_value(old_condition, then_condition); // While there is a condition on the stack we don't compile outside the condition // until it is popped. This ensures we inline the full then and else branches From 50b2816099a021e4b8cb44a9017fb849abf014e6 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 2 Aug 2023 14:20:28 +0100 Subject: [PATCH 03/11] feat: Add additional `BinaryOp` simplifications (#2124) feat: add additional `BinaryOp` simplifictions --- .../src/ssa_refactor/ir/instruction.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs index b7a3ea02ae9..6d74a99e002 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs @@ -733,6 +733,9 @@ impl Binary { let zero = dfg.make_constant(FieldElement::zero(), operand_type); return SimplifyResult::SimplifiedTo(zero); } + if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) { + return SimplifyResult::SimplifiedTo(self.lhs); + } } BinaryOp::Or => { if lhs_is_zero { @@ -741,8 +744,17 @@ impl Binary { if rhs_is_zero { return SimplifyResult::SimplifiedTo(self.lhs); } + if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) { + return SimplifyResult::SimplifiedTo(self.lhs); + } } BinaryOp::Xor => { + if lhs_is_zero { + return SimplifyResult::SimplifiedTo(self.rhs); + } + if rhs_is_zero { + return SimplifyResult::SimplifiedTo(self.lhs); + } if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) { let zero = dfg.make_constant(FieldElement::zero(), Type::bool()); return SimplifyResult::SimplifiedTo(zero); From b0fbc536dc432ba8d3ab6c12462758b11c2c21c4 Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:52:35 +0200 Subject: [PATCH 04/11] feat: Add support for bitshifts by distances known at runtime (#2072) * remove shr and shl from ssa instruction * move bit_shift_runtime test to test_data * code review, fix typo * Forbid signed integers for bit shift and fix brillig failing test * Check for signeness also during the delayed checks * Add missing method * Code review * Code review --- .../test_data/bit_shifts_runtime/Nargo.toml | 6 ++ .../test_data/bit_shifts_runtime/Prover.toml | 2 + .../test_data/bit_shifts_runtime/src/main.nr | 9 +++ .../src/brillig/brillig_gen/brillig_block.rs | 24 +++--- .../src/brillig/brillig_gen/brillig_fn.rs | 7 -- .../noirc_evaluator/src/brillig/brillig_ir.rs | 12 +++ .../src/brillig/brillig_ir/debug_show.rs | 5 +- .../src/ssa_refactor/acir_gen/mod.rs | 7 -- .../src/ssa_refactor/ir/instruction.rs | 20 ----- .../src/ssa_refactor/ssa_gen/context.rs | 77 +++++++++++++++---- crates/noirc_frontend/src/ast/expression.rs | 4 + .../noirc_frontend/src/hir/type_check/expr.rs | 20 ++++- crates/noirc_frontend/src/hir_def/expr.rs | 4 + crates/noirc_frontend/src/hir_def/types.rs | 4 + 14 files changed, 132 insertions(+), 69 deletions(-) create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts_runtime/Nargo.toml create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts_runtime/Prover.toml create mode 100644 crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Nargo.toml b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Nargo.toml new file mode 100644 index 00000000000..661f4f937d5 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "bit_shifts_runtime" +authors = [""] +compiler_version = "0.1" + +[dependencies] diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Prover.toml b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Prover.toml new file mode 100644 index 00000000000..98d8630792e --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/Prover.toml @@ -0,0 +1,2 @@ +x = 64 +y = 1 \ No newline at end of file diff --git a/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr new file mode 100644 index 00000000000..271a1ecb880 --- /dev/null +++ b/crates/nargo_cli/tests/test_data/bit_shifts_runtime/src/main.nr @@ -0,0 +1,9 @@ +fn main(x: u64, y: u64) { + // runtime shifts on comptime values + assert(64 << y == 128); + assert(64 >> y == 32); + + // runtime shifts on runtime values + assert(x << y == 128); + assert(x >> y == 32); +} \ No newline at end of file diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs index c7779533a8a..a9bbe189e57 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs @@ -336,10 +336,10 @@ impl<'block> BrilligBlock<'block> { dfg.instruction_results(instruction_id)[0], dfg, ); - + let heap_vec = self.brillig_context.extract_heap_vector(target_slice); self.brillig_context.radix_instruction( source, - self.function_context.extract_heap_vector(target_slice), + heap_vec, radix, limb_count, matches!(endianness, Endian::Big), @@ -355,10 +355,10 @@ impl<'block> BrilligBlock<'block> { ); let radix = self.brillig_context.make_constant(2_usize.into()); - + let heap_vec = self.brillig_context.extract_heap_vector(target_slice); self.brillig_context.radix_instruction( source, - self.function_context.extract_heap_vector(target_slice), + heap_vec, radix, limb_count, matches!(endianness, Endian::Big), @@ -589,7 +589,7 @@ impl<'block> BrilligBlock<'block> { dfg.instruction_results(instruction_id)[0], dfg, ); - let target_vector = self.function_context.extract_heap_vector(target_variable); + let target_vector = self.brillig_context.extract_heap_vector(target_variable); let item_value = self.convert_ssa_register_value(arguments[1], dfg); slice_push_back_operation( self.brillig_context, @@ -604,7 +604,7 @@ impl<'block> BrilligBlock<'block> { dfg.instruction_results(instruction_id)[0], dfg, ); - let target_vector = self.function_context.extract_heap_vector(target_variable); + let target_vector = self.brillig_context.extract_heap_vector(target_variable); let item_value = self.convert_ssa_register_value(arguments[1], dfg); slice_push_front_operation( self.brillig_context, @@ -618,7 +618,7 @@ impl<'block> BrilligBlock<'block> { let target_variable = self.function_context.create_variable(self.brillig_context, results[0], dfg); - let target_vector = self.function_context.extract_heap_vector(target_variable); + let target_vector = self.brillig_context.extract_heap_vector(target_variable); let pop_item = self.function_context.create_register_variable( self.brillig_context, @@ -643,7 +643,7 @@ impl<'block> BrilligBlock<'block> { ); let target_variable = self.function_context.create_variable(self.brillig_context, results[1], dfg); - let target_vector = self.function_context.extract_heap_vector(target_variable); + let target_vector = self.brillig_context.extract_heap_vector(target_variable); slice_pop_front_operation( self.brillig_context, @@ -659,7 +659,7 @@ impl<'block> BrilligBlock<'block> { let target_variable = self.function_context.create_variable(self.brillig_context, results[0], dfg); - let target_vector = self.function_context.extract_heap_vector(target_variable); + let target_vector = self.brillig_context.extract_heap_vector(target_variable); slice_insert_operation( self.brillig_context, target_vector, @@ -674,7 +674,7 @@ impl<'block> BrilligBlock<'block> { let target_variable = self.function_context.create_variable(self.brillig_context, results[0], dfg); - let target_vector = self.function_context.extract_heap_vector(target_variable); + let target_vector = self.brillig_context.extract_heap_vector(target_variable); let removed_item_register = self.function_context.create_register_variable( self.brillig_context, @@ -877,7 +877,7 @@ impl<'block> BrilligBlock<'block> { Type::Slice(_) => { let variable = self.function_context.create_variable(self.brillig_context, result, dfg); - let vector = self.function_context.extract_heap_vector(variable); + let vector = self.brillig_context.extract_heap_vector(variable); // Set the pointer to the current stack frame // The stack pointer will then be update by the caller of this method @@ -981,8 +981,6 @@ pub(crate) fn convert_ssa_binary_op_to_brillig_binary_op( BinaryOp::And => BinaryIntOp::And, BinaryOp::Or => BinaryIntOp::Or, BinaryOp::Xor => BinaryIntOp::Xor, - BinaryOp::Shl => BinaryIntOp::Shl, - BinaryOp::Shr => BinaryIntOp::Shr, }; BrilligBinaryOp::Integer { op: operation, bit_size } diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs index 1a751d28b23..210d6da7be6 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs @@ -115,13 +115,6 @@ impl FunctionContext { } } - pub(crate) fn extract_heap_vector(&self, variable: RegisterOrMemory) -> HeapVector { - match variable { - RegisterOrMemory::HeapVector(vector) => vector, - _ => unreachable!("ICE: Expected vector, got {variable:?}"), - } - } - /// Collects the registers that a given variable is stored in. pub(crate) fn extract_registers(&self, variable: RegisterOrMemory) -> Vec { match variable { diff --git a/crates/noirc_evaluator/src/brillig/brillig_ir.rs b/crates/noirc_evaluator/src/brillig/brillig_ir.rs index ac0103dd9ed..4471d507579 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_ir.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_ir.rs @@ -951,6 +951,18 @@ impl BrilligContext { self.deallocate_register(end_value_register); self.deallocate_register(index_at_end_of_array); } + + pub(crate) fn extract_heap_vector(&mut self, variable: RegisterOrMemory) -> HeapVector { + match variable { + RegisterOrMemory::HeapVector(vector) => vector, + RegisterOrMemory::HeapArray(array) => { + let size = self.allocate_register(); + self.const_instruction(size, array.size.into()); + HeapVector { pointer: array.pointer, size } + } + _ => unreachable!("ICE: Expected vector, got {variable:?}"), + } + } } /// Type to encapsulate the binary operation types in Brillig diff --git a/crates/noirc_evaluator/src/brillig/brillig_ir/debug_show.rs b/crates/noirc_evaluator/src/brillig/brillig_ir/debug_show.rs index 75716e49177..2bb753de760 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_ir/debug_show.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_ir/debug_show.rs @@ -73,8 +73,9 @@ impl DebugToString for BinaryIntOp { BinaryIntOp::And => "&&".into(), BinaryIntOp::Or => "||".into(), BinaryIntOp::Xor => "^".into(), - BinaryIntOp::Shl => "<<".into(), - BinaryIntOp::Shr => ">>".into(), + BinaryIntOp::Shl | BinaryIntOp::Shr => { + unreachable!("bit shift should have been replaced") + } } } } diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs index 4a7d2e46775..f00f15d8f05 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs @@ -796,13 +796,6 @@ impl Context { bit_count, self.current_side_effects_enabled_var, ), - BinaryOp::Shl => self.acir_context.shift_left_var(lhs, rhs, binary_type), - BinaryOp::Shr => self.acir_context.shift_right_var( - lhs, - rhs, - binary_type, - self.current_side_effects_enabled_var, - ), BinaryOp::Xor => self.acir_context.xor_var(lhs, rhs, binary_type), BinaryOp::And => self.acir_context.and_var(lhs, rhs, binary_type), BinaryOp::Or => self.acir_context.or_var(lhs, rhs, binary_type), diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs index 6d74a99e002..a56b12ab875 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs @@ -760,16 +760,6 @@ impl Binary { return SimplifyResult::SimplifiedTo(zero); } } - BinaryOp::Shl => { - if rhs_is_zero { - return SimplifyResult::SimplifiedTo(self.lhs); - } - } - BinaryOp::Shr => { - if rhs_is_zero { - return SimplifyResult::SimplifiedTo(self.lhs); - } - } } SimplifyResult::None } @@ -825,8 +815,6 @@ impl BinaryOp { BinaryOp::And => None, BinaryOp::Or => None, BinaryOp::Xor => None, - BinaryOp::Shl => None, - BinaryOp::Shr => None, } } @@ -840,8 +828,6 @@ impl BinaryOp { BinaryOp::And => |x, y| Some(x & y), BinaryOp::Or => |x, y| Some(x | y), BinaryOp::Xor => |x, y| Some(x ^ y), - BinaryOp::Shl => |x, y| x.checked_shl(y.try_into().ok()?), - BinaryOp::Shr => |x, y| Some(x >> y), BinaryOp::Eq => |x, y| Some((x == y) as u128), BinaryOp::Lt => |x, y| Some((x < y) as u128), } @@ -882,10 +868,6 @@ pub(crate) enum BinaryOp { Or, /// Bitwise xor (^) Xor, - /// Shift lhs left by rhs bits (<<) - Shl, - /// Shift lhs right by rhs bits (>>) - Shr, } impl std::fmt::Display for BinaryOp { @@ -901,8 +883,6 @@ impl std::fmt::Display for BinaryOp { BinaryOp::And => write!(f, "and"), BinaryOp::Or => write!(f, "or"), BinaryOp::Xor => write!(f, "xor"), - BinaryOp::Shl => write!(f, "shl"), - BinaryOp::Shr => write!(f, "shr"), } } } diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs index c485200a53e..a526d93f85b 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs @@ -7,12 +7,12 @@ use iter_extended::vecmap; use noirc_errors::Location; use noirc_frontend::monomorphization::ast::{self, LocalId, Parameters}; use noirc_frontend::monomorphization::ast::{FuncId, Program}; -use noirc_frontend::Signedness; +use noirc_frontend::{BinaryOpKind, Signedness}; use crate::ssa_refactor::ir::dfg::DataFlowGraph; use crate::ssa_refactor::ir::function::FunctionId as IrFunctionId; use crate::ssa_refactor::ir::function::{Function, RuntimeType}; -use crate::ssa_refactor::ir::instruction::BinaryOp; +use crate::ssa_refactor::ir::instruction::{BinaryOp, Endian, Intrinsic}; use crate::ssa_refactor::ir::map::AtomicCounter; use crate::ssa_refactor::ir::types::{NumericType, Type}; use crate::ssa_refactor::ir::value::ValueId; @@ -236,6 +236,46 @@ impl<'a> FunctionContext<'a> { Values::empty() } + /// Insert ssa instructions which computes lhs << rhs by doing lhs*2^rhs + fn insert_shift_left(&mut self, lhs: ValueId, rhs: ValueId) -> ValueId { + let base = self.builder.field_constant(FieldElement::from(2_u128)); + let pow = self.pow(base, rhs); + self.builder.insert_binary(lhs, BinaryOp::Mul, pow) + } + + /// Insert ssa instructions which computes lhs << rhs by doing lhs/2^rhs + fn insert_shift_right(&mut self, lhs: ValueId, rhs: ValueId) -> ValueId { + let base = self.builder.field_constant(FieldElement::from(2_u128)); + let pow = self.pow(base, rhs); + self.builder.insert_binary(lhs, BinaryOp::Div, pow) + } + + /// Computes lhs^rhs via square&multiply, using the bits decomposition of rhs + fn pow(&mut self, lhs: ValueId, rhs: ValueId) -> ValueId { + let typ = self.builder.current_function.dfg.type_of_value(rhs); + if let Type::Numeric(NumericType::Unsigned { bit_size }) = typ { + let to_bits = self.builder.import_intrinsic_id(Intrinsic::ToBits(Endian::Little)); + let length = self.builder.field_constant(FieldElement::from(bit_size as i128)); + let result_types = vec![Type::Array(Rc::new(vec![Type::bool()]), bit_size as usize)]; + let rhs_bits = self.builder.insert_call(to_bits, vec![rhs, length], result_types)[0]; + let one = self.builder.field_constant(FieldElement::one()); + let mut r = one; + for i in 1..bit_size + 1 { + let r1 = self.builder.insert_binary(r, BinaryOp::Mul, r); + let a = self.builder.insert_binary(r1, BinaryOp::Mul, lhs); + let idx = self.builder.field_constant(FieldElement::from((bit_size - i) as i128)); + let b = self.builder.insert_array_get(rhs_bits, idx, Type::field()); + let r2 = self.builder.insert_binary(a, BinaryOp::Mul, b); + let c = self.builder.insert_binary(one, BinaryOp::Sub, b); + let r3 = self.builder.insert_binary(c, BinaryOp::Mul, r1); + r = self.builder.insert_binary(r2, BinaryOp::Add, r3); + } + r + } else { + unreachable!("Value must be unsigned in power operation"); + } + } + /// Insert a binary instruction at the end of the current block. /// Converts the form of the binary instruction as necessary /// (e.g. swapping arguments, inserting a not) to represent it in the IR. @@ -247,17 +287,22 @@ impl<'a> FunctionContext<'a> { mut rhs: ValueId, location: Location, ) -> Values { - let op = convert_operator(operator); - - if op == BinaryOp::Eq && matches!(self.builder.type_of_value(lhs), Type::Array(..)) { - return self.insert_array_equality(lhs, operator, rhs, location); - } - - if operator_requires_swapped_operands(operator) { - std::mem::swap(&mut lhs, &mut rhs); - } - - let mut result = self.builder.set_location(location).insert_binary(lhs, op, rhs); + let mut result = match operator { + BinaryOpKind::ShiftLeft => self.insert_shift_left(lhs, rhs), + BinaryOpKind::ShiftRight => self.insert_shift_right(lhs, rhs), + BinaryOpKind::Equal | BinaryOpKind::NotEqual + if matches!(self.builder.type_of_value(lhs), Type::Array(..)) => + { + return self.insert_array_equality(lhs, operator, rhs, location) + } + _ => { + let op = convert_operator(operator); + if operator_requires_swapped_operands(operator) { + std::mem::swap(&mut lhs, &mut rhs); + } + self.builder.set_location(location).insert_binary(lhs, op, rhs) + } + }; if let Some(max_bit_size) = operator_result_max_bit_size_to_truncate( operator, @@ -704,7 +749,6 @@ fn operator_result_max_bit_size_to_truncate( /// checking operator_requires_not and operator_requires_swapped_operands /// to represent the full operation correctly. fn convert_operator(op: noirc_frontend::BinaryOpKind) -> BinaryOp { - use noirc_frontend::BinaryOpKind; match op { BinaryOpKind::Add => BinaryOp::Add, BinaryOpKind::Subtract => BinaryOp::Sub, @@ -720,8 +764,9 @@ fn convert_operator(op: noirc_frontend::BinaryOpKind) -> BinaryOp { BinaryOpKind::And => BinaryOp::And, BinaryOpKind::Or => BinaryOp::Or, BinaryOpKind::Xor => BinaryOp::Xor, - BinaryOpKind::ShiftRight => BinaryOp::Shr, - BinaryOpKind::ShiftLeft => BinaryOp::Shl, + BinaryOpKind::ShiftRight | BinaryOpKind::ShiftLeft => unreachable!( + "ICE - bit shift operators do not exist in SSA and should have been replaced" + ), } } diff --git a/crates/noirc_frontend/src/ast/expression.rs b/crates/noirc_frontend/src/ast/expression.rs index b1829e8c1ee..b1170ff0ed0 100644 --- a/crates/noirc_frontend/src/ast/expression.rs +++ b/crates/noirc_frontend/src/ast/expression.rs @@ -268,6 +268,10 @@ impl BinaryOpKind { BinaryOpKind::Modulo => Token::Percent, } } + + pub fn is_bit_shift(&self) -> bool { + matches!(self, BinaryOpKind::ShiftRight | BinaryOpKind::ShiftLeft) + } } #[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, Clone)] diff --git a/crates/noirc_frontend/src/hir/type_check/expr.rs b/crates/noirc_frontend/src/hir/type_check/expr.rs index 12c11bf20e1..24ac5f3443e 100644 --- a/crates/noirc_frontend/src/hir/type_check/expr.rs +++ b/crates/noirc_frontend/src/hir/type_check/expr.rs @@ -12,7 +12,7 @@ use crate::{ }, node_interner::{DefinitionKind, ExprId, FuncId}, token::Attribute::Deprecated, - CompTime, Shared, TypeBinding, TypeVariableKind, UnaryOp, + CompTime, Shared, Signedness, TypeBinding, TypeVariableKind, UnaryOp, }; use super::{errors::TypeCheckError, TypeChecker}; @@ -954,7 +954,7 @@ impl<'interner> TypeChecker<'interner> { if op.is_bitwise() && (other.is_bindable() || other.is_field()) { let other = other.follow_bindings(); - + let kind = op.kind; // This will be an error if these types later resolve to a Field, or stay // polymorphic as the bit size will be unknown. Delay this error until the function // finishes resolving so we can still allow cases like `let x: u8 = 1 << 2;`. @@ -963,6 +963,12 @@ impl<'interner> TypeChecker<'interner> { Err(TypeCheckError::InvalidBitwiseOperationOnField { span }) } else if other.is_bindable() { Err(TypeCheckError::AmbiguousBitWidth { span }) + } else if kind.is_bit_shift() && other.is_signed() { + Err(TypeCheckError::TypeCannotBeUsed { + typ: other, + place: "bit shift", + span, + }) } else { Ok(()) } @@ -1001,8 +1007,14 @@ impl<'interner> TypeChecker<'interner> { span, }); } - let comptime = comptime_x.and(comptime_y, op.location.span); - Ok(Integer(comptime, *sign_x, *bit_width_x)) + if op.is_bit_shift() + && (*sign_x == Signedness::Signed || *sign_y == Signedness::Signed) + { + Err(TypeCheckError::InvalidInfixOp { kind: "Signed integer", span }) + } else { + let comptime = comptime_x.and(comptime_y, op.location.span); + Ok(Integer(comptime, *sign_x, *bit_width_x)) + } } (Integer(..), FieldElement(..)) | (FieldElement(..), Integer(..)) => { Err(TypeCheckError::IntegerAndFieldBinaryOperation { span }) diff --git a/crates/noirc_frontend/src/hir_def/expr.rs b/crates/noirc_frontend/src/hir_def/expr.rs index 5db9751591a..db7db0a803d 100644 --- a/crates/noirc_frontend/src/hir_def/expr.rs +++ b/crates/noirc_frontend/src/hir_def/expr.rs @@ -72,6 +72,10 @@ impl HirBinaryOp { use BinaryOpKind::*; matches!(self.kind, And | Or | Xor | ShiftRight | ShiftLeft) } + + pub fn is_bit_shift(&self) -> bool { + self.kind.is_bit_shift() + } } #[derive(Debug, Clone)] diff --git a/crates/noirc_frontend/src/hir_def/types.rs b/crates/noirc_frontend/src/hir_def/types.rs index df4c2f6c229..ff0a4e53fae 100644 --- a/crates/noirc_frontend/src/hir_def/types.rs +++ b/crates/noirc_frontend/src/hir_def/types.rs @@ -659,6 +659,10 @@ impl Type { matches!(self.follow_bindings(), Type::FieldElement(_)) } + pub fn is_signed(&self) -> bool { + matches!(self.follow_bindings(), Type::Integer(_, Signedness::Signed, _)) + } + fn contains_numeric_typevar(&self, target_id: TypeVariableId) -> bool { // True if the given type is a NamedGeneric with the target_id let named_generic_id_matches_target = |typ: &Type| { From 292724fc8b4d3791a87a829ce8d87a1a537dfcc5 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:19:40 +0100 Subject: [PATCH 05/11] chore: create a `const` to hold the panic message (#2122) chore: create a const to hold the panic message --- crates/nargo_cli/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/nargo_cli/src/main.rs b/crates/nargo_cli/src/main.rs index a73785c64c6..a79c43dad48 100644 --- a/crates/nargo_cli/src/main.rs +++ b/crates/nargo_cli/src/main.rs @@ -3,12 +3,12 @@ use color_eyre::{config::HookBuilder, eyre}; use nargo_cli::cli::start_cli; +const PANIC_MESSAGE: &str = "This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.\nIf there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml"; + fn main() -> eyre::Result<()> { // Register a panic hook to display more readable panic messages to end-users - let (panic_hook, _) = HookBuilder::default() - .display_env_section(false) - .panic_section("This is a bug. We may have already fixed this in newer versions of Nargo so try searching for similar issues at https://github.com/noir-lang/noir/issues/.\nIf there isn't an open issue for this bug, consider opening one at https://github.com/noir-lang/noir/issues/new?labels=bug&template=bug_report.yml") - .into_hooks(); + let (panic_hook, _) = + HookBuilder::default().display_env_section(false).panic_section(PANIC_MESSAGE).into_hooks(); panic_hook.install(); start_cli() From 435ab3520d06b6b4f898d41a5ad403c5ddbd7771 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:51:04 +0100 Subject: [PATCH 06/11] feat: replace boolean `AND`s with multiplication (#1954) * feat: replace boolean `AND`s with multiplication * chore: move optimisation to live within ssa-gen * chore: fill out message in `unreachable` * chore: remove `SimplifyResult::None` * chore: abstract away `SimplifyResult::SimplifiedToInstruction(None)` * Revert "chore: abstract away `SimplifyResult::SimplifiedToInstruction(None)`" This reverts commit a7736eb418944864ff9a67b07aea01e7ba0bdb17. * Revert "chore: remove `SimplifyResult::None`" This reverts commit 429ccd473883ac3b210dda3eac59d780a0b45a2f. * chore: add `SimplifyResult.instruction()` --- .../noirc_evaluator/src/ssa_refactor/ir/dfg.rs | 3 ++- .../src/ssa_refactor/ir/instruction.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs index caf65c85a7e..6d74e49b03b 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs @@ -158,7 +158,8 @@ impl DataFlowGraph { SimplifiedToMultiple(simplification) } SimplifyResult::Remove => InstructionRemoved, - SimplifyResult::None => { + result @ (SimplifyResult::SimplifiedToInstruction(_) | SimplifyResult::None) => { + let instruction = result.instruction().unwrap_or(instruction); let id = self.make_instruction(instruction, ctrl_typevars); self.blocks[block].insert_instruction(id); if let Some(location) = location { diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs index a56b12ab875..afb47d423e2 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs @@ -736,6 +736,11 @@ impl Binary { if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) { return SimplifyResult::SimplifiedTo(self.lhs); } + if operand_type == Type::bool() { + // Boolean AND is equivalent to multiplication, which is a cheaper operation. + let instruction = Instruction::binary(BinaryOp::Mul, self.lhs, self.rhs); + return SimplifyResult::SimplifiedToInstruction(instruction); + } } BinaryOp::Or => { if lhs_is_zero { @@ -898,9 +903,21 @@ pub(crate) enum SimplifyResult { /// a function such as a tuple SimplifiedToMultiple(Vec), + /// Replace this function with an simpler but equivalent function. + SimplifiedToInstruction(Instruction), + /// Remove the instruction, it is unnecessary Remove, /// Instruction could not be simplified None, } + +impl SimplifyResult { + pub(crate) fn instruction(self) -> Option { + match self { + SimplifyResult::SimplifiedToInstruction(instruction) => Some(instruction), + _ => None, + } + } +} From 8a1ace792c4550ab1ce8c6044794abdb39d02872 Mon Sep 17 00:00:00 2001 From: jfecher Date: Wed, 2 Aug 2023 11:06:00 -0500 Subject: [PATCH 07/11] fix: Rename `Option::value` to `Option::_value` (#2127) * Rename Option::value to Option::_value * Add unwrap_unchecked method --- noir_stdlib/src/option.nr | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/noir_stdlib/src/option.nr b/noir_stdlib/src/option.nr index 5cc4dfae887..919c40fd9e0 100644 --- a/noir_stdlib/src/option.nr +++ b/noir_stdlib/src/option.nr @@ -1,17 +1,17 @@ struct Option { _is_some: bool, - value: T, + _value: T, } impl Option { /// Constructs a None value fn none() -> Self { - Self { _is_some: false, value: crate::unsafe::zeroed() } + Self { _is_some: false, _value: crate::unsafe::zeroed() } } /// Constructs a Some wrapper around the given value - fn some(value: T) -> Self { - Self { _is_some: true, value } + fn some(_value: T) -> Self { + Self { _is_some: true, _value } } /// True if this Option is None @@ -27,13 +27,20 @@ impl Option { /// Asserts `self.is_some()` and returns the wrapped value. fn unwrap(self) -> T { assert(self._is_some); - self.value + self._value + } + + /// Returns the inner value without asserting `self.is_some()` + /// Note that if `self` is `None`, there is no guarantee what value will be returned, + /// only that it will be of type `T`. + fn unwrap_unchecked(self) -> T { + self._value } /// Returns the wrapped value if `self.is_some()`. Otherwise, returns the given default value. fn unwrap_or(self, default: T) -> T { if self._is_some { - self.value + self._value } else { default } @@ -43,7 +50,7 @@ impl Option { /// a default value. fn unwrap_or_else(self, default: fn() -> T) -> T { if self._is_some { - self.value + self._value } else { default() } @@ -52,7 +59,7 @@ impl Option { /// If self is `Some(x)`, this returns `Some(f(x))`. Otherwise, this returns `None`. fn map(self, f: fn(T) -> U) -> Option { if self._is_some { - Option::some(f(self.value)) + Option::some(f(self._value)) } else { Option::none() } @@ -61,7 +68,7 @@ impl Option { /// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns the given default value. fn map_or(self, default: U, f: fn(T) -> U) -> U { if self._is_some { - f(self.value) + f(self._value) } else { default } @@ -70,7 +77,7 @@ impl Option { /// If self is `Some(x)`, this returns `f(x)`. Otherwise, this returns `default()`. fn map_or_else(self, default: fn() -> U, f: fn(T) -> U) -> U { if self._is_some { - f(self.value) + f(self._value) } else { default() } @@ -91,7 +98,7 @@ impl Option { /// In some languages this function is called `flat_map` or `bind`. fn and_then(self, f: fn(T) -> Option) -> Option { if self._is_some { - f(self.value) + f(self._value) } else { Option::none() } @@ -135,7 +142,7 @@ impl Option { /// Otherwise, this returns `None` fn filter(self, predicate: fn(T) -> bool) -> Self { if self._is_some { - if predicate(self.value) { + if predicate(self._value) { self } else { Option::none() @@ -149,7 +156,7 @@ impl Option { /// This returns None if the outer Option is None. Otherwise, this returns the inner Option. fn flatten(option: Option>) -> Option { if option._is_some { - option.value + option._value } else { Option::none() } From 47b372c1762ed1184bf2ed9b90d7dc3e2c161880 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 2 Aug 2023 17:17:29 +0100 Subject: [PATCH 08/11] feat: Optimize away constant calls to black box functions (#1981) * feat: optimize away constant calls to black box functions * chore: remove `use SimplifyResult::*` * chore: remove unnecessary match arms * Update crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs * Update crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs --------- Co-authored-by: jfecher --- .../src/ssa_refactor/ir/instruction.rs | 156 +------- .../src/ssa_refactor/ir/instruction/call.rs | 334 ++++++++++++++++++ 2 files changed, 338 insertions(+), 152 deletions(-) create mode 100644 crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs index afb47d423e2..7edb74f4206 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs @@ -1,5 +1,3 @@ -use std::rc::Rc; - use acvm::{acir::BlackBoxFunc, FieldElement}; use iter_extended::vecmap; use num_bigint::BigUint; @@ -14,6 +12,10 @@ use super::{ value::{Value, ValueId}, }; +mod call; + +use call::simplify_call; + /// Reference to an instruction /// /// Note that InstructionIds are not unique. That is, two InstructionIds @@ -385,156 +387,6 @@ fn simplify_cast(value: ValueId, dst_typ: &Type, dfg: &mut DataFlowGraph) -> Sim } } -/// Try to simplify this call instruction. If the instruction can be simplified to a known value, -/// that value is returned. Otherwise None is returned. -fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) -> SimplifyResult { - use SimplifyResult::*; - let intrinsic = match &dfg[func] { - Value::Intrinsic(intrinsic) => *intrinsic, - _ => return None, - }; - - let constant_args: Option> = - arguments.iter().map(|value_id| dfg.get_numeric_constant(*value_id)).collect(); - - match intrinsic { - Intrinsic::ToBits(endian) => { - if let Some(constant_args) = constant_args { - let field = constant_args[0]; - let limb_count = constant_args[1].to_u128() as u32; - SimplifiedTo(constant_to_radix(endian, field, 2, limb_count, dfg)) - } else { - None - } - } - Intrinsic::ToRadix(endian) => { - if let Some(constant_args) = constant_args { - let field = constant_args[0]; - let radix = constant_args[1].to_u128() as u32; - let limb_count = constant_args[2].to_u128() as u32; - SimplifiedTo(constant_to_radix(endian, field, radix, limb_count, dfg)) - } else { - None - } - } - Intrinsic::ArrayLen => { - let slice = dfg.get_array_constant(arguments[0]); - if let Some((slice, _)) = slice { - SimplifiedTo(dfg.make_constant((slice.len() as u128).into(), Type::field())) - } else if let Some(length) = dfg.try_get_array_length(arguments[0]) { - SimplifiedTo(dfg.make_constant((length as u128).into(), Type::field())) - } else { - None - } - } - Intrinsic::SlicePushBack => { - let slice = dfg.get_array_constant(arguments[0]); - if let (Some((mut slice, element_type)), elem) = (slice, arguments[1]) { - slice.push_back(elem); - let new_slice = dfg.make_array(slice, element_type); - SimplifiedTo(new_slice) - } else { - None - } - } - Intrinsic::SlicePushFront => { - let slice = dfg.get_array_constant(arguments[0]); - if let (Some((mut slice, element_type)), elem) = (slice, arguments[1]) { - slice.push_front(elem); - let new_slice = dfg.make_array(slice, element_type); - SimplifiedTo(new_slice) - } else { - None - } - } - Intrinsic::SlicePopBack => { - let slice = dfg.get_array_constant(arguments[0]); - if let Some((mut slice, element_type)) = slice { - let elem = - slice.pop_back().expect("There are no elements in this slice to be removed"); - let new_slice = dfg.make_array(slice, element_type); - SimplifiedToMultiple(vec![new_slice, elem]) - } else { - None - } - } - Intrinsic::SlicePopFront => { - let slice = dfg.get_array_constant(arguments[0]); - if let Some((mut slice, element_type)) = slice { - let elem = - slice.pop_front().expect("There are no elements in this slice to be removed"); - let new_slice = dfg.make_array(slice, element_type); - SimplifiedToMultiple(vec![elem, new_slice]) - } else { - None - } - } - Intrinsic::SliceInsert => { - let slice = dfg.get_array_constant(arguments[0]); - let index = dfg.get_numeric_constant(arguments[1]); - if let (Some((mut slice, element_type)), Some(index), value) = - (slice, index, arguments[2]) - { - slice.insert(index.to_u128() as usize, value); - let new_slice = dfg.make_array(slice, element_type); - SimplifiedTo(new_slice) - } else { - None - } - } - Intrinsic::SliceRemove => { - let slice = dfg.get_array_constant(arguments[0]); - let index = dfg.get_numeric_constant(arguments[1]); - if let (Some((mut slice, element_type)), Some(index)) = (slice, index) { - let removed_elem = slice.remove(index.to_u128() as usize); - let new_slice = dfg.make_array(slice, element_type); - SimplifiedToMultiple(vec![new_slice, removed_elem]) - } else { - None - } - } - Intrinsic::BlackBox(_) | Intrinsic::Println | Intrinsic::Sort => None, - } -} - -/// Returns a Value::Array of constants corresponding to the limbs of the radix decomposition. -fn constant_to_radix( - endian: Endian, - field: FieldElement, - radix: u32, - limb_count: u32, - dfg: &mut DataFlowGraph, -) -> ValueId { - let bit_size = u32::BITS - (radix - 1).leading_zeros(); - let radix_big = BigUint::from(radix); - assert_eq!(BigUint::from(2u128).pow(bit_size), radix_big, "ICE: Radix must be a power of 2"); - let big_integer = BigUint::from_bytes_be(&field.to_be_bytes()); - - // Decompose the integer into its radix digits in little endian form. - let decomposed_integer = big_integer.to_radix_le(radix); - let mut limbs = vecmap(0..limb_count, |i| match decomposed_integer.get(i as usize) { - Some(digit) => FieldElement::from_be_bytes_reduce(&[*digit]), - None => FieldElement::zero(), - }); - if endian == Endian::Big { - limbs.reverse(); - } - - // For legacy reasons (see #617) the to_radix interface supports 256 bits even though - // FieldElement::max_num_bits() is only 254 bits. Any limbs beyond the specified count - // become zero padding. - let max_decomposable_bits: u32 = 256; - let limb_count_with_padding = max_decomposable_bits / bit_size; - while limbs.len() < limb_count_with_padding as usize { - limbs.push(FieldElement::zero()); - } - let result_constants: im::Vector = - limbs.into_iter().map(|limb| dfg.make_constant(limb, Type::unsigned(bit_size))).collect(); - - let typ = Type::Array(Rc::new(vec![Type::unsigned(bit_size)]), result_constants.len()); - dfg.make_array(result_constants, typ) -} - /// The possible return values for Instruction::return_types pub(crate) enum InstructionResultType { /// The result type of this instruction matches that of this operand diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs new file mode 100644 index 00000000000..96998d92fcf --- /dev/null +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs @@ -0,0 +1,334 @@ +use std::rc::Rc; + +use acvm::{acir::BlackBoxFunc, BlackBoxResolutionError, FieldElement}; +use iter_extended::vecmap; +use num_bigint::BigUint; + +use crate::ssa_refactor::ir::{ + dfg::DataFlowGraph, + instruction::Intrinsic, + map::Id, + types::Type, + value::{Value, ValueId}, +}; + +use super::{Endian, SimplifyResult}; + +/// Try to simplify this call instruction. If the instruction can be simplified to a known value, +/// that value is returned. Otherwise None is returned. +pub(super) fn simplify_call( + func: ValueId, + arguments: &[ValueId], + dfg: &mut DataFlowGraph, +) -> SimplifyResult { + let intrinsic = match &dfg[func] { + Value::Intrinsic(intrinsic) => *intrinsic, + _ => return SimplifyResult::None, + }; + + let constant_args: Option> = + arguments.iter().map(|value_id| dfg.get_numeric_constant(*value_id)).collect(); + + match intrinsic { + Intrinsic::ToBits(endian) => { + if let Some(constant_args) = constant_args { + let field = constant_args[0]; + let limb_count = constant_args[1].to_u128() as u32; + SimplifyResult::SimplifiedTo(constant_to_radix(endian, field, 2, limb_count, dfg)) + } else { + SimplifyResult::None + } + } + Intrinsic::ToRadix(endian) => { + if let Some(constant_args) = constant_args { + let field = constant_args[0]; + let radix = constant_args[1].to_u128() as u32; + let limb_count = constant_args[2].to_u128() as u32; + SimplifyResult::SimplifiedTo(constant_to_radix( + endian, field, radix, limb_count, dfg, + )) + } else { + SimplifyResult::None + } + } + Intrinsic::ArrayLen => { + let slice = dfg.get_array_constant(arguments[0]); + if let Some((slice, _)) = slice { + SimplifyResult::SimplifiedTo( + dfg.make_constant((slice.len() as u128).into(), Type::field()), + ) + } else if let Some(length) = dfg.try_get_array_length(arguments[0]) { + SimplifyResult::SimplifiedTo( + dfg.make_constant((length as u128).into(), Type::field()), + ) + } else { + SimplifyResult::None + } + } + Intrinsic::SlicePushBack => { + let slice = dfg.get_array_constant(arguments[0]); + if let (Some((mut slice, element_type)), elem) = (slice, arguments[1]) { + slice.push_back(elem); + let new_slice = dfg.make_array(slice, element_type); + SimplifyResult::SimplifiedTo(new_slice) + } else { + SimplifyResult::None + } + } + Intrinsic::SlicePushFront => { + let slice = dfg.get_array_constant(arguments[0]); + if let (Some((mut slice, element_type)), elem) = (slice, arguments[1]) { + slice.push_front(elem); + let new_slice = dfg.make_array(slice, element_type); + SimplifyResult::SimplifiedTo(new_slice) + } else { + SimplifyResult::None + } + } + Intrinsic::SlicePopBack => { + let slice = dfg.get_array_constant(arguments[0]); + if let Some((mut slice, element_type)) = slice { + let elem = + slice.pop_back().expect("There are no elements in this slice to be removed"); + let new_slice = dfg.make_array(slice, element_type); + SimplifyResult::SimplifiedToMultiple(vec![new_slice, elem]) + } else { + SimplifyResult::None + } + } + Intrinsic::SlicePopFront => { + let slice = dfg.get_array_constant(arguments[0]); + if let Some((mut slice, element_type)) = slice { + let elem = + slice.pop_front().expect("There are no elements in this slice to be removed"); + let new_slice = dfg.make_array(slice, element_type); + SimplifyResult::SimplifiedToMultiple(vec![elem, new_slice]) + } else { + SimplifyResult::None + } + } + Intrinsic::SliceInsert => { + let slice = dfg.get_array_constant(arguments[0]); + let index = dfg.get_numeric_constant(arguments[1]); + if let (Some((mut slice, element_type)), Some(index), value) = + (slice, index, arguments[2]) + { + slice.insert(index.to_u128() as usize, value); + let new_slice = dfg.make_array(slice, element_type); + SimplifyResult::SimplifiedTo(new_slice) + } else { + SimplifyResult::None + } + } + Intrinsic::SliceRemove => { + let slice = dfg.get_array_constant(arguments[0]); + let index = dfg.get_numeric_constant(arguments[1]); + if let (Some((mut slice, element_type)), Some(index)) = (slice, index) { + let removed_elem = slice.remove(index.to_u128() as usize); + let new_slice = dfg.make_array(slice, element_type); + SimplifyResult::SimplifiedToMultiple(vec![new_slice, removed_elem]) + } else { + SimplifyResult::None + } + } + Intrinsic::BlackBox(bb_func) => simplify_black_box_func(bb_func, arguments, dfg), + Intrinsic::Println | Intrinsic::Sort => SimplifyResult::None, + } +} + +/// Try to simplify this black box call. If the call can be simplified to a known value, +/// that value is returned. Otherwise [`SimplifyResult::None`] is returned. +fn simplify_black_box_func( + bb_func: BlackBoxFunc, + arguments: &[ValueId], + dfg: &mut DataFlowGraph, +) -> SimplifyResult { + match bb_func { + BlackBoxFunc::SHA256 => simplify_hash(dfg, arguments, acvm::blackbox_solver::sha256), + BlackBoxFunc::Blake2s => simplify_hash(dfg, arguments, acvm::blackbox_solver::blake2s), + BlackBoxFunc::Keccak256 => { + match (dfg.get_array_constant(arguments[0]), dfg.get_numeric_constant(arguments[1])) { + (Some((input, _)), Some(num_bytes)) if array_is_constant(dfg, &input) => { + let input_bytes: Vec = to_u8_vec(dfg, input); + + let num_bytes = num_bytes.to_u128() as usize; + let truncated_input_bytes = &input_bytes[0..num_bytes]; + let hash = acvm::blackbox_solver::keccak256(truncated_input_bytes) + .expect("Rust solvable black box function should not fail"); + + let hash_values = + vecmap(hash, |byte| FieldElement::from_be_bytes_reduce(&[byte])); + + let result_array = make_constant_array(dfg, hash_values, Type::unsigned(8)); + SimplifyResult::SimplifiedTo(result_array) + } + _ => SimplifyResult::None, + } + } + BlackBoxFunc::HashToField128Security => match dfg.get_array_constant(arguments[0]) { + Some((input, _)) if array_is_constant(dfg, &input) => { + let input_bytes: Vec = to_u8_vec(dfg, input); + + let field = acvm::blackbox_solver::hash_to_field_128_security(&input_bytes) + .expect("Rust solvable black box function should not fail"); + + let field_constant = dfg.make_constant(field, Type::field()); + SimplifyResult::SimplifiedTo(field_constant) + } + _ => SimplifyResult::None, + }, + + BlackBoxFunc::EcdsaSecp256k1 => { + simplify_signature(dfg, arguments, acvm::blackbox_solver::ecdsa_secp256k1_verify) + } + BlackBoxFunc::EcdsaSecp256r1 => { + simplify_signature(dfg, arguments, acvm::blackbox_solver::ecdsa_secp256r1_verify) + } + + BlackBoxFunc::FixedBaseScalarMul | BlackBoxFunc::SchnorrVerify | BlackBoxFunc::Pedersen => { + // Currently unsolvable here as we rely on an implementation in the backend. + SimplifyResult::None + } + + BlackBoxFunc::RecursiveAggregation => SimplifyResult::None, + + BlackBoxFunc::AND => { + unreachable!("ICE: `BlackBoxFunc::AND` calls should be transformed into a `BinaryOp`") + } + BlackBoxFunc::XOR => { + unreachable!("ICE: `BlackBoxFunc::XOR` calls should be transformed into a `BinaryOp`") + } + BlackBoxFunc::RANGE => { + unreachable!( + "ICE: `BlackBoxFunc::RANGE` calls should be transformed into a `Instruction::Cast`" + ) + } + } +} + +fn make_constant_array(dfg: &mut DataFlowGraph, results: Vec, typ: Type) -> ValueId { + let result_constants = vecmap(results, |element| dfg.make_constant(element, typ.clone())); + + let typ = Type::Array(Rc::new(vec![typ]), result_constants.len()); + dfg.make_array(result_constants.into(), typ) +} + +/// Returns a Value::Array of constants corresponding to the limbs of the radix decomposition. +fn constant_to_radix( + endian: Endian, + field: FieldElement, + radix: u32, + limb_count: u32, + dfg: &mut DataFlowGraph, +) -> ValueId { + let bit_size = u32::BITS - (radix - 1).leading_zeros(); + let radix_big = BigUint::from(radix); + assert_eq!(BigUint::from(2u128).pow(bit_size), radix_big, "ICE: Radix must be a power of 2"); + let big_integer = BigUint::from_bytes_be(&field.to_be_bytes()); + + // Decompose the integer into its radix digits in little endian form. + let decomposed_integer = big_integer.to_radix_le(radix); + let mut limbs = vecmap(0..limb_count, |i| match decomposed_integer.get(i as usize) { + Some(digit) => FieldElement::from_be_bytes_reduce(&[*digit]), + None => FieldElement::zero(), + }); + if endian == Endian::Big { + limbs.reverse(); + } + + // For legacy reasons (see #617) the to_radix interface supports 256 bits even though + // FieldElement::max_num_bits() is only 254 bits. Any limbs beyond the specified count + // become zero padding. + let max_decomposable_bits: u32 = 256; + let limb_count_with_padding = max_decomposable_bits / bit_size; + while limbs.len() < limb_count_with_padding as usize { + limbs.push(FieldElement::zero()); + } + + make_constant_array(dfg, limbs, Type::unsigned(bit_size)) +} + +fn to_u8_vec(dfg: &DataFlowGraph, values: im::Vector>) -> Vec { + values + .iter() + .map(|id| { + let field = dfg + .get_numeric_constant(*id) + .expect("value id from array should point at constant"); + *field.to_be_bytes().last().unwrap() + }) + .collect() +} + +fn array_is_constant(dfg: &DataFlowGraph, values: &im::Vector>) -> bool { + values.iter().all(|value| dfg.get_numeric_constant(*value).is_some()) +} + +fn simplify_hash( + dfg: &mut DataFlowGraph, + arguments: &[ValueId], + hash_function: fn(&[u8]) -> Result<[u8; 32], BlackBoxResolutionError>, +) -> SimplifyResult { + match dfg.get_array_constant(arguments[0]) { + Some((input, _)) if array_is_constant(dfg, &input) => { + let input_bytes: Vec = to_u8_vec(dfg, input); + + let hash = hash_function(&input_bytes) + .expect("Rust solvable black box function should not fail"); + + let hash_values = vecmap(hash, |byte| FieldElement::from_be_bytes_reduce(&[byte])); + + let result_array = make_constant_array(dfg, hash_values, Type::unsigned(8)); + SimplifyResult::SimplifiedTo(result_array) + } + _ => SimplifyResult::None, + } +} + +type ECDSASignatureVerifier = fn( + hashed_msg: &[u8], + public_key_x: &[u8; 32], + public_key_y: &[u8; 32], + signature: &[u8; 64], +) -> Result; +fn simplify_signature( + dfg: &mut DataFlowGraph, + arguments: &[ValueId], + signature_verifier: ECDSASignatureVerifier, +) -> SimplifyResult { + match ( + dfg.get_array_constant(arguments[0]), + dfg.get_array_constant(arguments[1]), + dfg.get_array_constant(arguments[2]), + dfg.get_array_constant(arguments[3]), + ) { + ( + Some((public_key_x, _)), + Some((public_key_y, _)), + Some((signature, _)), + Some((hashed_message, _)), + ) if array_is_constant(dfg, &public_key_x) + && array_is_constant(dfg, &public_key_y) + && array_is_constant(dfg, &signature) + && array_is_constant(dfg, &hashed_message) => + { + let public_key_x: [u8; 32] = to_u8_vec(dfg, public_key_x) + .try_into() + .expect("ECDSA public key fields are 32 bytes"); + let public_key_y: [u8; 32] = to_u8_vec(dfg, public_key_y) + .try_into() + .expect("ECDSA public key fields are 32 bytes"); + let signature: [u8; 64] = + to_u8_vec(dfg, signature).try_into().expect("ECDSA signatures are 64 bytes"); + let hashed_message: Vec = to_u8_vec(dfg, hashed_message); + + let valid_signature = + signature_verifier(&hashed_message, &public_key_x, &public_key_y, &signature) + .expect("Rust solvable black box function should not fail"); + + let valid_signature = dfg.make_constant(valid_signature.into(), Type::bool()); + SimplifyResult::SimplifiedTo(valid_signature) + } + _ => SimplifyResult::None, + } +} From 1c21d0caf1e3b3a92266b4b8238f3e6e6c394d05 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 2 Aug 2023 17:21:35 +0100 Subject: [PATCH 09/11] fix(globals): Accurately filter literals for resolving globals (#2126) accurately filter literals for resolving globals --- .../tests/test_data/global_consts/src/main.nr | 7 +++++++ .../tests/test_data/strings/src/main.nr | 6 +++++- .../src/hir/def_collector/dc_crate.rs | 20 ++++++++++--------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crates/nargo_cli/tests/test_data/global_consts/src/main.nr b/crates/nargo_cli/tests/test_data/global_consts/src/main.nr index 9bcca2b8071..2ed6e4593dd 100644 --- a/crates/nargo_cli/tests/test_data/global_consts/src/main.nr +++ b/crates/nargo_cli/tests/test_data/global_consts/src/main.nr @@ -12,12 +12,19 @@ struct Dummy { y: [Field; foo::MAGIC_NUMBER] } +struct Test { + v: Field, +} +global VALS: [Test; 1] = [Test { v: 100 }]; +global NESTED = [VALS, VALS]; + fn main(a: [Field; M + N - N], b: [Field; 30 + N / 2], c : pub [Field; foo::MAGIC_NUMBER], d: [Field; foo::bar::N]) { let test_struct = Dummy { x: d, y: c }; for i in 0..foo::MAGIC_NUMBER { assert(c[i] == foo::MAGIC_NUMBER); assert(test_struct.y[i] == foo::MAGIC_NUMBER); + assert(test_struct.y[i] != NESTED[1][0].v); } assert(N != M); diff --git a/crates/nargo_cli/tests/test_data/strings/src/main.nr b/crates/nargo_cli/tests/test_data/strings/src/main.nr index bee2370201c..edf5fff55b4 100644 --- a/crates/nargo_cli/tests/test_data/strings/src/main.nr +++ b/crates/nargo_cli/tests/test_data/strings/src/main.nr @@ -1,10 +1,13 @@ use dep::std; +// Test global string literals +global HELLO_WORLD = "hello world"; + fn main(message : pub str<11>, y : Field, hex_as_string : str<4>, hex_as_field : Field) { let mut bad_message = "hello world"; assert(message == "hello world"); - bad_message = "helld world"; + assert(message == HELLO_WORLD); let x = 10; let z = x * 5; std::println(10); @@ -16,6 +19,7 @@ fn main(message : pub str<11>, y : Field, hex_as_string : str<4>, hex_as_field : assert(y == 5); // Change to y != 5 to see how the later print statements are not called std::println(array); + bad_message = "helld world"; std::println(bad_message); assert(message != bad_message); diff --git a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs index e974961a405..76fbea289be 100644 --- a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -13,7 +13,7 @@ use crate::hir::Context; use crate::node_interner::{FuncId, NodeInterner, StmtId, StructId, TypeAliasId}; use crate::{ ExpressionKind, Generics, Ident, LetStatement, NoirFunction, NoirStruct, NoirTypeAlias, - ParsedModule, Shared, Type, TypeBinding, UnresolvedGenerics, UnresolvedType, + ParsedModule, Shared, Type, TypeBinding, UnresolvedGenerics, UnresolvedType, Literal, }; use fm::FileId; use iter_extended::vecmap; @@ -161,10 +161,10 @@ impl DefCollector { // // Additionally, we must resolve integer globals before structs since structs may refer to // the values of integer globals as numeric generics. - let (integer_globals, other_globals) = - filter_integer_globals(def_collector.collected_globals); + let (literal_globals, other_globals) = + filter_literal_globals(def_collector.collected_globals); - let mut file_global_ids = resolve_globals(context, integer_globals, crate_id, errors); + let mut file_global_ids = resolve_globals(context, literal_globals, crate_id, errors); resolve_type_aliases(context, def_collector.collected_type_aliases, crate_id, errors); @@ -274,13 +274,15 @@ where } /// Separate the globals Vec into two. The first element in the tuple will be the -/// integer literal globals, and the second will be all other globals. -fn filter_integer_globals( +/// literal globals, except for arrays, and the second will be all other globals. +/// We exclude array literals as they can contain complex types +fn filter_literal_globals( globals: Vec, ) -> (Vec, Vec) { - globals - .into_iter() - .partition(|global| matches!(&global.stmt_def.expression.kind, ExpressionKind::Literal(_))) + globals.into_iter().partition(|global| match &global.stmt_def.expression.kind { + ExpressionKind::Literal(literal) => !matches!(literal, Literal::Array(_)), + _ => false, + }) } fn resolve_globals( From 27ab78f3e298e94202b8dcc9ea44075a185a78e7 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 2 Aug 2023 19:15:45 +0100 Subject: [PATCH 10/11] chore: Use `--show-output` flag on execution rather than compilation (#2116) * move show-output to occur on execute rather than compilation * remove assert(false) from test * fix compile err * report compile errors in tests * aupdate failing constraint test * change comment and link issue --- crates/nargo/src/ops/execute.rs | 3 +- crates/nargo/src/ops/foreign_calls.rs | 5 ++- crates/nargo_cli/src/cli/execute_cmd.rs | 2 +- crates/nargo_cli/src/cli/test_cmd.rs | 9 +++-- .../tests/test_data/strings/src/main.nr | 20 ++++++++-- crates/noirc_driver/src/lib.rs | 8 ++-- crates/noirc_evaluator/src/ssa_refactor.rs | 6 +-- .../acir_gen/acir_ir/acir_variable.rs | 13 ------- .../src/ssa_refactor/acir_gen/mod.rs | 37 +++++-------------- crates/wasm/src/compile.rs | 4 +- 10 files changed, 46 insertions(+), 61 deletions(-) diff --git a/crates/nargo/src/ops/execute.rs b/crates/nargo/src/ops/execute.rs index 13ea64ed261..2a126443468 100644 --- a/crates/nargo/src/ops/execute.rs +++ b/crates/nargo/src/ops/execute.rs @@ -10,6 +10,7 @@ pub fn execute_circuit( _backend: &B, circuit: Circuit, initial_witness: WitnessMap, + show_output: bool, ) -> Result { let mut acvm = ACVM::new(B::default(), circuit.opcodes, initial_witness); @@ -23,7 +24,7 @@ pub fn execute_circuit( } ACVMStatus::Failure(error) => return Err(error.into()), ACVMStatus::RequiresForeignCall(foreign_call) => { - let foreign_call_result = ForeignCall::execute(&foreign_call)?; + let foreign_call_result = ForeignCall::execute(&foreign_call, show_output)?; acvm.resolve_pending_foreign_call(foreign_call_result); } } diff --git a/crates/nargo/src/ops/foreign_calls.rs b/crates/nargo/src/ops/foreign_calls.rs index 2abc62b1032..4d2f5988e38 100644 --- a/crates/nargo/src/ops/foreign_calls.rs +++ b/crates/nargo/src/ops/foreign_calls.rs @@ -42,11 +42,14 @@ impl ForeignCall { pub(crate) fn execute( foreign_call: &ForeignCallWaitInfo, + show_output: bool, ) -> Result { let foreign_call_name = foreign_call.function.as_str(); match Self::lookup(foreign_call_name) { Some(ForeignCall::Println) => { - Self::execute_println(&foreign_call.inputs)?; + if show_output { + Self::execute_println(&foreign_call.inputs)?; + } Ok(ForeignCallResult { values: vec![] }) } Some(ForeignCall::Sequence) => { diff --git a/crates/nargo_cli/src/cli/execute_cmd.rs b/crates/nargo_cli/src/cli/execute_cmd.rs index ca5c18585ab..a2700caee0f 100644 --- a/crates/nargo_cli/src/cli/execute_cmd.rs +++ b/crates/nargo_cli/src/cli/execute_cmd.rs @@ -132,7 +132,7 @@ pub(crate) fn execute_program( debug_data: Option<(DebugInfo, Context)>, ) -> Result> { let initial_witness = abi.encode(inputs_map, None)?; - let solved_witness_err = nargo::ops::execute_circuit(backend, circuit, initial_witness); + let solved_witness_err = nargo::ops::execute_circuit(backend, circuit, initial_witness, true); match solved_witness_err { Ok(solved_witness) => Ok(solved_witness), Err(err) => { diff --git a/crates/nargo_cli/src/cli/test_cmd.rs b/crates/nargo_cli/src/cli/test_cmd.rs index 7eb1c9bff74..e52e3e5aa8d 100644 --- a/crates/nargo_cli/src/cli/test_cmd.rs +++ b/crates/nargo_cli/src/cli/test_cmd.rs @@ -106,14 +106,17 @@ fn run_test( show_output: bool, config: &CompileOptions, ) -> Result<(), CliError> { - let mut program = compile_no_check(context, show_output, config, main) - .map_err(|_| CliError::Generic(format!("Test '{test_name}' failed to compile")))?; + let mut program = compile_no_check(context, config, main).map_err(|err| { + noirc_errors::reporter::report_all(&context.file_manager, &[err], config.deny_warnings); + CliError::Generic(format!("Test '{test_name}' failed to compile")) + })?; + // Note: We could perform this test using the unoptimized ACIR as generated by `compile_no_check`. program.circuit = optimize_circuit(backend, program.circuit).unwrap().0; // Run the backend to ensure the PWG evaluates functions like std::hash::pedersen, // otherwise constraints involving these expressions will not error. - match execute_circuit(backend, program.circuit, WitnessMap::new()) { + match execute_circuit(backend, program.circuit, WitnessMap::new(), show_output) { Ok(_) => Ok(()), Err(error) => { let writer = StandardStream::stderr(ColorChoice::Always); diff --git a/crates/nargo_cli/tests/test_data/strings/src/main.nr b/crates/nargo_cli/tests/test_data/strings/src/main.nr index edf5fff55b4..9f122c3a137 100644 --- a/crates/nargo_cli/tests/test_data/strings/src/main.nr +++ b/crates/nargo_cli/tests/test_data/strings/src/main.nr @@ -43,9 +43,8 @@ fn test_prints_strings() { fn test_prints_array() { let array = [1, 2, 3, 5, 8]; - // TODO: Printing structs currently not supported - // let s = Test { a: 1, b: 2, c: [3, 4] }; - // std::println(s); + let s = Test { a: 1, b: 2, c: [3, 4] }; + std::println(s); std::println(array); @@ -53,6 +52,21 @@ fn test_prints_array() { std::println(hash); } +fn failed_constraint(hex_as_field: Field) { + // TODO(#2116): Note that `println` will not work if a failed constraint can be + // evaluated at compile time. + // When this method is called from a test method or with constant values + // a `Failed constraint` compile error will be caught before this `println` + // is executed as the input will be a constant. + std::println(hex_as_field); + assert(hex_as_field != 0x41); +} + +#[test] +fn test_failed_constraint() { + failed_constraint(0x41); +} + struct Test { a: Field, b: Field, diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 4d1b7fe2675..27109af6a2f 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -163,7 +163,7 @@ pub fn compile_main( } }; - let compiled_program = compile_no_check(context, true, options, main)?; + let compiled_program = compile_no_check(context, options, main)?; if options.print_acir { println!("Compiled ACIR for main (unoptimized):"); @@ -230,7 +230,7 @@ fn compile_contract( let mut errs = Vec::new(); for function_id in &contract.functions { let name = context.function_name(function_id).to_owned(); - let function = match compile_no_check(context, true, options, *function_id) { + let function = match compile_no_check(context, options, *function_id) { Ok(function) => function, Err(err) => { errs.push(err); @@ -267,14 +267,12 @@ fn compile_contract( #[allow(deprecated)] pub fn compile_no_check( context: &Context, - show_output: bool, options: &CompileOptions, main_function: FuncId, ) -> Result { let program = monomorphize(main_function, &context.def_interner); - let (circuit, debug, abi) = - create_circuit(program, options.show_ssa, options.show_brillig, show_output)?; + let (circuit, debug, abi) = create_circuit(program, options.show_ssa, options.show_brillig)?; Ok(CompiledProgram { circuit, debug, abi }) } diff --git a/crates/noirc_evaluator/src/ssa_refactor.rs b/crates/noirc_evaluator/src/ssa_refactor.rs index 6326b45554d..c57bb330b09 100644 --- a/crates/noirc_evaluator/src/ssa_refactor.rs +++ b/crates/noirc_evaluator/src/ssa_refactor.rs @@ -35,7 +35,6 @@ pub mod ssa_gen; /// convert the final SSA into ACIR and return it. pub(crate) fn optimize_into_acir( program: Program, - allow_log_ops: bool, print_ssa_passes: bool, print_brillig_trace: bool, ) -> Result { @@ -63,7 +62,7 @@ pub(crate) fn optimize_into_acir( .dead_instruction_elimination() .print(print_ssa_passes, "After Dead Instruction Elimination:"); } - ssa.into_acir(brillig, abi_distinctness, allow_log_ops) + ssa.into_acir(brillig, abi_distinctness) } /// Compiles the Program into ACIR and applies optimizations to the arithmetic gates @@ -74,7 +73,6 @@ pub fn create_circuit( program: Program, enable_ssa_logging: bool, enable_brillig_logging: bool, - show_output: bool, ) -> Result<(Circuit, DebugInfo, Abi), RuntimeError> { let func_sig = program.main_function_signature.clone(); let GeneratedAcir { @@ -84,7 +82,7 @@ pub fn create_circuit( locations, input_witnesses, .. - } = optimize_into_acir(program, show_output, enable_ssa_logging, enable_brillig_logging)?; + } = optimize_into_acir(program, enable_ssa_logging, enable_brillig_logging)?; let abi = gen_abi(func_sig, &input_witnesses, return_witnesses.clone()); let public_abi = abi.clone().public_abi(); diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs index 9177dc9ae6c..d1479ef1f1b 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs @@ -827,19 +827,6 @@ impl AcirContext { self.radix_decompose(endian, input_var, two_var, limb_count_var, result_element_type) } - /// Prints the given `AcirVar`s as witnesses. - pub(crate) fn print(&mut self, input: Vec) -> Result<(), RuntimeError> { - let input = Self::flatten_values(input); - - let witnesses = vecmap(input, |acir_var| { - let var_data = &self.vars[&acir_var]; - let expr = var_data.to_expression(); - self.acir_ir.get_or_create_witness(&expr) - }); - self.acir_ir.call_print(witnesses); - Ok(()) - } - /// Flatten the given Vector of AcirValues into a single vector of only variables. /// Each AcirValue::Array in the vector is recursively flattened, so each element /// will flattened into the resulting Vec. E.g. flatten_values([1, [2, 3]) == [1, 2, 3]. diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs index f00f15d8f05..62a9dd5969d 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs @@ -103,10 +103,9 @@ impl Ssa { self, brillig: Brillig, abi_distinctness: AbiDistinctness, - allow_log_ops: bool, ) -> Result { let context = Context::new(); - let mut generated_acir = context.convert_ssa(self, brillig, allow_log_ops)?; + let mut generated_acir = context.convert_ssa(self, brillig)?; match abi_distinctness { AbiDistinctness::Distinct => { @@ -144,15 +143,10 @@ impl Context { } /// Converts SSA into ACIR - fn convert_ssa( - self, - ssa: Ssa, - brillig: Brillig, - allow_log_ops: bool, - ) -> Result { + fn convert_ssa(self, ssa: Ssa, brillig: Brillig) -> Result { let main_func = ssa.main(); match main_func.runtime() { - RuntimeType::Acir => self.convert_acir_main(main_func, &ssa, brillig, allow_log_ops), + RuntimeType::Acir => self.convert_acir_main(main_func, &ssa, brillig), RuntimeType::Brillig => self.convert_brillig_main(main_func, brillig), } } @@ -162,14 +156,13 @@ impl Context { main_func: &Function, ssa: &Ssa, brillig: Brillig, - allow_log_ops: bool, ) -> Result { let dfg = &main_func.dfg; let entry_block = &dfg[main_func.entry_block()]; let input_witness = self.convert_ssa_block_params(entry_block.parameters(), dfg)?; for instruction_id in entry_block.instructions() { - self.convert_ssa_instruction(*instruction_id, dfg, ssa, &brillig, allow_log_ops)?; + self.convert_ssa_instruction(*instruction_id, dfg, ssa, &brillig)?; } self.convert_ssa_return(entry_block.unwrap_terminator(), dfg)?; @@ -294,7 +287,6 @@ impl Context { dfg: &DataFlowGraph, ssa: &Ssa, brillig: &Brillig, - allow_log_ops: bool, ) -> Result<(), RuntimeError> { let instruction = &dfg[instruction_id]; self.acir_context.set_location(dfg.get_location(&instruction_id)); @@ -339,13 +331,8 @@ impl Context { } } Value::Intrinsic(intrinsic) => { - let outputs = self.convert_ssa_intrinsic_call( - *intrinsic, - arguments, - dfg, - allow_log_ops, - result_ids, - )?; + let outputs = self + .convert_ssa_intrinsic_call(*intrinsic, arguments, dfg, result_ids)?; // Issue #1438 causes this check to fail with intrinsics that return 0 // results but the ssa form instead creates 1 unit result value. @@ -929,7 +916,6 @@ impl Context { intrinsic: Intrinsic, arguments: &[ValueId], dfg: &DataFlowGraph, - allow_log_ops: bool, result_ids: &[ValueId], ) -> Result, RuntimeError> { match intrinsic { @@ -959,13 +945,8 @@ impl Context { self.acir_context.bit_decompose(endian, field, bit_size, result_type) } - Intrinsic::Println => { - let inputs = vecmap(arguments, |arg| self.convert_value(*arg, dfg)); - if allow_log_ops { - self.acir_context.print(inputs)?; - } - Ok(Vec::new()) - } + // TODO(#2115): Remove the println intrinsic as the oracle println is now used instead + Intrinsic::Println => Ok(Vec::new()), Intrinsic::Sort => { let inputs = vecmap(arguments, |arg| self.convert_value(*arg, dfg)); // We flatten the inputs and retrieve the bit_size of the elements @@ -1133,7 +1114,7 @@ mod tests { let ssa = builder.finish(); let context = Context::new(); - let acir = context.convert_ssa(ssa, Brillig::default(), false).unwrap(); + let acir = context.convert_ssa(ssa, Brillig::default()).unwrap(); let expected_opcodes = vec![Opcode::Arithmetic(&Expression::one() - &Expression::from(Witness(1)))]; diff --git a/crates/wasm/src/compile.rs b/crates/wasm/src/compile.rs index 15d8d5107ea..4254110b849 100644 --- a/crates/wasm/src/compile.rs +++ b/crates/wasm/src/compile.rs @@ -107,8 +107,8 @@ pub fn compile(args: JsValue) -> JsValue { ::from_serde(&optimized_contracts).unwrap() } else { let main = context.get_main_function(&crate_id).expect("Could not find main function!"); - let mut compiled_program = compile_no_check(&context, true, &options.compile_options, main) - .expect("Compilation failed"); + let mut compiled_program = + compile_no_check(&context, &options.compile_options, main).expect("Compilation failed"); compiled_program.circuit = optimize_circuit(compiled_program.circuit); From a07b8a48924865d8425d35e40c75f48a13a81935 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 2 Aug 2023 20:00:23 +0100 Subject: [PATCH 11/11] chore: rename `ssa_refactor` module to `ssa` (#2129) --- .gitignore | 2 -- .../noirc_evaluator/src/brillig/brillig_gen.rs | 2 +- .../src/brillig/brillig_gen/brillig_block.rs | 9 +++++---- .../src/brillig/brillig_gen/brillig_fn.rs | 2 +- crates/noirc_evaluator/src/brillig/mod.rs | 2 +- crates/noirc_evaluator/src/lib.rs | 4 ++-- .../src/{ssa_refactor.rs => ssa.rs} | 0 .../src/{ssa_refactor => ssa}/abi_gen/mod.rs | 0 .../{ssa_refactor => ssa}/acir_gen/acir_ir.rs | 0 .../acir_gen/acir_ir/acir_variable.rs | 6 +++--- .../acir_gen/acir_ir/generated_acir.rs | 0 .../acir_gen/acir_ir/sort.rs | 0 .../src/{ssa_refactor => ssa}/acir_gen/mod.rs | 2 +- .../src/{ssa_refactor => ssa}/ir.rs | 0 .../src/{ssa_refactor => ssa}/ir/basic_block.rs | 0 .../src/{ssa_refactor => ssa}/ir/cfg.rs | 2 +- .../src/{ssa_refactor => ssa}/ir/dfg.rs | 4 ++-- .../src/{ssa_refactor => ssa}/ir/dom.rs | 2 +- .../src/{ssa_refactor => ssa}/ir/function.rs | 0 .../ir/function_inserter.rs | 0 .../src/{ssa_refactor => ssa}/ir/instruction.rs | 4 +--- .../{ssa_refactor => ssa}/ir/instruction/call.rs | 2 +- .../src/{ssa_refactor => ssa}/ir/map.rs | 0 .../src/{ssa_refactor => ssa}/ir/post_order.rs | 4 ++-- .../src/{ssa_refactor => ssa}/ir/printer.rs | 0 .../src/{ssa_refactor => ssa}/ir/types.rs | 0 .../src/{ssa_refactor => ssa}/ir/value.rs | 2 +- .../opt/constant_folding.rs | 4 ++-- .../{ssa_refactor => ssa}/opt/defunctionalize.rs | 2 +- .../src/{ssa_refactor => ssa}/opt/die.rs | 4 ++-- .../src/{ssa_refactor => ssa}/opt/flatten_cfg.rs | 6 +++--- .../opt/flatten_cfg/branch_analysis.rs | 6 ++---- .../src/{ssa_refactor => ssa}/opt/inlining.rs | 4 ++-- .../src/{ssa_refactor => ssa}/opt/mem2reg.rs | 4 ++-- .../src/{ssa_refactor => ssa}/opt/mod.rs | 0 .../{ssa_refactor => ssa}/opt/simplify_cfg.rs | 4 ++-- .../src/{ssa_refactor => ssa}/opt/unrolling.rs | 4 ++-- .../src/{ssa_refactor => ssa}/ssa_builder/mod.rs | 4 ++-- .../src/{ssa_refactor => ssa}/ssa_gen/context.rs | 16 ++++++++-------- .../src/{ssa_refactor => ssa}/ssa_gen/mod.rs | 0 .../src/{ssa_refactor => ssa}/ssa_gen/program.rs | 2 +- .../src/{ssa_refactor => ssa}/ssa_gen/value.rs | 4 ++-- 42 files changed, 54 insertions(+), 59 deletions(-) rename crates/noirc_evaluator/src/{ssa_refactor.rs => ssa.rs} (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/abi_gen/mod.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/acir_gen/acir_ir.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/acir_gen/acir_ir/acir_variable.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/acir_gen/acir_ir/generated_acir.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/acir_gen/acir_ir/sort.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/acir_gen/mod.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/basic_block.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/cfg.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/dfg.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/dom.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/function.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/function_inserter.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/instruction.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/instruction/call.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/map.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/post_order.rs (97%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/printer.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/types.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ir/value.rs (98%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/constant_folding.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/defunctionalize.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/die.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/flatten_cfg.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/flatten_cfg/branch_analysis.rs (98%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/inlining.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/mem2reg.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/mod.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/simplify_cfg.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/opt/unrolling.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ssa_builder/mod.rs (99%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ssa_gen/context.rs (98%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ssa_gen/mod.rs (100%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ssa_gen/program.rs (98%) rename crates/noirc_evaluator/src/{ssa_refactor => ssa}/ssa_gen/value.rs (98%) diff --git a/.gitignore b/.gitignore index af3a8e8beb2..8aec0edeadc 100644 --- a/.gitignore +++ b/.gitignore @@ -22,5 +22,3 @@ result **/target !crates/nargo_cli/tests/test_data/*/target !crates/nargo_cli/tests/test_data/*/target/witness.tr -!crates/nargo_cli/tests/test_data_ssa_refactor/*/target -!crates/nargo_cli/tests/test_data_ssa_refactor/*/target/witness.tr \ No newline at end of file diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen.rs b/crates/noirc_evaluator/src/brillig/brillig_gen.rs index 3ba04ed1afb..a1e82bbf443 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen.rs @@ -4,7 +4,7 @@ pub(crate) mod brillig_directive; pub(crate) mod brillig_fn; pub(crate) mod brillig_slice_ops; -use crate::ssa_refactor::ir::{function::Function, post_order::PostOrder}; +use crate::ssa::ir::{function::Function, post_order::PostOrder}; use std::collections::HashMap; diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs index a9bbe189e57..ded6be71bd5 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs @@ -4,12 +4,13 @@ use crate::brillig::brillig_gen::brillig_slice_ops::{ use crate::brillig::brillig_ir::{ BrilligBinaryOp, BrilligContext, BRILLIG_INTEGER_ARITHMETIC_BIT_SIZE, }; -use crate::ssa_refactor::ir::function::FunctionId; -use crate::ssa_refactor::ir::instruction::{Endian, Intrinsic}; -use crate::ssa_refactor::ir::{ +use crate::ssa::ir::{ basic_block::{BasicBlock, BasicBlockId}, dfg::DataFlowGraph, - instruction::{Binary, BinaryOp, Instruction, InstructionId, TerminatorInstruction}, + function::FunctionId, + instruction::{ + Binary, BinaryOp, Endian, Instruction, InstructionId, Intrinsic, TerminatorInstruction, + }, types::{NumericType, Type}, value::{Value, ValueId}, }; diff --git a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs index 210d6da7be6..7c4cb5e2ced 100644 --- a/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs +++ b/crates/noirc_evaluator/src/brillig/brillig_gen/brillig_fn.rs @@ -8,7 +8,7 @@ use crate::{ artifact::{BrilligParameter, Label}, BrilligContext, }, - ssa_refactor::ir::{ + ssa::ir::{ dfg::DataFlowGraph, function::{Function, FunctionId}, types::{CompositeType, Type}, diff --git a/crates/noirc_evaluator/src/brillig/mod.rs b/crates/noirc_evaluator/src/brillig/mod.rs index 105475323a7..0c6ddd53a4e 100644 --- a/crates/noirc_evaluator/src/brillig/mod.rs +++ b/crates/noirc_evaluator/src/brillig/mod.rs @@ -5,7 +5,7 @@ use self::{ brillig_gen::{brillig_fn::FunctionContext, convert_ssa_function}, brillig_ir::artifact::{BrilligArtifact, Label}, }; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ function::{Function, FunctionId, RuntimeType}, value::Value, diff --git a/crates/noirc_evaluator/src/lib.rs b/crates/noirc_evaluator/src/lib.rs index c7d4f5baed6..f5403e1cf49 100644 --- a/crates/noirc_evaluator/src/lib.rs +++ b/crates/noirc_evaluator/src/lib.rs @@ -7,8 +7,8 @@ mod errors; // SSA code to create the SSA based IR // for functions and execute different optimizations. -pub mod ssa_refactor; +pub mod ssa; pub mod brillig; -pub use ssa_refactor::create_circuit; +pub use ssa::create_circuit; diff --git a/crates/noirc_evaluator/src/ssa_refactor.rs b/crates/noirc_evaluator/src/ssa.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor.rs rename to crates/noirc_evaluator/src/ssa.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/abi_gen/mod.rs b/crates/noirc_evaluator/src/ssa/abi_gen/mod.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/abi_gen/mod.rs rename to crates/noirc_evaluator/src/ssa/abi_gen/mod.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir.rs b/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir.rs rename to crates/noirc_evaluator/src/ssa/acir_gen/acir_ir.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs b/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs rename to crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs index d1479ef1f1b..779aaa559ed 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs @@ -1,9 +1,9 @@ use super::generated_acir::GeneratedAcir; use crate::brillig::brillig_gen::brillig_directive; use crate::errors::{InternalError, RuntimeError}; -use crate::ssa_refactor::acir_gen::{AcirDynamicArray, AcirValue}; -use crate::ssa_refactor::ir::types::Type as SsaType; -use crate::ssa_refactor::ir::{instruction::Endian, types::NumericType}; +use crate::ssa::acir_gen::{AcirDynamicArray, AcirValue}; +use crate::ssa::ir::types::Type as SsaType; +use crate::ssa::ir::{instruction::Endian, types::NumericType}; use acvm::acir::circuit::opcodes::{BlockId, MemOp}; use acvm::acir::circuit::Opcode; use acvm::acir::{ diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs b/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/generated_acir.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs rename to crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/generated_acir.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/sort.rs b/crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/sort.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/sort.rs rename to crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/sort.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs b/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs rename to crates/noirc_evaluator/src/ssa/acir_gen/mod.rs index 62a9dd5969d..331c56f59d7 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs +++ b/crates/noirc_evaluator/src/ssa/acir_gen/mod.rs @@ -1086,7 +1086,7 @@ mod tests { use crate::{ brillig::Brillig, - ssa_refactor::{ + ssa::{ ir::{function::RuntimeType, map::Id, types::Type}, ssa_builder::FunctionBuilder, }, diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir.rs b/crates/noirc_evaluator/src/ssa/ir.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ir.rs rename to crates/noirc_evaluator/src/ssa/ir.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/basic_block.rs b/crates/noirc_evaluator/src/ssa/ir/basic_block.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ir/basic_block.rs rename to crates/noirc_evaluator/src/ssa/ir/basic_block.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/cfg.rs b/crates/noirc_evaluator/src/ssa/ir/cfg.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/ir/cfg.rs rename to crates/noirc_evaluator/src/ssa/ir/cfg.rs index f08b477696a..a91123438fa 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/cfg.rs +++ b/crates/noirc_evaluator/src/ssa/ir/cfg.rs @@ -128,7 +128,7 @@ impl ControlFlowGraph { #[cfg(test)] mod tests { - use crate::ssa_refactor::ir::{instruction::TerminatorInstruction, map::Id, types::Type}; + use crate::ssa::ir::{instruction::TerminatorInstruction, map::Id, types::Type}; use super::{super::function::Function, ControlFlowGraph}; diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs b/crates/noirc_evaluator/src/ssa/ir/dfg.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs rename to crates/noirc_evaluator/src/ssa/ir/dfg.rs index 6d74e49b03b..29f5156a88c 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs +++ b/crates/noirc_evaluator/src/ssa/ir/dfg.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, collections::HashMap}; -use crate::ssa_refactor::ir::instruction::SimplifyResult; +use crate::ssa::ir::instruction::SimplifyResult; use super::{ basic_block::{BasicBlock, BasicBlockId}, @@ -503,7 +503,7 @@ impl<'dfg> InsertInstructionResult<'dfg> { #[cfg(test)] mod tests { use super::DataFlowGraph; - use crate::ssa_refactor::ir::instruction::Instruction; + use crate::ssa::ir::instruction::Instruction; #[test] fn make_instruction() { diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/dom.rs b/crates/noirc_evaluator/src/ssa/ir/dom.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/ir/dom.rs rename to crates/noirc_evaluator/src/ssa/ir/dom.rs index 4763ffffbd1..b7b1728d035 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/dom.rs +++ b/crates/noirc_evaluator/src/ssa/ir/dom.rs @@ -245,7 +245,7 @@ impl DominatorTree { mod tests { use std::cmp::Ordering; - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{ basic_block::BasicBlockId, dom::DominatorTree, diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/function.rs b/crates/noirc_evaluator/src/ssa/ir/function.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ir/function.rs rename to crates/noirc_evaluator/src/ssa/ir/function.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/function_inserter.rs b/crates/noirc_evaluator/src/ssa/ir/function_inserter.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ir/function_inserter.rs rename to crates/noirc_evaluator/src/ssa/ir/function_inserter.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs b/crates/noirc_evaluator/src/ssa/ir/instruction.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs rename to crates/noirc_evaluator/src/ssa/ir/instruction.rs index 7edb74f4206..680715fb0ec 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs +++ b/crates/noirc_evaluator/src/ssa/ir/instruction.rs @@ -2,13 +2,11 @@ use acvm::{acir::BlackBoxFunc, FieldElement}; use iter_extended::vecmap; use num_bigint::BigUint; -use crate::ssa_refactor::ir::types::NumericType; - use super::{ basic_block::BasicBlockId, dfg::DataFlowGraph, map::Id, - types::Type, + types::{NumericType, Type}, value::{Value, ValueId}, }; diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs b/crates/noirc_evaluator/src/ssa/ir/instruction/call.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs rename to crates/noirc_evaluator/src/ssa/ir/instruction/call.rs index 96998d92fcf..2f0c077a1a7 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction/call.rs +++ b/crates/noirc_evaluator/src/ssa/ir/instruction/call.rs @@ -4,7 +4,7 @@ use acvm::{acir::BlackBoxFunc, BlackBoxResolutionError, FieldElement}; use iter_extended::vecmap; use num_bigint::BigUint; -use crate::ssa_refactor::ir::{ +use crate::ssa::ir::{ dfg::DataFlowGraph, instruction::Intrinsic, map::Id, diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/map.rs b/crates/noirc_evaluator/src/ssa/ir/map.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ir/map.rs rename to crates/noirc_evaluator/src/ssa/ir/map.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/post_order.rs b/crates/noirc_evaluator/src/ssa/ir/post_order.rs similarity index 97% rename from crates/noirc_evaluator/src/ssa_refactor/ir/post_order.rs rename to crates/noirc_evaluator/src/ssa/ir/post_order.rs index 2f7b5edebe6..202f5cff716 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/post_order.rs +++ b/crates/noirc_evaluator/src/ssa/ir/post_order.rs @@ -5,7 +5,7 @@ use std::collections::HashSet; -use crate::ssa_refactor::ir::{basic_block::BasicBlockId, function::Function}; +use crate::ssa::ir::{basic_block::BasicBlockId, function::Function}; /// Depth-first traversal stack state marker for computing the cfg post-order. enum Visit { @@ -67,7 +67,7 @@ impl PostOrder { #[cfg(test)] mod tests { - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{ function::{Function, RuntimeType}, map::Id, diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/printer.rs b/crates/noirc_evaluator/src/ssa/ir/printer.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ir/printer.rs rename to crates/noirc_evaluator/src/ssa/ir/printer.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/types.rs b/crates/noirc_evaluator/src/ssa/ir/types.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ir/types.rs rename to crates/noirc_evaluator/src/ssa/ir/types.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/value.rs b/crates/noirc_evaluator/src/ssa/ir/value.rs similarity index 98% rename from crates/noirc_evaluator/src/ssa_refactor/ir/value.rs rename to crates/noirc_evaluator/src/ssa/ir/value.rs index cea526058b4..54831eb4a07 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/value.rs +++ b/crates/noirc_evaluator/src/ssa/ir/value.rs @@ -1,6 +1,6 @@ use acvm::FieldElement; -use crate::ssa_refactor::ir::basic_block::BasicBlockId; +use crate::ssa::ir::basic_block::BasicBlockId; use super::{ function::FunctionId, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/constant_folding.rs b/crates/noirc_evaluator/src/ssa/opt/constant_folding.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/constant_folding.rs rename to crates/noirc_evaluator/src/ssa/opt/constant_folding.rs index acf048595d7..ea46ddf1d4f 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/constant_folding.rs +++ b/crates/noirc_evaluator/src/ssa/opt/constant_folding.rs @@ -2,7 +2,7 @@ use std::collections::HashSet; use iter_extended::vecmap; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::BasicBlockId, dfg::InsertInstructionResult, function::Function, instruction::InstructionId, @@ -94,7 +94,7 @@ impl Context { mod test { use std::rc::Rc; - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{ function::RuntimeType, instruction::{BinaryOp, TerminatorInstruction}, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/defunctionalize.rs b/crates/noirc_evaluator/src/ssa/opt/defunctionalize.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/defunctionalize.rs rename to crates/noirc_evaluator/src/ssa/opt/defunctionalize.rs index fc3bc5d9aa6..10561bf731f 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/defunctionalize.rs +++ b/crates/noirc_evaluator/src/ssa/opt/defunctionalize.rs @@ -9,7 +9,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use acvm::FieldElement; use iter_extended::vecmap; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::BasicBlockId, function::{Function, FunctionId, RuntimeType, Signature}, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/die.rs b/crates/noirc_evaluator/src/ssa/opt/die.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/die.rs rename to crates/noirc_evaluator/src/ssa/opt/die.rs index ef73938cc37..935568af2db 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/die.rs +++ b/crates/noirc_evaluator/src/ssa/opt/die.rs @@ -2,7 +2,7 @@ //! which the results are unused. use std::collections::HashSet; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::{BasicBlock, BasicBlockId}, dfg::DataFlowGraph, @@ -133,7 +133,7 @@ impl Context { #[cfg(test)] mod test { - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{function::RuntimeType, instruction::BinaryOp, map::Id, types::Type}, ssa_builder::FunctionBuilder, }; diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs b/crates/noirc_evaluator/src/ssa/opt/flatten_cfg.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs rename to crates/noirc_evaluator/src/ssa/opt/flatten_cfg.rs index fdc4be085d7..1bcdf433d79 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs +++ b/crates/noirc_evaluator/src/ssa/opt/flatten_cfg.rs @@ -137,7 +137,7 @@ use acvm::FieldElement; use iter_extended::vecmap; use noirc_errors::Location; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::BasicBlockId, cfg::ControlFlowGraph, @@ -213,7 +213,7 @@ fn flatten_function_cfg(function: &mut Function) { // TODO This loops forever, if the predecessors are not then processed // TODO Because it will visit the same block again, pop it out of the queue // TODO then back into the queue again. - if let crate::ssa_refactor::ir::function::RuntimeType::Brillig = function.runtime() { + if let crate::ssa::ir::function::RuntimeType::Brillig = function.runtime() { return; } let cfg = ControlFlowGraph::with_function(function); @@ -739,7 +739,7 @@ impl<'f> Context<'f> { mod test { use std::rc::Rc; - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{ dfg::DataFlowGraph, function::{Function, RuntimeType}, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg/branch_analysis.rs b/crates/noirc_evaluator/src/ssa/opt/flatten_cfg/branch_analysis.rs similarity index 98% rename from crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg/branch_analysis.rs rename to crates/noirc_evaluator/src/ssa/opt/flatten_cfg/branch_analysis.rs index bed0686e45b..1203d03f562 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg/branch_analysis.rs +++ b/crates/noirc_evaluator/src/ssa/opt/flatten_cfg/branch_analysis.rs @@ -21,9 +21,7 @@ //! the resulting map from each split block to each join block is returned. use std::collections::HashMap; -use crate::ssa_refactor::ir::{ - basic_block::BasicBlockId, cfg::ControlFlowGraph, function::Function, -}; +use crate::ssa::ir::{basic_block::BasicBlockId, cfg::ControlFlowGraph, function::Function}; /// Returns a `HashMap` mapping blocks that start a branch (i.e. blocks terminated with jmpif) to /// their corresponding blocks that end the branch. @@ -114,7 +112,7 @@ impl<'cfg> Context<'cfg> { #[cfg(test)] mod test { - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{cfg::ControlFlowGraph, function::RuntimeType, map::Id, types::Type}, opt::flatten_cfg::branch_analysis::find_branch_ends, ssa_builder::FunctionBuilder, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/inlining.rs b/crates/noirc_evaluator/src/ssa/opt/inlining.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/inlining.rs rename to crates/noirc_evaluator/src/ssa/opt/inlining.rs index 7aa2f9d176a..d4c118fd3f4 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/inlining.rs +++ b/crates/noirc_evaluator/src/ssa/opt/inlining.rs @@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet}; use iter_extended::vecmap; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::BasicBlockId, dfg::InsertInstructionResult, @@ -482,7 +482,7 @@ impl<'function> PerFunctionContext<'function> { mod test { use acvm::FieldElement; - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{ basic_block::BasicBlockId, function::RuntimeType, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/mem2reg.rs b/crates/noirc_evaluator/src/ssa/opt/mem2reg.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/mem2reg.rs rename to crates/noirc_evaluator/src/ssa/opt/mem2reg.rs index 15108abc490..b9e849bb77c 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/mem2reg.rs +++ b/crates/noirc_evaluator/src/ssa/opt/mem2reg.rs @@ -5,7 +5,7 @@ use std::collections::{BTreeMap, HashMap, HashSet}; use iter_extended::vecmap; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::BasicBlockId, dfg::DataFlowGraph, @@ -182,7 +182,7 @@ mod tests { use acvm::FieldElement; use im::vector; - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{ basic_block::BasicBlockId, dfg::DataFlowGraph, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/mod.rs b/crates/noirc_evaluator/src/ssa/opt/mod.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/opt/mod.rs rename to crates/noirc_evaluator/src/ssa/opt/mod.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/simplify_cfg.rs b/crates/noirc_evaluator/src/ssa/opt/simplify_cfg.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/simplify_cfg.rs rename to crates/noirc_evaluator/src/ssa/opt/simplify_cfg.rs index 22991e38b94..58259cec90c 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/simplify_cfg.rs +++ b/crates/noirc_evaluator/src/ssa/opt/simplify_cfg.rs @@ -11,7 +11,7 @@ //! Currently, 1 and 4 are unimplemented. use std::collections::HashSet; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::BasicBlockId, cfg::ControlFlowGraph, function::Function, instruction::TerminatorInstruction, @@ -148,7 +148,7 @@ fn try_inline_into_predecessor( #[cfg(test)] mod test { - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{ function::RuntimeType, instruction::{BinaryOp, TerminatorInstruction}, diff --git a/crates/noirc_evaluator/src/ssa_refactor/opt/unrolling.rs b/crates/noirc_evaluator/src/ssa/opt/unrolling.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/opt/unrolling.rs rename to crates/noirc_evaluator/src/ssa/opt/unrolling.rs index e5d7d6f0d5c..f6d7c952277 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/opt/unrolling.rs +++ b/crates/noirc_evaluator/src/ssa/opt/unrolling.rs @@ -14,7 +14,7 @@ //! program that will need to be removed by a later simplify cfg pass. use std::collections::{HashMap, HashSet}; -use crate::ssa_refactor::{ +use crate::ssa::{ ir::{ basic_block::BasicBlockId, cfg::ControlFlowGraph, dfg::DataFlowGraph, dom::DominatorTree, function::Function, function_inserter::FunctionInserter, @@ -424,7 +424,7 @@ impl<'f> LoopIteration<'f> { #[cfg(test)] mod tests { - use crate::ssa_refactor::{ + use crate::ssa::{ ir::{function::RuntimeType, instruction::BinaryOp, map::Id, types::Type}, ssa_builder::FunctionBuilder, }; diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_builder/mod.rs b/crates/noirc_evaluator/src/ssa/ssa_builder/mod.rs similarity index 99% rename from crates/noirc_evaluator/src/ssa_refactor/ssa_builder/mod.rs rename to crates/noirc_evaluator/src/ssa/ssa_builder/mod.rs index 02350d9ed17..066b5b51199 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_builder/mod.rs +++ b/crates/noirc_evaluator/src/ssa/ssa_builder/mod.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use acvm::FieldElement; use noirc_errors::Location; -use crate::ssa_refactor::ir::{ +use crate::ssa::ir::{ basic_block::BasicBlockId, function::{Function, FunctionId}, instruction::{Binary, BinaryOp, Instruction, TerminatorInstruction}, @@ -363,7 +363,7 @@ mod tests { use acvm::FieldElement; - use crate::ssa_refactor::ir::{ + use crate::ssa::ir::{ function::RuntimeType, instruction::{Endian, Intrinsic}, map::Id, diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs b/crates/noirc_evaluator/src/ssa/ssa_gen/context.rs similarity index 98% rename from crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs rename to crates/noirc_evaluator/src/ssa/ssa_gen/context.rs index a526d93f85b..3e0bbff2a83 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/context.rs +++ b/crates/noirc_evaluator/src/ssa/ssa_gen/context.rs @@ -9,14 +9,14 @@ use noirc_frontend::monomorphization::ast::{self, LocalId, Parameters}; use noirc_frontend::monomorphization::ast::{FuncId, Program}; use noirc_frontend::{BinaryOpKind, Signedness}; -use crate::ssa_refactor::ir::dfg::DataFlowGraph; -use crate::ssa_refactor::ir::function::FunctionId as IrFunctionId; -use crate::ssa_refactor::ir::function::{Function, RuntimeType}; -use crate::ssa_refactor::ir::instruction::{BinaryOp, Endian, Intrinsic}; -use crate::ssa_refactor::ir::map::AtomicCounter; -use crate::ssa_refactor::ir::types::{NumericType, Type}; -use crate::ssa_refactor::ir::value::ValueId; -use crate::ssa_refactor::ssa_builder::FunctionBuilder; +use crate::ssa::ir::dfg::DataFlowGraph; +use crate::ssa::ir::function::FunctionId as IrFunctionId; +use crate::ssa::ir::function::{Function, RuntimeType}; +use crate::ssa::ir::instruction::{BinaryOp, Endian, Intrinsic}; +use crate::ssa::ir::map::AtomicCounter; +use crate::ssa::ir::types::{NumericType, Type}; +use crate::ssa::ir::value::ValueId; +use crate::ssa::ssa_builder::FunctionBuilder; use super::value::{Tree, Value, Values}; diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs b/crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs similarity index 100% rename from crates/noirc_evaluator/src/ssa_refactor/ssa_gen/mod.rs rename to crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/program.rs b/crates/noirc_evaluator/src/ssa/ssa_gen/program.rs similarity index 98% rename from crates/noirc_evaluator/src/ssa_refactor/ssa_gen/program.rs rename to crates/noirc_evaluator/src/ssa/ssa_gen/program.rs index aec0e4262c8..509f778f3b0 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/program.rs +++ b/crates/noirc_evaluator/src/ssa/ssa_gen/program.rs @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, fmt::Display}; use iter_extended::btree_map; -use crate::ssa_refactor::ir::{ +use crate::ssa::ir::{ function::{Function, FunctionId}, map::AtomicCounter, }; diff --git a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/value.rs b/crates/noirc_evaluator/src/ssa/ssa_gen/value.rs similarity index 98% rename from crates/noirc_evaluator/src/ssa_refactor/ssa_gen/value.rs rename to crates/noirc_evaluator/src/ssa/ssa_gen/value.rs index 2d209635610..e7bb515465b 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ssa_gen/value.rs +++ b/crates/noirc_evaluator/src/ssa/ssa_gen/value.rs @@ -1,7 +1,7 @@ use iter_extended::vecmap; -use crate::ssa_refactor::ir::types::Type; -use crate::ssa_refactor::ir::value::ValueId as IrValueId; +use crate::ssa::ir::types::Type; +use crate::ssa::ir::value::ValueId as IrValueId; use super::context::FunctionContext;