Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/acir/call/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Context<'_> {
| Intrinsic::AssertConstant
| Intrinsic::ArrayRefCount
| Intrinsic::SliceRefCount => {
unreachable!("Expected {intrinsic} to be removed by this point")
unreachable!("Expected {intrinsic} to have been removing during SSA optimizations")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::brillig::brillig_ir::{BrilligBinaryOp, registers::RegisterAllocator};
use crate::ssa::ir::instruction::{Endian, Hint, InstructionId, Intrinsic};
use crate::ssa::ir::{
dfg::DataFlowGraph,
types::{NumericType, Type},
types::NumericType,
value::{Value, ValueId},
};

Expand Down Expand Up @@ -88,36 +88,6 @@ impl<Registers: RegisterAllocator> BrilligBlock<'_, Registers> {
}
}

/// Converts the ArrayLen intrinsic to Brillig bytecode.
///
/// For slices (represented as tuples of (length, contents)), this directly moves
/// the length field. For arrays, it calculates the length based on the array size.
fn convert_ssa_array_len_intrinsic(
&mut self,
arguments: &[ValueId],
instruction_id: InstructionId,
dfg: &DataFlowGraph,
) {
let [result_value] = dfg.instruction_result(instruction_id);
let result_variable = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
result_value,
dfg,
);
let param_id = arguments[0];
// Slices are represented as a tuple in the form: (length, slice contents).
// Thus, we can expect the first argument to a field in the case of a slice
// or an array in the case of an array.
if let Type::Numeric(_) = dfg.type_of_value(param_id) {
let len_variable = self.convert_ssa_value(arguments[0], dfg);
let length = len_variable.extract_single_addr();
self.brillig_context.mov_instruction(result_variable.address, length.address);
} else {
self.convert_ssa_array_len(arguments[0], result_variable.address, dfg);
}
}

/// Converts a field less than comparison intrinsic to Brillig bytecode.
///
/// Compares two field elements and returns a boolean result.
Expand Down Expand Up @@ -268,9 +238,6 @@ impl<Registers: RegisterAllocator> BrilligBlock<'_, Registers> {
// This match could be combined with the above but without it rust analyzer
// can't automatically insert any missing cases
match intrinsic {
Intrinsic::ArrayLen => {
self.convert_ssa_array_len_intrinsic(arguments, instruction_id, dfg);
}
Intrinsic::AsSlice => {
self.convert_ssa_as_slice(arguments, instruction_id, dfg);
}
Expand Down Expand Up @@ -379,14 +346,23 @@ impl<Registers: RegisterAllocator> BrilligBlock<'_, Registers> {
let array = array.extract_register();
self.brillig_context.load_instruction(destination, array);
}
Intrinsic::ApplyRangeConstraint => {
unreachable!(
"ICE: `Intrinsic::ApplyRangeConstraint` calls should be transformed into an `Instruction::RangeCheck`"
);
}
Intrinsic::DerivePedersenGenerators => {
unreachable!("unsupported function call type {:?}", dfg[func])
}
Intrinsic::IsUnconstrained
| Intrinsic::DerivePedersenGenerators
| Intrinsic::ApplyRangeConstraint
| Intrinsic::ArrayLen
| Intrinsic::ArrayAsStrUnchecked
| Intrinsic::StrAsBytes
| Intrinsic::AssertConstant
| Intrinsic::StaticAssert
| Intrinsic::ArrayAsStrUnchecked => {
unreachable!("unsupported function call type {:?}", dfg[func])
| Intrinsic::AssertConstant => {
unreachable!(
"Expected {intrinsic} to have been removing during SSA optimizations"
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub(super) mod brillig_black_box;
pub(super) mod brillig_slice_ops;
pub(super) mod code_gen_call;

use acvm::acir::brillig::MemoryAddress;
use iter_extended::vecmap;

use crate::brillig::BrilligBlock;
Expand Down Expand Up @@ -147,39 +146,6 @@ impl<Registers: RegisterAllocator> BrilligBlock<'_, Registers> {
self.brillig_context.codegen_call(func_id, &argument_variables, &return_variables);
}

/// Gets the "user-facing" length of an array.
/// An array of structs with two fields would be stored as an 2 * array.len() array/vector.
/// So we divide the length by the number of subitems in an item to get the user-facing length.
fn convert_ssa_array_len(
&mut self,
array_id: ValueId,
result_register: MemoryAddress,
dfg: &DataFlowGraph,
) {
let array_variable = self.convert_ssa_value(array_id, dfg);
let element_size = dfg.type_of_value(array_id).element_size();

match array_variable {
BrilligVariable::BrilligArray(BrilligArray { size, .. }) => {
self.brillig_context
.usize_const_instruction(result_register, (size / element_size).into());
}
BrilligVariable::BrilligVector(vector) => {
let size = self.brillig_context.codegen_make_vector_length(vector);

self.brillig_context.codegen_usize_op(
size.address,
result_register,
BrilligBinaryOp::UnsignedDiv,
element_size,
);
}
_ => {
unreachable!("ICE: Cannot get length of {array_variable:?}")
}
}
}

/// Increase or decrease the slice length by 1.
///
/// Slices have a tuple structure (slice length, slice contents) to enable logic
Expand Down
36 changes: 0 additions & 36 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/tests/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,6 @@ use crate::{
ssa::ir::map::Id,
};

// Tests ArrayLen intrinsic code-gen for Brillig
#[test]
fn brillig_array_len() {
let src = "
brillig(inline) fn foo f0 {
b0():
v0 = make_array [u32 10, u32 20, u32 30] : [u32; 3]
v1 = call array_len(v0) -> u32
return v1
}
";

let brillig = ssa_to_brillig_artifacts(src);
let foo = &brillig.ssa_function_to_brillig[&Id::test_new(0)];
assert_artifact_snapshot!(foo, @r"
fn foo
0: call 0
1: sp[1] = const u32 10
2: sp[2] = const u32 20
3: sp[3] = const u32 30
4: sp[4] = @1
5: sp[5] = const u32 4
6: @1 = u32 add @1, sp[5]
7: sp[4] = indirect const u32 1
8: sp[5] = u32 add sp[4], @2
9: sp[6] = sp[5]
10: store sp[1] at sp[6]
11: sp[6] = u32 add sp[6], @2
12: store sp[2] at sp[6]
13: sp[6] = u32 add sp[6], @2
14: store sp[3] at sp[6]
15: sp[1] = const u32 3
16: return
");
}

// Tests AsSlice intrinsic code-gen for Brillig.
#[test]
fn brillig_as_slice() {
Expand Down
Loading