From 1a26404f10d48b80c8d1ca355266ff7a3182339d Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 16:28:49 +1000 Subject: [PATCH 1/6] coverage: Separately compute the set of BCBs with counter mappings --- .../src/coverage/mappings.rs | 84 +++++++++++-------- .../rustc_mir_transform/src/coverage/mod.rs | 21 +++-- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index ddbe1333c4b08..b2f996e2df78f 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -53,7 +53,6 @@ pub(super) struct MCDCDecision { } pub(super) struct CoverageSpans { - bcb_has_mappings: BitSet, pub(super) code_mappings: Vec, pub(super) branch_pairs: Vec, test_vector_bitmap_bytes: u32, @@ -62,10 +61,6 @@ pub(super) struct CoverageSpans { } impl CoverageSpans { - pub(super) fn bcb_has_coverage_spans(&self, bcb: BasicCoverageBlock) -> bool { - self.bcb_has_mappings.contains(bcb) - } - pub(super) fn test_vector_bitmap_bytes(&self) -> u32 { self.test_vector_bitmap_bytes } @@ -73,13 +68,11 @@ impl CoverageSpans { /// Extracts coverage-relevant spans from MIR, and associates them with /// their corresponding BCBs. -/// -/// Returns `None` if no coverage-relevant spans could be extracted. pub(super) fn generate_coverage_spans( mir_body: &mir::Body<'_>, hir_info: &ExtractedHirInfo, basic_coverage_blocks: &CoverageGraph, -) -> Option { +) -> CoverageSpans { let mut code_mappings = vec![]; let mut branch_pairs = vec![]; let mut mcdc_branches = vec![]; @@ -107,32 +100,6 @@ pub(super) fn generate_coverage_spans( ); } - if code_mappings.is_empty() - && branch_pairs.is_empty() - && mcdc_branches.is_empty() - && mcdc_decisions.is_empty() - { - return None; - } - - // Identify which BCBs have one or more mappings. - let mut bcb_has_mappings = BitSet::new_empty(basic_coverage_blocks.num_nodes()); - let mut insert = |bcb| { - bcb_has_mappings.insert(bcb); - }; - - for &CodeMapping { span: _, bcb } in &code_mappings { - insert(bcb); - } - for &BranchPair { 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 .iter() @@ -142,14 +109,57 @@ pub(super) fn generate_coverage_spans( .max() .unwrap_or(0); - Some(CoverageSpans { - bcb_has_mappings, + CoverageSpans { code_mappings, branch_pairs, test_vector_bitmap_bytes, mcdc_branches, mcdc_decisions, - }) + } +} + +impl CoverageSpans { + pub(super) fn all_bcbs_with_counter_mappings( + &self, + basic_coverage_blocks: &CoverageGraph, // Only used for allocating a correctly-sized set + ) -> BitSet { + // Fully destructure self to make sure we don't miss any fields that have mappings. + let Self { + code_mappings, + branch_pairs, + test_vector_bitmap_bytes: _, + mcdc_branches, + mcdc_decisions, + } = self; + + // Identify which BCBs have one or more mappings. + let mut bcbs_with_counter_mappings = BitSet::new_empty(basic_coverage_blocks.num_nodes()); + let mut insert = |bcb| { + bcbs_with_counter_mappings.insert(bcb); + }; + + for &CodeMapping { span: _, bcb } in code_mappings { + insert(bcb); + } + for &BranchPair { 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); + } + + // MC/DC decisions refer to BCBs, but don't require those BCBs to have counters. + if bcbs_with_counter_mappings.is_empty() { + debug_assert!( + mcdc_decisions.is_empty(), + "A function with no counter mappings shouldn't have any decisions: {mcdc_decisions:?}", + ); + } + + bcbs_with_counter_mappings + } } fn resolve_block_markers( diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 1e2599e78e9e1..5cc01b4c9b850 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -70,21 +70,24 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: //////////////////////////////////////////////////// // Compute coverage spans from the `CoverageGraph`. - let Some(coverage_spans) = - mappings::generate_coverage_spans(mir_body, &hir_info, &basic_coverage_blocks) - else { - // No relevant spans were found in MIR, so skip instrumenting this function. - return; - }; + let coverage_spans = + mappings::generate_coverage_spans(mir_body, &hir_info, &basic_coverage_blocks); //////////////////////////////////////////////////// // Create an optimized mix of `Counter`s and `Expression`s for the `CoverageGraph`. Ensure // every coverage span has a `Counter` or `Expression` assigned to its `BasicCoverageBlock` // and all `Expression` dependencies (operands) are also generated, for any other // `BasicCoverageBlock`s not already associated with a coverage span. - let bcb_has_coverage_spans = |bcb| coverage_spans.bcb_has_coverage_spans(bcb); + let bcbs_with_counter_mappings = + coverage_spans.all_bcbs_with_counter_mappings(&basic_coverage_blocks); + if bcbs_with_counter_mappings.is_empty() { + // No relevant spans were found in MIR, so skip instrumenting this function. + 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_coverage_spans); + CoverageCounters::make_bcb_counters(&basic_coverage_blocks, bcb_has_counter_mappings); let mappings = create_mappings(tcx, &hir_info, &coverage_spans, &coverage_counters); if mappings.is_empty() { @@ -96,7 +99,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: inject_coverage_statements( mir_body, &basic_coverage_blocks, - bcb_has_coverage_spans, + bcb_has_counter_mappings, &coverage_counters, ); From 496ae1ee1c6c9bbd820607e8b53c91f95e26ffdc Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 20:24:23 +1000 Subject: [PATCH 2/6] coverage: Make the special case for async functions exit early --- .../src/coverage/mappings.rs | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index b2f996e2df78f..9695e39d23e07 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -52,6 +52,7 @@ pub(super) struct MCDCDecision { pub(super) decision_depth: u16, } +#[derive(Default)] pub(super) struct CoverageSpans { pub(super) code_mappings: Vec, pub(super) branch_pairs: Vec, @@ -73,33 +74,35 @@ pub(super) fn generate_coverage_spans( hir_info: &ExtractedHirInfo, basic_coverage_blocks: &CoverageGraph, ) -> CoverageSpans { - let mut code_mappings = vec![]; - let mut branch_pairs = vec![]; - let mut mcdc_branches = vec![]; - let mut mcdc_decisions = vec![]; - if hir_info.is_async_fn { // An async function desugars into a function that returns a future, // with the user code wrapped in a closure. Any spans in the desugared // outer function will be unhelpful, so just keep the signature span // and ignore all of the spans in the MIR body. + let mut mappings = CoverageSpans::default(); if let Some(span) = hir_info.fn_sig_span_extended { - code_mappings.push(CodeMapping { span, bcb: START_BCB }); + mappings.code_mappings.push(CodeMapping { span, bcb: START_BCB }); } - } else { - 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)); - - extract_mcdc_mappings( - mir_body, - hir_info.body_span, - basic_coverage_blocks, - &mut mcdc_branches, - &mut mcdc_decisions, - ); + return mappings; } + let mut code_mappings = vec![]; + let mut branch_pairs = vec![]; + let mut mcdc_branches = vec![]; + let mut mcdc_decisions = vec![]; + + 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)); + + extract_mcdc_mappings( + mir_body, + hir_info.body_span, + basic_coverage_blocks, + &mut mcdc_branches, + &mut mcdc_decisions, + ); + // Determine the length of the test vector bitmap. let test_vector_bitmap_bytes = mcdc_decisions .iter() From 83852d9bf359a886f2126504f929cfde3f17e62b Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 19:53:03 +1000 Subject: [PATCH 3/6] coverage: Don't recompute the number of test vector bitmap bytes The code in `extract_mcdc_mappings` that allocates these bytes already knows how many are needed in total, so there's no need to immediately recompute that value in the calling function. --- .../src/coverage/mappings.rs | 39 ++++++------------- .../rustc_mir_transform/src/coverage/mod.rs | 6 +-- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index 9695e39d23e07..3df9579d9d0b2 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -56,17 +56,11 @@ pub(super) struct MCDCDecision { pub(super) struct CoverageSpans { pub(super) code_mappings: Vec, pub(super) branch_pairs: Vec, - test_vector_bitmap_bytes: u32, + pub(super) mcdc_bitmap_bytes: u32, pub(super) mcdc_branches: Vec, pub(super) mcdc_decisions: Vec, } -impl CoverageSpans { - pub(super) fn test_vector_bitmap_bytes(&self) -> u32 { - self.test_vector_bitmap_bytes - } -} - /// Extracts coverage-relevant spans from MIR, and associates them with /// their corresponding BCBs. pub(super) fn generate_coverage_spans( @@ -88,6 +82,7 @@ pub(super) fn generate_coverage_spans( let mut code_mappings = vec![]; let mut branch_pairs = vec![]; + let mut mcdc_bitmap_bytes = 0; let mut mcdc_branches = vec![]; let mut mcdc_decisions = vec![]; @@ -99,26 +94,12 @@ pub(super) fn generate_coverage_spans( mir_body, hir_info.body_span, basic_coverage_blocks, + &mut mcdc_bitmap_bytes, &mut mcdc_branches, &mut mcdc_decisions, ); - // 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); - - CoverageSpans { - code_mappings, - branch_pairs, - test_vector_bitmap_bytes, - mcdc_branches, - mcdc_decisions, - } + CoverageSpans { code_mappings, branch_pairs, mcdc_bitmap_bytes, mcdc_branches, mcdc_decisions } } impl CoverageSpans { @@ -130,7 +111,7 @@ impl CoverageSpans { let Self { code_mappings, branch_pairs, - test_vector_bitmap_bytes: _, + mcdc_bitmap_bytes: _, mcdc_branches, mcdc_decisions, } = self; @@ -228,6 +209,7 @@ pub(super) fn extract_mcdc_mappings( mir_body: &mir::Body<'_>, body_span: Span, basic_coverage_blocks: &CoverageGraph, + mcdc_bitmap_bytes: &mut u32, mcdc_branches: &mut impl Extend, mcdc_decisions: &mut impl Extend, ) { @@ -266,8 +248,6 @@ pub(super) fn extract_mcdc_mappings( }, )); - let mut next_bitmap_idx = 0; - 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)?; @@ -278,8 +258,11 @@ pub(super) fn extract_mcdc_mappings( .map(|&marker| bcb_from_marker(marker)) .collect::>()?; - let bitmap_idx = next_bitmap_idx; - next_bitmap_idx += (1_u32 << decision.conditions_num).div_ceil(8); + // Each decision containing N conditions needs 2^N bits of space in + // the bitmap, rounded up to a whole number of bytes. + // The decision's "bitmap index" points to its first byte in the bitmap. + let bitmap_idx = *mcdc_bitmap_bytes; + *mcdc_bitmap_bytes += (1_u32 << decision.conditions_num).div_ceil(8); Some(MCDCDecision { span, diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 5cc01b4c9b850..572502bc7bb00 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -115,7 +115,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo { function_source_hash: hir_info.function_source_hash, num_counters: coverage_counters.num_counters(), - mcdc_bitmap_bytes: coverage_spans.test_vector_bitmap_bytes(), + mcdc_bitmap_bytes: coverage_spans.mcdc_bitmap_bytes, expressions: coverage_counters.into_expressions(), mappings, mcdc_num_condition_bitmaps, @@ -254,10 +254,6 @@ fn inject_mcdc_statements<'tcx>( basic_coverage_blocks: &CoverageGraph, coverage_spans: &CoverageSpans, ) { - if coverage_spans.test_vector_bitmap_bytes() == 0 { - return; - } - // Inject test vector update first because `inject_statement` always insert new statement at head. for &mappings::MCDCDecision { span: _, From 84cedbec9dc007deca85d226097624ec1bf5ee4e Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 16:45:23 +1000 Subject: [PATCH 4/6] coverage: Destructure the mappings struct to make sure we don't miss any --- .../rustc_mir_transform/src/coverage/mod.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 572502bc7bb00..3666aad3fadf4 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -151,9 +151,18 @@ fn create_mappings<'tcx>( }; let region_for_span = |span: Span| make_code_region(source_map, file_name, span, body_span); + // Fully destructure the mappings struct to make sure we don't miss any kinds. + let CoverageSpans { + code_mappings, + branch_pairs, + mcdc_bitmap_bytes: _, + mcdc_branches, + mcdc_decisions, + } = coverage_spans; let mut mappings = Vec::new(); - mappings.extend(coverage_spans.code_mappings.iter().filter_map( + mappings.extend(code_mappings.iter().filter_map( + // Ordinary code mappings are the simplest kind. |&mappings::CodeMapping { span, bcb }| { let code_region = region_for_span(span)?; let kind = MappingKind::Code(term_for_bcb(bcb)); @@ -161,7 +170,7 @@ fn create_mappings<'tcx>( }, )); - mappings.extend(coverage_spans.branch_pairs.iter().filter_map( + mappings.extend(branch_pairs.iter().filter_map( |&mappings::BranchPair { span, true_bcb, false_bcb }| { let true_term = term_for_bcb(true_bcb); let false_term = term_for_bcb(false_bcb); @@ -171,7 +180,7 @@ fn create_mappings<'tcx>( }, )); - mappings.extend(coverage_spans.mcdc_branches.iter().filter_map( + mappings.extend(mcdc_branches.iter().filter_map( |&mappings::MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth: _ }| { let code_region = region_for_span(span)?; let true_term = term_for_bcb(true_bcb); @@ -184,7 +193,7 @@ fn create_mappings<'tcx>( }, )); - mappings.extend(coverage_spans.mcdc_decisions.iter().filter_map( + mappings.extend(mcdc_decisions.iter().filter_map( |&mappings::MCDCDecision { span, bitmap_idx, conditions_num, .. }| { let code_region = region_for_span(span)?; let kind = MappingKind::MCDCDecision(DecisionInfo { bitmap_idx, conditions_num }); From 56c6288c6f3264e1300a36636814365fdafac871 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 17:05:32 +1000 Subject: [PATCH 5/6] coverage: Rename `CoverageSpans` to `ExtractedMappings` --- .../src/coverage/mappings.rs | 18 +++++++---- .../rustc_mir_transform/src/coverage/mod.rs | 30 +++++++++---------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mappings.rs b/compiler/rustc_mir_transform/src/coverage/mappings.rs index 3df9579d9d0b2..61aabea1d8b31 100644 --- a/compiler/rustc_mir_transform/src/coverage/mappings.rs +++ b/compiler/rustc_mir_transform/src/coverage/mappings.rs @@ -53,7 +53,7 @@ pub(super) struct MCDCDecision { } #[derive(Default)] -pub(super) struct CoverageSpans { +pub(super) struct ExtractedMappings { pub(super) code_mappings: Vec, pub(super) branch_pairs: Vec, pub(super) mcdc_bitmap_bytes: u32, @@ -63,17 +63,17 @@ pub(super) struct CoverageSpans { /// Extracts coverage-relevant spans from MIR, and associates them with /// their corresponding BCBs. -pub(super) fn generate_coverage_spans( +pub(super) fn extract_all_mapping_info_from_mir( mir_body: &mir::Body<'_>, hir_info: &ExtractedHirInfo, basic_coverage_blocks: &CoverageGraph, -) -> CoverageSpans { +) -> ExtractedMappings { if hir_info.is_async_fn { // An async function desugars into a function that returns a future, // with the user code wrapped in a closure. Any spans in the desugared // outer function will be unhelpful, so just keep the signature span // and ignore all of the spans in the MIR body. - let mut mappings = CoverageSpans::default(); + let mut mappings = ExtractedMappings::default(); if let Some(span) = hir_info.fn_sig_span_extended { mappings.code_mappings.push(CodeMapping { span, bcb: START_BCB }); } @@ -99,10 +99,16 @@ pub(super) fn generate_coverage_spans( &mut mcdc_decisions, ); - CoverageSpans { code_mappings, branch_pairs, mcdc_bitmap_bytes, mcdc_branches, mcdc_decisions } + ExtractedMappings { + code_mappings, + branch_pairs, + mcdc_bitmap_bytes, + mcdc_branches, + mcdc_decisions, + } } -impl CoverageSpans { +impl ExtractedMappings { pub(super) fn all_bcbs_with_counter_mappings( &self, basic_coverage_blocks: &CoverageGraph, // Only used for allocating a correctly-sized set diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 3666aad3fadf4..ed3fd889917f0 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::CoverageSpans; +use self::mappings::ExtractedMappings; use crate::MirPass; @@ -69,9 +69,9 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: let basic_coverage_blocks = CoverageGraph::from_mir(mir_body); //////////////////////////////////////////////////// - // Compute coverage spans from the `CoverageGraph`. - let coverage_spans = - mappings::generate_coverage_spans(mir_body, &hir_info, &basic_coverage_blocks); + // Extract coverage spans and other mapping info from MIR. + let extracted_mappings = + mappings::extract_all_mapping_info_from_mir(mir_body, &hir_info, &basic_coverage_blocks); //////////////////////////////////////////////////// // Create an optimized mix of `Counter`s and `Expression`s for the `CoverageGraph`. Ensure @@ -79,7 +79,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: // and all `Expression` dependencies (operands) are also generated, for any other // `BasicCoverageBlock`s not already associated with a coverage span. let bcbs_with_counter_mappings = - coverage_spans.all_bcbs_with_counter_mappings(&basic_coverage_blocks); + extracted_mappings.all_bcbs_with_counter_mappings(&basic_coverage_blocks); if bcbs_with_counter_mappings.is_empty() { // No relevant spans were found in MIR, so skip instrumenting this function. return; @@ -89,7 +89,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: let coverage_counters = CoverageCounters::make_bcb_counters(&basic_coverage_blocks, bcb_has_counter_mappings); - let mappings = create_mappings(tcx, &hir_info, &coverage_spans, &coverage_counters); + let mappings = create_mappings(tcx, &hir_info, &extracted_mappings, &coverage_counters); if mappings.is_empty() { // No spans could be converted into valid mappings, so skip this function. debug!("no spans could be converted into valid mappings; skipping"); @@ -103,9 +103,9 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: &coverage_counters, ); - inject_mcdc_statements(mir_body, &basic_coverage_blocks, &coverage_spans); + inject_mcdc_statements(mir_body, &basic_coverage_blocks, &extracted_mappings); - let mcdc_num_condition_bitmaps = coverage_spans + let mcdc_num_condition_bitmaps = extracted_mappings .mcdc_decisions .iter() .map(|&mappings::MCDCDecision { decision_depth, .. }| decision_depth) @@ -115,7 +115,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo { function_source_hash: hir_info.function_source_hash, num_counters: coverage_counters.num_counters(), - mcdc_bitmap_bytes: coverage_spans.mcdc_bitmap_bytes, + mcdc_bitmap_bytes: extracted_mappings.mcdc_bitmap_bytes, expressions: coverage_counters.into_expressions(), mappings, mcdc_num_condition_bitmaps, @@ -130,7 +130,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir: fn create_mappings<'tcx>( tcx: TyCtxt<'tcx>, hir_info: &ExtractedHirInfo, - coverage_spans: &CoverageSpans, + extracted_mappings: &ExtractedMappings, coverage_counters: &CoverageCounters, ) -> Vec { let source_map = tcx.sess.source_map(); @@ -152,13 +152,13 @@ fn create_mappings<'tcx>( let region_for_span = |span: Span| make_code_region(source_map, file_name, span, body_span); // Fully destructure the mappings struct to make sure we don't miss any kinds. - let CoverageSpans { + let ExtractedMappings { code_mappings, branch_pairs, mcdc_bitmap_bytes: _, mcdc_branches, mcdc_decisions, - } = coverage_spans; + } = extracted_mappings; let mut mappings = Vec::new(); mappings.extend(code_mappings.iter().filter_map( @@ -261,7 +261,7 @@ fn inject_coverage_statements<'tcx>( fn inject_mcdc_statements<'tcx>( mir_body: &mut mir::Body<'tcx>, basic_coverage_blocks: &CoverageGraph, - coverage_spans: &CoverageSpans, + extracted_mappings: &ExtractedMappings, ) { // Inject test vector update first because `inject_statement` always insert new statement at head. for &mappings::MCDCDecision { @@ -270,7 +270,7 @@ fn inject_mcdc_statements<'tcx>( bitmap_idx, conditions_num: _, decision_depth, - } in &coverage_spans.mcdc_decisions + } in &extracted_mappings.mcdc_decisions { for end in end_bcbs { let end_bb = basic_coverage_blocks[*end].leader_bb(); @@ -283,7 +283,7 @@ fn inject_mcdc_statements<'tcx>( } for &mappings::MCDCBranch { span: _, true_bcb, false_bcb, condition_info, decision_depth } in - &coverage_spans.mcdc_branches + &extracted_mappings.mcdc_branches { let Some(condition_info) = condition_info else { continue }; let id = condition_info.condition_id; From 0c12a3b1d4f9d6b742abd03a05b20cb5bd3d73c1 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Thu, 2 May 2024 17:32:18 +1000 Subject: [PATCH 6/6] coverage: Tidy imports in `rustc_mir_transform::coverage` --- compiler/rustc_mir_transform/src/coverage/mod.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index ed3fd889917f0..28e0c633d5aaa 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -7,13 +7,9 @@ mod spans; #[cfg(test)] mod tests; -use self::counters::{CounterIncrementSite, CoverageCounters}; -use self::graph::{BasicCoverageBlock, CoverageGraph}; -use self::mappings::ExtractedMappings; - -use crate::MirPass; - -use rustc_middle::mir::coverage::*; +use rustc_middle::mir::coverage::{ + CodeRegion, CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind, +}; use rustc_middle::mir::{ self, BasicBlock, BasicBlockData, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, @@ -23,6 +19,11 @@ use rustc_span::def_id::LocalDefId; use rustc_span::source_map::SourceMap; use rustc_span::{BytePos, Pos, RelativeBytePos, Span, Symbol}; +use crate::coverage::counters::{CounterIncrementSite, CoverageCounters}; +use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph}; +use crate::coverage::mappings::ExtractedMappings; +use crate::MirPass; + /// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected /// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen /// to construct the coverage map.