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 #9769

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
a2b7fef
[1 changes] fix: let formatter respect newlines between comments (htt…
AztecBot Nov 6, 2024
a3ec50b
chore: apply sync fixes
AztecBot Nov 6, 2024
5c83542
[1 changes] fix: Discard optimisation that would change execution ord…
AztecBot Nov 7, 2024
c9247ac
chore: apply sync fixes
AztecBot Nov 7, 2024
173891c
fix: Discard optimisation that would change execution ordering or tha…
AztecBot Nov 7, 2024
85c2dde
[1 changes] chore: update example to show how to split public inputs …
AztecBot Nov 8, 2024
98f5f3f
chore: apply sync fixes
AztecBot Nov 8, 2024
5a03b89
chore: update example to show how to split public inputs in bash (htt…
AztecBot Nov 8, 2024
585e0a6
[1 changes] fix(docs): Fix broken links in oracles doc (https://githu…
AztecBot Nov 9, 2024
a3a3c01
chore: apply sync fixes
AztecBot Nov 9, 2024
7ca18a3
fix(docs): Fix broken links in oracles doc (https://github.com/noir-l…
AztecBot Nov 9, 2024
1cb1751
[1 changes] fix(docs): Fix broken links in oracles doc (https://githu…
AztecBot Nov 10, 2024
cbd6f07
chore: apply sync fixes
AztecBot Nov 10, 2024
a1d673f
fix(docs): Fix broken links in oracles doc (https://github.com/noir-l…
AztecBot Nov 10, 2024
b1a02a5
[1 changes] fix(docs): Fix broken links in oracles doc (https://githu…
AztecBot Nov 11, 2024
2c12eaa
chore: apply sync fixes
AztecBot Nov 11, 2024
00d16d6
fix(docs): Fix broken links in oracles doc (https://github.com/noir-l…
AztecBot Nov 11, 2024
ea98ac0
.
TomAFrench Nov 11, 2024
ddbbeb8
Merge branch 'master' into sync-noir
TomAFrench Nov 11, 2024
6cf4806
.
TomAFrench Nov 11, 2024
5834f67
.
TomAFrench Nov 11, 2024
fadb62c
.
TomAFrench Nov 11, 2024
2a24b4c
.
TomAFrench Nov 11, 2024
7639753
.
TomAFrench Nov 11, 2024
d5ae61c
[1 changes] feat: Deduplicate instructions across blocks (https://git…
AztecBot Nov 12, 2024
e03d9fc
chore: apply sync fixes
AztecBot Nov 12, 2024
6b6263f
feat: Deduplicate instructions across blocks (https://github.com/noir…
AztecBot Nov 12, 2024
f7b7892
Merge branch 'master' into sync-noir
TomAFrench Nov 12, 2024
cd9b001
.
TomAFrench Nov 12, 2024
89d66fe
[1 changes] fix: perform arithmetic simplification through `CheckedCa…
AztecBot Nov 12, 2024
eabf1fd
chore: apply sync fixes
AztecBot Nov 12, 2024
90adecd
fix: perform arithmetic simplification through `CheckedCast` (https:/…
AztecBot Nov 12, 2024
fc2b7a1
.
TomAFrench Nov 12, 2024
914af18
.
TomAFrench Nov 12, 2024
fad3191
.
TomAFrench Nov 11, 2024
614519e
.
TomAFrench Nov 11, 2024
aeff2ad
.
TomAFrench Nov 12, 2024
300b61c
.
TomAFrench Nov 12, 2024
457666c
.
TomAFrench Nov 12, 2024
90bcfe7
Merge branch 'master' into sync-noir
TomAFrench Nov 12, 2024
cf01d9d
.
TomAFrench Nov 12, 2024
6470736
Update noir/noir-repo/compiler/noirc_evaluator/src/ssa/function_build…
TomAFrench Nov 12, 2024
31f1dda
[1 changes] fix: set local_module before elaborating each trait (http…
AztecBot Nov 13, 2024
1c072e4
chore: apply sync fixes
AztecBot Nov 13, 2024
04d78e9
fix: set local_module before elaborating each trait (https://github.c…
AztecBot Nov 13, 2024
25fcc81
Merge branch 'master' into sync-noir
TomAFrench Nov 13, 2024
413d803
.
TomAFrench Nov 13, 2024
3762ff1
Merge branch 'master' into sync-noir
TomAFrench Nov 13, 2024
da1c13d
Merge branch 'master' into sync-noir
TomAFrench Nov 13, 2024
32e2fe6
Merge branch 'master' into sync-noir
TomAFrench Nov 13, 2024
c1b7295
[1 changes] chore: Parse negatives in SSA parser (https://github.com/…
AztecBot Nov 14, 2024
70667ce
chore: apply sync fixes
AztecBot Nov 14, 2024
fdededb
chore: Parse negatives in SSA parser (https://github.com/noir-lang/no…
AztecBot Nov 14, 2024
22cea25
[1 changes] fix: disallow `#[test]` on associated functions (https://…
AztecBot Nov 14, 2024
d7663ef
chore: apply sync fixes
AztecBot Nov 14, 2024
550b3aa
fix: disallow `#[test]` on associated functions (https://github.com/n…
AztecBot Nov 14, 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 @@
13856a121125b1ccca15919942081a5d157d280e
35408ab303f1018c1e2c38e6ea55430a2c89dc4c
26 changes: 25 additions & 1 deletion noir/noir-repo/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ mod tests {
public_parameters: PublicInputs::default(),
return_values: PublicInputs::default(),
assert_messages: Default::default(),
recursive: false,
};
check_circuit(circuit);
}
Expand Down Expand Up @@ -347,6 +348,7 @@ mod tests {
public_parameters: PublicInputs::default(),
return_values: PublicInputs::default(),
assert_messages: Default::default(),
recursive: false,
};
check_circuit(circuit);
}
Expand Down
1 change: 1 addition & 0 deletions noir/noir-repo/acvm-repo/acvm/src/compiler/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ mod tests {
public_parameters,
return_values: PublicInputs::default(),
assert_messages: Default::default(),
recursive: false,
}
}

Expand Down
41 changes: 25 additions & 16 deletions noir/noir-repo/compiler/noirc_driver/src/abi_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use iter_extended::vecmap;
use noirc_abi::{
Abi, AbiErrorType, AbiParameter, AbiReturnType, AbiType, AbiValue, AbiVisibility, Sign,
};
use noirc_evaluator::ErrorType;
use noirc_errors::Span;
use noirc_frontend::ast::{Signedness, Visibility};
use noirc_frontend::TypeBinding;
use noirc_frontend::{
Expand Down Expand Up @@ -40,20 +40,26 @@ pub(super) fn gen_abi(
Abi { parameters, return_type, error_types }
}

fn build_abi_error_type(context: &Context, typ: ErrorType) -> AbiErrorType {
// Get the Span of the root crate's main function, or else a dummy span if that fails
fn get_main_function_span(context: &Context) -> Span {
if let Some(func_id) = context.get_main_function(context.root_crate_id()) {
context.function_meta(&func_id).location.span
} else {
Span::default()
}
}

fn build_abi_error_type(context: &Context, typ: &Type) -> AbiErrorType {
match typ {
ErrorType::Dynamic(typ) => {
if let Type::FmtString(len, item_types) = typ {
let length = len.evaluate_to_u32().expect("Cannot evaluate fmt length");
let Type::Tuple(item_types) = item_types.as_ref() else {
unreachable!("FmtString items must be a tuple")
};
let item_types =
item_types.iter().map(|typ| abi_type_from_hir_type(context, typ)).collect();
AbiErrorType::FmtString { length, item_types }
} else {
AbiErrorType::Custom(abi_type_from_hir_type(context, &typ))
}
Type::FmtString(len, item_types) => {
let span = get_main_function_span(context);
let length = len.evaluate_to_u32(span).expect("Cannot evaluate fmt length");
let Type::Tuple(item_types) = item_types.as_ref() else {
unreachable!("FmtString items must be a tuple")
};
let item_types =
item_types.iter().map(|typ| abi_type_from_hir_type(context, typ)).collect();
AbiErrorType::FmtString { length, item_types }
}
ErrorType::String(string) => AbiErrorType::String { string },
}
Expand All @@ -63,8 +69,9 @@ pub(super) fn abi_type_from_hir_type(context: &Context, typ: &Type) -> AbiType {
match typ {
Type::FieldElement => AbiType::Field,
Type::Array(size, typ) => {
let span = get_main_function_span(context);
let length = size
.evaluate_to_u32()
.evaluate_to_u32(span)
.expect("Cannot have variable sized arrays as a parameter to main");
let typ = typ.as_ref();
AbiType::Array { length, typ: Box::new(abi_type_from_hir_type(context, typ)) }
Expand All @@ -91,8 +98,9 @@ pub(super) fn abi_type_from_hir_type(context: &Context, typ: &Type) -> AbiType {
}
Type::Bool => AbiType::Boolean,
Type::String(size) => {
let span = get_main_function_span(context);
let size = size
.evaluate_to_u32()
.evaluate_to_u32(span)
.expect("Cannot have variable sized strings as a parameter to main");
AbiType::String { length: size }
}
Expand All @@ -107,6 +115,7 @@ pub(super) fn abi_type_from_hir_type(context: &Context, typ: &Type) -> AbiType {
AbiType::Struct { fields, path }
}
Type::Alias(def, args) => abi_type_from_hir_type(context, &def.borrow().get_type(args)),
Type::CheckedCast { to, .. } => abi_type_from_hir_type(context, to),
Type::Tuple(fields) => {
let fields = vecmap(fields, |typ| abi_type_from_hir_type(context, typ));
AbiType::Tuple { fields }
Expand Down
5 changes: 2 additions & 3 deletions noir/noir-repo/compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,8 @@ fn compile_contract_inner(
.secondary
.iter()
.filter_map(|attr| match attr {
SecondaryAttribute::Tag(attribute) | SecondaryAttribute::Meta(attribute) => {
Some(attribute.contents.clone())
}
SecondaryAttribute::Tag(attribute) => Some(attribute.contents.clone()),
SecondaryAttribute::Meta(attribute) => Some(attribute.to_string()),
_ => None,
})
.collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::brillig::brillig_ir::artifact::Label;
use crate::brillig::brillig_ir::brillig_variable::{
type_to_heap_value_type, BrilligArray, BrilligVariable, SingleAddrVariable,
};

use crate::brillig::brillig_ir::registers::Stack;
use crate::brillig::brillig_ir::{
BrilligBinaryOp, BrilligContext, ReservedRegisters, BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
Expand Down Expand Up @@ -1202,7 +1203,7 @@ impl<'block> BrilligBlock<'block> {
let brillig_binary_op = match binary.operator {
BinaryOp::Div => {
if is_signed {
self.convert_signed_division(left, right, result_variable);
self.brillig_context.convert_signed_division(left, right, result_variable);
return;
} else if is_field {
BrilligBinaryOp::FieldDiv
Expand Down Expand Up @@ -1234,7 +1235,14 @@ impl<'block> BrilligBlock<'block> {
BinaryOp::Or => BrilligBinaryOp::Or,
BinaryOp::Xor => BrilligBinaryOp::Xor,
BinaryOp::Shl => BrilligBinaryOp::Shl,
BinaryOp::Shr => BrilligBinaryOp::Shr,
BinaryOp::Shr => {
if is_signed {
self.convert_signed_shr(left, right, result_variable);
return;
} else {
BrilligBinaryOp::Shr
}
}
};

self.brillig_context.binary_instruction(left, right, result_variable, brillig_binary_op);
Expand All @@ -1250,98 +1258,6 @@ impl<'block> BrilligBlock<'block> {
);
}

/// Splits a two's complement signed integer in the sign bit and the absolute value.
/// For example, -6 i8 (11111010) is split to 00000110 (6, absolute value) and 1 (is_negative).
fn absolute_value(
&mut self,
num: SingleAddrVariable,
absolute_value: SingleAddrVariable,
result_is_negative: SingleAddrVariable,
) {
let max_positive = self
.brillig_context
.make_constant_instruction(((1_u128 << (num.bit_size - 1)) - 1).into(), num.bit_size);

// Compute if num is negative
self.brillig_context.binary_instruction(
max_positive,
num,
result_is_negative,
BrilligBinaryOp::LessThan,
);

// Two's complement of num
let zero = self.brillig_context.make_constant_instruction(0_usize.into(), num.bit_size);
let twos_complement =
SingleAddrVariable::new(self.brillig_context.allocate_register(), num.bit_size);
self.brillig_context.binary_instruction(zero, num, twos_complement, BrilligBinaryOp::Sub);

// absolute_value = result_is_negative ? twos_complement : num
self.brillig_context.codegen_branch(result_is_negative.address, |ctx, is_negative| {
if is_negative {
ctx.mov_instruction(absolute_value.address, twos_complement.address);
} else {
ctx.mov_instruction(absolute_value.address, num.address);
}
});

self.brillig_context.deallocate_single_addr(zero);
self.brillig_context.deallocate_single_addr(max_positive);
self.brillig_context.deallocate_single_addr(twos_complement);
}

fn convert_signed_division(
&mut self,
left: SingleAddrVariable,
right: SingleAddrVariable,
result: SingleAddrVariable,
) {
let left_is_negative = SingleAddrVariable::new(self.brillig_context.allocate_register(), 1);
let left_abs_value =
SingleAddrVariable::new(self.brillig_context.allocate_register(), left.bit_size);

let right_is_negative =
SingleAddrVariable::new(self.brillig_context.allocate_register(), 1);
let right_abs_value =
SingleAddrVariable::new(self.brillig_context.allocate_register(), right.bit_size);

let result_is_negative =
SingleAddrVariable::new(self.brillig_context.allocate_register(), 1);

// Compute both absolute values
self.absolute_value(left, left_abs_value, left_is_negative);
self.absolute_value(right, right_abs_value, right_is_negative);

// Perform the division on the absolute values
self.brillig_context.binary_instruction(
left_abs_value,
right_abs_value,
result,
BrilligBinaryOp::UnsignedDiv,
);

// Compute result sign
self.brillig_context.binary_instruction(
left_is_negative,
right_is_negative,
result_is_negative,
BrilligBinaryOp::Xor,
);

// If result has to be negative, perform two's complement
self.brillig_context.codegen_if(result_is_negative.address, |ctx| {
let zero = ctx.make_constant_instruction(0_usize.into(), result.bit_size);
ctx.binary_instruction(zero, result, result, BrilligBinaryOp::Sub);
ctx.deallocate_single_addr(zero);
});

self.brillig_context.deallocate_single_addr(left_is_negative);
self.brillig_context.deallocate_single_addr(left_abs_value);
self.brillig_context.deallocate_single_addr(right_is_negative);
self.brillig_context.deallocate_single_addr(right_abs_value);
self.brillig_context.deallocate_single_addr(result_is_negative);
}

fn convert_signed_modulo(
&mut self,
left: SingleAddrVariable,
Expand All @@ -1354,7 +1270,7 @@ impl<'block> BrilligBlock<'block> {
SingleAddrVariable::new(self.brillig_context.allocate_register(), left.bit_size);

// i = left / right
self.convert_signed_division(left, right, scratch_var_i);
self.brillig_context.convert_signed_division(left, right, scratch_var_i);

// j = i * right
self.brillig_context.binary_instruction(
Expand Down Expand Up @@ -1401,6 +1317,56 @@ impl<'block> BrilligBlock<'block> {
self.brillig_context.deallocate_single_addr(bias);
}

fn convert_signed_shr(
&mut self,
left: SingleAddrVariable,
right: SingleAddrVariable,
result: SingleAddrVariable,
) {
// Check if left is negative
let left_is_negative = SingleAddrVariable::new(self.brillig_context.allocate_register(), 1);
let max_positive = self
.brillig_context
.make_constant_instruction(((1_u128 << (left.bit_size - 1)) - 1).into(), left.bit_size);
self.brillig_context.binary_instruction(
max_positive,
left,
left_is_negative,
BrilligBinaryOp::LessThan,
);

self.brillig_context.codegen_branch(left_is_negative.address, |ctx, is_negative| {
if is_negative {
let one = ctx.make_constant_instruction(1_u128.into(), left.bit_size);

// computes 2^right
let two = ctx.make_constant_instruction(2_u128.into(), left.bit_size);
let two_pow = ctx.make_constant_instruction(1_u128.into(), left.bit_size);
let right_u32 = SingleAddrVariable::new(ctx.allocate_register(), 32);
ctx.cast(right_u32, right);
let pow_body = |ctx: &mut BrilligContext<_, _>, _: SingleAddrVariable| {
ctx.binary_instruction(two_pow, two, two_pow, BrilligBinaryOp::Mul);
};
ctx.codegen_for_loop(None, right_u32.address, None, pow_body);

// Right shift using division on 1-complement
ctx.binary_instruction(left, one, result, BrilligBinaryOp::Add);
ctx.convert_signed_division(result, two_pow, result);
ctx.binary_instruction(result, one, result, BrilligBinaryOp::Sub);

// Clean-up
ctx.deallocate_single_addr(one);
ctx.deallocate_single_addr(two);
ctx.deallocate_single_addr(two_pow);
ctx.deallocate_single_addr(right_u32);
} else {
ctx.binary_instruction(left, right, result, BrilligBinaryOp::Shr);
}
});

self.brillig_context.deallocate_single_addr(left_is_negative);
}

#[allow(clippy::too_many_arguments)]
fn add_overflow_check(
&mut self,
Expand Down
Loading
Loading