Skip to content

Commit 83852d9

Browse files
committed
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.
1 parent 496ae1e commit 83852d9

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

compiler/rustc_mir_transform/src/coverage/mappings.rs

+11-28
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,11 @@ pub(super) struct MCDCDecision {
5656
pub(super) struct CoverageSpans {
5757
pub(super) code_mappings: Vec<CodeMapping>,
5858
pub(super) branch_pairs: Vec<BranchPair>,
59-
test_vector_bitmap_bytes: u32,
59+
pub(super) mcdc_bitmap_bytes: u32,
6060
pub(super) mcdc_branches: Vec<MCDCBranch>,
6161
pub(super) mcdc_decisions: Vec<MCDCDecision>,
6262
}
6363

64-
impl CoverageSpans {
65-
pub(super) fn test_vector_bitmap_bytes(&self) -> u32 {
66-
self.test_vector_bitmap_bytes
67-
}
68-
}
69-
7064
/// Extracts coverage-relevant spans from MIR, and associates them with
7165
/// their corresponding BCBs.
7266
pub(super) fn generate_coverage_spans(
@@ -88,6 +82,7 @@ pub(super) fn generate_coverage_spans(
8882

8983
let mut code_mappings = vec![];
9084
let mut branch_pairs = vec![];
85+
let mut mcdc_bitmap_bytes = 0;
9186
let mut mcdc_branches = vec![];
9287
let mut mcdc_decisions = vec![];
9388

@@ -99,26 +94,12 @@ pub(super) fn generate_coverage_spans(
9994
mir_body,
10095
hir_info.body_span,
10196
basic_coverage_blocks,
97+
&mut mcdc_bitmap_bytes,
10298
&mut mcdc_branches,
10399
&mut mcdc_decisions,
104100
);
105101

106-
// Determine the length of the test vector bitmap.
107-
let test_vector_bitmap_bytes = mcdc_decisions
108-
.iter()
109-
.map(|&MCDCDecision { bitmap_idx, conditions_num, .. }| {
110-
bitmap_idx + (1_u32 << u32::from(conditions_num)).div_ceil(8)
111-
})
112-
.max()
113-
.unwrap_or(0);
114-
115-
CoverageSpans {
116-
code_mappings,
117-
branch_pairs,
118-
test_vector_bitmap_bytes,
119-
mcdc_branches,
120-
mcdc_decisions,
121-
}
102+
CoverageSpans { code_mappings, branch_pairs, mcdc_bitmap_bytes, mcdc_branches, mcdc_decisions }
122103
}
123104

124105
impl CoverageSpans {
@@ -130,7 +111,7 @@ impl CoverageSpans {
130111
let Self {
131112
code_mappings,
132113
branch_pairs,
133-
test_vector_bitmap_bytes: _,
114+
mcdc_bitmap_bytes: _,
134115
mcdc_branches,
135116
mcdc_decisions,
136117
} = self;
@@ -228,6 +209,7 @@ pub(super) fn extract_mcdc_mappings(
228209
mir_body: &mir::Body<'_>,
229210
body_span: Span,
230211
basic_coverage_blocks: &CoverageGraph,
212+
mcdc_bitmap_bytes: &mut u32,
231213
mcdc_branches: &mut impl Extend<MCDCBranch>,
232214
mcdc_decisions: &mut impl Extend<MCDCDecision>,
233215
) {
@@ -266,8 +248,6 @@ pub(super) fn extract_mcdc_mappings(
266248
},
267249
));
268250

269-
let mut next_bitmap_idx = 0;
270-
271251
mcdc_decisions.extend(branch_info.mcdc_decision_spans.iter().filter_map(
272252
|decision: &mir::coverage::MCDCDecisionSpan| {
273253
let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?;
@@ -278,8 +258,11 @@ pub(super) fn extract_mcdc_mappings(
278258
.map(|&marker| bcb_from_marker(marker))
279259
.collect::<Option<_>>()?;
280260

281-
let bitmap_idx = next_bitmap_idx;
282-
next_bitmap_idx += (1_u32 << decision.conditions_num).div_ceil(8);
261+
// Each decision containing N conditions needs 2^N bits of space in
262+
// the bitmap, rounded up to a whole number of bytes.
263+
// The decision's "bitmap index" points to its first byte in the bitmap.
264+
let bitmap_idx = *mcdc_bitmap_bytes;
265+
*mcdc_bitmap_bytes += (1_u32 << decision.conditions_num).div_ceil(8);
283266

284267
Some(MCDCDecision {
285268
span,

compiler/rustc_mir_transform/src/coverage/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
115115
mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo {
116116
function_source_hash: hir_info.function_source_hash,
117117
num_counters: coverage_counters.num_counters(),
118-
mcdc_bitmap_bytes: coverage_spans.test_vector_bitmap_bytes(),
118+
mcdc_bitmap_bytes: coverage_spans.mcdc_bitmap_bytes,
119119
expressions: coverage_counters.into_expressions(),
120120
mappings,
121121
mcdc_num_condition_bitmaps,
@@ -254,10 +254,6 @@ fn inject_mcdc_statements<'tcx>(
254254
basic_coverage_blocks: &CoverageGraph,
255255
coverage_spans: &CoverageSpans,
256256
) {
257-
if coverage_spans.test_vector_bitmap_bytes() == 0 {
258-
return;
259-
}
260-
261257
// Inject test vector update first because `inject_statement` always insert new statement at head.
262258
for &mappings::MCDCDecision {
263259
span: _,

0 commit comments

Comments
 (0)