Skip to content

Commit df1e46b

Browse files
committed
coverage: Fetch expressions and mappings separately
1 parent 8ff508f commit df1e46b

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::{
@@ -193,26 +194,11 @@ impl<'tcx> FunctionCoverage<'tcx> {
193194
if self.is_used { self.function_coverage_info.function_source_hash } else { 0 }
194195
}
195196

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

226-
self.function_coverage_info
227-
.expressions
228-
.iter()
229-
.map(|&Expression { lhs, op, rhs }| CounterExpression {
212+
self.function_coverage_info.expressions.iter().map(move |&Expression { lhs, op, rhs }| {
213+
CounterExpression {
230214
lhs: counter_from_operand(lhs),
231215
kind: match op {
232216
Op::Add => ExprKind::Add,
233217
Op::Subtract => ExprKind::Subtract,
234218
},
235219
rhs: counter_from_operand(rhs),
236-
})
237-
.collect::<Vec<_>>()
220+
}
221+
})
238222
}
239223

240224
/// Converts this function's coverage mappings into an intermediate form
241225
/// that will be used by `mapgen` when preparing for FFI.
242-
fn counter_regions(&self) -> impl Iterator<Item = (Counter, &CodeRegion)> {
226+
pub(crate) fn counter_regions(
227+
&self,
228+
) -> impl Iterator<Item = (Counter, &CodeRegion)> + ExactSizeIterator {
243229
// Historically, mappings were stored directly in counter/expression
244230
// statements in MIR, and MIR optimizations would sometimes remove them.
245231
// 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)