Skip to content

Commit 0e3d0e6

Browse files
author
zhuyunxing
committed
coverage. Add discard end blocks for decisions to reset condbitmap without updating global bitmap
1 parent 5cdd395 commit 0e3d0e6

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

compiler/rustc_middle/src/mir/coverage.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,20 @@ pub struct DecisionInfo {
331331
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
332332
pub struct MCDCDecisionSpan {
333333
pub span: Span,
334-
pub end_markers: Vec<BlockMarkerId>,
334+
// Blocks where update test vectors of the decision.
335+
pub update_end_markers: Vec<BlockMarkerId>,
336+
// Block where discard written condition bitmap of the decision.
337+
pub discard_end_markers: Vec<BlockMarkerId>,
335338
pub decision_depth: u16,
336339
}
337340

338341
impl MCDCDecisionSpan {
339342
pub fn new(span: Span) -> Self {
340-
Self { span, end_markers: Vec::new(), decision_depth: 0 }
343+
Self {
344+
span,
345+
update_end_markers: Vec::new(),
346+
discard_end_markers: Vec::new(),
347+
decision_depth: 0,
348+
}
341349
}
342350
}

compiler/rustc_middle/src/mir/pretty.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,20 @@ fn write_coverage_info_hi(
513513
did_print = true;
514514
}
515515

516-
for (coverage::MCDCDecisionSpan { span, end_markers, decision_depth }, conditions) in mcdc_spans
516+
for (
517+
coverage::MCDCDecisionSpan {
518+
span,
519+
update_end_markers: end_markers,
520+
discard_end_markers: discard_markers,
521+
decision_depth,
522+
},
523+
conditions,
524+
) in mcdc_spans
517525
{
518526
let num_conditions = conditions.len();
519527
writeln!(
520528
w,
521-
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
529+
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, discard_markers: {discard_markers:?} depth: {decision_depth:?} }} => {span:?}"
522530
)?;
523531
for coverage::MCDCBranchSpan { span, condition_info, true_markers, false_markers } in
524532
conditions

compiler/rustc_mir_build/src/build/coverageinfo/mcdc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ impl BooleanDecisionCtx {
148148
false_next_id: None,
149149
});
150150
if condition_info.true_next_id.is_none() {
151-
self.decision_info.end_markers.push(true_marker);
151+
self.decision_info.update_end_markers.push(true_marker);
152152
}
153153
if condition_info.false_next_id.is_none() {
154-
self.decision_info.end_markers.push(false_marker);
154+
self.decision_info.update_end_markers.push(false_marker);
155155
}
156156

157157
self.conditions.push(MCDCBranchSpan {

compiler/rustc_mir_transform/src/coverage/mappings.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::collections::BTreeSet;
2-
3-
use rustc_data_structures::fx::FxIndexMap;
1+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
42
use rustc_data_structures::graph::DirectedGraph;
53
use rustc_index::bit_set::BitSet;
64
use rustc_index::IndexVec;
@@ -51,7 +49,8 @@ pub(super) struct MCDCBranch {
5149
#[derive(Debug)]
5250
pub(super) struct MCDCDecision {
5351
pub(super) span: Span,
54-
pub(super) end_bcbs: BTreeSet<BasicCoverageBlock>,
52+
pub(super) update_end_bcbs: FxIndexSet<BasicCoverageBlock>,
53+
pub(super) discard_end_bcbs: FxIndexSet<BasicCoverageBlock>,
5554
pub(super) bitmap_idx: usize,
5655
pub(super) num_test_vectors: usize,
5756
pub(super) decision_depth: u16,
@@ -308,11 +307,16 @@ pub(super) fn extract_mcdc_mappings(
308307
}
309308
let decision_span = unexpand_into_body_span(decision.span, body_span)?;
310309

311-
let end_bcbs = decision
312-
.end_markers
310+
let update_end_bcbs = decision
311+
.update_end_markers
312+
.iter()
313+
.filter_map(|&marker| bcb_from_marker(marker))
314+
.collect();
315+
let discard_end_bcbs = decision
316+
.discard_end_markers
313317
.iter()
314-
.map(|&marker| bcb_from_marker(marker))
315-
.collect::<Option<_>>()?;
318+
.filter_map(|&marker| bcb_from_marker(marker))
319+
.collect();
316320

317321
let mut branch_mappings: Vec<_> =
318322
branches.into_iter().filter_map(extract_condition_mapping).collect();
@@ -344,7 +348,8 @@ pub(super) fn extract_mcdc_mappings(
344348
Some((
345349
MCDCDecision {
346350
span,
347-
end_bcbs,
351+
update_end_bcbs,
352+
discard_end_bcbs,
348353
bitmap_idx,
349354
num_test_vectors,
350355
decision_depth: decision.decision_depth,
@@ -403,7 +408,10 @@ fn calc_test_vectors_index(conditions: &mut Vec<MCDCBranch>) -> usize {
403408
}
404409
}
405410
}
406-
assert!(next_conditions.is_empty(), "the decision tree has untouched nodes");
411+
assert!(
412+
next_conditions.is_empty(),
413+
"the decision tree has untouched nodes, next_conditions: {next_conditions:?}"
414+
);
407415
let mut cur_idx = 0;
408416
// LLVM hopes the end nodes are sorted in descending order by `num_paths` so that it can
409417
// optimize bitmap size for decisions in tree form such as `a && b && c && d && ...`.

compiler/rustc_mir_transform/src/coverage/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ fn inject_mcdc_statements<'tcx>(
351351
) {
352352
for (decision, conditions) in &extracted_mappings.mcdc_mappings {
353353
// Inject test vector update first because `inject_statement` always insert new statement at head.
354-
for &end in &decision.end_bcbs {
354+
for &end in &decision.update_end_bcbs {
355355
let end_bb = basic_coverage_blocks[end].leader_bb();
356356
inject_statement(
357357
mir_body,
@@ -363,6 +363,15 @@ fn inject_mcdc_statements<'tcx>(
363363
);
364364
}
365365

366+
for &discard in &decision.discard_end_bcbs {
367+
let discard_bb = basic_coverage_blocks[discard].leader_bb();
368+
inject_statement(
369+
mir_body,
370+
CoverageKind::CondBitmapReset { decision_depth: decision.decision_depth },
371+
discard_bb,
372+
);
373+
}
374+
366375
for mappings::MCDCBranch {
367376
span: _,
368377
true_bcbs,

0 commit comments

Comments
 (0)