rustc_codegen_llvm/coverageinfo/
mod.rs1use std::cell::{OnceCell, RefCell};
2use std::ffi::{CStr, CString};
3
4use rustc_codegen_ssa::traits::{
5 ConstCodegenMethods, CoverageInfoBuilderMethods, MiscCodegenMethods,
6};
7use rustc_data_structures::fx::FxIndexMap;
8use rustc_middle::mir::coverage::CoverageKind;
9use rustc_middle::ty::Instance;
10use tracing::{debug, instrument};
11
12use crate::builder::Builder;
13use crate::common::CodegenCx;
14use crate::llvm;
15
16pub(crate) mod ffi;
17mod llvm_cov;
18mod mapgen;
19
20pub(crate) struct CguCoverageContext<'ll, 'tcx> {
22 pub(crate) pgo_func_name_var_map: RefCell<FxIndexMap<Instance<'tcx>, &'ll llvm::Value>>,
30
31 covfun_section_name: OnceCell<CString>,
32}
33
34impl<'ll, 'tcx> CguCoverageContext<'ll, 'tcx> {
35 pub(crate) fn new() -> Self {
36 Self { pgo_func_name_var_map: Default::default(), covfun_section_name: Default::default() }
37 }
38
39 pub(crate) fn instances_used(&self) -> Vec<Instance<'tcx>> {
42 self.pgo_func_name_var_map.borrow().keys().copied().collect::<Vec<_>>()
45 }
46}
47
48impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
49 pub(crate) fn coverageinfo_finalize(&mut self) {
50 mapgen::finalize(self)
51 }
52
53 fn covfun_section_name(&self) -> &CStr {
62 self.coverage_cx()
63 .covfun_section_name
64 .get_or_init(|| llvm_cov::covfun_section_name(self.llmod))
65 }
66
67 fn ensure_pgo_func_name_var(&self, instance: Instance<'tcx>) -> &'ll llvm::Value {
75 debug!("getting pgo_func_name_var for instance={:?}", instance);
76 let mut pgo_func_name_var_map = self.coverage_cx().pgo_func_name_var_map.borrow_mut();
77 pgo_func_name_var_map.entry(instance).or_insert_with(|| {
78 let llfn = self.get_fn(instance);
79 let mangled_fn_name: &str = self.tcx.symbol_name(instance).name;
80 llvm_cov::create_pgo_func_name_var(llfn, mangled_fn_name)
81 })
82 }
83}
84
85impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
86 #[instrument(level = "debug", skip(self))]
87 fn add_coverage(&mut self, instance: Instance<'tcx>, kind: &CoverageKind) {
88 let bx = self;
96
97 let Some(_coverage_cx) = &bx.cx.coverage_cx else { return };
103
104 let Some(function_coverage_info) =
105 bx.tcx.instance_mir(instance.def).function_coverage_info.as_deref()
106 else {
107 debug!("function has a coverage statement but no coverage info");
108 return;
109 };
110 let Some(ids_info) = bx.tcx.coverage_ids_info(instance.def) else {
111 debug!("function has a coverage statement but no IDs info");
112 return;
113 };
114
115 match *kind {
116 CoverageKind::SpanMarker | CoverageKind::BlockMarker { .. } => unreachable!(
117 "marker statement {kind:?} should have been removed by CleanupPostBorrowck"
118 ),
119 CoverageKind::VirtualCounter { bcb }
120 if let Some(&id) = ids_info.phys_counter_for_node.get(&bcb) =>
121 {
122 let fn_name = bx.ensure_pgo_func_name_var(instance);
123 let hash = bx.const_u64(function_coverage_info.function_source_hash);
124 let num_counters = bx.const_u32(ids_info.num_counters);
125 let index = bx.const_u32(id.as_u32());
126 debug!(
127 "codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
128 fn_name, hash, num_counters, index,
129 );
130 bx.instrprof_increment(fn_name, hash, num_counters, index);
131 }
132 CoverageKind::VirtualCounter { .. } => {}
134 }
135 }
136}