2020-08-15 11:42:13 +00:00
|
|
|
use crate::traits::*;
|
|
|
|
|
|
|
|
use rustc_middle::mir::coverage::*;
|
|
|
|
use rustc_middle::mir::Coverage;
|
2021-03-13 00:00:00 +00:00
|
|
|
use rustc_middle::mir::SourceScope;
|
2020-08-15 11:42:13 +00:00
|
|
|
|
|
|
|
use super::FunctionCx;
|
|
|
|
|
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2021-03-13 00:00:00 +00:00
|
|
|
pub fn codegen_coverage(&self, bx: &mut Bx, coverage: Coverage, scope: SourceScope) {
|
|
|
|
// Determine the instance that coverage data was originally generated for.
|
|
|
|
let scope_data = &self.mir.source_scopes[scope];
|
|
|
|
let instance = if let Some((inlined_instance, _)) = scope_data.inlined {
|
|
|
|
self.monomorphize(inlined_instance)
|
|
|
|
} else if let Some(inlined_scope) = scope_data.inlined_parent_scope {
|
|
|
|
self.monomorphize(self.mir.source_scopes[inlined_scope].inlined.unwrap().0)
|
|
|
|
} else {
|
|
|
|
self.instance
|
|
|
|
};
|
|
|
|
|
2020-08-15 11:42:13 +00:00
|
|
|
let Coverage { kind, code_region } = coverage;
|
|
|
|
match kind {
|
|
|
|
CoverageKind::Counter { function_source_hash, id } => {
|
2021-03-13 00:00:00 +00:00
|
|
|
if bx.set_function_source_hash(instance, function_source_hash) {
|
2020-10-25 18:13:16 +00:00
|
|
|
// If `set_function_source_hash()` returned true, the coverage map is enabled,
|
|
|
|
// so continue adding the counter.
|
|
|
|
if let Some(code_region) = code_region {
|
|
|
|
// Note: Some counters do not have code regions, but may still be referenced
|
|
|
|
// from expressions. In that case, don't add the counter to the coverage map,
|
|
|
|
// but do inject the counter intrinsic.
|
2021-03-13 00:00:00 +00:00
|
|
|
bx.add_coverage_counter(instance, id, code_region);
|
2020-10-25 18:13:16 +00:00
|
|
|
}
|
2020-10-05 23:36:10 +00:00
|
|
|
|
2021-03-13 00:00:00 +00:00
|
|
|
let coverageinfo = bx.tcx().coverageinfo(instance.def_id());
|
2020-08-15 11:42:13 +00:00
|
|
|
|
2021-03-13 00:00:00 +00:00
|
|
|
let fn_name = bx.create_pgo_func_name_var(instance);
|
2020-10-23 18:41:56 +00:00
|
|
|
let hash = bx.const_u64(function_source_hash);
|
|
|
|
let num_counters = bx.const_u32(coverageinfo.num_counters);
|
2020-10-22 21:30:03 +00:00
|
|
|
let index = bx.const_u32(u32::from(id));
|
2020-10-23 18:41:56 +00:00
|
|
|
debug!(
|
|
|
|
"codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
|
2020-10-22 21:30:03 +00:00
|
|
|
fn_name, hash, num_counters, index,
|
2020-10-23 18:41:56 +00:00
|
|
|
);
|
2020-10-05 23:36:10 +00:00
|
|
|
bx.instrprof_increment(fn_name, hash, num_counters, index);
|
2020-10-23 18:41:56 +00:00
|
|
|
}
|
2020-08-15 11:42:13 +00:00
|
|
|
}
|
|
|
|
CoverageKind::Expression { id, lhs, op, rhs } => {
|
2021-03-13 00:00:00 +00:00
|
|
|
bx.add_coverage_counter_expression(instance, id, lhs, op, rhs, code_region);
|
2020-08-15 11:42:13 +00:00
|
|
|
}
|
|
|
|
CoverageKind::Unreachable => {
|
2020-10-05 23:36:10 +00:00
|
|
|
bx.add_coverage_unreachable(
|
2021-03-13 00:00:00 +00:00
|
|
|
instance,
|
2020-10-05 23:36:10 +00:00
|
|
|
code_region.expect("unreachable regions always have code regions"),
|
|
|
|
);
|
2020-08-15 11:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|