Skip to content

Commit 7418d2e

Browse files
committed
coverage: Simplify grouping of mappings by file
This removes an ad-hoc implementation of `group_by`.
1 parent a7bdfd4 commit 7418d2e

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,34 @@ fn encode_mappings_for_function(
165165

166166
let mut virtual_file_mapping = Vec::new();
167167
let mut mapping_regions = Vec::with_capacity(counter_regions.len());
168-
let mut current_file_name = None;
169-
let mut current_file_id = 0;
170-
171-
// Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted
172-
// by filename and position. Capture any new files to compute the `CounterMappingRegion`s
173-
// `file_id` (indexing files referenced by the current function), and construct the
174-
// function-specific `virtual_file_mapping` from `file_id` to its index in the module's
175-
// `filenames` array.
168+
169+
// Sort the list of (counter, region) mapping pairs by region, so that they
170+
// can be grouped by filename. Prepare file IDs for each filename, and
171+
// prepare the mapping data so that we can pass it through FFI to LLVM.
176172
counter_regions.sort_by_key(|(_counter, region)| *region);
177-
for (counter, region) in counter_regions {
178-
let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region;
179-
let same_file = current_file_name.is_some_and(|p| p == file_name);
180-
if !same_file {
181-
if current_file_name.is_some() {
182-
current_file_id += 1;
183-
}
184-
current_file_name = Some(file_name);
185-
debug!(" file_id: {} = '{:?}'", current_file_id, file_name);
186-
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
187-
virtual_file_mapping.push(global_file_id);
188-
}
189-
{
190-
debug!("Adding counter {:?} to map for {:?}", counter, region);
173+
for counter_regions_for_file in
174+
counter_regions.group_by(|(_, a), (_, b)| a.file_name == b.file_name)
175+
{
176+
// Look up (or allocate) the global file ID for this filename.
177+
let file_name = counter_regions_for_file[0].1.file_name;
178+
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
179+
180+
// Associate that global file ID with a local file ID for this function.
181+
let local_file_id: u32 = virtual_file_mapping.len().try_into().unwrap_or_else(|_| {
182+
bug!("overflow in local file ID");
183+
});
184+
virtual_file_mapping.push(global_file_id);
185+
debug!(" file id: local {local_file_id} => global {global_file_id} = '{file_name:?}'");
186+
187+
// For each counter/region pair in this function+file, convert it to a
188+
// form suitable for FFI.
189+
for &(counter, region) in counter_regions_for_file {
190+
let CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = *region;
191+
192+
debug!("Adding counter {counter:?} to map for {region:?}");
191193
mapping_regions.push(CounterMappingRegion::code_region(
192194
counter,
193-
current_file_id,
195+
local_file_id,
194196
start_line,
195197
start_col,
196198
end_line,

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(iter_intersperse)]
1111
#![feature(let_chains)]
1212
#![feature(never_type)]
13+
#![feature(slice_group_by)]
1314
#![feature(impl_trait_in_assoc_type)]
1415
#![recursion_limit = "256"]
1516
#![allow(rustc::potential_query_instability)]

0 commit comments

Comments
 (0)