Skip to content

Commit 4f80489

Browse files
authored
Rollup merge of #144823 - Zalathar:hir-info, r=oli-obk
coverage: Extract HIR-related helper code out of the main module This is a decent chunk of helper code with a narrow external interface (one function returning one struct), making it a good candidate to be extracted out of the main `rustc_mir_transform::coverage` module. No functional changes.
2 parents 2053728 + db9f0bb commit 4f80489

File tree

4 files changed

+142
-136
lines changed

4 files changed

+142
-136
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use rustc_hir as hir;
2+
use rustc_hir::intravisit::{Visitor, walk_expr};
3+
use rustc_middle::hir::nested_filter;
4+
use rustc_middle::ty::TyCtxt;
5+
use rustc_span::Span;
6+
use rustc_span::def_id::LocalDefId;
7+
8+
/// Function information extracted from HIR by the coverage instrumentor.
9+
#[derive(Debug)]
10+
pub(crate) struct ExtractedHirInfo {
11+
pub(crate) function_source_hash: u64,
12+
pub(crate) is_async_fn: bool,
13+
/// The span of the function's signature, if available.
14+
/// Must have the same context and filename as the body span.
15+
pub(crate) fn_sig_span: Option<Span>,
16+
pub(crate) body_span: Span,
17+
/// "Holes" are regions within the function body (or its expansions) that
18+
/// should not be included in coverage spans for this function
19+
/// (e.g. closures and nested items).
20+
pub(crate) hole_spans: Vec<Span>,
21+
}
22+
23+
pub(crate) fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo {
24+
// FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
25+
// to HIR for it.
26+
27+
// HACK: For synthetic MIR bodies (async closures), use the def id of the HIR body.
28+
if tcx.is_synthetic_mir(def_id) {
29+
return extract_hir_info(tcx, tcx.local_parent(def_id));
30+
}
31+
32+
let hir_node = tcx.hir_node_by_def_id(def_id);
33+
let fn_body_id = hir_node.body_id().expect("HIR node is a function with body");
34+
let hir_body = tcx.hir_body(fn_body_id);
35+
36+
let maybe_fn_sig = hir_node.fn_sig();
37+
let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async());
38+
39+
let mut body_span = hir_body.value.span;
40+
41+
use hir::{Closure, Expr, ExprKind, Node};
42+
// Unexpand a closure's body span back to the context of its declaration.
43+
// This helps with closure bodies that consist of just a single bang-macro,
44+
// and also with closure bodies produced by async desugaring.
45+
if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) =
46+
hir_node
47+
{
48+
body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span);
49+
}
50+
51+
// The actual signature span is only used if it has the same context and
52+
// filename as the body, and precedes the body.
53+
let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| {
54+
let source_map = tcx.sess.source_map();
55+
let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
56+
57+
fn_sig_span.eq_ctxt(body_span)
58+
&& fn_sig_span.hi() <= body_span.lo()
59+
&& file_idx(fn_sig_span) == file_idx(body_span)
60+
});
61+
62+
let function_source_hash = hash_mir_source(tcx, hir_body);
63+
64+
let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
65+
66+
ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans }
67+
}
68+
69+
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {
70+
let owner = hir_body.id().hir_id.owner;
71+
tcx.hir_owner_nodes(owner)
72+
.opt_hash_including_bodies
73+
.expect("hash should be present when coverage instrumentation is enabled")
74+
.to_smaller_hash()
75+
.as_u64()
76+
}
77+
78+
fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {
79+
struct HolesVisitor<'tcx> {
80+
tcx: TyCtxt<'tcx>,
81+
hole_spans: Vec<Span>,
82+
}
83+
84+
impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> {
85+
/// We have special handling for nested items, but we still want to
86+
/// traverse into nested bodies of things that are not considered items,
87+
/// such as "anon consts" (e.g. array lengths).
88+
type NestedFilter = nested_filter::OnlyBodies;
89+
90+
fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
91+
self.tcx
92+
}
93+
94+
/// We override `visit_nested_item` instead of `visit_item` because we
95+
/// only need the item's span, not the item itself.
96+
fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result {
97+
let span = self.tcx.def_span(id.owner_id.def_id);
98+
self.visit_hole_span(span);
99+
// Having visited this item, we don't care about its children,
100+
// so don't call `walk_item`.
101+
}
102+
103+
// We override `visit_expr` instead of the more specific expression
104+
// visitors, so that we have direct access to the expression span.
105+
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
106+
match expr.kind {
107+
hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => {
108+
self.visit_hole_span(expr.span);
109+
// Having visited this expression, we don't care about its
110+
// children, so don't call `walk_expr`.
111+
}
112+
113+
// For other expressions, recursively visit as normal.
114+
_ => walk_expr(self, expr),
115+
}
116+
}
117+
}
118+
impl HolesVisitor<'_> {
119+
fn visit_hole_span(&mut self, hole_span: Span) {
120+
self.hole_spans.push(hole_span);
121+
}
122+
}
123+
124+
let mut visitor = HolesVisitor { tcx, hole_spans: vec![] };
125+
126+
visitor.visit_body(hir_body);
127+
visitor.hole_spans
128+
}

compiler/rustc_mir_transform/src/coverage/mappings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rustc_middle::mir::{self, BasicBlock, StatementKind};
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::Span;
66

7-
use crate::coverage::ExtractedHirInfo;
87
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
8+
use crate::coverage::hir_info::ExtractedHirInfo;
99
use crate::coverage::spans::extract_refined_covspans;
1010
use crate::coverage::unexpand::unexpand_into_body_span;
1111

Lines changed: 11 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
mod counters;
2-
mod graph;
3-
mod mappings;
4-
pub(super) mod query;
5-
mod spans;
6-
#[cfg(test)]
7-
mod tests;
8-
mod unexpand;
9-
10-
use rustc_hir as hir;
11-
use rustc_hir::intravisit::{Visitor, walk_expr};
12-
use rustc_middle::hir::nested_filter;
131
use rustc_middle::mir::coverage::{CoverageKind, FunctionCoverageInfo, Mapping, MappingKind};
142
use rustc_middle::mir::{self, BasicBlock, Statement, StatementKind, TerminatorKind};
153
use rustc_middle::ty::TyCtxt;
16-
use rustc_span::Span;
17-
use rustc_span::def_id::LocalDefId;
184
use tracing::{debug, debug_span, trace};
195

206
use crate::coverage::counters::BcbCountersData;
217
use crate::coverage::graph::CoverageGraph;
228
use crate::coverage::mappings::ExtractedMappings;
239

10+
mod counters;
11+
mod graph;
12+
mod hir_info;
13+
mod mappings;
14+
pub(super) mod query;
15+
mod spans;
16+
#[cfg(test)]
17+
mod tests;
18+
mod unexpand;
19+
2420
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
2521
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
2622
/// to construct the coverage map.
@@ -67,7 +63,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
6763
let def_id = mir_body.source.def_id();
6864
let _span = debug_span!("instrument_function_for_coverage", ?def_id).entered();
6965

70-
let hir_info = extract_hir_info(tcx, def_id.expect_local());
66+
let hir_info = hir_info::extract_hir_info(tcx, def_id.expect_local());
7167

7268
// Build the coverage graph, which is a simplified view of the MIR control-flow
7369
// graph that ignores some details not relevant to coverage instrumentation.
@@ -147,122 +143,3 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
147143
let statement = Statement::new(source_info, StatementKind::Coverage(counter_kind));
148144
data.statements.insert(0, statement);
149145
}
150-
151-
/// Function information extracted from HIR by the coverage instrumentor.
152-
#[derive(Debug)]
153-
struct ExtractedHirInfo {
154-
function_source_hash: u64,
155-
is_async_fn: bool,
156-
/// The span of the function's signature, if available.
157-
/// Must have the same context and filename as the body span.
158-
fn_sig_span: Option<Span>,
159-
body_span: Span,
160-
/// "Holes" are regions within the function body (or its expansions) that
161-
/// should not be included in coverage spans for this function
162-
/// (e.g. closures and nested items).
163-
hole_spans: Vec<Span>,
164-
}
165-
166-
fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHirInfo {
167-
// FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
168-
// to HIR for it.
169-
170-
// HACK: For synthetic MIR bodies (async closures), use the def id of the HIR body.
171-
if tcx.is_synthetic_mir(def_id) {
172-
return extract_hir_info(tcx, tcx.local_parent(def_id));
173-
}
174-
175-
let hir_node = tcx.hir_node_by_def_id(def_id);
176-
let fn_body_id = hir_node.body_id().expect("HIR node is a function with body");
177-
let hir_body = tcx.hir_body(fn_body_id);
178-
179-
let maybe_fn_sig = hir_node.fn_sig();
180-
let is_async_fn = maybe_fn_sig.is_some_and(|fn_sig| fn_sig.header.is_async());
181-
182-
let mut body_span = hir_body.value.span;
183-
184-
use hir::{Closure, Expr, ExprKind, Node};
185-
// Unexpand a closure's body span back to the context of its declaration.
186-
// This helps with closure bodies that consist of just a single bang-macro,
187-
// and also with closure bodies produced by async desugaring.
188-
if let Node::Expr(&Expr { kind: ExprKind::Closure(&Closure { fn_decl_span, .. }), .. }) =
189-
hir_node
190-
{
191-
body_span = body_span.find_ancestor_in_same_ctxt(fn_decl_span).unwrap_or(body_span);
192-
}
193-
194-
// The actual signature span is only used if it has the same context and
195-
// filename as the body, and precedes the body.
196-
let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| {
197-
let source_map = tcx.sess.source_map();
198-
let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
199-
200-
fn_sig_span.eq_ctxt(body_span)
201-
&& fn_sig_span.hi() <= body_span.lo()
202-
&& file_idx(fn_sig_span) == file_idx(body_span)
203-
});
204-
205-
let function_source_hash = hash_mir_source(tcx, hir_body);
206-
207-
let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
208-
209-
ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans }
210-
}
211-
212-
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {
213-
// FIXME(cjgillot) Stop hashing HIR manually here.
214-
let owner = hir_body.id().hir_id.owner;
215-
tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64()
216-
}
217-
218-
fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {
219-
struct HolesVisitor<'tcx> {
220-
tcx: TyCtxt<'tcx>,
221-
hole_spans: Vec<Span>,
222-
}
223-
224-
impl<'tcx> Visitor<'tcx> for HolesVisitor<'tcx> {
225-
/// We have special handling for nested items, but we still want to
226-
/// traverse into nested bodies of things that are not considered items,
227-
/// such as "anon consts" (e.g. array lengths).
228-
type NestedFilter = nested_filter::OnlyBodies;
229-
230-
fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
231-
self.tcx
232-
}
233-
234-
/// We override `visit_nested_item` instead of `visit_item` because we
235-
/// only need the item's span, not the item itself.
236-
fn visit_nested_item(&mut self, id: hir::ItemId) -> Self::Result {
237-
let span = self.tcx.def_span(id.owner_id.def_id);
238-
self.visit_hole_span(span);
239-
// Having visited this item, we don't care about its children,
240-
// so don't call `walk_item`.
241-
}
242-
243-
// We override `visit_expr` instead of the more specific expression
244-
// visitors, so that we have direct access to the expression span.
245-
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
246-
match expr.kind {
247-
hir::ExprKind::Closure(_) | hir::ExprKind::ConstBlock(_) => {
248-
self.visit_hole_span(expr.span);
249-
// Having visited this expression, we don't care about its
250-
// children, so don't call `walk_expr`.
251-
}
252-
253-
// For other expressions, recursively visit as normal.
254-
_ => walk_expr(self, expr),
255-
}
256-
}
257-
}
258-
impl HolesVisitor<'_> {
259-
fn visit_hole_span(&mut self, hole_span: Span) {
260-
self.hole_spans.push(hole_span);
261-
}
262-
}
263-
264-
let mut visitor = HolesVisitor { tcx, hole_spans: vec![] };
265-
266-
visitor.visit_body(hir_body);
267-
visitor.hole_spans
268-
}

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span};
77
use tracing::instrument;
88

99
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
10+
use crate::coverage::hir_info::ExtractedHirInfo;
1011
use crate::coverage::spans::from_mir::{Hole, RawSpanFromMir, SpanFromMir};
11-
use crate::coverage::{ExtractedHirInfo, mappings, unexpand};
12+
use crate::coverage::{mappings, unexpand};
1213

1314
mod from_mir;
1415

0 commit comments

Comments
 (0)