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

feat: Sync from noir #9034

Merged
merged 25 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3d17e4a
[1 changes] feat(perf): Follow array sets backwards in array set from…
AztecBot Oct 5, 2024
0178e71
chore: apply sync fixes
AztecBot Oct 5, 2024
ebd7820
[1 changes] feat(perf): Follow array sets backwards in array set from…
AztecBot Oct 6, 2024
44a4d46
chore: apply sync fixes
AztecBot Oct 6, 2024
66fcb08
feat(perf): Follow array sets backwards in array set from get optimiz…
AztecBot Oct 6, 2024
7fcceac
[1 changes] feat(perf): Follow array sets backwards in array set from…
AztecBot Oct 7, 2024
414f3d3
chore: apply sync fixes
AztecBot Oct 7, 2024
dfc1444
feat(perf): Follow array sets backwards in array set from get optimiz…
AztecBot Oct 7, 2024
33140b3
[1 changes] feat: add more `Type` and `UnresolvedType` methods (https…
AztecBot Oct 7, 2024
d67386c
chore: apply sync fixes
AztecBot Oct 7, 2024
a256d70
feat: add more `Type` and `UnresolvedType` methods (https://github.co…
AztecBot Oct 7, 2024
484b386
Merge branch 'master' into sync-noir
sirasistant Oct 7, 2024
c17bc36
[1 changes] fix: handle nested arrays in calldata (https://github.com…
AztecBot Oct 7, 2024
1cb2abd
chore: apply sync fixes
AztecBot Oct 7, 2024
4ce6453
fix: handle nested arrays in calldata (https://github.com/noir-lang/n…
AztecBot Oct 7, 2024
fe9b4a4
fix: restore verify honk
sirasistant Oct 7, 2024
104725a
[1 changes] feat: handwritten parser (https://github.com/noir-lang/no…
AztecBot Oct 7, 2024
b47ebaf
chore: apply sync fixes
AztecBot Oct 7, 2024
a8b414c
feat: handwritten parser (https://github.com/noir-lang/noir/pull/6180)
AztecBot Oct 7, 2024
898b6a1
[1 changes] feat: handwritten parser (https://github.com/noir-lang/no…
AztecBot Oct 8, 2024
d16681f
chore: apply sync fixes
AztecBot Oct 8, 2024
cc51963
feat: handwritten parser (https://github.com/noir-lang/noir/pull/6180)
AztecBot Oct 8, 2024
576c7c7
fix: restore deleted tests
sirasistant Oct 8, 2024
b2ca704
update cargo lock
sirasistant Oct 8, 2024
94a8dc4
Merge branch 'master' into sync-noir
sirasistant Oct 8, 2024
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
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c3cb38a7c4de6fc321b367eda3fca6d06e76b77a
0ab8f5e3c32af05a3c158562c0fcf9729741e0ab
47 changes: 31 additions & 16 deletions noir/noir-repo/compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,29 @@ impl<'a> Context<'a> {
}
}

/// Returns the acir value at the provided databus offset
fn get_from_call_data(
&mut self,
offset: &mut AcirVar,
call_data_block: BlockId,
typ: &Type,
) -> Result<AcirValue, RuntimeError> {
match typ {
Type::Numeric(_) => self.array_get_value(&Type::field(), call_data_block, offset),
Type::Array(arc, len) => {
let mut result = Vector::new();
for _i in 0..*len {
for sub_type in arc.iter() {
let element = self.get_from_call_data(offset, call_data_block, sub_type)?;
result.push_back(element);
}
}
Ok(AcirValue::Array(result))
}
_ => unimplemented!("Unsupported type in databus"),
}
}

/// Generates a read opcode for the array
/// `index_side_effect == false` means that we ensured `var_index` will have a type matching the value in the array
fn array_get(
Expand All @@ -1316,27 +1339,19 @@ impl<'a> Context<'a> {
let block_id = self.ensure_array_is_initialized(array, dfg)?;
let results = dfg.instruction_results(instruction);
let res_typ = dfg.type_of_value(results[0]);

// Get operations to call-data parameters are replaced by a get to the call-data-bus array
if let Some(call_data) =
self.data_bus.call_data.iter().find(|cd| cd.index_map.contains_key(&array))
{
let type_size = res_typ.flattened_size();
let type_size = self.acir_context.add_constant(FieldElement::from(type_size as i128));
let offset = self.acir_context.mul_var(var_index, type_size)?;
let call_data =
self.data_bus.call_data.iter().find(|cd| cd.index_map.contains_key(&array)).cloned();
if let Some(call_data) = call_data {
let call_data_block = self.ensure_array_is_initialized(call_data.array_id, dfg)?;
let bus_index = self
.acir_context
.add_constant(FieldElement::from(call_data.index_map[&array] as i128));
let new_index = self.acir_context.add_var(offset, bus_index)?;
return self.array_get(
instruction,
call_data.array_id,
new_index,
dfg,
index_side_effect,
);
let mut current_index = self.acir_context.add_var(bus_index, var_index)?;
let result = self.get_from_call_data(&mut current_index, call_data_block, &res_typ)?;
self.define_result(dfg, instruction, result.clone());
return Ok(result);
}

// Compiler sanity check
assert!(
!res_typ.contains_slice_element(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,24 @@ impl FunctionBuilder {
databus.index += 1;
}
Type::Array(typ, len) => {
assert!(typ.len() == 1, "unsupported composite type");
databus.map.insert(value, databus.index);
for i in 0..len {
// load each element of the array
let index = self
.current_function
.dfg
.make_constant(FieldElement::from(i as i128), Type::length_type());
let element = self.insert_array_get(value, index, typ[0].clone());
self.add_to_data_bus(element, databus);

let mut index = 0;
for _i in 0..len {
for subitem_typ in typ.iter() {
// load each element of the array, and add it to the databus
let index_var = self
.current_function
.dfg
.make_constant(FieldElement::from(index as i128), Type::length_type());
let element = self.insert_array_get(value, index_var, subitem_typ.clone());
index += match subitem_typ {
Type::Array(_, _) | Type::Slice(_) => subitem_typ.element_size(),
Type::Numeric(_) => 1,
_ => unreachable!("Unsupported type for databus"),
};
self.add_to_data_bus(element, databus);
}
}
}
Type::Reference(_) => {
Expand Down
83 changes: 77 additions & 6 deletions noir/noir-repo/compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,25 +818,96 @@ fn try_optimize_array_get_from_previous_set(
SimplifyResult::None
}

/// If we have an array set whose value is from an array get on the same array at the same index,
/// we can simplify that array set to the array we were looking to perform an array set upon.
///
/// Simple case:
/// v3 = array_get v1, index v2
/// v5 = array_set v1, index v2, value v3
///
/// If we could not immediately simplify the array set from its value, we can try to follow
/// the array set backwards in the case we have constant indices:
///
/// v3 = array_get v1, index 1
/// v5 = array_set v1, index 2, value [Field 100, Field 101, Field 102]
/// v7 = array_set mut v5, index 1, value v3
///
/// We want to optimize `v7` to `v5`. We see that `v3` comes from an array get to `v1`. We follow `v5` backwards and see an array set
/// to `v1` and see that the previous array set occurs to a different constant index.
///
/// For each array_set:
/// - If the index is non-constant we fail the optimization since any index may be changed.
/// - If the index is constant and is our target index, we conservatively fail the optimization.
/// - Otherwise, we check the array value of the `array_set`. We will refer to this array as array'.
/// In the case above, array' is `v1` from `v5 = array set ...`
/// - If the original `array_set` value comes from an `array_get`, check the array in that `array_get` against array'.
/// - If the two values are equal we can simplify.
/// - Continuing the example above, as we have `v3 = array_get v1, index 1`, `v1` is
/// what we want to check against array'. We now know we can simplify `v7` to `v5` as it is unchanged.
/// - If they are not equal, recur marking the current `array_set` array as the new array id to use in the checks
fn try_optimize_array_set_from_previous_get(
dfg: &DataFlowGraph,
array_id: ValueId,
mut array_id: ValueId,
target_index: ValueId,
target_value: ValueId,
) -> SimplifyResult {
match &dfg[target_value] {
let array_from_get = match &dfg[target_value] {
Value::Instruction { instruction, .. } => match &dfg[*instruction] {
Instruction::ArrayGet { array, index } => {
if *array == array_id && *index == target_index {
SimplifyResult::SimplifiedTo(array_id)
// If array and index match from the value, we can immediately simplify
return SimplifyResult::SimplifiedTo(array_id);
} else if *index == target_index {
*array
} else {
SimplifyResult::None
return SimplifyResult::None;
}
}
_ => SimplifyResult::None,
_ => return SimplifyResult::None,
},
_ => SimplifyResult::None,
_ => return SimplifyResult::None,
};

// At this point we have determined that the value we are writing in the `array_set` instruction
// comes from an `array_get` from the same index at which we want to write it at.
// It's possible that we're acting on the same array where other indices have been mutated in between
// the `array_get` and `array_set` (resulting in the `array_id` not matching).
//
// We then inspect the set of `array_set`s which which led to the current array the `array_set` is acting on.
// If we can work back to the array on which the `array_get` was reading from without having another `array_set`
// act on the same index then we can be sure that the new `array_set` can be removed without affecting the final result.
let Some(target_index) = dfg.get_numeric_constant(target_index) else {
return SimplifyResult::None;
};

let original_array_id = array_id;
// Arbitrary number of maximum tries just to prevent this optimization from taking too long.
let max_tries = 5;
for _ in 0..max_tries {
match &dfg[array_id] {
Value::Instruction { instruction, .. } => match &dfg[*instruction] {
Instruction::ArraySet { array, index, .. } => {
let Some(index) = dfg.get_numeric_constant(*index) else {
return SimplifyResult::None;
};

if index == target_index {
return SimplifyResult::None;
}

if *array == array_from_get {
return SimplifyResult::SimplifiedTo(original_array_id);
}

array_id = *array; // recur
}
_ => return SimplifyResult::None,
},
_ => return SimplifyResult::None,
}
}

SimplifyResult::None
}

pub(crate) type ErrorType = HirType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"type_as_array" => type_as_array(arguments, return_type, location),
"type_as_constant" => type_as_constant(arguments, return_type, location),
"type_as_integer" => type_as_integer(arguments, return_type, location),
"type_as_mutable_reference" => {
type_as_mutable_reference(arguments, return_type, location)
}
"type_as_slice" => type_as_slice(arguments, return_type, location),
"type_as_str" => type_as_str(arguments, return_type, location),
"type_as_struct" => type_as_struct(arguments, return_type, location),
Expand All @@ -205,14 +208,23 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"type_implements" => type_implements(interner, arguments, location),
"type_is_bool" => type_is_bool(arguments, location),
"type_is_field" => type_is_field(arguments, location),
"type_is_unit" => type_is_unit(arguments, location),
"type_of" => type_of(arguments, location),
"typed_expr_as_function_definition" => {
typed_expr_as_function_definition(interner, arguments, return_type, location)
}
"typed_expr_get_type" => {
typed_expr_get_type(interner, arguments, return_type, location)
}
"unresolved_type_as_mutable_reference" => {
unresolved_type_as_mutable_reference(interner, arguments, return_type, location)
}
"unresolved_type_as_slice" => {
unresolved_type_as_slice(interner, arguments, return_type, location)
}
"unresolved_type_is_bool" => unresolved_type_is_bool(interner, arguments, location),
"unresolved_type_is_field" => unresolved_type_is_field(interner, arguments, location),
"unresolved_type_is_unit" => unresolved_type_is_unit(interner, arguments, location),
"zeroed" => zeroed(return_type),
_ => {
let item = format!("Comptime evaluation for builtin function {name}");
Expand Down Expand Up @@ -855,6 +867,21 @@ fn type_as_integer(
})
}

// fn as_mutable_reference(self) -> Option<Type>
fn type_as_mutable_reference(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
type_as(arguments, return_type, location, |typ| {
if let Type::MutableReference(typ) = typ {
Some(Value::Type(*typ))
} else {
None
}
})
}

// fn as_slice(self) -> Option<Type>
fn type_as_slice(
arguments: Vec<(Value, Location)>,
Expand Down Expand Up @@ -1013,6 +1040,14 @@ fn type_is_field(arguments: Vec<(Value, Location)>, location: Location) -> IResu
Ok(Value::Bool(matches!(typ, Type::FieldElement)))
}

// fn is_unit(self) -> bool
fn type_is_unit(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
let value = check_one_argument(arguments, location)?;
let typ = get_type(value)?;

Ok(Value::Bool(matches!(typ, Type::Unit)))
}

// fn type_of<T>(x: T) -> Type
fn type_of(arguments: Vec<(Value, Location)>, location: Location) -> IResult<Value> {
let (value, _) = check_one_argument(arguments, location)?;
Expand Down Expand Up @@ -1115,6 +1150,49 @@ fn typed_expr_get_type(
option(return_type, option_value)
}

// fn as_mutable_reference(self) -> Option<UnresolvedType>
fn unresolved_type_as_mutable_reference(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
unresolved_type_as(interner, arguments, return_type, location, |typ| {
if let UnresolvedTypeData::MutableReference(typ) = typ {
Some(Value::UnresolvedType(typ.typ))
} else {
None
}
})
}

// fn as_slice(self) -> Option<UnresolvedType>
fn unresolved_type_as_slice(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
unresolved_type_as(interner, arguments, return_type, location, |typ| {
if let UnresolvedTypeData::Slice(typ) = typ {
Some(Value::UnresolvedType(typ.typ))
} else {
None
}
})
}

// fn is_bool(self) -> bool
fn unresolved_type_is_bool(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let typ = get_unresolved_type(interner, self_argument)?;
Ok(Value::Bool(matches!(typ, UnresolvedTypeData::Bool)))
}

// fn is_field(self) -> bool
fn unresolved_type_is_field(
interner: &NodeInterner,
Expand All @@ -1126,6 +1204,36 @@ fn unresolved_type_is_field(
Ok(Value::Bool(matches!(typ, UnresolvedTypeData::FieldElement)))
}

// fn is_unit(self) -> bool
fn unresolved_type_is_unit(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let typ = get_unresolved_type(interner, self_argument)?;
Ok(Value::Bool(matches!(typ, UnresolvedTypeData::Unit)))
}

// Helper function for implementing the `unresolved_type_as_...` functions.
fn unresolved_type_as<F>(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
f: F,
) -> IResult<Value>
where
F: FnOnce(UnresolvedTypeData) -> Option<Value>,
{
let value = check_one_argument(arguments, location)?;
let typ = get_unresolved_type(interner, value)?;

let option_value = f(typ);

option(return_type, option_value)
}

// fn zeroed<T>() -> T
fn zeroed(return_type: Type) -> IResult<Value> {
match return_type {
Expand Down
12 changes: 12 additions & 0 deletions noir/noir-repo/docs/docs/noir/standard_library/meta/typ.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ return the numeric constant.
If this is an integer type, return a boolean which is `true`
if the type is signed, as well as the number of bits of this integer type.

### as_mutable_reference

#include_code as_mutable_reference noir_stdlib/src/meta/typ.nr rust

If this is a mutable reference type `&mut T`, returns the mutable type `T`.

### as_slice

#include_code as_slice noir_stdlib/src/meta/typ.nr rust
Expand Down Expand Up @@ -146,6 +152,12 @@ fn foo<T>() where T: Default {

`true` if this type is `Field`.

### is_unit

#include_code is_unit noir_stdlib/src/meta/typ.nr rust

`true` if this type is the unit `()` type.

## Trait Implementations

```rust
Expand Down
Loading
Loading