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

fix: initialise arrays returned by brillig #2048

Merged
merged 11 commits into from
Sep 11, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "brillig_unitialised_arrays"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
y = 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

fn main(x: Field, y: Field) -> pub Field {
let notes = create_notes(x, y);
sum_x(notes, x, y)
}

fn sum_x(notes: [Field; 2], x: Field, y: Field) -> Field {
notes[x] + notes[y]
}

unconstrained fn create_notes(x: Field, y: Field) -> [Field; 2] {
[x,y]
}
22 changes: 17 additions & 5 deletions crates/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,19 +1206,31 @@ impl AcirContext {
) -> Result<(), InternalError> {
// If the optional values are supplied, then we fill the initialized
// array with those values. If not, then we fill it with zeros.
let mut nested = false;
let initialized_values = match optional_values {
None => {
let zero = self.add_constant(FieldElement::zero());
let zero_witness = self.var_to_witness(zero)?;
vec![zero_witness; len]
}
Some(optional_values) => try_vecmap(optional_values, |value| {
let value = value.clone().into_var()?;
self.var_to_witness(value)
})?,
Some(optional_values) => {
let mut values = Vec::new();
for value in optional_values {
if let Ok(some_value) = value.clone().into_var() {
values.push(self.var_to_witness(some_value)?);
} else {
nested = true;
break;
}
}
values
}
};
// we do not initialize nested arrays. This means that non-const indexes are not supported for nested arrays
if !nested {
self.acir_ir.push_opcode(Opcode::MemoryInit { block_id, init: initialized_values });
}

self.acir_ir.push_opcode(Opcode::MemoryInit { block_id, init: initialized_values });
Ok(())
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ impl Context {
assert_eq!(result_ids.len(), output_values.len(), "ICE: The number of Brillig output values should match the result ids in SSA");

for result in result_ids.iter().zip(output_values) {
if let AcirValue::Array(values) = &result.1 {
let block_id = self.block_id(&dfg.resolve(*result.0));
let values: Vec<AcirValue> = values.iter().cloned().collect();
self.initialize_array(block_id, values.len(), Some(&values))?;
}
self.ssa_values.insert(*result.0, result.1);
}
}
Expand Down