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!: specify databus arrays for BB #6239

Merged
merged 10 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Expand Up @@ -454,6 +454,7 @@ struct Opcode {
struct MemoryInit {
Program::BlockId block_id;
std::vector<Program::Witness> init;
uint8_t databus;

friend bool operator==(const MemoryInit&, const MemoryInit&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down Expand Up @@ -7824,6 +7825,9 @@ inline bool operator==(const Opcode::MemoryInit& lhs, const Opcode::MemoryInit&
if (!(lhs.init == rhs.init)) {
return false;
}
if (!(lhs.databus == rhs.databus)) {
return false;
}
return true;
}

Expand Down Expand Up @@ -7853,6 +7857,7 @@ void serde::Serializable<Program::Opcode::MemoryInit>::serialize(const Program::
{
serde::Serializable<decltype(obj.block_id)>::serialize(obj.block_id, serializer);
serde::Serializable<decltype(obj.init)>::serialize(obj.init, serializer);
serde::Serializable<decltype(obj.databus)>::serialize(obj.databus, serializer);
}

template <>
Expand All @@ -7862,6 +7867,7 @@ Program::Opcode::MemoryInit serde::Deserializable<Program::Opcode::MemoryInit>::
Program::Opcode::MemoryInit obj;
obj.block_id = serde::Deserializable<decltype(obj.block_id)>::deserialize(deserializer);
obj.init = serde::Deserializable<decltype(obj.init)>::deserialize(deserializer);
obj.databus = serde::Deserializable<decltype(obj.databus)>::deserialize(deserializer);
return obj;
}

Expand Down
4 changes: 4 additions & 0 deletions noir/noir-repo/acvm-repo/acir/codegen/acir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ namespace Program {
struct MemoryInit {
Program::BlockId block_id;
std::vector<Program::Witness> init;
uint8_t databus;

friend bool operator==(const MemoryInit&, const MemoryInit&);
std::vector<uint8_t> bincodeSerialize() const;
Expand Down Expand Up @@ -6451,6 +6452,7 @@ namespace Program {
inline bool operator==(const Opcode::MemoryInit &lhs, const Opcode::MemoryInit &rhs) {
if (!(lhs.block_id == rhs.block_id)) { return false; }
if (!(lhs.init == rhs.init)) { return false; }
if (!(lhs.databus == rhs.databus)) { return false; }
return true;
}

Expand All @@ -6476,6 +6478,7 @@ template <typename Serializer>
void serde::Serializable<Program::Opcode::MemoryInit>::serialize(const Program::Opcode::MemoryInit &obj, Serializer &serializer) {
serde::Serializable<decltype(obj.block_id)>::serialize(obj.block_id, serializer);
serde::Serializable<decltype(obj.init)>::serialize(obj.init, serializer);
serde::Serializable<decltype(obj.databus)>::serialize(obj.databus, serializer);
}

template <>
Expand All @@ -6484,6 +6487,7 @@ Program::Opcode::MemoryInit serde::Deserializable<Program::Opcode::MemoryInit>::
Program::Opcode::MemoryInit obj;
obj.block_id = serde::Deserializable<decltype(obj.block_id)>::deserialize(deserializer);
obj.init = serde::Deserializable<decltype(obj.init)>::deserialize(deserializer);
obj.databus = serde::Deserializable<decltype(obj.databus)>::deserialize(deserializer);
return obj;
}

Expand Down
11 changes: 9 additions & 2 deletions noir/noir-repo/acvm-repo/acir/src/circuit/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum Opcode {
MemoryInit {
block_id: BlockId,
init: Vec<Witness>,
/// 0 for regular memory, 1 for call-data and 2 for return-data
databus: u8,
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel an enum would be clearer here and it also aligns with how we have been distinguishing opcodes already

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

},
/// Calls to unconstrained functions
BrilligCall {
Expand Down Expand Up @@ -103,8 +105,13 @@ impl std::fmt::Display for Opcode {
write!(f, "(id: {}, op {} at: {}) ", block_id.0, op.operation, op.index)
}
}
Opcode::MemoryInit { block_id, init } => {
write!(f, "INIT ")?;
Opcode::MemoryInit { block_id, init, databus } => {
match databus {
0 => write!(f, "INIT ")?,
1 => write!(f, "INIT CALLDATA ")?,
2 => write!(f, "INIT RETURNDATA ")?,
_ => unreachable!(),
}
write!(f, "(id: {}, len: {}) ", block_id.0, init.len())
}
// We keep the display for a BrilligCall and circuit Call separate as they
Expand Down
2 changes: 1 addition & 1 deletion noir/noir-repo/acvm-repo/acvm/src/pwg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl<'a, B: BlackBoxFunctionSolver> ACVM<'a, B> {
&mut self.bigint_solver,
),
Opcode::Directive(directive) => solve_directives(&mut self.witness_map, directive),
Opcode::MemoryInit { block_id, init } => {
Opcode::MemoryInit { block_id, init, .. } => {
let solver = self.block_solvers.entry(*block_id).or_default();
solver.init(init, &self.witness_map)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,7 @@ impl AcirContext {
block_id: BlockId,
len: usize,
optional_value: Option<AcirValue>,
databus: u8,
) -> Result<(), InternalError> {
let initialized_values = match optional_value {
None => {
Expand All @@ -1772,7 +1773,11 @@ impl AcirContext {
}
};

self.acir_ir.push_opcode(Opcode::MemoryInit { block_id, init: initialized_values });
self.acir_ir.push_opcode(Opcode::MemoryInit {
block_id,
init: initialized_values,
databus,
});

Ok(())
}
Expand Down
13 changes: 12 additions & 1 deletion noir/noir-repo/compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,18 @@ impl<'a> Context<'a> {
len: usize,
value: Option<AcirValue>,
) -> Result<(), InternalError> {
self.acir_context.initialize_array(array, len, value)?;
let databus = if self.data_bus.call_data.is_some()
&& self.block_id(&self.data_bus.call_data.unwrap()) == array
{
1
} else if self.data_bus.return_data.is_some()
&& self.block_id(&self.data_bus.return_data.unwrap()) == array
{
2
} else {
0
};
self.acir_context.initialize_array(array, len, value, databus)?;
self.initialized_arrays.insert(array);
Ok(())
}
Expand Down
Loading