Skip to content

Commit 72f4c78

Browse files
committed
coverage: Don't store function_source_hash in BcbCounter::Counter
This shows one small benefit of separating `BcbCounter` from `CoverageKind`. The function source hash will be the same for all counters within a function, so instead of passing it through `CoverageCounters` and storing it in every counter, we can just supply it during the final conversion to `CoverageKind`.
1 parent fbab055 commit 72f4c78

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

compiler/rustc_mir_transform/src/coverage/counters.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::fmt::{self, Debug};
2020
/// BCB node or BCB edge.
2121
#[derive(Clone)]
2222
pub(super) enum BcbCounter {
23-
Counter { function_source_hash: u64, id: CounterId },
23+
Counter { id: CounterId },
2424
Expression { id: ExpressionId, lhs: Operand, op: Op, rhs: Operand },
2525
}
2626

@@ -59,7 +59,6 @@ impl Debug for BcbCounter {
5959
/// Generates and stores coverage counter and coverage expression information
6060
/// associated with nodes/edges in the BCB graph.
6161
pub(super) struct CoverageCounters {
62-
function_source_hash: u64,
6362
next_counter_id: CounterId,
6463
next_expression_id: ExpressionId,
6564

@@ -81,11 +80,10 @@ pub(super) struct CoverageCounters {
8180
}
8281

8382
impl CoverageCounters {
84-
pub(super) fn new(function_source_hash: u64, basic_coverage_blocks: &CoverageGraph) -> Self {
83+
pub(super) fn new(basic_coverage_blocks: &CoverageGraph) -> Self {
8584
let num_bcbs = basic_coverage_blocks.num_nodes();
8685

8786
Self {
88-
function_source_hash,
8987
next_counter_id: CounterId::START,
9088
next_expression_id: ExpressionId::START,
9189

@@ -119,10 +117,7 @@ impl CoverageCounters {
119117
where
120118
F: Fn() -> Option<String>,
121119
{
122-
let counter = BcbCounter::Counter {
123-
function_source_hash: self.function_source_hash,
124-
id: self.next_counter(),
125-
};
120+
let counter = BcbCounter::Counter { id: self.next_counter() };
126121
if self.debug_counters.is_enabled() {
127122
self.debug_counters.add_counter(&counter, (debug_block_label_fn)());
128123
}

compiler/rustc_mir_transform/src/coverage/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ struct Instrumentor<'a, 'tcx> {
106106
source_file: Lrc<SourceFile>,
107107
fn_sig_span: Span,
108108
body_span: Span,
109+
function_source_hash: u64,
109110
basic_coverage_blocks: CoverageGraph,
110111
coverage_counters: CoverageCounters,
111112
}
@@ -137,7 +138,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
137138

138139
let function_source_hash = hash_mir_source(tcx, hir_body);
139140
let basic_coverage_blocks = CoverageGraph::from_mir(mir_body);
140-
let coverage_counters = CoverageCounters::new(function_source_hash, &basic_coverage_blocks);
141+
let coverage_counters = CoverageCounters::new(&basic_coverage_blocks);
141142

142143
Self {
143144
pass_name,
@@ -146,6 +147,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
146147
source_file,
147148
fn_sig_span,
148149
body_span,
150+
function_source_hash,
149151
basic_coverage_blocks,
150152
coverage_counters,
151153
}
@@ -435,8 +437,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
435437

436438
fn make_mir_coverage_kind(&self, counter_kind: &BcbCounter) -> CoverageKind {
437439
match *counter_kind {
438-
BcbCounter::Counter { function_source_hash, id } => {
439-
CoverageKind::Counter { function_source_hash, id }
440+
BcbCounter::Counter { id } => {
441+
CoverageKind::Counter { function_source_hash: self.function_source_hash, id }
440442
}
441443
BcbCounter::Expression { id, lhs, op, rhs } => {
442444
CoverageKind::Expression { id, lhs, op, rhs }

compiler/rustc_mir_transform/src/coverage/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ fn test_make_bcb_counters() {
674674
));
675675
}
676676
}
677-
let mut coverage_counters = counters::CoverageCounters::new(0, &basic_coverage_blocks);
677+
let mut coverage_counters = counters::CoverageCounters::new(&basic_coverage_blocks);
678678
coverage_counters
679679
.make_bcb_counters(&mut basic_coverage_blocks, &coverage_spans)
680680
.expect("should be Ok");

0 commit comments

Comments
 (0)