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

chore(brillig): Add handling of the not instruction #1609

Merged
merged 3 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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,2 @@
x = "1"
y = "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 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
}
59 changes: 54 additions & 5 deletions crates/noirc_evaluator/src/brillig/brillig_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ use crate::ssa_refactor::ir::{
types::{NumericType, Type},
value::{Value, ValueId},
};
use acvm::acir::brillig_vm::{
BinaryFieldOp, BinaryIntOp, Opcode as BrilligOpcode, RegisterIndex, Value as BrilligValue,
use acvm::{
acir::brillig_vm::{
BinaryFieldOp, BinaryIntOp, Opcode as BrilligOpcode, RegisterIndex, Value as BrilligValue,
},
FieldElement,
};
use std::collections::HashMap;

Expand Down Expand Up @@ -37,11 +40,23 @@ impl BrilligGen {
return *register_index;
}

let register = RegisterIndex::from(self.latest_register);
let register = self.create_register();

// Cache the `ValueId` so that if we call it again, it will
// return the register that has just been created.
//
// WARNING: This assumes that a register has not been
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty sneaky, especially with function calls which may overwrite registers using MOV. MOV instruction does not update the cache

Copy link
Collaborator

@ludamad ludamad Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this apply right now when we use a unique register for everything + no recursion? Eventually we'll need a register allocator. Ideally we revisit the exacts of registers when the public VM is partially working, though, as we already had some disquiet with the plans we made with limited registers etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With no recursion this should be okay, the edge case to consider is function calls which may overwrite registers for the function that called it. I opened up a milestone for function to function call, so this should be exposed there

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye so I think in order we should

  1. link functions so that registers are all unique
  2. do function to function calls without recursion
  3. wait for public vm progress
  4. do register allocator guided by public VM optimization that tracks register reuse properly (caching to memory and back)
  5. do recursion

// modified. If a MOV instruction has overwritten the value
// at a register, then this cache will be invalid.
self.ssa_value_to_register.insert(value, register);

self.latest_register += 1;
register
}

/// Creates a new register.
fn create_register(&mut self) -> RegisterIndex {
let register = RegisterIndex::from(self.latest_register);
self.latest_register += 1;
register
}

Expand Down Expand Up @@ -146,10 +161,44 @@ impl BrilligGen {
let result_register = self.get_or_create_register(result_ids[0]);
self.convert_ssa_binary(binary, dfg, result_register);
}
_ => todo!("ICE: Instruction not supported"),
Instruction::Not(value) => {
let result_ids = dfg.instruction_results(instruction_id);
let result_register = self.get_or_create_register(result_ids[0]);

assert_eq!(
dfg.type_of_value(*value),
Type::bool(),
"not operator can only be applied to boolean values"
);

let one = self.make_constant(FieldElement::one());
let condition = self.convert_ssa_value(*value, dfg);

// Compile !x as (1 - x)
let opcode = BrilligOpcode::BinaryIntOp {
destination: result_register,
op: BinaryIntOp::Sub,
bit_size: 1,
lhs: one,
rhs: condition,
};
self.push_code(opcode)
}
_ => todo!("ICE: Instruction not supported {instruction:?}"),
};
}

/// Returns a register which holds the value of a constant
fn make_constant(&mut self, constant: FieldElement) -> RegisterIndex {
let register = self.create_register();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note @vezenovm if we use Const consistently, the only registers we should be passing as an input to brillig are the input args (just flagging as relevant to documentation)


let const_opcode =
BrilligOpcode::Const { destination: register, value: BrilligValue::from(constant) };
self.push_code(const_opcode);

register
}

/// Converts the Binary instruction into a sequence of Brillig opcodes.
fn convert_ssa_binary(
&mut self,
Expand Down