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

Rollup of 5 pull requests #130281

Merged
merged 13 commits into from
Sep 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
coverage: Simplify creation of sum counters
  • Loading branch information
Zalathar committed Sep 12, 2024
commit 2344133ba6abfe54e48cafecca2dff53a9484b07
30 changes: 13 additions & 17 deletions compiler/rustc_mir_transform/src/coverage/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ impl CoverageCounters {
BcbCounter::Expression { id }
}

/// Variant of `make_expression` that makes `lhs` optional and assumes [`Op::Add`].
/// Creates a counter that is the sum of the given counters.
///
/// This is useful when using [`Iterator::fold`] to build an arbitrary-length sum.
fn make_sum_expression(&mut self, lhs: Option<BcbCounter>, rhs: BcbCounter) -> BcbCounter {
let Some(lhs) = lhs else { return rhs };
self.make_expression(lhs, Op::Add, rhs)
/// Returns `None` if the given list of counters was empty.
fn make_sum(&mut self, counters: &[BcbCounter]) -> Option<BcbCounter> {
counters
.iter()
.copied()
.reduce(|accum, counter| self.make_expression(accum, Op::Add, counter))
}

pub(super) fn num_counters(&self) -> usize {
Expand Down Expand Up @@ -322,12 +324,9 @@ impl<'a> MakeBcbCounters<'a> {
.filter(|&to_bcb| to_bcb != expression_to_bcb)
.map(|to_bcb| self.get_or_make_edge_counter(from_bcb, to_bcb))
.collect::<Vec<_>>();
let sum_of_all_other_out_edges: BcbCounter = other_out_edge_counters
.iter()
.copied()
.fold(None, |accum, edge_counter| {
Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
})
let sum_of_all_other_out_edges: BcbCounter = self
.coverage_counters
.make_sum(&other_out_edge_counters)
.expect("there must be at least one other out-edge");

// Now create an expression for the chosen edge, by taking the counter
Expand Down Expand Up @@ -380,12 +379,9 @@ impl<'a> MakeBcbCounters<'a> {
.copied()
.map(|from_bcb| self.get_or_make_edge_counter(from_bcb, bcb))
.collect::<Vec<_>>();
let sum_of_in_edges: BcbCounter = in_edge_counters
.iter()
.copied()
.fold(None, |accum, edge_counter| {
Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
})
let sum_of_in_edges: BcbCounter = self
.coverage_counters
.make_sum(&in_edge_counters)
.expect("there must be at least one in-edge");

debug!("{bcb:?} gets a new counter (sum of predecessor counters): {sum_of_in_edges:?}");
Expand Down
Loading