]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
New upstream version 1.69.0+dfsg1
[rustc.git] / compiler / rustc_codegen_llvm / src / coverageinfo / mapgen.rs
CommitLineData
3dfed10e
XL
1use crate::common::CodegenCx;
2use crate::coverageinfo;
3use crate::llvm;
4
5use llvm::coverageinfo::CounterMappingRegion;
cdc7bbd5
XL
6use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression};
7use rustc_codegen_ssa::traits::{ConstMethods, CoverageInfoMethods};
5099ac24
FG
8use rustc_data_structures::fx::FxIndexSet;
9use rustc_hir::def::DefKind;
9c376795 10use rustc_hir::def_id::DefId;
3dfed10e 11use rustc_llvm::RustString;
5099ac24
FG
12use rustc_middle::bug;
13use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
3dfed10e 14use rustc_middle::mir::coverage::CodeRegion;
a2a8927a 15use rustc_middle::ty::TyCtxt;
3dfed10e
XL
16
17use std::ffi::CString;
18
3dfed10e
XL
19/// Generates and exports the Coverage Map.
20///
9ffffee4
FG
21/// Rust Coverage Map generation supports LLVM Coverage Mapping Format version
22/// 6 (zero-based encoded as 5), as defined at
a2a8927a
XL
23/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
24/// These versions are supported by the LLVM coverage tools (`llvm-profdata` and `llvm-cov`)
25/// bundled with Rust's fork of LLVM.
3dfed10e
XL
26///
27/// Consequently, Rust's bundled version of Clang also generates Coverage Maps compliant with
cdc7bbd5
XL
28/// the same version. Clang's implementation of Coverage Map generation was referenced when
29/// implementing this Rust version, and though the format documentation is very explicit and
30/// detailed, some undocumented details in Clang's implementation (that may or may not be important)
31/// were also replicated for Rust's Coverage Map.
9c376795 32pub fn finalize(cx: &CodegenCx<'_, '_>) {
fc512014 33 let tcx = cx.tcx;
cdc7bbd5 34
9ffffee4
FG
35 // Ensure the installed version of LLVM supports Coverage Map Version 6
36 // (encoded as a zero-based value: 5), which was introduced with LLVM 13.
fc512014 37 let version = coverageinfo::mapping_version();
9ffffee4 38 assert_eq!(version, 5, "The `CoverageMappingVersion` exposed by `llvm-wrapper` is out of sync");
fc512014
XL
39
40 debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
41
cdc7bbd5
XL
42 // In order to show that unused functions have coverage counts of zero (0), LLVM requires the
43 // functions exist. Generate synthetic functions with a (required) single counter, and add the
44 // MIR `Coverage` code regions to the `function_coverage_map`, before calling
45 // `ctx.take_function_coverage_map()`.
5099ac24 46 if cx.codegen_unit.is_code_coverage_dead_code_cgu() {
cdc7bbd5
XL
47 add_unused_functions(cx);
48 }
49
50 let function_coverage_map = match cx.coverage_context() {
29967ef6
XL
51 Some(ctx) => ctx.take_function_coverage_map(),
52 None => return,
53 };
cdc7bbd5 54
3dfed10e
XL
55 if function_coverage_map.is_empty() {
56 // This module has no functions with coverage instrumentation
57 return;
58 }
59
9ffffee4 60 let mut mapgen = CoverageMapGenerator::new(tcx);
3dfed10e
XL
61
62 // Encode coverage mappings and generate function records
fc512014
XL
63 let mut function_data = Vec::new();
64 for (instance, function_coverage) in function_coverage_map {
65 debug!("Generate function coverage for {}, {:?}", cx.codegen_unit.name(), instance);
66 let mangled_function_name = tcx.symbol_name(instance).to_string();
cdc7bbd5
XL
67 let source_hash = function_coverage.source_hash();
68 let is_used = function_coverage.is_used();
fc512014
XL
69 let (expressions, counter_regions) =
70 function_coverage.get_expressions_and_counter_regions();
71
72 let coverage_mapping_buffer = llvm::build_byte_buffer(|coverage_mapping_buffer| {
73 mapgen.write_coverage_mapping(expressions, counter_regions, coverage_mapping_buffer);
74 });
5099ac24
FG
75
76 if coverage_mapping_buffer.is_empty() {
77 if function_coverage.is_used() {
78 bug!(
79 "A used function should have had coverage mapping data but did not: {}",
80 mangled_function_name
81 );
82 } else {
83 debug!("unused function had no coverage mapping data: {}", mangled_function_name);
84 continue;
85 }
86 }
fc512014 87
cdc7bbd5 88 function_data.push((mangled_function_name, source_hash, is_used, coverage_mapping_buffer));
fc512014 89 }
3dfed10e
XL
90
91 // Encode all filenames referenced by counters/expressions in this module
92 let filenames_buffer = llvm::build_byte_buffer(|filenames_buffer| {
93 coverageinfo::write_filenames_section_to_buffer(&mapgen.filenames, filenames_buffer);
94 });
95
fc512014 96 let filenames_size = filenames_buffer.len();
a2a8927a 97 let filenames_val = cx.const_bytes(&filenames_buffer);
fc512014
XL
98 let filenames_ref = coverageinfo::hash_bytes(filenames_buffer);
99
3dfed10e 100 // Generate the LLVM IR representation of the coverage map and store it in a well-known global
fc512014
XL
101 let cov_data_val = mapgen.generate_coverage_map(cx, version, filenames_size, filenames_val);
102
cdc7bbd5 103 for (mangled_function_name, source_hash, is_used, coverage_mapping_buffer) in function_data {
fc512014
XL
104 save_function_record(
105 cx,
106 mangled_function_name,
cdc7bbd5 107 source_hash,
fc512014
XL
108 filenames_ref,
109 coverage_mapping_buffer,
cdc7bbd5 110 is_used,
fc512014
XL
111 );
112 }
113
114 // Save the coverage data value to LLVM IR
115 coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
3dfed10e
XL
116}
117
118struct CoverageMapGenerator {
119 filenames: FxIndexSet<CString>,
120}
121
122impl CoverageMapGenerator {
9ffffee4 123 fn new(tcx: TyCtxt<'_>) -> Self {
a2a8927a 124 let mut filenames = FxIndexSet::default();
9ffffee4
FG
125 // LLVM Coverage Mapping Format version 6 (zero-based encoded as 5)
126 // requires setting the first filename to the compilation directory.
127 // Since rustc generates coverage maps with relative paths, the
128 // compilation directory can be combined with the relative paths
129 // to get absolute paths, if needed.
130 let working_dir =
131 tcx.sess.opts.working_dir.remapped_path_if_available().to_string_lossy().to_string();
132 let c_filename =
133 CString::new(working_dir).expect("null error converting filename to C string");
134 filenames.insert(c_filename);
a2a8927a 135 Self { filenames }
3dfed10e
XL
136 }
137
138 /// Using the `expressions` and `counter_regions` collected for the current function, generate
139 /// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use
140 /// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
fc512014 141 /// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format.
a2a8927a 142 fn write_coverage_mapping<'a>(
3dfed10e
XL
143 &mut self,
144 expressions: Vec<CounterExpression>,
145 counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
fc512014 146 coverage_mapping_buffer: &RustString,
3dfed10e
XL
147 ) {
148 let mut counter_regions = counter_regions.collect::<Vec<_>>();
149 if counter_regions.is_empty() {
150 return;
151 }
152
153 let mut virtual_file_mapping = Vec::new();
154 let mut mapping_regions = Vec::new();
155 let mut current_file_name = None;
156 let mut current_file_id = 0;
157
158 // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted
159 // by filename and position. Capture any new files to compute the `CounterMappingRegion`s
160 // `file_id` (indexing files referenced by the current function), and construct the
161 // function-specific `virtual_file_mapping` from `file_id` to its index in the module's
162 // `filenames` array.
163 counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
164 for (counter, region) in counter_regions {
165 let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region;
487cf647 166 let same_file = current_file_name.map_or(false, |p| p == file_name);
3dfed10e
XL
167 if !same_file {
168 if current_file_name.is_some() {
169 current_file_id += 1;
170 }
171 current_file_name = Some(file_name);
172 let c_filename = CString::new(file_name.to_string())
173 .expect("null error converting filename to C string");
174 debug!(" file_id: {} = '{:?}'", current_file_id, c_filename);
175 let (filenames_index, _) = self.filenames.insert_full(c_filename);
176 virtual_file_mapping.push(filenames_index as u32);
177 }
29967ef6 178 debug!("Adding counter {:?} to map for {:?}", counter, region);
3dfed10e
XL
179 mapping_regions.push(CounterMappingRegion::code_region(
180 counter,
181 current_file_id,
182 start_line,
183 start_col,
184 end_line,
185 end_col,
186 ));
187 }
188
189 // Encode and append the current function's coverage mapping data
190 coverageinfo::write_mapping_to_buffer(
191 virtual_file_mapping,
192 expressions,
193 mapping_regions,
fc512014 194 coverage_mapping_buffer,
3dfed10e
XL
195 );
196 }
197
fc512014
XL
198 /// Construct coverage map header and the array of function records, and combine them into the
199 /// coverage map. Save the coverage map data into the LLVM IR as a static global using a
200 /// specific, well-known section and name.
a2a8927a 201 fn generate_coverage_map<'ll>(
3dfed10e 202 self,
a2a8927a 203 cx: &CodegenCx<'ll, '_>,
fc512014
XL
204 version: u32,
205 filenames_size: usize,
206 filenames_val: &'ll llvm::Value,
207 ) -> &'ll llvm::Value {
208 debug!("cov map: filenames_size = {}, 0-based version = {}", filenames_size, version);
3dfed10e 209
fc512014
XL
210 // Create the coverage data header (Note, fields 0 and 2 are now always zero,
211 // as of `llvm::coverage::CovMapVersion::Version4`.)
212 let zero_was_n_records_val = cx.const_u32(0);
3dfed10e 213 let filenames_size_val = cx.const_u32(filenames_size as u32);
fc512014
XL
214 let zero_was_coverage_size_val = cx.const_u32(0);
215 let version_val = cx.const_u32(version);
3dfed10e 216 let cov_data_header_val = cx.const_struct(
fc512014 217 &[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val],
3dfed10e
XL
218 /*packed=*/ false,
219 );
220
3dfed10e 221 // Create the complete LLVM coverage data value to add to the LLVM IR
fc512014
XL
222 cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false)
223 }
224}
225
226/// Construct a function record and combine it with the function's coverage mapping data.
227/// Save the function record into the LLVM IR as a static global using a
228/// specific, well-known section and name.
229fn save_function_record(
a2a8927a 230 cx: &CodegenCx<'_, '_>,
fc512014 231 mangled_function_name: String,
cdc7bbd5 232 source_hash: u64,
fc512014
XL
233 filenames_ref: u64,
234 coverage_mapping_buffer: Vec<u8>,
cdc7bbd5 235 is_used: bool,
fc512014
XL
236) {
237 // Concatenate the encoded coverage mappings
238 let coverage_mapping_size = coverage_mapping_buffer.len();
a2a8927a 239 let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
fc512014
XL
240
241 let func_name_hash = coverageinfo::hash_str(&mangled_function_name);
242 let func_name_hash_val = cx.const_u64(func_name_hash);
243 let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
cdc7bbd5 244 let source_hash_val = cx.const_u64(source_hash);
fc512014
XL
245 let filenames_ref_val = cx.const_u64(filenames_ref);
246 let func_record_val = cx.const_struct(
247 &[
248 func_name_hash_val,
249 coverage_mapping_size_val,
cdc7bbd5 250 source_hash_val,
fc512014
XL
251 filenames_ref_val,
252 coverage_mapping_val,
253 ],
254 /*packed=*/ true,
255 );
3dfed10e 256
fc512014
XL
257 coverageinfo::save_func_record_to_mod(cx, func_name_hash, func_record_val, is_used);
258}
259
260/// When finalizing the coverage map, `FunctionCoverage` only has the `CodeRegion`s and counters for
261/// the functions that went through codegen; such as public functions and "used" functions
262/// (functions referenced by other "used" or public items). Any other functions considered unused,
cdc7bbd5
XL
263/// or "Unreachable", were still parsed and processed through the MIR stage, but were not
264/// codegenned. (Note that `-Clink-dead-code` can force some unused code to be codegenned, but
5099ac24 265/// that flag is known to cause other errors, when combined with `-C instrument-coverage`; and
cdc7bbd5 266/// `-Clink-dead-code` will not generate code for unused generic functions.)
fc512014 267///
cdc7bbd5
XL
268/// We can find the unused functions (including generic functions) by the set difference of all MIR
269/// `DefId`s (`tcx` query `mir_keys`) minus the codegenned `DefId`s (`tcx` query
17df50a5 270/// `codegened_and_inlined_items`).
fc512014 271///
5099ac24
FG
272/// These unused functions are then codegen'd in one of the CGUs which is marked as the
273/// "code coverage dead code cgu" during the partitioning process. This prevents us from generating
274/// code regions for the same function more than once which can lead to linker errors regarding
275/// duplicate symbols.
9c376795 276fn add_unused_functions(cx: &CodegenCx<'_, '_>) {
5099ac24 277 assert!(cx.codegen_unit.is_code_coverage_dead_code_cgu());
cdc7bbd5 278
5099ac24 279 let tcx = cx.tcx;
fc512014 280
cdc7bbd5 281 let ignore_unused_generics = tcx.sess.instrument_coverage_except_unused_generics();
fc512014 282
9c376795 283 let eligible_def_ids: Vec<DefId> = tcx
17df50a5 284 .mir_keys(())
cdc7bbd5
XL
285 .iter()
286 .filter_map(|local_def_id| {
287 let def_id = local_def_id.to_def_id();
5099ac24
FG
288 let kind = tcx.def_kind(def_id);
289 // `mir_keys` will give us `DefId`s for all kinds of things, not
290 // just "functions", like consts, statics, etc. Filter those out.
291 // If `ignore_unused_generics` was specified, filter out any
292 // generic functions from consideration as well.
293 if !matches!(
294 kind,
295 DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Generator
296 ) {
297 return None;
9ffffee4
FG
298 }
299 if ignore_unused_generics && tcx.generics_of(def_id).requires_monomorphization(tcx) {
cdc7bbd5
XL
300 return None;
301 }
302 Some(local_def_id.to_def_id())
303 })
304 .collect();
fc512014 305
17df50a5 306 let codegenned_def_ids = tcx.codegened_and_inlined_items(());
fc512014 307
9c376795
FG
308 for non_codegenned_def_id in
309 eligible_def_ids.into_iter().filter(|id| !codegenned_def_ids.contains(id))
310 {
5099ac24 311 let codegen_fn_attrs = tcx.codegen_fn_attrs(non_codegenned_def_id);
fc512014 312
5099ac24
FG
313 // If a function is marked `#[no_coverage]`, then skip generating a
314 // dead code stub for it.
315 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_COVERAGE) {
316 debug!("skipping unused fn marked #[no_coverage]: {:?}", non_codegenned_def_id);
317 continue;
fc512014 318 }
fc512014 319
5099ac24
FG
320 debug!("generating unused fn: {:?}", non_codegenned_def_id);
321 cx.define_unused_fn(non_codegenned_def_id);
3dfed10e
XL
322 }
323}