Skip to content
8 changes: 4 additions & 4 deletions compiler/noirc_evaluator/src/acir/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
//! ACIR generation use twos different array types for representing arrays:
//!
//! [Constant arrays][AcirValue::Array]
//! - Known at compile time.
//! - Known at compile time.
//! - Reads and writes may be folded into an [AcirValue] where possible.
//! - Useful for optimization (e.g., constant element lookups do not require laying down opcodes)
//! - Useful for optimization (e.g., constant element lookups do not require laying down opcodes)
//!
//! [Dynamic arrays][AcirValue::DynamicArray]
//! - Referenced by a [unique identifier][BlockId]
Expand Down Expand Up @@ -562,8 +562,8 @@ impl Context<'_> {
index_side_effect: bool,
) -> Result<(), RuntimeError> {
let block_id = self.ensure_array_is_initialized(array, dfg)?;
let results = dfg.instruction_results(instruction);
let res_typ = dfg.type_of_value(results[0]);
let [result] = dfg.instruction_result(instruction);
let res_typ = dfg.type_of_value(result);

let value = self.load_array_value(array, block_id, var_index, &res_typ, dfg)?;

Expand Down
10 changes: 5 additions & 5 deletions compiler/noirc_evaluator/src/acir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ impl<'a> Context<'a> {
Instruction::MakeArray { elements, typ: _ } => {
let elements = elements.iter().map(|element| self.convert_value(*element, dfg));
let value = AcirValue::Array(elements.collect());
let result = dfg.instruction_results(instruction_id)[0];
let [result] = dfg.instruction_result(instruction_id);
self.ssa_values.insert(result, value);
}
Instruction::Noop => (),
Expand All @@ -586,8 +586,8 @@ impl<'a> Context<'a> {
instruction: InstructionId,
result: AcirValue,
) {
let result_ids = dfg.instruction_results(instruction);
self.ssa_values.insert(result_ids[0], result);
let [result_id] = dfg.instruction_result(instruction);
self.ssa_values.insert(result_id, result);
}

/// Remember the result of instruction returning a single numeric value
Expand All @@ -597,8 +597,8 @@ impl<'a> Context<'a> {
instruction: InstructionId,
result: AcirVar,
) {
let result_ids = dfg.instruction_results(instruction);
let typ = dfg.type_of_value(result_ids[0]).into();
let [result_id] = dfg.instruction_result(instruction);
let typ = dfg.type_of_value(result_id).into();
self.define_result(dfg, instruction, AcirValue::Var(result, typ));
}

Expand Down
46 changes: 26 additions & 20 deletions compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,11 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
);
match instruction {
Instruction::Binary(binary) => {
let [result_value] = dfg.instruction_result(instruction_id);
let result_var = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
dfg.instruction_results(instruction_id)[0],
result_value,
dfg,
);
self.convert_ssa_binary(binary, dfg, result_var);
Expand Down Expand Up @@ -411,7 +412,7 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
}

Instruction::Allocate => {
let result_value = dfg.instruction_results(instruction_id)[0];
let [result_value] = dfg.instruction_result(instruction_id);
let pointer = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
Expand All @@ -428,10 +429,12 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
.store_instruction(address_var.address, source_variable.extract_register());
}
Instruction::Load { address } => {
let [result_value] = dfg.instruction_result(instruction_id);

let target_variable = self.variables.define_variable(
self.function_context,
self.brillig_context,
dfg.instruction_results(instruction_id)[0],
result_value,
dfg,
);

Expand All @@ -441,11 +444,12 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
.load_instruction(target_variable.extract_register(), address_variable.address);
}
Instruction::Not(value) => {
let [result_value] = dfg.instruction_result(instruction_id);
let condition_register = self.convert_ssa_single_addr_value(*value, dfg);
let result_register = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
dfg.instruction_results(instruction_id)[0],
result_value,
dfg,
);
self.brillig_context.not_instruction(condition_register, result_register);
Expand Down Expand Up @@ -538,10 +542,11 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
// can't automatically insert any missing cases
match intrinsic {
Intrinsic::ArrayLen => {
let [result_value] = dfg.instruction_result(instruction_id);
let result_variable = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
dfg.instruction_results(instruction_id)[0],
result_value,
dfg,
);
let param_id = arguments[0];
Expand Down Expand Up @@ -630,7 +635,7 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
);
}
Intrinsic::ToBits(endianness) => {
let results = dfg.instruction_results(instruction_id);
let [result] = dfg.instruction_result(instruction_id);

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

Expand All @@ -639,7 +644,7 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
.define_variable(
self.function_context,
self.brillig_context,
results[0],
result,
dfg,
)
.extract_array();
Expand All @@ -660,7 +665,7 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
}

Intrinsic::ToRadix(endianness) => {
let results = dfg.instruction_results(instruction_id);
let [result] = dfg.instruction_result(instruction_id);

let source = self.convert_ssa_single_addr_value(arguments[0], dfg);
let radix = self.convert_ssa_single_addr_value(arguments[1], dfg);
Expand All @@ -670,7 +675,7 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
.define_variable(
self.function_context,
self.brillig_context,
results[0],
result,
dfg,
)
.extract_array();
Expand Down Expand Up @@ -755,13 +760,13 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
let rhs = self.convert_ssa_single_addr_value(arguments[1], dfg);
assert!(rhs.bit_size == FieldElement::max_num_bits());

let results = dfg.instruction_results(instruction_id);
let [result] = dfg.instruction_result(instruction_id);
let destination = self
.variables
.define_variable(
self.function_context,
self.brillig_context,
results[0],
result,
dfg,
)
.extract_single_addr();
Expand All @@ -776,7 +781,7 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
}
Intrinsic::ArrayRefCount => {
let array = self.convert_ssa_value(arguments[0], dfg);
let result = dfg.instruction_results(instruction_id)[0];
let [result] = dfg.instruction_result(instruction_id);

let destination = self.variables.define_variable(
self.function_context,
Expand All @@ -790,7 +795,7 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
}
Intrinsic::SliceRefCount => {
let array = self.convert_ssa_value(arguments[1], dfg);
let result = dfg.instruction_results(instruction_id)[0];
let [result] = dfg.instruction_result(instruction_id);

let destination = self.variables.define_variable(
self.function_context,
Expand Down Expand Up @@ -821,11 +826,11 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
}
},
Instruction::Truncate { value, bit_size, .. } => {
let result_ids = dfg.instruction_results(instruction_id);
let [result_id] = dfg.instruction_result(instruction_id);
let destination_register = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
result_ids[0],
result_id,
dfg,
);
let source_register = self.convert_ssa_single_addr_value(*value, dfg);
Expand All @@ -836,22 +841,22 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
);
}
Instruction::Cast(value, _) => {
let result_ids = dfg.instruction_results(instruction_id);
let [result_id] = dfg.instruction_result(instruction_id);
let destination_variable = self.variables.define_single_addr_variable(
self.function_context,
self.brillig_context,
result_ids[0],
result_id,
dfg,
);
let source_variable = self.convert_ssa_single_addr_value(*value, dfg);
self.convert_cast(destination_variable, source_variable);
}
Instruction::ArrayGet { array, index } => {
let result_ids = dfg.instruction_results(instruction_id);
let [result_id] = dfg.instruction_result(instruction_id);
let destination_variable = self.variables.define_variable(
self.function_context,
self.brillig_context,
result_ids[0],
result_id,
dfg,
);

Expand Down Expand Up @@ -978,10 +983,11 @@ impl<'block, Registers: RegisterAllocator> BrilligBlock<'block, Registers> {
let then_condition = self.convert_ssa_single_addr_value(*then_condition, dfg);
let then_value = self.convert_ssa_value(*then_value, dfg);
let else_value = self.convert_ssa_value(*else_value, dfg);
let [result_value] = dfg.instruction_result(instruction_id);
let result = self.variables.define_variable(
self.function_context,
self.brillig_context,
dfg.instruction_results(instruction_id)[0],
result_value,
dfg,
);
match (then_value, else_value) {
Expand Down
13 changes: 13 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,19 @@ impl DataFlowGraph {
self.results.get(&instruction_id).expect("expected a list of Values").as_slice()
}

/// Returns N results, asserting that there are exactly N items.
pub(crate) fn instruction_result<const N: usize>(
&self,
instruction_id: InstructionId,
) -> [ValueId; N] {
let results = self.instruction_results(instruction_id);
if results.len() != N {
let instruction = &self[instruction_id];
panic!("expected {instruction:?} to have {N} results; got {}", results.len());
}
std::array::from_fn(|i| results[i])
}

/// Remove an instruction by replacing it with a `Noop` instruction.
/// Doing this avoids shifting over each instruction after this one in its block's instructions vector.
#[allow(unused)]
Expand Down
3 changes: 1 addition & 2 deletions compiler/noirc_evaluator/src/ssa/opt/as_slice_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ impl Function {
unreachable!("AsSlice called with non-array {}", array_typ);
};

let call_returns = context.dfg.instruction_results(instruction_id);
let original_slice_length = call_returns[0];
let [original_slice_length, _] = context.dfg.instruction_result(instruction_id);
let known_length = context.dfg.make_constant(length.into(), NumericType::length_type());
context.replace_value(original_slice_length, known_length);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn interpreter_value_to_ir_value(

let instruction_id = dfg.make_instruction(instruction, None);
dfg[block_id].instructions_mut().push(instruction_id);
*dfg.instruction_results(instruction_id).first().unwrap()
dfg.instruction_result::<1>(instruction_id)[0]
}
Type::Slice(element_types) => {
let array = match value {
Expand All @@ -161,7 +161,7 @@ fn interpreter_value_to_ir_value(

let instruction_id = dfg.make_instruction(instruction, None);
dfg[block_id].instructions_mut().push(instruction_id);
*dfg.instruction_results(instruction_id).first().unwrap()
dfg.instruction_result::<1>(instruction_id)[0]
}
Type::Function | Type::Reference(_) => unreachable!("Cannot be a constant value"),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Function {
// We remove the current instruction, as we will need to replace it with multiple new instructions.
context.remove_current_instruction();

let old_result = *context.dfg.instruction_results(instruction_id).first().unwrap();
let [old_result] = context.dfg.instruction_result(instruction_id);

let mut expansion_context = Context { context };
let new_result = match operator {
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ impl<'f> LoopInvariantContext<'f> {
// If we are hoisting a MakeArray instruction,
// we need to issue an extra inc_rc in case they are mutated afterward.
if insert_rc {
let result =
self.inserter.function.dfg.instruction_results(instruction_id)[0];
let [result] =
self.inserter.function.dfg.instruction_result(instruction_id);
let inc_rc = Instruction::IncrementRc { value: result };
let call_stack = self
.inserter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ impl LoopInvariantContext<'_> {
// Simplify the instruction and update it in the DFG.
match self.simplify_induction_variable(loop_context, block_context, instruction_id) {
SimplifyResult::SimplifiedTo(id) => {
let results =
self.inserter.function.dfg.instruction_results(instruction_id).to_vec();
assert!(results.len() == 1);
self.inserter.map_value(results[0], id);
let [result] = self.inserter.function.dfg.instruction_result(instruction_id);
self.inserter.map_value(result, id);
true
}
SimplifyResult::SimplifiedToInstruction(instruction) => {
Expand Down
Loading
Loading