Skip to content

Commit be1b2e0

Browse files
zhuyunxingLambdaris
zhuyunxing
authored andcommitted
coverage. Add discard end blocks for decisions to reset condbitmap without updating global bitmap
1 parent f290117 commit be1b2e0

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
@@ -307,13 +307,21 @@ pub struct DecisionInfo {
307307
#[derive(TyEncodable, TyDecodable, Hash, HashStable)]
308308
pub struct MCDCDecisionSpan {
309309
pub span: Span,
310-
pub end_markers: Vec<BlockMarkerId>,
310+
// Blocks where update test vectors of the decision.
311+
pub update_end_markers: Vec<BlockMarkerId>,
312+
// Block where discard written condition bitmap of the decision.
313+
pub discard_end_markers: Vec<BlockMarkerId>,
311314
pub decision_depth: u16,
312315
}
313316

314317
impl MCDCDecisionSpan {
315318
pub fn new(span: Span) -> Self {
316-
Self { span, end_markers: Vec::new(), decision_depth: 0 }
319+
Self {
320+
span,
321+
update_end_markers: Vec::new(),
322+
discard_end_markers: Vec::new(),
323+
decision_depth: 0,
324+
}
317325
}
318326
}
319327

compiler/rustc_middle/src/mir/pretty.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,20 @@ fn write_coverage_info_hi(
584584
did_print = true;
585585
}
586586

587-
for (coverage::MCDCDecisionSpan { span, end_markers, decision_depth }, conditions) in mcdc_spans
587+
for (
588+
coverage::MCDCDecisionSpan {
589+
span,
590+
update_end_markers: end_markers,
591+
discard_end_markers: discard_markers,
592+
decision_depth,
593+
},
594+
conditions,
595+
) in mcdc_spans
588596
{
589597
let num_conditions = conditions.len();
590598
writeln!(
591599
w,
592-
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
600+
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, discard_markers: {discard_markers:?} depth: {decision_depth:?} }} => {span:?}"
593601
)?;
594602
for coverage::MCDCBranchSpan { span, condition_info, true_markers, false_markers } in
595603
conditions

compiler/rustc_mir_build/src/builder/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::DenseBitSet;
@@ -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,
@@ -306,11 +305,16 @@ pub(super) fn extract_mcdc_mappings(
306305
}
307306
let decision_span = unexpand_into_body_span(decision.span, body_span)?;
308307

309-
let end_bcbs = decision
310-
.end_markers
308+
let update_end_bcbs = decision
309+
.update_end_markers
310+
.iter()
311+
.filter_map(|&marker| bcb_from_marker(marker))
312+
.collect();
313+
let discard_end_bcbs = decision
314+
.discard_end_markers
311315
.iter()
312-
.map(|&marker| bcb_from_marker(marker))
313-
.collect::<Option<_>>()?;
316+
.filter_map(|&marker| bcb_from_marker(marker))
317+
.collect();
314318

315319
let mut branch_mappings: Vec<_> =
316320
branches.into_iter().filter_map(extract_condition_mapping).collect();
@@ -342,7 +346,8 @@ pub(super) fn extract_mcdc_mappings(
342346
Some((
343347
MCDCDecision {
344348
span,
345-
end_bcbs,
349+
update_end_bcbs,
350+
discard_end_bcbs,
346351
bitmap_idx,
347352
num_test_vectors,
348353
decision_depth: decision.decision_depth,
@@ -400,7 +405,10 @@ fn calc_test_vectors_index(conditions: &mut Vec<MCDCBranch>) -> usize {
400405
}
401406
}
402407
}
403-
assert!(next_conditions.is_empty(), "the decision tree has untouched nodes");
408+
assert!(
409+
next_conditions.is_empty(),
410+
"the decision tree has untouched nodes, next_conditions: {next_conditions:?}"
411+
);
404412
let mut cur_idx = 0;
405413
// LLVM hopes the end nodes are sorted in descending order by `num_paths` so that it can
406414
// 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
@@ -277,7 +277,7 @@ fn inject_mcdc_statements<'tcx>(
277277
) {
278278
for (decision, conditions) in &extracted_mappings.mcdc_mappings {
279279
// Inject test vector update first because `inject_statement` always insert new statement at head.
280-
for &end in &decision.end_bcbs {
280+
for &end in &decision.update_end_bcbs {
281281
let end_bb = graph[end].leader_bb();
282282
inject_statement(
283283
mir_body,
@@ -289,6 +289,15 @@ fn inject_mcdc_statements<'tcx>(
289289
);
290290
}
291291

292+
for &discard in &decision.discard_end_bcbs {
293+
let discard_bb = graph[discard].leader_bb();
294+
inject_statement(
295+
mir_body,
296+
CoverageKind::CondBitmapReset { decision_depth: decision.decision_depth },
297+
discard_bb,
298+
);
299+
}
300+
292301
for mappings::MCDCBranch {
293302
span: _,
294303
true_bcbs,

0 commit comments

Comments
 (0)