Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ssa refactor): Switch to immutable arrays #1578

Merged
merged 26 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b7ff73b
Represent SSA arrays with im::Vector
jfecher Jun 2, 2023
31dd36f
Get tests passing
jfecher Jun 2, 2023
5d4be5c
Implement assign with immutable arrays
jfecher Jun 6, 2023
584f972
Add constant folding pass
jfecher Jun 6, 2023
399b708
Update comments
jfecher Jun 6, 2023
fccb3e8
Merge branch 'jf/constant-folding' into jf/ssa-im-vector
jfecher Jun 6, 2023
3e80698
Clippy
jfecher Jun 6, 2023
e6be421
Update comment
jfecher Jun 6, 2023
fc296ab
Update type of array
jfecher Jun 6, 2023
9107005
Update crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
jfecher Jun 7, 2023
3aaa978
Undo formatting changes in instruction.rs
jfecher Jun 7, 2023
bfc4fe2
Merge branch 'master' into jf/ssa-im-vector
jfecher Jun 7, 2023
65f5b0c
Massive acir_gen update
jfecher Jun 7, 2023
d48bf2b
Merge branch 'master' into jf/ssa-im-vector
jfecher Jun 7, 2023
1116881
Refactor acir array operations into a shared function
jfecher Jun 7, 2023
d94444c
Appease clippy
jfecher Jun 7, 2023
e6c3cd0
Update to_radix and to_bits in acir_gen to return arrays
jfecher Jun 7, 2023
faa991c
Disable assert
jfecher Jun 8, 2023
9c28a16
Merge branch 'master' into jf/ssa-im-vector
jfecher Jun 8, 2023
5e7c1dd
Fix convert_type for arrays
jfecher Jun 8, 2023
81f1e05
Include AcirType in AcirValue::Var variant
jfecher Jun 8, 2023
0d26c7a
Merge branch 'master' into jf/ssa-im-vector
jfecher Jun 8, 2023
1478a35
Fix black box functions
jfecher Jun 8, 2023
70137b8
Appease clippy
jfecher Jun 8, 2023
4715851
Fix simple_radix
jfecher Jun 8, 2023
9b0748e
Add doc comments
jfecher Jun 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/nargo_cli/src/cli/prove_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(crate) struct ProveCommand {
verifier_name: String,

/// Verify proof after proving
#[arg(short, long)]
#[arg(long)]
verify: bool,

#[clap(flatten)]
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_evaluator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ iter-extended.workspace = true
thiserror.workspace = true
num-bigint = "0.4"
num-traits = "0.2.8"
im = "15.1"

[dev-dependencies]
rand="0.8.5"
3 changes: 1 addition & 2 deletions crates/noirc_evaluator/src/ssa_refactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub mod ssa_gen;
/// form and performing optimizations there. When finished,
/// convert the final SSA into ACIR and return it.
pub(crate) fn optimize_into_acir(program: Program, allow_log_ops: bool) -> GeneratedAcir {
let func_signature = program.main_function_signature.clone();
let ssa = ssa_gen::generate_ssa(program).print("Initial SSA:");
let brillig = ssa.to_brillig();
ssa.inline_functions()
Expand All @@ -44,7 +43,7 @@ pub(crate) fn optimize_into_acir(program: Program, allow_log_ops: bool) -> Gener
.print("After Mem2Reg:")
.fold_constants()
.print("After Constant Folding:")
.into_acir(func_signature, brillig, allow_log_ops)
.into_acir(brillig, allow_log_ops)
}

/// Compiles the Program into ACIR and applies optimizations to the arithmetic gates
Expand Down
87 changes: 1 addition & 86 deletions crates/noirc_evaluator/src/ssa_refactor/abi_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,7 @@ use std::collections::BTreeMap;

use acvm::acir::native_types::Witness;
use iter_extended::{btree_map, vecmap};
use noirc_abi::{Abi, AbiParameter, AbiType, FunctionSignature};

/// Traverses the parameters to the program to infer the lengths of any arrays that occur.
///
/// This is needed for the acir_gen pass, because while the SSA representation of the program
/// knows the positions at which any arrays occur in the parameters to main, it does not know the
/// lengths of said arrays.
///
/// This function returns the lengths ordered such as to correspond to the ordering used by the
/// SSA representation. This allows the lengths to be consumed as array params are encountered in
/// the SSA.
pub(crate) fn collate_array_info(abi_params: &[AbiParameter]) -> Vec<(usize, AbiType)> {
let mut acc = Vec::new();
for abi_param in abi_params {
collate_array_info_recursive(&mut acc, &abi_param.typ);
}
acc
}

/// The underlying recursive implementation of `collate_array_info`
///
/// This does a depth-first traversal of the abi until an array (or string) is encountered, at
/// which point arrays are handled differently depending on the element type:
/// - arrays of fields, integers or booleans produce an array of the specified length
/// - arrays of structs produce an array of the specified length for each field of the flatten
/// struct (which reflects a simplification made during monomorphization)
fn collate_array_info_recursive(acc: &mut Vec<(usize, AbiType)>, abi_type: &AbiType) {
match abi_type {
AbiType::Array { length, typ: elem_type } => {
let elem_type = elem_type.as_ref();
match elem_type {
AbiType::Array { .. } => {
unreachable!("2D arrays are not supported");
}
AbiType::Struct { .. } => {
// monomorphization converts arrays of structs into an array per flattened
// struct field.
let mut destructured_array_types = Vec::new();
flatten_abi_type_recursive(&mut destructured_array_types, elem_type);
for abi_type in destructured_array_types {
acc.push((*length as usize, abi_type));
}
}
AbiType::String { .. } => {
unreachable!("Arrays of strings are not supported");
}
AbiType::Boolean | AbiType::Field | AbiType::Integer { .. } => {
// Simple 1D array
acc.push((*length as usize, elem_type.clone()));
}
}
}
AbiType::Struct { fields } => {
for (_, field_type) in fields {
collate_array_info_recursive(acc, field_type);
}
}
AbiType::String { length } => {
// Strings are implemented as u8 arrays
let element_type = AbiType::Integer { sign: noirc_abi::Sign::Unsigned, width: 8 };
acc.push((*length as usize, element_type));
}
AbiType::Boolean | AbiType::Field | AbiType::Integer { .. } => {
// Do not produce arrays
}
}
}

/// Used for flattening a struct into its ordered constituent field types. This is needed for
/// informing knowing the bit widths of any array sets that were destructured from an array of
/// structs. For this reason, any array encountered within this function are considered to be
/// nested within a struct and are therefore disallowed. This is acceptable because this function
/// will only be applied to structs which have been found in an array.
fn flatten_abi_type_recursive(acc: &mut Vec<AbiType>, abi_type: &AbiType) {
match abi_type {
AbiType::Array { .. } | AbiType::String { .. } => {
unreachable!("2D arrays are unsupported")
}
AbiType::Boolean | AbiType::Integer { .. } | AbiType::Field => acc.push(abi_type.clone()),
AbiType::Struct { fields } => {
for (_, field_type) in fields {
flatten_abi_type_recursive(acc, field_type);
}
}
}
}
use noirc_abi::{Abi, AbiParameter, FunctionSignature};

/// Arranges a function signature and a generated circuit's return witnesses into a
/// `noirc_abi::Abi`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub(crate) mod acir_variable;
pub(crate) mod errors;
pub(crate) mod generated_acir;
pub(crate) mod memory;
Loading