Skip to content

Commit b9603cb

Browse files
author
zhuyunxing
committed
coverage. Refactor MCDCInfoBuilder for pattern matching implementation and change the way to calculate decision depth
1 parent 661b585 commit b9603cb

File tree

14 files changed

+897
-239
lines changed

14 files changed

+897
-239
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
217217
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_index, cond_bitmap);
218218
bx.mcdc_condbitmap_reset(cond_bitmap);
219219
}
220+
CoverageKind::CondBitmapReset { decision_depth } => {
221+
drop(coverage_map);
222+
let cond_bitmap = bx.coverage_cx()
223+
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
224+
.expect("mcdc cond bitmap should have been allocated for merging into the global bitmap");
225+
bx.mcdc_condbitmap_reset(cond_bitmap);
226+
}
220227
}
221228
}
222229
}

compiler/rustc_middle/src/mir/coverage.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ rustc_index::newtype_index! {
5151
pub struct ExpressionId {}
5252
}
5353

54+
rustc_index::newtype_index! {
55+
/// ID of a mcdc decision. Used to identify decision in a function.
56+
#[derive(HashStable)]
57+
#[encodable]
58+
#[orderable]
59+
#[debug_format = "DecisionId({})"]
60+
pub struct DecisionId {}
61+
}
62+
5463
rustc_index::newtype_index! {
5564
/// ID of a mcdc condition. Used by llvm to check mcdc coverage.
5665
///
@@ -131,6 +140,11 @@ pub enum CoverageKind {
131140
/// This is eventually lowered to instruments updating mcdc temp variables.
132141
CondBitmapUpdate { index: u32, decision_depth: u16 },
133142

143+
/// Marks the point in MIR control flow where a condition bitmap is reset.
144+
///
145+
/// This is eventually lowered to instruments set the mcdc temp variable to zero.
146+
CondBitmapReset { decision_depth: u16 },
147+
134148
/// Marks the point in MIR control flow represented by a evaluated decision.
135149
///
136150
/// This is eventually lowered to `llvm.instrprof.mcdc.tvbitmap.update` in LLVM IR.
@@ -148,6 +162,9 @@ impl Debug for CoverageKind {
148162
CondBitmapUpdate { index, decision_depth } => {
149163
write!(fmt, "CondBitmapUpdate(index={:?}, depth={:?})", index, decision_depth)
150164
}
165+
CondBitmapReset { decision_depth } => {
166+
write!(fmt, "CondBitmapReset(depth={:?})", decision_depth)
167+
}
151168
TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
152169
write!(fmt, "TestVectorUpdate({:?}, depth={:?})", bitmap_idx, decision_depth)
153170
}
@@ -271,7 +288,7 @@ pub struct CoverageInfoHi {
271288
pub branch_spans: Vec<BranchSpan>,
272289
/// Branch spans generated by mcdc. Because of some limits mcdc builder give up generating
273290
/// decisions including them so that they are handled as normal branch spans.
274-
pub mcdc_degraded_branch_spans: Vec<MCDCBranchSpan>,
291+
pub mcdc_degraded_spans: Vec<MCDCBranchSpan>,
275292
pub mcdc_spans: Vec<(MCDCDecisionSpan, Vec<MCDCBranchSpan>)>,
276293
}
277294

@@ -296,8 +313,11 @@ pub struct ConditionInfo {
296313
pub struct MCDCBranchSpan {
297314
pub span: Span,
298315
pub condition_info: ConditionInfo,
299-
pub true_marker: BlockMarkerId,
300-
pub false_marker: BlockMarkerId,
316+
// For boolean decisions and most pattern matching decisions there is only
317+
// one true marker and one false marker in each branch. But for matching decisions
318+
// with `|` there can be several.
319+
pub true_markers: Vec<BlockMarkerId>,
320+
pub false_markers: Vec<BlockMarkerId>,
301321
}
302322

303323
#[derive(Copy, Clone, Debug)]
@@ -313,5 +333,10 @@ pub struct MCDCDecisionSpan {
313333
pub span: Span,
314334
pub end_markers: Vec<BlockMarkerId>,
315335
pub decision_depth: u16,
316-
pub num_conditions: usize,
336+
}
337+
338+
impl MCDCDecisionSpan {
339+
pub fn new(span: Span) -> Self {
340+
Self { span, end_markers: Vec::new(), decision_depth: 0 }
341+
}
317342
}

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ fn write_coverage_info_hi(
538538
let coverage::CoverageInfoHi {
539539
num_block_markers: _,
540540
branch_spans,
541-
mcdc_degraded_branch_spans,
541+
mcdc_degraded_spans,
542542
mcdc_spans,
543543
} = coverage_info_hi;
544544

@@ -553,32 +553,27 @@ fn write_coverage_info_hi(
553553
did_print = true;
554554
}
555555

556-
for coverage::MCDCBranchSpan { span, true_marker, false_marker, .. } in
557-
mcdc_degraded_branch_spans
558-
{
556+
for coverage::MCDCBranchSpan { span, true_markers, false_markers, .. } in mcdc_degraded_spans {
559557
writeln!(
560558
w,
561-
"{INDENT}coverage branch {{ true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
559+
"{INDENT}coverage branch {{ true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
562560
)?;
563561
did_print = true;
564562
}
565563

566-
for (
567-
coverage::MCDCDecisionSpan { span, end_markers, decision_depth, num_conditions: _ },
568-
conditions,
569-
) in mcdc_spans
564+
for (coverage::MCDCDecisionSpan { span, end_markers, decision_depth }, conditions) in mcdc_spans
570565
{
571566
let num_conditions = conditions.len();
572567
writeln!(
573568
w,
574569
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
575570
)?;
576-
for coverage::MCDCBranchSpan { span, condition_info, true_marker, false_marker } in
571+
for coverage::MCDCBranchSpan { span, condition_info, true_markers, false_markers } in
577572
conditions
578573
{
579574
writeln!(
580575
w,
581-
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
576+
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
582577
condition_info.condition_id
583578
)?;
584579
}

compiler/rustc_mir_build/src/build/coverageinfo.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ impl CoverageInfoBuilder {
144144
// Separate path for handling branches when MC/DC is enabled.
145145
if let Some(mcdc_info) = self.mcdc_info.as_mut() {
146146
let inject_block_marker =
147-
|source_info, block| self.markers.inject_block_marker(cfg, source_info, block);
147+
|block| self.markers.inject_block_marker(cfg, source_info, block);
148148
mcdc_info.visit_evaluated_condition(
149149
tcx,
150-
source_info,
150+
source_info.span,
151151
true_block,
152152
false_block,
153153
inject_block_marker,
@@ -175,18 +175,18 @@ impl CoverageInfoBuilder {
175175
let branch_spans =
176176
branch_info.map(|branch_info| branch_info.branch_spans).unwrap_or_default();
177177

178-
let (mcdc_spans, mcdc_degraded_branch_spans) =
178+
let (mcdc_degraded_spans, mcdc_spans) =
179179
mcdc_info.map(MCDCInfoBuilder::into_done).unwrap_or_default();
180180

181181
// For simplicity, always return an info struct (without Option), even
182182
// if there's nothing interesting in it.
183183
Box::new(CoverageInfoHi {
184184
num_block_markers,
185185
branch_spans,
186-
mcdc_degraded_branch_spans,
187-
mcdc_spans,
188-
})
189-
}
186+
mcdc_degraded_spans,
187+
mcdc_spans
188+
})
189+
}
190190
}
191191

192192
impl<'tcx> Builder<'_, 'tcx> {

0 commit comments

Comments
 (0)