Skip to content

Commit ceee6b1

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 60616ad commit ceee6b1

File tree

7 files changed

+65
-36
lines changed

7 files changed

+65
-36
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
146146
CoverageKind::ExpressionUsed { id } => {
147147
func_coverage.mark_expression_id_seen(id);
148148
}
149-
CoverageKind::CondBitmapUpdate { id, value, .. } => {
149+
CoverageKind::CondBitmapUpdate { id, value, decision_depth } => {
150150
drop(coverage_map);
151151
assert_ne!(
152152
id.as_u32(),
@@ -156,21 +156,23 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
156156
let cond_bitmaps = coverage_context
157157
.try_get_mcdc_condition_bitmap(&instance)
158158
.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");
159+
let cond_bitmap = cond_bitmaps
160+
.get(decision_depth as usize)
161+
.expect("no mcdc cond bitmap with the given depth");
161162
let cond_loc = bx.const_i32(id.as_u32() as i32 - 1);
162163
let bool_value = bx.const_bool(value);
163164
let fn_name = bx.get_pgo_func_name_var(instance);
164165
let hash = bx.const_u64(function_coverage_info.function_source_hash);
165166
bx.mcdc_condbitmap_update(fn_name, hash, cond_loc, cond_bitmap, bool_value);
166167
}
167-
CoverageKind::TestVectorBitmapUpdate { bitmap_idx } => {
168+
CoverageKind::TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
168169
drop(coverage_map);
169170
let cond_bitmaps = coverage_context
170171
.try_get_mcdc_condition_bitmap(&instance)
171172
.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");
173+
let cond_bitmap = cond_bitmaps
174+
.get(decision_depth as usize)
175+
.expect("no mcdc cond bitmap with the given depth");
174176
let bitmap_bytes = bx.tcx().coverage_ids_info(instance.def).mcdc_bitmap_bytes;
175177
assert!(bitmap_idx < bitmap_bytes, "bitmap index of the decision out of range");
176178
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
}
@@ -317,6 +317,7 @@ pub struct MCDCBranchSpan {
317317
pub condition_info: ConditionInfo,
318318
pub true_marker: BlockMarkerId,
319319
pub false_marker: BlockMarkerId,
320+
pub decision_depth: u16,
320321
}
321322

322323
#[derive(Copy, Clone, Debug)]
@@ -332,4 +333,5 @@ pub struct MCDCDecisionSpan {
332333
pub span: Span,
333334
pub conditions_num: usize,
334335
pub end_markers: Vec<BlockMarkerId>,
336+
pub decision_depth: u16,
335337
}

compiler/rustc_middle/src/mir/pretty.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -485,20 +485,27 @@ fn write_coverage_branch_info(
485485
)?;
486486
}
487487

488-
for coverage::MCDCBranchSpan { span, condition_info, true_marker, false_marker } in
489-
mcdc_branch_spans
488+
for coverage::MCDCBranchSpan {
489+
span,
490+
condition_info,
491+
true_marker,
492+
false_marker,
493+
decision_depth,
494+
} in mcdc_branch_spans
490495
{
491496
writeln!(
492497
w,
493-
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
498+
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?}, depth: {decision_depth:?} }} => {span:?}",
494499
condition_info.condition_id
495500
)?;
496501
}
497502

498-
for coverage::MCDCDecisionSpan { span, conditions_num, end_markers } in mcdc_decision_spans {
503+
for coverage::MCDCDecisionSpan { span, conditions_num, end_markers, decision_depth } in
504+
mcdc_decision_spans
505+
{
499506
writeln!(
500507
w,
501-
"{INDENT}coverage mcdc decision {{ conditions_num: {conditions_num:?}, end: {end_markers:?} }} => {span:?}"
508+
"{INDENT}coverage mcdc decision {{ conditions_num: {conditions_num:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
502509
)?;
503510
}
504511

compiler/rustc_mir_build/src/build/coverageinfo.rs

+2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ impl MCDCState {
242242
span,
243243
conditions_num: 0,
244244
end_markers: vec![],
245+
decision_depth: 0,
245246
}),
246247
};
247248

@@ -363,6 +364,7 @@ impl Builder<'_, '_> {
363364
condition_info,
364365
true_marker,
365366
false_marker,
367+
decision_depth: 0,
366368
});
367369
} else {
368370
branch_info.branch_spans.push(BranchSpan {

compiler/rustc_mir_transform/src/coverage/mod.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn create_mappings<'tcx>(
149149
true_term: term_for_bcb(true_bcb),
150150
false_term: term_for_bcb(false_bcb),
151151
},
152-
BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info } => {
152+
BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info, .. } => {
153153
MappingKind::MCDCBranch {
154154
true_term: term_for_bcb(true_bcb),
155155
false_term: term_for_bcb(false_bcb),
@@ -232,38 +232,42 @@ fn inject_mcdc_statements<'tcx>(
232232
}
233233

234234
// Inject test vector update first because `inject_statement` always insert new statement at head.
235-
for (end_bcbs, bitmap_idx) in
235+
for (end_bcbs, bitmap_idx, decision_depth) in
236236
coverage_spans.all_bcb_mappings().filter_map(|mapping| match &mapping.kind {
237-
BcbMappingKind::MCDCDecision { end_bcbs, bitmap_idx, .. } => {
238-
Some((end_bcbs, *bitmap_idx))
237+
BcbMappingKind::MCDCDecision { end_bcbs, bitmap_idx, decision_depth, .. } => {
238+
Some((end_bcbs, *bitmap_idx, *decision_depth))
239239
}
240240
_ => None,
241241
})
242242
{
243243
for end in end_bcbs {
244244
let end_bb = basic_coverage_blocks[*end].leader_bb();
245-
inject_statement(mir_body, CoverageKind::TestVectorBitmapUpdate { bitmap_idx }, end_bb);
245+
inject_statement(
246+
mir_body,
247+
CoverageKind::TestVectorBitmapUpdate { bitmap_idx, decision_depth },
248+
end_bb,
249+
);
246250
}
247251
}
248252

249-
for (true_bcb, false_bcb, condition_id) in
253+
for (true_bcb, false_bcb, condition_id, decision_depth) in
250254
coverage_spans.all_bcb_mappings().filter_map(|mapping| match mapping.kind {
251-
BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info } => {
252-
Some((true_bcb, false_bcb, condition_info.condition_id))
255+
BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info, decision_depth } => {
256+
Some((true_bcb, false_bcb, condition_info.condition_id, decision_depth))
253257
}
254258
_ => None,
255259
})
256260
{
257261
let true_bb = basic_coverage_blocks[true_bcb].leader_bb();
258262
inject_statement(
259263
mir_body,
260-
CoverageKind::CondBitmapUpdate { id: condition_id, value: true },
264+
CoverageKind::CondBitmapUpdate { id: condition_id, value: true, decision_depth },
261265
true_bb,
262266
);
263267
let false_bb = basic_coverage_blocks[false_bcb].leader_bb();
264268
inject_statement(
265269
mir_body,
266-
CoverageKind::CondBitmapUpdate { id: condition_id, value: false },
270+
CoverageKind::CondBitmapUpdate { id: condition_id, value: false, decision_depth },
267271
false_bb,
268272
);
269273
}

compiler/rustc_mir_transform/src/coverage/spans.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ pub(super) enum BcbMappingKind {
2222
true_bcb: BasicCoverageBlock,
2323
false_bcb: BasicCoverageBlock,
2424
condition_info: ConditionInfo,
25+
decision_depth: u16
2526
},
2627
/// Associates a mcdc decision with its join BCB.
27-
MCDCDecision { end_bcbs: BTreeSet<BasicCoverageBlock>, bitmap_idx: u32, conditions_num: u16 },
28+
MCDCDecision { end_bcbs: BTreeSet<BasicCoverageBlock>, bitmap_idx: u32, conditions_num: u16, decision_depth: u16 },
2829
}
2930

3031
#[derive(Debug)]

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

+19-8
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,25 @@ pub(super) fn extract_branch_mappings(
412412
})
413413
};
414414

415-
let mcdc_branch_filter_map =
416-
|&MCDCBranchSpan { span: raw_span, true_marker, false_marker, condition_info }| {
417-
check_branch_bcb(raw_span, true_marker, false_marker).map(
418-
|(span, true_bcb, false_bcb)| BcbMapping {
419-
kind: BcbMappingKind::MCDCBranch { true_bcb, false_bcb, condition_info },
420-
span,
415+
let mcdc_branch_filter_map = |&MCDCBranchSpan {
416+
span: raw_span,
417+
true_marker,
418+
false_marker,
419+
condition_info,
420+
decision_depth,
421+
}| {
422+
check_branch_bcb(raw_span, true_marker, false_marker).map(|(span, true_bcb, false_bcb)| {
423+
BcbMapping {
424+
kind: BcbMappingKind::MCDCBranch {
425+
true_bcb,
426+
false_bcb,
427+
condition_info,
428+
decision_depth,
421429
},
422-
)
423-
};
430+
span,
431+
}
432+
})
433+
};
424434

425435
let mut next_bitmap_idx = 0;
426436

@@ -441,6 +451,7 @@ pub(super) fn extract_branch_mappings(
441451
end_bcbs,
442452
bitmap_idx,
443453
conditions_num: decision.conditions_num as u16,
454+
decision_depth: decision.decision_depth,
444455
},
445456
span,
446457
})

0 commit comments

Comments
 (0)