Skip to content

Commit 2ee7385

Browse files
committed
coverage: Remove the old code for simplifying counters after MIR opts
1 parent b0fd529 commit 2ee7385

File tree

5 files changed

+17
-183
lines changed

5 files changed

+17
-183
lines changed

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
5454
let fn_cov_info = tcx.instance_mir(instance.def).function_coverage_info.as_deref()?;
5555
let ids_info = tcx.coverage_ids_info(instance.def)?;
5656

57-
let expressions = prepare_expressions(ids_info, is_used);
57+
let expressions = prepare_expressions(ids_info);
5858

5959
let mut covfun = CovfunRecord {
6060
mangled_function_name: tcx.symbol_name(instance).name,
@@ -76,16 +76,8 @@ pub(crate) fn prepare_covfun_record<'tcx>(
7676
}
7777

7878
/// Convert the function's coverage-counter expressions into a form suitable for FFI.
79-
fn prepare_expressions(ids_info: &CoverageIdsInfo, is_used: bool) -> Vec<ffi::CounterExpression> {
80-
// If any counters or expressions were removed by MIR opts, replace their
81-
// terms with zero.
82-
let counter_for_term = |term| {
83-
if !is_used || ids_info.is_zero_term(term) {
84-
ffi::Counter::ZERO
85-
} else {
86-
ffi::Counter::from_term(term)
87-
}
88-
};
79+
fn prepare_expressions(ids_info: &CoverageIdsInfo) -> Vec<ffi::CounterExpression> {
80+
let counter_for_term = ffi::Counter::from_term;
8981

9082
// We know that LLVM will optimize out any unused expressions before
9183
// producing the final coverage map, so there's no need to do the same
@@ -133,13 +125,14 @@ fn fill_region_tables<'tcx>(
133125

134126
// For each counter/region pair in this function+file, convert it to a
135127
// form suitable for FFI.
136-
let is_zero_term = |term| !covfun.is_used || ids_info.is_zero_term(term);
137128
for &Mapping { ref kind, span } in &fn_cov_info.mappings {
138-
// If the mapping refers to counters/expressions that were removed by
139-
// MIR opts, replace those occurrences with zero.
129+
// If this function is unused, replace all counters with zero.
140130
let counter_for_bcb = |bcb: BasicCoverageBlock| -> ffi::Counter {
141-
let term = ids_info.term_for_bcb[bcb].expect("every BCB in a mapping was given a term");
142-
let term = if is_zero_term(term) { CovTerm::Zero } else { term };
131+
let term = if covfun.is_used {
132+
ids_info.term_for_bcb[bcb].expect("every BCB in a mapping was given a term")
133+
} else {
134+
CovTerm::Zero
135+
};
143136
ffi::Counter::from_term(term)
144137
};
145138

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,9 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
163163
CoverageKind::VirtualCounter { bcb }
164164
if let Some(&id) = ids_info.phys_counter_for_node.get(&bcb) =>
165165
{
166-
let num_counters = ids_info.num_counters_after_mir_opts();
167-
168166
let fn_name = bx.get_pgo_func_name_var(instance);
169167
let hash = bx.const_u64(function_coverage_info.function_source_hash);
170-
let num_counters = bx.const_u32(num_counters);
168+
let num_counters = bx.const_u32(ids_info.num_counters);
171169
let index = bx.const_u32(id.as_u32());
172170
debug!(
173171
"codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",

Diff for: compiler/rustc_middle/src/mir/coverage.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::fmt::{self, Debug, Formatter};
44

55
use rustc_data_structures::fx::FxIndexMap;
6-
use rustc_index::bit_set::DenseBitSet;
76
use rustc_index::{Idx, IndexVec};
87
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
98
use rustc_span::Span;
@@ -277,41 +276,12 @@ pub struct MCDCDecisionSpan {
277276
/// Returned by the `coverage_ids_info` query.
278277
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable)]
279278
pub struct CoverageIdsInfo {
280-
pub counters_seen: DenseBitSet<CounterId>,
281-
pub zero_expressions: DenseBitSet<ExpressionId>,
282-
279+
pub num_counters: u32,
283280
pub phys_counter_for_node: FxIndexMap<BasicCoverageBlock, CounterId>,
284281
pub term_for_bcb: IndexVec<BasicCoverageBlock, Option<CovTerm>>,
285282
pub expressions: IndexVec<ExpressionId, Expression>,
286283
}
287284

288-
impl CoverageIdsInfo {
289-
/// Coverage codegen needs to know how many coverage counters are ever
290-
/// incremented within a function, so that it can set the `num-counters`
291-
/// argument of the `llvm.instrprof.increment` intrinsic.
292-
///
293-
/// This may be less than the highest counter ID emitted by the
294-
/// InstrumentCoverage MIR pass, if the highest-numbered counter increments
295-
/// were removed by MIR optimizations.
296-
pub fn num_counters_after_mir_opts(&self) -> u32 {
297-
// FIXME(Zalathar): Currently this treats an unused counter as "used"
298-
// if its ID is less than that of the highest counter that really is
299-
// used. Fixing this would require adding a renumbering step somewhere.
300-
self.counters_seen.last_set_in(..).map_or(0, |max| max.as_u32() + 1)
301-
}
302-
303-
/// Returns `true` if the given term is known to have a value of zero, taking
304-
/// into account knowledge of which counters are unused and which expressions
305-
/// are always zero.
306-
pub fn is_zero_term(&self, term: CovTerm) -> bool {
307-
match term {
308-
CovTerm::Zero => true,
309-
CovTerm::Counter(id) => !self.counters_seen.contains(id),
310-
CovTerm::Expression(id) => self.zero_expressions.contains(id),
311-
}
312-
}
313-
}
314-
315285
rustc_index::newtype_index! {
316286
/// During the `InstrumentCoverage` MIR pass, a BCB is a node in the
317287
/// "coverage graph", which is a refinement of the MIR control-flow graph

Diff for: compiler/rustc_mir_transform/src/coverage/counters.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub(super) struct CoverageCounters {
135135
/// List of places where a counter-increment statement should be injected
136136
/// into MIR, each with its corresponding counter ID.
137137
pub(crate) phys_counter_for_node: FxIndexMap<BasicCoverageBlock, CounterId>,
138-
next_counter_id: CounterId,
138+
pub(crate) next_counter_id: CounterId,
139139

140140
/// Coverage counters/expressions that are associated with individual BCBs.
141141
pub(crate) node_counters: IndexVec<BasicCoverageBlock, Option<CovTerm>>,

Diff for: compiler/rustc_mir_transform/src/coverage/query.rs

+5-132
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use rustc_data_structures::captures::Captures;
2-
use rustc_index::IndexSlice;
32
use rustc_index::bit_set::DenseBitSet;
43
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
5-
use rustc_middle::mir::coverage::{
6-
BasicCoverageBlock, CounterId, CovTerm, CoverageIdsInfo, CoverageKind, Expression,
7-
ExpressionId, MappingKind, Op,
8-
};
4+
use rustc_middle::mir::coverage::{BasicCoverageBlock, CoverageIdsInfo, CoverageKind, MappingKind};
95
use rustc_middle::mir::{Body, Statement, StatementKind};
106
use rustc_middle::query::TyCtxtAt;
117
use rustc_middle::ty::{self, TyCtxt};
@@ -136,44 +132,12 @@ fn coverage_ids_info<'tcx>(
136132
let node_counters = make_node_counters(&fn_cov_info.node_flow_data, &fn_cov_info.priority_list);
137133
let coverage_counters = transcribe_counters(&node_counters, &bcb_needs_counter, &bcbs_seen);
138134

139-
let mut counters_seen = DenseBitSet::new_empty(coverage_counters.node_counters.len());
140-
let mut expressions_seen = DenseBitSet::new_filled(coverage_counters.expressions.len());
141-
142-
// For each expression ID that is directly used by one or more mappings,
143-
// mark it as not-yet-seen. This indicates that we expect to see a
144-
// corresponding `VirtualCounter` statement during MIR traversal.
145-
for mapping in fn_cov_info.mappings.iter() {
146-
// Currently we only worry about ordinary code mappings.
147-
// For branch and MC/DC mappings, expressions might not correspond
148-
// to any particular point in the control-flow graph.
149-
if let MappingKind::Code { bcb } = mapping.kind
150-
&& let Some(CovTerm::Expression(id)) = coverage_counters.node_counters[bcb]
151-
{
152-
expressions_seen.remove(id);
153-
}
154-
}
155-
156-
for bcb in bcbs_seen.iter() {
157-
if let Some(&id) = coverage_counters.phys_counter_for_node.get(&bcb) {
158-
counters_seen.insert(id);
159-
}
160-
if let Some(CovTerm::Expression(id)) = coverage_counters.node_counters[bcb] {
161-
expressions_seen.insert(id);
162-
}
163-
}
164-
165-
let zero_expressions = identify_zero_expressions(
166-
&coverage_counters.expressions,
167-
&counters_seen,
168-
&expressions_seen,
169-
);
170-
171-
let CoverageCounters { phys_counter_for_node, node_counters, expressions, .. } =
172-
coverage_counters;
135+
let CoverageCounters {
136+
phys_counter_for_node, next_counter_id, node_counters, expressions, ..
137+
} = coverage_counters;
173138

174139
Some(CoverageIdsInfo {
175-
counters_seen,
176-
zero_expressions,
140+
num_counters: next_counter_id.as_u32(),
177141
phys_counter_for_node,
178142
term_for_bcb: node_counters,
179143
expressions,
@@ -195,94 +159,3 @@ fn is_inlined(body: &Body<'_>, statement: &Statement<'_>) -> bool {
195159
let scope_data = &body.source_scopes[statement.source_info.scope];
196160
scope_data.inlined.is_some() || scope_data.inlined_parent_scope.is_some()
197161
}
198-
199-
/// Identify expressions that will always have a value of zero, and note their
200-
/// IDs in a `DenseBitSet`. Mappings that refer to a zero expression can instead
201-
/// become mappings to a constant zero value.
202-
///
203-
/// This function mainly exists to preserve the simplifications that were
204-
/// already being performed by the Rust-side expression renumbering, so that
205-
/// the resulting coverage mappings don't get worse.
206-
fn identify_zero_expressions(
207-
expressions: &IndexSlice<ExpressionId, Expression>,
208-
counters_seen: &DenseBitSet<CounterId>,
209-
expressions_seen: &DenseBitSet<ExpressionId>,
210-
) -> DenseBitSet<ExpressionId> {
211-
// The set of expressions that either were optimized out entirely, or
212-
// have zero as both of their operands, and will therefore always have
213-
// a value of zero. Other expressions that refer to these as operands
214-
// can have those operands replaced with `CovTerm::Zero`.
215-
let mut zero_expressions = DenseBitSet::new_empty(expressions.len());
216-
217-
// Simplify a copy of each expression based on lower-numbered expressions,
218-
// and then update the set of always-zero expressions if necessary.
219-
// (By construction, expressions can only refer to other expressions
220-
// that have lower IDs, so one pass is sufficient.)
221-
for (id, expression) in expressions.iter_enumerated() {
222-
if !expressions_seen.contains(id) {
223-
// If an expression was not seen, it must have been optimized away,
224-
// so any operand that refers to it can be replaced with zero.
225-
zero_expressions.insert(id);
226-
continue;
227-
}
228-
229-
// We don't need to simplify the actual expression data in the
230-
// expressions list; we can just simplify a temporary copy and then
231-
// use that to update the set of always-zero expressions.
232-
let Expression { mut lhs, op, mut rhs } = *expression;
233-
234-
// If an expression has an operand that is also an expression, the
235-
// operand's ID must be strictly lower. This is what lets us find
236-
// all zero expressions in one pass.
237-
let assert_operand_expression_is_lower = |operand_id: ExpressionId| {
238-
assert!(
239-
operand_id < id,
240-
"Operand {operand_id:?} should be less than {id:?} in {expression:?}",
241-
)
242-
};
243-
244-
// If an operand refers to a counter or expression that is always
245-
// zero, then that operand can be replaced with `CovTerm::Zero`.
246-
let maybe_set_operand_to_zero = |operand: &mut CovTerm| {
247-
if let CovTerm::Expression(id) = *operand {
248-
assert_operand_expression_is_lower(id);
249-
}
250-
251-
if is_zero_term(&counters_seen, &zero_expressions, *operand) {
252-
*operand = CovTerm::Zero;
253-
}
254-
};
255-
maybe_set_operand_to_zero(&mut lhs);
256-
maybe_set_operand_to_zero(&mut rhs);
257-
258-
// Coverage counter values cannot be negative, so if an expression
259-
// involves subtraction from zero, assume that its RHS must also be zero.
260-
// (Do this after simplifications that could set the LHS to zero.)
261-
if lhs == CovTerm::Zero && op == Op::Subtract {
262-
rhs = CovTerm::Zero;
263-
}
264-
265-
// After the above simplifications, if both operands are zero, then
266-
// we know that this expression is always zero too.
267-
if lhs == CovTerm::Zero && rhs == CovTerm::Zero {
268-
zero_expressions.insert(id);
269-
}
270-
}
271-
272-
zero_expressions
273-
}
274-
275-
/// Returns `true` if the given term is known to have a value of zero, taking
276-
/// into account knowledge of which counters are unused and which expressions
277-
/// are always zero.
278-
fn is_zero_term(
279-
counters_seen: &DenseBitSet<CounterId>,
280-
zero_expressions: &DenseBitSet<ExpressionId>,
281-
term: CovTerm,
282-
) -> bool {
283-
match term {
284-
CovTerm::Zero => true,
285-
CovTerm::Counter(id) => !counters_seen.contains(id),
286-
CovTerm::Expression(id) => zero_expressions.contains(id),
287-
}
288-
}

0 commit comments

Comments
 (0)