Skip to content

Commit f824c15

Browse files
committed
coverage: Build the global file table on the fly
1 parent ec5b21c commit f824c15

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+18-31
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use std::iter;
22

3-
use itertools::Itertools as _;
43
use rustc_abi::Align;
54
use rustc_codegen_ssa::traits::{
65
BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods,
76
};
87
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
98
use rustc_hir::def_id::{DefId, LocalDefId};
109
use rustc_index::IndexVec;
10+
use rustc_middle::mir;
1111
use rustc_middle::ty::{self, TyCtxt};
12-
use rustc_middle::{bug, mir};
1312
use rustc_session::RemapFileNameExt;
1413
use rustc_session::config::RemapPathScopeComponents;
1514
use rustc_span::def_id::DefIdSet;
@@ -67,25 +66,12 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
6766
return;
6867
}
6968

70-
let all_file_names = function_coverage_map
71-
.iter()
72-
.map(|(_, fn_cov)| fn_cov.function_coverage_info.body_span)
73-
.map(|span| span_file_name(tcx, span));
74-
let global_file_table = GlobalFileTable::new(all_file_names);
75-
76-
// Encode all filenames referenced by coverage mappings in this CGU.
77-
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
78-
// The `llvm-cov` tool uses this hash to associate each covfun record with
79-
// its corresponding filenames table, since the final binary will typically
80-
// contain multiple covmap records from different compilation units.
81-
let filenames_hash = llvm_cov::hash_bytes(&filenames_buffer);
82-
83-
let mut unused_function_names = Vec::new();
69+
let mut global_file_table = GlobalFileTable::new();
8470

8571
let covfun_records = function_coverage_map
8672
.into_iter()
8773
.filter_map(|(instance, function_coverage)| {
88-
prepare_covfun_record(tcx, &global_file_table, instance, &function_coverage)
74+
prepare_covfun_record(tcx, &mut global_file_table, instance, &function_coverage)
8975
})
9076
.collect::<Vec<_>>();
9177

@@ -98,6 +84,15 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
9884
return;
9985
}
10086

87+
// Encode all filenames referenced by coverage mappings in this CGU.
88+
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
89+
// The `llvm-cov` tool uses this hash to associate each covfun record with
90+
// its corresponding filenames table, since the final binary will typically
91+
// contain multiple covmap records from different compilation units.
92+
let filenames_hash = llvm_cov::hash_bytes(&filenames_buffer);
93+
94+
let mut unused_function_names = vec![];
95+
10196
for covfun in &covfun_records {
10297
unused_function_names.extend(covfun.mangled_function_name_if_unused());
10398

@@ -137,22 +132,14 @@ struct GlobalFileTable {
137132
}
138133

139134
impl GlobalFileTable {
140-
fn new(all_file_names: impl IntoIterator<Item = Symbol>) -> Self {
141-
// Collect all of the filenames into a set. Filenames usually come in
142-
// contiguous runs, so we can dedup adjacent ones to save work.
143-
let mut raw_file_table = all_file_names.into_iter().dedup().collect::<FxIndexSet<Symbol>>();
144-
145-
// Sort the file table by its actual string values, not the arbitrary
146-
// ordering of its symbols.
147-
raw_file_table.sort_unstable_by(|a, b| a.as_str().cmp(b.as_str()));
148-
149-
Self { raw_file_table }
135+
fn new() -> Self {
136+
Self { raw_file_table: FxIndexSet::default() }
150137
}
151138

152-
fn global_file_id_for_file_name(&self, file_name: Symbol) -> GlobalFileId {
153-
let raw_id = self.raw_file_table.get_index_of(&file_name).unwrap_or_else(|| {
154-
bug!("file name not found in prepared global file table: {file_name}");
155-
});
139+
fn global_file_id_for_file_name(&mut self, file_name: Symbol) -> GlobalFileId {
140+
// Ensure the given file has a table entry, and get its index.
141+
self.raw_file_table.insert(file_name);
142+
let raw_id = self.raw_file_table.get_index_of(&file_name).unwrap();
156143
// The raw file table doesn't include an entry for the working dir
157144
// (which has ID 0), so add 1 to get the correct ID.
158145
GlobalFileId::from_usize(raw_id + 1)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'tcx> CovfunRecord<'tcx> {
4545

4646
pub(crate) fn prepare_covfun_record<'tcx>(
4747
tcx: TyCtxt<'tcx>,
48-
global_file_table: &GlobalFileTable,
48+
global_file_table: &mut GlobalFileTable,
4949
instance: Instance<'tcx>,
5050
function_coverage: &FunctionCoverage<'tcx>,
5151
) -> Option<CovfunRecord<'tcx>> {
@@ -75,7 +75,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
7575
/// Populates the mapping region tables in the current function's covfun record.
7676
fn fill_region_tables<'tcx>(
7777
tcx: TyCtxt<'tcx>,
78-
global_file_table: &GlobalFileTable,
78+
global_file_table: &mut GlobalFileTable,
7979
function_coverage: &FunctionCoverage<'tcx>,
8080
covfun: &mut CovfunRecord<'tcx>,
8181
) {

tests/coverage/unused_mod.cov-map

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
Function name: unused_mod::main
2-
Raw bytes (9): 0x[01, 02, 00, 01, 01, 04, 01, 02, 02]
2+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 04, 01, 02, 02]
33
Number of files: 1
4-
- file 0 => global file 2
4+
- file 0 => global file 1
55
Number of expressions: 0
66
Number of file 0 mappings: 1
77
- Code(Counter(0)) at (prev + 4, 1) to (start + 2, 2)
88
Highest counter ID seen: c0
99

1010
Function name: unused_mod::unused_module::never_called_function (unused)
11-
Raw bytes (9): 0x[01, 01, 00, 01, 00, 02, 01, 02, 02]
11+
Raw bytes (9): 0x[01, 02, 00, 01, 00, 02, 01, 02, 02]
1212
Number of files: 1
13-
- file 0 => global file 1
13+
- file 0 => global file 2
1414
Number of expressions: 0
1515
Number of file 0 mappings: 1
1616
- Code(Zero) at (prev + 2, 1) to (start + 2, 2)

0 commit comments

Comments
 (0)