]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs
Auto merge of #101173 - jyn514:simplify-macro-arguments, r=cjgillot
[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 instance = if let Some(inlined) = scope.inlined_instance(&self.mir.source_scopes) {
13             self.monomorphize(inlined)
14         } else {
15             self.instance
16         };
17
18         let Coverage { kind, code_region } = coverage;
19         match kind {
20             CoverageKind::Counter { function_source_hash, id } => {
21                 if bx.set_function_source_hash(instance, function_source_hash) {
22                     // If `set_function_source_hash()` returned true, the coverage map is enabled,
23                     // so continue adding the counter.
24                     if let Some(code_region) = code_region {
25                         // Note: Some counters do not have code regions, but may still be referenced
26                         // from expressions. In that case, don't add the counter to the coverage map,
27                         // but do inject the counter intrinsic.
28                         bx.add_coverage_counter(instance, id, code_region);
29                     }
30
31                     let coverageinfo = bx.tcx().coverageinfo(instance.def);
32
33                     let fn_name = bx.get_pgo_func_name_var(instance);
34                     let hash = bx.const_u64(function_source_hash);
35                     let num_counters = bx.const_u32(coverageinfo.num_counters);
36                     let index = bx.const_u32(id.zero_based_index());
37                     debug!(
38                         "codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
39                         fn_name, hash, num_counters, index,
40                     );
41                     bx.instrprof_increment(fn_name, hash, num_counters, index);
42                 }
43             }
44             CoverageKind::Expression { id, lhs, op, rhs } => {
45                 bx.add_coverage_counter_expression(instance, id, lhs, op, rhs, code_region);
46             }
47             CoverageKind::Unreachable => {
48                 bx.add_coverage_unreachable(
49                     instance,
50                     code_region.expect("unreachable regions always have code regions"),
51                 );
52             }
53         }
54     }
55 }