Skip to content

Commit 670706f

Browse files
committed
coverage: Encapsulate local-to-global file mappings
1 parent 80f1bcd commit 670706f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
147147
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
148148
}
149149

150+
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
150151
struct GlobalFileTable {
151152
/// This "raw" table doesn't include the working dir, so a filename's
152153
/// 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

Lines changed: 1 addition & 0 deletions
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)