Skip to content

Commit

Permalink
coverage: Store bcb_needs_counter in a field as a bitset
Browse files Browse the repository at this point in the history
This makes it possible for other parts of counter-assignment to check whether a
node is guaranteed to end up with some kind of counter.

Switching from `impl Fn` to a concrete `&BitSet` just avoids the hassle of
trying to store a closure in a struct field, and currently there's no
foreseeable need for this information to not be a bitset.
  • Loading branch information
Zalathar committed Oct 6, 2024
1 parent c6e4fcd commit ab154e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 13 additions & 6 deletions compiler/rustc_mir_transform/src/coverage/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::graph::DirectedGraph;
use rustc_index::IndexVec;
use rustc_index::bit_set::BitSet;
use rustc_middle::bug;
use rustc_middle::mir::coverage::{CounterId, CovTerm, Expression, ExpressionId, Op};
use tracing::{debug, debug_span, instrument};
Expand Down Expand Up @@ -78,10 +79,10 @@ impl CoverageCounters {
/// counters or counter expressions for nodes and edges as required.
pub(super) fn make_bcb_counters(
basic_coverage_blocks: &CoverageGraph,
bcb_needs_counter: impl Fn(BasicCoverageBlock) -> bool,
bcb_needs_counter: &BitSet<BasicCoverageBlock>,
) -> Self {
let mut counters = MakeBcbCounters::new(basic_coverage_blocks);
counters.make_bcb_counters(bcb_needs_counter);
let mut counters = MakeBcbCounters::new(basic_coverage_blocks, bcb_needs_counter);
counters.make_bcb_counters();

counters.coverage_counters
}
Expand Down Expand Up @@ -268,17 +269,23 @@ impl CoverageCounters {
struct MakeBcbCounters<'a> {
coverage_counters: CoverageCounters,
basic_coverage_blocks: &'a CoverageGraph,
bcb_needs_counter: &'a BitSet<BasicCoverageBlock>,
}

impl<'a> MakeBcbCounters<'a> {
fn new(basic_coverage_blocks: &'a CoverageGraph) -> Self {
fn new(
basic_coverage_blocks: &'a CoverageGraph,
bcb_needs_counter: &'a BitSet<BasicCoverageBlock>,
) -> Self {
assert_eq!(basic_coverage_blocks.num_nodes(), bcb_needs_counter.domain_size());
Self {
coverage_counters: CoverageCounters::with_num_bcbs(basic_coverage_blocks.num_nodes()),
basic_coverage_blocks,
bcb_needs_counter,
}
}

fn make_bcb_counters(&mut self, bcb_needs_counter: impl Fn(BasicCoverageBlock) -> bool) {
fn make_bcb_counters(&mut self) {
debug!("make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock");

// Traverse the coverage graph, ensuring that every node that needs a
Expand All @@ -291,7 +298,7 @@ impl<'a> MakeBcbCounters<'a> {
let mut traversal = TraverseCoverageGraphWithLoops::new(self.basic_coverage_blocks);
while let Some(bcb) = traversal.next() {
let _span = debug_span!("traversal", ?bcb).entered();
if bcb_needs_counter(bcb) {
if self.bcb_needs_counter.contains(bcb) {
self.make_node_counter_and_out_edge_counters(&traversal, bcb);
}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
return;
}

let bcb_has_counter_mappings = |bcb| bcbs_with_counter_mappings.contains(bcb);
let coverage_counters =
CoverageCounters::make_bcb_counters(&basic_coverage_blocks, bcb_has_counter_mappings);
CoverageCounters::make_bcb_counters(&basic_coverage_blocks, &bcbs_with_counter_mappings);

let mappings = create_mappings(tcx, &hir_info, &extracted_mappings, &coverage_counters);
if mappings.is_empty() {
Expand Down

0 comments on commit ab154e6

Please sign in to comment.