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!: return arrays instead of slices from to_be_radix functions #5851

Merged
merged 19 commits into from
Sep 2, 2024
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
77 changes: 17 additions & 60 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ impl<'block> BrilligBlock<'block> {
);
}
Value::Intrinsic(Intrinsic::ToRadix(endianness)) => {
let results = dfg.instruction_results(instruction_id);

let source = self.convert_ssa_single_addr_value(arguments[0], dfg);

let radix: u32 = dfg
Expand All @@ -502,88 +504,43 @@ impl<'block> BrilligBlock<'block> {
.try_into()
.expect("Radix should be u32");

let limb_count: usize = dfg
.get_numeric_constant(arguments[2])
.expect("Limb count should be known")
.try_to_u64()
.expect("Limb count should fit in u64")
.try_into()
.expect("Limb count should fit in usize");

let results = dfg.instruction_results(instruction_id);

let target_len = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
results[0],
dfg,
);

let target_vector = self
let target_array = self
.variables
.define_variable(
self.function_context,
self.brillig_context,
results[1],
results[0],
dfg,
)
.extract_vector();

// Update the user-facing slice length
self.brillig_context
.usize_const_instruction(target_len.address, limb_count.into());
.extract_array();

self.brillig_context.codegen_to_radix(
source,
target_vector,
target_array,
radix,
limb_count,
matches!(endianness, Endian::Big),
8,
);
}
Value::Intrinsic(Intrinsic::ToBits(endianness)) => {
let source = self.convert_ssa_single_addr_value(arguments[0], dfg);
let limb_count: usize = dfg
.get_numeric_constant(arguments[1])
.expect("Limb count should be known")
.try_to_u64()
.expect("Limb count should fit in u64")
.try_into()
.expect("Limb count should fit in usize");

let results = dfg.instruction_results(instruction_id);

let target_len_variable = self.variables.define_variable(
self.function_context,
self.brillig_context,
results[0],
dfg,
);
let target_len = target_len_variable.extract_single_addr();

let target_vector = match self.variables.define_variable(
self.function_context,
self.brillig_context,
results[1],
dfg,
) {
BrilligVariable::BrilligArray(array) => {
self.brillig_context.array_to_vector_instruction(&array)
}
BrilligVariable::BrilligVector(vector) => vector,
BrilligVariable::SingleAddr(..) => unreachable!("ICE: ToBits on non-array"),
};
let source = self.convert_ssa_single_addr_value(arguments[0], dfg);

// Update the user-facing slice length
self.brillig_context
.usize_const_instruction(target_len.address, limb_count.into());
let target_array = self
.variables
.define_variable(
self.function_context,
self.brillig_context,
results[0],
dfg,
)
.extract_array();

self.brillig_context.codegen_to_radix(
source,
target_vector,
target_array,
2,
limb_count,
matches!(endianness, Endian::Big),
1,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use acvm::acir::{
brillig::{BlackBoxOp, HeapArray, IntegerBitSize},
brillig::{BlackBoxOp, IntegerBitSize},
AcirField,
};

use crate::brillig::brillig_ir::BrilligBinaryOp;

use super::{
brillig_variable::{BrilligVector, SingleAddrVariable},
brillig_variable::{BrilligArray, SingleAddrVariable},
debug_show::DebugToString,
BrilligContext,
};
Expand Down Expand Up @@ -66,48 +66,42 @@ impl<F: AcirField + DebugToString> BrilligContext<F> {
pub(crate) fn codegen_to_radix(
&mut self,
source_field: SingleAddrVariable,
target_vector: BrilligVector,
target_array: BrilligArray,
radix: u32,
limb_count: usize,
big_endian: bool,
limb_bit_size: u32,
) {
assert!(source_field.bit_size == F::max_num_bits());

self.usize_const_instruction(target_vector.size, limb_count.into());
self.usize_const_instruction(target_vector.rc, 1_usize.into());
self.codegen_allocate_array(target_vector.pointer, target_vector.size);

self.black_box_op_instruction(BlackBoxOp::ToRadix {
input: source_field.address,
radix,
output: HeapArray { pointer: target_vector.pointer, size: limb_count },
output: target_array.to_heap_array(),
});

let limb_field = SingleAddrVariable::new(self.allocate_register(), F::max_num_bits());
let limb_casted = SingleAddrVariable::new(self.allocate_register(), limb_bit_size);

if limb_bit_size != F::max_num_bits() {
self.codegen_loop(target_vector.size, |ctx, iterator_register| {
let size = self.allocate_register();
self.usize_const_instruction(size, target_array.size.into());
self.codegen_loop(size, |ctx, iterator_register| {
// Read the limb
ctx.codegen_array_get(target_vector.pointer, iterator_register, limb_field.address);
ctx.codegen_array_get(target_array.pointer, iterator_register, limb_field.address);
// Cast it
ctx.cast_instruction(limb_casted, limb_field);
// Write it
ctx.codegen_array_set(
target_vector.pointer,
iterator_register,
limb_casted.address,
);
ctx.codegen_array_set(target_array.pointer, iterator_register, limb_casted.address);
});
self.deallocate_register(size);
}

// Deallocate our temporary registers
self.deallocate_single_addr(limb_field);
self.deallocate_single_addr(limb_casted);

if big_endian {
self.codegen_reverse_vector_in_place(target_vector);
self.codegen_reverse_array_in_place(target_array);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,21 @@ impl<F: AcirField + DebugToString> BrilligContext<F> {
}
}

/// This instruction will reverse the order of the elements in a vector.
pub(crate) fn codegen_reverse_vector_in_place(&mut self, vector: BrilligVector) {
/// This instruction will reverse the order of the elements in a array.
pub(crate) fn codegen_reverse_array_in_place(&mut self, array: BrilligArray) {
let iteration_count = self.allocate_register();
self.codegen_usize_op(vector.size, iteration_count, BrilligBinaryOp::UnsignedDiv, 2);
self.usize_const_instruction(iteration_count, array.size.into());

let start_value_register = self.allocate_register();
let index_at_end_of_array = self.allocate_register();
let end_value_register = self.allocate_register();

self.codegen_loop(iteration_count, |ctx, iterator_register| {
// Load both values
ctx.codegen_array_get(vector.pointer, iterator_register, start_value_register);
ctx.codegen_array_get(array.pointer, iterator_register, start_value_register);

// The index at the end of array is size - 1 - iterator
ctx.mov_instruction(index_at_end_of_array, vector.size);
ctx.usize_const_instruction(index_at_end_of_array, array.size.into());
ctx.codegen_usize_op_in_place(index_at_end_of_array, BrilligBinaryOp::Sub, 1);
ctx.memory_op_instruction(
index_at_end_of_array,
Expand All @@ -278,15 +278,15 @@ impl<F: AcirField + DebugToString> BrilligContext<F> {
);

ctx.codegen_array_get(
vector.pointer,
array.pointer,
SingleAddrVariable::new_usize(index_at_end_of_array),
end_value_register,
);

// Write both values
ctx.codegen_array_set(vector.pointer, iterator_register, end_value_register);
ctx.codegen_array_set(array.pointer, iterator_register, end_value_register);
ctx.codegen_array_set(
vector.pointer,
array.pointer,
SingleAddrVariable::new_usize(index_at_end_of_array),
start_value_register,
);
Expand Down
25 changes: 6 additions & 19 deletions compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,9 +1504,9 @@ impl<F: AcirField> AcirContext<F> {
endian: Endian,
input_var: AcirVar,
radix_var: AcirVar,
limb_count_var: AcirVar,
limb_count: u32,
result_element_type: AcirType,
) -> Result<Vec<AcirValue>, RuntimeError> {
) -> Result<AcirValue, RuntimeError> {
let radix = match self.vars[&radix_var].as_constant() {
Some(radix) => radix.to_u128() as u32,
None => {
Expand All @@ -1517,16 +1517,6 @@ impl<F: AcirField> AcirContext<F> {
}
};

let limb_count = match self.vars[&limb_count_var].as_constant() {
Some(limb_count) => limb_count.to_u128() as u32,
None => {
return Err(RuntimeError::InternalError(InternalError::NotAConstant {
name: "limb_size".to_string(),
call_stack: self.get_call_stack(),
}));
}
};

let input_expr = self.var_to_expression(input_var)?;

let bit_size = u32::BITS - (radix - 1).leading_zeros();
Expand All @@ -1543,22 +1533,19 @@ impl<F: AcirField> AcirContext<F> {

// `Intrinsic::ToRadix` returns slices which are represented
// by tuples with the structure (length, slice contents)
Ok(vec![
AcirValue::Var(self.add_constant(limb_vars.len()), AcirType::field()),
AcirValue::Array(limb_vars.into()),
])
Ok(AcirValue::Array(limb_vars.into()))
}

/// Returns `AcirVar`s constrained to be the bit decomposition of the provided input
pub(crate) fn bit_decompose(
&mut self,
endian: Endian,
input_var: AcirVar,
limb_count_var: AcirVar,
limb_count: u32,
result_element_type: AcirType,
) -> Result<Vec<AcirValue>, RuntimeError> {
) -> Result<AcirValue, RuntimeError> {
let two_var = self.add_constant(2_u128);
self.radix_decompose(endian, input_var, two_var, limb_count_var, result_element_type)
self.radix_decompose(endian, input_var, two_var, limb_count, result_element_type)
}

/// Recursive helper to flatten a single AcirValue into the result vector.
Expand Down
36 changes: 30 additions & 6 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ impl<'a> Context<'a> {

match self.convert_value(array_id, dfg) {
AcirValue::Var(acir_var, _) => {
println!("{array}[{index}]");
Err(RuntimeError::InternalError(InternalError::Unexpected {
expected: "an array value".to_string(),
found: format!("{acir_var:?}"),
Expand Down Expand Up @@ -2177,19 +2178,42 @@ impl<'a> Context<'a> {
Intrinsic::ToRadix(endian) => {
let field = self.convert_value(arguments[0], dfg).into_var()?;
let radix = self.convert_value(arguments[1], dfg).into_var()?;
let limb_size = self.convert_value(arguments[2], dfg).into_var()?;

let result_type = Self::array_element_type(dfg, result_ids[1]);
let Type::Array(result_type, array_length) = dfg.type_of_value(result_ids[0])
else {
unreachable!("ICE: ToRadix result must be an array");
};

self.acir_context.radix_decompose(endian, field, radix, limb_size, result_type)
self.acir_context
.radix_decompose(
endian,
field,
radix,
array_length as u32,
result_type[0].clone().into(),
)
.map(|array| vec![array])
}
Intrinsic::ToBits(endian) => {
let field = self.convert_value(arguments[0], dfg).into_var()?;
let bit_size = self.convert_value(arguments[1], dfg).into_var()?;

let result_type = Self::array_element_type(dfg, result_ids[1]);
let Type::Array(result_type, array_length) = dfg.type_of_value(result_ids[0])
else {
println!("{:?}", result_ids);
println!("{:?}", dfg.type_of_value(result_ids[0]));
println!("{:?}", dfg.type_of_value(result_ids[1]));

self.acir_context.bit_decompose(endian, field, bit_size, result_type)
unreachable!("ICE: ToRadix result must be an array");
};

self.acir_context
.bit_decompose(
endian,
field,
array_length as u32,
result_type[0].clone().into(),
)
.map(|array| vec![array])
}
Intrinsic::ArrayLen => {
let len = match self.convert_value(arguments[0], dfg) {
Expand Down
Loading
Loading