Skip to content

Commit 2748009

Browse files
committed
coverage: Identify source files by ID, not by interned filename
1 parent b9fb1a6 commit 2748009

File tree

1 file changed

+42
-43
lines changed
  • compiler/rustc_codegen_llvm/src/coverageinfo

1 file changed

+42
-43
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+42-43
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
mod spans;
22

33
use std::ffi::CString;
4-
use std::iter;
4+
use std::sync::Arc;
55

66
use itertools::Itertools as _;
77
use rustc_abi::Align;
88
use rustc_codegen_ssa::traits::{
99
BaseTypeCodegenMethods, ConstCodegenMethods, StaticCodegenMethods,
1010
};
11-
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
11+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1212
use rustc_hir::def_id::{DefId, LocalDefId};
1313
use rustc_index::IndexVec;
1414
use rustc_middle::mir::coverage::MappingKind;
@@ -17,7 +17,7 @@ use rustc_middle::{bug, mir};
1717
use rustc_session::RemapFileNameExt;
1818
use rustc_session::config::RemapPathScopeComponents;
1919
use rustc_span::def_id::DefIdSet;
20-
use rustc_span::{Span, Symbol};
20+
use rustc_span::{SourceFile, StableSourceFileId};
2121
use rustc_target::spec::HasTargetSpec;
2222
use tracing::debug;
2323

@@ -74,11 +74,11 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
7474
.map(|(instance, function_coverage)| (instance, function_coverage.into_finished()))
7575
.collect::<Vec<_>>();
7676

77-
let all_file_names = function_coverage_entries
77+
let all_files = function_coverage_entries
7878
.iter()
7979
.map(|(_, fn_cov)| fn_cov.function_coverage_info.body_span)
80-
.map(|span| span_file_name(tcx, span));
81-
let global_file_table = GlobalFileTable::new(all_file_names);
80+
.map(|span| tcx.sess.source_map().lookup_source_file(span.lo()));
81+
let global_file_table = GlobalFileTable::new(all_files);
8282

8383
// Encode all filenames referenced by coverage mappings in this CGU.
8484
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
@@ -143,54 +143,62 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
143143
}
144144
}
145145

146-
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
146+
/// Maps "global" (per-CGU) file ID numbers to their underlying source files.
147147
struct GlobalFileTable {
148-
/// This "raw" table doesn't include the working dir, so a filename's
148+
/// This "raw" table doesn't include the working dir, so a file's
149149
/// global ID is its index in this set **plus one**.
150-
raw_file_table: FxIndexSet<Symbol>,
150+
raw_file_table: FxIndexMap<StableSourceFileId, Arc<SourceFile>>,
151151
}
152152

153153
impl GlobalFileTable {
154-
fn new(all_file_names: impl IntoIterator<Item = Symbol>) -> Self {
155-
// Collect all of the filenames into a set. Filenames usually come in
156-
// contiguous runs, so we can dedup adjacent ones to save work.
157-
let mut raw_file_table = all_file_names.into_iter().dedup().collect::<FxIndexSet<Symbol>>();
154+
fn new(all_files: impl IntoIterator<Item = Arc<SourceFile>>) -> Self {
155+
// Collect all of the files into a set. Files usually come in contiguous
156+
// runs, so we can dedup adjacent ones to save work.
157+
let mut raw_file_table = all_files
158+
.into_iter()
159+
.dedup_by(|a, b| a.stable_id == b.stable_id)
160+
.map(|f| (f.stable_id, f))
161+
.collect::<FxIndexMap<StableSourceFileId, Arc<SourceFile>>>();
158162

159-
// Sort the file table by its actual string values, not the arbitrary
160-
// ordering of its symbols.
161-
raw_file_table.sort_unstable_by(|a, b| a.as_str().cmp(b.as_str()));
163+
// Sort the file table by its underlying filenames.
164+
raw_file_table.sort_unstable_by(|_, a, _, b| {
165+
Ord::cmp(&a.name, &b.name).then_with(|| Ord::cmp(&a.stable_id, &b.stable_id))
166+
});
162167

163168
Self { raw_file_table }
164169
}
165170

166-
fn global_file_id_for_file_name(&self, file_name: Symbol) -> GlobalFileId {
167-
let raw_id = self.raw_file_table.get_index_of(&file_name).unwrap_or_else(|| {
168-
bug!("file name not found in prepared global file table: {file_name}");
171+
fn global_file_id_for_file(&self, file: &SourceFile) -> GlobalFileId {
172+
let raw_id = self.raw_file_table.get_index_of(&file.stable_id).unwrap_or_else(|| {
173+
bug!("file not found in prepared global file table: {:?}", file.name);
169174
});
170175
// The raw file table doesn't include an entry for the working dir
171176
// (which has ID 0), so add 1 to get the correct ID.
172177
GlobalFileId::from_usize(raw_id + 1)
173178
}
174179

175180
fn make_filenames_buffer(&self, tcx: TyCtxt<'_>) -> Vec<u8> {
181+
let mut table = Vec::with_capacity(self.raw_file_table.len() + 1);
182+
176183
// LLVM Coverage Mapping Format version 6 (zero-based encoded as 5)
177184
// requires setting the first filename to the compilation directory.
178185
// Since rustc generates coverage maps with relative paths, the
179186
// compilation directory can be combined with the relative paths
180187
// to get absolute paths, if needed.
181-
use rustc_session::RemapFileNameExt;
182-
use rustc_session::config::RemapPathScopeComponents;
183-
let working_dir: &str = &tcx
184-
.sess
185-
.opts
186-
.working_dir
187-
.for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
188-
.to_string_lossy();
189-
190-
// Insert the working dir at index 0, before the other filenames.
191-
let filenames =
192-
iter::once(working_dir).chain(self.raw_file_table.iter().map(Symbol::as_str));
193-
llvm_cov::write_filenames_to_buffer(filenames)
188+
table.push(
189+
tcx.sess
190+
.opts
191+
.working_dir
192+
.for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
193+
.to_string_lossy(),
194+
);
195+
196+
// Add the regular entries after the base directory.
197+
table.extend(self.raw_file_table.values().map(|file| {
198+
file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy()
199+
}));
200+
201+
llvm_cov::write_filenames_to_buffer(table.iter().map(|f| f.as_ref()))
194202
}
195203
}
196204

@@ -229,13 +237,6 @@ impl VirtualFileMapping {
229237
}
230238
}
231239

232-
fn span_file_name(tcx: TyCtxt<'_>, span: Span) -> Symbol {
233-
let source_file = tcx.sess.source_map().lookup_source_file(span.lo());
234-
let name =
235-
source_file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy();
236-
Symbol::intern(&name)
237-
}
238-
239240
/// Using the expressions and counter regions collected for a single function,
240241
/// generate the variable-sized payload of its corresponding `__llvm_covfun`
241242
/// entry. The payload is returned as a vector of bytes.
@@ -262,16 +263,14 @@ fn encode_mappings_for_function(
262263
let mut mcdc_decision_regions = vec![];
263264

264265
// Currently a function's mappings must all be in the same file as its body span.
265-
let file_name = span_file_name(tcx, fn_cov_info.body_span);
266266
let source_map = tcx.sess.source_map();
267267
let source_file = source_map.lookup_source_file(fn_cov_info.body_span.lo());
268268

269-
// Look up the global file ID for that filename.
270-
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
269+
// Look up the global file ID for that file.
270+
let global_file_id = global_file_table.global_file_id_for_file(&source_file);
271271

272272
// Associate that global file ID with a local file ID for this function.
273273
let local_file_id = virtual_file_mapping.local_id_for_global(global_file_id);
274-
debug!(" file id: {local_file_id:?} => {global_file_id:?} = '{file_name:?}'");
275274

276275
let make_cov_span = |span| {
277276
spans::make_coverage_span(local_file_id, source_map, fn_cov_info, &source_file, span)

0 commit comments

Comments
 (0)