Skip to content

Commit

Permalink
coverage. Fix DropGlue instances might cause rustc panic when mcdc is…
Browse files Browse the repository at this point in the history
… enabled
  • Loading branch information
zhuyunxing committed Sep 5, 2024
1 parent 51e6096 commit d7a46c6
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 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::layout::HasTyCtxt;
use rustc_middle::ty::Instance;
use rustc_target::abi::{Align, Size};
Expand Down Expand Up @@ -92,16 +93,39 @@ 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;
};

// If there are no MC/DC bitmaps to set up, return immediately.
if function_coverage_info.mcdc_bitmap_bits == 0 {
return;
}
let is_mcdc_instrument = |statement: &Statement<'_>| {
let StatementKind::Coverage(coverage_kind) = &statement.kind else {
return false;
};
matches!(
coverage_kind,
CoverageKind::TestVectorBitmapUpdate { .. }
| CoverageKind::CondBitmapUpdate { .. }
| CoverageKind::CondBitmapReset { .. }
)
};
// Mir bodies of instances like `DropGlue` might be cloned from other instances' and removed some basic blocks.
// Such instances likely have same `function_coverage_info` as primary instances but have no coverage statements.
// No need to initialize mcdc parameters for this kind of instances.
if !mir_body
.basic_blocks
.iter()
.map(|bb| bb.statements.iter())
.flatten()
.any(is_mcdc_instrument)
{
return;
}

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

0 comments on commit d7a46c6

Please sign in to comment.