Skip to content

Commit 33de2bb

Browse files
committed
mcdc-coverage(instrumentation): Rename Id names, Start instrumentation implementation
1 parent 83b8166 commit 33de2bb

File tree

5 files changed

+257
-49
lines changed

5 files changed

+257
-49
lines changed

compiler/rustc_middle/src/mir/coverage.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ rustc_index::newtype_index! {
1515
pub struct BlockMarkerId {}
1616
}
1717

18+
// DecisionMarkerId and ConditionMarkerId are used between THIR and MIR representations.
19+
// DecisionId and ConditionId are used between MIR representation and LLVM-IR.
20+
21+
rustc_index::newtype_index! {
22+
#[derive(HashStable)]
23+
#[encodable]
24+
#[debug_format = "DecisionMarkerId({})"]
25+
pub struct DecisionMarkerId {}
26+
}
27+
28+
rustc_index::newtype_index! {
29+
#[derive(HashStable)]
30+
#[encodable]
31+
#[debug_format = "ConditionMarkerId({})"]
32+
pub struct ConditionMarkerId {}
33+
}
34+
1835
rustc_index::newtype_index! {
1936
#[derive(HashStable)]
2037
#[encodable]
@@ -115,27 +132,31 @@ pub enum CoverageKind {
115132
/// conditions in the same decision will reference this id.
116133
///
117134
/// Has no effect during codegen.
118-
MCDCDecisionEntryMarker { id: DecisionId },
135+
MCDCDecisionEntryMarker { decm_id: DecisionMarkerId },
119136

120137
/// Marks one of the basic blocks following the decision referenced with `id`.
121138
/// the outcome bool designates which branch of the decision it is:
122139
/// `true` for the then block, `false` for the else block.
123140
///
124141
/// Has no effect during codegen.
125-
MCDCDecisionOutputMarker { id: DecisionId, outcome: bool },
142+
MCDCDecisionOutputMarker { decm_id: DecisionMarkerId, outcome: bool },
126143

127144
/// Marks a basic block where a condition evaluation occurs
128145
/// The block may end with a SwitchInt where the 2 successors BBs have a
129146
/// MCDCConditionOutcomeMarker statement with a matching ID.
130147
///
131148
/// Has no effect during codegen.
132-
MCDCConditionEntryMarker { decision_id: DecisionId, id: ConditionId },
149+
MCDCConditionEntryMarker { decm_id: DecisionMarkerId, condm_id: ConditionMarkerId },
133150

134151
/// Marks a basic block that is branched from a condition evaluation.
135152
/// The block may have a predecessor with a matching ID.
136153
///
137154
/// Has no effect during codegen.
138-
MCDCConditionOutputMarker { decision_id: DecisionId, id: ConditionId, outcome: bool },
155+
MCDCConditionOutputMarker {
156+
decm_id: DecisionMarkerId,
157+
condm_id: ConditionMarkerId,
158+
outcome: bool,
159+
},
139160

140161
/// Marks the point in MIR control flow represented by a coverage counter.
141162
///
@@ -178,20 +199,20 @@ impl Debug for CoverageKind {
178199
match self {
179200
SpanMarker => write!(fmt, "SpanMarker"),
180201
BlockMarker { id } => write!(fmt, "BlockMarker({:?})", id.index()),
181-
MCDCDecisionEntryMarker { id } => {
202+
MCDCDecisionEntryMarker { decm_id: id } => {
182203
write!(fmt, "MCDCDecisionEntryMarker({:?})", id.index())
183204
}
184-
MCDCDecisionOutputMarker { id, outcome } => {
205+
MCDCDecisionOutputMarker { decm_id: id, outcome } => {
185206
write!(fmt, "MCDCDecisionOutputMarker({:?}, {})", id.index(), outcome)
186207
}
187-
MCDCConditionEntryMarker { decision_id, id } => {
208+
MCDCConditionEntryMarker { decm_id: decision_id, condm_id: id } => {
188209
write!(fmt, "MCDCConditionEntryMarker({:?}, {:?})", decision_id.index(), id.index())
189210
}
190-
MCDCConditionOutputMarker { decision_id, id, outcome } => {
211+
MCDCConditionOutputMarker { decm_id: decision_marker_id, condm_id: id, outcome } => {
191212
write!(
192213
fmt,
193214
"MCDCConditionOutcomeMarker({:?}, {:?}, {})",
194-
decision_id.index(),
215+
decision_marker_id.index(),
195216
id.index(),
196217
outcome
197218
)
@@ -323,7 +344,7 @@ pub struct BranchInfo {
323344

324345
/// Associate a span for every decision in the function body.
325346
/// Empty if MCDC coverage is disabled.
326-
pub decision_spans: IndexVec<DecisionId, DecisionSpan>,
347+
pub decision_spans: IndexVec<DecisionMarkerId, DecisionSpan>,
327348
}
328349

329350
#[derive(Clone, Debug)]
@@ -342,7 +363,7 @@ pub struct BranchSpan {
342363
pub span: Span,
343364
/// ID of Decision structure the branch is part of. Only used in
344365
/// the MCDC coverage.
345-
pub decision_id: DecisionId,
366+
pub decision_id: DecisionMarkerId,
346367
pub true_marker: BlockMarkerId,
347368
pub false_marker: BlockMarkerId,
348369
}

compiler/rustc_middle/src/ty/structural_impls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ TrivialTypeTraversalImpls! {
407407
::rustc_hir::MatchSource,
408408
::rustc_target::asm::InlineAsmRegOrRegClass,
409409
crate::mir::coverage::BlockMarkerId,
410+
crate::mir::coverage::DecisionMarkerId,
411+
crate::mir::coverage::ConditionMarkerId,
410412
crate::mir::coverage::DecisionId,
411413
crate::mir::coverage::ConditionId,
412414
crate::mir::coverage::CounterId,

compiler/rustc_mir_build/src/build/coverageinfo.rs

+15-28
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, ConditionId, CoverageKind, DecisionId, DecisionSpan,
7+
BlockMarkerId, BranchSpan, ConditionMarkerId, CoverageKind, DecisionMarkerId, DecisionSpan,
88
};
99
use rustc_middle::mir::{self, BasicBlock, UnOp};
1010
use rustc_middle::thir::{ExprId, ExprKind, Thir};
@@ -19,12 +19,12 @@ struct MCDCInfoBuilder {
1919
/// ID of the current decision.
2020
/// Do not use directly. Use the function instead, as it will hide
2121
/// the decision in the scope of nested decisions.
22-
current_decision_id: Option<DecisionId>,
22+
current_decision_id: Option<DecisionMarkerId>,
2323
/// Track the nesting level of decision to avoid MCDC instrumentation of
2424
/// nested decisions.
2525
nested_decision_level: u32,
2626
/// Vector for storing all the decisions with their span
27-
decision_spans: IndexVec<DecisionId, Span>,
27+
decision_spans: IndexVec<DecisionMarkerId, Span>,
2828

2929
next_condition_id: u32,
3030
}
@@ -52,7 +52,7 @@ impl MCDCInfoBuilder {
5252
self.nested_decision_level > 1
5353
}
5454

55-
pub fn current_decision_id(&self) -> Option<DecisionId> {
55+
pub fn current_decision_id(&self) -> Option<DecisionMarkerId> {
5656
if self.in_nested_condition() { None } else { self.current_decision_id }
5757
}
5858

@@ -235,7 +235,7 @@ impl Builder<'_, '_> {
235235
.mcdc_info
236236
.as_ref()
237237
.and_then(|mcdc_info| mcdc_info.current_decision_id())
238-
.unwrap_or(DecisionId::from_u32(0)),
238+
.unwrap_or(DecisionMarkerId::from_u32(0)),
239239
true_marker,
240240
false_marker,
241241
});
@@ -268,7 +268,7 @@ impl Builder<'_, '_> {
268268
let marker_statement = mir::Statement {
269269
source_info,
270270
kind: mir::StatementKind::Coverage(CoverageKind::MCDCDecisionEntryMarker {
271-
id: decision_id,
271+
decm_id: decision_id,
272272
}),
273273
};
274274
self.cfg.push(block, marker_statement);
@@ -290,43 +290,31 @@ impl Builder<'_, '_> {
290290
/// If MCDC is enabled and the current decision is being instrumented,
291291
/// inject an `MCDCDecisionOutputMarker` to the given basic block.
292292
/// `outcome` should be true for the then block and false for the else block.
293-
pub(crate) fn mcdc_decision_outcome_block(
294-
&mut self,
295-
bb: BasicBlock,
296-
outcome: bool,
297-
) -> BasicBlock {
293+
pub(crate) fn mcdc_decision_outcome_block(&mut self, bb: BasicBlock, outcome: bool) {
298294
// Get the MCDCInfoBuilder object, which existence implies that MCDC is enabled.
299295
let Some(BranchInfoBuilder { mcdc_info: Some(mcdc_info), .. }) =
300296
self.coverage_branch_info.as_mut()
301297
else {
302-
return bb;
298+
return;
303299
};
304300

305301
let Some(decision_id) = mcdc_info.current_decision_id() else {
306302
// Decision is not instrumented
307-
return bb;
303+
return;
308304
};
309305

310306
let span = mcdc_info.decision_spans[decision_id];
311307
let source_info = self.source_info(span);
312308
let marker_statement = mir::Statement {
313309
source_info,
314310
kind: mir::StatementKind::Coverage(CoverageKind::MCDCDecisionOutputMarker {
315-
id: decision_id,
311+
decm_id: decision_id,
316312
outcome,
317313
}),
318314
};
319315

320316
// Insert statements at the beginning of the following basic block
321317
self.cfg.block_data_mut(bb).statements.insert(0, marker_statement);
322-
323-
// Create a new block to return
324-
let new_bb = self.cfg.start_new_block();
325-
326-
// Set bb -> new_bb
327-
self.cfg.goto(bb, source_info, new_bb);
328-
329-
new_bb
330318
}
331319

332320
/// Add markers on the condition's basic blocks to ease the later MCDC instrumentation.
@@ -344,13 +332,12 @@ impl Builder<'_, '_> {
344332
return;
345333
};
346334

347-
let Some(decision_id) = mcdc_info.current_decision_id() else {
335+
let Some(decm_id) = mcdc_info.current_decision_id() else {
348336
// If current_decision_id() is None, the decision is not instrumented.
349337
return;
350338
};
351339

352-
353-
let id = ConditionId::from_u32(mcdc_info.next_condition_id());
340+
let condm_id = ConditionMarkerId::from_u32(mcdc_info.next_condition_id());
354341
let span = self.thir[condition_expr].span;
355342
let source_info = self.source_info(span);
356343

@@ -362,15 +349,15 @@ impl Builder<'_, '_> {
362349

363350
inject_statement(
364351
condition_block,
365-
CoverageKind::MCDCConditionEntryMarker { decision_id, id },
352+
CoverageKind::MCDCConditionEntryMarker { decm_id, condm_id },
366353
);
367354
inject_statement(
368355
then_block,
369-
CoverageKind::MCDCConditionOutputMarker { decision_id, id, outcome: true },
356+
CoverageKind::MCDCConditionOutputMarker { decm_id, condm_id, outcome: true },
370357
);
371358
inject_statement(
372359
else_block,
373-
CoverageKind::MCDCConditionOutputMarker { decision_id, id, outcome: false },
360+
CoverageKind::MCDCConditionOutputMarker { decm_id, condm_id, outcome: false },
374361
);
375362
}
376363
}

compiler/rustc_mir_build/src/build/expr/into.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9292
));
9393

9494
// If MCDC is enabled, inject an outcome marker.
95-
let then_blk = this.mcdc_decision_outcome_block(then_blk, true);
95+
this.mcdc_decision_outcome_block(then_blk, true);
9696

9797
// Lower the `then` arm into its block.
9898
this.expr_into_dest(destination, then_blk, then)
@@ -101,7 +101,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
101101
// If MCDC is enabled and decision was instrumented, exit the
102102
// decision scope and inject an MCDC decision output marker
103103
// in the then and else blocks.
104-
let else_block = this.mcdc_decision_outcome_block(else_block, false);
104+
this.mcdc_decision_outcome_block(else_block, false);
105105
this.end_mcdc_decision_coverage();
106106

107107
// Pack `(then_block, else_block)` into `BlockAnd<BasicBlock>`.

0 commit comments

Comments
 (0)