@@ -9,6 +9,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
9
9
use rustc_llvm:: RustString ;
10
10
use rustc_middle:: bug;
11
11
use rustc_middle:: mir:: coverage:: CoverageKind ;
12
+ use rustc_middle:: mir:: { Statement , StatementKind } ;
12
13
use rustc_middle:: ty:: layout:: HasTyCtxt ;
13
14
use rustc_middle:: ty:: Instance ;
14
15
use rustc_target:: abi:: { Align , Size } ;
@@ -92,16 +93,39 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
92
93
93
94
impl < ' tcx > CoverageInfoBuilderMethods < ' tcx > for Builder < ' _ , ' _ , ' tcx > {
94
95
fn init_coverage ( & mut self , instance : Instance < ' tcx > ) {
95
- let Some ( function_coverage_info ) =
96
- self . tcx . instance_mir ( instance . def ) . function_coverage_info . as_deref ( )
97
- else {
96
+ let mir_body = self . tcx . instance_mir ( instance . def ) ;
97
+
98
+ let Some ( function_coverage_info ) = mir_body . function_coverage_info . as_deref ( ) else {
98
99
return ;
99
100
} ;
100
101
101
102
// If there are no MC/DC bitmaps to set up, return immediately.
102
103
if function_coverage_info. mcdc_bitmap_bits == 0 {
103
104
return ;
104
105
}
106
+ let is_mcdc_instrument = |statement : & Statement < ' _ > | {
107
+ let StatementKind :: Coverage ( coverage_kind) = & statement. kind else {
108
+ return false ;
109
+ } ;
110
+ matches ! (
111
+ coverage_kind,
112
+ CoverageKind :: TestVectorBitmapUpdate { .. }
113
+ | CoverageKind :: CondBitmapUpdate { .. }
114
+ | CoverageKind :: CondBitmapReset { .. }
115
+ )
116
+ } ;
117
+ // Mir bodies of instances like `DropGlue` might be cloned from other instances' and removed some basic blocks.
118
+ // Such instances likely have same `function_coverage_info` as primary instances but have no coverage statements.
119
+ // No need to initialize mcdc parameters for this kind of instances.
120
+ if !mir_body
121
+ . basic_blocks
122
+ . iter ( )
123
+ . map ( |bb| bb. statements . iter ( ) )
124
+ . flatten ( )
125
+ . any ( is_mcdc_instrument)
126
+ {
127
+ return ;
128
+ }
105
129
106
130
let fn_name = self . get_pgo_func_name_var ( instance) ;
107
131
let hash = self . const_u64 ( function_coverage_info. function_source_hash ) ;
0 commit comments