Skip to content

Commit

Permalink
Merge branch 'master' into aztec-packages
Browse files Browse the repository at this point in the history
* master: (25 commits)
  feat: Complex outputs from acir call (#4952)
  fix: Require for all foldable functions to use distinct return  (#4949)
  feat!: use `distinct` return value witnesses by default (#4951)
  chore(docs): adding matomo tracking (#4898)
  chore: fix typo in `ResolverError::AbiAttributeOutsideContract` (#4933)
  chore: Add test for recursing a foldable function (#4948)
  feat: Handle `no_predicates` attribute (#4942)
  fix: ensure where clauses propagated to trait default definitions (#4894)
  chore: Remove unnecessary `pub(super)` in interpreter (#4939)
  feat: add de-sugaring for `impl Trait` in function parameters (#4919)
  feat(experimental): `comptime` globals (#4918)
  chore: update error conversion traits to act on references (#4936)
  fix: ban self-referential structs (#4883)
  chore: add regression test for #3051 (#4815)
  fix: discard ref counts during unrolling (#4923)
  feat!: Bit shift is restricted to u8 right operand (#4907)
  feat: Add `#[inline(tag)]` attribute and codegen (#4913)
  chore: rework workspace structure for utils crates (#4886)
  feat: add variable size sha256 (#4920)
  chore: delete flake.lock (#4855)
  ...
  • Loading branch information
TomAFrench committed May 1, 2024
2 parents 4d4b9a4 + 2e085b9 commit 29069d4
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
31 changes: 26 additions & 5 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,27 @@ fn generate_distinct_return_witnesses(acir: &mut GeneratedAcir) {
acir.return_witnesses = distinct_return_witness;
}

fn generate_distinct_return_witnesses(acir: &mut GeneratedAcir) {
// Create a witness for each return witness we have to guarantee that the return witnesses match the standard
// layout for serializing those types as if they were being passed as inputs.
//
// This is required for recursion as otherwise in situations where we cannot make use of the program's ABI
// (e.g. for `std::verify_proof` or the solidity verifier), we need extra knowledge about the program we're
// working with rather than following the standard ABI encoding rules.
//
// TODO: We're being conservative here by generating a new witness for every expression.
// This means that we're likely to get a number of constraints which are just renumbering witnesses.
// This can be tackled by:
// - Tracking the last assigned public input witness and only renumbering a witness if it is below this value.
// - Modifying existing constraints to rearrange their outputs so they are suitable
// - See: https://github.com/noir-lang/noir/pull/4467
let distinct_return_witness = vecmap(acir.return_witnesses.clone(), |return_witness| {
acir.create_witness_for_expression(&Expression::from(return_witness))
});

acir.return_witnesses = distinct_return_witness;
}

impl<'a> Context<'a> {
fn new(shared_context: &'a mut SharedContext) -> Context<'a> {
let mut acir_context = AcirContext::default();
Expand Down Expand Up @@ -732,11 +753,10 @@ impl<'a> Context<'a> {
assert!(!matches!(inline_type, InlineType::Inline), "ICE: Got an ACIR function named {} that should have already been inlined", func.name());

let inputs = vecmap(arguments, |arg| self.convert_value(*arg, dfg));
// TODO(https://github.com/noir-lang/noir/issues/4608): handle complex return types from ACIR functions
let output_count =
result_ids.iter().fold(0usize, |sum, result_id| {
sum + dfg.try_get_array_length(*result_id).unwrap_or(1)
});
let output_count = result_ids
.iter()
.map(|result_id| dfg.type_of_value(*result_id).flattened_size())
.sum();

let acir_function_id = ssa
.entry_point_to_generated_index
Expand All @@ -748,6 +768,7 @@ impl<'a> Context<'a> {
output_count,
self.current_side_effects_enabled_var,
)?;

let output_values =
self.convert_vars_to_values(output_vars, dfg, result_ids);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "fold_complex_outputs"
type = "bin"
authors = [""]
compiler_version = ">=0.28.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "5"
y = "3"
72 changes: 72 additions & 0 deletions test_programs/execution_success/fold_complex_outputs/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
struct MyStruct {
x: u32,
y: u32,
z: u32,
nested_struct: InnerStruct
}

struct InnerStruct {
small_array: [u32; 2],
big_array: [u32; 5],
}

struct ParentStruct {
basic_array: [Field; 3],
id: u32,
my_structs: [MyStruct; 2],
}

fn main(x: u32, y: pub u32) {
let nested_struct = InnerStruct { small_array: [1 as u32; 2], big_array: [0 as u32; 5] };
let s = MyStruct { x, y, z: x + y, nested_struct };
let parent = ParentStruct { basic_array: [1; 3], id: 100, my_structs: [s, s] };
let new_parent = map_fields(parent);

// Now check that the outputs are as we expect them to be
assert(new_parent.basic_array[0] == 1);
assert(new_parent.basic_array[1] == 18);
assert(new_parent.basic_array[2] == 1);

let struct_0 = new_parent.my_structs[0];
assert(struct_0.x == 5);
assert(struct_0.y == 3);
assert(struct_0.z == 8);
assert(struct_0.nested_struct.small_array == nested_struct.small_array);
assert(struct_0.nested_struct.big_array == nested_struct.big_array);

let struct_1 = new_parent.my_structs[1];
assert(struct_1.x == 50);
assert(struct_1.y == 30);
assert(struct_1.z == 80);
assert(struct_1.nested_struct.small_array == [5, 10]);
assert(struct_1.nested_struct.big_array == [15, 20, 25, 30, 35]);
}

// Meaningless mapping to test whether the values returned are what we expect
#[fold]
fn map_fields(mut input: ParentStruct) -> ParentStruct {
let current_struct = input.my_structs[0];
let mut sum = 0;
for value in current_struct.nested_struct.small_array {
sum += value;
}
for value in current_struct.nested_struct.big_array {
sum += value;
}
sum += (current_struct.x + current_struct.y + current_struct.z);

input.basic_array[1] = sum as Field;

input.my_structs[1].nested_struct.small_array = [5, 10];
input.my_structs[1].nested_struct.big_array = [15, 20, 25, 30, 35];

// LHS input.my_structs[1].x == 50
input.my_structs[1].x = input.my_structs[1].x * 10;
// LHS input.my_structs[1].y == 30
input.my_structs[1].y = input.my_structs[1].y * 10;
// LHS input.my_structs[1].x == 80
input.my_structs[1].z = input.my_structs[1].x + input.my_structs[1].y;

input
}

1 change: 1 addition & 0 deletions tooling/debugger/ignored-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ no_predicates_numeric_generic_poseidon
regression_4709
fold_distinct_return
fold_fibonacci
fold_complex_outputs

0 comments on commit 29069d4

Please sign in to comment.