]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/coverageinfo/mod.rs
Rollup merge of #75485 - RalfJung:pin, r=nagisa
[rust.git] / src / librustc_codegen_llvm / coverageinfo / mod.rs
1 use crate::llvm;
2
3 use crate::builder::Builder;
4 use crate::common::CodegenCx;
5
6 use libc::c_uint;
7 use llvm::coverageinfo::CounterMappingRegion;
8 use log::debug;
9 use rustc_codegen_ssa::coverageinfo::map::{CounterExpression, ExprKind, FunctionCoverage, Region};
10 use rustc_codegen_ssa::traits::{
11     BaseTypeMethods, CoverageInfoBuilderMethods, CoverageInfoMethods, MiscMethods, StaticMethods,
12 };
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_llvm::RustString;
15 use rustc_middle::ty::Instance;
16
17 use std::cell::RefCell;
18 use std::ffi::CString;
19
20 pub mod mapgen;
21
22 const COVMAP_VAR_ALIGN_BYTES: usize = 8;
23
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>>>,
28 }
29
30 impl<'tcx> CrateCoverageContext<'tcx> {
31     pub fn new() -> Self {
32         Self { function_coverage_map: Default::default() }
33     }
34
35     pub fn take_function_coverage_map(&self) -> FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>> {
36         self.function_coverage_map.replace(FxHashMap::default())
37     }
38 }
39
40 impl CoverageInfoMethods for CodegenCx<'ll, 'tcx> {
41     fn coverageinfo_finalize(&self) {
42         mapgen::finalize(self)
43     }
44 }
45
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()) }
55     }
56
57     fn add_counter_region(
58         &mut self,
59         instance: Instance<'tcx>,
60         function_source_hash: u64,
61         id: u32,
62         region: Region<'tcx>,
63     ) {
64         debug!(
65             "adding counter to coverage_regions: instance={:?}, function_source_hash={}, id={}, \
66              at {:?}",
67             instance, function_source_hash, id, region,
68         );
69         let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
70         coverage_regions
71             .entry(instance)
72             .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
73             .add_counter(function_source_hash, id, region);
74     }
75
76     fn add_counter_expression_region(
77         &mut self,
78         instance: Instance<'tcx>,
79         id_descending_from_max: u32,
80         lhs: u32,
81         op: ExprKind,
82         rhs: u32,
83         region: Region<'tcx>,
84     ) {
85         debug!(
86             "adding counter expression to coverage_regions: instance={:?}, id={}, {} {:?} {}, \
87              at {:?}",
88             instance, id_descending_from_max, lhs, op, rhs, region,
89         );
90         let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
91         coverage_regions
92             .entry(instance)
93             .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
94             .add_counter_expression(id_descending_from_max, lhs, op, rhs, region);
95     }
96
97     fn add_unreachable_region(&mut self, instance: Instance<'tcx>, region: Region<'tcx>) {
98         debug!(
99             "adding unreachable code to coverage_regions: instance={:?}, at {:?}",
100             instance, region,
101         );
102         let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
103         coverage_regions
104             .entry(instance)
105             .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
106             .add_unreachable_region(region);
107     }
108 }
109
110 pub(crate) fn write_filenames_section_to_buffer<'a>(
111     filenames: impl IntoIterator<Item = &'a CString>,
112     buffer: &RustString,
113 ) {
114     let c_str_vec = filenames.into_iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
115     unsafe {
116         llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
117             c_str_vec.as_ptr(),
118             c_str_vec.len(),
119             buffer,
120         );
121     }
122 }
123
124 pub(crate) fn write_mapping_to_buffer(
125     virtual_file_mapping: Vec<u32>,
126     expressions: Vec<CounterExpression>,
127     mut mapping_regions: Vec<CounterMappingRegion>,
128     buffer: &RustString,
129 ) {
130     unsafe {
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,
138             buffer,
139         );
140     }
141 }
142
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()) }
146 }
147
148 pub(crate) fn mapping_version() -> u32 {
149     unsafe { llvm::LLVMRustCoverageMappingVersion() }
150 }
151
152 pub(crate) fn save_map_to_mod<'ll, 'tcx>(
153     cx: &CodegenCx<'ll, 'tcx>,
154     cov_data_val: &'ll llvm::Value,
155 ) {
156     let covmap_var_name = llvm::build_string(|s| unsafe {
157         llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
158     })
159     .expect("Rust Coverage Mapping var name failed UTF-8 conversion");
160     debug!("covmap var name: {:?}", covmap_var_name);
161
162     let covmap_section_name = llvm::build_string(|s| unsafe {
163         llvm::LLVMRustCoverageWriteSectionNameToString(cx.llmod, s);
164     })
165     .expect("Rust Coverage section name failed UTF-8 conversion");
166     debug!("covmap section name: {:?}", covmap_section_name);
167
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);
175 }