Skip to content

Commit ecce90b

Browse files
committed
coverage: Remove intermediate data structures from mapping creation
1 parent 2c1ac85 commit ecce90b

File tree

3 files changed

+31
-77
lines changed

3 files changed

+31
-77
lines changed
Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,36 @@
11
use rustc_index::IndexVec;
2-
use rustc_middle::mir::coverage::{BlockMarkerId, BranchSpan, CoverageInfoHi, CoverageKind};
2+
use rustc_middle::mir::coverage::{
3+
BlockMarkerId, BranchSpan, CoverageInfoHi, CoverageKind, Mapping, MappingKind,
4+
};
35
use rustc_middle::mir::{self, BasicBlock, StatementKind};
46
use rustc_middle::ty::TyCtxt;
5-
use rustc_span::Span;
67

7-
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
8+
use crate::coverage::graph::CoverageGraph;
89
use crate::coverage::hir_info::ExtractedHirInfo;
910
use crate::coverage::spans::extract_refined_covspans;
1011
use crate::coverage::unexpand::unexpand_into_body_span;
1112

12-
/// Associates an ordinary executable code span with its corresponding BCB.
13-
#[derive(Debug)]
14-
pub(super) struct CodeMapping {
15-
pub(super) span: Span,
16-
pub(super) bcb: BasicCoverageBlock,
17-
}
18-
19-
#[derive(Debug)]
20-
pub(super) struct BranchPair {
21-
pub(super) span: Span,
22-
pub(super) true_bcb: BasicCoverageBlock,
23-
pub(super) false_bcb: BasicCoverageBlock,
24-
}
25-
2613
#[derive(Default)]
27-
pub(super) struct ExtractedMappings {
28-
pub(super) code_mappings: Vec<CodeMapping>,
29-
pub(super) branch_pairs: Vec<BranchPair>,
14+
pub(crate) struct ExtractedMappings {
15+
pub(crate) mappings: Vec<Mapping>,
3016
}
3117

32-
/// Extracts coverage-relevant spans from MIR, and associates them with
33-
/// their corresponding BCBs.
34-
pub(super) fn extract_all_mapping_info_from_mir<'tcx>(
18+
/// Extracts coverage-relevant spans from MIR, and uses them to create
19+
/// coverage mapping data for inclusion in MIR.
20+
pub(crate) fn extract_mappings_from_mir<'tcx>(
3521
tcx: TyCtxt<'tcx>,
3622
mir_body: &mir::Body<'tcx>,
3723
hir_info: &ExtractedHirInfo,
3824
graph: &CoverageGraph,
3925
) -> ExtractedMappings {
40-
let mut code_mappings = vec![];
41-
let mut branch_pairs = vec![];
26+
let mut mappings = vec![];
4227

4328
// Extract ordinary code mappings from MIR statement/terminator spans.
44-
extract_refined_covspans(tcx, mir_body, hir_info, graph, &mut code_mappings);
29+
extract_refined_covspans(tcx, mir_body, hir_info, graph, &mut mappings);
4530

46-
branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, graph));
31+
extract_branch_mappings(mir_body, hir_info, graph, &mut mappings);
4732

48-
ExtractedMappings { code_mappings, branch_pairs }
33+
ExtractedMappings { mappings }
4934
}
5035

5136
fn resolve_block_markers(
@@ -69,19 +54,18 @@ fn resolve_block_markers(
6954
block_markers
7055
}
7156

72-
pub(super) fn extract_branch_pairs(
57+
pub(super) fn extract_branch_mappings(
7358
mir_body: &mir::Body<'_>,
7459
hir_info: &ExtractedHirInfo,
7560
graph: &CoverageGraph,
76-
) -> Vec<BranchPair> {
77-
let Some(coverage_info_hi) = mir_body.coverage_info_hi.as_deref() else { return vec![] };
61+
mappings: &mut Vec<Mapping>,
62+
) {
63+
let Some(coverage_info_hi) = mir_body.coverage_info_hi.as_deref() else { return };
7864

7965
let block_markers = resolve_block_markers(coverage_info_hi, mir_body);
8066

81-
coverage_info_hi
82-
.branch_spans
83-
.iter()
84-
.filter_map(|&BranchSpan { span: raw_span, true_marker, false_marker }| {
67+
mappings.extend(coverage_info_hi.branch_spans.iter().filter_map(
68+
|&BranchSpan { span: raw_span, true_marker, false_marker }| try {
8569
// For now, ignore any branch span that was introduced by
8670
// expansion. This makes things like assert macros less noisy.
8771
if !raw_span.ctxt().outer_expn_data().is_root() {
@@ -94,7 +78,7 @@ pub(super) fn extract_branch_pairs(
9478
let true_bcb = bcb_from_marker(true_marker)?;
9579
let false_bcb = bcb_from_marker(false_marker)?;
9680

97-
Some(BranchPair { span, true_bcb, false_bcb })
98-
})
99-
.collect::<Vec<_>>()
81+
Mapping { span, kind: MappingKind::Branch { true_bcb, false_bcb } }
82+
},
83+
));
10084
}

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_middle::mir::coverage::{CoverageKind, FunctionCoverageInfo, Mapping, MappingKind};
1+
use rustc_middle::mir::coverage::{CoverageKind, FunctionCoverageInfo};
22
use rustc_middle::mir::{self, BasicBlock, Statement, StatementKind, TerminatorKind};
33
use rustc_middle::ty::TyCtxt;
44
use tracing::{debug, debug_span, trace};
@@ -71,10 +71,8 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
7171

7272
////////////////////////////////////////////////////
7373
// Extract coverage spans and other mapping info from MIR.
74-
let extracted_mappings =
75-
mappings::extract_all_mapping_info_from_mir(tcx, mir_body, &hir_info, &graph);
76-
77-
let mappings = create_mappings(&extracted_mappings);
74+
let ExtractedMappings { mappings } =
75+
mappings::extract_mappings_from_mir(tcx, mir_body, &hir_info, &graph);
7876
if mappings.is_empty() {
7977
// No spans could be converted into valid mappings, so skip this function.
8078
debug!("no spans could be converted into valid mappings; skipping");
@@ -100,34 +98,6 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
10098
}));
10199
}
102100

103-
/// For each coverage span extracted from MIR, create a corresponding mapping.
104-
///
105-
/// FIXME(Zalathar): This used to be where BCBs in the extracted mappings were
106-
/// resolved to a `CovTerm`. But that is now handled elsewhere, so this
107-
/// function can potentially be simplified even further.
108-
fn create_mappings(extracted_mappings: &ExtractedMappings) -> Vec<Mapping> {
109-
// Fully destructure the mappings struct to make sure we don't miss any kinds.
110-
let ExtractedMappings { code_mappings, branch_pairs } = extracted_mappings;
111-
let mut mappings = Vec::new();
112-
113-
mappings.extend(code_mappings.iter().map(
114-
// Ordinary code mappings are the simplest kind.
115-
|&mappings::CodeMapping { span, bcb }| {
116-
let kind = MappingKind::Code { bcb };
117-
Mapping { kind, span }
118-
},
119-
));
120-
121-
mappings.extend(branch_pairs.iter().map(
122-
|&mappings::BranchPair { span, true_bcb, false_bcb }| {
123-
let kind = MappingKind::Branch { true_bcb, false_bcb };
124-
Mapping { kind, span }
125-
},
126-
));
127-
128-
mappings
129-
}
130-
131101
/// Inject any necessary coverage statements into MIR, so that they influence codegen.
132102
fn inject_coverage_statements<'tcx>(mir_body: &mut mir::Body<'tcx>, graph: &CoverageGraph) {
133103
for (bcb, data) in graph.iter_enumerated() {

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::fx::FxHashSet;
22
use rustc_middle::mir;
3-
use rustc_middle::mir::coverage::START_BCB;
3+
use rustc_middle::mir::coverage::{Mapping, MappingKind, START_BCB};
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::source_map::SourceMap;
66
use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span};
@@ -9,7 +9,7 @@ use tracing::instrument;
99
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
1010
use crate::coverage::hir_info::ExtractedHirInfo;
1111
use crate::coverage::spans::from_mir::{Hole, RawSpanFromMir, SpanFromMir};
12-
use crate::coverage::{mappings, unexpand};
12+
use crate::coverage::unexpand;
1313

1414
mod from_mir;
1515

@@ -18,15 +18,15 @@ pub(super) fn extract_refined_covspans<'tcx>(
1818
mir_body: &mir::Body<'tcx>,
1919
hir_info: &ExtractedHirInfo,
2020
graph: &CoverageGraph,
21-
code_mappings: &mut Vec<mappings::CodeMapping>,
21+
mappings: &mut Vec<Mapping>,
2222
) {
2323
if hir_info.is_async_fn {
2424
// An async function desugars into a function that returns a future,
2525
// with the user code wrapped in a closure. Any spans in the desugared
2626
// outer function will be unhelpful, so just keep the signature span
2727
// and ignore all of the spans in the MIR body.
2828
if let Some(span) = hir_info.fn_sig_span {
29-
code_mappings.push(mappings::CodeMapping { span, bcb: START_BCB });
29+
mappings.push(Mapping { span, kind: MappingKind::Code { bcb: START_BCB } })
3030
}
3131
return;
3232
}
@@ -111,9 +111,9 @@ pub(super) fn extract_refined_covspans<'tcx>(
111111
// Merge covspans that can be merged.
112112
covspans.dedup_by(|b, a| a.merge_if_eligible(b));
113113

114-
code_mappings.extend(covspans.into_iter().map(|Covspan { span, bcb }| {
114+
mappings.extend(covspans.into_iter().map(|Covspan { span, bcb }| {
115115
// Each span produced by the refiner represents an ordinary code region.
116-
mappings::CodeMapping { span, bcb }
116+
Mapping { span, kind: MappingKind::Code { bcb } }
117117
}));
118118
}
119119

0 commit comments

Comments
 (0)