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

fix: Add locations to most SSA instructions #5697

Merged
merged 3 commits into from
Aug 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ impl<'block> BrilligBlock<'block> {
dfg: &DataFlowGraph,
) {
match terminator_instruction {
TerminatorInstruction::JmpIf { condition, then_destination, else_destination } => {
TerminatorInstruction::JmpIf {
condition,
then_destination,
else_destination,
call_stack: _,
} => {
let condition = self.convert_ssa_single_addr_value(*condition, dfg);
self.brillig_context.jump_if_instruction(
condition.address,
Expand Down
2 changes: 2 additions & 0 deletions compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,12 @@ impl FunctionBuilder {
then_destination: BasicBlockId,
else_destination: BasicBlockId,
) {
let call_stack = self.call_stack.clone();
self.terminate_block_with(TerminatorInstruction::JmpIf {
condition,
then_destination,
else_destination,
call_stack,
});
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ mod tests {
condition: cond,
then_destination: block2_id,
else_destination: block1_id,
call_stack: CallStack::new(),
});
func.dfg[block1_id].set_terminator(TerminatorInstruction::JmpIf {
condition: cond,
then_destination: block1_id,
else_destination: block2_id,
call_stack: CallStack::new(),
});
func.dfg[block2_id].set_terminator(TerminatorInstruction::Return {
return_values: vec![],
Expand Down Expand Up @@ -235,6 +237,7 @@ mod tests {
condition: cond,
then_destination: block1_id,
else_destination: ret_block_id,
call_stack: CallStack::new(),
});

// Recompute new and changed blocks
Expand Down
19 changes: 17 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use noirc_errors::Location;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};

Expand Down Expand Up @@ -804,7 +805,12 @@ pub(crate) enum TerminatorInstruction {
///
/// If the condition is true: jump to the specified `then_destination`.
/// Otherwise, jump to the specified `else_destination`.
JmpIf { condition: ValueId, then_destination: BasicBlockId, else_destination: BasicBlockId },
JmpIf {
condition: ValueId,
then_destination: BasicBlockId,
else_destination: BasicBlockId,
call_stack: CallStack,
},

/// Unconditional Jump
///
Expand All @@ -831,10 +837,11 @@ impl TerminatorInstruction {
) -> TerminatorInstruction {
use TerminatorInstruction::*;
match self {
JmpIf { condition, then_destination, else_destination } => JmpIf {
JmpIf { condition, then_destination, else_destination, call_stack } => JmpIf {
condition: f(*condition),
then_destination: *then_destination,
else_destination: *else_destination,
call_stack: call_stack.clone(),
},
Jmp { destination, arguments, call_stack } => Jmp {
destination: *destination,
Expand Down Expand Up @@ -902,6 +909,14 @@ impl TerminatorInstruction {
Return { .. } => (),
}
}

pub(crate) fn call_stack(&self) -> im::Vector<Location> {
match self {
TerminatorInstruction::JmpIf { call_stack, .. }
| TerminatorInstruction::Jmp { call_stack, .. }
| TerminatorInstruction::Return { call_stack, .. } => call_stack.clone(),
}
}
}

/// Contains the result to Instruction::simplify, specifying how the instruction
Expand Down
23 changes: 16 additions & 7 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ pub(super) fn simplify_call(
return SimplifyResult::SimplifiedToMultiple(vec![new_slice_length, new_slice]);
}

simplify_slice_push_back(slice, element_type, arguments, dfg, block)
simplify_slice_push_back(
slice,
element_type,
arguments,
dfg,
block,
call_stack.clone(),
)
} else {
SimplifyResult::None
}
Expand All @@ -147,7 +154,7 @@ pub(super) fn simplify_call(
Intrinsic::SlicePopBack => {
let slice = dfg.get_array_constant(arguments[1]);
if let Some((_, typ)) = slice {
simplify_slice_pop_back(typ, arguments, dfg, block)
simplify_slice_pop_back(typ, arguments, dfg, block, call_stack.clone())
} else {
SimplifyResult::None
}
Expand Down Expand Up @@ -346,12 +353,12 @@ fn simplify_slice_push_back(
arguments: &[ValueId],
dfg: &mut DataFlowGraph,
block: BasicBlockId,
call_stack: CallStack,
) -> SimplifyResult {
// The capacity must be an integer so that we can compare it against the slice length
let capacity = dfg.make_constant((slice.len() as u128).into(), Type::length_type());
let len_equals_capacity_instr =
Instruction::Binary(Binary { lhs: arguments[0], operator: BinaryOp::Eq, rhs: capacity });
let call_stack = dfg.get_value_call_stack(arguments[0]);
let len_equals_capacity = dfg
.insert_instruction_and_results(len_equals_capacity_instr, block, None, call_stack.clone())
.first();
Expand Down Expand Up @@ -382,15 +389,16 @@ fn simplify_slice_push_back(
};

let set_last_slice_value = dfg
.insert_instruction_and_results(set_last_slice_value_instr, block, None, call_stack)
.insert_instruction_and_results(set_last_slice_value_instr, block, None, call_stack.clone())
.first();

let mut slice_sizes = HashMap::default();
slice_sizes.insert(set_last_slice_value, slice_size / element_size);
slice_sizes.insert(new_slice, slice_size / element_size);

let unknown = &mut HashMap::default();
let mut value_merger = ValueMerger::new(dfg, block, &mut slice_sizes, unknown, None);
let mut value_merger =
ValueMerger::new(dfg, block, &mut slice_sizes, unknown, None, call_stack);

let new_slice = value_merger.merge_values(
len_not_equals_capacity,
Expand All @@ -407,6 +415,7 @@ fn simplify_slice_pop_back(
arguments: &[ValueId],
dfg: &mut DataFlowGraph,
block: BasicBlockId,
call_stack: CallStack,
) -> SimplifyResult {
let element_types = match element_type.clone() {
Type::Slice(element_types) | Type::Array(element_types, _) => element_types,
Expand All @@ -423,7 +432,7 @@ fn simplify_slice_pop_back(
let element_size = dfg.make_constant((element_count as u128).into(), Type::length_type());
let flattened_len_instr = Instruction::binary(BinaryOp::Mul, arguments[0], element_size);
let mut flattened_len = dfg
.insert_instruction_and_results(flattened_len_instr, block, None, CallStack::new())
.insert_instruction_and_results(flattened_len_instr, block, None, call_stack.clone())
.first();
flattened_len = update_slice_length(flattened_len, dfg, BinaryOp::Sub, block);

Expand All @@ -436,7 +445,7 @@ fn simplify_slice_pop_back(
get_last_elem_instr,
block,
Some(element_types.to_vec()),
CallStack::new(),
call_stack.clone(),
)
.first();
results.push_front(get_last_elem);
Expand Down
7 changes: 6 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ pub(crate) fn display_terminator(
Some(TerminatorInstruction::Jmp { destination, arguments, call_stack: _ }) => {
writeln!(f, " jmp {}({})", destination, value_list(function, arguments))
}
Some(TerminatorInstruction::JmpIf { condition, then_destination, else_destination }) => {
Some(TerminatorInstruction::JmpIf {
condition,
then_destination,
else_destination,
call_stack: _,
}) => {
writeln!(
f,
" jmpif {} then: {}, else: {}",
Expand Down
Loading
Loading