Skip to content

Commit 67ebff7

Browse files
committed
mcdc-coverage: Get decision_depth from THIR lowring
1 parent ceee6b1 commit 67ebff7

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
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

+27-5
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ impl BranchInfoBuilder {
106106
tcx: TyCtxt<'_>,
107107
true_marker: BlockMarkerId,
108108
false_marker: BlockMarkerId,
109-
) -> Option<ConditionInfo> {
109+
) -> Option<(u16, ConditionInfo)> {
110110
let mcdc_state = self.mcdc_state.as_mut()?;
111+
let decision_depth = mcdc_state.decision_depth;
111112
let (mut condition_info, decision_result) =
112113
mcdc_state.take_condition(true_marker, false_marker);
113114
if let Some(decision) = decision_result {
@@ -141,7 +142,7 @@ impl BranchInfoBuilder {
141142
}
142143
}
143144
}
144-
condition_info
145+
condition_info.map(|cond_info| (decision_depth, cond_info))
145146
}
146147

147148
fn next_block_marker_id(&mut self) -> BlockMarkerId {
@@ -183,13 +184,18 @@ struct MCDCState {
183184
/// To construct condition evaluation tree.
184185
decision_stack: VecDeque<ConditionInfo>,
185186
processing_decision: Option<MCDCDecisionSpan>,
187+
decision_depth: u16,
186188
}
187189

188190
impl MCDCState {
189191
fn new_if_enabled(tcx: TyCtxt<'_>) -> Option<Self> {
190192
tcx.sess
191193
.instrument_coverage_mcdc()
192-
.then(|| Self { decision_stack: VecDeque::new(), processing_decision: None })
194+
.then(|| Self {
195+
decision_stack: VecDeque::new(),
196+
processing_decision: None,
197+
decision_depth: 0,
198+
})
193199
}
194200

195201
// At first we assign ConditionIds for each sub expression.
@@ -356,15 +362,15 @@ impl Builder<'_, '_> {
356362
let true_marker = inject_branch_marker(then_block);
357363
let false_marker = inject_branch_marker(else_block);
358364

359-
if let Some(condition_info) =
365+
if let Some((decision_depth, condition_info)) =
360366
branch_info.fetch_condition_info(self.tcx, true_marker, false_marker)
361367
{
362368
branch_info.mcdc_branch_spans.push(MCDCBranchSpan {
363369
span: source_info.span,
364370
condition_info,
365371
true_marker,
366372
false_marker,
367-
decision_depth: 0,
373+
decision_depth,
368374
});
369375
} else {
370376
branch_info.branch_spans.push(BranchSpan {
@@ -380,4 +386,20 @@ impl Builder<'_, '_> {
380386
branch_info.record_conditions_operation(logical_op, span);
381387
}
382388
}
389+
390+
pub(crate) fn mcdc_increment_depth_if_enabled(&mut self) {
391+
if let Some(branch_info) = self.coverage_branch_info.as_mut()
392+
&& let Some(mcdc_state) = branch_info.mcdc_state.as_mut()
393+
{
394+
mcdc_state.decision_depth += 1;
395+
};
396+
}
397+
398+
pub(crate) fn mcdc_decrement_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+
}
383405
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,22 @@ 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+
.all_bcb_mappings()
107+
.filter_map(|bcb_mapping| match bcb_mapping.kind {
108+
BcbMappingKind::MCDCDecision { decision_depth, .. } => Some(decision_depth),
109+
_ => None,
110+
})
111+
.max()
112+
.unwrap_or(0);
113+
105114
mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo {
106115
function_source_hash: hir_info.function_source_hash,
107116
num_counters: coverage_counters.num_counters(),
108117
mcdc_bitmap_bytes: coverage_spans.test_vector_bitmap_bytes(),
109118
expressions: coverage_counters.into_expressions(),
110119
mappings,
120+
mcdc_max_decision_depth
111121
}));
112122
}
113123

0 commit comments

Comments
 (0)