Skip to content

Commit

Permalink
coverage: Unused functions don't need to store CoverageIdsInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed Dec 8, 2024
1 parent 4d2bfec commit 3a35fb6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
23 changes: 12 additions & 11 deletions compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,31 @@ use crate::coverageinfo::ffi::{Counter, CounterExpression, ExprKind};

pub(crate) struct FunctionCoverage<'tcx> {
pub(crate) function_coverage_info: &'tcx FunctionCoverageInfo,
ids_info: &'tcx CoverageIdsInfo,
is_used: bool,
/// If `None`, the corresponding function is unused.
ids_info: Option<&'tcx CoverageIdsInfo>,
}

impl<'tcx> FunctionCoverage<'tcx> {
pub(crate) fn new_used(
function_coverage_info: &'tcx FunctionCoverageInfo,
ids_info: &'tcx CoverageIdsInfo,
) -> Self {
Self { function_coverage_info, ids_info, is_used: true }
Self { function_coverage_info, ids_info: Some(ids_info) }
}

pub(crate) fn new_unused(
function_coverage_info: &'tcx FunctionCoverageInfo,
ids_info: &'tcx CoverageIdsInfo,
) -> Self {
Self { function_coverage_info, ids_info, is_used: false }
pub(crate) fn new_unused(function_coverage_info: &'tcx FunctionCoverageInfo) -> Self {
Self { function_coverage_info, ids_info: None }
}

/// Returns true for a used (called) function, and false for an unused function.
pub(crate) fn is_used(&self) -> bool {
self.is_used
self.ids_info.is_some()
}

/// Return the source hash, generated from the HIR node structure, and used to indicate whether
/// or not the source code structure changed between different compilations.
pub(crate) fn source_hash(&self) -> u64 {
if self.is_used { self.function_coverage_info.function_source_hash } else { 0 }
if self.is_used() { self.function_coverage_info.function_source_hash } else { 0 }
}

/// Convert this function's coverage expression data into a form that can be
Expand Down Expand Up @@ -78,6 +75,10 @@ impl<'tcx> FunctionCoverage<'tcx> {
}

fn is_zero_term(&self, term: CovTerm) -> bool {
!self.is_used || self.ids_info.is_zero_term(term)
match self.ids_info {
Some(ids_info) => ids_info.is_zero_term(term),
// This function is unused, so all coverage counters/expressions are zero.
None => true,
}
}
}
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ fn add_unused_function_coverage<'tcx>(
);

// An unused function's mappings will all be rewritten to map to zero.
let function_coverage =
FunctionCoverage::new_unused(function_coverage_info, tcx.coverage_ids_info(instance.def));
let function_coverage = FunctionCoverage::new_unused(function_coverage_info);
cx.coverage_cx().function_coverage_map.borrow_mut().insert(instance, function_coverage);
}

0 comments on commit 3a35fb6

Please sign in to comment.