Skip to content

Commit 86b55cc

Browse files
committed
coverage: Fetch expressions and mappings separately
The combined `get_expressions_and_counter_regions` method was an artifact of having to prepare the expressions and mappings at the same time, to avoid ownership/lifetime problems with temporary data used by both. Now that we have an explicit transition from `FunctionCoverageCollector` to the final `FunctionCoverage`, we can prepare any shared data during that step and store it in the final struct.
1 parent 371883a commit 86b55cc

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::coverageinfo::ffi::{Counter, CounterExpression, ExprKind};
22

3+
use rustc_data_structures::captures::Captures;
34
use rustc_data_structures::fx::FxIndexSet;
45
use rustc_index::bit_set::BitSet;
56
use rustc_middle::mir::coverage::{
@@ -188,26 +189,11 @@ impl<'tcx> FunctionCoverage<'tcx> {
188189
if self.is_used { self.function_coverage_info.function_source_hash } else { 0 }
189190
}
190191

191-
/// Generate an array of CounterExpressions, and an iterator over all `Counter`s and their
192-
/// associated `Regions` (from which the LLVM-specific `CoverageMapGenerator` will create
193-
/// `CounterMappingRegion`s.
194-
pub fn get_expressions_and_counter_regions(
195-
&self,
196-
) -> (Vec<CounterExpression>, impl Iterator<Item = (Counter, &CodeRegion)>) {
197-
let counter_expressions = self.counter_expressions();
198-
// Expression IDs are indices into `self.expressions`, and on the LLVM
199-
// side they will be treated as indices into `counter_expressions`, so
200-
// the two vectors should correspond 1:1.
201-
assert_eq!(self.function_coverage_info.expressions.len(), counter_expressions.len());
202-
203-
let counter_regions = self.counter_regions();
204-
205-
(counter_expressions, counter_regions)
206-
}
207-
208192
/// Convert this function's coverage expression data into a form that can be
209193
/// passed through FFI to LLVM.
210-
fn counter_expressions(&self) -> Vec<CounterExpression> {
194+
pub(crate) fn counter_expressions(
195+
&self,
196+
) -> impl Iterator<Item = CounterExpression> + ExactSizeIterator + Captures<'_> {
211197
// We know that LLVM will optimize out any unused expressions before
212198
// producing the final coverage map, so there's no need to do the same
213199
// thing on the Rust side unless we're confident we can do much better.
@@ -218,23 +204,23 @@ impl<'tcx> FunctionCoverage<'tcx> {
218204
_ => Counter::from_term(operand),
219205
};
220206

221-
self.function_coverage_info
222-
.expressions
223-
.iter()
224-
.map(|&Expression { lhs, op, rhs }| CounterExpression {
207+
self.function_coverage_info.expressions.iter().map(move |&Expression { lhs, op, rhs }| {
208+
CounterExpression {
225209
lhs: counter_from_operand(lhs),
226210
kind: match op {
227211
Op::Add => ExprKind::Add,
228212
Op::Subtract => ExprKind::Subtract,
229213
},
230214
rhs: counter_from_operand(rhs),
231-
})
232-
.collect::<Vec<_>>()
215+
}
216+
})
233217
}
234218

235219
/// Converts this function's coverage mappings into an intermediate form
236220
/// that will be used by `mapgen` when preparing for FFI.
237-
fn counter_regions(&self) -> impl Iterator<Item = (Counter, &CodeRegion)> {
221+
pub(crate) fn counter_regions(
222+
&self,
223+
) -> impl Iterator<Item = (Counter, &CodeRegion)> + ExactSizeIterator {
238224
// Historically, mappings were stored directly in counter/expression
239225
// statements in MIR, and MIR optimizations would sometimes remove them.
240226
// That's mostly no longer true, so now we detect cases where that would

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ fn encode_mappings_for_function(
185185
global_file_table: &mut GlobalFileTable,
186186
function_coverage: &FunctionCoverage<'_>,
187187
) -> Vec<u8> {
188-
let (expressions, counter_regions) = function_coverage.get_expressions_and_counter_regions();
189-
190-
let mut counter_regions = counter_regions.collect::<Vec<_>>();
188+
let mut counter_regions = function_coverage.counter_regions().collect::<Vec<_>>();
191189
if counter_regions.is_empty() {
192190
return Vec::new();
193191
}
194192

193+
let expressions = function_coverage.counter_expressions().collect::<Vec<_>>();
194+
195195
let mut virtual_file_mapping = IndexVec::<u32, u32>::new();
196196
let mut mapping_regions = Vec::with_capacity(counter_regions.len());
197197

0 commit comments

Comments
 (0)