-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ssa refactor): Brillig main (#1561)
* remove mac runner * chore: generate brillig opcode for simple identity unconstrained function (#1536) * feat(brillig): added arithmetic operations on brillig (#1565) Co-authored-by: kevaundray <kevtheappdev@gmail.com> * make ranges be polymorphic integers * chore(brillig): Clean up handling of Binary operations (#1571) * chore(ssa refactor): Rename Brillig example (#1563) * chore(brillig): added tests for all field binary operations (#1586) * chore(brillig): added tests for brillig integer operations (#1590) * feat: process blocks and jumps when compiling brillig (#1591) * process jumps between blocks * fix jumps * add doc comments * cargo fmt * code refactor * update code comment --------- Co-authored-by: kevaundray <kevtheappdev@gmail.com> * feat: process blocks and jumps when compiling brillig (#1591) * process jumps between blocks * fix jumps * add doc comments * cargo fmt * code refactor * update code comment --------- Co-authored-by: kevaundray <kevtheappdev@gmail.com> * feat(brillig): parsing oracles/foreign calls (#1596) * feat(brillig): start of oracles/foreign calls * fix: broken tests * Update execute.rs * self.data -> self.vars * chore(brillig): Add handling of the not instruction (#1609) * make behavior consistent * remove closure * change index_type * Update crates/noirc_frontend/src/hir/type_check/expr.rs * feat(brillig): loops (#1610) * make ranges be polymorphic integers * feat: brillig loop support * fix: fixed brillig returns and stop * fix: do not apply constants folding to brillig fns * chore: update acvm pointer, cleanup * style: newline on cargo toml * make behavior consistent * remove closure * change index_type * Update crates/noirc_frontend/src/hir/type_check/expr.rs * better debug information for unsupported instruction * remove edge case for optimizations * clippy fix * patch infinite loop --------- Co-authored-by: kevaundray <kevtheappdev@gmail.com> Co-authored-by: jfecher <jake@aztecprotocol.com> * chore: resolve immutable array merge differences (#1617) * chore(ssa refactor): Switch to immutable arrays (#1578) * Represent SSA arrays with im::Vector * Get tests passing * Implement assign with immutable arrays * Add constant folding pass * Update comments * Clippy * Update comment * Update type of array * Update crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> * Undo formatting changes in instruction.rs * Massive acir_gen update * Refactor acir array operations into a shared function * Appease clippy * Update to_radix and to_bits in acir_gen to return arrays * Disable assert * Fix convert_type for arrays * Include AcirType in AcirValue::Var variant * Fix black box functions * Appease clippy * Fix simple_radix * Add doc comments --------- Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> * feat: Make for-loop range be a polymorphic integer instead of just Field in unconstrained functions (#1583) * make ranges be polymorphic integers * make behavior consistent * remove closure * change index_type * Update crates/noirc_frontend/src/hir/type_check/expr.rs --------- Co-authored-by: jfecher <jake@aztecprotocol.com> * chore(ssa refactor): fix brillig post master merge * chore(ssa refactor): accidental merge undelete --------- Co-authored-by: jfecher <jake@aztecprotocol.com> Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> Co-authored-by: kevaundray <kevtheappdev@gmail.com> * chore(ssa refactor): Add more documentation for truncation (#1607) * add more documentation * small change * Update crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs * Update .github/workflows/test.yml * Update .github/workflows/test.yml --------- Co-authored-by: guipublic <47281315+guipublic@users.noreply.github.com> Co-authored-by: Álvaro Rodríguez <sirasistant@gmail.com> Co-authored-by: ludamad <adam.domurad@gmail.com> Co-authored-by: jfecher <jake@aztecprotocol.com> Co-authored-by: joss-aztec <94053499+joss-aztec@users.noreply.github.com> Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
- Loading branch information
1 parent
77fba56
commit 2cb87dc
Showing
40 changed files
with
912 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_conditional/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_conditional/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x = "1" |
14 changes: 14 additions & 0 deletions
14
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_conditional/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Tests a very simple program. | ||
// | ||
// The features being tested is basic conditonal on brillig | ||
fn main(x: Field) { | ||
assert(4 == conditional(x as bool)); | ||
} | ||
|
||
unconstrained fn conditional(x : bool) -> Field { | ||
if x { | ||
4 | ||
}else { | ||
5 | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_field_binary_operations/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
Empty file.
25 changes: 25 additions & 0 deletions
25
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_field_binary_operations/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Tests arithmetic operations on fields | ||
fn main() { | ||
let x = 4; | ||
let y = 2; | ||
assert((x + y) == add(x, y)); | ||
assert((x - y) == sub(x, y)); | ||
assert((x * y) == mul(x, y)); | ||
assert((x / y) == div(x, y)); | ||
} | ||
|
||
unconstrained fn add(x : Field, y : Field) -> Field { | ||
x + y | ||
} | ||
|
||
unconstrained fn sub(x : Field, y : Field) -> Field { | ||
x - y | ||
} | ||
|
||
unconstrained fn mul(x : Field, y : Field) -> Field { | ||
x * y | ||
} | ||
|
||
unconstrained fn div(x : Field, y : Field) -> Field { | ||
x / y | ||
} |
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_identity_function/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_identity_function/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "3" | ||
|
10 changes: 10 additions & 0 deletions
10
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_identity_function/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Tests a very simple program. | ||
// | ||
// The features being tested is the identity function in Brillig | ||
fn main(x : Field) { | ||
assert(x == identity(x)); | ||
} | ||
|
||
unconstrained fn identity(x : Field) -> Field { | ||
x | ||
} |
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_integer_binary_operations/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
Empty file.
80 changes: 80 additions & 0 deletions
80
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_integer_binary_operations/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Tests arithmetic operations on integers | ||
fn main() { | ||
let x: u32 = 6; | ||
let y: u32 = 2; | ||
|
||
assert((x + y) == add(x, y)); | ||
|
||
assert((x - y) == sub(x, y)); | ||
|
||
assert((x * y) == mul(x, y)); | ||
|
||
assert((x / y) == div(x, y)); | ||
|
||
// TODO SSA => ACIR has some issues with i32 ops | ||
assert(check_signed_div(6, 2, 3)); | ||
|
||
assert(eq(1, 2) == false); | ||
assert(eq(1, 1)); | ||
|
||
assert(lt(x, y) == false); | ||
assert(lt(y, x)); | ||
|
||
assert((x & y) == and(x, y)); | ||
assert((x | y) == or(x, y)); | ||
|
||
// TODO SSA => ACIR has some issues with xor ops | ||
|
||
assert(check_xor(x, y, 4)); | ||
assert((x >> y) == shr(x, y)); | ||
assert((x << y) == shl(x, y)); | ||
} | ||
|
||
unconstrained fn add(x : u32, y : u32) -> u32 { | ||
x + y | ||
} | ||
|
||
unconstrained fn sub(x : u32, y : u32) -> u32 { | ||
x - y | ||
} | ||
|
||
unconstrained fn mul(x : u32, y : u32) -> u32 { | ||
x * y | ||
} | ||
|
||
unconstrained fn div(x : u32, y : u32) -> u32 { | ||
x / y | ||
} | ||
|
||
unconstrained fn check_signed_div(x: i32, y: i32, result: i32) -> bool { | ||
(x / y) == result | ||
} | ||
|
||
unconstrained fn eq(x : u32, y : u32) -> bool { | ||
x == y | ||
} | ||
|
||
unconstrained fn lt(x : u32, y : u32) -> bool { | ||
x < y | ||
} | ||
|
||
unconstrained fn and(x : u32, y : u32) -> u32 { | ||
x & y | ||
} | ||
|
||
unconstrained fn or(x : u32, y : u32) -> u32 { | ||
x | y | ||
} | ||
|
||
unconstrained fn check_xor(x : u32, y : u32, result: u32) -> bool { | ||
(x ^ y) == result | ||
} | ||
|
||
unconstrained fn shr(x : u32, y : u32) -> u32 { | ||
x >> y | ||
} | ||
|
||
unconstrained fn shl(x : u32, y : u32) -> u32 { | ||
x << y | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_loop/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_loop/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sum = "6" |
14 changes: 14 additions & 0 deletions
14
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_loop/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Tests a very simple program. | ||
// | ||
// The features being tested is basic looping on brillig | ||
fn main(sum: u32){ | ||
assert(loop(4) == sum); | ||
} | ||
|
||
unconstrained fn loop(x: u32) -> u32 { | ||
let mut sum = 0; | ||
for i in 0..x { | ||
sum = sum + i; | ||
} | ||
sum | ||
} |
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "1" | ||
y = "0" |
11 changes: 11 additions & 0 deletions
11
crates/nargo_cli/tests/test_data_ssa_refactor/brillig_not/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Tests a very simple Brillig function. | ||
// | ||
// The features being tested is not instruction on brillig | ||
fn main(x: Field, y : Field) { | ||
assert(false == not_operator(x as bool)); | ||
assert(true == not_operator(y as bool)); | ||
} | ||
|
||
unconstrained fn not_operator(x : bool) -> bool { | ||
!x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,98 @@ | ||
use crate::ssa_refactor::ir::basic_block::BasicBlockId; | ||
use acvm::acir::brillig_vm::Opcode as BrilligOpcode; | ||
use std::collections::HashMap; | ||
|
||
/// Pointer to a unresolved Jump instruction in | ||
/// the bytecode. | ||
pub(crate) type JumpLabel = usize; | ||
|
||
/// Pointer to a position in the bytecode where a | ||
/// particular basic block starts. | ||
pub(crate) type BlockLabel = usize; | ||
|
||
#[derive(Default, Debug, Clone)] | ||
/// Artifacts resulting from the compilation of a function into brillig byte code | ||
/// Currently it is just the brillig bytecode of the function | ||
/// Artifacts resulting from the compilation of a function into brillig byte code. | ||
/// Currently it is just the brillig bytecode of the function. | ||
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)>, | ||
/// A map of the basic blocks to their positions | ||
/// in the bytecode. | ||
blocks: HashMap<BasicBlockId, BlockLabel>, | ||
} | ||
|
||
impl BrilligArtifact { | ||
// Link some compiled brillig bytecode with its referenced artifacts | ||
/// Link some compiled brillig bytecode with its referenced artifacts. | ||
pub(crate) fn link(&mut self, obj: &BrilligArtifact) -> Vec<BrilligOpcode> { | ||
self.link_with(obj); | ||
self.resolve_jumps(); | ||
self.byte_code.clone() | ||
} | ||
|
||
// Link with a brillig artifact | ||
/// Link with a brillig artifact | ||
fn link_with(&mut self, obj: &BrilligArtifact) { | ||
if obj.byte_code.is_empty() { | ||
panic!("ICE: unresolved symbol"); | ||
let offset = self.code_len(); | ||
for (jump_label, block_id) in &obj.unresolved_jumps { | ||
self.unresolved_jumps.push((jump_label + offset, *block_id)); | ||
} | ||
|
||
for (block_id, block_label) in &obj.blocks { | ||
self.blocks.insert(*block_id, block_label + offset); | ||
} | ||
|
||
self.byte_code.extend_from_slice(&obj.byte_code); | ||
} | ||
|
||
/// Adds a unresolved jump to be fixed at the end of bytecode processing. | ||
pub(crate) fn add_unresolved_jump(&mut self, destination: BasicBlockId) { | ||
self.unresolved_jumps.push((self.code_len(), destination)); | ||
} | ||
|
||
/// Adds a label in the bytecode to specify where this block's | ||
/// opcodes will start. | ||
pub(crate) fn add_block_label(&mut self, block: BasicBlockId) { | ||
self.blocks.insert(block, self.code_len()); | ||
} | ||
|
||
/// Number of the opcodes currently in the bytecode | ||
pub(crate) fn code_len(&self) -> usize { | ||
self.byte_code.len() | ||
} | ||
|
||
/// Resolves all of the unresolved jumps in the program. | ||
/// | ||
/// Note: This should only be called once all blocks are processed. | ||
fn resolve_jumps(&mut self) { | ||
for (jump_label, block) in &self.unresolved_jumps { | ||
let jump_instruction = self.byte_code[*jump_label].clone(); | ||
|
||
let actual_block_location = self.blocks[block]; | ||
|
||
match jump_instruction { | ||
BrilligOpcode::Jump { location } => { | ||
assert_eq!(location, 0, "location is not zero, which means that the jump label does not need resolving"); | ||
|
||
self.byte_code[*jump_label] = | ||
BrilligOpcode::Jump { location: actual_block_location }; | ||
} | ||
BrilligOpcode::JumpIfNot { condition, location } => { | ||
assert_eq!(location, 0, "location is not zero, which means that the jump label does not need resolving"); | ||
|
||
self.byte_code[*jump_label] = | ||
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"); | ||
|
||
self.byte_code[*jump_label] = | ||
BrilligOpcode::JumpIf { condition, location: actual_block_location }; | ||
} | ||
_ => unreachable!( | ||
"all jump labels should point to a jump instruction in the bytecode" | ||
), | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.