Skip to content

Commit c46abe4

Browse files
committed
coverage: Emit the filenames section before encoding per-function mappings
Most coverage metadata is encoded into two sections in the final executable. The `__llvm_covmap` section mostly just contains a list of filenames, while the `__llvm_covfun` section contains encoded coverage maps for each instrumented function. The catch is that each per-function record also needs to contain a hash of the filenames list that it refers to. Historically this was handled by assembling most of the per-function data into a temporary list, then assembling the filenames buffer, then using the filenames hash to emit the per-function data, and then finally emitting the filenames table itself. However, now that we build the filenames table up-front (via a separate traversal of the per-function data), we can hash and emit that part first, and then emit each of the per-function records immediately after building. This removes the awkwardness of having to temporarily store nearly-complete per-function records.
1 parent de4cfbc commit c46abe4

File tree

1 file changed

+15
-21
lines changed
  • compiler/rustc_codegen_llvm/src/coverageinfo

1 file changed

+15
-21
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+15-21
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,22 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
6767
function_coverage_entries.iter().flat_map(|(_, fn_cov)| fn_cov.all_file_names());
6868
let global_file_table = GlobalFileTable::new(all_file_names);
6969

70+
// Encode all filenames referenced by coverage mappings in this CGU.
71+
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
72+
73+
let filenames_size = filenames_buffer.len();
74+
let filenames_val = cx.const_bytes(&filenames_buffer);
75+
let filenames_ref = coverageinfo::hash_bytes(&filenames_buffer);
76+
77+
// Generate the coverage map header, which contains the filenames used by
78+
// this CGU's coverage mappings, and store it in a well-known global.
79+
let cov_data_val = generate_coverage_map(cx, version, filenames_size, filenames_val);
80+
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
81+
82+
let mut unused_function_names = Vec::new();
83+
let covfun_section_name = coverageinfo::covfun_section_name(cx);
84+
7085
// Encode coverage mappings and generate function records
71-
let mut function_data = Vec::new();
7286
for (instance, function_coverage) in function_coverage_entries {
7387
debug!("Generate function coverage for {}, {:?}", cx.codegen_unit.name(), instance);
7488

@@ -91,23 +105,6 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
91105
}
92106
}
93107

94-
function_data.push((mangled_function_name, source_hash, is_used, coverage_mapping_buffer));
95-
}
96-
97-
// Encode all filenames referenced by counters/expressions in this module
98-
let filenames_buffer = global_file_table.make_filenames_buffer(tcx);
99-
100-
let filenames_size = filenames_buffer.len();
101-
let filenames_val = cx.const_bytes(&filenames_buffer);
102-
let filenames_ref = coverageinfo::hash_bytes(&filenames_buffer);
103-
104-
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
105-
let cov_data_val = generate_coverage_map(cx, version, filenames_size, filenames_val);
106-
107-
let mut unused_function_names = Vec::new();
108-
109-
let covfun_section_name = coverageinfo::covfun_section_name(cx);
110-
for (mangled_function_name, source_hash, is_used, coverage_mapping_buffer) in function_data {
111108
if !is_used {
112109
unused_function_names.push(mangled_function_name);
113110
}
@@ -141,9 +138,6 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
141138
llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
142139
llvm::set_initializer(array, initializer);
143140
}
144-
145-
// Save the coverage data value to LLVM IR
146-
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
147141
}
148142

149143
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.

0 commit comments

Comments
 (0)