coverage: Split out MC/DC decisions from BcbMappingKind

This commit is contained in:
Zalathar 2024-05-02 12:30:07 +10:00
parent d2d24e395a
commit af33fc85de
2 changed files with 71 additions and 51 deletions

View file

@ -2,8 +2,9 @@ use std::collections::BTreeSet;
use rustc_data_structures::graph::DirectedGraph; use rustc_data_structures::graph::DirectedGraph;
use rustc_index::bit_set::BitSet; use rustc_index::bit_set::BitSet;
use rustc_index::IndexVec;
use rustc_middle::mir::coverage::{ use rustc_middle::mir::coverage::{
BlockMarkerId, BranchSpan, ConditionInfo, CoverageKind, MCDCBranchSpan, MCDCDecisionSpan, BlockMarkerId, BranchSpan, ConditionInfo, CoverageKind, MCDCBranchSpan,
}; };
use rustc_middle::mir::{self, BasicBlock, StatementKind}; use rustc_middle::mir::{self, BasicBlock, StatementKind};
use rustc_span::Span; use rustc_span::Span;
@ -13,7 +14,6 @@ use crate::coverage::spans::{
extract_refined_covspans, unexpand_into_body_span_with_visible_macro, extract_refined_covspans, unexpand_into_body_span_with_visible_macro,
}; };
use crate::coverage::ExtractedHirInfo; use crate::coverage::ExtractedHirInfo;
use rustc_index::IndexVec;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(super) enum BcbMappingKind { pub(super) enum BcbMappingKind {
@ -32,13 +32,6 @@ pub(super) enum BcbMappingKind {
condition_info: Option<ConditionInfo>, condition_info: Option<ConditionInfo>,
decision_depth: u16, decision_depth: u16,
}, },
/// Associates a mcdc decision with its join BCB.
MCDCDecision {
end_bcbs: BTreeSet<BasicCoverageBlock>,
bitmap_idx: u32,
conditions_num: u16,
decision_depth: u16,
},
} }
#[derive(Debug)] #[derive(Debug)]
@ -57,11 +50,22 @@ pub(super) struct BcbBranchPair {
pub(super) false_bcb: BasicCoverageBlock, pub(super) false_bcb: BasicCoverageBlock,
} }
/// Associates an MC/DC decision with its join BCBs.
#[derive(Debug)]
pub(super) struct MCDCDecision {
pub(super) span: Span,
pub(super) end_bcbs: BTreeSet<BasicCoverageBlock>,
pub(super) bitmap_idx: u32,
pub(super) conditions_num: u16,
pub(super) decision_depth: u16,
}
pub(super) struct CoverageSpans { pub(super) struct CoverageSpans {
bcb_has_mappings: BitSet<BasicCoverageBlock>, bcb_has_mappings: BitSet<BasicCoverageBlock>,
pub(super) mappings: Vec<BcbMapping>, pub(super) mappings: Vec<BcbMapping>,
pub(super) branch_pairs: Vec<BcbBranchPair>, pub(super) branch_pairs: Vec<BcbBranchPair>,
test_vector_bitmap_bytes: u32, test_vector_bitmap_bytes: u32,
pub(super) mcdc_decisions: Vec<MCDCDecision>,
} }
impl CoverageSpans { impl CoverageSpans {
@ -85,6 +89,7 @@ pub(super) fn generate_coverage_spans(
) -> Option<CoverageSpans> { ) -> Option<CoverageSpans> {
let mut mappings = vec![]; let mut mappings = vec![];
let mut branch_pairs = vec![]; let mut branch_pairs = vec![];
let mut mcdc_decisions = vec![];
if hir_info.is_async_fn { if hir_info.is_async_fn {
// An async function desugars into a function that returns a future, // An async function desugars into a function that returns a future,
@ -99,10 +104,15 @@ pub(super) fn generate_coverage_spans(
branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, basic_coverage_blocks)); branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, basic_coverage_blocks));
mappings.extend(extract_mcdc_mappings(mir_body, hir_info.body_span, basic_coverage_blocks)); mappings.extend(extract_mcdc_mappings(
mir_body,
hir_info.body_span,
basic_coverage_blocks,
&mut mcdc_decisions,
));
} }
if mappings.is_empty() && branch_pairs.is_empty() { if mappings.is_empty() && branch_pairs.is_empty() && mcdc_decisions.is_empty() {
return None; return None;
} }
@ -111,7 +121,7 @@ pub(super) fn generate_coverage_spans(
let mut insert = |bcb| { let mut insert = |bcb| {
bcb_has_mappings.insert(bcb); bcb_has_mappings.insert(bcb);
}; };
let mut test_vector_bitmap_bytes = 0;
for BcbMapping { kind, span: _ } in &mappings { for BcbMapping { kind, span: _ } in &mappings {
match *kind { match *kind {
BcbMappingKind::Code(bcb) => insert(bcb), BcbMappingKind::Code(bcb) => insert(bcb),
@ -119,13 +129,6 @@ pub(super) fn generate_coverage_spans(
insert(true_bcb); insert(true_bcb);
insert(false_bcb); insert(false_bcb);
} }
BcbMappingKind::MCDCDecision { bitmap_idx, conditions_num, .. } => {
// `bcb_has_mappings` is used for inject coverage counters
// but they are not needed for decision BCBs.
// While the length of test vector bitmap should be calculated here.
test_vector_bitmap_bytes = test_vector_bitmap_bytes
.max(bitmap_idx + (1_u32 << conditions_num as u32).div_ceil(8));
}
} }
} }
for &BcbBranchPair { true_bcb, false_bcb, .. } in &branch_pairs { for &BcbBranchPair { true_bcb, false_bcb, .. } in &branch_pairs {
@ -133,7 +136,22 @@ pub(super) fn generate_coverage_spans(
insert(false_bcb); insert(false_bcb);
} }
Some(CoverageSpans { bcb_has_mappings, mappings, branch_pairs, test_vector_bitmap_bytes }) // Determine the length of the test vector bitmap.
let test_vector_bitmap_bytes = mcdc_decisions
.iter()
.map(|&MCDCDecision { bitmap_idx, conditions_num, .. }| {
bitmap_idx + (1_u32 << u32::from(conditions_num)).div_ceil(8)
})
.max()
.unwrap_or(0);
Some(CoverageSpans {
bcb_has_mappings,
mappings,
branch_pairs,
test_vector_bitmap_bytes,
mcdc_decisions,
})
} }
fn resolve_block_markers( fn resolve_block_markers(
@ -199,6 +217,7 @@ pub(super) fn extract_mcdc_mappings(
mir_body: &mir::Body<'_>, mir_body: &mir::Body<'_>,
body_span: Span, body_span: Span,
basic_coverage_blocks: &CoverageGraph, basic_coverage_blocks: &CoverageGraph,
mcdc_decisions: &mut impl Extend<MCDCDecision>,
) -> Vec<BcbMapping> { ) -> Vec<BcbMapping> {
let Some(branch_info) = mir_body.coverage_branch_info.as_deref() else { let Some(branch_info) = mir_body.coverage_branch_info.as_deref() else {
return vec![]; return vec![];
@ -245,31 +264,30 @@ pub(super) fn extract_mcdc_mappings(
let mut next_bitmap_idx = 0; let mut next_bitmap_idx = 0;
let decision_filter_map = |decision: &MCDCDecisionSpan| { mcdc_decisions.extend(branch_info.mcdc_decision_spans.iter().filter_map(
let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?; |decision: &mir::coverage::MCDCDecisionSpan| {
let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?;
let end_bcbs = decision let end_bcbs = decision
.end_markers .end_markers
.iter() .iter()
.map(|&marker| bcb_from_marker(marker)) .map(|&marker| bcb_from_marker(marker))
.collect::<Option<_>>()?; .collect::<Option<_>>()?;
let bitmap_idx = next_bitmap_idx; let bitmap_idx = next_bitmap_idx;
next_bitmap_idx += (1_u32 << decision.conditions_num).div_ceil(8); next_bitmap_idx += (1_u32 << decision.conditions_num).div_ceil(8);
Some(BcbMapping { Some(MCDCDecision {
kind: BcbMappingKind::MCDCDecision { span,
end_bcbs, end_bcbs,
bitmap_idx, bitmap_idx,
conditions_num: decision.conditions_num as u16, conditions_num: decision.conditions_num as u16,
decision_depth: decision.decision_depth, decision_depth: decision.decision_depth,
}, })
span, },
}) ));
};
std::iter::empty() std::iter::empty()
.chain(branch_info.mcdc_branch_spans.iter().filter_map(mcdc_branch_filter_map)) .chain(branch_info.mcdc_branch_spans.iter().filter_map(mcdc_branch_filter_map))
.chain(branch_info.mcdc_decision_spans.iter().filter_map(decision_filter_map))
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }

View file

@ -103,12 +103,9 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
inject_mcdc_statements(mir_body, &basic_coverage_blocks, &coverage_spans); inject_mcdc_statements(mir_body, &basic_coverage_blocks, &coverage_spans);
let mcdc_num_condition_bitmaps = coverage_spans let mcdc_num_condition_bitmaps = coverage_spans
.mappings .mcdc_decisions
.iter() .iter()
.filter_map(|bcb_mapping| match bcb_mapping.kind { .map(|&mappings::MCDCDecision { decision_depth, .. }| decision_depth)
BcbMappingKind::MCDCDecision { decision_depth, .. } => Some(decision_depth),
_ => None,
})
.max() .max()
.map_or(0, |max| usize::from(max) + 1); .map_or(0, |max| usize::from(max) + 1);
@ -172,9 +169,6 @@ fn create_mappings<'tcx>(
false_term: term_for_bcb(false_bcb), false_term: term_for_bcb(false_bcb),
mcdc_params, mcdc_params,
}, },
BcbMappingKind::MCDCDecision { bitmap_idx, conditions_num, .. } => {
MappingKind::MCDCDecision(DecisionInfo { bitmap_idx, conditions_num })
}
}; };
let code_region = make_code_region(source_map, file_name, *span, body_span)?; let code_region = make_code_region(source_map, file_name, *span, body_span)?;
Some(Mapping { kind, code_region }) Some(Mapping { kind, code_region })
@ -191,6 +185,14 @@ fn create_mappings<'tcx>(
}, },
)); ));
mappings.extend(coverage_spans.mcdc_decisions.iter().filter_map(
|&mappings::MCDCDecision { span, bitmap_idx, conditions_num, .. }| {
let code_region = make_code_region(source_map, file_name, span, body_span)?;
let kind = MappingKind::MCDCDecision(DecisionInfo { bitmap_idx, conditions_num });
Some(Mapping { kind, code_region })
},
));
mappings mappings
} }
@ -258,13 +260,13 @@ fn inject_mcdc_statements<'tcx>(
} }
// Inject test vector update first because `inject_statement` always insert new statement at head. // Inject test vector update first because `inject_statement` always insert new statement at head.
for (end_bcbs, bitmap_idx, decision_depth) in for &mappings::MCDCDecision {
coverage_spans.mappings.iter().filter_map(|mapping| match &mapping.kind { span: _,
BcbMappingKind::MCDCDecision { end_bcbs, bitmap_idx, decision_depth, .. } => { ref end_bcbs,
Some((end_bcbs, *bitmap_idx, *decision_depth)) bitmap_idx,
} conditions_num: _,
_ => None, decision_depth,
}) } in &coverage_spans.mcdc_decisions
{ {
for end in end_bcbs { for end in end_bcbs {
let end_bb = basic_coverage_blocks[*end].leader_bb(); let end_bb = basic_coverage_blocks[*end].leader_bb();