forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#133849 - Zalathar:replay, r=oli-obk coverage: Use a separate counter type and simplification step during counter creation When instrumenting a function's MIR for coverage, there is a point where we need to decide, for each node in the control-flow graph, whether its execution count will be tracked by a physical counter, or by an expression that combines physical counters from other parts of the graph. Currently the code for doing that is heavily tied to the final form of the LLVM coverage mapping format, and performs some important simplification steps on-the-fly. These factors make the code extremely difficult to modify without breaking or massively worsening the resulting coverage-instrumentation metadata. --- This PR aims to improve that situation somewhat by adding an extra intermediate representation between the code that chooses how each node will be counted, and the code that converts those decisions into actual tables of physical counters and trees of counter expressions. As part of doing that, some of the simplifications that are currently performed during the main counter creation step have been pulled out into a separate step. In most cases the resulting coverage metadata is equivalent, slightly better, or slightly worse. The biggest outlier is `counters.rs`, where the coverage metadata ends up about 10% larger. This seems to be the result of the new approach having less subexpression sharing (because it relies on flatten-sort-cancel), and therefore being less effective at taking advantage of MIR optimizations to replace counters for unused control-flow with zeroes. I think the modest downside is acceptable in light of the future possibilities opened up by this decoupling.
- Loading branch information
Showing
38 changed files
with
1,719 additions
and
1,589 deletions.
There are no files selected for viewing
374 changes: 230 additions & 144 deletions
374
compiler/rustc_mir_transform/src/coverage/counters.rs
Large diffs are not rendered by default.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
compiler/rustc_mir_transform/src/coverage/counters/tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::fmt::Debug; | ||
|
||
use super::sort_and_cancel; | ||
|
||
fn flatten<T>(input: Vec<Option<T>>) -> Vec<T> { | ||
input.into_iter().flatten().collect() | ||
} | ||
|
||
fn sort_and_cancel_and_flatten<T: Clone + Ord>(pos: Vec<T>, neg: Vec<T>) -> (Vec<T>, Vec<T>) { | ||
let (pos_actual, neg_actual) = sort_and_cancel(pos, neg); | ||
(flatten(pos_actual), flatten(neg_actual)) | ||
} | ||
|
||
#[track_caller] | ||
fn check_test_case<T: Clone + Debug + Ord>( | ||
pos: Vec<T>, | ||
neg: Vec<T>, | ||
pos_expected: Vec<T>, | ||
neg_expected: Vec<T>, | ||
) { | ||
eprintln!("pos = {pos:?}; neg = {neg:?}"); | ||
let output = sort_and_cancel_and_flatten(pos, neg); | ||
assert_eq!(output, (pos_expected, neg_expected)); | ||
} | ||
|
||
#[test] | ||
fn cancellation() { | ||
let cases: &[(Vec<u32>, Vec<u32>, Vec<u32>, Vec<u32>)] = &[ | ||
(vec![], vec![], vec![], vec![]), | ||
(vec![4, 2, 1, 5, 3], vec![], vec![1, 2, 3, 4, 5], vec![]), | ||
(vec![5, 5, 5, 5, 5], vec![5], vec![5, 5, 5, 5], vec![]), | ||
(vec![1, 1, 2, 2, 3, 3], vec![1, 2, 3], vec![1, 2, 3], vec![]), | ||
(vec![1, 1, 2, 2, 3, 3], vec![2, 4, 2], vec![1, 1, 3, 3], vec![4]), | ||
]; | ||
|
||
for (pos, neg, pos_expected, neg_expected) in cases { | ||
check_test_case(pos.to_vec(), neg.to_vec(), pos_expected.to_vec(), neg_expected.to_vec()); | ||
// Same test case, but with its inputs flipped and its outputs flipped. | ||
check_test_case(neg.to_vec(), pos.to_vec(), neg_expected.to_vec(), pos_expected.to_vec()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.