Skip to content

Commit 7f13445

Browse files
committed
mcdc-coverage: Add decision_depth field in structs
Add decision_depth field to TVBitmapUpdate/CondBitmapUpdate statements Add decision_depth field to BcbMappingKinds MCDCBranch and MCDCDecision Add decision_depth field to MCDCBranchSpan and MCDCDecisionSpan
1 parent 389a1c3 commit 7f13445

File tree

7 files changed

+81
-47
lines changed

7 files changed

+81
-47
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
5353
fn try_get_mcdc_condition_bitmap(
5454
&self,
5555
instance: &Instance<'tcx>,
56-
) -> Option<Vec<&'ll llvm::Value>> {
57-
self.mcdc_condition_bitmap_map.borrow().get(instance).cloned()
56+
decision_depth: u16,
57+
) -> Option<&'ll llvm::Value> {
58+
self.mcdc_condition_bitmap_map
59+
.borrow()
60+
.get(instance)
61+
.and_then(|bitmap_map| bitmap_map.get(decision_depth as usize))
62+
.copied() // Copy Option<&&Value> to Option<&Value>
5863
}
5964
}
6065

@@ -146,31 +151,27 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
146151
CoverageKind::ExpressionUsed { id } => {
147152
func_coverage.mark_expression_id_seen(id);
148153
}
149-
CoverageKind::CondBitmapUpdate { id, value, .. } => {
154+
CoverageKind::CondBitmapUpdate { id, value, decision_depth } => {
150155
drop(coverage_map);
151156
assert_ne!(
152157
id.as_u32(),
153158
0,
154159
"ConditionId of evaluated conditions should never be zero"
155160
);
156-
let cond_bitmaps = coverage_context
157-
.try_get_mcdc_condition_bitmap(&instance)
161+
let cond_bitmap = coverage_context
162+
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
158163
.expect("mcdc cond bitmap should have been allocated for updating");
159-
let cond_bitmap =
160-
cond_bitmaps.get(0).expect("no mcdc cond bitmap with the given depth");
161164
let cond_loc = bx.const_i32(id.as_u32() as i32 - 1);
162165
let bool_value = bx.const_bool(value);
163166
let fn_name = bx.get_pgo_func_name_var(instance);
164167
let hash = bx.const_u64(function_coverage_info.function_source_hash);
165168
bx.mcdc_condbitmap_update(fn_name, hash, cond_loc, cond_bitmap, bool_value);
166169
}
167-
CoverageKind::TestVectorBitmapUpdate { bitmap_idx } => {
170+
CoverageKind::TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
168171
drop(coverage_map);
169-
let cond_bitmaps = coverage_context
170-
.try_get_mcdc_condition_bitmap(&instance)
172+
let cond_bitmap = coverage_context
173+
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
171174
.expect("mcdc cond bitmap should have been allocated for merging into the global bitmap");
172-
let cond_bitmap =
173-
cond_bitmaps.get(0).expect("no mcdc cond bitmap with the given depth");
174175
let bitmap_bytes = bx.tcx().coverage_ids_info(instance.def).mcdc_bitmap_bytes;
175176
assert!(bitmap_idx < bitmap_bytes, "bitmap index of the decision out of range");
176177
assert!(

compiler/rustc_middle/src/mir/coverage.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ pub enum CoverageKind {
132132
///
133133
/// If this statement does not survive MIR optimizations, the condition would never be
134134
/// taken as evaluated.
135-
CondBitmapUpdate { id: ConditionId, value: bool },
135+
CondBitmapUpdate { id: ConditionId, value: bool, decision_depth: u16 },
136136

137137
/// Marks the point in MIR control flow represented by a evaluated decision.
138138
///
139139
/// This is eventually lowered to `llvm.instrprof.mcdc.tvbitmap.update` in LLVM IR.
140140
///
141141
/// If this statement does not survive MIR optimizations, the decision would never be
142142
/// taken as evaluated.
143-
TestVectorBitmapUpdate { bitmap_idx: u32 },
143+
TestVectorBitmapUpdate { bitmap_idx: u32, decision_depth: u16 },
144144
}
145145

146146
impl Debug for CoverageKind {
@@ -151,11 +151,11 @@ impl Debug for CoverageKind {
151151
BlockMarker { id } => write!(fmt, "BlockMarker({:?})", id.index()),
152152
CounterIncrement { id } => write!(fmt, "CounterIncrement({:?})", id.index()),
153153
ExpressionUsed { id } => write!(fmt, "ExpressionUsed({:?})", id.index()),
154-
CondBitmapUpdate { id, value } => {
155-
write!(fmt, "CondBitmapUpdate({:?}, {:?})", id.index(), value)
154+
CondBitmapUpdate { id, value, decision_depth } => {
155+
write!(fmt, "CondBitmapUpdate({:?}, {:?}, {:?})", id.index(), value, decision_depth)
156156
}
157-
TestVectorBitmapUpdate { bitmap_idx } => {
158-
write!(fmt, "TestVectorUpdate({:?})", bitmap_idx)
157+
TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
158+
write!(fmt, "TestVectorUpdate({:?}, {:?})", bitmap_idx, decision_depth)
159159
}
160160
}
161161
}
@@ -319,6 +319,7 @@ pub struct MCDCBranchSpan {
319319
pub condition_info: Option<ConditionInfo>,
320320
pub true_marker: BlockMarkerId,
321321
pub false_marker: BlockMarkerId,
322+
pub decision_depth: u16,
322323
}
323324

324325
#[derive(Copy, Clone, Debug)]
@@ -334,4 +335,5 @@ pub struct MCDCDecisionSpan {
334335
pub span: Span,
335336
pub conditions_num: usize,
336337
pub end_markers: Vec<BlockMarkerId>,
338+
pub decision_depth: u16,
337339
}

compiler/rustc_middle/src/mir/pretty.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -496,20 +496,27 @@ fn write_coverage_branch_info(
496496
)?;
497497
}
498498

499-
for coverage::MCDCBranchSpan { span, condition_info, true_marker, false_marker } in
500-
mcdc_branch_spans
499+
for coverage::MCDCBranchSpan {
500+
span,
501+
condition_info,
502+
true_marker,
503+
false_marker,
504+
decision_depth,
505+
} in mcdc_branch_spans
501506
{
502507
writeln!(
503508
w,
504-
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
509+
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?}, depth: {decision_depth:?} }} => {span:?}",
505510
condition_info.map(|info| info.condition_id)
506511
)?;
507512
}
508513

509-
for coverage::MCDCDecisionSpan { span, conditions_num, end_markers } in mcdc_decision_spans {
514+
for coverage::MCDCDecisionSpan { span, conditions_num, end_markers, decision_depth } in
515+
mcdc_decision_spans
516+
{
510517
writeln!(
511518
w,
512-
"{INDENT}coverage mcdc decision {{ conditions_num: {conditions_num:?}, end: {end_markers:?} }} => {span:?}"
519+
"{INDENT}coverage mcdc decision {{ conditions_num: {conditions_num:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
513520
)?;
514521
}
515522

compiler/rustc_mir_build/src/build/coverageinfo.rs

+2
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl MCDCState {
262262
span,
263263
conditions_num: 0,
264264
end_markers: vec![],
265+
decision_depth: 0,
265266
}),
266267
};
267268

@@ -371,6 +372,7 @@ impl Builder<'_, '_> {
371372
condition_info,
372373
true_marker,
373374
false_marker,
375+
decision_depth: 0,
374376
});
375377
return;
376378
}

compiler/rustc_mir_transform/src/coverage/mod.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,17 @@ fn create_mappings<'tcx>(
145145
|BcbMapping { kind: bcb_mapping_kind, span }| {
146146
let kind = match *bcb_mapping_kind {
147147
BcbMappingKind::Code(bcb) => MappingKind::Code(term_for_bcb(bcb)),
148-
BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info: None } => {
149-
MappingKind::Branch {
150-
true_term: term_for_bcb(true_bcb),
151-
false_term: term_for_bcb(false_bcb),
152-
}
153-
}
148+
BcbMappingKind::MCDCBranch {
149+
true_bcb, false_bcb, condition_info: None, ..
150+
} => MappingKind::Branch {
151+
true_term: term_for_bcb(true_bcb),
152+
false_term: term_for_bcb(false_bcb),
153+
},
154154
BcbMappingKind::MCDCBranch {
155155
true_bcb,
156156
false_bcb,
157157
condition_info: Some(mcdc_params),
158+
..
158159
} => MappingKind::MCDCBranch {
159160
true_term: term_for_bcb(true_bcb),
160161
false_term: term_for_bcb(false_bcb),
@@ -246,38 +247,42 @@ fn inject_mcdc_statements<'tcx>(
246247
}
247248

248249
// Inject test vector update first because `inject_statement` always insert new statement at head.
249-
for (end_bcbs, bitmap_idx) in
250+
for (end_bcbs, bitmap_idx, decision_depth) in
250251
coverage_spans.mappings.iter().filter_map(|mapping| match &mapping.kind {
251-
BcbMappingKind::MCDCDecision { end_bcbs, bitmap_idx, .. } => {
252-
Some((end_bcbs, *bitmap_idx))
252+
BcbMappingKind::MCDCDecision { end_bcbs, bitmap_idx, decision_depth, .. } => {
253+
Some((end_bcbs, *bitmap_idx, *decision_depth))
253254
}
254255
_ => None,
255256
})
256257
{
257258
for end in end_bcbs {
258259
let end_bb = basic_coverage_blocks[*end].leader_bb();
259-
inject_statement(mir_body, CoverageKind::TestVectorBitmapUpdate { bitmap_idx }, end_bb);
260+
inject_statement(
261+
mir_body,
262+
CoverageKind::TestVectorBitmapUpdate { bitmap_idx, decision_depth },
263+
end_bb,
264+
);
260265
}
261266
}
262267

263-
for (true_bcb, false_bcb, condition_id) in
268+
for (true_bcb, false_bcb, condition_id, decision_depth) in
264269
coverage_spans.mappings.iter().filter_map(|mapping| match mapping.kind {
265-
BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info } => {
266-
Some((true_bcb, false_bcb, condition_info?.condition_id))
270+
BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info, decision_depth } => {
271+
Some((true_bcb, false_bcb, condition_info?.condition_id, decision_depth))
267272
}
268273
_ => None,
269274
})
270275
{
271276
let true_bb = basic_coverage_blocks[true_bcb].leader_bb();
272277
inject_statement(
273278
mir_body,
274-
CoverageKind::CondBitmapUpdate { id: condition_id, value: true },
279+
CoverageKind::CondBitmapUpdate { id: condition_id, value: true, decision_depth },
275280
true_bb,
276281
);
277282
let false_bb = basic_coverage_blocks[false_bcb].leader_bb();
278283
inject_statement(
279284
mir_body,
280-
CoverageKind::CondBitmapUpdate { id: condition_id, value: false },
285+
CoverageKind::CondBitmapUpdate { id: condition_id, value: false, decision_depth },
281286
false_bb,
282287
);
283288
}

compiler/rustc_mir_transform/src/coverage/spans.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ pub(super) enum BcbMappingKind {
2626
/// If `None`, this actually represents a normal branch mapping inserted
2727
/// for code that was too complex for MC/DC.
2828
condition_info: Option<ConditionInfo>,
29+
decision_depth: u16,
2930
},
3031
/// Associates a mcdc decision with its join BCB.
31-
MCDCDecision { end_bcbs: BTreeSet<BasicCoverageBlock>, bitmap_idx: u32, conditions_num: u16 },
32+
MCDCDecision {
33+
end_bcbs: BTreeSet<BasicCoverageBlock>,
34+
bitmap_idx: u32,
35+
conditions_num: u16,
36+
decision_depth: u16,
37+
},
3238
}
3339

3440
#[derive(Debug)]

compiler/rustc_mir_transform/src/coverage/spans/from_mir.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,25 @@ pub(super) fn extract_mcdc_mappings(
453453
Some((span, true_bcb, false_bcb))
454454
};
455455

456-
let mcdc_branch_filter_map =
457-
|&MCDCBranchSpan { span: raw_span, true_marker, false_marker, condition_info }| {
458-
check_branch_bcb(raw_span, true_marker, false_marker).map(
459-
|(span, true_bcb, false_bcb)| BcbMapping {
460-
kind: BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info },
461-
span,
456+
let mcdc_branch_filter_map = |&MCDCBranchSpan {
457+
span: raw_span,
458+
true_marker,
459+
false_marker,
460+
condition_info,
461+
decision_depth,
462+
}| {
463+
check_branch_bcb(raw_span, true_marker, false_marker).map(|(span, true_bcb, false_bcb)| {
464+
BcbMapping {
465+
kind: BcbMappingKind::MCDCBranch {
466+
true_bcb,
467+
false_bcb,
468+
condition_info,
469+
decision_depth,
462470
},
463-
)
464-
};
471+
span,
472+
}
473+
})
474+
};
465475

466476
let mut next_bitmap_idx = 0;
467477

@@ -482,6 +492,7 @@ pub(super) fn extract_mcdc_mappings(
482492
end_bcbs,
483493
bitmap_idx,
484494
conditions_num: decision.conditions_num as u16,
495+
decision_depth: decision.decision_depth,
485496
},
486497
span,
487498
})

0 commit comments

Comments
 (0)