@@ -9,6 +9,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
99use rustc_llvm:: RustString ;
1010use rustc_middle:: bug;
1111use rustc_middle:: mir:: coverage:: CoverageKind ;
12+ use rustc_middle:: mir:: { Statement , StatementKind } ;
1213use rustc_middle:: ty:: Instance ;
1314use rustc_middle:: ty:: layout:: HasTyCtxt ;
1415use rustc_target:: abi:: { Align , Size } ;
@@ -91,9 +92,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
9192
9293impl < ' tcx > CoverageInfoBuilderMethods < ' tcx > for Builder < ' _ , ' _ , ' tcx > {
9394 fn init_coverage ( & mut self , instance : Instance < ' tcx > ) {
94- let Some ( function_coverage_info) =
95- self . tcx . instance_mir ( instance. def ) . function_coverage_info . as_deref ( )
96- else {
95+ let mir_body = self . tcx . instance_mir ( instance. def ) ;
96+ let Some ( function_coverage_info) = mir_body. function_coverage_info . as_deref ( ) else {
9797 return ;
9898 } ;
9999
@@ -102,6 +102,22 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
102102 return ;
103103 }
104104
105+ let is_mcdc_statement = |statement : & Statement < ' _ > | {
106+ let StatementKind :: Coverage ( cov_kind) = & statement. kind else {
107+ return false ;
108+ } ;
109+ matches ! (
110+ cov_kind,
111+ CoverageKind :: TestVectorBitmapUpdate { .. } | CoverageKind :: CondBitmapUpdate { .. }
112+ )
113+ } ;
114+ // Some instances like `DropGlue` clone mir body from others and modified the basic blocks after coverage pass.
115+ // Such instances might have same `function_coverage_info` as the primary but have no associated coverage statement.
116+ // Ignore these instances here.
117+ if !mir_body. basic_blocks . iter ( ) . map ( |bb| & bb. statements ) . flatten ( ) . any ( is_mcdc_statement) {
118+ return ;
119+ }
120+
105121 let fn_name = self . get_pgo_func_name_var ( instance) ;
106122 let hash = self . const_u64 ( function_coverage_info. function_source_hash ) ;
107123 let bitmap_bytes = self . const_u32 ( function_coverage_info. mcdc_bitmap_bytes ) ;
0 commit comments