Skip to content

Commit

Permalink
coverage. reduce generated mapping expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuyunxing committed May 13, 2024
1 parent 435098f commit 446e013
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 233 deletions.
17 changes: 15 additions & 2 deletions compiler/rustc_middle/src/mir/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl Debug for CodeRegion {
}
}

#[derive(Copy, Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
#[derive(TypeFoldable, TypeVisitable)]
pub enum Op {
Subtract,
Expand All @@ -204,14 +204,27 @@ impl Op {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq)]
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
pub struct Expression {
pub lhs: CovTerm,
pub op: Op,
pub rhs: CovTerm,
}

impl PartialEq for Expression {
fn eq(&self, other: &Self) -> bool {
match (self.op, other.op) {
(Op::Add, Op::Add) => {
(self.lhs == other.lhs && self.rhs == other.rhs)
|| (self.lhs == other.rhs && self.rhs == other.lhs)
}
(Op::Subtract, Op::Subtract) => self.lhs == other.lhs && self.rhs == other.rhs,
_ => false,
}
}
}

#[derive(Clone, Debug)]
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
pub enum MappingKind {
Expand Down
35 changes: 33 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub(super) struct CoverageCounters {
/// corresponding operator (+ or -) and its LHS/RHS operands.
expressions: IndexVec<ExpressionId, Expression>,

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

Expand All @@ -105,8 +107,37 @@ impl CoverageCounters {
rhs: BcbCounter,
) -> BcbCounter {
let expression = Expression { lhs: lhs.as_term(), op, rhs: rhs.as_term() };
let id = self.expressions.push(expression);
BcbCounter::Expression { id }
if let Some(counter) = self.expression_reference.get(&expression) {
*counter
} else {
let counter = BcbCounter::Expression { id: self.expressions.push(expression.clone()) };
self.expression_reference.insert(expression, counter);
// Later branches of pattern matching might try to make expression with C2 - (C2 - C1), (C2 - C1) + C1
match (lhs, op, rhs) {
(BcbCounter::Counter { .. }, Op::Add, BcbCounter::Counter { .. }) => {
self.expression_reference.insert(
Expression { lhs: counter.as_term(), op: Op::Subtract, rhs: lhs.as_term() },
rhs,
);
self.expression_reference.insert(
Expression { lhs: counter.as_term(), op: Op::Subtract, rhs: rhs.as_term() },
lhs,
);
}
(BcbCounter::Counter { .. }, Op::Subtract, BcbCounter::Counter { .. }) => {
self.expression_reference.insert(
Expression { lhs: counter.as_term(), op: Op::Add, rhs: rhs.as_term() },
lhs,
);
self.expression_reference.insert(
Expression { lhs: lhs.as_term(), op: Op::Subtract, rhs: counter.as_term() },
rhs,
);
}
_ => {}
}
counter
}
}

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

0 comments on commit 446e013

Please sign in to comment.