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: shortcircuit merging two of the same value during flattening #4693

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 7 additions & 8 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl<'f> Context<'f> {
self.if_start(condition, then_destination, else_destination, &block)
}
TerminatorInstruction::Jmp { destination, arguments, call_stack: _ } => {
let arguments = vecmap(arguments.clone(), |value| self.inserter.resolve(value));
let arguments = vecmap(arguments, |value| self.inserter.resolve(*value));
self.arguments_stack.push(arguments);
if work_list.contains(destination) {
if work_list.last() == Some(destination) {
Expand All @@ -374,8 +374,7 @@ impl<'f> Context<'f> {
}
TerminatorInstruction::Return { return_values, call_stack } => {
let call_stack = call_stack.clone();
let return_values =
vecmap(return_values.clone(), |value| self.inserter.resolve(value));
let return_values = vecmap(return_values, |value| self.inserter.resolve(*value));
let new_return = TerminatorInstruction::Return { return_values, call_stack };
let entry = self.inserter.function.entry_block();

Expand Down Expand Up @@ -527,7 +526,7 @@ impl<'f> Context<'f> {
let last_then = cond_context.then_branch.last_block;
let mut else_args = Vec::new();
if cond_context.else_branch.is_some() {
let last_else = cond_context.else_branch.clone().unwrap().last_block;
let last_else = cond_context.else_branch.as_ref().unwrap().last_block;
else_args = self.inserter.function.dfg[last_else].terminator_arguments().to_vec();
}

Expand Down Expand Up @@ -557,7 +556,7 @@ impl<'f> Context<'f> {
let args = vecmap(args, |(then_arg, else_arg)| {
value_merger.merge_values(
cond_context.then_branch.condition,
cond_context.else_branch.clone().unwrap().condition,
cond_context.else_branch.as_ref().unwrap().condition,
then_arg,
else_arg,
)
Expand Down Expand Up @@ -634,11 +633,11 @@ impl<'f> Context<'f> {
}

if else_branch.is_some() {
for (address, store) in else_branch.clone().unwrap().store_values {
if let Some(entry) = new_map.get_mut(&address) {
for (address, store) in &else_branch.as_ref().unwrap().store_values {
if let Some(entry) = new_map.get_mut(address) {
entry.1 = store.new_value;
} else {
new_map.insert(address, (store.old_value, store.new_value, store.old_value));
new_map.insert(*address, (store.old_value, store.new_value, store.old_value));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ impl<'a> ValueMerger<'a> {
then_value: ValueId,
else_value: ValueId,
) -> ValueId {
if self.dfg.resolve(then_value) == self.dfg.resolve(else_value) {
// We assume here that `then_condition + else_condition == 1` which is reasonable.
return then_value;
}

let then_type = self.dfg.type_of_value(then_value);
let else_type = self.dfg.type_of_value(else_value);
assert_eq!(
Expand Down
Loading