Skip to content

Commit 20f55c1

Browse files
committed
Refactor MIR coverage instrumentation
Lays a better foundation for injecting more counters in each function.
1 parent 12ddd60 commit 20f55c1

File tree

5 files changed

+151
-120
lines changed

5 files changed

+151
-120
lines changed

src/librustc_codegen_llvm/coverageinfo/mapgen.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::ffi::CString;
2424
/// replicated for Rust's Coverage Map.
2525
pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
2626
let function_coverage_map = cx.coverage_context().take_function_coverage_map();
27-
if function_coverage_map.len() == 0 {
27+
if function_coverage_map.is_empty() {
2828
// This module has no functions with coverage instrumentation
2929
return;
3030
}
@@ -81,7 +81,7 @@ struct CoverageMapGenerator {
8181

8282
impl CoverageMapGenerator {
8383
fn new() -> Self {
84-
Self { filenames: Vec::new(), filename_to_index: FxHashMap::<CString, u32>::default() }
84+
Self { filenames: Vec::new(), filename_to_index: FxHashMap::default() }
8585
}
8686

8787
/// Using the `expressions` and `counter_regions` collected for the current function, generate
@@ -95,7 +95,7 @@ impl CoverageMapGenerator {
9595
coverage_mappings_buffer: &RustString,
9696
) {
9797
let mut counter_regions = counter_regions.collect::<Vec<_>>();
98-
if counter_regions.len() == 0 {
98+
if counter_regions.is_empty() {
9999
return;
100100
}
101101

@@ -109,7 +109,7 @@ impl CoverageMapGenerator {
109109
// `file_id` (indexing files referenced by the current function), and construct the
110110
// function-specific `virtual_file_mapping` from `file_id` to its index in the module's
111111
// `filenames` array.
112-
counter_regions.sort_by_key(|(_counter, region)| *region);
112+
counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
113113
for (counter, region) in counter_regions {
114114
let (file_path, start_line, start_col, end_line, end_col) = region.file_start_and_end();
115115
let same_file = current_file_path.as_ref().map_or(false, |p| p == file_path);

src/librustc_codegen_llvm/coverageinfo/mod.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -110,36 +110,37 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
110110
}
111111
}
112112

113-
/// Aligns to C++ struct llvm::coverage::Counter::CounterKind.
114-
/// The order of discrimiators is important.
113+
/// Aligns with [llvm::coverage::CounterMappingRegion::RegionKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L205-L221)
115114
#[derive(Copy, Clone, Debug)]
116115
#[repr(C)]
117116
enum RegionKind {
118117
/// A CodeRegion associates some code with a counter
119-
CodeRegion,
118+
CodeRegion = 0,
120119

121120
/// An ExpansionRegion represents a file expansion region that associates
122121
/// a source range with the expansion of a virtual source file, such as
123122
/// for a macro instantiation or #include file.
124-
ExpansionRegion,
123+
ExpansionRegion = 1,
125124

126125
/// A SkippedRegion represents a source range with code that was skipped
127126
/// by a preprocessor or similar means.
128-
SkippedRegion,
127+
SkippedRegion = 2,
129128

130129
/// A GapRegion is like a CodeRegion, but its count is only set as the
131130
/// line execution count when its the only region in the line.
132-
GapRegion,
131+
GapRegion = 3,
133132
}
134133

135134
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
136-
/// coverage map in accordance with LLVM's "Coverage Mapping Format". The struct composes fields
137-
/// representing the `Counter` type and value(s) (injected counter ID, or expression type and
138-
/// operands), the source file (an indirect index into a "filenames array", encoded separately),
139-
/// and source location (start and end positions of the represented code region).
135+
/// coverage map, in accordance with the
136+
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
137+
/// The struct composes fields representing the `Counter` type and value(s) (injected counter ID,
138+
/// or expression type and operands), the source file (an indirect index into a "filenames array",
139+
/// encoded separately), and source location (start and end positions of the represented code
140+
/// region).
140141
///
141-
/// Aligns to C++ struct llvm::coverage::CounterMappingRegion.
142-
/// The order of fields is important.
142+
/// Aligns with [llvm::coverage::CounterMappingRegion](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L223-L226)
143+
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
143144
#[derive(Copy, Clone, Debug)]
144145
#[repr(C)]
145146
pub struct CounterMappingRegion {

src/librustc_codegen_ssa/coverageinfo/map.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ use std::cmp::{Ord, Ordering};
77
use std::fmt;
88
use std::path::PathBuf;
99

10-
/// Aligns to C++ struct llvm::coverage::Counter::CounterKind.
11-
/// The order of discriminators is important.
10+
/// Aligns with [llvm::coverage::Counter::CounterKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L91)
1211
#[derive(Copy, Clone, Debug)]
1312
#[repr(C)]
1413
enum CounterKind {
15-
Zero,
16-
CounterValueReference,
17-
Expression,
14+
Zero = 0,
15+
CounterValueReference = 1,
16+
Expression = 2,
1817
}
1918

20-
/// Aligns to C++ struct llvm::coverage::Counter. Note that `id` has
21-
/// different interpretations, depending on the `kind`:
19+
/// A reference to an instance of an abstract "counter" that will yield a value in a coverage
20+
/// report. Note that `id` has different interpretations, depending on the `kind`:
2221
/// * For `CounterKind::Zero`, `id` is assumed to be `0`
2322
/// * For `CounterKind::CounterValueReference`, `id` matches the `counter_id` of the injected
2423
/// instrumentation counter (the `index` argument to the LLVM intrinsic `instrprof.increment()`)
25-
/// * For `CounterKind::Expression`, `id` is the index into the array of counter expressions.
26-
/// The order of fields is important.
24+
/// * For `CounterKind::Expression`, `id` is the index into the coverage map's array of counter
25+
/// expressions.
26+
/// Aligns with [llvm::coverage::Counter](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L98-L99)
27+
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
2728
#[derive(Copy, Clone, Debug)]
2829
#[repr(C)]
2930
pub struct Counter {
31+
// Important: The layout (order and types of fields) must match its C++ counterpart.
3032
kind: CounterKind,
3133
id: u32,
3234
}
@@ -45,21 +47,19 @@ impl Counter {
4547
}
4648
}
4749

48-
/// Aligns to C++ struct llvm::coverage::CounterExpression::ExprKind.
49-
/// The order of discriminators is important.
50+
/// Aligns with [llvm::coverage::CounterExpression::ExprKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L146)
5051
#[derive(Copy, Clone, Debug)]
5152
#[repr(C)]
5253
pub enum ExprKind {
53-
Subtract,
54-
Add,
54+
Subtract = 0,
55+
Add = 1,
5556
}
5657

57-
/// Aligns to C++ struct llvm::coverage::CounterExpression.
58-
/// The order of fields is important.
58+
/// Aligns with [llvm::coverage::CounterExpression](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L147-L148)
59+
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
5960
#[derive(Copy, Clone, Debug)]
6061
#[repr(C)]
6162
pub struct CounterExpression {
62-
// Note the field order is important.
6363
kind: ExprKind,
6464
lhs: Counter,
6565
rhs: Counter,

0 commit comments

Comments
 (0)