3 use crate::builder::Builder;
4 use crate::common::CodegenCx;
7 use llvm::coverageinfo::CounterMappingRegion;
9 use rustc_codegen_ssa::coverageinfo::map::{CounterExpression, ExprKind, FunctionCoverage, Region};
10 use rustc_codegen_ssa::traits::{
11 BaseTypeMethods, CoverageInfoBuilderMethods, CoverageInfoMethods, MiscMethods, StaticMethods,
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_llvm::RustString;
15 use rustc_middle::ty::Instance;
17 use std::cell::RefCell;
18 use std::ffi::CString;
22 const COVMAP_VAR_ALIGN_BYTES: usize = 8;
24 /// A context object for maintaining all state needed by the coverageinfo module.
25 pub struct CrateCoverageContext<'tcx> {
26 // Coverage region data for each instrumented function identified by DefId.
27 pub(crate) function_coverage_map: RefCell<FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>>>,
30 impl<'tcx> CrateCoverageContext<'tcx> {
31 pub fn new() -> Self {
32 Self { function_coverage_map: Default::default() }
35 pub fn take_function_coverage_map(&self) -> FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>> {
36 self.function_coverage_map.replace(FxHashMap::default())
40 impl CoverageInfoMethods for CodegenCx<'ll, 'tcx> {
41 fn coverageinfo_finalize(&self) {
42 mapgen::finalize(self)
46 impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
47 /// Calls llvm::createPGOFuncNameVar() with the given function instance's mangled function name.
48 /// The LLVM API returns an llvm::GlobalVariable containing the function name, with the specific
49 /// variable name and linkage required by LLVM InstrProf source-based coverage instrumentation.
50 fn create_pgo_func_name_var(&self, instance: Instance<'tcx>) -> Self::Value {
51 let llfn = self.cx.get_fn(instance);
52 let mangled_fn_name = CString::new(self.tcx.symbol_name(instance).name)
53 .expect("error converting function name to C string");
54 unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
57 fn add_counter_region(
59 instance: Instance<'tcx>,
60 function_source_hash: u64,
65 "adding counter to coverage_regions: instance={:?}, function_source_hash={}, id={}, \
67 instance, function_source_hash, id, region,
69 let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
72 .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
73 .add_counter(function_source_hash, id, region);
76 fn add_counter_expression_region(
78 instance: Instance<'tcx>,
79 id_descending_from_max: u32,
86 "adding counter expression to coverage_regions: instance={:?}, id={}, {} {:?} {}, \
88 instance, id_descending_from_max, lhs, op, rhs, region,
90 let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
93 .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
94 .add_counter_expression(id_descending_from_max, lhs, op, rhs, region);
97 fn add_unreachable_region(&mut self, instance: Instance<'tcx>, region: Region<'tcx>) {
99 "adding unreachable code to coverage_regions: instance={:?}, at {:?}",
102 let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
105 .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
106 .add_unreachable_region(region);
110 pub(crate) fn write_filenames_section_to_buffer<'a>(
111 filenames: impl IntoIterator<Item = &'a CString>,
114 let c_str_vec = filenames.into_iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
116 llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
124 pub(crate) fn write_mapping_to_buffer(
125 virtual_file_mapping: Vec<u32>,
126 expressions: Vec<CounterExpression>,
127 mut mapping_regions: Vec<CounterMappingRegion>,
131 llvm::LLVMRustCoverageWriteMappingToBuffer(
132 virtual_file_mapping.as_ptr(),
133 virtual_file_mapping.len() as c_uint,
134 expressions.as_ptr(),
135 expressions.len() as c_uint,
136 mapping_regions.as_mut_ptr(),
137 mapping_regions.len() as c_uint,
143 pub(crate) fn compute_hash(name: &str) -> u64 {
144 let name = CString::new(name).expect("null error converting hashable name to C string");
145 unsafe { llvm::LLVMRustCoverageComputeHash(name.as_ptr()) }
148 pub(crate) fn mapping_version() -> u32 {
149 unsafe { llvm::LLVMRustCoverageMappingVersion() }
152 pub(crate) fn save_map_to_mod<'ll, 'tcx>(
153 cx: &CodegenCx<'ll, 'tcx>,
154 cov_data_val: &'ll llvm::Value,
156 let covmap_var_name = llvm::build_string(|s| unsafe {
157 llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
159 .expect("Rust Coverage Mapping var name failed UTF-8 conversion");
160 debug!("covmap var name: {:?}", covmap_var_name);
162 let covmap_section_name = llvm::build_string(|s| unsafe {
163 llvm::LLVMRustCoverageWriteSectionNameToString(cx.llmod, s);
165 .expect("Rust Coverage section name failed UTF-8 conversion");
166 debug!("covmap section name: {:?}", covmap_section_name);
168 let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
169 llvm::set_initializer(llglobal, cov_data_val);
170 llvm::set_global_constant(llglobal, true);
171 llvm::set_linkage(llglobal, llvm::Linkage::InternalLinkage);
172 llvm::set_section(llglobal, &covmap_section_name);
173 llvm::set_alignment(llglobal, COVMAP_VAR_ALIGN_BYTES);
174 cx.add_used_global(llglobal);