rustc_codegen_llvm/coverageinfo/mapgen.rs
1use std::sync::Arc;
2
3use itertools::Itertools;
4use rustc_abi::Align;
5use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
6use rustc_data_structures::fx::FxIndexMap;
7use rustc_index::IndexVec;
8use rustc_middle::ty::TyCtxt;
9use rustc_session::RemapFileNameExt;
10use rustc_session::config::RemapPathScopeComponents;
11use rustc_span::{SourceFile, StableSourceFileId};
12use tracing::debug;
13
14use crate::common::CodegenCx;
15use crate::coverageinfo::llvm_cov;
16use crate::coverageinfo::mapgen::covfun::prepare_covfun_record;
17use crate::llvm;
18
19mod covfun;
20mod spans;
21mod unused;
22
23/// Generates and exports the coverage map, which is embedded in special
24/// linker sections in the final binary.
25///
26/// Those sections are then read and understood by LLVM's `llvm-cov` tool,
27/// which is distributed in the `llvm-tools` rustup component.
28pub(crate) fn finalize(cx: &mut CodegenCx<'_, '_>) {
29 let tcx = cx.tcx;
30
31 // Ensure that LLVM is using a version of the coverage mapping format that
32 // agrees with our Rust-side code. Expected versions (encoded as n-1) are:
33 // - `CovMapVersion::Version7` (6) used by LLVM 18-19
34 let covmap_version = {
35 let llvm_covmap_version = llvm_cov::mapping_version();
36 let expected_versions = 6..=6;
37 assert!(
38 expected_versions.contains(&llvm_covmap_version),
39 "Coverage mapping version exposed by `llvm-wrapper` is out of sync; \
40 expected {expected_versions:?} but was {llvm_covmap_version}"
41 );
42 // This is the version number that we will embed in the covmap section:
43 llvm_covmap_version
44 };
45
46 debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
47
48 // FIXME(#132395): Can this be none even when coverage is enabled?
49 let instances_used = match cx.coverage_cx {
50 Some(ref cx) => cx.instances_used.borrow(),
51 None => return,
52 };
53
54 let mut covfun_records = instances_used
55 .iter()
56 .copied()
57 // Sort by symbol name, so that the global file table is built in an
58 // order that doesn't depend on the stable-hash-based order in which
59 // instances were visited during codegen.
60 .sorted_by_cached_key(|&instance| tcx.symbol_name(instance).name)
61 .filter_map(|instance| prepare_covfun_record(tcx, instance, true))
62 .collect::<Vec<_>>();
63 drop(instances_used);
64
65 // In a single designated CGU, also prepare covfun records for functions
66 // in this crate that were instrumented for coverage, but are unused.
67 if cx.codegen_unit.is_code_coverage_dead_code_cgu() {
68 unused::prepare_covfun_records_for_unused_functions(cx, &mut covfun_records);
69 }
70
71 // If there are no covfun records for this CGU, don't generate a covmap record.
72 // Emitting a covmap record without any covfun records causes `llvm-cov` to
73 // fail when generating coverage reports, and if there are no covfun records
74 // then the covmap record isn't useful anyway.
75 // This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
76 if covfun_records.is_empty() {
77 return;
78 }
79
80 // Prepare the global file table for this CGU, containing all paths needed
81 // by one or more covfun records.
82 let global_file_table =
83 GlobalFileTable::build(tcx, covfun_records.iter().flat_map(|c| c.all_source_files()));
84
85 for covfun in &covfun_records {
86 covfun::generate_covfun_record(cx, &global_file_table, covfun)
87 }
88
89 // Generate the coverage map header, which contains the filenames used by
90 // this CGU's coverage mappings, and store it in a well-known global.
91 // (This is skipped if we returned early due to having no covfun records.)
92 generate_covmap_record(cx, covmap_version, &global_file_table.filenames_buffer);
93}
94
95/// Maps "global" (per-CGU) file ID numbers to their underlying source file paths.
96#[derive(Debug)]
97struct GlobalFileTable {
98 /// This "raw" table doesn't include the working dir, so a file's
99 /// global ID is its index in this set **plus one**.
100 raw_file_table: FxIndexMap<StableSourceFileId, String>,
101
102 /// The file table in encoded form (possibly compressed), which can be
103 /// included directly in this CGU's `__llvm_covmap` record.
104 filenames_buffer: Vec<u8>,
105
106 /// Truncated hash of the bytes in `filenames_buffer`.
107 ///
108 /// The `llvm-cov` tool uses this hash to associate each covfun record with
109 /// its corresponding filenames table, since the final binary will typically
110 /// contain multiple covmap records from different compilation units.
111 filenames_hash: u64,
112}
113
114impl GlobalFileTable {
115 /// Builds a "global file table" for this CGU, mapping numeric IDs to
116 /// path strings.
117 fn build<'a>(tcx: TyCtxt<'_>, all_files: impl Iterator<Item = &'a SourceFile>) -> Self {
118 let mut raw_file_table = FxIndexMap::default();
119
120 for file in all_files {
121 raw_file_table.entry(file.stable_id).or_insert_with(|| {
122 file.name
123 .for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
124 .to_string_lossy()
125 .into_owned()
126 });
127 }
128
129 // FIXME(Zalathar): Consider sorting the file table here, but maybe
130 // only after adding filename support to coverage-dump, so that the
131 // table order isn't directly visible in `.coverage-map` snapshots.
132
133 let mut table = Vec::with_capacity(raw_file_table.len() + 1);
134
135 // Since version 6 of the LLVM coverage mapping format, the first entry
136 // in the global file table is treated as a base directory, used to
137 // resolve any other entries that are stored as relative paths.
138 let base_dir = tcx
139 .sess
140 .opts
141 .working_dir
142 .for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
143 .to_string_lossy();
144 table.push(base_dir.as_ref());
145
146 // Add the regular entries after the base directory.
147 table.extend(raw_file_table.values().map(|name| name.as_str()));
148
149 // Encode the file table into a buffer, and get the hash of its encoded
150 // bytes, so that we can embed that hash in `__llvm_covfun` records.
151 let filenames_buffer = llvm_cov::write_filenames_to_buffer(&table);
152 let filenames_hash = llvm_cov::hash_bytes(&filenames_buffer);
153
154 Self { raw_file_table, filenames_buffer, filenames_hash }
155 }
156
157 fn get_existing_id(&self, file: &SourceFile) -> Option<GlobalFileId> {
158 let raw_id = self.raw_file_table.get_index_of(&file.stable_id)?;
159 // The raw file table doesn't include an entry for the base dir
160 // (which has ID 0), so add 1 to get the correct ID.
161 Some(GlobalFileId::from_usize(raw_id + 1))
162 }
163}
164
165rustc_index::newtype_index! {
166 /// An index into the CGU's overall list of file paths. The underlying paths
167 /// will be embedded in the `__llvm_covmap` linker section.
168 struct GlobalFileId {}
169}
170rustc_index::newtype_index! {
171 /// An index into a function's list of global file IDs. That underlying list
172 /// of local-to-global mappings will be embedded in the function's record in
173 /// the `__llvm_covfun` linker section.
174 struct LocalFileId {}
175}
176
177/// Holds a mapping from "local" (per-function) file IDs to their corresponding
178/// source files.
179#[derive(Debug, Default)]
180struct VirtualFileMapping {
181 local_file_table: IndexVec<LocalFileId, Arc<SourceFile>>,
182}
183
184impl VirtualFileMapping {
185 fn push_file(&mut self, source_file: &Arc<SourceFile>) -> LocalFileId {
186 self.local_file_table.push(Arc::clone(source_file))
187 }
188
189 /// Resolves all of the filenames in this local file mapping to a list of
190 /// global file IDs in its CGU, for inclusion in this function's
191 /// `__llvm_covfun` record.
192 ///
193 /// The global file IDs are returned as `u32` to make FFI easier.
194 fn resolve_all(&self, global_file_table: &GlobalFileTable) -> Option<Vec<u32>> {
195 self.local_file_table
196 .iter()
197 .map(|file| try {
198 let id = global_file_table.get_existing_id(file)?;
199 GlobalFileId::as_u32(id)
200 })
201 .collect::<Option<Vec<_>>>()
202 }
203}
204
205/// Generates the contents of the covmap record for this CGU, which mostly
206/// consists of a header and a list of filenames. The record is then stored
207/// as a global variable in the `__llvm_covmap` section.
208fn generate_covmap_record<'ll>(cx: &mut CodegenCx<'ll, '_>, version: u32, filenames_buffer: &[u8]) {
209 // A covmap record consists of four target-endian u32 values, followed by
210 // the encoded filenames table. Two of the header fields are unused in
211 // modern versions of the LLVM coverage mapping format, and are always 0.
212 // <https://llvm.org/docs/CoverageMappingFormat.html#llvm-ir-representation>
213 // See also `src/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp`.
214 let covmap_header = cx.const_struct(
215 &[
216 cx.const_u32(0), // (unused)
217 cx.const_u32(filenames_buffer.len() as u32),
218 cx.const_u32(0), // (unused)
219 cx.const_u32(version),
220 ],
221 /* packed */ false,
222 );
223 let covmap_record = cx
224 .const_struct(&[covmap_header, cx.const_bytes(filenames_buffer)], /* packed */ false);
225
226 let covmap_global =
227 llvm::add_global(cx.llmod, cx.val_ty(covmap_record), &llvm_cov::covmap_var_name());
228 llvm::set_initializer(covmap_global, covmap_record);
229 llvm::set_global_constant(covmap_global, true);
230 llvm::set_linkage(covmap_global, llvm::Linkage::PrivateLinkage);
231 llvm::set_section(covmap_global, &llvm_cov::covmap_section_name(cx.llmod));
232 // LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
233 // <https://llvm.org/docs/CoverageMappingFormat.html>
234 llvm::set_alignment(covmap_global, Align::EIGHT);
235
236 cx.add_used_global(covmap_global);
237}