Skip to content

Commit f290117

Browse files
zhuyunxingLambdaris
zhuyunxing
authored andcommitted
coverage. Refactor MCDCInfoBuilder for pattern matching implementation and change the way to calculate decision depth
1 parent 0b26d8a commit f290117

File tree

14 files changed

+945
-285
lines changed

14 files changed

+945
-285
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
210210
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_index, cond_bitmap);
211211
bx.mcdc_condbitmap_reset(cond_bitmap);
212212
}
213+
CoverageKind::CondBitmapReset { decision_depth } => {
214+
let cond_bitmap = bx.coverage_cx()
215+
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
216+
.expect("mcdc cond bitmap should have been allocated for merging into the global bitmap");
217+
bx.mcdc_condbitmap_reset(cond_bitmap);
218+
}
213219
}
214220
}
215221
}

compiler/rustc_middle/src/mir/coverage.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ rustc_index::newtype_index! {
5050
pub struct ExpressionId {}
5151
}
5252

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

138+
/// Marks the point in MIR control flow where a condition bitmap is reset.
139+
///
140+
/// This is eventually lowered to instruments set the mcdc temp variable to zero.
141+
CondBitmapReset { decision_depth: u16 },
142+
129143
/// Marks the point in MIR control flow represented by a evaluated decision.
130144
///
131145
/// This is eventually lowered to `llvm.instrprof.mcdc.tvbitmap.update` in LLVM IR.
@@ -143,6 +157,9 @@ impl Debug for CoverageKind {
143157
CondBitmapUpdate { index, decision_depth } => {
144158
write!(fmt, "CondBitmapUpdate(index={:?}, depth={:?})", index, decision_depth)
145159
}
160+
CondBitmapReset { decision_depth } => {
161+
write!(fmt, "CondBitmapReset(depth={:?})", decision_depth)
162+
}
146163
TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
147164
write!(fmt, "TestVectorUpdate({:?}, depth={:?})", bitmap_idx, decision_depth)
148165
}
@@ -247,7 +264,7 @@ pub struct CoverageInfoHi {
247264
pub branch_spans: Vec<BranchSpan>,
248265
/// Branch spans generated by mcdc. Because of some limits mcdc builder give up generating
249266
/// decisions including them so that they are handled as normal branch spans.
250-
pub mcdc_degraded_branch_spans: Vec<MCDCBranchSpan>,
267+
pub mcdc_degraded_spans: Vec<MCDCBranchSpan>,
251268
pub mcdc_spans: Vec<(MCDCDecisionSpan, Vec<MCDCBranchSpan>)>,
252269
}
253270

@@ -272,8 +289,11 @@ pub struct ConditionInfo {
272289
pub struct MCDCBranchSpan {
273290
pub span: Span,
274291
pub condition_info: ConditionInfo,
275-
pub true_marker: BlockMarkerId,
276-
pub false_marker: BlockMarkerId,
292+
// For boolean decisions and most pattern matching decisions there is only
293+
// one true marker and one false marker in each branch. But for matching decisions
294+
// with `|` there can be several.
295+
pub true_markers: Vec<BlockMarkerId>,
296+
pub false_markers: Vec<BlockMarkerId>,
277297
}
278298

279299
#[derive(Copy, Clone, Debug)]
@@ -289,7 +309,12 @@ pub struct MCDCDecisionSpan {
289309
pub span: Span,
290310
pub end_markers: Vec<BlockMarkerId>,
291311
pub decision_depth: u16,
292-
pub num_conditions: usize,
312+
}
313+
314+
impl MCDCDecisionSpan {
315+
pub fn new(span: Span) -> Self {
316+
Self { span, end_markers: Vec::new(), decision_depth: 0 }
317+
}
293318
}
294319

295320
/// Summarizes coverage IDs inserted by the `InstrumentCoverage` MIR pass

compiler/rustc_middle/src/mir/pretty.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn write_coverage_info_hi(
561561
let coverage::CoverageInfoHi {
562562
num_block_markers: _,
563563
branch_spans,
564-
mcdc_degraded_branch_spans,
564+
mcdc_degraded_spans,
565565
mcdc_spans,
566566
} = coverage_info_hi;
567567

@@ -576,32 +576,27 @@ fn write_coverage_info_hi(
576576
did_print = true;
577577
}
578578

579-
for coverage::MCDCBranchSpan { span, true_marker, false_marker, .. } in
580-
mcdc_degraded_branch_spans
581-
{
579+
for coverage::MCDCBranchSpan { span, true_markers, false_markers, .. } in mcdc_degraded_spans {
582580
writeln!(
583581
w,
584-
"{INDENT}coverage branch {{ true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
582+
"{INDENT}coverage branch {{ true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
585583
)?;
586584
did_print = true;
587585
}
588586

589-
for (
590-
coverage::MCDCDecisionSpan { span, end_markers, decision_depth, num_conditions: _ },
591-
conditions,
592-
) in mcdc_spans
587+
for (coverage::MCDCDecisionSpan { span, end_markers, decision_depth }, conditions) in mcdc_spans
593588
{
594589
let num_conditions = conditions.len();
595590
writeln!(
596591
w,
597592
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
598593
)?;
599-
for coverage::MCDCBranchSpan { span, condition_info, true_marker, false_marker } in
594+
for coverage::MCDCBranchSpan { span, condition_info, true_markers, false_markers } in
600595
conditions
601596
{
602597
writeln!(
603598
w,
604-
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
599+
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
605600
condition_info.condition_id
606601
)?;
607602
}

compiler/rustc_mir_build/src/builder/coverageinfo.rs

+7-7
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)