Skip to content

Commit

Permalink
coverage. Fix that instances with no mcdc statements still initialize…
Browse files Browse the repository at this point in the history
… mcdc parameters
  • Loading branch information
Lambdaris committed Oct 24, 2024
1 parent a772336 commit a00c89e
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_llvm::RustString;
use rustc_middle::bug;
use rustc_middle::mir::coverage::CoverageKind;
use rustc_middle::mir::{Statement, StatementKind};
use rustc_middle::ty::Instance;
use rustc_middle::ty::layout::HasTyCtxt;
use rustc_target::abi::{Align, Size};
Expand Down Expand Up @@ -91,9 +92,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {

impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
fn init_coverage(&mut self, instance: Instance<'tcx>) {
let Some(function_coverage_info) =
self.tcx.instance_mir(instance.def).function_coverage_info.as_deref()
else {
let mir_body = self.tcx.instance_mir(instance.def);
let Some(function_coverage_info) = mir_body.function_coverage_info.as_deref() else {
return;
};

Expand All @@ -102,6 +102,25 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
return;
}

let is_counter_instrument = |statement: &Statement<'_>| {
let StatementKind::Coverage(cov_kind) = &statement.kind else {
return false;
};
matches!(cov_kind, CoverageKind::CounterIncrement { .. })
};
// Some instances like `DropGlue` clone mir body from others and modified the basic blocks after coverage pass.
// Such instances might have same `function_coverage_info` as the primary but have no associated coverage statement.
// Ignore these instances here.
if !mir_body
.basic_blocks
.iter()
.map(|bb| &bb.statements)
.flatten()
.any(is_counter_instrument)
{
return;
}

let fn_name = self.get_pgo_func_name_var(instance);
let hash = self.const_u64(function_coverage_info.function_source_hash);
let bitmap_bytes = self.const_u32(function_coverage_info.mcdc_bitmap_bytes);
Expand Down

0 comments on commit a00c89e

Please sign in to comment.