1use std::sync::Arc;
23use itertools::Itertools;
4use rustc_abi::Align;
5use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
6use rustc_data_structures::assert_matches;
7use rustc_data_structures::fx::FxIndexMap;
8use rustc_index::IndexVec;
9use rustc_middle::ty::TyCtxt;
10use rustc_span::{FileName, RemapPathScopeComponents, SourceFile, StableSourceFileId};
11use tracing::debug;
1213use crate::common::CodegenCx;
14use crate::coverageinfo::llvm_cov;
15use crate::coverageinfo::mapgen::covfun::prepare_covfun_record;
16use crate::{TryFromU32, llvm};
1718mod covfun;
19mod spans;
20mod unused;
2122/// Version number that will be included the `__llvm_covmap` section header.
23/// Corresponds to LLVM's `llvm::coverage::CovMapVersion` (in `CoverageMapping.h`),
24/// or at least the subset that we know and care about.
25///
26/// Note that version `n` is encoded as `(n-1)`.
27#[derive(#[automatically_derived]
impl ::core::clone::Clone for CovmapVersion {
#[inline]
fn clone(&self) -> CovmapVersion { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for CovmapVersion { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for CovmapVersion {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "Version7")
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for CovmapVersion {
#[inline]
fn eq(&self, other: &CovmapVersion) -> bool { true }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CovmapVersion {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for CovmapVersion {
#[inline]
fn partial_cmp(&self, other: &CovmapVersion)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::option::Option::Some(::core::cmp::Ordering::Equal)
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for CovmapVersion {
#[inline]
fn cmp(&self, other: &CovmapVersion) -> ::core::cmp::Ordering {
::core::cmp::Ordering::Equal
}
}Ord, impl ::core::convert::TryFrom<u32> for CovmapVersion {
type Error = u32;
#[allow(deprecated)]
fn try_from(value: u32)
-> ::core::result::Result<CovmapVersion, Self::Error> {
if value == const { CovmapVersion::Version7 as u32 } {
return Ok(CovmapVersion::Version7)
}
Err(value)
}
}TryFromU32)]
28enum CovmapVersion {
29/// Used by LLVM 18 onwards.
30Version7 = 6,
31}
3233impl CovmapVersion {
34fn to_u32(self) -> u32 {
35selfas u3236 }
37}
3839/// Generates and exports the coverage map, which is embedded in special
40/// linker sections in the final binary.
41///
42/// Those sections are then read and understood by LLVM's `llvm-cov` tool,
43/// which is distributed in the `llvm-tools` rustup component.
44pub(crate) fn finalize(cx: &mut CodegenCx<'_, '_>) {
45let tcx = cx.tcx;
4647// Ensure that LLVM is using a version of the coverage mapping format that
48 // agrees with our Rust-side code. Expected versions are:
49 // - `Version7` (6) used by LLVM 18 onwards.
50let covmap_version =
51CovmapVersion::try_from(llvm_cov::mapping_version()).unwrap_or_else(|raw_version: u32| {
52{
::core::panicking::panic_fmt(format_args!("unknown coverage mapping version reported by `llvm-wrapper`: {0}",
raw_version));
}panic!("unknown coverage mapping version reported by `llvm-wrapper`: {raw_version}")53 });
54match covmap_version {
CovmapVersion::Version7 => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"CovmapVersion::Version7", ::core::option::Option::None);
}
};assert_matches!(covmap_version, CovmapVersion::Version7);
5556{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs:56",
"rustc_codegen_llvm::coverageinfo::mapgen",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs"),
::tracing_core::__macro_support::Option::Some(56u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::coverageinfo::mapgen"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Generating coverage map for CodegenUnit: `{0}`",
cx.codegen_unit.name()) as &dyn Value))])
});
} else { ; }
};debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
5758// FIXME(#132395): Can this be none even when coverage is enabled?
59let Some(ref coverage_cx) = cx.coverage_cx else { return };
6061let mut covfun_records = coverage_cx62 .instances_used()
63 .into_iter()
64// Sort by symbol name, so that the global file table is built in an
65 // order that doesn't depend on the stable-hash-based order in which
66 // instances were visited during codegen.
67.sorted_by_cached_key(|&instance| tcx.symbol_name(instance).name)
68 .filter_map(|instance| prepare_covfun_record(tcx, instance, true))
69 .collect::<Vec<_>>();
7071// In a single designated CGU, also prepare covfun records for functions
72 // in this crate that were instrumented for coverage, but are unused.
73if cx.codegen_unit.is_code_coverage_dead_code_cgu() {
74 unused::prepare_covfun_records_for_unused_functions(cx, &mut covfun_records);
75 }
7677// If there are no covfun records for this CGU, don't generate a covmap record.
78 // Emitting a covmap record without any covfun records causes `llvm-cov` to
79 // fail when generating coverage reports, and if there are no covfun records
80 // then the covmap record isn't useful anyway.
81 // This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
82if covfun_records.is_empty() {
83return;
84 }
8586// Prepare the global file table for this CGU, containing all paths needed
87 // by one or more covfun records.
88let global_file_table =
89GlobalFileTable::build(tcx, covfun_records.iter().flat_map(|c| c.all_source_files()));
9091for covfun in &covfun_records {
92 covfun::generate_covfun_record(cx, &global_file_table, covfun)
93 }
9495// Generate the coverage map header, which contains the filenames used by
96 // this CGU's coverage mappings, and store it in a well-known global.
97 // (This is skipped if we returned early due to having no covfun records.)
98generate_covmap_record(cx, covmap_version, &global_file_table.filenames_buffer);
99}
100101/// Maps "global" (per-CGU) file ID numbers to their underlying source file paths.
102#[derive(#[automatically_derived]
impl ::core::fmt::Debug for GlobalFileTable {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f,
"GlobalFileTable", "raw_file_table", &self.raw_file_table,
"filenames_buffer", &self.filenames_buffer, "filenames_hash",
&&self.filenames_hash)
}
}Debug)]
103struct GlobalFileTable {
104/// This "raw" table doesn't include the working dir, so a file's
105 /// global ID is its index in this set **plus one**.
106raw_file_table: FxIndexMap<StableSourceFileId, String>,
107108/// The file table in encoded form (possibly compressed), which can be
109 /// included directly in this CGU's `__llvm_covmap` record.
110filenames_buffer: Vec<u8>,
111112/// Truncated hash of the bytes in `filenames_buffer`.
113 ///
114 /// The `llvm-cov` tool uses this hash to associate each covfun record with
115 /// its corresponding filenames table, since the final binary will typically
116 /// contain multiple covmap records from different compilation units.
117filenames_hash: u64,
118}
119120impl GlobalFileTable {
121/// Builds a "global file table" for this CGU, mapping numeric IDs to
122 /// path strings.
123fn build<'a>(tcx: TyCtxt<'_>, all_files: impl Iterator<Item = &'a SourceFile>) -> Self {
124let mut raw_file_table = FxIndexMap::default();
125126for file in all_files {
127 raw_file_table.entry(file.stable_id).or_insert_with(|| {
128// Prefer using the embeddable filename as this filename is going to
129 // end-up in the coverage artifacts (see rust-lang/rust#150020).
130if let FileName::Real(real) = &file.name {
131let (_work_dir, abs_name) =
132 real.embeddable_name(RemapPathScopeComponents::COVERAGE);
133134 abs_name.to_string_lossy().into_owned()
135 } else {
136 file.name
137 .display(RemapPathScopeComponents::COVERAGE)
138 .to_string_lossy()
139 .into_owned()
140 }
141 });
142 }
143144// FIXME(Zalathar): Consider sorting the file table here, but maybe
145 // only after adding filename support to coverage-dump, so that the
146 // table order isn't directly visible in `.coverage-map` snapshots.
147148let mut table = Vec::with_capacity(raw_file_table.len() + 1);
149150// Since version 6 of the LLVM coverage mapping format, the first entry
151 // in the global file table is treated as a base directory, used to
152 // resolve any other entries that are stored as relative paths.
153let base_dir = tcx154 .sess
155 .psess
156 .source_map()
157 .working_dir()
158 .path(RemapPathScopeComponents::COVERAGE)
159 .to_string_lossy();
160table.push(base_dir.as_ref());
161162// Add the regular entries after the base directory.
163table.extend(raw_file_table.values().map(|name| name.as_str()));
164165// Encode the file table into a buffer, and get the hash of its encoded
166 // bytes, so that we can embed that hash in `__llvm_covfun` records.
167let filenames_buffer = llvm_cov::write_filenames_to_buffer(&table);
168let filenames_hash = llvm_cov::hash_bytes(&filenames_buffer);
169170Self { raw_file_table, filenames_buffer, filenames_hash }
171 }
172173fn get_existing_id(&self, file: &SourceFile) -> Option<GlobalFileId> {
174let raw_id = self.raw_file_table.get_index_of(&file.stable_id)?;
175// The raw file table doesn't include an entry for the base dir
176 // (which has ID 0), so add 1 to get the correct ID.
177Some(GlobalFileId::from_usize(raw_id + 1))
178 }
179}
180181impl ::std::fmt::Debug for GlobalFileId {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
182/// An index into the CGU's overall list of file paths. The underlying paths
183 /// will be embedded in the `__llvm_covmap` linker section.
184struct GlobalFileId {}
185}186impl ::std::fmt::Debug for LocalFileId {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
187/// An index into a function's list of global file IDs. That underlying list
188 /// of local-to-global mappings will be embedded in the function's record in
189 /// the `__llvm_covfun` linker section.
190struct LocalFileId {}
191}192193/// Holds a mapping from "local" (per-function) file IDs to their corresponding
194/// source files.
195#[derive(#[automatically_derived]
impl ::core::fmt::Debug for VirtualFileMapping {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"VirtualFileMapping", "local_file_table", &&self.local_file_table)
}
}Debug, #[automatically_derived]
impl ::core::default::Default for VirtualFileMapping {
#[inline]
fn default() -> VirtualFileMapping {
VirtualFileMapping {
local_file_table: ::core::default::Default::default(),
}
}
}Default)]
196struct VirtualFileMapping {
197 local_file_table: IndexVec<LocalFileId, Arc<SourceFile>>,
198}
199200impl VirtualFileMapping {
201fn push_file(&mut self, source_file: &Arc<SourceFile>) -> LocalFileId {
202self.local_file_table.push(Arc::clone(source_file))
203 }
204205/// Resolves all of the filenames in this local file mapping to a list of
206 /// global file IDs in its CGU, for inclusion in this function's
207 /// `__llvm_covfun` record.
208 ///
209 /// The global file IDs are returned as `u32` to make FFI easier.
210fn resolve_all(&self, global_file_table: &GlobalFileTable) -> Option<Vec<u32>> {
211self.local_file_table
212 .iter()
213 .map(|file| try {
214let id = global_file_table.get_existing_id(file)?;
215GlobalFileId::as_u32(id)
216 })
217 .collect::<Option<Vec<_>>>()
218 }
219}
220221/// Generates the contents of the covmap record for this CGU, which mostly
222/// consists of a header and a list of filenames. The record is then stored
223/// as a global variable in the `__llvm_covmap` section.
224fn generate_covmap_record<'ll>(
225 cx: &mut CodegenCx<'ll, '_>,
226 version: CovmapVersion,
227 filenames_buffer: &[u8],
228) {
229// A covmap record consists of four target-endian u32 values, followed by
230 // the encoded filenames table. Two of the header fields are unused in
231 // modern versions of the LLVM coverage mapping format, and are always 0.
232 // <https://llvm.org/docs/CoverageMappingFormat.html#llvm-ir-representation>
233 // See also `src/llvm-project/clang/lib/CodeGen/CoverageMappingGen.cpp`.
234let covmap_header = cx.const_struct(
235&[
236cx.const_u32(0), // (unused)
237cx.const_u32(filenames_buffer.len() as u32),
238cx.const_u32(0), // (unused)
239cx.const_u32(version.to_u32()),
240 ],
241/* packed */ false,
242 );
243let covmap_record = cx244 .const_struct(&[covmap_header, cx.const_bytes(filenames_buffer)], /* packed */ false);
245246let covmap_global =
247 llvm::add_global(cx.llmod, cx.val_ty(covmap_record), &llvm_cov::covmap_var_name());
248 llvm::set_initializer(covmap_global, covmap_record);
249 llvm::set_global_constant(covmap_global, true);
250 llvm::set_linkage(covmap_global, llvm::Linkage::PrivateLinkage);
251 llvm::set_section(covmap_global, &llvm_cov::covmap_section_name(cx.llmod));
252// LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
253 // <https://llvm.org/docs/CoverageMappingFormat.html>
254llvm::set_alignment(covmap_global, Align::EIGHT);
255256cx.add_used_global(covmap_global);
257}