]>
Commit | Line | Data |
---|---|---|
3dfed10e XL |
1 | use crate::common::CodegenCx; |
2 | use crate::coverageinfo; | |
3 | use crate::llvm; | |
4 | ||
5 | use llvm::coverageinfo::CounterMappingRegion; | |
fc512014 XL |
6 | use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression, FunctionCoverage}; |
7 | use rustc_codegen_ssa::traits::ConstMethods; | |
8 | use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; | |
9 | use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE}; | |
3dfed10e XL |
10 | use rustc_llvm::RustString; |
11 | use rustc_middle::mir::coverage::CodeRegion; | |
fc512014 XL |
12 | use rustc_middle::ty::{Instance, TyCtxt}; |
13 | use rustc_span::Symbol; | |
3dfed10e XL |
14 | |
15 | use std::ffi::CString; | |
16 | ||
17 | use tracing::debug; | |
18 | ||
19 | /// Generates and exports the Coverage Map. | |
20 | /// | |
fc512014 XL |
21 | /// This Coverage Map complies with Coverage Mapping Format version 4 (zero-based encoded as 3), |
22 | /// as defined at [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/11.0-2020-10-12/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format) | |
23 | /// and published in Rust's current (November 2020) fork of LLVM. This version is supported by the | |
3dfed10e XL |
24 | /// LLVM coverage tools (`llvm-profdata` and `llvm-cov`) bundled with Rust's fork of LLVM. |
25 | /// | |
26 | /// Consequently, Rust's bundled version of Clang also generates Coverage Maps compliant with | |
27 | /// version 3. Clang's implementation of Coverage Map generation was referenced when implementing | |
28 | /// this Rust version, and though the format documentation is very explicit and detailed, some | |
29 | /// undocumented details in Clang's implementation (that may or may not be important) were also | |
30 | /// replicated for Rust's Coverage Map. | |
31 | pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) { | |
fc512014 XL |
32 | let tcx = cx.tcx; |
33 | // Ensure LLVM supports Coverage Map Version 4 (encoded as a zero-based value: 3). | |
34 | // If not, the LLVM Version must be less than 11. | |
35 | let version = coverageinfo::mapping_version(); | |
36 | if version != 3 { | |
37 | tcx.sess.fatal("rustc option `-Z instrument-coverage` requires LLVM 11 or higher."); | |
38 | } | |
39 | ||
40 | debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name()); | |
41 | ||
42 | let mut function_coverage_map = match cx.coverage_context() { | |
29967ef6 XL |
43 | Some(ctx) => ctx.take_function_coverage_map(), |
44 | None => return, | |
45 | }; | |
3dfed10e XL |
46 | if function_coverage_map.is_empty() { |
47 | // This module has no functions with coverage instrumentation | |
48 | return; | |
49 | } | |
50 | ||
fc512014 XL |
51 | add_unreachable_coverage(tcx, &mut function_coverage_map); |
52 | ||
3dfed10e XL |
53 | let mut mapgen = CoverageMapGenerator::new(); |
54 | ||
55 | // Encode coverage mappings and generate function records | |
fc512014 XL |
56 | let mut function_data = Vec::new(); |
57 | for (instance, function_coverage) in function_coverage_map { | |
58 | debug!("Generate function coverage for {}, {:?}", cx.codegen_unit.name(), instance); | |
59 | let mangled_function_name = tcx.symbol_name(instance).to_string(); | |
60 | let function_source_hash = function_coverage.source_hash(); | |
61 | let (expressions, counter_regions) = | |
62 | function_coverage.get_expressions_and_counter_regions(); | |
63 | ||
64 | let coverage_mapping_buffer = llvm::build_byte_buffer(|coverage_mapping_buffer| { | |
65 | mapgen.write_coverage_mapping(expressions, counter_regions, coverage_mapping_buffer); | |
66 | }); | |
67 | debug_assert!( | |
68 | coverage_mapping_buffer.len() > 0, | |
69 | "Every `FunctionCoverage` should have at least one counter" | |
70 | ); | |
71 | ||
72 | function_data.push((mangled_function_name, function_source_hash, coverage_mapping_buffer)); | |
73 | } | |
3dfed10e XL |
74 | |
75 | // Encode all filenames referenced by counters/expressions in this module | |
76 | let filenames_buffer = llvm::build_byte_buffer(|filenames_buffer| { | |
77 | coverageinfo::write_filenames_section_to_buffer(&mapgen.filenames, filenames_buffer); | |
78 | }); | |
79 | ||
fc512014 XL |
80 | let filenames_size = filenames_buffer.len(); |
81 | let filenames_val = cx.const_bytes(&filenames_buffer[..]); | |
82 | let filenames_ref = coverageinfo::hash_bytes(filenames_buffer); | |
83 | ||
3dfed10e | 84 | // Generate the LLVM IR representation of the coverage map and store it in a well-known global |
fc512014 XL |
85 | let cov_data_val = mapgen.generate_coverage_map(cx, version, filenames_size, filenames_val); |
86 | ||
87 | for (mangled_function_name, function_source_hash, coverage_mapping_buffer) in function_data { | |
88 | save_function_record( | |
89 | cx, | |
90 | mangled_function_name, | |
91 | function_source_hash, | |
92 | filenames_ref, | |
93 | coverage_mapping_buffer, | |
94 | ); | |
95 | } | |
96 | ||
97 | // Save the coverage data value to LLVM IR | |
98 | coverageinfo::save_cov_data_to_mod(cx, cov_data_val); | |
3dfed10e XL |
99 | } |
100 | ||
101 | struct CoverageMapGenerator { | |
102 | filenames: FxIndexSet<CString>, | |
103 | } | |
104 | ||
105 | impl CoverageMapGenerator { | |
106 | fn new() -> Self { | |
107 | Self { filenames: FxIndexSet::default() } | |
108 | } | |
109 | ||
110 | /// Using the `expressions` and `counter_regions` collected for the current function, generate | |
111 | /// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use | |
112 | /// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into | |
fc512014 XL |
113 | /// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format. |
114 | fn write_coverage_mapping( | |
3dfed10e XL |
115 | &mut self, |
116 | expressions: Vec<CounterExpression>, | |
117 | counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>, | |
fc512014 | 118 | coverage_mapping_buffer: &RustString, |
3dfed10e XL |
119 | ) { |
120 | let mut counter_regions = counter_regions.collect::<Vec<_>>(); | |
121 | if counter_regions.is_empty() { | |
122 | return; | |
123 | } | |
124 | ||
125 | let mut virtual_file_mapping = Vec::new(); | |
126 | let mut mapping_regions = Vec::new(); | |
127 | let mut current_file_name = None; | |
128 | let mut current_file_id = 0; | |
129 | ||
130 | // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted | |
131 | // by filename and position. Capture any new files to compute the `CounterMappingRegion`s | |
132 | // `file_id` (indexing files referenced by the current function), and construct the | |
133 | // function-specific `virtual_file_mapping` from `file_id` to its index in the module's | |
134 | // `filenames` array. | |
135 | counter_regions.sort_unstable_by_key(|(_counter, region)| *region); | |
136 | for (counter, region) in counter_regions { | |
137 | let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region; | |
138 | let same_file = current_file_name.as_ref().map_or(false, |p| *p == file_name); | |
139 | if !same_file { | |
140 | if current_file_name.is_some() { | |
141 | current_file_id += 1; | |
142 | } | |
143 | current_file_name = Some(file_name); | |
144 | let c_filename = CString::new(file_name.to_string()) | |
145 | .expect("null error converting filename to C string"); | |
146 | debug!(" file_id: {} = '{:?}'", current_file_id, c_filename); | |
147 | let (filenames_index, _) = self.filenames.insert_full(c_filename); | |
148 | virtual_file_mapping.push(filenames_index as u32); | |
149 | } | |
29967ef6 | 150 | debug!("Adding counter {:?} to map for {:?}", counter, region); |
3dfed10e XL |
151 | mapping_regions.push(CounterMappingRegion::code_region( |
152 | counter, | |
153 | current_file_id, | |
154 | start_line, | |
155 | start_col, | |
156 | end_line, | |
157 | end_col, | |
158 | )); | |
159 | } | |
160 | ||
161 | // Encode and append the current function's coverage mapping data | |
162 | coverageinfo::write_mapping_to_buffer( | |
163 | virtual_file_mapping, | |
164 | expressions, | |
165 | mapping_regions, | |
fc512014 | 166 | coverage_mapping_buffer, |
3dfed10e XL |
167 | ); |
168 | } | |
169 | ||
fc512014 XL |
170 | /// Construct coverage map header and the array of function records, and combine them into the |
171 | /// coverage map. Save the coverage map data into the LLVM IR as a static global using a | |
172 | /// specific, well-known section and name. | |
173 | fn generate_coverage_map( | |
3dfed10e XL |
174 | self, |
175 | cx: &CodegenCx<'ll, 'tcx>, | |
fc512014 XL |
176 | version: u32, |
177 | filenames_size: usize, | |
178 | filenames_val: &'ll llvm::Value, | |
179 | ) -> &'ll llvm::Value { | |
180 | debug!("cov map: filenames_size = {}, 0-based version = {}", filenames_size, version); | |
3dfed10e | 181 | |
fc512014 XL |
182 | // Create the coverage data header (Note, fields 0 and 2 are now always zero, |
183 | // as of `llvm::coverage::CovMapVersion::Version4`.) | |
184 | let zero_was_n_records_val = cx.const_u32(0); | |
3dfed10e | 185 | let filenames_size_val = cx.const_u32(filenames_size as u32); |
fc512014 XL |
186 | let zero_was_coverage_size_val = cx.const_u32(0); |
187 | let version_val = cx.const_u32(version); | |
3dfed10e | 188 | let cov_data_header_val = cx.const_struct( |
fc512014 | 189 | &[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val], |
3dfed10e XL |
190 | /*packed=*/ false, |
191 | ); | |
192 | ||
3dfed10e | 193 | // Create the complete LLVM coverage data value to add to the LLVM IR |
fc512014 XL |
194 | cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false) |
195 | } | |
196 | } | |
197 | ||
198 | /// Construct a function record and combine it with the function's coverage mapping data. | |
199 | /// Save the function record into the LLVM IR as a static global using a | |
200 | /// specific, well-known section and name. | |
201 | fn save_function_record( | |
202 | cx: &CodegenCx<'ll, 'tcx>, | |
203 | mangled_function_name: String, | |
204 | function_source_hash: u64, | |
205 | filenames_ref: u64, | |
206 | coverage_mapping_buffer: Vec<u8>, | |
207 | ) { | |
208 | // Concatenate the encoded coverage mappings | |
209 | let coverage_mapping_size = coverage_mapping_buffer.len(); | |
210 | let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer[..]); | |
211 | ||
212 | let func_name_hash = coverageinfo::hash_str(&mangled_function_name); | |
213 | let func_name_hash_val = cx.const_u64(func_name_hash); | |
214 | let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32); | |
215 | let func_hash_val = cx.const_u64(function_source_hash); | |
216 | let filenames_ref_val = cx.const_u64(filenames_ref); | |
217 | let func_record_val = cx.const_struct( | |
218 | &[ | |
219 | func_name_hash_val, | |
220 | coverage_mapping_size_val, | |
221 | func_hash_val, | |
222 | filenames_ref_val, | |
223 | coverage_mapping_val, | |
224 | ], | |
225 | /*packed=*/ true, | |
226 | ); | |
3dfed10e | 227 | |
fc512014 XL |
228 | // At the present time, the coverage map for Rust assumes every instrumented function `is_used`. |
229 | // Note that Clang marks functions as "unused" in `CodeGenPGO::emitEmptyCounterMapping`. (See: | |
230 | // https://github.com/rust-lang/llvm-project/blob/de02a75e398415bad4df27b4547c25b896c8bf3b/clang%2Flib%2FCodeGen%2FCodeGenPGO.cpp#L877-L878 | |
231 | // for example.) | |
232 | // | |
233 | // It's not yet clear if or how this may be applied to Rust in the future, but the `is_used` | |
234 | // argument is available and handled similarly. | |
235 | let is_used = true; | |
236 | coverageinfo::save_func_record_to_mod(cx, func_name_hash, func_record_val, is_used); | |
237 | } | |
238 | ||
239 | /// When finalizing the coverage map, `FunctionCoverage` only has the `CodeRegion`s and counters for | |
240 | /// the functions that went through codegen; such as public functions and "used" functions | |
241 | /// (functions referenced by other "used" or public items). Any other functions considered unused, | |
242 | /// or "Unreachable" were still parsed and processed through the MIR stage. | |
243 | /// | |
244 | /// We can find the unreachable functions by the set difference of all MIR `DefId`s (`tcx` query | |
245 | /// `mir_keys`) minus the codegenned `DefId`s (`tcx` query `collect_and_partition_mono_items`). | |
246 | /// | |
247 | /// *HOWEVER* the codegenned `DefId`s are partitioned across multiple `CodegenUnit`s (CGUs), and | |
248 | /// this function is processing a `function_coverage_map` for the functions (`Instance`/`DefId`) | |
249 | /// allocated to only one of those CGUs. We must NOT inject any "Unreachable" functions's | |
250 | /// `CodeRegion`s more than once, so we have to pick which CGU's `function_coverage_map` to add | |
251 | /// each "Unreachable" function to. | |
252 | /// | |
253 | /// Some constraints: | |
254 | /// | |
255 | /// 1. The file name of an "Unreachable" function must match the file name of the existing | |
256 | /// codegenned (covered) function to which the unreachable code regions will be added. | |
6a06907d | 257 | /// 2. The function to which the unreachable code regions will be added must not be a generic |
fc512014 XL |
258 | /// function (must not have type parameters) because the coverage tools will get confused |
259 | /// if the codegenned function has more than one instantiation and additional `CodeRegion`s | |
260 | /// attached to only one of those instantiations. | |
261 | fn add_unreachable_coverage<'tcx>( | |
262 | tcx: TyCtxt<'tcx>, | |
263 | function_coverage_map: &mut FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>>, | |
264 | ) { | |
265 | // FIXME(#79622): Can this solution be simplified and/or improved? Are there other sources | |
266 | // of compiler state data that might help (or better sources that could be exposed, but | |
267 | // aren't yet)? | |
268 | ||
269 | // Note: If the crate *only* defines generic functions, there are no codegenerated non-generic | |
270 | // functions to add any unreachable code to. In this case, the unreachable code regions will | |
271 | // have no coverage, instead of having coverage with zero executions. | |
272 | // | |
273 | // This is probably still an improvement over Clang, which does not generate any coverage | |
274 | // for uninstantiated template functions. | |
275 | ||
276 | let has_non_generic_def_ids = | |
277 | function_coverage_map.keys().any(|instance| instance.def.attrs(tcx).len() == 0); | |
278 | ||
279 | if !has_non_generic_def_ids { | |
280 | // There are no non-generic functions to add unreachable `CodeRegion`s to | |
281 | return; | |
282 | } | |
283 | ||
284 | let all_def_ids: DefIdSet = | |
285 | tcx.mir_keys(LOCAL_CRATE).iter().map(|local_def_id| local_def_id.to_def_id()).collect(); | |
286 | ||
6a06907d | 287 | let codegenned_def_ids = tcx.codegened_and_inlined_items(LOCAL_CRATE); |
fc512014 XL |
288 | |
289 | let mut unreachable_def_ids_by_file: FxHashMap<Symbol, Vec<DefId>> = FxHashMap::default(); | |
290 | for &non_codegenned_def_id in all_def_ids.difference(codegenned_def_ids) { | |
291 | // Make sure the non-codegenned (unreachable) function has a file_name | |
292 | if let Some(non_codegenned_file_name) = tcx.covered_file_name(non_codegenned_def_id) { | |
293 | let def_ids = unreachable_def_ids_by_file | |
294 | .entry(*non_codegenned_file_name) | |
5869c6ff | 295 | .or_insert_with(Vec::new); |
fc512014 XL |
296 | def_ids.push(non_codegenned_def_id); |
297 | } | |
298 | } | |
299 | ||
300 | if unreachable_def_ids_by_file.is_empty() { | |
301 | // There are no unreachable functions with file names to add (in any CGU) | |
302 | return; | |
303 | } | |
304 | ||
305 | // Since there may be multiple `CodegenUnit`s, some codegenned_def_ids may be codegenned in a | |
306 | // different CGU, and will be added to the function_coverage_map for each CGU. Determine which | |
307 | // function_coverage_map has the responsibility for publishing unreachable coverage | |
308 | // based on file name: | |
309 | // | |
310 | // For each covered file name, sort ONLY the non-generic codegenned_def_ids, and if | |
311 | // covered_def_ids.contains(the first def_id) for a given file_name, add the unreachable code | |
312 | // region in this function_coverage_map. Otherwise, ignore it and assume another CGU's | |
313 | // function_coverage_map will be adding it (because it will be first for one, and only one, | |
314 | // of them). | |
315 | let mut sorted_codegenned_def_ids: Vec<DefId> = | |
316 | codegenned_def_ids.iter().map(|def_id| *def_id).collect(); | |
317 | sorted_codegenned_def_ids.sort_unstable(); | |
318 | ||
319 | let mut first_covered_def_id_by_file: FxHashMap<Symbol, DefId> = FxHashMap::default(); | |
320 | for &def_id in sorted_codegenned_def_ids.iter() { | |
321 | // Only consider non-generic functions, to potentially add unreachable code regions | |
322 | if tcx.generics_of(def_id).count() == 0 { | |
323 | if let Some(covered_file_name) = tcx.covered_file_name(def_id) { | |
324 | // Only add files known to have unreachable functions | |
325 | if unreachable_def_ids_by_file.contains_key(covered_file_name) { | |
326 | first_covered_def_id_by_file.entry(*covered_file_name).or_insert(def_id); | |
327 | } | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
332 | // Get the set of def_ids with coverage regions, known by *this* CoverageContext. | |
333 | let cgu_covered_def_ids: DefIdSet = | |
334 | function_coverage_map.keys().map(|instance| instance.def.def_id()).collect(); | |
335 | ||
336 | let mut cgu_covered_files: FxHashSet<Symbol> = first_covered_def_id_by_file | |
337 | .iter() | |
338 | .filter_map( | |
339 | |(&file_name, def_id)| { | |
340 | if cgu_covered_def_ids.contains(def_id) { Some(file_name) } else { None } | |
341 | }, | |
342 | ) | |
343 | .collect(); | |
344 | ||
345 | // Find the first covered, non-generic function (instance) for each cgu_covered_file. Take the | |
346 | // unreachable code regions for that file, and add them to the function. | |
347 | // | |
348 | // There are three `for` loops here, but (a) the lists have already been reduced to the minimum | |
349 | // required values, the lists are further reduced (by `remove()` calls) when elements are no | |
350 | // longer needed, and there are several opportunities to branch out of loops early. | |
351 | for (instance, function_coverage) in function_coverage_map.iter_mut() { | |
352 | if instance.def.attrs(tcx).len() > 0 { | |
353 | continue; | |
354 | } | |
355 | // The covered function is not generic... | |
356 | let covered_def_id = instance.def.def_id(); | |
357 | if let Some(covered_file_name) = tcx.covered_file_name(covered_def_id) { | |
358 | if !cgu_covered_files.remove(&covered_file_name) { | |
359 | continue; | |
360 | } | |
361 | // The covered function's file is one of the files with unreachable code regions, so | |
362 | // all of the unreachable code regions for this file will be added to this function. | |
363 | for def_id in | |
364 | unreachable_def_ids_by_file.remove(&covered_file_name).into_iter().flatten() | |
365 | { | |
366 | // Note, this loop adds an unreachable code regions for each MIR-derived region. | |
367 | // Alternatively, we could add a single code region for the maximum span of all | |
368 | // code regions here. | |
369 | // | |
370 | // Observed downsides of this approach are: | |
371 | // | |
372 | // 1. The coverage results will appear inconsistent compared with the same (or | |
373 | // similar) code in a function that is reached. | |
374 | // 2. If the function is unreachable from one crate but reachable when compiling | |
375 | // another referencing crate (such as a cross-crate reference to a | |
376 | // generic function or inlined function), actual coverage regions overlaid | |
377 | // on a single larger code span of `Zero` coverage can appear confusing or | |
378 | // wrong. Chaning the unreachable coverage from a `code_region` to a | |
379 | // `gap_region` can help, but still can look odd with `0` line counts for | |
380 | // lines between executed (> 0) lines (such as for blank lines or comments). | |
381 | for ®ion in tcx.covered_code_regions(def_id) { | |
382 | function_coverage.add_unreachable_region(region.clone()); | |
383 | } | |
384 | } | |
385 | if cgu_covered_files.is_empty() { | |
386 | break; | |
387 | } | |
388 | } | |
3dfed10e XL |
389 | } |
390 | } |