Skip to content

Commit 446e013

Browse files
author
zhuyunxing
committed
coverage. reduce generated mapping expressions
1 parent 435098f commit 446e013

File tree

6 files changed

+242
-233
lines changed

6 files changed

+242
-233
lines changed

compiler/rustc_middle/src/mir/coverage.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl Debug for CodeRegion {
187187
}
188188
}
189189

190-
#[derive(Copy, Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
190+
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
191191
#[derive(TypeFoldable, TypeVisitable)]
192192
pub enum Op {
193193
Subtract,
@@ -204,14 +204,27 @@ impl Op {
204204
}
205205
}
206206

207-
#[derive(Clone, Debug)]
207+
#[derive(Clone, Debug, Eq)]
208208
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
209209
pub struct Expression {
210210
pub lhs: CovTerm,
211211
pub op: Op,
212212
pub rhs: CovTerm,
213213
}
214214

215+
impl PartialEq for Expression {
216+
fn eq(&self, other: &Self) -> bool {
217+
match (self.op, other.op) {
218+
(Op::Add, Op::Add) => {
219+
(self.lhs == other.lhs && self.rhs == other.rhs)
220+
|| (self.lhs == other.rhs && self.rhs == other.lhs)
221+
}
222+
(Op::Subtract, Op::Subtract) => self.lhs == other.lhs && self.rhs == other.rhs,
223+
_ => false,
224+
}
225+
}
226+
}
227+
215228
#[derive(Clone, Debug)]
216229
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
217230
pub enum MappingKind {

compiler/rustc_mir_transform/src/coverage/counters.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub(super) struct CoverageCounters {
6262
/// corresponding operator (+ or -) and its LHS/RHS operands.
6363
expressions: IndexVec<ExpressionId, Expression>,
6464

65+
expression_reference: FxHashMap<Expression, BcbCounter>,
6566
/// Some terms referencing to composite expressions count sum execution times
6667
/// of several basic coverage blocks. Mostly such coverage terms are used by patterns including or pattern.
6768
/// Expressions for these terms should generate statements to some blocks in case
@@ -84,6 +85,7 @@ impl CoverageCounters {
8485
bcb_counters: IndexVec::from_elem_n(None, num_bcbs),
8586
bcb_edge_counters: FxHashMap::default(),
8687
expressions: IndexVec::new(),
88+
expression_reference: FxHashMap::default(),
8789
combined_bcb_expressions: BTreeMap::new(),
8890
};
8991

@@ -105,8 +107,37 @@ impl CoverageCounters {
105107
rhs: BcbCounter,
106108
) -> BcbCounter {
107109
let expression = Expression { lhs: lhs.as_term(), op, rhs: rhs.as_term() };
108-
let id = self.expressions.push(expression);
109-
BcbCounter::Expression { id }
110+
if let Some(counter) = self.expression_reference.get(&expression) {
111+
*counter
112+
} else {
113+
let counter = BcbCounter::Expression { id: self.expressions.push(expression.clone()) };
114+
self.expression_reference.insert(expression, counter);
115+
// Later branches of pattern matching might try to make expression with C2 - (C2 - C1), (C2 - C1) + C1
116+
match (lhs, op, rhs) {
117+
(BcbCounter::Counter { .. }, Op::Add, BcbCounter::Counter { .. }) => {
118+
self.expression_reference.insert(
119+
Expression { lhs: counter.as_term(), op: Op::Subtract, rhs: lhs.as_term() },
120+
rhs,
121+
);
122+
self.expression_reference.insert(
123+
Expression { lhs: counter.as_term(), op: Op::Subtract, rhs: rhs.as_term() },
124+
lhs,
125+
);
126+
}
127+
(BcbCounter::Counter { .. }, Op::Subtract, BcbCounter::Counter { .. }) => {
128+
self.expression_reference.insert(
129+
Expression { lhs: counter.as_term(), op: Op::Add, rhs: rhs.as_term() },
130+
lhs,
131+
);
132+
self.expression_reference.insert(
133+
Expression { lhs: lhs.as_term(), op: Op::Subtract, rhs: counter.as_term() },
134+
rhs,
135+
);
136+
}
137+
_ => {}
138+
}
139+
counter
140+
}
110141
}
111142

112143
/// Variant of `make_expression` that makes `lhs` optional and assumes [`Op::Add`].

0 commit comments

Comments
 (0)