Skip to content

Commit c5d04d5

Browse files
committed
coverage: Encapsulate local-to-global file mappings
1 parent 290b275 commit c5d04d5

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::llvm;
66

77
use itertools::Itertools as _;
88
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
9-
use rustc_data_structures::fx::FxIndexSet;
9+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
1010
use rustc_hir::def::DefKind;
1111
use rustc_hir::def_id::DefId;
1212
use rustc_index::IndexVec;
@@ -146,6 +146,7 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
146146
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
147147
}
148148

149+
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
149150
struct GlobalFileTable {
150151
/// This "raw" table doesn't include the working dir, so a filename's
151152
/// global ID is its index in this set **plus one**.
@@ -206,6 +207,30 @@ impl GlobalFileTable {
206207
}
207208
}
208209

210+
rustc_index::newtype_index! {
211+
// Tell the newtype macro to not generate `Encode`/`Decode` impls.
212+
#[custom_encodable]
213+
#[max = 0xFFFF_FFFF]
214+
struct LocalFileId {}
215+
}
216+
217+
/// Holds a mapping from "local" (per-function) file IDs to "global" (per-CGU)
218+
/// file IDs.
219+
#[derive(Default)]
220+
struct VirtualFileMapping {
221+
local_to_global: IndexVec<LocalFileId, u32>,
222+
}
223+
224+
impl VirtualFileMapping {
225+
fn push_global_id(&mut self, global_file_id: u32) -> LocalFileId {
226+
self.local_to_global.push(global_file_id)
227+
}
228+
229+
fn into_vec(self) -> Vec<u32> {
230+
self.local_to_global.raw
231+
}
232+
}
233+
209234
/// Using the expressions and counter regions collected for a single function,
210235
/// generate the variable-sized payload of its corresponding `__llvm_covfun`
211236
/// entry. The payload is returned as a vector of bytes.
@@ -222,7 +247,7 @@ fn encode_mappings_for_function(
222247

223248
let expressions = function_coverage.counter_expressions().collect::<Vec<_>>();
224249

225-
let mut virtual_file_mapping = IndexVec::<u32, u32>::new();
250+
let mut virtual_file_mapping = VirtualFileMapping::default();
226251
let mut mapping_regions = Vec::with_capacity(counter_regions.len());
227252

228253
// Sort and group the list of (counter, region) mapping pairs by filename.
@@ -238,8 +263,8 @@ fn encode_mappings_for_function(
238263
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
239264

240265
// Associate that global file ID with a local file ID for this function.
241-
let local_file_id: u32 = virtual_file_mapping.push(global_file_id);
242-
debug!(" file id: local {local_file_id} => global {global_file_id} = '{file_name:?}'");
266+
let local_file_id = virtual_file_mapping.push_global_id(global_file_id);
267+
debug!(" file id: {local_file_id:?} => global {global_file_id} = '{file_name:?}'");
243268

244269
// For each counter/region pair in this function+file, convert it to a
245270
// form suitable for FFI.
@@ -249,7 +274,7 @@ fn encode_mappings_for_function(
249274
debug!("Adding counter {counter:?} to map for {region:?}");
250275
mapping_regions.push(CounterMappingRegion::code_region(
251276
counter,
252-
local_file_id,
277+
local_file_id.as_u32(),
253278
start_line,
254279
start_col,
255280
end_line,
@@ -261,7 +286,7 @@ fn encode_mappings_for_function(
261286
// Encode the function's coverage mappings into a buffer.
262287
llvm::build_byte_buffer(|buffer| {
263288
coverageinfo::write_mapping_to_buffer(
264-
virtual_file_mapping.raw,
289+
virtual_file_mapping.into_vec(),
265290
expressions,
266291
mapping_regions,
267292
buffer,

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(hash_raw_entry)]
1313
#![feature(iter_intersperse)]
1414
#![feature(let_chains)]
15+
#![feature(min_specialization)]
1516
#![feature(never_type)]
1617
#![feature(slice_group_by)]
1718
#![feature(impl_trait_in_assoc_type)]

0 commit comments

Comments
 (0)