1
Fork 0

Make coverage counter IDs count up from 0, not 1

Operand types are now tracked explicitly, so there is no need to reserve ID 0
for the special always-zero counter.

As part of the renumbering, this change fixes an off-by-one error in the way
counters were counted by the `coverageinfo` query. As a result, functions
should now have exactly the number of counters they actually need, instead of
always having an extra counter that is never used.
This commit is contained in:
Zalathar 2023-06-29 12:36:19 +10:00
parent f103db894f
commit 3920e07f0b
12 changed files with 43 additions and 61 deletions

View file

@ -16,7 +16,7 @@ use rustc_middle::mir::coverage::*;
/// `Coverage` statements.
pub(super) struct CoverageCounters {
function_source_hash: u64,
next_counter_id: u32,
next_counter_id: CounterId,
next_expression_id: ExpressionId,
pub debug_counters: DebugCounters,
}
@ -25,7 +25,7 @@ impl CoverageCounters {
pub fn new(function_source_hash: u64) -> Self {
Self {
function_source_hash,
next_counter_id: CounterValueReference::START.as_u32(),
next_counter_id: CounterId::START,
next_expression_id: ExpressionId::START,
debug_counters: DebugCounters::new(),
}
@ -93,10 +93,10 @@ impl CoverageCounters {
}
/// Counter IDs start from one and go up.
fn next_counter(&mut self) -> CounterValueReference {
fn next_counter(&mut self) -> CounterId {
let next = self.next_counter_id;
self.next_counter_id += 1;
CounterValueReference::from(next)
self.next_counter_id = next.next_id();
next
}
/// Expression IDs start from 0 and go up.

View file

@ -43,11 +43,9 @@ struct CoverageVisitor {
}
impl CoverageVisitor {
/// Updates `num_counters` to the maximum encountered zero-based counter_id plus 1. Note the
/// final computed number of counters should be the number of all `CoverageKind::Counter`
/// statements in the MIR *plus one* for the implicit `ZERO` counter.
/// Updates `num_counters` to the maximum encountered counter ID plus 1.
#[inline(always)]
fn update_num_counters(&mut self, counter_id: CounterValueReference) {
fn update_num_counters(&mut self, counter_id: CounterId) {
let counter_id = counter_id.as_u32();
self.info.num_counters = std::cmp::max(self.info.num_counters, counter_id + 1);
}
@ -103,8 +101,7 @@ fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) ->
let mir_body = tcx.instance_mir(instance_def);
let mut coverage_visitor = CoverageVisitor {
// num_counters always has at least the `ZERO` counter.
info: CoverageInfo { num_counters: 1, num_expressions: 0 },
info: CoverageInfo { num_counters: 0, num_expressions: 0 },
add_missing_operands: false,
};

View file

@ -683,7 +683,7 @@ fn test_make_bcb_counters() {
let_bcb!(1);
assert_eq!(
1, // coincidentally, bcb1 has a `Counter` with id = 1
0, // bcb1 has a `Counter` with id = 0
match basic_coverage_blocks[bcb1].counter().expect("should have a counter") {
CoverageKind::Counter { id, .. } => id,
_ => panic!("expected a Counter"),
@ -693,7 +693,7 @@ fn test_make_bcb_counters() {
let_bcb!(2);
assert_eq!(
2, // coincidentally, bcb2 has a `Counter` with id = 2
1, // bcb2 has a `Counter` with id = 1
match basic_coverage_blocks[bcb2].counter().expect("should have a counter") {
CoverageKind::Counter { id, .. } => id,
_ => panic!("expected a Counter"),