]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs
Rollup merge of #93862 - Mark-Simulacrum:apple-split, r=pietroalbini
[rust.git] / compiler / rustc_codegen_ssa / src / mir / coverageinfo.rs
1 use crate::traits::*;
2
3 use rustc_middle::mir::coverage::*;
4 use rustc_middle::mir::Coverage;
5 use rustc_middle::mir::SourceScope;
6
7 use super::FunctionCx;
8
9 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10     pub fn codegen_coverage(&self, bx: &mut Bx, coverage: Coverage, scope: SourceScope) {
11         // Determine the instance that coverage data was originally generated for.
12         let scope_data = &self.mir.source_scopes[scope];
13         let instance = if let Some((inlined_instance, _)) = scope_data.inlined {
14             self.monomorphize(inlined_instance)
15         } else if let Some(inlined_scope) = scope_data.inlined_parent_scope {
16             self.monomorphize(self.mir.source_scopes[inlined_scope].inlined.unwrap().0)
17         } else {
18             self.instance
19         };
20
21         let Coverage { kind, code_region } = coverage;
22         match kind {
23             CoverageKind::Counter { function_source_hash, id } => {
24                 if bx.set_function_source_hash(instance, function_source_hash) {
25                     // If `set_function_source_hash()` returned true, the coverage map is enabled,
26                     // so continue adding the counter.
27                     if let Some(code_region) = code_region {
28                         // Note: Some counters do not have code regions, but may still be referenced
29                         // from expressions. In that case, don't add the counter to the coverage map,
30                         // but do inject the counter intrinsic.
31                         bx.add_coverage_counter(instance, id, code_region);
32                     }
33
34                     let coverageinfo = bx.tcx().coverageinfo(instance.def);
35
36                     let fn_name = bx.get_pgo_func_name_var(instance);
37                     let hash = bx.const_u64(function_source_hash);
38                     let num_counters = bx.const_u32(coverageinfo.num_counters);
39                     let index = bx.const_u32(id.zero_based_index());
40                     debug!(
41                         "codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
42                         fn_name, hash, num_counters, index,
43                     );
44                     bx.instrprof_increment(fn_name, hash, num_counters, index);
45                 }
46             }
47             CoverageKind::Expression { id, lhs, op, rhs } => {
48                 bx.add_coverage_counter_expression(instance, id, lhs, op, rhs, code_region);
49             }
50             CoverageKind::Unreachable => {
51                 bx.add_coverage_unreachable(
52                     instance,
53                     code_region.expect("unreachable regions always have code regions"),
54                 );
55             }
56         }
57     }
58 }