1
- use super :: Error ;
2
-
3
1
use super :: graph;
4
2
5
3
use graph:: { BasicCoverageBlock , BcbBranch , CoverageGraph , TraverseCoverageGraphWithLoops } ;
@@ -53,7 +51,7 @@ pub(super) struct CoverageCounters {
53
51
/// edge between two BCBs.
54
52
bcb_edge_counters : FxHashMap < ( BasicCoverageBlock , BasicCoverageBlock ) , BcbCounter > ,
55
53
/// Tracks which BCBs have a counter associated with some incoming edge.
56
- /// Only used by debug assertions, to verify that BCBs with incoming edge
54
+ /// Only used by assertions, to verify that BCBs with incoming edge
57
55
/// counters do not have their own physical counters (expressions are allowed).
58
56
bcb_has_incoming_edge_counters : BitSet < BasicCoverageBlock > ,
59
57
/// Table of expression data, associating each expression ID with its
@@ -81,7 +79,7 @@ impl CoverageCounters {
81
79
& mut self ,
82
80
basic_coverage_blocks : & CoverageGraph ,
83
81
bcb_has_coverage_spans : impl Fn ( BasicCoverageBlock ) -> bool ,
84
- ) -> Result < ( ) , Error > {
82
+ ) {
85
83
MakeBcbCounters :: new ( self , basic_coverage_blocks) . make_bcb_counters ( bcb_has_coverage_spans)
86
84
}
87
85
@@ -111,26 +109,23 @@ impl CoverageCounters {
111
109
self . expressions . len ( )
112
110
}
113
111
114
- fn set_bcb_counter (
115
- & mut self ,
116
- bcb : BasicCoverageBlock ,
117
- counter_kind : BcbCounter ,
118
- ) -> Result < CovTerm , Error > {
119
- debug_assert ! (
112
+ fn set_bcb_counter ( & mut self , bcb : BasicCoverageBlock , counter_kind : BcbCounter ) -> CovTerm {
113
+ assert ! (
120
114
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
121
115
// have an expression (to be injected into an existing `BasicBlock` represented by this
122
116
// `BasicCoverageBlock`).
123
117
counter_kind. is_expression( ) || !self . bcb_has_incoming_edge_counters. contains( bcb) ,
124
118
"attempt to add a `Counter` to a BCB target with existing incoming edge counters"
125
119
) ;
120
+
126
121
let term = counter_kind. as_term ( ) ;
127
122
if let Some ( replaced) = self . bcb_counters [ bcb] . replace ( counter_kind) {
128
- Error :: from_string ( format ! (
123
+ bug ! (
129
124
"attempt to set a BasicCoverageBlock coverage counter more than once; \
130
125
{bcb:?} already had counter {replaced:?}",
131
- ) )
126
+ ) ;
132
127
} else {
133
- Ok ( term)
128
+ term
134
129
}
135
130
}
136
131
@@ -139,27 +134,26 @@ impl CoverageCounters {
139
134
from_bcb : BasicCoverageBlock ,
140
135
to_bcb : BasicCoverageBlock ,
141
136
counter_kind : BcbCounter ,
142
- ) -> Result < CovTerm , Error > {
143
- if level_enabled ! ( tracing:: Level :: DEBUG ) {
144
- // If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
145
- // have an expression (to be injected into an existing `BasicBlock` represented by this
146
- // `BasicCoverageBlock`).
147
- if self . bcb_counter ( to_bcb) . is_some_and ( |c| !c. is_expression ( ) ) {
148
- return Error :: from_string ( format ! (
149
- "attempt to add an incoming edge counter from {from_bcb:?} when the target BCB already \
150
- has a `Counter`"
151
- ) ) ;
152
- }
137
+ ) -> CovTerm {
138
+ // If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
139
+ // have an expression (to be injected into an existing `BasicBlock` represented by this
140
+ // `BasicCoverageBlock`).
141
+ if let Some ( node_counter) = self . bcb_counter ( to_bcb) && !node_counter. is_expression ( ) {
142
+ bug ! (
143
+ "attempt to add an incoming edge counter from {from_bcb:?} \
144
+ when the target BCB already has {node_counter:?}"
145
+ ) ;
153
146
}
147
+
154
148
self . bcb_has_incoming_edge_counters . insert ( to_bcb) ;
155
149
let term = counter_kind. as_term ( ) ;
156
150
if let Some ( replaced) = self . bcb_edge_counters . insert ( ( from_bcb, to_bcb) , counter_kind) {
157
- Error :: from_string ( format ! (
151
+ bug ! (
158
152
"attempt to set an edge counter more than once; from_bcb: \
159
153
{from_bcb:?} already had counter {replaced:?}",
160
- ) )
154
+ ) ;
161
155
} else {
162
- Ok ( term)
156
+ term
163
157
}
164
158
}
165
159
@@ -213,14 +207,7 @@ impl<'a> MakeBcbCounters<'a> {
213
207
/// One way to predict which branch executes the least is by considering loops. A loop is exited
214
208
/// at a branch, so the branch that jumps to a `BasicCoverageBlock` outside the loop is almost
215
209
/// always executed less than the branch that does not exit the loop.
216
- ///
217
- /// Returns any non-code-span expressions created to represent intermediate values (such as to
218
- /// add two counters so the result can be subtracted from another counter), or an Error with
219
- /// message for subsequent debugging.
220
- fn make_bcb_counters (
221
- & mut self ,
222
- bcb_has_coverage_spans : impl Fn ( BasicCoverageBlock ) -> bool ,
223
- ) -> Result < ( ) , Error > {
210
+ fn make_bcb_counters ( & mut self , bcb_has_coverage_spans : impl Fn ( BasicCoverageBlock ) -> bool ) {
224
211
debug ! ( "make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock" ) ;
225
212
226
213
// Walk the `CoverageGraph`. For each `BasicCoverageBlock` node with an associated
@@ -237,10 +224,10 @@ impl<'a> MakeBcbCounters<'a> {
237
224
while let Some ( bcb) = traversal. next ( ) {
238
225
if bcb_has_coverage_spans ( bcb) {
239
226
debug ! ( "{:?} has at least one coverage span. Get or make its counter" , bcb) ;
240
- let branching_counter_operand = self . get_or_make_counter_operand ( bcb) ? ;
227
+ let branching_counter_operand = self . get_or_make_counter_operand ( bcb) ;
241
228
242
229
if self . bcb_needs_branch_counters ( bcb) {
243
- self . make_branch_counters ( & traversal, bcb, branching_counter_operand) ? ;
230
+ self . make_branch_counters ( & traversal, bcb, branching_counter_operand) ;
244
231
}
245
232
} else {
246
233
debug ! (
@@ -251,22 +238,19 @@ impl<'a> MakeBcbCounters<'a> {
251
238
}
252
239
}
253
240
254
- if traversal. is_complete ( ) {
255
- Ok ( ( ) )
256
- } else {
257
- Error :: from_string ( format ! (
258
- "`TraverseCoverageGraphWithLoops` missed some `BasicCoverageBlock`s: {:?}" ,
259
- traversal. unvisited( ) ,
260
- ) )
261
- }
241
+ assert ! (
242
+ traversal. is_complete( ) ,
243
+ "`TraverseCoverageGraphWithLoops` missed some `BasicCoverageBlock`s: {:?}" ,
244
+ traversal. unvisited( ) ,
245
+ ) ;
262
246
}
263
247
264
248
fn make_branch_counters (
265
249
& mut self ,
266
250
traversal : & TraverseCoverageGraphWithLoops < ' _ > ,
267
251
branching_bcb : BasicCoverageBlock ,
268
252
branching_counter_operand : CovTerm ,
269
- ) -> Result < ( ) , Error > {
253
+ ) {
270
254
let branches = self . bcb_branches ( branching_bcb) ;
271
255
debug ! (
272
256
"{:?} has some branch(es) without counters:\n {}" ,
@@ -299,10 +283,10 @@ impl<'a> MakeBcbCounters<'a> {
299
283
counter",
300
284
branch, branching_bcb
301
285
) ;
302
- self . get_or_make_counter_operand ( branch. target_bcb ) ?
286
+ self . get_or_make_counter_operand ( branch. target_bcb )
303
287
} else {
304
288
debug ! ( " {:?} has multiple incoming edges, so adding an edge counter" , branch) ;
305
- self . get_or_make_edge_counter_operand ( branching_bcb, branch. target_bcb ) ?
289
+ self . get_or_make_edge_counter_operand ( branching_bcb, branch. target_bcb )
306
290
} ;
307
291
if let Some ( sumup_counter_operand) =
308
292
some_sumup_counter_operand. replace ( branch_counter_operand)
@@ -337,19 +321,18 @@ impl<'a> MakeBcbCounters<'a> {
337
321
debug ! ( "{:?} gets an expression: {:?}" , expression_branch, expression) ;
338
322
let bcb = expression_branch. target_bcb ;
339
323
if expression_branch. is_only_path_to_target ( ) {
340
- self . coverage_counters . set_bcb_counter ( bcb, expression) ? ;
324
+ self . coverage_counters . set_bcb_counter ( bcb, expression) ;
341
325
} else {
342
- self . coverage_counters . set_bcb_edge_counter ( branching_bcb, bcb, expression) ? ;
326
+ self . coverage_counters . set_bcb_edge_counter ( branching_bcb, bcb, expression) ;
343
327
}
344
- Ok ( ( ) )
345
328
}
346
329
347
330
#[ instrument( level = "debug" , skip( self ) ) ]
348
- fn get_or_make_counter_operand ( & mut self , bcb : BasicCoverageBlock ) -> Result < CovTerm , Error > {
331
+ fn get_or_make_counter_operand ( & mut self , bcb : BasicCoverageBlock ) -> CovTerm {
349
332
// If the BCB already has a counter, return it.
350
333
if let Some ( counter_kind) = & self . coverage_counters . bcb_counters [ bcb] {
351
334
debug ! ( "{bcb:?} already has a counter: {counter_kind:?}" ) ;
352
- return Ok ( counter_kind. as_term ( ) ) ;
335
+ return counter_kind. as_term ( ) ;
353
336
}
354
337
355
338
// A BCB with only one incoming edge gets a simple `Counter` (via `make_counter()`).
@@ -378,10 +361,10 @@ impl<'a> MakeBcbCounters<'a> {
378
361
379
362
let mut predecessors = self . bcb_predecessors ( bcb) . to_owned ( ) . into_iter ( ) ;
380
363
let first_edge_counter_operand =
381
- self . get_or_make_edge_counter_operand ( predecessors. next ( ) . unwrap ( ) , bcb) ? ;
364
+ self . get_or_make_edge_counter_operand ( predecessors. next ( ) . unwrap ( ) , bcb) ;
382
365
let mut some_sumup_edge_counter_operand = None ;
383
366
for predecessor in predecessors {
384
- let edge_counter_operand = self . get_or_make_edge_counter_operand ( predecessor, bcb) ? ;
367
+ let edge_counter_operand = self . get_or_make_edge_counter_operand ( predecessor, bcb) ;
385
368
if let Some ( sumup_edge_counter_operand) =
386
369
some_sumup_edge_counter_operand. replace ( edge_counter_operand)
387
370
{
@@ -411,7 +394,7 @@ impl<'a> MakeBcbCounters<'a> {
411
394
& mut self ,
412
395
from_bcb : BasicCoverageBlock ,
413
396
to_bcb : BasicCoverageBlock ,
414
- ) -> Result < CovTerm , Error > {
397
+ ) -> CovTerm {
415
398
// If the source BCB has only one successor (assumed to be the given target), an edge
416
399
// counter is unnecessary. Just get or make a counter for the source BCB.
417
400
let successors = self . bcb_successors ( from_bcb) . iter ( ) ;
@@ -424,7 +407,7 @@ impl<'a> MakeBcbCounters<'a> {
424
407
self . coverage_counters . bcb_edge_counters . get ( & ( from_bcb, to_bcb) )
425
408
{
426
409
debug ! ( "Edge {from_bcb:?}->{to_bcb:?} already has a counter: {counter_kind:?}" ) ;
427
- return Ok ( counter_kind. as_term ( ) ) ;
410
+ return counter_kind. as_term ( ) ;
428
411
}
429
412
430
413
// Make a new counter to count this edge.
0 commit comments