From af33fc85de33e72e798fd9518c078b6b8f44c7b0 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 12:30:07 +1000 Subject: [PATCH 1/5] coverage: Split out MC/DC decisions from `BcbMappingKind` --- .../src/coverage/mappings.rs | 90 +++++++++++-------- .../rustc_mir_transform/src/coverage/mod.rs | 32 +++---- 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index d364658efb6d6..79c335c8dd25f 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -2,8 +2,9 @@ use std::collections::BTreeSet; use rustc_data_structures::graph::DirectedGraph; use rustc_index::bit_set::BitSet; +use rustc_index::IndexVec; 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_span::Span; @@ -13,7 +14,6 @@ use crate::coverage::spans::{ extract_refined_covspans, unexpand_into_body_span_with_visible_macro, }; use crate::coverage::ExtractedHirInfo; -use rustc_index::IndexVec; #[derive(Clone, Debug)] pub(super) enum BcbMappingKind { @@ -32,13 +32,6 @@ pub(super) enum BcbMappingKind { condition_info: Option, decision_depth: u16, }, - /// Associates a mcdc decision with its join BCB. - MCDCDecision { - end_bcbs: BTreeSet, - bitmap_idx: u32, - conditions_num: u16, - decision_depth: u16, - }, } #[derive(Debug)] @@ -57,11 +50,22 @@ pub(super) struct BcbBranchPair { 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, + pub(super) bitmap_idx: u32, + pub(super) conditions_num: u16, + pub(super) decision_depth: u16, +} + pub(super) struct CoverageSpans { bcb_has_mappings: BitSet, pub(super) mappings: Vec, pub(super) branch_pairs: Vec, test_vector_bitmap_bytes: u32, + pub(super) mcdc_decisions: Vec, } impl CoverageSpans { @@ -85,6 +89,7 @@ pub(super) fn generate_coverage_spans( ) -> Option { let mut mappings = vec![]; let mut branch_pairs = vec![]; + let mut mcdc_decisions = vec![]; if hir_info.is_async_fn { // 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)); - 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; } @@ -111,7 +121,7 @@ pub(super) fn generate_coverage_spans( let mut insert = |bcb| { bcb_has_mappings.insert(bcb); }; - let mut test_vector_bitmap_bytes = 0; + for BcbMapping { kind, span: _ } in &mappings { match *kind { BcbMappingKind::Code(bcb) => insert(bcb), @@ -119,13 +129,6 @@ pub(super) fn generate_coverage_spans( insert(true_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 { @@ -133,7 +136,22 @@ pub(super) fn generate_coverage_spans( 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( @@ -199,6 +217,7 @@ pub(super) fn extract_mcdc_mappings( mir_body: &mir::Body<'_>, body_span: Span, basic_coverage_blocks: &CoverageGraph, + mcdc_decisions: &mut impl Extend, ) -> Vec { let Some(branch_info) = mir_body.coverage_branch_info.as_deref() else { return vec![]; @@ -245,31 +264,30 @@ pub(super) fn extract_mcdc_mappings( let mut next_bitmap_idx = 0; - let decision_filter_map = |decision: &MCDCDecisionSpan| { - let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?; + mcdc_decisions.extend(branch_info.mcdc_decision_spans.iter().filter_map( + |decision: &mir::coverage::MCDCDecisionSpan| { + let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?; - let end_bcbs = decision - .end_markers - .iter() - .map(|&marker| bcb_from_marker(marker)) - .collect::>()?; + let end_bcbs = decision + .end_markers + .iter() + .map(|&marker| bcb_from_marker(marker)) + .collect::>()?; - let bitmap_idx = next_bitmap_idx; - next_bitmap_idx += (1_u32 << decision.conditions_num).div_ceil(8); + let bitmap_idx = next_bitmap_idx; + next_bitmap_idx += (1_u32 << decision.conditions_num).div_ceil(8); - Some(BcbMapping { - kind: BcbMappingKind::MCDCDecision { + Some(MCDCDecision { + span, end_bcbs, bitmap_idx, conditions_num: decision.conditions_num as u16, decision_depth: decision.decision_depth, - }, - span, - }) - }; + }) + }, + )); std::iter::empty() .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::>() } diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index ffe61e761c53d..24df197b60107 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -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); let mcdc_num_condition_bitmaps = coverage_spans - .mappings + .mcdc_decisions .iter() - .filter_map(|bcb_mapping| match bcb_mapping.kind { - BcbMappingKind::MCDCDecision { decision_depth, .. } => Some(decision_depth), - _ => None, - }) + .map(|&mappings::MCDCDecision { decision_depth, .. }| decision_depth) .max() .map_or(0, |max| usize::from(max) + 1); @@ -172,9 +169,6 @@ fn create_mappings<'tcx>( false_term: term_for_bcb(false_bcb), 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)?; 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 } @@ -258,13 +260,13 @@ fn inject_mcdc_statements<'tcx>( } // Inject test vector update first because `inject_statement` always insert new statement at head. - for (end_bcbs, bitmap_idx, decision_depth) in - coverage_spans.mappings.iter().filter_map(|mapping| match &mapping.kind { - BcbMappingKind::MCDCDecision { end_bcbs, bitmap_idx, decision_depth, .. } => { - Some((end_bcbs, *bitmap_idx, *decision_depth)) - } - _ => None, - }) + for &mappings::MCDCDecision { + span: _, + ref end_bcbs, + bitmap_idx, + conditions_num: _, + decision_depth, + } in &coverage_spans.mcdc_decisions { for end in end_bcbs { let end_bb = basic_coverage_blocks[*end].leader_bb(); From 23b6508181dc2c26cd258583d43d42a724806db5 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 12:46:23 +1000 Subject: [PATCH 2/5] coverage: Split out MC/DC branches from `BcbMappingKind` --- .../src/coverage/mappings.rs | 101 +++++++++--------- .../rustc_mir_transform/src/coverage/mod.rs | 51 ++++----- 2 files changed, 72 insertions(+), 80 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index 79c335c8dd25f..568a07aa932cd 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -3,9 +3,7 @@ use std::collections::BTreeSet; use rustc_data_structures::graph::DirectedGraph; use rustc_index::bit_set::BitSet; use rustc_index::IndexVec; -use rustc_middle::mir::coverage::{ - BlockMarkerId, BranchSpan, ConditionInfo, CoverageKind, MCDCBranchSpan, -}; +use rustc_middle::mir::coverage::{BlockMarkerId, BranchSpan, ConditionInfo, CoverageKind}; use rustc_middle::mir::{self, BasicBlock, StatementKind}; use rustc_span::Span; @@ -15,23 +13,13 @@ use crate::coverage::spans::{ }; use crate::coverage::ExtractedHirInfo; -#[derive(Clone, Debug)] +#[derive(Clone, Copy, Debug)] pub(super) enum BcbMappingKind { /// Associates an ordinary executable code span with its corresponding BCB. Code(BasicCoverageBlock), - - // Ordinary branch mappings are stored separately, so they don't have a - // variant in this enum. // - /// Associates a mcdc branch span with condition info besides fields for normal branch. - MCDCBranch { - true_bcb: BasicCoverageBlock, - false_bcb: BasicCoverageBlock, - /// If `None`, this actually represents a normal branch mapping inserted - /// for code that was too complex for MC/DC. - condition_info: Option, - decision_depth: u16, - }, + // Branch and MC/DC mappings are more complex, so they are represented + // separately. } #[derive(Debug)] @@ -50,6 +38,18 @@ pub(super) struct BcbBranchPair { pub(super) false_bcb: BasicCoverageBlock, } +/// Associates an MC/DC branch span with condition info besides fields for normal branch. +#[derive(Debug)] +pub(super) struct MCDCBranch { + pub(super) span: Span, + pub(super) true_bcb: BasicCoverageBlock, + pub(super) false_bcb: BasicCoverageBlock, + /// If `None`, this actually represents a normal branch mapping inserted + /// for code that was too complex for MC/DC. + pub(super) condition_info: Option, + pub(super) decision_depth: u16, +} + /// Associates an MC/DC decision with its join BCBs. #[derive(Debug)] pub(super) struct MCDCDecision { @@ -65,6 +65,7 @@ pub(super) struct CoverageSpans { pub(super) mappings: Vec, pub(super) branch_pairs: Vec, test_vector_bitmap_bytes: u32, + pub(super) mcdc_branches: Vec, pub(super) mcdc_decisions: Vec, } @@ -89,6 +90,7 @@ pub(super) fn generate_coverage_spans( ) -> Option { let mut mappings = vec![]; let mut branch_pairs = vec![]; + let mut mcdc_branches = vec![]; let mut mcdc_decisions = vec![]; if hir_info.is_async_fn { @@ -104,15 +106,20 @@ pub(super) fn generate_coverage_spans( branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, basic_coverage_blocks)); - mappings.extend(extract_mcdc_mappings( + extract_mcdc_mappings( mir_body, hir_info.body_span, basic_coverage_blocks, + &mut mcdc_branches, &mut mcdc_decisions, - )); + ); } - if mappings.is_empty() && branch_pairs.is_empty() && mcdc_decisions.is_empty() { + if mappings.is_empty() + && branch_pairs.is_empty() + && mcdc_branches.is_empty() + && mcdc_decisions.is_empty() + { return None; } @@ -122,19 +129,19 @@ pub(super) fn generate_coverage_spans( bcb_has_mappings.insert(bcb); }; - for BcbMapping { kind, span: _ } in &mappings { - match *kind { + for &BcbMapping { kind, span: _ } in &mappings { + match kind { BcbMappingKind::Code(bcb) => insert(bcb), - BcbMappingKind::MCDCBranch { true_bcb, false_bcb, .. } => { - insert(true_bcb); - insert(false_bcb); - } } } for &BcbBranchPair { true_bcb, false_bcb, .. } in &branch_pairs { insert(true_bcb); insert(false_bcb); } + for &MCDCBranch { true_bcb, false_bcb, .. } in &mcdc_branches { + insert(true_bcb); + insert(false_bcb); + } // Determine the length of the test vector bitmap. let test_vector_bitmap_bytes = mcdc_decisions @@ -150,6 +157,7 @@ pub(super) fn generate_coverage_spans( mappings, branch_pairs, test_vector_bitmap_bytes, + mcdc_branches, mcdc_decisions, }) } @@ -217,11 +225,10 @@ pub(super) fn extract_mcdc_mappings( mir_body: &mir::Body<'_>, body_span: Span, basic_coverage_blocks: &CoverageGraph, + mcdc_branches: &mut impl Extend, mcdc_decisions: &mut impl Extend, -) -> Vec { - let Some(branch_info) = mir_body.coverage_branch_info.as_deref() else { - return vec![]; - }; +) { + let Some(branch_info) = mir_body.coverage_branch_info.as_deref() else { return }; let block_markers = resolve_block_markers(branch_info, mir_body); @@ -242,25 +249,19 @@ pub(super) fn extract_mcdc_mappings( Some((span, true_bcb, false_bcb)) }; - let mcdc_branch_filter_map = |&MCDCBranchSpan { - span: raw_span, - true_marker, - false_marker, - condition_info, - decision_depth, - }| { - check_branch_bcb(raw_span, true_marker, false_marker).map(|(span, true_bcb, false_bcb)| { - BcbMapping { - kind: BcbMappingKind::MCDCBranch { - true_bcb, - false_bcb, - condition_info, - decision_depth, - }, - span, - } - }) - }; + mcdc_branches.extend(branch_info.mcdc_branch_spans.iter().filter_map( + |&mir::coverage::MCDCBranchSpan { + span: raw_span, + condition_info, + true_marker, + false_marker, + decision_depth, + }| { + let (span, true_bcb, false_bcb) = + check_branch_bcb(raw_span, true_marker, false_marker)?; + Some(MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth }) + }, + )); let mut next_bitmap_idx = 0; @@ -286,8 +287,4 @@ pub(super) fn extract_mcdc_mappings( }) }, )); - - std::iter::empty() - .chain(branch_info.mcdc_branch_spans.iter().filter_map(mcdc_branch_filter_map)) - .collect::>() } diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 24df197b60107..fc61e69b6adbb 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -150,27 +150,11 @@ fn create_mappings<'tcx>( let mut mappings = Vec::new(); mappings.extend(coverage_spans.mappings.iter().filter_map( - |BcbMapping { kind: bcb_mapping_kind, span }| { - let kind = match *bcb_mapping_kind { + |&BcbMapping { kind: bcb_mapping_kind, span }| { + let kind = match bcb_mapping_kind { BcbMappingKind::Code(bcb) => MappingKind::Code(term_for_bcb(bcb)), - BcbMappingKind::MCDCBranch { - true_bcb, false_bcb, condition_info: None, .. - } => MappingKind::Branch { - true_term: term_for_bcb(true_bcb), - false_term: term_for_bcb(false_bcb), - }, - BcbMappingKind::MCDCBranch { - true_bcb, - false_bcb, - condition_info: Some(mcdc_params), - .. - } => MappingKind::MCDCBranch { - true_term: term_for_bcb(true_bcb), - false_term: term_for_bcb(false_bcb), - mcdc_params, - }, }; - 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 }) }, )); @@ -185,6 +169,19 @@ fn create_mappings<'tcx>( }, )); + mappings.extend(coverage_spans.mcdc_branches.iter().filter_map( + |&mappings::MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth: _ }| { + let code_region = make_code_region(source_map, file_name, span, body_span)?; + let true_term = term_for_bcb(true_bcb); + let false_term = term_for_bcb(false_bcb); + let kind = match condition_info { + Some(mcdc_params) => MappingKind::MCDCBranch { true_term, false_term, mcdc_params }, + None => MappingKind::Branch { true_term, false_term }, + }; + Some(Mapping { kind, code_region }) + }, + )); + 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)?; @@ -278,24 +275,22 @@ fn inject_mcdc_statements<'tcx>( } } - for (true_bcb, false_bcb, condition_id, decision_depth) in - coverage_spans.mappings.iter().filter_map(|mapping| match mapping.kind { - BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info, decision_depth } => { - Some((true_bcb, false_bcb, condition_info?.condition_id, decision_depth)) - } - _ => None, - }) + for &mappings::MCDCBranch { span: _, true_bcb, false_bcb, condition_info, decision_depth } in + &coverage_spans.mcdc_branches { + let Some(condition_info) = condition_info else { continue }; + let id = condition_info.condition_id; + let true_bb = basic_coverage_blocks[true_bcb].leader_bb(); inject_statement( mir_body, - CoverageKind::CondBitmapUpdate { id: condition_id, value: true, decision_depth }, + CoverageKind::CondBitmapUpdate { id, value: true, decision_depth }, true_bb, ); let false_bb = basic_coverage_blocks[false_bcb].leader_bb(); inject_statement( mir_body, - CoverageKind::CondBitmapUpdate { id: condition_id, value: false, decision_depth }, + CoverageKind::CondBitmapUpdate { id, value: false, decision_depth }, false_bb, ); } From cf2d741d4046b0e7dd4f96d17096b86c78846fc9 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 12:51:37 +1000 Subject: [PATCH 3/5] coverage: Extract helper `region_for_span` --- compiler/rustc_mir_transform/src/coverage/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index fc61e69b6adbb..2f03c7a055648 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -146,6 +146,7 @@ fn create_mappings<'tcx>( .expect("all BCBs with spans were given counters") .as_term() }; + let region_for_span = |span: Span| make_code_region(source_map, file_name, span, body_span); let mut mappings = Vec::new(); @@ -154,7 +155,7 @@ fn create_mappings<'tcx>( let kind = match bcb_mapping_kind { BcbMappingKind::Code(bcb) => MappingKind::Code(term_for_bcb(bcb)), }; - let code_region = make_code_region(source_map, file_name, span, body_span)?; + let code_region = region_for_span(span)?; Some(Mapping { kind, code_region }) }, )); @@ -164,14 +165,14 @@ fn create_mappings<'tcx>( let true_term = term_for_bcb(true_bcb); let false_term = term_for_bcb(false_bcb); let kind = MappingKind::Branch { true_term, false_term }; - let code_region = make_code_region(source_map, file_name, span, body_span)?; + let code_region = region_for_span(span)?; Some(Mapping { kind, code_region }) }, )); mappings.extend(coverage_spans.mcdc_branches.iter().filter_map( |&mappings::MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth: _ }| { - let code_region = make_code_region(source_map, file_name, span, body_span)?; + let code_region = region_for_span(span)?; let true_term = term_for_bcb(true_bcb); let false_term = term_for_bcb(false_bcb); let kind = match condition_info { @@ -184,7 +185,7 @@ 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 code_region = region_for_span(span)?; let kind = MappingKind::MCDCDecision(DecisionInfo { bitmap_idx, conditions_num }); Some(Mapping { kind, code_region }) }, From 76d8d01604282316110377162c941499a931d7bf Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 14:18:22 +1000 Subject: [PATCH 4/5] coverage: Flatten `BcbMappingKind` into `mappings::CodeMapping` Now that branch and MC/DC mappings have been split out into separate types and vectors, this enum is no longer needed, since it only represents ordinary "code" regions. (We can revisit this decision if we ever add support for other region kinds, such as skipped regions or expansion regions. But at that point, we might just add new structs/vectors for those kinds as well.) --- .../src/coverage/mappings.rs | 34 +++++++------------ .../rustc_mir_transform/src/coverage/mod.rs | 10 +++--- .../rustc_mir_transform/src/coverage/spans.rs | 8 ++--- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index 568a07aa932cd..a8eac9985e02d 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -13,22 +13,14 @@ use crate::coverage::spans::{ }; use crate::coverage::ExtractedHirInfo; -#[derive(Clone, Copy, Debug)] -pub(super) enum BcbMappingKind { - /// Associates an ordinary executable code span with its corresponding BCB. - Code(BasicCoverageBlock), - // - // Branch and MC/DC mappings are more complex, so they are represented - // separately. -} - +/// Associates an ordinary executable code span with its corresponding BCB. #[derive(Debug)] -pub(super) struct BcbMapping { - pub(super) kind: BcbMappingKind, +pub(super) struct CodeMapping { pub(super) span: Span, + pub(super) bcb: BasicCoverageBlock, } -/// This is separate from [`BcbMappingKind`] to help prepare for larger changes +/// This is separate from [`MCDCBranch`] to help prepare for larger changes /// that will be needed for improved branch coverage in the future. /// (See .) #[derive(Debug)] @@ -62,7 +54,7 @@ pub(super) struct MCDCDecision { pub(super) struct CoverageSpans { bcb_has_mappings: BitSet, - pub(super) mappings: Vec, + pub(super) code_mappings: Vec, pub(super) branch_pairs: Vec, test_vector_bitmap_bytes: u32, pub(super) mcdc_branches: Vec, @@ -88,7 +80,7 @@ pub(super) fn generate_coverage_spans( hir_info: &ExtractedHirInfo, basic_coverage_blocks: &CoverageGraph, ) -> Option { - let mut mappings = vec![]; + let mut code_mappings = vec![]; let mut branch_pairs = vec![]; let mut mcdc_branches = vec![]; let mut mcdc_decisions = vec![]; @@ -99,10 +91,10 @@ pub(super) fn generate_coverage_spans( // outer function will be unhelpful, so just keep the signature span // and ignore all of the spans in the MIR body. if let Some(span) = hir_info.fn_sig_span_extended { - mappings.push(BcbMapping { kind: BcbMappingKind::Code(START_BCB), span }); + code_mappings.push(CodeMapping { span, bcb: START_BCB }); } } else { - extract_refined_covspans(mir_body, hir_info, basic_coverage_blocks, &mut mappings); + extract_refined_covspans(mir_body, hir_info, basic_coverage_blocks, &mut code_mappings); branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, basic_coverage_blocks)); @@ -115,7 +107,7 @@ pub(super) fn generate_coverage_spans( ); } - if mappings.is_empty() + if code_mappings.is_empty() && branch_pairs.is_empty() && mcdc_branches.is_empty() && mcdc_decisions.is_empty() @@ -129,10 +121,8 @@ pub(super) fn generate_coverage_spans( bcb_has_mappings.insert(bcb); }; - for &BcbMapping { kind, span: _ } in &mappings { - match kind { - BcbMappingKind::Code(bcb) => insert(bcb), - } + for &CodeMapping { span: _, bcb } in &code_mappings { + insert(bcb); } for &BcbBranchPair { true_bcb, false_bcb, .. } in &branch_pairs { insert(true_bcb); @@ -154,7 +144,7 @@ pub(super) fn generate_coverage_spans( Some(CoverageSpans { bcb_has_mappings, - mappings, + code_mappings, branch_pairs, test_vector_bitmap_bytes, mcdc_branches, diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 2f03c7a055648..8dfc7af95060f 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -9,7 +9,7 @@ mod tests; use self::counters::{CounterIncrementSite, CoverageCounters}; use self::graph::{BasicCoverageBlock, CoverageGraph}; -use self::mappings::{BcbBranchPair, BcbMapping, BcbMappingKind, CoverageSpans}; +use self::mappings::{BcbBranchPair, CoverageSpans}; use crate::MirPass; @@ -150,12 +150,10 @@ fn create_mappings<'tcx>( let mut mappings = Vec::new(); - mappings.extend(coverage_spans.mappings.iter().filter_map( - |&BcbMapping { kind: bcb_mapping_kind, span }| { - let kind = match bcb_mapping_kind { - BcbMappingKind::Code(bcb) => MappingKind::Code(term_for_bcb(bcb)), - }; + mappings.extend(coverage_spans.code_mappings.iter().filter_map( + |&mappings::CodeMapping { span, bcb }| { let code_region = region_for_span(span)?; + let kind = MappingKind::Code(term_for_bcb(bcb)); Some(Mapping { kind, code_region }) }, )); diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index d6432e2e9d41c..a0570c45f9630 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -2,7 +2,7 @@ use rustc_middle::mir; use rustc_span::{BytePos, Span}; use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph}; -use crate::coverage::mappings::{BcbMapping, BcbMappingKind}; +use crate::coverage::mappings; use crate::coverage::spans::from_mir::SpanFromMir; use crate::coverage::ExtractedHirInfo; @@ -17,14 +17,14 @@ pub(super) fn extract_refined_covspans( mir_body: &mir::Body<'_>, hir_info: &ExtractedHirInfo, basic_coverage_blocks: &CoverageGraph, - mappings: &mut impl Extend, + code_mappings: &mut impl Extend, ) { let sorted_spans = from_mir::mir_to_initial_sorted_coverage_spans(mir_body, hir_info, basic_coverage_blocks); let coverage_spans = SpansRefiner::refine_sorted_spans(sorted_spans); - mappings.extend(coverage_spans.into_iter().map(|RefinedCovspan { bcb, span, .. }| { + code_mappings.extend(coverage_spans.into_iter().map(|RefinedCovspan { bcb, span, .. }| { // Each span produced by the generator represents an ordinary code region. - BcbMapping { kind: BcbMappingKind::Code(bcb), span } + mappings::CodeMapping { span, bcb } })); } From 6968123c3f960da2ec75b049e8a6497eaca67a2b Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 14:39:24 +1000 Subject: [PATCH 5/5] coverage: Rename `BcbBranchPair` to `mappings::BranchPair` This makes it consistent with the other mapping structs introduced by this PR. --- compiler/rustc_mir_transform/src/coverage/mappings.rs | 10 +++++----- compiler/rustc_mir_transform/src/coverage/mod.rs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index a8eac9985e02d..ddbe1333c4b08 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -24,7 +24,7 @@ pub(super) struct CodeMapping { /// that will be needed for improved branch coverage in the future. /// (See .) #[derive(Debug)] -pub(super) struct BcbBranchPair { +pub(super) struct BranchPair { pub(super) span: Span, pub(super) true_bcb: BasicCoverageBlock, pub(super) false_bcb: BasicCoverageBlock, @@ -55,7 +55,7 @@ pub(super) struct MCDCDecision { pub(super) struct CoverageSpans { bcb_has_mappings: BitSet, pub(super) code_mappings: Vec, - pub(super) branch_pairs: Vec, + pub(super) branch_pairs: Vec, test_vector_bitmap_bytes: u32, pub(super) mcdc_branches: Vec, pub(super) mcdc_decisions: Vec, @@ -124,7 +124,7 @@ pub(super) fn generate_coverage_spans( for &CodeMapping { span: _, bcb } in &code_mappings { insert(bcb); } - for &BcbBranchPair { true_bcb, false_bcb, .. } in &branch_pairs { + for &BranchPair { true_bcb, false_bcb, .. } in &branch_pairs { insert(true_bcb); insert(false_bcb); } @@ -183,7 +183,7 @@ pub(super) fn extract_branch_pairs( mir_body: &mir::Body<'_>, hir_info: &ExtractedHirInfo, basic_coverage_blocks: &CoverageGraph, -) -> Vec { +) -> Vec { let Some(branch_info) = mir_body.coverage_branch_info.as_deref() else { return vec![] }; let block_markers = resolve_block_markers(branch_info, mir_body); @@ -206,7 +206,7 @@ pub(super) fn extract_branch_pairs( let true_bcb = bcb_from_marker(true_marker)?; let false_bcb = bcb_from_marker(false_marker)?; - Some(BcbBranchPair { span, true_bcb, false_bcb }) + Some(BranchPair { span, true_bcb, false_bcb }) }) .collect::>() } diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 8dfc7af95060f..1e2599e78e9e1 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -9,7 +9,7 @@ mod tests; use self::counters::{CounterIncrementSite, CoverageCounters}; use self::graph::{BasicCoverageBlock, CoverageGraph}; -use self::mappings::{BcbBranchPair, CoverageSpans}; +use self::mappings::CoverageSpans; use crate::MirPass; @@ -159,7 +159,7 @@ fn create_mappings<'tcx>( )); mappings.extend(coverage_spans.branch_pairs.iter().filter_map( - |&BcbBranchPair { span, true_bcb, false_bcb }| { + |&mappings::BranchPair { span, true_bcb, false_bcb }| { let true_term = term_for_bcb(true_bcb); let false_term = term_for_bcb(false_bcb); let kind = MappingKind::Branch { true_term, false_term };