@@ -7,6 +7,7 @@ use rustc_codegen_ssa::traits::{
7
7
} ;
8
8
use rustc_data_structures:: fx:: { FxHashMap , FxIndexSet } ;
9
9
use rustc_middle:: mir:: coverage:: CoverageKind ;
10
+ use rustc_middle:: mir:: { Statement , StatementKind } ;
10
11
use rustc_middle:: ty:: Instance ;
11
12
use tracing:: { debug, instrument} ;
12
13
@@ -91,9 +92,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
91
92
92
93
impl < ' tcx > CoverageInfoBuilderMethods < ' tcx > for Builder < ' _ , ' _ , ' tcx > {
93
94
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 {
97
97
return ;
98
98
} ;
99
99
@@ -102,6 +102,25 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
102
102
return ;
103
103
}
104
104
105
+ let is_counter_instrument = |statement : & Statement < ' _ > | {
106
+ let StatementKind :: Coverage ( cov_kind) = & statement. kind else {
107
+ return false ;
108
+ } ;
109
+ matches ! ( cov_kind, CoverageKind :: CounterIncrement { .. } )
110
+ } ;
111
+ // Some instances like `DropGlue` clone mir body from others and modified the basic blocks after coverage pass.
112
+ // Such instances might have same `function_coverage_info` as the primary but have no associated coverage statement.
113
+ // Ignore these instances here.
114
+ if !mir_body
115
+ . basic_blocks
116
+ . iter ( )
117
+ . map ( |bb| & bb. statements )
118
+ . flatten ( )
119
+ . any ( is_counter_instrument)
120
+ {
121
+ return ;
122
+ }
123
+
105
124
let fn_name = self . get_pgo_func_name_var ( instance) ;
106
125
let hash = self . const_u64 ( function_coverage_info. function_source_hash ) ;
107
126
let bitmap_bits = self . const_u32 ( function_coverage_info. mcdc_bitmap_bits as u32 ) ;
0 commit comments