Skip to content

Commit f191b1c

Browse files
committed
coverage: Simplify the coverageinfo query to a single pass
1 parent 3f54946 commit f191b1c

File tree

1 file changed

+13
-30
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+13
-30
lines changed

compiler/rustc_mir_transform/src/coverage/query.rs

+13-30
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,12 @@ pub(crate) fn provide(providers: &mut Providers) {
2929
/// calls may not work; but computing the number of counters or expressions by adding `1` to the
3030
/// highest ID (for a given instrumented function) is valid.
3131
///
32-
/// This visitor runs twice, first with `add_missing_operands` set to `false`, to find the maximum
33-
/// counter ID and maximum expression ID based on their enum variant `id` fields; then, as a
34-
/// safeguard, with `add_missing_operands` set to `true`, to find any other counter or expression
35-
/// IDs referenced by expression operands, if not already seen.
36-
///
37-
/// Ideally, each operand ID in a MIR `CoverageKind::Expression` will have a separate MIR `Coverage`
38-
/// statement for the `Counter` or `Expression` with the referenced ID. but since current or future
39-
/// MIR optimizations can theoretically optimize out segments of a MIR, it may not be possible to
40-
/// guarantee this, so the second pass ensures the `CoverageInfo` counts include all referenced IDs.
32+
/// It's possible for a coverage expression to remain in MIR while one or both of its operands
33+
/// have been optimized away. To avoid problems in codegen, we include those operands' IDs when
34+
/// determining the maximum counter/expression ID, even if the underlying counter/expression is
35+
/// no longer present.
4136
struct CoverageVisitor {
4237
info: CoverageInfo,
43-
add_missing_operands: bool,
4438
}
4539

4640
impl CoverageVisitor {
@@ -73,35 +67,24 @@ impl CoverageVisitor {
7367
}
7468

7569
fn visit_coverage(&mut self, coverage: &Coverage) {
76-
if self.add_missing_operands {
77-
match coverage.kind {
78-
CoverageKind::Expression { lhs, rhs, .. } => {
79-
self.update_from_expression_operand(lhs);
80-
self.update_from_expression_operand(rhs);
81-
}
82-
_ => {}
83-
}
84-
} else {
85-
match coverage.kind {
86-
CoverageKind::Counter { id, .. } => self.update_num_counters(id),
87-
CoverageKind::Expression { id, .. } => self.update_num_expressions(id),
88-
_ => {}
70+
match coverage.kind {
71+
CoverageKind::Counter { id, .. } => self.update_num_counters(id),
72+
CoverageKind::Expression { id, lhs, rhs, .. } => {
73+
self.update_num_expressions(id);
74+
self.update_from_expression_operand(lhs);
75+
self.update_from_expression_operand(rhs);
8976
}
77+
CoverageKind::Unreachable => {}
9078
}
9179
}
9280
}
9381

9482
fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) -> CoverageInfo {
9583
let mir_body = tcx.instance_mir(instance_def);
9684

97-
let mut coverage_visitor = CoverageVisitor {
98-
info: CoverageInfo { num_counters: 0, num_expressions: 0 },
99-
add_missing_operands: false,
100-
};
101-
102-
coverage_visitor.visit_body(mir_body);
85+
let mut coverage_visitor =
86+
CoverageVisitor { info: CoverageInfo { num_counters: 0, num_expressions: 0 } };
10387

104-
coverage_visitor.add_missing_operands = true;
10588
coverage_visitor.visit_body(mir_body);
10689

10790
coverage_visitor.info

0 commit comments

Comments
 (0)