Skip to content

Commit 3002284

Browse files
committed
coverage: Hoist the removal of unwanted macro expansion spans
1 parent 8e34642 commit 3002284

File tree

3 files changed

+63
-76
lines changed

3 files changed

+63
-76
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -292,32 +292,9 @@ impl<'a> CoverageSpansGenerator<'a> {
292292
} else if curr.is_closure {
293293
self.carve_out_span_for_closure();
294294
} else if self.prev_original_span == curr.span {
295-
// Note that this compares the new (`curr`) span to `prev_original_span`.
296-
// In this branch, the actual span byte range of `prev_original_span` is not
297-
// important. What is important is knowing whether the new `curr` span was
298-
// **originally** the same as the original span of `prev()`. The original spans
299-
// reflect their original sort order, and for equal spans, conveys a partial
300-
// ordering based on CFG dominator priority.
301-
if prev.is_macro_expansion() && curr.is_macro_expansion() {
302-
// Macros that expand to include branching (such as
303-
// `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
304-
// `trace!()`) typically generate callee spans with identical
305-
// ranges (typically the full span of the macro) for all
306-
// `BasicBlocks`. This makes it impossible to distinguish
307-
// the condition (`if val1 != val2`) from the optional
308-
// branched statements (such as the call to `panic!()` on
309-
// assert failure). In this case it is better (or less
310-
// worse) to drop the optional branch bcbs and keep the
311-
// non-conditional statements, to count when reached.
312-
debug!(
313-
" curr and prev are part of a macro expansion, and curr has the same span \
314-
as prev, but is in a different bcb. Drop curr and keep prev for next iter. \
315-
prev={prev:?}",
316-
);
317-
self.take_curr(); // Discards curr.
318-
} else {
319-
self.update_pending_dups();
320-
}
295+
// `prev` and `curr` have the same span, or would have had the
296+
// same span before `prev` was modified by other spans.
297+
self.update_pending_dups();
321298
} else {
322299
self.cutoff_prev_at_overlapping_curr();
323300
self.maybe_push_macro_name_span();

compiler/rustc_mir_transform/src/coverage/spans/from_mir.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_data_structures::captures::Captures;
2+
use rustc_data_structures::fx::FxHashSet;
23
use rustc_middle::mir::{
34
self, AggregateKind, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
45
TerminatorKind,
@@ -36,6 +37,9 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
3637

3738
initial_spans.push(CoverageSpan::for_fn_sig(fn_sig_span));
3839

40+
initial_spans.sort_by(|a, b| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb));
41+
remove_unwanted_macro_spans(&mut initial_spans);
42+
3943
initial_spans.sort_by(|a, b| {
4044
// First sort by span start.
4145
Ord::cmp(&a.span.lo(), &b.span.lo())
@@ -56,6 +60,26 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
5660
initial_spans
5761
}
5862

63+
/// Macros that expand into branches (e.g. `assert!`, `trace!`) tend to generate
64+
/// multiple condition/consequent blocks that have the span of the whole macro
65+
/// invocation, which is unhelpful. Keeping only the first such span seems to
66+
/// give better mappings, so remove the others.
67+
///
68+
/// (The input spans should be sorted in BCB dominator order, so that the
69+
/// retained "first" span is likely to dominate the others.)
70+
fn remove_unwanted_macro_spans(initial_spans: &mut Vec<CoverageSpan>) {
71+
let mut seen_spans = FxHashSet::default();
72+
initial_spans.retain(|covspan| {
73+
// Ignore (retain) closure spans and non-macro-expansion spans.
74+
if covspan.is_closure || !covspan.is_macro_expansion() {
75+
return true;
76+
}
77+
78+
// Retain only the first macro-expanded covspan with this span.
79+
seen_spans.insert(covspan.span)
80+
});
81+
}
82+
5983
// Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of
6084
// the `BasicBlock`(s) in the given `BasicCoverageBlockData`. One `CoverageSpan` is generated
6185
// for each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will

tests/coverage/closure.cov-map

+36-50
Original file line numberDiff line numberDiff line change
@@ -81,77 +81,63 @@ Number of file 0 mappings: 1
8181
- Code(Zero) at (prev + 171, 13) to (start + 2, 14)
8282

8383
Function name: closure::main::{closure#14}
84-
Raw bytes (36): 0x[01, 01, 03, 05, 0a, 01, 05, 01, 05, 05, 03, b2, 01, 0d, 00, 15, 01, 01, 11, 01, 1b, 05, 01, 1e, 00, 25, 0a, 00, 2f, 00, 33, 03, 01, 0d, 00, 0e]
84+
Raw bytes (29): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, b2, 01, 0d, 02, 1b, 05, 02, 1e, 00, 25, 02, 00, 2f, 00, 33, 07, 01, 0d, 00, 0e]
8585
Number of files: 1
8686
- file 0 => global file 1
87-
Number of expressions: 3
88-
- expression 0 operands: lhs = Counter(1), rhs = Expression(2, Sub)
89-
- expression 1 operands: lhs = Counter(0), rhs = Counter(1)
90-
- expression 2 operands: lhs = Counter(0), rhs = Counter(1)
91-
Number of file 0 mappings: 5
92-
- Code(Expression(0, Add)) at (prev + 178, 13) to (start + 0, 21)
93-
= (c1 + (c0 - c1))
94-
- Code(Counter(0)) at (prev + 1, 17) to (start + 1, 27)
95-
- Code(Counter(1)) at (prev + 1, 30) to (start + 0, 37)
96-
- Code(Expression(2, Sub)) at (prev + 0, 47) to (start + 0, 51)
87+
Number of expressions: 2
88+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
89+
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
90+
Number of file 0 mappings: 4
91+
- Code(Counter(0)) at (prev + 178, 13) to (start + 2, 27)
92+
- Code(Counter(1)) at (prev + 2, 30) to (start + 0, 37)
93+
- Code(Expression(0, Sub)) at (prev + 0, 47) to (start + 0, 51)
9794
= (c0 - c1)
98-
- Code(Expression(0, Add)) at (prev + 1, 13) to (start + 0, 14)
95+
- Code(Expression(1, Add)) at (prev + 1, 13) to (start + 0, 14)
9996
= (c1 + (c0 - c1))
10097

10198
Function name: closure::main::{closure#15}
102-
Raw bytes (41): 0x[01, 01, 03, 05, 0a, 01, 05, 01, 05, 06, 01, ba, 01, 09, 00, 0a, 03, 01, 0d, 00, 15, 01, 01, 11, 01, 1b, 05, 01, 1e, 00, 25, 0a, 00, 2f, 00, 33, 03, 02, 09, 00, 0a]
99+
Raw bytes (29): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, ba, 01, 09, 03, 1b, 05, 03, 1e, 00, 25, 02, 00, 2f, 00, 33, 07, 02, 09, 00, 0a]
103100
Number of files: 1
104101
- file 0 => global file 1
105-
Number of expressions: 3
106-
- expression 0 operands: lhs = Counter(1), rhs = Expression(2, Sub)
107-
- expression 1 operands: lhs = Counter(0), rhs = Counter(1)
108-
- expression 2 operands: lhs = Counter(0), rhs = Counter(1)
109-
Number of file 0 mappings: 6
110-
- Code(Counter(0)) at (prev + 186, 9) to (start + 0, 10)
111-
- Code(Expression(0, Add)) at (prev + 1, 13) to (start + 0, 21)
112-
= (c1 + (c0 - c1))
113-
- Code(Counter(0)) at (prev + 1, 17) to (start + 1, 27)
114-
- Code(Counter(1)) at (prev + 1, 30) to (start + 0, 37)
115-
- Code(Expression(2, Sub)) at (prev + 0, 47) to (start + 0, 51)
102+
Number of expressions: 2
103+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
104+
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
105+
Number of file 0 mappings: 4
106+
- Code(Counter(0)) at (prev + 186, 9) to (start + 3, 27)
107+
- Code(Counter(1)) at (prev + 3, 30) to (start + 0, 37)
108+
- Code(Expression(0, Sub)) at (prev + 0, 47) to (start + 0, 51)
116109
= (c0 - c1)
117-
- Code(Expression(0, Add)) at (prev + 2, 9) to (start + 0, 10)
110+
- Code(Expression(1, Add)) at (prev + 2, 9) to (start + 0, 10)
118111
= (c1 + (c0 - c1))
119112

120113
Function name: closure::main::{closure#16}
121-
Raw bytes (36): 0x[01, 01, 03, 05, 0a, 01, 05, 01, 05, 05, 03, c4, 01, 0d, 00, 15, 01, 01, 11, 01, 1b, 05, 01, 1e, 00, 25, 0a, 00, 2f, 00, 33, 03, 01, 0d, 00, 0e]
114+
Raw bytes (29): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, c4, 01, 0d, 02, 1b, 05, 02, 1e, 00, 25, 02, 00, 2f, 00, 33, 07, 01, 0d, 00, 0e]
122115
Number of files: 1
123116
- file 0 => global file 1
124-
Number of expressions: 3
125-
- expression 0 operands: lhs = Counter(1), rhs = Expression(2, Sub)
126-
- expression 1 operands: lhs = Counter(0), rhs = Counter(1)
127-
- expression 2 operands: lhs = Counter(0), rhs = Counter(1)
128-
Number of file 0 mappings: 5
129-
- Code(Expression(0, Add)) at (prev + 196, 13) to (start + 0, 21)
130-
= (c1 + (c0 - c1))
131-
- Code(Counter(0)) at (prev + 1, 17) to (start + 1, 27)
132-
- Code(Counter(1)) at (prev + 1, 30) to (start + 0, 37)
133-
- Code(Expression(2, Sub)) at (prev + 0, 47) to (start + 0, 51)
117+
Number of expressions: 2
118+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
119+
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
120+
Number of file 0 mappings: 4
121+
- Code(Counter(0)) at (prev + 196, 13) to (start + 2, 27)
122+
- Code(Counter(1)) at (prev + 2, 30) to (start + 0, 37)
123+
- Code(Expression(0, Sub)) at (prev + 0, 47) to (start + 0, 51)
134124
= (c0 - c1)
135-
- Code(Expression(0, Add)) at (prev + 1, 13) to (start + 0, 14)
125+
- Code(Expression(1, Add)) at (prev + 1, 13) to (start + 0, 14)
136126
= (c1 + (c0 - c1))
137127

138128
Function name: closure::main::{closure#17}
139-
Raw bytes (41): 0x[01, 01, 03, 05, 0a, 01, 05, 01, 05, 06, 01, cc, 01, 09, 00, 0a, 03, 01, 0d, 00, 15, 01, 01, 11, 01, 1b, 05, 01, 1e, 00, 25, 0a, 00, 2f, 00, 33, 03, 02, 09, 00, 0a]
129+
Raw bytes (29): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, cc, 01, 09, 03, 1b, 05, 03, 1e, 00, 25, 02, 00, 2f, 00, 33, 07, 02, 09, 00, 0a]
140130
Number of files: 1
141131
- file 0 => global file 1
142-
Number of expressions: 3
143-
- expression 0 operands: lhs = Counter(1), rhs = Expression(2, Sub)
144-
- expression 1 operands: lhs = Counter(0), rhs = Counter(1)
145-
- expression 2 operands: lhs = Counter(0), rhs = Counter(1)
146-
Number of file 0 mappings: 6
147-
- Code(Counter(0)) at (prev + 204, 9) to (start + 0, 10)
148-
- Code(Expression(0, Add)) at (prev + 1, 13) to (start + 0, 21)
149-
= (c1 + (c0 - c1))
150-
- Code(Counter(0)) at (prev + 1, 17) to (start + 1, 27)
151-
- Code(Counter(1)) at (prev + 1, 30) to (start + 0, 37)
152-
- Code(Expression(2, Sub)) at (prev + 0, 47) to (start + 0, 51)
132+
Number of expressions: 2
133+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
134+
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
135+
Number of file 0 mappings: 4
136+
- Code(Counter(0)) at (prev + 204, 9) to (start + 3, 27)
137+
- Code(Counter(1)) at (prev + 3, 30) to (start + 0, 37)
138+
- Code(Expression(0, Sub)) at (prev + 0, 47) to (start + 0, 51)
153139
= (c0 - c1)
154-
- Code(Expression(0, Add)) at (prev + 2, 9) to (start + 0, 10)
140+
- Code(Expression(1, Add)) at (prev + 2, 9) to (start + 0, 10)
155141
= (c1 + (c0 - c1))
156142

157143
Function name: closure::main::{closure#18}

0 commit comments

Comments
 (0)