Skip to content

Commit 41d8a40

Browse files
author
zhuyunxing
committed
coverage. Add discard end blocks for decisions to reset condbitmap without updating global bitmap
1 parent 2b01b56 commit 41d8a40

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
@@ -561,12 +561,20 @@ fn write_coverage_info_hi(
561561
did_print = true;
562562
}
563563

564-
for (coverage::MCDCDecisionSpan { span, end_markers, decision_depth }, conditions) in mcdc_spans
564+
for (
565+
coverage::MCDCDecisionSpan {
566+
span,
567+
update_end_markers: end_markers,
568+
discard_end_markers: discard_markers,
569+
decision_depth,
570+
},
571+
conditions,
572+
) in mcdc_spans
565573
{
566574
let num_conditions = conditions.len();
567575
writeln!(
568576
w,
569-
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
577+
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, discard_markers: {discard_markers:?} depth: {decision_depth:?} }} => {span:?}"
570578
)?;
571579
for coverage::MCDCBranchSpan { span, condition_info, true_markers, false_markers } in
572580
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::IndexVec;
64
use rustc_index::bit_set::BitSet;
@@ -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
@@ -332,7 +332,7 @@ fn inject_mcdc_statements<'tcx>(
332332
) {
333333
for (decision, conditions) in &extracted_mappings.mcdc_mappings {
334334
// Inject test vector update first because `inject_statement` always insert new statement at head.
335-
for &end in &decision.end_bcbs {
335+
for &end in &decision.update_end_bcbs {
336336
let end_bb = basic_coverage_blocks[end].leader_bb();
337337
inject_statement(
338338
mir_body,
@@ -344,6 +344,15 @@ fn inject_mcdc_statements<'tcx>(
344344
);
345345
}
346346

347+
for &discard in &decision.discard_end_bcbs {
348+
let discard_bb = basic_coverage_blocks[discard].leader_bb();
349+
inject_statement(
350+
mir_body,
351+
CoverageKind::CondBitmapReset { decision_depth: decision.decision_depth },
352+
discard_bb,
353+
);
354+
}
355+
347356
for mappings::MCDCBranch {
348357
span: _,
349358
true_bcbs,

0 commit comments

Comments
 (0)