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(mem2reg): Remove possibility of underflow #6107

Merged
merged 5 commits into from
Sep 19, 2024
Merged
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
34 changes: 18 additions & 16 deletions compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
//!
//! Repeating this algorithm for each block in the function in program order should result in
//! optimizing out most known loads. However, identifying all aliases correctly has been proven
//! undecidable in general (Landi, 1992). So this pass will not always optimize out all loads

Check warning on line 59 in compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Landi)
//! that could theoretically be optimized out. This pass can be performed at any time in the
//! SSA optimization pipeline, although it will be more successful the simpler the program's CFG is.
//! This pass is currently performed several times to enable other passes - most notably being
Expand Down Expand Up @@ -144,7 +144,7 @@
/// Load and Store instructions that should be removed at the end of the pass.
///
/// We avoid removing individual instructions as we go since removing elements
/// from the middle of Vecs many times will be slower than a single call to `retain`.

Check warning on line 147 in compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Vecs)
instructions_to_remove: HashSet<InstructionId>,

/// Track a value's last load across all blocks.
Expand Down Expand Up @@ -288,6 +288,8 @@
.filter(|param| self.inserter.function.dfg.value_is_reference(**param))
.collect::<BTreeSet<_>>();

// Must collect here as we are immutably borrowing `self` to fetch the reference parameters
let mut values_to_reduce_counts = Vec::new();
for (allocation, instruction) in &references.last_stores {
if let Some(expression) = references.expressions.get(allocation) {
if let Some(aliases) = references.aliases.get(expression) {
Expand All @@ -297,13 +299,15 @@
// If `allocation_aliases_parameter` is known to be false
if allocation_aliases_parameter == Some(false) {
self.instructions_to_remove.insert(*instruction);
if let Some(context) = self.load_results.get_mut(allocation) {
context.uses -= 1;
}
values_to_reduce_counts.push(*allocation);
}
}
}
}

for value in values_to_reduce_counts {
self.reduce_load_result_count(value);
}
}

fn increase_load_ref_counts(&mut self, value: ValueId) {
Expand Down Expand Up @@ -406,25 +410,17 @@
else {
panic!("Should have a store instruction here");
};
if let Some(context) = self.load_results.get_mut(&address) {
context.uses -= 1;
}
if let Some(context) = self.load_results.get_mut(&value) {
context.uses -= 1;
}
self.reduce_load_result_count(address);
self.reduce_load_result_count(value);
}

let known_value = references.get_known_value(value);
if let Some(known_value) = known_value {
let known_value_is_address = known_value == address;
if known_value_is_address {
self.instructions_to_remove.insert(instruction);
if let Some(context) = self.load_results.get_mut(&address) {
context.uses -= 1;
}
if let Some(context) = self.load_results.get_mut(&value) {
context.uses -= 1;
}
self.reduce_load_result_count(address);
self.reduce_load_result_count(value);
} else {
references.last_stores.insert(address, instruction);
}
Expand Down Expand Up @@ -617,6 +613,12 @@
}
}

fn reduce_load_result_count(&mut self, value: ValueId) {
if let Some(context) = self.load_results.get_mut(&value) {
context.uses = context.uses.saturating_sub(1);
}
}

fn recursively_add_values(&self, value: ValueId, set: &mut HashSet<ValueId>) {
set.insert(value);
if let Some((elements, _)) = self.inserter.function.dfg.get_array_constant(value) {
Expand Down Expand Up @@ -741,7 +743,7 @@
if all_loads_removed && !store_alias_used {
self.instructions_to_remove.insert(*store_instruction);
if let Some((_, counter)) = remaining_last_stores.get_mut(store_address) {
*counter -= 1;
*counter = counter.saturating_sub(1);
}
} else if let Some((_, counter)) = remaining_last_stores.get_mut(store_address) {
*counter += 1;
Expand Down
Loading