Skip to content

Commit 5d4db48

Browse files
committed
mcdc-coverage: Get decision_depth from THIR lowring
1 parent d33a846 commit 5d4db48

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ fn ensure_mcdc_parameters<'ll, 'tcx>(
204204
let fn_name = bx.get_pgo_func_name_var(instance);
205205
let hash = bx.const_u64(function_coverage_info.function_source_hash);
206206
let bitmap_bytes = bx.const_u32(function_coverage_info.mcdc_bitmap_bytes);
207-
let cond_bitmap = bx.mcdc_parameters(fn_name, hash, bitmap_bytes, 1_u32);
207+
let max_decision_depth = function_coverage_info.mcdc_max_decision_depth;
208+
let cond_bitmap = bx.mcdc_parameters(fn_name, hash, bitmap_bytes, max_decision_depth as u32);
208209
bx.coverage_context()
209210
.expect("already checked above")
210211
.mcdc_condition_bitmap_map

compiler/rustc_middle/src/mir/coverage.rs

+3
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ pub struct FunctionCoverageInfo {
269269
pub mcdc_bitmap_bytes: u32,
270270
pub expressions: IndexVec<ExpressionId, Expression>,
271271
pub mappings: Vec<Mapping>,
272+
/// The depth of the deepest decision is used to know how many
273+
/// temp condbitmaps should be allocated for the function.
274+
pub mcdc_max_decision_depth: u16,
272275
}
273276

274277
/// Branch information recorded during THIR-to-MIR lowering, and stored in MIR.

compiler/rustc_mir_build/src/build/coverageinfo.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ impl BranchInfoBuilder {
101101
tcx: TyCtxt<'_>,
102102
true_marker: BlockMarkerId,
103103
false_marker: BlockMarkerId,
104-
) -> Option<ConditionInfo> {
104+
) -> Option<(u16, ConditionInfo)> {
105105
let mcdc_state = self.mcdc_state.as_mut()?;
106+
let decision_depth = mcdc_state.decision_depth;
106107
let (mut condition_info, decision_result) =
107108
mcdc_state.take_condition(true_marker, false_marker);
108109
if let Some(decision) = decision_result {
@@ -131,7 +132,7 @@ impl BranchInfoBuilder {
131132
}
132133
}
133134
}
134-
condition_info
135+
condition_info.map(|cond_info| (decision_depth, cond_info))
135136
}
136137

137138
fn add_two_way_branch<'tcx>(
@@ -203,13 +204,16 @@ struct MCDCState {
203204
/// To construct condition evaluation tree.
204205
decision_stack: VecDeque<ConditionInfo>,
205206
processing_decision: Option<MCDCDecisionSpan>,
207+
decision_depth: u16,
206208
}
207209

208210
impl MCDCState {
209211
fn new_if_enabled(tcx: TyCtxt<'_>) -> Option<Self> {
210-
tcx.sess
211-
.instrument_coverage_mcdc()
212-
.then(|| Self { decision_stack: VecDeque::new(), processing_decision: None })
212+
tcx.sess.instrument_coverage_mcdc().then(|| Self {
213+
decision_stack: VecDeque::new(),
214+
processing_decision: None,
215+
decision_depth: 0,
216+
})
213217
}
214218

215219
// At first we assign ConditionIds for each sub expression.
@@ -365,14 +369,17 @@ impl Builder<'_, '_> {
365369
|block| branch_info.inject_block_marker(&mut self.cfg, source_info, block);
366370
let true_marker = inject_block_marker(then_block);
367371
let false_marker = inject_block_marker(else_block);
368-
let condition_info =
369-
branch_info.fetch_mcdc_condition_info(self.tcx, true_marker, false_marker);
372+
let (decision_depth, condition_info) = branch_info
373+
.fetch_mcdc_condition_info(self.tcx, true_marker, false_marker)
374+
.map_or((0, None), |(decision_depth, condition_info)| {
375+
(decision_depth, Some(condition_info))
376+
});
370377
branch_info.mcdc_branch_spans.push(MCDCBranchSpan {
371378
span: source_info.span,
372379
condition_info,
373380
true_marker,
374381
false_marker,
375-
decision_depth: 0,
382+
decision_depth,
376383
});
377384
return;
378385
}
@@ -387,4 +394,20 @@ impl Builder<'_, '_> {
387394
mcdc_state.record_conditions(logical_op, span);
388395
}
389396
}
397+
398+
pub(crate) fn mcdc_increment_depth_if_enabled(&mut self) {
399+
if let Some(branch_info) = self.coverage_branch_info.as_mut()
400+
&& let Some(mcdc_state) = branch_info.mcdc_state.as_mut()
401+
{
402+
mcdc_state.decision_depth += 1;
403+
};
404+
}
405+
406+
pub(crate) fn mcdc_decrement_depth_if_enabled(&mut self) {
407+
if let Some(branch_info) = self.coverage_branch_info.as_mut()
408+
&& let Some(mcdc_state) = branch_info.mcdc_state.as_mut()
409+
{
410+
mcdc_state.decision_depth -= 1;
411+
};
412+
}
390413
}

compiler/rustc_mir_build/src/build/matches/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
151151
let mut block = block;
152152
let temp_scope = args.temp_scope_override.unwrap_or_else(|| this.local_scope());
153153
let mutability = Mutability::Mut;
154+
155+
// Increment the decision depth, in case we encounter boolean expressions
156+
// further down.
157+
this.mcdc_increment_depth_if_enabled();
154158
let place =
155159
unpack!(block = this.as_temp(block, Some(temp_scope), expr_id, mutability));
160+
this.mcdc_decrement_depth_if_enabled();
161+
156162
let operand = Operand::Move(Place::from(place));
157163

158164
let then_block = this.cfg.start_new_block();

compiler/rustc_mir_transform/src/coverage/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,23 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
102102

103103
inject_mcdc_statements(mir_body, &basic_coverage_blocks, &coverage_spans);
104104

105+
let mcdc_max_decision_depth = coverage_spans
106+
.mappings
107+
.iter()
108+
.filter_map(|bcb_mapping| match bcb_mapping.kind {
109+
BcbMappingKind::MCDCDecision { decision_depth, .. } => Some(decision_depth),
110+
_ => None,
111+
})
112+
.max()
113+
.unwrap_or(0);
114+
105115
mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo {
106116
function_source_hash: hir_info.function_source_hash,
107117
num_counters: coverage_counters.num_counters(),
108118
mcdc_bitmap_bytes: coverage_spans.test_vector_bitmap_bytes(),
109119
expressions: coverage_counters.into_expressions(),
110120
mappings,
121+
mcdc_max_decision_depth,
111122
}));
112123
}
113124

0 commit comments

Comments
 (0)