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: add support for assert in brillig #1603

Merged
merged 35 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6184f52
remove mac runner
kevaundray Jun 6, 2023
823bd84
chore: generate brillig opcode for simple identity unconstrained func…
guipublic Jun 6, 2023
545d333
feat(brillig): added arithmetic operations on brillig (#1565)
sirasistant Jun 6, 2023
f6810af
make ranges be polymorphic integers
kevaundray Jun 6, 2023
c5e34c5
chore(brillig): Clean up handling of Binary operations (#1571)
kevaundray Jun 6, 2023
06543b5
chore(ssa refactor): Rename Brillig example (#1563)
kevaundray Jun 6, 2023
3c9f106
chore(brillig): added tests for all field binary operations (#1586)
sirasistant Jun 7, 2023
9a9c461
chore(brillig): added tests for brillig integer operations (#1590)
sirasistant Jun 7, 2023
043c3c4
feat: process blocks and jumps when compiling brillig (#1591)
guipublic Jun 7, 2023
e51d3c6
feat: process blocks and jumps when compiling brillig (#1591)
guipublic Jun 7, 2023
cd39144
feat(brillig): parsing oracles/foreign calls (#1596)
ludamad Jun 7, 2023
7743551
support assert in brillig
guipublic Jun 8, 2023
cf393ff
Merge remote-tracking branch 'origin/master' into kw/brillig-main
kevaundray Jun 8, 2023
4644da9
self.data -> self.vars
kevaundray Jun 8, 2023
d50c69f
Merge remote-tracking branch 'origin/master' into kw/brillig-main
kevaundray Jun 8, 2023
89ad1e2
Merge branch 'kw/brillig-main' into gd/brillig-assert
kevaundray Jun 8, 2023
265e04f
Avoid not in the test
guipublic Jun 8, 2023
398ddf9
chore(brillig): Add handling of the not instruction (#1609)
kevaundray Jun 8, 2023
1d52f5e
make behavior consistent
kevaundray Jun 8, 2023
2d3ab61
remove closure
kevaundray Jun 8, 2023
801a739
change index_type
kevaundray Jun 8, 2023
2bcfc24
Update crates/noirc_frontend/src/hir/type_check/expr.rs
jfecher Jun 8, 2023
49a151d
Merge remote-tracking branch 'origin/master' into kw/brillig-main
kevaundray Jun 8, 2023
5247a48
Merge remote-tracking branch 'origin/kw/polymorphic-integers-on-for-l…
kevaundray Jun 8, 2023
5995b30
feat(brillig): loops (#1610)
sirasistant Jun 8, 2023
1a9d33c
chore: resolve immutable array merge differences (#1617)
joss-aztec Jun 9, 2023
2787cc9
chore(ssa refactor): Add more documentation for truncation (#1607)
kevaundray Jun 9, 2023
8713a89
Merge remote-tracking branch 'origin/master' into kw/brillig-main
kevaundray Jun 9, 2023
70f8fe2
Update .github/workflows/test.yml
kevaundray Jun 9, 2023
91defbc
Update .github/workflows/test.yml
kevaundray Jun 9, 2023
17227e4
Merge remote-tracking branch 'origin/kw/brillig-main' into gd/brillig…
kevaundray Jun 9, 2023
554a3eb
Remove optimisation for handling assert
guipublic Jun 9, 2023
9b41216
add the assert jump to the list of jumps to fix
guipublic Jun 9, 2023
1b6f900
Merge branch 'kw/brillig-main' into gd/brillig-assert
kevaundray Jun 9, 2023
12e83a2
add doc comment
kevaundray Jun 9, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Tests a very simple program.
//
// The features being tested is using assert on brillig
fn main(x: Field) {
assert(1 == conditional(x as bool));
}

unconstrained fn conditional(x : bool) -> Field {
assert(x);
1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Tests a very simple program.
//
// The features being tested is using assert on brillig
fn main(x: Field) {
assert(1 == conditional(x as bool));
}

unconstrained fn conditional(x : bool) -> Field {
assert(x);
1
}
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data_ssa_refactor/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ exclude = []


# List of tests (as their directory name) expecting to fail: if the test pass, we report an error.
fail = [""]
fail = ["brillig_assert_fail"]
35 changes: 30 additions & 5 deletions crates/noirc_evaluator/src/brillig/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,32 @@ pub(crate) struct BrilligArtifact {
pub(crate) byte_code: Vec<BrilligOpcode>,
/// The set of jumps that need to have their locations
/// resolved.
unresolved_jumps: Vec<(JumpLabel, BasicBlockId)>,
unresolved_jumps: Vec<(JumpLabel, UnresolvedJumpLocation)>,
/// A map of the basic blocks to their positions
/// in the bytecode.
blocks: HashMap<BasicBlockId, BlockLabel>,
}

/// When constructing the bytecode, there may be instructions
/// which require one to jump to a specific `Block`
/// or a position relative to the current instruction.
///
/// The position of a `Block` cannot always be known
/// at this point in time, so Jumps are unresolved
/// until all blocks have been processed in the `Block`
/// variant of this enum.
///
/// Sometimes the relative position of an Jump
/// may be known, from the Jump label, but since
/// the absolute position of a Jump label is not known until
/// after we have linked the bytecode to other functions.
/// We add relative jumps into the `Relative` variant of this enum.
#[derive(Debug, Clone, Copy)]
pub(crate) enum UnresolvedJumpLocation {
Block(BasicBlockId),
Relative(i32),
}

impl BrilligArtifact {
/// Link some compiled brillig bytecode with its referenced artifacts.
pub(crate) fn link(&mut self, obj: &BrilligArtifact) -> Vec<BrilligOpcode> {
Expand All @@ -46,7 +66,7 @@ impl BrilligArtifact {
}

/// Adds a unresolved jump to be fixed at the end of bytecode processing.
pub(crate) fn add_unresolved_jump(&mut self, destination: BasicBlockId) {
pub(crate) fn add_unresolved_jump(&mut self, destination: UnresolvedJumpLocation) {
self.unresolved_jumps.push((self.code_len(), destination));
}

Expand All @@ -65,10 +85,15 @@ impl BrilligArtifact {
///
/// Note: This should only be called once all blocks are processed.
fn resolve_jumps(&mut self) {
for (jump_label, block) in &self.unresolved_jumps {
for (jump_label, unresolved_location) in &self.unresolved_jumps {
let jump_instruction = self.byte_code[*jump_label].clone();

let actual_block_location = self.blocks[block];
let actual_block_location = match unresolved_location {
UnresolvedJumpLocation::Block(b) => self.blocks[b],
UnresolvedJumpLocation::Relative(location) => {
(location + *jump_label as i32) as usize
}
};

match jump_instruction {
BrilligOpcode::Jump { location } => {
Expand All @@ -84,7 +109,7 @@ impl BrilligArtifact {
BrilligOpcode::JumpIfNot { condition, location: actual_block_location };
}
BrilligOpcode::JumpIf { condition, location } => {
assert_eq!(location, 0,"location is not zero, which means that the jump label does not need resolving");
assert_eq!(location, 0, "location is not zero, which means that the jump label does not need resolving");

self.byte_code[*jump_label] =
BrilligOpcode::JumpIf { condition, location: actual_block_location };
Expand Down
13 changes: 10 additions & 3 deletions crates/noirc_evaluator/src/brillig/brillig_gen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
artifact::BrilligArtifact,
artifact::{BrilligArtifact, UnresolvedJumpLocation},
binary::{type_of_binary_operation, BrilligBinaryOp},
memory::BrilligMemory,
};
Expand Down Expand Up @@ -101,13 +101,13 @@ impl BrilligGen {

/// Adds a unresolved `Jump` instruction to the bytecode.
fn jump(&mut self, target: BasicBlockId) {
self.obj.add_unresolved_jump(target);
self.obj.add_unresolved_jump(UnresolvedJumpLocation::Block(target));
self.push_code(BrilligOpcode::Jump { location: 0 });
}

/// Adds a unresolved `JumpIf` instruction to the bytecode.
fn jump_if(&mut self, condition: RegisterIndex, target: BasicBlockId) {
self.obj.add_unresolved_jump(target);
self.obj.add_unresolved_jump(UnresolvedJumpLocation::Block(target));
self.push_code(BrilligOpcode::JumpIf { condition, location: 0 });
}

Expand Down Expand Up @@ -160,6 +160,13 @@ impl BrilligGen {
let result_register = self.get_or_create_register(result_ids[0]);
self.convert_ssa_binary(binary, dfg, result_register);
}
Instruction::Constrain(value) => {
let condition = self.convert_ssa_value(*value, dfg);
// jump to the relative location after the trap
self.obj.add_unresolved_jump(UnresolvedJumpLocation::Relative(2));
self.push_code(BrilligOpcode::JumpIf { condition, location: 0 });
self.push_code(BrilligOpcode::Trap);
}
Instruction::Allocate => {
let pointer_register =
self.get_or_create_register(dfg.instruction_results(instruction_id)[0]);
Expand Down