1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
use crate::transform::{MirPass, MirSource};
use crate::util::pretty;
use crate::util::spanview::{
source_range_no_file, statement_kind_name, terminator_kind_name, write_spanview_document,
SpanViewable, TOOLTIP_INDENT,
};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_index::bit_set::BitSet;
use rustc_middle::hir;
use rustc_middle::ich::StableHashingContext;
use rustc_middle::mir;
use rustc_middle::mir::coverage::*;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{
BasicBlock, BasicBlockData, Coverage, CoverageInfo, Location, Statement, StatementKind,
TerminatorKind,
};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::DefId;
use rustc_span::{FileName, Pos, RealFileName, Span, Symbol};
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
/// to construct the coverage map.
pub struct InstrumentCoverage;
/// The `query` provider for `CoverageInfo`, requested by `codegen_coverage()` (to inject each
/// counter) and `FunctionCoverage::new()` (to extract the coverage map metadata from the MIR).
pub(crate) fn provide(providers: &mut Providers) {
providers.coverageinfo = |tcx, def_id| coverageinfo_from_mir(tcx, def_id);
}
struct CoverageVisitor {
info: CoverageInfo,
}
impl Visitor<'_> for CoverageVisitor {
fn visit_coverage(&mut self, coverage: &Coverage, _location: Location) {
match coverage.kind {
CoverageKind::Counter { id, .. } => {
let counter_id = u32::from(id);
self.info.num_counters = std::cmp::max(self.info.num_counters, counter_id + 1);
}
CoverageKind::Expression { id, .. } => {
let expression_index = u32::MAX - u32::from(id);
self.info.num_expressions =
std::cmp::max(self.info.num_expressions, expression_index + 1);
}
_ => {}
}
}
}
fn coverageinfo_from_mir<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> CoverageInfo {
let mir_body = tcx.optimized_mir(def_id);
// The `num_counters` argument to `llvm.instrprof.increment` is the number of injected
// counters, with each counter having a counter ID from `0..num_counters-1`. MIR optimization
// may split and duplicate some BasicBlock sequences. Simply counting the calls may not
// work; but computing the num_counters by adding `1` to the highest counter_id (for a given
// instrumented function) is valid.
//
// `num_expressions` is the number of counter expressions added to the MIR body. Both
// `num_counters` and `num_expressions` are used to initialize new vectors, during backend
// code generate, to lookup counters and expressions by simple u32 indexes.
let mut coverage_visitor =
CoverageVisitor { info: CoverageInfo { num_counters: 0, num_expressions: 0 } };
coverage_visitor.visit_body(mir_body);
coverage_visitor.info
}
impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
fn run_pass(
&self,
tcx: TyCtxt<'tcx>,
mir_source: MirSource<'tcx>,
mir_body: &mut mir::Body<'tcx>,
) {
// If the InstrumentCoverage pass is called on promoted MIRs, skip them.
// See: https://github.com/rust-lang/rust/pull/73011#discussion_r438317601
if mir_source.promoted.is_none() {
Instrumentor::new(&self.name(), tcx, mir_source, mir_body).inject_counters();
}
}
}
#[derive(Clone)]
struct CoverageRegion {
pub span: Span,
pub blocks: Vec<BasicBlock>,
}
struct Instrumentor<'a, 'tcx> {
pass_name: &'a str,
tcx: TyCtxt<'tcx>,
mir_source: MirSource<'tcx>,
mir_body: &'a mut mir::Body<'tcx>,
hir_body: &'tcx rustc_hir::Body<'tcx>,
function_source_hash: Option<u64>,
num_counters: u32,
num_expressions: u32,
}
impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
fn new(
pass_name: &'a str,
tcx: TyCtxt<'tcx>,
mir_source: MirSource<'tcx>,
mir_body: &'a mut mir::Body<'tcx>,
) -> Self {
let hir_body = hir_body(tcx, mir_source.def_id());
Self {
pass_name,
tcx,
mir_source,
mir_body,
hir_body,
function_source_hash: None,
num_counters: 0,
num_expressions: 0,
}
}
/// Counter IDs start from zero and go up.
fn next_counter(&mut self) -> CounterValueReference {
assert!(self.num_counters < u32::MAX - self.num_expressions);
let next = self.num_counters;
self.num_counters += 1;
CounterValueReference::from(next)
}
/// Expression IDs start from u32::MAX and go down because a CounterExpression can reference
/// (add or subtract counts) of both Counter regions and CounterExpression regions. The counter
/// expression operand IDs must be unique across both types.
fn next_expression(&mut self) -> InjectedExpressionIndex {
assert!(self.num_counters < u32::MAX - self.num_expressions);
let next = u32::MAX - self.num_expressions;
self.num_expressions += 1;
InjectedExpressionIndex::from(next)
}
fn function_source_hash(&mut self) -> u64 {
match self.function_source_hash {
Some(hash) => hash,
None => {
let hash = hash_mir_source(self.tcx, self.hir_body);
self.function_source_hash.replace(hash);
hash
}
}
}
fn inject_counters(&mut self) {
let tcx = self.tcx;
let def_id = self.mir_source.def_id();
let mir_body = &self.mir_body;
let body_span = self.hir_body.value.span;
debug!(
"instrumenting {:?}, span: {}",
def_id,
tcx.sess.source_map().span_to_string(body_span)
);
if !tcx.sess.opts.debugging_opts.experimental_coverage {
// Coverage at the function level should be accurate. This is the default implementation
// if `-Z experimental-coverage` is *NOT* enabled.
let block = rustc_middle::mir::START_BLOCK;
let counter = self.make_counter();
self.inject_statement(counter, body_span, block);
return;
}
// FIXME(richkadel): else if `-Z experimental-coverage` *IS* enabled: Efforts are still in
// progress to identify the correct code region spans and associated counters to generate
// accurate Rust coverage reports.
let block_span = |data: &BasicBlockData<'tcx>| {
// The default span will be the `Terminator` span; but until we have a smarter solution,
// the coverage region also incorporates at least the statements in this BasicBlock as
// well. Extend the span to encompass all, if possible.
// FIXME(richkadel): Assuming the terminator's span is already known to be contained in `body_span`.
let mut span = data.terminator().source_info.span;
// FIXME(richkadel): It's looking unlikely that we should compute a span from MIR
// spans, but if we do keep something like this logic, we will need a smarter way
// to combine `Statement`s and/or `Terminator`s with `Span`s from different
// files.
for statement_span in data.statements.iter().map(|statement| statement.source_info.span)
{
// Only combine Spans from the function's body_span.
if body_span.contains(statement_span) {
span = span.to(statement_span);
}
}
span
};
// Traverse the CFG but ignore anything following an `unwind`
let cfg_without_unwind = ShortCircuitPreorder::new(mir_body, |term_kind| {
let mut successors = term_kind.successors();
match &term_kind {
// SwitchInt successors are never unwind, and all of them should be traversed
TerminatorKind::SwitchInt { .. } => successors,
// For all other kinds, return only the first successor, if any, and ignore unwinds
_ => successors.next().into_iter().chain(&[]),
}
});
let mut coverage_regions = Vec::with_capacity(cfg_without_unwind.size_hint().0);
for (bb, data) in cfg_without_unwind {
if !body_span.contains(data.terminator().source_info.span) {
continue;
}
// FIXME(richkadel): Regions will soon contain multiple blocks.
let mut blocks = Vec::new();
blocks.push(bb);
let span = block_span(data);
coverage_regions.push(CoverageRegion { span, blocks });
}
let span_viewables = if pretty::dump_enabled(tcx, self.pass_name, def_id) {
Some(self.span_viewables(&coverage_regions))
} else {
None
};
// Inject counters for the selected spans
for CoverageRegion { span, blocks } in coverage_regions {
debug!(
"Injecting counter at: {:?}:\n{}\n==========",
span,
tcx.sess.source_map().span_to_snippet(span).expect("Error getting source for span"),
);
let counter = self.make_counter();
self.inject_statement(counter, span, blocks[0]);
}
if let Some(span_viewables) = span_viewables {
let mut file =
pretty::create_dump_file(tcx, "html", None, self.pass_name, &0, self.mir_source)
.expect("Unexpected error creating MIR spanview HTML file");
write_spanview_document(tcx, def_id, span_viewables, &mut file)
.expect("Unexpected IO error dumping coverage spans as HTML");
}
// FIXME(richkadel): Some regions will be counted by "counter expression". Counter
// expressions are supported, but are not yet generated. When they are, remove this `fake_use`
// block.
let fake_use = false;
if fake_use {
let add = false;
let fake_counter = CoverageKind::Counter {
function_source_hash: self.function_source_hash(),
id: CounterValueReference::from_u32(1),
};
let fake_expression = CoverageKind::Expression {
id: InjectedExpressionIndex::from(u32::MAX - 1),
lhs: ExpressionOperandId::from_u32(1),
op: Op::Add,
rhs: ExpressionOperandId::from_u32(2),
};
let lhs = fake_counter.as_operand_id();
let op = if add { Op::Add } else { Op::Subtract };
let rhs = fake_expression.as_operand_id();
let block = rustc_middle::mir::START_BLOCK;
let expression = self.make_expression(lhs, op, rhs);
self.inject_statement(expression, body_span, block);
}
}
fn make_counter(&mut self) -> CoverageKind {
CoverageKind::Counter {
function_source_hash: self.function_source_hash(),
id: self.next_counter(),
}
}
fn make_expression(
&mut self,
lhs: ExpressionOperandId,
op: Op,
rhs: ExpressionOperandId,
) -> CoverageKind {
CoverageKind::Expression { id: self.next_expression(), lhs, op, rhs }
}
fn inject_statement(&mut self, coverage_kind: CoverageKind, span: Span, block: BasicBlock) {
let code_region = make_code_region(self.tcx, &span);
debug!(" injecting statement {:?} covering {:?}", coverage_kind, code_region);
let data = &mut self.mir_body[block];
let source_info = data.terminator().source_info;
let statement = Statement {
source_info,
kind: StatementKind::Coverage(box Coverage { kind: coverage_kind, code_region }),
};
data.statements.push(statement);
}
/// Converts the computed `CoverageRegion`s into `SpanViewable`s.
fn span_viewables(&self, coverage_regions: &Vec<CoverageRegion>) -> Vec<SpanViewable> {
let mut span_viewables = Vec::new();
for coverage_region in coverage_regions {
span_viewables.push(SpanViewable {
span: coverage_region.span,
id: format!("{}", coverage_region.blocks[0].index()),
tooltip: self.make_tooltip_text(coverage_region),
});
}
span_viewables
}
/// A custom tooltip renderer used in a spanview HTML+CSS document used for coverage analysis.
fn make_tooltip_text(&self, coverage_region: &CoverageRegion) -> String {
const INCLUDE_COVERAGE_STATEMENTS: bool = false;
let tcx = self.tcx;
let source_map = tcx.sess.source_map();
let mut text = Vec::new();
for (i, &bb) in coverage_region.blocks.iter().enumerate() {
if i > 0 {
text.push("\n".to_owned());
}
text.push(format!("{:?}: {}:", bb, &source_map.span_to_string(coverage_region.span)));
let data = &self.mir_body.basic_blocks()[bb];
for statement in &data.statements {
let statement_string = match statement.kind {
StatementKind::Coverage(box ref coverage) => match coverage.kind {
CoverageKind::Counter { id, .. } => {
if !INCLUDE_COVERAGE_STATEMENTS {
continue;
}
format!("increment counter #{}", id.index())
}
CoverageKind::Expression { id, lhs, op, rhs } => {
if !INCLUDE_COVERAGE_STATEMENTS {
continue;
}
format!(
"expression #{} = {} {} {}",
id.index(),
lhs.index(),
if op == Op::Add { "+" } else { "-" },
rhs.index()
)
}
CoverageKind::Unreachable => {
if !INCLUDE_COVERAGE_STATEMENTS {
continue;
}
String::from("unreachable")
}
},
_ => format!("{:?}", statement),
};
let source_range = source_range_no_file(tcx, &statement.source_info.span);
text.push(format!(
"\n{}{}: {}: {}",
TOOLTIP_INDENT,
source_range,
statement_kind_name(statement),
statement_string
));
}
let term = data.terminator();
let source_range = source_range_no_file(tcx, &term.source_info.span);
text.push(format!(
"\n{}{}: {}: {:?}",
TOOLTIP_INDENT,
source_range,
terminator_kind_name(term),
term.kind
));
}
text.join("")
}
}
/// Convert the Span into its file name, start line and column, and end line and column
fn make_code_region<'tcx>(tcx: TyCtxt<'tcx>, span: &Span) -> CodeRegion {
let source_map = tcx.sess.source_map();
let start = source_map.lookup_char_pos(span.lo());
let end = if span.hi() == span.lo() {
start.clone()
} else {
let end = source_map.lookup_char_pos(span.hi());
debug_assert_eq!(
start.file.name,
end.file.name,
"Region start ({:?} -> {:?}) and end ({:?} -> {:?}) don't come from the same source file!",
span.lo(),
start,
span.hi(),
end
);
end
};
match &start.file.name {
FileName::Real(RealFileName::Named(path)) => CodeRegion {
file_name: Symbol::intern(&path.to_string_lossy()),
start_line: start.line as u32,
start_col: start.col.to_u32() + 1,
end_line: end.line as u32,
end_col: end.col.to_u32() + 1,
},
_ => bug!("start.file.name should be a RealFileName, but it was: {:?}", start.file.name),
}
}
fn hir_body<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx rustc_hir::Body<'tcx> {
let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local");
let fn_body_id = hir::map::associated_body(hir_node).expect("HIR node is a function with body");
tcx.hir().body(fn_body_id)
}
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>) -> u64 {
let mut hcx = tcx.create_no_span_stable_hashing_context();
hash(&mut hcx, &hir_body.value).to_smaller_hash()
}
fn hash(
hcx: &mut StableHashingContext<'tcx>,
node: &impl HashStable<StableHashingContext<'tcx>>,
) -> Fingerprint {
let mut stable_hasher = StableHasher::new();
node.hash_stable(hcx, &mut stable_hasher);
stable_hasher.finish()
}
pub struct ShortCircuitPreorder<
'a,
'tcx,
F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>,
> {
body: &'a mir::Body<'tcx>,
visited: BitSet<BasicBlock>,
worklist: Vec<BasicBlock>,
filtered_successors: F,
}
impl<'a, 'tcx, F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>>
ShortCircuitPreorder<'a, 'tcx, F>
{
pub fn new(
body: &'a mir::Body<'tcx>,
filtered_successors: F,
) -> ShortCircuitPreorder<'a, 'tcx, F> {
let worklist = vec![mir::START_BLOCK];
ShortCircuitPreorder {
body,
visited: BitSet::new_empty(body.basic_blocks().len()),
worklist,
filtered_successors,
}
}
}
impl<'a: 'tcx, 'tcx, F: Fn(&'tcx TerminatorKind<'tcx>) -> mir::Successors<'tcx>> Iterator
for ShortCircuitPreorder<'a, 'tcx, F>
{
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
while let Some(idx) = self.worklist.pop() {
if !self.visited.insert(idx) {
continue;
}
let data = &self.body[idx];
if let Some(ref term) = data.terminator {
self.worklist.extend((self.filtered_successors)(&term.kind));
}
return Some((idx, data));
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.body.basic_blocks().len() - self.visited.count();
(size, Some(size))
}
}
|