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

coverage: Use a separate counter type and simplification step during counter creation #133849

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
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: Rename CounterIncrementSite to just Site
A "site" is a node or edge in the coverage graph.
  • Loading branch information
Zalathar committed Dec 4, 2024
commit 7ecc677f5b35e18967af9b8a18d6b432a2c1ed36
17 changes: 9 additions & 8 deletions compiler/rustc_mir_transform/src/coverage/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ struct BcbExpression {
rhs: BcbCounter,
}

#[derive(Debug)]
pub(super) enum CounterIncrementSite {
/// Enum representing either a node or an edge in the coverage graph.
#[derive(Clone, Copy, Debug)]
pub(super) enum Site {
Node { bcb: BasicCoverageBlock },
Edge { from_bcb: BasicCoverageBlock, to_bcb: BasicCoverageBlock },
}
Expand All @@ -54,7 +55,7 @@ pub(super) enum CounterIncrementSite {
pub(super) struct CoverageCounters {
/// List of places where a counter-increment statement should be injected
/// into MIR, each with its corresponding counter ID.
counter_increment_sites: IndexVec<CounterId, CounterIncrementSite>,
counter_increment_sites: IndexVec<CounterId, Site>,

/// Coverage counters/expressions that are associated with individual BCBs.
node_counters: IndexVec<BasicCoverageBlock, Option<BcbCounter>>,
Expand Down Expand Up @@ -98,14 +99,14 @@ impl CoverageCounters {

/// Shared helper used by [`Self::make_phys_node_counter`] and
/// [`Self::make_phys_edge_counter`]. Don't call this directly.
fn make_counter_inner(&mut self, site: CounterIncrementSite) -> BcbCounter {
fn make_counter_inner(&mut self, site: Site) -> BcbCounter {
let id = self.counter_increment_sites.push(site);
BcbCounter::Counter { id }
}

/// Creates a new physical counter for a BCB node.
fn make_phys_node_counter(&mut self, bcb: BasicCoverageBlock) -> BcbCounter {
self.make_counter_inner(CounterIncrementSite::Node { bcb })
self.make_counter_inner(Site::Node { bcb })
}

/// Creates a new physical counter for a BCB edge.
Expand All @@ -114,7 +115,7 @@ impl CoverageCounters {
from_bcb: BasicCoverageBlock,
to_bcb: BasicCoverageBlock,
) -> BcbCounter {
self.make_counter_inner(CounterIncrementSite::Edge { from_bcb, to_bcb })
self.make_counter_inner(Site::Edge { from_bcb, to_bcb })
}

fn make_expression(&mut self, lhs: BcbCounter, op: Op, rhs: BcbCounter) -> BcbCounter {
Expand Down Expand Up @@ -224,8 +225,8 @@ impl CoverageCounters {
/// each site's corresponding counter ID.
pub(super) fn counter_increment_sites(
&self,
) -> impl Iterator<Item = (CounterId, &CounterIncrementSite)> {
self.counter_increment_sites.iter_enumerated()
) -> impl Iterator<Item = (CounterId, Site)> + Captures<'_> {
self.counter_increment_sites.iter_enumerated().map(|(id, &site)| (id, site))
}

/// Returns an iterator over the subset of BCB nodes that have been associated
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, Pos, SourceFile, Span};
use tracing::{debug, debug_span, trace};

use crate::coverage::counters::{CounterIncrementSite, CoverageCounters};
use crate::coverage::counters::{CoverageCounters, Site};
use crate::coverage::graph::CoverageGraph;
use crate::coverage::mappings::ExtractedMappings;

Expand Down Expand Up @@ -265,13 +265,13 @@ fn inject_coverage_statements<'tcx>(
coverage_counters: &CoverageCounters,
) {
// Inject counter-increment statements into MIR.
for (id, counter_increment_site) in coverage_counters.counter_increment_sites() {
for (id, site) in coverage_counters.counter_increment_sites() {
// Determine the block to inject a counter-increment statement into.
// For BCB nodes this is just their first block, but for edges we need
// to create a new block between the two BCBs, and inject into that.
let target_bb = match *counter_increment_site {
CounterIncrementSite::Node { bcb } => basic_coverage_blocks[bcb].leader_bb(),
CounterIncrementSite::Edge { from_bcb, to_bcb } => {
let target_bb = match site {
Site::Node { bcb } => basic_coverage_blocks[bcb].leader_bb(),
Site::Edge { from_bcb, to_bcb } => {
// Create a new block between the last block of `from_bcb` and
// the first block of `to_bcb`.
let from_bb = basic_coverage_blocks[from_bcb].last_bb();
Expand Down