Skip to content

Commit cb55ae4

Browse files
committed
mcdc-coverage: Remove MCDCBlockMarker, Rename DecisionMarkerId, Add ConditionEntry/Output markers
1 parent 20cdc51 commit cb55ae4

File tree

6 files changed

+58
-41
lines changed

6 files changed

+58
-41
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
106106
match *kind {
107107
CoverageKind::SpanMarker
108108
| CoverageKind::BlockMarker { .. }
109-
| CoverageKind::MCDCBlockMarker { .. }
110109
| CoverageKind::MCDCDecisionEntryMarker { .. }
111-
| CoverageKind::MCDCDecisionOutputMarker { .. } => unreachable!(
110+
| CoverageKind::MCDCDecisionOutputMarker { .. }
111+
| CoverageKind::MCDCConditionEntryMarker { .. }
112+
| CoverageKind::MCDCConditionOutputMarker { .. } => unreachable!(
112113
"marker statement {kind:?} should have been removed by CleanupPostBorrowck"
113114
),
114115
CoverageKind::CounterIncrement { id } => {

compiler/rustc_middle/src/mir/coverage.rs

+38-15
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ rustc_index::newtype_index! {
1818
rustc_index::newtype_index! {
1919
#[derive(HashStable)]
2020
#[encodable]
21-
#[debug_format = "DecisionMarkerId({})"]
22-
pub struct DecisionMarkerId {}
21+
#[debug_format = "DecisionId({})"]
22+
pub struct DecisionId {}
23+
}
24+
25+
rustc_index::newtype_index! {
26+
#[derive(HashStable)]
27+
#[encodable]
28+
#[debug_format = "ConditionId({})"]
29+
pub struct ConditionId {}
2330
}
2431

2532
rustc_index::newtype_index! {
@@ -104,24 +111,31 @@ pub enum CoverageKind {
104111
/// Should be erased before codegen (at some point after `InstrumentCoverage`).
105112
BlockMarker { id: BlockMarkerId },
106113

107-
/// Same as BlockMarker, but carries a reference to its parent decision for
108-
/// MCDC coverage.
109-
///
110-
/// Has no effect during codegen.
111-
MCDCBlockMarker { id: BlockMarkerId, decision_id: DecisionMarkerId },
112-
113114
/// Marks the first condition of a decision (boolean expression). All
114115
/// conditions in the same decision will reference this id.
115116
///
116117
/// Has no effect during codegen.
117-
MCDCDecisionEntryMarker { id: DecisionMarkerId },
118+
MCDCDecisionEntryMarker { id: DecisionId },
118119

119120
/// Marks one of the basic blocks following the decision referenced with `id`.
120121
/// the outcome bool designates which branch of the decision it is:
121122
/// `true` for the then block, `false` for the else block.
122123
///
123124
/// Has no effect during codegen.
124-
MCDCDecisionOutputMarker { id: DecisionMarkerId, outcome: bool },
125+
MCDCDecisionOutputMarker { id: DecisionId, outcome: bool },
126+
127+
/// Marks a basic block where a condition evaluation occurs
128+
/// The block may end with a SwitchInt where the 2 successors BBs have a
129+
/// MCDCConditionOutcomeMarker statement with a matching ID.
130+
///
131+
/// Has no effect during codegen.
132+
MCDCConditionEntryMarker { decision_id: DecisionId, id: ConditionId },
133+
134+
/// Marks a basic block that is branched from a condition evaluation.
135+
/// The block may have a predecessor with a matching ID.
136+
///
137+
/// Has no effect during codegen.
138+
MCDCConditionOutputMarker { decision_id: DecisionId, id: ConditionId, outcome: bool },
125139

126140
/// Marks the point in MIR control flow represented by a coverage counter.
127141
///
@@ -164,15 +178,24 @@ impl Debug for CoverageKind {
164178
match self {
165179
SpanMarker => write!(fmt, "SpanMarker"),
166180
BlockMarker { id } => write!(fmt, "BlockMarker({:?})", id.index()),
167-
MCDCBlockMarker { id, decision_id } => {
168-
write!(fmt, "MCDCBlockMarker({:?}, {:?})", id.index(), decision_id.index())
169-
}
170181
MCDCDecisionEntryMarker { id } => {
171182
write!(fmt, "MCDCDecisionEntryMarker({:?})", id.index())
172183
}
173184
MCDCDecisionOutputMarker { id, outcome } => {
174185
write!(fmt, "MCDCDecisionOutputMarker({:?}, {})", id.index(), outcome)
175186
}
187+
MCDCConditionEntryMarker { decision_id, id } => {
188+
write!(fmt, "MCDCConditionEntryMarker({:?}, {:?})", decision_id.index(), id.index())
189+
}
190+
MCDCConditionOutputMarker { decision_id, id, outcome } => {
191+
write!(
192+
fmt,
193+
"MCDCConditionOutcomeMarker({:?}, {:?}, {})",
194+
decision_id.index(),
195+
id.index(),
196+
outcome
197+
)
198+
}
176199
CounterIncrement { id } => write!(fmt, "CounterIncrement({:?})", id.index()),
177200
ExpressionUsed { id } => write!(fmt, "ExpressionUsed({:?})", id.index()),
178201
MCDCBitmapRequire { needed_bytes } => {
@@ -300,7 +323,7 @@ pub struct BranchInfo {
300323

301324
/// Associate a span for every decision in the function body.
302325
/// Empty if MCDC coverage is disabled.
303-
pub decision_spans: IndexVec<DecisionMarkerId, DecisionSpan>,
326+
pub decision_spans: IndexVec<DecisionId, DecisionSpan>,
304327
}
305328

306329
#[derive(Clone, Debug)]
@@ -319,7 +342,7 @@ pub struct BranchSpan {
319342
pub span: Span,
320343
/// ID of Decision structure the branch is part of. Only used in
321344
/// the MCDC coverage.
322-
pub decision_id: DecisionMarkerId,
345+
pub decision_id: DecisionId,
323346
pub true_marker: BlockMarkerId,
324347
pub false_marker: BlockMarkerId,
325348
}

compiler/rustc_middle/src/ty/structural_impls.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ TrivialTypeTraversalImpls! {
407407
::rustc_hir::MatchSource,
408408
::rustc_target::asm::InlineAsmRegOrRegClass,
409409
crate::mir::coverage::BlockMarkerId,
410-
crate::mir::coverage::DecisionMarkerId,
410+
crate::mir::coverage::DecisionId,
411+
crate::mir::coverage::ConditionId,
411412
crate::mir::coverage::CounterId,
412413
crate::mir::coverage::ExpressionId,
413414
crate::mir::Local,

compiler/rustc_mir_build/src/build/coverageinfo.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::hash_map::Entry;
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_index::IndexVec;
66
use rustc_middle::mir::coverage::{
7-
BlockMarkerId, BranchSpan, CoverageKind, DecisionMarkerId, DecisionSpan,
7+
BlockMarkerId, BranchSpan, CoverageKind, DecisionId, DecisionSpan,
88
};
99
use rustc_middle::mir::{self, BasicBlock, UnOp};
1010
use rustc_middle::thir::{ExprId, ExprKind, Thir};
@@ -26,12 +26,12 @@ pub(crate) struct BranchInfoBuilder {
2626
/// ID of the current decision.
2727
/// Do not use directly. Use the function instead, as it will hide
2828
/// the decision in the scope of nested decisions.
29-
current_decision_id: Option<DecisionMarkerId>,
29+
current_decision_id: Option<DecisionId>,
3030
/// Track the nesting level of decision to avoid MCDC instrumentation of
3131
/// nested decisions.
3232
nested_decision_level: u32,
3333
/// Vector for storing all the decisions with their span
34-
decisions: IndexVec<DecisionMarkerId, Span>,
34+
decisions: IndexVec<DecisionId, Span>,
3535
}
3636

3737
#[derive(Clone, Copy)]
@@ -158,7 +158,7 @@ impl BranchInfoBuilder {
158158
self.nested_decision_level > 1
159159
}
160160

161-
pub fn current_decision_id(&self) -> Option<DecisionMarkerId> {
161+
pub fn current_decision_id(&self) -> Option<DecisionId> {
162162
if self.in_nested_condition() { None } else { self.current_decision_id }
163163
}
164164
}
@@ -191,14 +191,10 @@ impl Builder<'_, '_> {
191191
let mut inject_branch_marker = |block: BasicBlock| {
192192
let id = branch_info.next_block_marker_id();
193193

194-
let cov_kind = if let Some(decision_id) = branch_info.current_decision_id() {
195-
CoverageKind::MCDCBlockMarker { id, decision_id }
196-
} else {
197-
CoverageKind::BlockMarker { id }
194+
let marker_statement = mir::Statement {
195+
source_info,
196+
kind: mir::StatementKind::Coverage(CoverageKind::BlockMarker { id }),
198197
};
199-
200-
let marker_statement =
201-
mir::Statement { source_info, kind: mir::StatementKind::Coverage(cov_kind) };
202198
self.cfg.push(block, marker_statement);
203199

204200
id
@@ -210,7 +206,7 @@ impl Builder<'_, '_> {
210206
branch_info.branch_spans.push(BranchSpan {
211207
span: source_info.span,
212208
// FIXME(dprn): Handle case when MCDC is disabled better than just putting 0.
213-
decision_id: branch_info.current_decision_id.unwrap_or(DecisionMarkerId::from_u32(0)),
209+
decision_id: branch_info.current_decision_id.unwrap_or(DecisionId::from_u32(0)),
214210
true_marker,
215211
false_marker,
216212
});

compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ impl<'tcx> MirPass<'tcx> for CleanupPostBorrowck {
3737
// MIR building, and are not needed after InstrumentCoverage.
3838
CoverageKind::BlockMarker { .. }
3939
| CoverageKind::SpanMarker { .. }
40-
| CoverageKind::MCDCBlockMarker { .. }
4140
| CoverageKind::MCDCDecisionEntryMarker { .. }
42-
| CoverageKind::MCDCDecisionOutputMarker { .. },
41+
| CoverageKind::MCDCDecisionOutputMarker { .. }
42+
| CoverageKind::MCDCConditionEntryMarker { .. }
43+
| CoverageKind::MCDCConditionOutputMarker { .. },
4344
)
4445
| StatementKind::FakeRead(..) => statement.make_nop(),
4546
_ => (),

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span> {
226226
// Block markers are used for branch coverage, so ignore them here.
227227
CoverageKind::BlockMarker {..}
228228
// Ignore MCDC markers as well
229-
| CoverageKind::MCDCBlockMarker{ .. }
230229
| CoverageKind::MCDCDecisionEntryMarker{ .. }
231230
| CoverageKind::MCDCDecisionOutputMarker { .. }
231+
| CoverageKind::MCDCConditionEntryMarker { .. }
232+
| CoverageKind::MCDCConditionOutputMarker { .. }
232233
) => None,
233234

234235
// These coverage statements should not exist prior to coverage instrumentation.
@@ -389,14 +390,8 @@ pub(super) fn extract_branch_mappings(
389390
// Fill out the mapping from block marker IDs to their enclosing blocks.
390391
for (bb, data) in mir_body.basic_blocks.iter_enumerated() {
391392
for statement in &data.statements {
392-
if let StatementKind::Coverage(kind) = &statement.kind {
393-
match kind {
394-
CoverageKind::BlockMarker { id }
395-
| CoverageKind::MCDCBlockMarker { id, decision_id: _ } => {
396-
block_markers[*id] = Some(bb);
397-
}
398-
_ => (),
399-
}
393+
if let StatementKind::Coverage(CoverageKind::BlockMarker { id }) = &statement.kind {
394+
block_markers[*id] = Some(bb);
400395
}
401396
}
402397
}

0 commit comments

Comments
 (0)