]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Upgrades the coverage map to Version 4
[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::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_data = Vec::new();
42     for (instance, function_coverage) in function_coverage_map.into_iter() {
43         debug!("Generate coverage map for: {:?}", instance);
44
45         let mangled_function_name = cx.tcx.symbol_name(instance).to_string();
46         let function_source_hash = function_coverage.source_hash();
47         let (expressions, counter_regions) =
48             function_coverage.get_expressions_and_counter_regions();
49
50         let coverage_mapping_buffer = llvm::build_byte_buffer(|coverage_mapping_buffer| {
51             mapgen.write_coverage_mapping(expressions, counter_regions, coverage_mapping_buffer);
52         });
53         debug_assert!(
54             coverage_mapping_buffer.len() > 0,
55             "Every `FunctionCoverage` should have at least one counter"
56         );
57
58         function_data.push((mangled_function_name, function_source_hash, coverage_mapping_buffer));
59     }
60
61     // Encode all filenames referenced by counters/expressions in this module
62     let filenames_buffer = llvm::build_byte_buffer(|filenames_buffer| {
63         coverageinfo::write_filenames_section_to_buffer(&mapgen.filenames, filenames_buffer);
64     });
65
66     let filenames_size = filenames_buffer.len();
67     let filenames_val = cx.const_bytes(&filenames_buffer[..]);
68     let filenames_ref = coverageinfo::hash_bytes(filenames_buffer);
69
70     // Generate the LLVM IR representation of the coverage map and store it in a well-known global
71     let cov_data_val = mapgen.generate_coverage_map(cx, filenames_size, filenames_val);
72
73     for (mangled_function_name, function_source_hash, coverage_mapping_buffer) in function_data {
74         save_function_record(
75             cx,
76             mangled_function_name,
77             function_source_hash,
78             filenames_ref,
79             coverage_mapping_buffer,
80         );
81     }
82
83     // Save the coverage data value to LLVM IR
84     coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
85 }
86
87 struct CoverageMapGenerator {
88     filenames: FxIndexSet<CString>,
89 }
90
91 impl CoverageMapGenerator {
92     fn new() -> Self {
93         Self { filenames: FxIndexSet::default() }
94     }
95
96     /// Using the `expressions` and `counter_regions` collected for the current function, generate
97     /// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use
98     /// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
99     /// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format.
100     fn write_coverage_mapping(
101         &mut self,
102         expressions: Vec<CounterExpression>,
103         counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
104         coverage_mapping_buffer: &RustString,
105     ) {
106         let mut counter_regions = counter_regions.collect::<Vec<_>>();
107         if counter_regions.is_empty() {
108             return;
109         }
110
111         let mut virtual_file_mapping = Vec::new();
112         let mut mapping_regions = Vec::new();
113         let mut current_file_name = None;
114         let mut current_file_id = 0;
115
116         // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted
117         // by filename and position. Capture any new files to compute the `CounterMappingRegion`s
118         // `file_id` (indexing files referenced by the current function), and construct the
119         // function-specific `virtual_file_mapping` from `file_id` to its index in the module's
120         // `filenames` array.
121         counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
122         for (counter, region) in counter_regions {
123             let CodeRegion { file_name, start_line, start_col, end_line, end_col } = *region;
124             let same_file = current_file_name.as_ref().map_or(false, |p| *p == file_name);
125             if !same_file {
126                 if current_file_name.is_some() {
127                     current_file_id += 1;
128                 }
129                 current_file_name = Some(file_name);
130                 let c_filename = CString::new(file_name.to_string())
131                     .expect("null error converting filename to C string");
132                 debug!("  file_id: {} = '{:?}'", current_file_id, c_filename);
133                 let (filenames_index, _) = self.filenames.insert_full(c_filename);
134                 virtual_file_mapping.push(filenames_index as u32);
135             }
136             debug!("Adding counter {:?} to map for {:?}", counter, region);
137             mapping_regions.push(CounterMappingRegion::code_region(
138                 counter,
139                 current_file_id,
140                 start_line,
141                 start_col,
142                 end_line,
143                 end_col,
144             ));
145         }
146
147         // Encode and append the current function's coverage mapping data
148         coverageinfo::write_mapping_to_buffer(
149             virtual_file_mapping,
150             expressions,
151             mapping_regions,
152             coverage_mapping_buffer,
153         );
154     }
155
156     /// Construct coverage map header and the array of function records, and combine them into the
157     /// coverage map. Save the coverage map data into the LLVM IR as a static global using a
158     /// specific, well-known section and name.
159     fn generate_coverage_map(
160         self,
161         cx: &CodegenCx<'ll, 'tcx>,
162         filenames_size: usize,
163         filenames_val: &'ll llvm::Value,
164     ) -> &'ll llvm::Value {
165         debug!(
166             "cov map: filenames_size = {}, 0-based version = {}",
167             filenames_size,
168             coverageinfo::mapping_version()
169         );
170
171         // Create the coverage data header (Note, fields 0 and 2 are now always zero,
172         // as of `llvm::coverage::CovMapVersion::Version4`.
173         let zero_was_n_records_val = cx.const_u32(0);
174         let filenames_size_val = cx.const_u32(filenames_size as u32);
175         let zero_was_coverage_size_val = cx.const_u32(0 as u32);
176         let version_val = cx.const_u32(coverageinfo::mapping_version());
177         let cov_data_header_val = cx.const_struct(
178             &[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val],
179             /*packed=*/ false,
180         );
181
182         // Create the complete LLVM coverage data value to add to the LLVM IR
183         cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false)
184     }
185 }
186
187 /// Construct a function record and combine it with the function's coverage mapping data.
188 /// Save the function record into the LLVM IR as a static global using a
189 /// specific, well-known section and name.
190 fn save_function_record(
191     cx: &CodegenCx<'ll, 'tcx>,
192     mangled_function_name: String,
193     function_source_hash: u64,
194     filenames_ref: u64,
195     coverage_mapping_buffer: Vec<u8>,
196 ) {
197     // Concatenate the encoded coverage mappings
198     let coverage_mapping_size = coverage_mapping_buffer.len();
199     let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer[..]);
200
201     let func_name_hash = coverageinfo::hash_str(&mangled_function_name);
202     let func_name_hash_val = cx.const_u64(func_name_hash);
203     let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
204     let func_hash_val = cx.const_u64(function_source_hash);
205     let filenames_ref_val = cx.const_u64(filenames_ref);
206     let func_record_val = cx.const_struct(
207         &[
208             func_name_hash_val,
209             coverage_mapping_size_val,
210             func_hash_val,
211             filenames_ref_val,
212             coverage_mapping_val,
213         ],
214         /*packed=*/ true,
215     );
216
217     // At the present time, the coverage map for Rust assumes every instrumented function `is_used`.
218     // Note that Clang marks functions as "unused" in `CodeGenPGO::emitEmptyCounterMapping`. (See:
219     // https://github.com/rust-lang/llvm-project/blob/de02a75e398415bad4df27b4547c25b896c8bf3b/clang%2Flib%2FCodeGen%2FCodeGenPGO.cpp#L877-L878
220     // for example.)
221     //
222     // It's not yet clear if or how this may be applied to Rust in the future, but the `is_used`
223     // argument is available and handled similarly.
224     let is_used = true;
225     coverageinfo::save_func_record_to_mod(cx, func_name_hash, func_record_val, is_used);
226 }