about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-10-16 21:25:09 +1100
committerZalathar <Zalathar@users.noreply.github.com>2024-12-04 17:00:25 +1100
commit2a3b4a0afd84933b94b0181f421c0f0446b4bed7 (patch)
tree5333196846a9118d1cd8cf6992e0ed5c147cc3a0
parent3b382642aba7cffbb2f47829b24635fad87bcf5c (diff)
downloadrust-2a3b4a0afd84933b94b0181f421c0f0446b4bed7.tar.gz
rust-2a3b4a0afd84933b94b0181f421c0f0446b4bed7.zip
coverage: Extract `subtracted_sum` in counter creation
-rw-r--r--compiler/rustc_mir_transform/src/coverage/counters.rs16
-rw-r--r--tests/coverage/branch/guard.cov-map10
2 files changed, 14 insertions, 12 deletions
diff --git a/compiler/rustc_mir_transform/src/coverage/counters.rs b/compiler/rustc_mir_transform/src/coverage/counters.rs
index cf9f6981c5a..c1c2d4b96c6 100644
--- a/compiler/rustc_mir_transform/src/coverage/counters.rs
+++ b/compiler/rustc_mir_transform/src/coverage/counters.rs
@@ -182,6 +182,12 @@ impl CoverageCounters {
             .reduce(|accum, counter| self.make_expression(accum, Op::Add, counter))
     }
 
+    /// Creates a counter whose value is `lhs - SUM(rhs)`.
+    fn make_subtracted_sum(&mut self, lhs: BcbCounter, rhs: &[BcbCounter]) -> BcbCounter {
+        let Some(rhs_sum) = self.make_sum(rhs) else { return lhs };
+        self.make_expression(lhs, Op::Subtract, rhs_sum)
+    }
+
     pub(super) fn num_counters(&self) -> usize {
         self.counter_increment_sites.len()
     }
@@ -338,8 +344,7 @@ impl<'a> CountersBuilder<'a> {
         };
 
         // For each out-edge other than the one that was chosen to get an expression,
-        // ensure that it has a counter (existing counter/expression or a new counter),
-        // and accumulate the corresponding counters into a single sum expression.
+        // ensure that it has a counter (existing counter/expression or a new counter).
         let other_out_edge_counters = successors
             .iter()
             .copied()
@@ -347,15 +352,10 @@ impl<'a> CountersBuilder<'a> {
             .filter(|&edge_target_bcb| edge_target_bcb != target_bcb)
             .map(|to_bcb| self.get_or_make_edge_counter(from_bcb, to_bcb))
             .collect::<Vec<_>>();
-        let Some(sum_of_all_other_out_edges) = self.counters.make_sum(&other_out_edge_counters)
-        else {
-            return;
-        };
 
         // Now create an expression for the chosen edge, by taking the counter
         // for its source node and subtracting the sum of its sibling out-edges.
-        let expression =
-            self.counters.make_expression(node_counter, Op::Subtract, sum_of_all_other_out_edges);
+        let expression = self.counters.make_subtracted_sum(node_counter, &other_out_edge_counters);
 
         debug!("{target_bcb:?} gets an expression: {expression:?}");
         self.counters.set_edge_counter(from_bcb, target_bcb, expression);
diff --git a/tests/coverage/branch/guard.cov-map b/tests/coverage/branch/guard.cov-map
index 0f33ed17a0a..e87b389c1ac 100644
--- a/tests/coverage/branch/guard.cov-map
+++ b/tests/coverage/branch/guard.cov-map
@@ -1,5 +1,5 @@
 Function name: guard::branch_match_guard
-Raw bytes (85): 0x[01, 01, 06, 19, 0d, 05, 09, 0f, 15, 13, 11, 17, 0d, 05, 09, 0d, 01, 0c, 01, 01, 10, 1d, 03, 0b, 00, 0c, 15, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 19, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 1d, 02, 0a, 11, 03, 0e, 00, 0f, 1d, 00, 14, 00, 19, 20, 11, 09, 00, 14, 00, 1e, 11, 00, 1d, 02, 0a, 17, 03, 0e, 02, 0a, 0b, 04, 01, 00, 02]
+Raw bytes (85): 0x[01, 01, 06, 19, 0d, 05, 09, 0f, 15, 13, 11, 17, 0d, 05, 09, 0d, 01, 0c, 01, 01, 10, 02, 03, 0b, 00, 0c, 15, 01, 14, 02, 0a, 0d, 03, 0e, 00, 0f, 19, 00, 14, 00, 19, 20, 0d, 02, 00, 14, 00, 1e, 0d, 00, 1d, 02, 0a, 11, 03, 0e, 00, 0f, 02, 00, 14, 00, 19, 20, 11, 09, 00, 14, 00, 1e, 11, 00, 1d, 02, 0a, 17, 03, 0e, 02, 0a, 0b, 04, 01, 00, 02]
 Number of files: 1
 - file 0 => global file 1
 Number of expressions: 6
@@ -11,7 +11,8 @@ Number of expressions: 6
 - expression 5 operands: lhs = Counter(1), rhs = Counter(2)
 Number of file 0 mappings: 13
 - Code(Counter(0)) at (prev + 12, 1) to (start + 1, 16)
-- Code(Counter(7)) at (prev + 3, 11) to (start + 0, 12)
+- Code(Expression(0, Sub)) at (prev + 3, 11) to (start + 0, 12)
+    = (c6 - c3)
 - Code(Counter(5)) at (prev + 1, 20) to (start + 2, 10)
 - Code(Counter(3)) at (prev + 3, 14) to (start + 0, 15)
 - Code(Counter(6)) at (prev + 0, 20) to (start + 0, 25)
@@ -20,7 +21,8 @@ Number of file 0 mappings: 13
     false = (c6 - c3)
 - Code(Counter(3)) at (prev + 0, 29) to (start + 2, 10)
 - Code(Counter(4)) at (prev + 3, 14) to (start + 0, 15)
-- Code(Counter(7)) at (prev + 0, 20) to (start + 0, 25)
+- Code(Expression(0, Sub)) at (prev + 0, 20) to (start + 0, 25)
+    = (c6 - c3)
 - Branch { true: Counter(4), false: Counter(2) } at (prev + 0, 20) to (start + 0, 30)
     true  = c4
     false = c2
@@ -29,5 +31,5 @@ Number of file 0 mappings: 13
     = (c1 + c2)
 - Code(Expression(2, Add)) at (prev + 4, 1) to (start + 0, 2)
     = ((((c1 + c2) + c3) + c4) + c5)
-Highest counter ID seen: c7
+Highest counter ID seen: c6