coverage: Reduce/simplify visibility in coverage::graph

Using `pub(super)` makes it harder to move code between modules, and doesn't
provide much privacy benefit over `pub(crate)`.
This commit is contained in:
Zalathar 2024-06-16 14:04:36 +10:00
parent 5eb30f0699
commit 917b455a87

View File

@ -14,16 +14,16 @@
/// A coverage-specific simplification of the MIR control flow graph (CFG). The `CoverageGraph`s /// A coverage-specific simplification of the MIR control flow graph (CFG). The `CoverageGraph`s
/// nodes are `BasicCoverageBlock`s, which encompass one or more MIR `BasicBlock`s. /// nodes are `BasicCoverageBlock`s, which encompass one or more MIR `BasicBlock`s.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct CoverageGraph { pub(crate) struct CoverageGraph {
bcbs: IndexVec<BasicCoverageBlock, BasicCoverageBlockData>, bcbs: IndexVec<BasicCoverageBlock, BasicCoverageBlockData>,
bb_to_bcb: IndexVec<BasicBlock, Option<BasicCoverageBlock>>, bb_to_bcb: IndexVec<BasicBlock, Option<BasicCoverageBlock>>,
pub successors: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>, pub(crate) successors: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
pub predecessors: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>, pub(crate) predecessors: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
dominators: Option<Dominators<BasicCoverageBlock>>, dominators: Option<Dominators<BasicCoverageBlock>>,
} }
impl CoverageGraph { impl CoverageGraph {
pub fn from_mir(mir_body: &mir::Body<'_>) -> Self { pub(crate) fn from_mir(mir_body: &mir::Body<'_>) -> Self {
let (bcbs, bb_to_bcb) = Self::compute_basic_coverage_blocks(mir_body); let (bcbs, bb_to_bcb) = Self::compute_basic_coverage_blocks(mir_body);
// Pre-transform MIR `BasicBlock` successors and predecessors into the BasicCoverageBlock // Pre-transform MIR `BasicBlock` successors and predecessors into the BasicCoverageBlock
@ -135,24 +135,28 @@ fn compute_basic_coverage_blocks(
} }
#[inline(always)] #[inline(always)]
pub fn iter_enumerated( pub(crate) fn iter_enumerated(
&self, &self,
) -> impl Iterator<Item = (BasicCoverageBlock, &BasicCoverageBlockData)> { ) -> impl Iterator<Item = (BasicCoverageBlock, &BasicCoverageBlockData)> {
self.bcbs.iter_enumerated() self.bcbs.iter_enumerated()
} }
#[inline(always)] #[inline(always)]
pub fn bcb_from_bb(&self, bb: BasicBlock) -> Option<BasicCoverageBlock> { pub(crate) fn bcb_from_bb(&self, bb: BasicBlock) -> Option<BasicCoverageBlock> {
if bb.index() < self.bb_to_bcb.len() { self.bb_to_bcb[bb] } else { None } if bb.index() < self.bb_to_bcb.len() { self.bb_to_bcb[bb] } else { None }
} }
#[inline(always)] #[inline(always)]
pub fn dominates(&self, dom: BasicCoverageBlock, node: BasicCoverageBlock) -> bool { pub(crate) fn dominates(&self, dom: BasicCoverageBlock, node: BasicCoverageBlock) -> bool {
self.dominators.as_ref().unwrap().dominates(dom, node) self.dominators.as_ref().unwrap().dominates(dom, node)
} }
#[inline(always)] #[inline(always)]
pub fn cmp_in_dominator_order(&self, a: BasicCoverageBlock, b: BasicCoverageBlock) -> Ordering { pub(crate) fn cmp_in_dominator_order(
&self,
a: BasicCoverageBlock,
b: BasicCoverageBlock,
) -> Ordering {
self.dominators.as_ref().unwrap().cmp_in_dominator_order(a, b) self.dominators.as_ref().unwrap().cmp_in_dominator_order(a, b)
} }
@ -166,7 +170,7 @@ pub fn cmp_in_dominator_order(&self, a: BasicCoverageBlock, b: BasicCoverageBloc
/// ///
/// FIXME: That assumption might not be true for [`TerminatorKind::Yield`]? /// FIXME: That assumption might not be true for [`TerminatorKind::Yield`]?
#[inline(always)] #[inline(always)]
pub(super) fn bcb_has_multiple_in_edges(&self, bcb: BasicCoverageBlock) -> bool { pub(crate) fn bcb_has_multiple_in_edges(&self, bcb: BasicCoverageBlock) -> bool {
// Even though bcb0 conceptually has an extra virtual in-edge due to // Even though bcb0 conceptually has an extra virtual in-edge due to
// being the entry point, we've already asserted that it has no _other_ // being the entry point, we've already asserted that it has no _other_
// in-edges, so there's no possibility of it having _multiple_ in-edges. // in-edges, so there's no possibility of it having _multiple_ in-edges.
@ -227,7 +231,7 @@ fn predecessors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
/// A node in the control-flow graph of CoverageGraph. /// A node in the control-flow graph of CoverageGraph.
#[orderable] #[orderable]
#[debug_format = "bcb{}"] #[debug_format = "bcb{}"]
pub(super) struct BasicCoverageBlock { pub(crate) struct BasicCoverageBlock {
const START_BCB = 0; const START_BCB = 0;
} }
} }
@ -259,23 +263,23 @@ pub(super) struct BasicCoverageBlock {
/// queries (`dominates()`, `predecessors`, `successors`, etc.) have branch (control flow) /// queries (`dominates()`, `predecessors`, `successors`, etc.) have branch (control flow)
/// significance. /// significance.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(super) struct BasicCoverageBlockData { pub(crate) struct BasicCoverageBlockData {
pub basic_blocks: Vec<BasicBlock>, pub(crate) basic_blocks: Vec<BasicBlock>,
} }
impl BasicCoverageBlockData { impl BasicCoverageBlockData {
pub fn from(basic_blocks: Vec<BasicBlock>) -> Self { fn from(basic_blocks: Vec<BasicBlock>) -> Self {
assert!(basic_blocks.len() > 0); assert!(basic_blocks.len() > 0);
Self { basic_blocks } Self { basic_blocks }
} }
#[inline(always)] #[inline(always)]
pub fn leader_bb(&self) -> BasicBlock { pub(crate) fn leader_bb(&self) -> BasicBlock {
self.basic_blocks[0] self.basic_blocks[0]
} }
#[inline(always)] #[inline(always)]
pub fn last_bb(&self) -> BasicBlock { pub(crate) fn last_bb(&self) -> BasicBlock {
*self.basic_blocks.last().unwrap() *self.basic_blocks.last().unwrap()
} }
} }
@ -364,7 +368,7 @@ fn bcb_filtered_successors<'a, 'tcx>(terminator: &'a Terminator<'tcx>) -> Covera
/// CoverageGraph outside all loops. This supports traversing the BCB CFG in a way that /// CoverageGraph outside all loops. This supports traversing the BCB CFG in a way that
/// ensures a loop is completely traversed before processing Blocks after the end of the loop. /// ensures a loop is completely traversed before processing Blocks after the end of the loop.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct TraversalContext { struct TraversalContext {
/// BCB with one or more incoming loop backedges, indicating which loop /// BCB with one or more incoming loop backedges, indicating which loop
/// this context is for. /// this context is for.
/// ///
@ -375,7 +379,7 @@ pub(super) struct TraversalContext {
worklist: VecDeque<BasicCoverageBlock>, worklist: VecDeque<BasicCoverageBlock>,
} }
pub(super) struct TraverseCoverageGraphWithLoops<'a> { pub(crate) struct TraverseCoverageGraphWithLoops<'a> {
basic_coverage_blocks: &'a CoverageGraph, basic_coverage_blocks: &'a CoverageGraph,
backedges: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>, backedges: IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>>,
@ -384,7 +388,7 @@ pub(super) struct TraverseCoverageGraphWithLoops<'a> {
} }
impl<'a> TraverseCoverageGraphWithLoops<'a> { impl<'a> TraverseCoverageGraphWithLoops<'a> {
pub(super) fn new(basic_coverage_blocks: &'a CoverageGraph) -> Self { pub(crate) fn new(basic_coverage_blocks: &'a CoverageGraph) -> Self {
let backedges = find_loop_backedges(basic_coverage_blocks); let backedges = find_loop_backedges(basic_coverage_blocks);
let worklist = VecDeque::from([basic_coverage_blocks.start_node()]); let worklist = VecDeque::from([basic_coverage_blocks.start_node()]);
@ -400,7 +404,7 @@ pub(super) fn new(basic_coverage_blocks: &'a CoverageGraph) -> Self {
/// For each loop on the loop context stack (top-down), yields a list of BCBs /// For each loop on the loop context stack (top-down), yields a list of BCBs
/// within that loop that have an outgoing edge back to the loop header. /// within that loop that have an outgoing edge back to the loop header.
pub(super) fn reloop_bcbs_per_loop(&self) -> impl Iterator<Item = &[BasicCoverageBlock]> { pub(crate) fn reloop_bcbs_per_loop(&self) -> impl Iterator<Item = &[BasicCoverageBlock]> {
self.context_stack self.context_stack
.iter() .iter()
.rev() .rev()
@ -408,7 +412,7 @@ pub(super) fn reloop_bcbs_per_loop(&self) -> impl Iterator<Item = &[BasicCoverag
.map(|header_bcb| self.backedges[header_bcb].as_slice()) .map(|header_bcb| self.backedges[header_bcb].as_slice())
} }
pub(super) fn next(&mut self) -> Option<BasicCoverageBlock> { pub(crate) fn next(&mut self) -> Option<BasicCoverageBlock> {
debug!( debug!(
"TraverseCoverageGraphWithLoops::next - context_stack: {:?}", "TraverseCoverageGraphWithLoops::next - context_stack: {:?}",
self.context_stack.iter().rev().collect::<Vec<_>>() self.context_stack.iter().rev().collect::<Vec<_>>()
@ -440,7 +444,7 @@ pub(super) fn next(&mut self) -> Option<BasicCoverageBlock> {
None None
} }
pub fn add_successors_to_worklists(&mut self, bcb: BasicCoverageBlock) { fn add_successors_to_worklists(&mut self, bcb: BasicCoverageBlock) {
let successors = &self.basic_coverage_blocks.successors[bcb]; let successors = &self.basic_coverage_blocks.successors[bcb];
debug!("{:?} has {} successors:", bcb, successors.len()); debug!("{:?} has {} successors:", bcb, successors.len());
@ -494,11 +498,11 @@ pub fn add_successors_to_worklists(&mut self, bcb: BasicCoverageBlock) {
} }
} }
pub fn is_complete(&self) -> bool { pub(crate) fn is_complete(&self) -> bool {
self.visited.count() == self.visited.domain_size() self.visited.count() == self.visited.domain_size()
} }
pub fn unvisited(&self) -> Vec<BasicCoverageBlock> { pub(crate) fn unvisited(&self) -> Vec<BasicCoverageBlock> {
let mut unvisited_set: BitSet<BasicCoverageBlock> = let mut unvisited_set: BitSet<BasicCoverageBlock> =
BitSet::new_filled(self.visited.domain_size()); BitSet::new_filled(self.visited.domain_size());
unvisited_set.subtract(&self.visited); unvisited_set.subtract(&self.visited);
@ -506,7 +510,7 @@ pub fn unvisited(&self) -> Vec<BasicCoverageBlock> {
} }
} }
pub(super) fn find_loop_backedges( fn find_loop_backedges(
basic_coverage_blocks: &CoverageGraph, basic_coverage_blocks: &CoverageGraph,
) -> IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>> { ) -> IndexVec<BasicCoverageBlock, Vec<BasicCoverageBlock>> {
let num_bcbs = basic_coverage_blocks.num_nodes(); let num_bcbs = basic_coverage_blocks.num_nodes();