Skip to content

Commit c337f2a

Browse files
committed
coverage: Pull region conversion out of map_data.rs
1 parent d4f1062 commit c337f2a

File tree

2 files changed

+14
-37
lines changed

2 files changed

+14
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use rustc_middle::mir::coverage::{
2-
CovTerm, CoverageIdsInfo, FunctionCoverageInfo, Mapping, MappingKind, SourceRegion,
3-
};
1+
use rustc_middle::mir::coverage::{CoverageIdsInfo, FunctionCoverageInfo};
42

53
pub(crate) struct FunctionCoverage<'tcx> {
64
pub(crate) function_coverage_info: &'tcx FunctionCoverageInfo,
@@ -30,25 +28,4 @@ impl<'tcx> FunctionCoverage<'tcx> {
3028
pub(crate) fn source_hash(&self) -> u64 {
3129
if self.is_used() { self.function_coverage_info.function_source_hash } else { 0 }
3230
}
33-
34-
/// Converts this function's coverage mappings into an intermediate form
35-
/// that will be used by `mapgen` when preparing for FFI.
36-
pub(crate) fn counter_regions(
37-
&self,
38-
) -> impl Iterator<Item = (MappingKind, &SourceRegion)> + ExactSizeIterator {
39-
self.function_coverage_info.mappings.iter().map(move |mapping| {
40-
let Mapping { kind, source_region } = mapping;
41-
let kind =
42-
kind.map_terms(|term| if self.is_zero_term(term) { CovTerm::Zero } else { term });
43-
(kind, source_region)
44-
})
45-
}
46-
47-
fn is_zero_term(&self, term: CovTerm) -> bool {
48-
match self.ids_info {
49-
Some(ids_info) => ids_info.is_zero_term(term),
50-
// This function is unused, so all coverage counters/expressions are zero.
51-
None => true,
52-
}
53-
}
5431
}

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_codegen_ssa::traits::{
1212
};
1313
use rustc_middle::bug;
1414
use rustc_middle::mir::coverage::{
15-
CoverageIdsInfo, Expression, FunctionCoverageInfo, MappingKind, Op,
15+
CovTerm, CoverageIdsInfo, Expression, FunctionCoverageInfo, Mapping, MappingKind, Op,
1616
};
1717
use rustc_middle::ty::{Instance, TyCtxt};
1818
use rustc_target::spec::HasTargetSpec;
@@ -65,7 +65,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
6565
regions: ffi::Regions::default(),
6666
};
6767

68-
fill_region_tables(tcx, global_file_table, function_coverage, &mut covfun);
68+
fill_region_tables(tcx, global_file_table, fn_cov_info, ids_info, &mut covfun);
6969

7070
if covfun.regions.has_no_regions() {
7171
if covfun.is_used {
@@ -117,16 +117,12 @@ fn prepare_expressions(
117117
fn fill_region_tables<'tcx>(
118118
tcx: TyCtxt<'tcx>,
119119
global_file_table: &mut GlobalFileTable,
120-
function_coverage: &FunctionCoverage<'tcx>,
120+
fn_cov_info: &'tcx FunctionCoverageInfo,
121+
ids_info: &'tcx CoverageIdsInfo,
121122
covfun: &mut CovfunRecord<'tcx>,
122123
) {
123-
let counter_regions = function_coverage.counter_regions();
124-
if counter_regions.is_empty() {
125-
return;
126-
}
127-
128124
// Currently a function's mappings must all be in the same file as its body span.
129-
let file_name = span_file_name(tcx, function_coverage.function_coverage_info.body_span);
125+
let file_name = span_file_name(tcx, fn_cov_info.body_span);
130126

131127
// Look up the global file ID for that filename.
132128
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
@@ -140,10 +136,14 @@ fn fill_region_tables<'tcx>(
140136

141137
// For each counter/region pair in this function+file, convert it to a
142138
// form suitable for FFI.
143-
for (mapping_kind, region) in counter_regions {
144-
debug!("Adding counter {mapping_kind:?} to map for {region:?}");
145-
let span = ffi::CoverageSpan::from_source_region(local_file_id, region);
146-
match mapping_kind {
139+
let is_zero_term = |term| !covfun.is_used || ids_info.is_zero_term(term);
140+
for Mapping { kind, ref source_region } in &fn_cov_info.mappings {
141+
// If the mapping refers to counters/expressions that were removed by
142+
// MIR opts, replace those occurrences with zero.
143+
let kind = kind.map_terms(|term| if is_zero_term(term) { CovTerm::Zero } else { term });
144+
145+
let span = ffi::CoverageSpan::from_source_region(local_file_id, source_region);
146+
match kind {
147147
MappingKind::Code(term) => {
148148
code_regions.push(ffi::CodeRegion { span, counter: ffi::Counter::from_term(term) });
149149
}

0 commit comments

Comments
 (0)