1
Fork 0

coverage: Store the number of counters/expressions in function coverage info

Coverage codegen can now allocate arrays based on the number of
counters/expressions originally used by the instrumentor.

The existing query that inspects coverage statements is still used for
determining the number of counters passed to `llvm.instrprof.increment`. If
some high-numbered counters were removed by MIR optimizations, the instrumented
binary can potentially use less memory and disk space at runtime.
This commit is contained in:
Zalathar 2023-09-13 12:15:40 +10:00
parent c479bc7f3b
commit a18c5f3b75
8 changed files with 69 additions and 102 deletions

View file

@ -6,7 +6,6 @@ use rustc_middle::mir::coverage::{
CodeRegion, CounterId, ExpressionId, FunctionCoverageInfo, Op, Operand, CodeRegion, CounterId, ExpressionId, FunctionCoverageInfo, Op, Operand,
}; };
use rustc_middle::ty::Instance; use rustc_middle::ty::Instance;
use rustc_middle::ty::TyCtxt;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Expression { pub struct Expression {
@ -40,38 +39,36 @@ pub struct FunctionCoverage<'tcx> {
impl<'tcx> FunctionCoverage<'tcx> { impl<'tcx> FunctionCoverage<'tcx> {
/// Creates a new set of coverage data for a used (called) function. /// Creates a new set of coverage data for a used (called) function.
pub fn new( pub fn new(
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>, instance: Instance<'tcx>,
function_coverage_info: &'tcx FunctionCoverageInfo, function_coverage_info: &'tcx FunctionCoverageInfo,
) -> Self { ) -> Self {
Self::create(tcx, instance, function_coverage_info, true) Self::create(instance, function_coverage_info, true)
} }
/// Creates a new set of coverage data for an unused (never called) function. /// Creates a new set of coverage data for an unused (never called) function.
pub fn unused( pub fn unused(
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>, instance: Instance<'tcx>,
function_coverage_info: &'tcx FunctionCoverageInfo, function_coverage_info: &'tcx FunctionCoverageInfo,
) -> Self { ) -> Self {
Self::create(tcx, instance, function_coverage_info, false) Self::create(instance, function_coverage_info, false)
} }
fn create( fn create(
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>, instance: Instance<'tcx>,
function_coverage_info: &'tcx FunctionCoverageInfo, function_coverage_info: &'tcx FunctionCoverageInfo,
is_used: bool, is_used: bool,
) -> Self { ) -> Self {
let coverageinfo = tcx.coverageinfo(instance.def); let num_counters = function_coverage_info.num_counters;
let num_expressions = function_coverage_info.num_expressions;
debug!( debug!(
"FunctionCoverage::create(instance={:?}) has coverageinfo={:?}. is_used={}", "FunctionCoverage::create(instance={instance:?}) has \
instance, coverageinfo, is_used num_counters={num_counters}, num_expressions={num_expressions}, is_used={is_used}"
); );
Self { Self {
function_coverage_info, function_coverage_info,
is_used, is_used,
counters: IndexVec::from_elem_n(None, coverageinfo.num_counters as usize), counters: IndexVec::from_elem_n(None, num_counters),
expressions: IndexVec::from_elem_n(None, coverageinfo.num_expressions as usize), expressions: IndexVec::from_elem_n(None, num_expressions),
unreachable_regions: Vec::new(), unreachable_regions: Vec::new(),
} }
} }

View file

@ -114,7 +114,7 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
let mut coverage_map = coverage_context.function_coverage_map.borrow_mut(); let mut coverage_map = coverage_context.function_coverage_map.borrow_mut();
let func_coverage = coverage_map let func_coverage = coverage_map
.entry(instance) .entry(instance)
.or_insert_with(|| FunctionCoverage::new(bx.tcx(), instance, function_coverage_info)); .or_insert_with(|| FunctionCoverage::new(instance, function_coverage_info));
let Coverage { kind, code_regions } = coverage; let Coverage { kind, code_regions } = coverage;
match *kind { match *kind {
@ -124,11 +124,21 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
// as that needs an exclusive borrow. // as that needs an exclusive borrow.
drop(coverage_map); drop(coverage_map);
let coverageinfo = bx.tcx().coverageinfo(instance.def); // The number of counters passed to `llvm.instrprof.increment` might
// be smaller than the number originally inserted by the instrumentor,
// if some high-numbered counters were removed by MIR optimizations.
// If so, LLVM's profiler runtime will use fewer physical counters.
let num_counters =
bx.tcx().coverage_ids_info(instance.def).max_counter_id.as_u32() + 1;
assert!(
num_counters as usize <= function_coverage_info.num_counters,
"num_counters disagreement: query says {num_counters} but function info only has {}",
function_coverage_info.num_counters
);
let fn_name = bx.get_pgo_func_name_var(instance); let fn_name = bx.get_pgo_func_name_var(instance);
let hash = bx.const_u64(function_coverage_info.function_source_hash); let hash = bx.const_u64(function_coverage_info.function_source_hash);
let num_counters = bx.const_u32(coverageinfo.num_counters); let num_counters = bx.const_u32(num_counters);
let index = bx.const_u32(id.as_u32()); let index = bx.const_u32(id.as_u32());
debug!( debug!(
"codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})", "codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
@ -208,7 +218,7 @@ fn add_unused_function_coverage<'tcx>(
) { ) {
let tcx = cx.tcx; let tcx = cx.tcx;
let mut function_coverage = FunctionCoverage::unused(tcx, instance, function_coverage_info); let mut function_coverage = FunctionCoverage::unused(instance, function_coverage_info);
for &code_region in tcx.covered_code_regions(def_id) { for &code_region in tcx.covered_code_regions(def_id) {
let code_region = std::slice::from_ref(code_region); let code_region = std::slice::from_ref(code_region);
function_coverage.add_unreachable_regions(code_region); function_coverage.add_unreachable_regions(code_region);

View file

@ -140,4 +140,6 @@ impl Op {
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)] #[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
pub struct FunctionCoverageInfo { pub struct FunctionCoverageInfo {
pub function_source_hash: u64, pub function_source_hash: u64,
pub num_counters: usize,
pub num_expressions: usize,
} }

View file

@ -1,5 +1,6 @@
//! Values computed by queries that use MIR. //! Values computed by queries that use MIR.
use crate::mir;
use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt}; use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::unord::UnordSet; use rustc_data_structures::unord::UnordSet;
@ -445,14 +446,19 @@ pub struct DestructuredConstant<'tcx> {
pub fields: &'tcx [(ConstValue<'tcx>, Ty<'tcx>)], pub fields: &'tcx [(ConstValue<'tcx>, Ty<'tcx>)],
} }
/// Coverage information summarized from a MIR if instrumented for source code coverage (see /// Summarizes coverage IDs inserted by the `InstrumentCoverage` MIR pass
/// compiler option `-Cinstrument-coverage`). This information is generated by the /// (for compiler option `-Cinstrument-coverage`), after MIR optimizations
/// `InstrumentCoverage` MIR pass and can be retrieved via the `coverageinfo` query. /// have had a chance to potentially remove some of them.
///
/// Used by the `coverage_ids_info` query.
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable)] #[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable)]
pub struct CoverageInfo { pub struct CoverageIdsInfo {
/// The total number of coverage region counters added to the MIR `Body`. /// Coverage codegen needs to know the highest counter ID that is ever
pub num_counters: u32, /// incremented within a function, so that it can set the `num-counters`
/// argument of the `llvm.instrprof.increment` intrinsic.
/// The total number of coverage region counter expressions added to the MIR `Body`. ///
pub num_expressions: u32, /// This may be less than the highest counter ID emitted by the
/// InstrumentCoverage MIR pass, if the highest-numbered counter increments
/// were removed by MIR optimizations.
pub max_counter_id: mir::coverage::CounterId,
} }

View file

@ -573,10 +573,11 @@ rustc_queries! {
separate_provide_extern separate_provide_extern
} }
/// Returns coverage summary info for a function, after executing the `InstrumentCoverage` /// Summarizes coverage IDs inserted by the `InstrumentCoverage` MIR pass
/// MIR pass (assuming the -Cinstrument-coverage option is enabled). /// (for compiler option `-Cinstrument-coverage`), after MIR optimizations
query coverageinfo(key: ty::InstanceDef<'tcx>) -> &'tcx mir::CoverageInfo { /// have had a chance to potentially remove some of them.
desc { |tcx| "retrieving coverage info from MIR for `{}`", tcx.def_path_str(key.def_id()) } query coverage_ids_info(key: ty::InstanceDef<'tcx>) -> &'tcx mir::CoverageIdsInfo {
desc { |tcx| "retrieving coverage IDs info from MIR for `{}`", tcx.def_path_str(key.def_id()) }
arena_cache arena_cache
} }

View file

@ -126,6 +126,14 @@ impl CoverageCounters {
next next
} }
pub(super) fn num_counters(&self) -> usize {
self.next_counter_id.as_usize()
}
pub(super) fn num_expressions(&self) -> usize {
self.next_expression_id.as_usize()
}
fn set_bcb_counter( fn set_bcb_counter(
&mut self, &mut self,
bcb: BasicCoverageBlock, bcb: BasicCoverageBlock,

View file

@ -214,6 +214,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
self.mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo { self.mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo {
function_source_hash: self.function_source_hash, function_source_hash: self.function_source_hash,
num_counters: self.coverage_counters.num_counters(),
num_expressions: self.coverage_counters.num_expressions(),
})); }));
} }

View file

@ -2,92 +2,33 @@ use super::*;
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
use rustc_middle::mir::coverage::*; use rustc_middle::mir::coverage::*;
use rustc_middle::mir::{self, Body, Coverage, CoverageInfo}; use rustc_middle::mir::{self, Body, Coverage, CoverageIdsInfo};
use rustc_middle::query::Providers; use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
/// A `query` provider for retrieving coverage information injected into MIR. /// A `query` provider for retrieving coverage information injected into MIR.
pub(crate) fn provide(providers: &mut Providers) { pub(crate) fn provide(providers: &mut Providers) {
providers.coverageinfo = |tcx, def_id| coverageinfo(tcx, def_id); providers.coverage_ids_info = |tcx, def_id| coverage_ids_info(tcx, def_id);
providers.covered_code_regions = |tcx, def_id| covered_code_regions(tcx, def_id); providers.covered_code_regions = |tcx, def_id| covered_code_regions(tcx, def_id);
} }
/// Coverage codegen needs to know the total number of counter IDs and expression IDs that have /// Query implementation for `coverage_ids_info`.
/// been used by a function's coverage mappings. These totals are used to create vectors to hold fn coverage_ids_info<'tcx>(
/// the relevant counter and expression data, and the maximum counter ID (+ 1) is also needed by tcx: TyCtxt<'tcx>,
/// the `llvm.instrprof.increment` intrinsic. instance_def: ty::InstanceDef<'tcx>,
/// ) -> CoverageIdsInfo {
/// MIR optimization may split and duplicate some BasicBlock sequences, or optimize out some code
/// including injected counters. (It is OK if some counters are optimized out, but those counters
/// are still included in the total `num_counters` or `num_expressions`.) Simply counting the
/// calls may not work; but computing the number of counters or expressions by adding `1` to the
/// highest ID (for a given instrumented function) is valid.
///
/// It's possible for a coverage expression to remain in MIR while one or both of its operands
/// have been optimized away. To avoid problems in codegen, we include those operands' IDs when
/// determining the maximum counter/expression ID, even if the underlying counter/expression is
/// no longer present.
struct CoverageVisitor {
max_counter_id: CounterId,
max_expression_id: ExpressionId,
}
impl CoverageVisitor {
/// Updates `max_counter_id` to the maximum encountered counter ID.
#[inline(always)]
fn update_max_counter_id(&mut self, counter_id: CounterId) {
self.max_counter_id = self.max_counter_id.max(counter_id);
}
/// Updates `max_expression_id` to the maximum encountered expression ID.
#[inline(always)]
fn update_max_expression_id(&mut self, expression_id: ExpressionId) {
self.max_expression_id = self.max_expression_id.max(expression_id);
}
fn update_from_expression_operand(&mut self, operand: Operand) {
match operand {
Operand::Counter(id) => self.update_max_counter_id(id),
Operand::Expression(id) => self.update_max_expression_id(id),
Operand::Zero => {}
}
}
fn visit_body(&mut self, body: &Body<'_>) {
for coverage in all_coverage_in_mir_body(body) {
self.visit_coverage(coverage);
}
}
fn visit_coverage(&mut self, coverage: &Coverage) {
match coverage.kind {
CoverageKind::Counter { id, .. } => self.update_max_counter_id(id),
CoverageKind::Expression { id, lhs, rhs, .. } => {
self.update_max_expression_id(id);
self.update_from_expression_operand(lhs);
self.update_from_expression_operand(rhs);
}
CoverageKind::Unreachable => {}
}
}
}
fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) -> CoverageInfo {
let mir_body = tcx.instance_mir(instance_def); let mir_body = tcx.instance_mir(instance_def);
let mut coverage_visitor = CoverageVisitor { let max_counter_id = all_coverage_in_mir_body(mir_body)
max_counter_id: CounterId::START, .filter_map(|coverage| match coverage.kind {
max_expression_id: ExpressionId::START, CoverageKind::Counter { id } => Some(id),
}; _ => None,
})
.max()
.unwrap_or(CounterId::START);
coverage_visitor.visit_body(mir_body); CoverageIdsInfo { max_counter_id }
// Add 1 to the highest IDs to get the total number of IDs.
CoverageInfo {
num_counters: (coverage_visitor.max_counter_id + 1).as_u32(),
num_expressions: (coverage_visitor.max_expression_id + 1).as_u32(),
}
} }
fn covered_code_regions(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<&CodeRegion> { fn covered_code_regions(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<&CodeRegion> {