@@ -7,13 +7,9 @@ mod spans;
77#[ cfg( test) ]
88mod tests;
99
10- use self :: counters:: { CounterIncrementSite , CoverageCounters } ;
11- use self :: graph:: { BasicCoverageBlock , CoverageGraph } ;
12- use self :: mappings:: CoverageSpans ;
13-
14- use crate :: MirPass ;
15-
16- use rustc_middle:: mir:: coverage:: * ;
10+ use rustc_middle:: mir:: coverage:: {
11+ CodeRegion , CoverageKind , DecisionInfo , FunctionCoverageInfo , Mapping , MappingKind ,
12+ } ;
1713use rustc_middle:: mir:: {
1814 self , BasicBlock , BasicBlockData , SourceInfo , Statement , StatementKind , Terminator ,
1915 TerminatorKind ,
@@ -23,6 +19,11 @@ use rustc_span::def_id::LocalDefId;
2319use rustc_span:: source_map:: SourceMap ;
2420use rustc_span:: { BytePos , Pos , RelativeBytePos , Span , Symbol } ;
2521
22+ use crate :: coverage:: counters:: { CounterIncrementSite , CoverageCounters } ;
23+ use crate :: coverage:: graph:: { BasicCoverageBlock , CoverageGraph } ;
24+ use crate :: coverage:: mappings:: ExtractedMappings ;
25+ use crate :: MirPass ;
26+
2627/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
2728/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
2829/// to construct the coverage map.
@@ -69,24 +70,27 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
6970 let basic_coverage_blocks = CoverageGraph :: from_mir ( mir_body) ;
7071
7172 ////////////////////////////////////////////////////
72- // Compute coverage spans from the `CoverageGraph`.
73- let Some ( coverage_spans) =
74- mappings:: generate_coverage_spans ( mir_body, & hir_info, & basic_coverage_blocks)
75- else {
76- // No relevant spans were found in MIR, so skip instrumenting this function.
77- return ;
78- } ;
73+ // Extract coverage spans and other mapping info from MIR.
74+ let extracted_mappings =
75+ mappings:: extract_all_mapping_info_from_mir ( mir_body, & hir_info, & basic_coverage_blocks) ;
7976
8077 ////////////////////////////////////////////////////
8178 // Create an optimized mix of `Counter`s and `Expression`s for the `CoverageGraph`. Ensure
8279 // every coverage span has a `Counter` or `Expression` assigned to its `BasicCoverageBlock`
8380 // and all `Expression` dependencies (operands) are also generated, for any other
8481 // `BasicCoverageBlock`s not already associated with a coverage span.
85- let bcb_has_coverage_spans = |bcb| coverage_spans. bcb_has_coverage_spans ( bcb) ;
82+ let bcbs_with_counter_mappings =
83+ extracted_mappings. all_bcbs_with_counter_mappings ( & basic_coverage_blocks) ;
84+ if bcbs_with_counter_mappings. is_empty ( ) {
85+ // No relevant spans were found in MIR, so skip instrumenting this function.
86+ return ;
87+ }
88+
89+ let bcb_has_counter_mappings = |bcb| bcbs_with_counter_mappings. contains ( bcb) ;
8690 let coverage_counters =
87- CoverageCounters :: make_bcb_counters ( & basic_coverage_blocks, bcb_has_coverage_spans ) ;
91+ CoverageCounters :: make_bcb_counters ( & basic_coverage_blocks, bcb_has_counter_mappings ) ;
8892
89- let mappings = create_mappings ( tcx, & hir_info, & coverage_spans , & coverage_counters) ;
93+ let mappings = create_mappings ( tcx, & hir_info, & extracted_mappings , & coverage_counters) ;
9094 if mappings. is_empty ( ) {
9195 // No spans could be converted into valid mappings, so skip this function.
9296 debug ! ( "no spans could be converted into valid mappings; skipping" ) ;
@@ -96,13 +100,13 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
96100 inject_coverage_statements (
97101 mir_body,
98102 & basic_coverage_blocks,
99- bcb_has_coverage_spans ,
103+ bcb_has_counter_mappings ,
100104 & coverage_counters,
101105 ) ;
102106
103- inject_mcdc_statements ( mir_body, & basic_coverage_blocks, & coverage_spans ) ;
107+ inject_mcdc_statements ( mir_body, & basic_coverage_blocks, & extracted_mappings ) ;
104108
105- let mcdc_num_condition_bitmaps = coverage_spans
109+ let mcdc_num_condition_bitmaps = extracted_mappings
106110 . mcdc_decisions
107111 . iter ( )
108112 . map ( |& mappings:: MCDCDecision { decision_depth, .. } | decision_depth)
@@ -112,7 +116,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
112116 mir_body. function_coverage_info = Some ( Box :: new ( FunctionCoverageInfo {
113117 function_source_hash : hir_info. function_source_hash ,
114118 num_counters : coverage_counters. num_counters ( ) ,
115- mcdc_bitmap_bytes : coverage_spans . test_vector_bitmap_bytes ( ) ,
119+ mcdc_bitmap_bytes : extracted_mappings . mcdc_bitmap_bytes ,
116120 expressions : coverage_counters. into_expressions ( ) ,
117121 mappings,
118122 mcdc_num_condition_bitmaps,
@@ -127,7 +131,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
127131fn create_mappings < ' tcx > (
128132 tcx : TyCtxt < ' tcx > ,
129133 hir_info : & ExtractedHirInfo ,
130- coverage_spans : & CoverageSpans ,
134+ extracted_mappings : & ExtractedMappings ,
131135 coverage_counters : & CoverageCounters ,
132136) -> Vec < Mapping > {
133137 let source_map = tcx. sess . source_map ( ) ;
@@ -148,17 +152,26 @@ fn create_mappings<'tcx>(
148152 } ;
149153 let region_for_span = |span : Span | make_code_region ( source_map, file_name, span, body_span) ;
150154
155+ // Fully destructure the mappings struct to make sure we don't miss any kinds.
156+ let ExtractedMappings {
157+ code_mappings,
158+ branch_pairs,
159+ mcdc_bitmap_bytes : _,
160+ mcdc_branches,
161+ mcdc_decisions,
162+ } = extracted_mappings;
151163 let mut mappings = Vec :: new ( ) ;
152164
153- mappings. extend ( coverage_spans. code_mappings . iter ( ) . filter_map (
165+ mappings. extend ( code_mappings. iter ( ) . filter_map (
166+ // Ordinary code mappings are the simplest kind.
154167 |& mappings:: CodeMapping { span, bcb } | {
155168 let code_region = region_for_span ( span) ?;
156169 let kind = MappingKind :: Code ( term_for_bcb ( bcb) ) ;
157170 Some ( Mapping { kind, code_region } )
158171 } ,
159172 ) ) ;
160173
161- mappings. extend ( coverage_spans . branch_pairs . iter ( ) . filter_map (
174+ mappings. extend ( branch_pairs. iter ( ) . filter_map (
162175 |& mappings:: BranchPair { span, true_bcb, false_bcb } | {
163176 let true_term = term_for_bcb ( true_bcb) ;
164177 let false_term = term_for_bcb ( false_bcb) ;
@@ -168,7 +181,7 @@ fn create_mappings<'tcx>(
168181 } ,
169182 ) ) ;
170183
171- mappings. extend ( coverage_spans . mcdc_branches . iter ( ) . filter_map (
184+ mappings. extend ( mcdc_branches. iter ( ) . filter_map (
172185 |& mappings:: MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth : _ } | {
173186 let code_region = region_for_span ( span) ?;
174187 let true_term = term_for_bcb ( true_bcb) ;
@@ -181,7 +194,7 @@ fn create_mappings<'tcx>(
181194 } ,
182195 ) ) ;
183196
184- mappings. extend ( coverage_spans . mcdc_decisions . iter ( ) . filter_map (
197+ mappings. extend ( mcdc_decisions. iter ( ) . filter_map (
185198 |& mappings:: MCDCDecision { span, bitmap_idx, conditions_num, .. } | {
186199 let code_region = region_for_span ( span) ?;
187200 let kind = MappingKind :: MCDCDecision ( DecisionInfo { bitmap_idx, conditions_num } ) ;
@@ -249,20 +262,16 @@ fn inject_coverage_statements<'tcx>(
249262fn inject_mcdc_statements < ' tcx > (
250263 mir_body : & mut mir:: Body < ' tcx > ,
251264 basic_coverage_blocks : & CoverageGraph ,
252- coverage_spans : & CoverageSpans ,
265+ extracted_mappings : & ExtractedMappings ,
253266) {
254- if coverage_spans. test_vector_bitmap_bytes ( ) == 0 {
255- return ;
256- }
257-
258267 // Inject test vector update first because `inject_statement` always insert new statement at head.
259268 for & mappings:: MCDCDecision {
260269 span : _,
261270 ref end_bcbs,
262271 bitmap_idx,
263272 conditions_num : _,
264273 decision_depth,
265- } in & coverage_spans . mcdc_decisions
274+ } in & extracted_mappings . mcdc_decisions
266275 {
267276 for end in end_bcbs {
268277 let end_bb = basic_coverage_blocks[ * end] . leader_bb ( ) ;
@@ -275,7 +284,7 @@ fn inject_mcdc_statements<'tcx>(
275284 }
276285
277286 for & mappings:: MCDCBranch { span : _, true_bcb, false_bcb, condition_info, decision_depth } in
278- & coverage_spans . mcdc_branches
287+ & extracted_mappings . mcdc_branches
279288 {
280289 let Some ( condition_info) = condition_info else { continue } ;
281290 let id = condition_info. condition_id ;
0 commit comments