]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Rollup merge of #76765 - guswynn:async_return, r=tmandry
[rust.git] / compiler / rustc_codegen_llvm / src / coverageinfo / mapgen.rs
1 use crate::common::CodegenCx;
2 use crate::coverageinfo;
3 use crate::llvm;
4
5 use llvm::coverageinfo::CounterMappingRegion;
6 use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression};
7 use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
8 use rustc_data_structures::fx::FxIndexSet;
9 use rustc_llvm::RustString;
10 use rustc_middle::mir::coverage::CodeRegion;
11
12 use std::ffi::CString;
13
14 use tracing::debug;
15
16 /// Generates and exports the Coverage Map.
17 ///
18 /// This Coverage Map complies with Coverage Mapping Format version 3 (zero-based encoded as 2),
19 /// as defined at [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format)
20 /// and published in Rust's current (July 2020) fork of LLVM. This version is supported by the
21 /// LLVM coverage tools (`llvm-profdata` and `llvm-cov`) bundled with Rust's fork of LLVM.
22 ///
23 /// Consequently, Rust's bundled version of Clang also generates Coverage Maps compliant with
24 /// version 3. Clang's implementation of Coverage Map generation was referenced when implementing
25 /// this Rust version, and though the format documentation is very explicit and detailed, some
26 /// undocumented details in Clang's implementation (that may or may not be important) were also
27 /// replicated for Rust's Coverage Map.
28 pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
29     let function_coverage_map = match cx.coverage_context() {
30         Some(ctx) => ctx.take_function_coverage_map(),
31         None => return,
32     };
33     if function_coverage_map.is_empty() {
34         // This module has no functions with coverage instrumentation
35         return;
36     }
37
38     let mut mapgen = CoverageMapGenerator::new();
39
40     // Encode coverage mappings and generate function records
41     let mut function_records = Vec::<&'ll llvm::Value>::new();
42     let coverage_mappings_buffer = llvm::build_byte_buffer(|coverage_mappings_buffer| {
43         for (instance, function_coverage) in function_coverage_map.into_iter() {
44             debug!("Generate coverage map for: {:?}", instance);
45
46             let mangled_function_name = cx.tcx.symbol_name(instance).to_string();
47             let function_source_hash = function_coverage.source_hash();
48             let (expressions, counter_regions) =
49                 function_coverage.get_expressions_and_counter_regions();
50
51             let old_len = coverage_mappings_buffer.len();
52             mapgen.write_coverage_mappings(expressions, counter_regions, coverage_mappings_buffer);
53             let mapping_data_size = coverage_mappings_buffer.len() - old_len;
54             debug_assert!(
55                 mapping_data_size > 0,
56                 "Every `FunctionCoverage` should have at least one counter"
57             );
58
59             let function_record = mapgen.make_function_record(
60                 cx,
61                 mangled_function_name,
62                 function_source_hash,
63                 mapping_data_size,
64             );
65             function_records.push(function_record);
66         }
67     });
68
69     // Encode all filenames referenced by counters/expressions in this module
70     let filenames_buffer = llvm::build_byte_buffer(|filenames_buffer| {
71         coverageinfo::write_filenames_section_to_buffer(&mapgen.filenames, filenames_buffer);
72     });
73
74     // Generate the LLVM IR representation of the coverage map and store it in a well-known global
75     mapgen.save_generated_coverage_map(
76         cx,
77         function_records,
78         filenames_buffer,
79         coverage_mappings_buffer,
80     );
81 }
82
83 struct CoverageMapGenerator {
84     filenames: FxIndexSet<CString>,
85 }
86
87 impl CoverageMapGenerator {
88     fn new() -> Self {
89         Self { filenames: FxIndexSet::default() }
90     }
91
92     /// Using the `expressions` and `counter_regions` collected for the current function, generate
93     /// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use
94     /// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
95     /// the given `coverage_mappings` byte buffer, compliant with the LLVM Coverage Mapping format.
96     fn write_coverage_mappings(
97         &mut self,
98         expressions: Vec<CounterExpression>,
99         counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
100         coverage_mappings_buffer: &RustString,
101     ) {
102         let mut counter_regions = counter_regions.collect::<Vec<_>>();
103         if counter_regions.is_empty() {
104             return;
105         }
106
107         let mut virtual_file_mapping = Vec::new();
108         let mut mapping_regions = Vec::new();
109         let mut current_file_name = None;
110         let mut current_file_id = 0;
111
112         // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted
113         // by filename and position. Capture any new files to compute the `CounterMappingRegion`s
114         // `file_id` (indexing files referenced by the current function), and construct the
115         // function-specific `virtual_file_mapping` from `file_id` to its index in the module's
116         // `filenames` array.
117         counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
118         for (counter, region) in counter_regions {
119             let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region;
120             let same_file = current_file_name.as_ref().map_or(false, |p| *p == file_name);
121             if !same_file {
122                 if current_file_name.is_some() {
123                     current_file_id += 1;
124                 }
125                 current_file_name = Some(file_name);
126                 let c_filename = CString::new(file_name.to_string())
127                     .expect("null error converting filename to C string");
128                 debug!("  file_id: {} = '{:?}'", current_file_id, c_filename);
129                 let (filenames_index, _) = self.filenames.insert_full(c_filename);
130                 virtual_file_mapping.push(filenames_index as u32);
131             }
132             debug!("Adding counter {:?} to map for {:?}", counter, region);
133             mapping_regions.push(CounterMappingRegion::code_region(
134                 counter,
135                 current_file_id,
136                 start_line,
137                 start_col,
138                 end_line,
139                 end_col,
140             ));
141         }
142
143         // Encode and append the current function's coverage mapping data
144         coverageinfo::write_mapping_to_buffer(
145             virtual_file_mapping,
146             expressions,
147             mapping_regions,
148             coverage_mappings_buffer,
149         );
150     }
151
152     /// Generate and return the function record `Value`
153     fn make_function_record(
154         &mut self,
155         cx: &CodegenCx<'ll, 'tcx>,
156         mangled_function_name: String,
157         function_source_hash: u64,
158         mapping_data_size: usize,
159     ) -> &'ll llvm::Value {
160         let name_ref = coverageinfo::compute_hash(&mangled_function_name);
161         let name_ref_val = cx.const_u64(name_ref);
162         let mapping_data_size_val = cx.const_u32(mapping_data_size as u32);
163         let func_hash_val = cx.const_u64(function_source_hash);
164         cx.const_struct(
165             &[name_ref_val, mapping_data_size_val, func_hash_val],
166             /*packed=*/ true,
167         )
168     }
169
170     /// Combine the filenames and coverage mappings buffers, construct coverage map header and the
171     /// array of function records, and combine everything into the complete coverage map. Save the
172     /// coverage map data into the LLVM IR as a static global using a specific, well-known section
173     /// and name.
174     fn save_generated_coverage_map(
175         self,
176         cx: &CodegenCx<'ll, 'tcx>,
177         function_records: Vec<&'ll llvm::Value>,
178         filenames_buffer: Vec<u8>,
179         mut coverage_mappings_buffer: Vec<u8>,
180     ) {
181         // Concatenate the encoded filenames and encoded coverage mappings, and add additional zero
182         // bytes as-needed to ensure 8-byte alignment.
183         let mut coverage_size = coverage_mappings_buffer.len();
184         let filenames_size = filenames_buffer.len();
185         let remaining_bytes =
186             (filenames_size + coverage_size) % coverageinfo::COVMAP_VAR_ALIGN_BYTES;
187         if remaining_bytes > 0 {
188             let pad = coverageinfo::COVMAP_VAR_ALIGN_BYTES - remaining_bytes;
189             coverage_mappings_buffer.append(&mut [0].repeat(pad));
190             coverage_size += pad;
191         }
192         let filenames_and_coverage_mappings = [filenames_buffer, coverage_mappings_buffer].concat();
193         let filenames_and_coverage_mappings_val =
194             cx.const_bytes(&filenames_and_coverage_mappings[..]);
195
196         debug!(
197             "cov map: n_records = {}, filenames_size = {}, coverage_size = {}, 0-based version = {}",
198             function_records.len(),
199             filenames_size,
200             coverage_size,
201             coverageinfo::mapping_version()
202         );
203
204         // Create the coverage data header
205         let n_records_val = cx.const_u32(function_records.len() as u32);
206         let filenames_size_val = cx.const_u32(filenames_size as u32);
207         let coverage_size_val = cx.const_u32(coverage_size as u32);
208         let version_val = cx.const_u32(coverageinfo::mapping_version());
209         let cov_data_header_val = cx.const_struct(
210             &[n_records_val, filenames_size_val, coverage_size_val, version_val],
211             /*packed=*/ false,
212         );
213
214         // Create the function records array
215         let name_ref_from_u64 = cx.type_i64();
216         let mapping_data_size_from_u32 = cx.type_i32();
217         let func_hash_from_u64 = cx.type_i64();
218         let function_record_ty = cx.type_struct(
219             &[name_ref_from_u64, mapping_data_size_from_u32, func_hash_from_u64],
220             /*packed=*/ true,
221         );
222         let function_records_val = cx.const_array(function_record_ty, &function_records[..]);
223
224         // Create the complete LLVM coverage data value to add to the LLVM IR
225         let cov_data_val = cx.const_struct(
226             &[cov_data_header_val, function_records_val, filenames_and_coverage_mappings_val],
227             /*packed=*/ false,
228         );
229
230         // Save the coverage data value to LLVM IR
231         coverageinfo::save_map_to_mod(cx, cov_data_val);
232     }
233 }