1//! Finds local items that are "reachable", which means that other crates need access to their
2//! compiled code or their *runtime* MIR. (Compile-time MIR is always encoded anyway, so we don't
3//! worry about that here.)
4//!
5//! An item is "reachable" if codegen that happens in downstream crates can end up referencing this
6//! item. This obviously includes all public items. However, some of these items cannot be codegen'd
7//! (because they are generic), and for some the compiled code is not sufficient (because we want to
8//! cross-crate inline them). These items "need cross-crate MIR". When a reachable function `f`
9//! needs cross-crate MIR, then its MIR may be codegen'd in a downstream crate, and hence items it
10//! mentions need to be considered reachable.
11//!
12//! Furthermore, if a `const`/`const fn` is reachable, then it can return pointers to other items,
13//! making those reachable as well. For instance, consider a `const fn` returning a pointer to an
14//! otherwise entirely private function: if a downstream crate calls that `const fn` to compute the
15//! initial value of a `static`, then it needs to generate a direct reference to this function --
16//! i.e., the function is directly reachable from that downstream crate! Hence we have to recurse
17//! into `const` and `const fn`.
18//!
19//! Conversely, reachability *stops* when it hits a monomorphic non-`const` function that we do not
20//! want to cross-crate inline. That function will just be codegen'd in this crate, which means the
21//! monomorphization collector will consider it a root and then do another graph traversal to
22//! codegen everything called by this function -- but that's a very different graph from what we are
23//! considering here as at that point, everything is monomorphic.
2425use hir::def_id::LocalDefIdSet;
26use rustc_data_structures::stack::ensure_sufficient_stack;
27use rustc_hiras hir;
28use rustc_hir::Node;
29use rustc_hir::def::{DefKind, Res};
30use rustc_hir::def_id::{DefId, LocalDefId};
31use rustc_hir::intravisit::{self, Visitor};
32use rustc_middle::bug;
33use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
34use rustc_middle::middle::privacy::{self, Level};
35use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc};
36use rustc_middle::query::Providers;
37use rustc_middle::ty::{self, ExistentialTraitRef, TyCtxt};
38use rustc_privacy::DefIdVisitor;
39use rustc_session::config::CrateType;
40use tracing::debug;
4142/// Determines whether this item is recursive for reachability. See `is_recursively_reachable_local`
43/// below for details.
44fn recursively_reachable(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
45tcx.generics_of(def_id).requires_monomorphization(tcx)
46 || tcx.cross_crate_inlinable(def_id)
47 || tcx.is_const_fn(def_id)
48}
4950// Information needed while computing reachability.
51struct ReachableContext<'tcx> {
52// The type context.
53tcx: TyCtxt<'tcx>,
54 maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
55// The set of items which must be exported in the linkage sense.
56reachable_symbols: LocalDefIdSet,
57// A worklist of item IDs. Each item ID in this worklist will be inlined
58 // and will be scanned for further references.
59 // FIXME(eddyb) benchmark if this would be faster as a `VecDeque`.
60worklist: Vec<LocalDefId>,
61// Whether any output of this compilation is a library
62any_library: bool,
63}
6465impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
66fn visit_nested_body(&mut self, body: hir::BodyId) {
67let old_maybe_typeck_results =
68self.maybe_typeck_results.replace(self.tcx.typeck_body(body));
69let body = self.tcx.hir_body(body);
70self.visit_body(body);
71self.maybe_typeck_results = old_maybe_typeck_results;
72 }
7374fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
75let res = match expr.kind {
76 hir::ExprKind::Path(ref qpath) => {
77// This covers fn ptr casts but also "non-method" calls.
78Some(self.typeck_results().qpath_res(qpath, expr.hir_id))
79 }
80 hir::ExprKind::MethodCall(..) => {
81// Method calls don't involve a full "path", so we need to determine the callee
82 // based on the receiver type.
83 // If this is a method call on a generic type, we might not be able to find the
84 // callee. That's why `reachable_set` also adds all potential callees for such
85 // calls, i.e. all trait impl items, to the reachable set. So here we only worry
86 // about the calls we can identify.
87self.typeck_results()
88 .type_dependent_def(expr.hir_id)
89 .map(|(kind, def_id)| Res::Def(kind, def_id))
90 }
91 hir::ExprKind::Closure(&hir::Closure { def_id, .. }) => {
92self.reachable_symbols.insert(def_id);
93None94 }
95_ => None,
96 };
9798if let Some(res) = res {
99self.propagate_item(res);
100 }
101102 intravisit::walk_expr(self, expr)
103 }
104105fn visit_inline_asm(&mut self, asm: &'tcx hir::InlineAsm<'tcx>, id: hir::HirId) {
106for (op, _) in asm.operands {
107if let hir::InlineAsmOperand::SymStatic { def_id, .. } = op
108 && let Some(def_id) = def_id.as_local()
109 {
110self.reachable_symbols.insert(def_id);
111 }
112 }
113 intravisit::walk_inline_asm(self, asm, id);
114 }
115}
116117impl<'tcx> ReachableContext<'tcx> {
118/// Gets the type-checking results for the current body.
119 /// As this will ICE if called outside bodies, only call when working with
120 /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
121#[track_caller]
122fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
123self.maybe_typeck_results
124 .expect("`ReachableContext::typeck_results` called outside of body")
125 }
126127/// Returns true if the given def ID represents a local item that is recursive for reachability,
128 /// i.e. whether everything mentioned in here also needs to be considered reachable.
129 ///
130 /// There are two reasons why an item may be recursively reachable:
131 /// - It needs cross-crate MIR (see the module-level doc comment above).
132 /// - It is a `const` or `const fn`. This is *not* because we need the MIR to interpret them
133 /// (MIR for const-eval and MIR for codegen is separate, and MIR for const-eval is always
134 /// encoded). Instead, it is because `const fn` can create `fn()` pointers to other items
135 /// which end up in the evaluated result of the constant and can then be called from other
136 /// crates. Those items must be considered reachable.
137fn is_recursively_reachable_local(&self, def_id: DefId) -> bool {
138let Some(def_id) = def_id.as_local() else {
139return false;
140 };
141142match self.tcx.hir_node_by_def_id(def_id) {
143 Node::Item(item) => match item.kind {
144 hir::ItemKind::Fn { .. } => recursively_reachable(self.tcx, def_id.into()),
145_ => false,
146 },
147 Node::TraitItem(trait_method) => match trait_method.kind {
148 hir::TraitItemKind::Const(_, ref default, _) => default.is_some(),
149 hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => true,
150 hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
151 | hir::TraitItemKind::Type(..) => false,
152 },
153 Node::ImplItem(impl_item) => match impl_item.kind {
154 hir::ImplItemKind::Const(..) => true,
155 hir::ImplItemKind::Fn(..) => {
156recursively_reachable(self.tcx, impl_item.hir_id().owner.to_def_id())
157 }
158 hir::ImplItemKind::Type(_) => false,
159 },
160 Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => true,
161_ => false,
162 }
163 }
164165// Step 2: Mark all symbols that the symbols on the worklist touch.
166fn propagate(&mut self) {
167let mut scanned = LocalDefIdSet::default();
168while let Some(search_item) = self.worklist.pop() {
169if !scanned.insert(search_item) {
170continue;
171 }
172173self.propagate_node(&self.tcx.hir_node_by_def_id(search_item), search_item);
174 }
175 }
176177fn propagate_node(&mut self, node: &Node<'tcx>, search_item: LocalDefId) {
178if !self.any_library {
179// If we are building an executable, only explicitly extern
180 // types need to be exported.
181let codegen_attrs = if self.tcx.def_kind(search_item).has_codegen_attrs() {
182self.tcx.codegen_fn_attrs(search_item)
183 } else {
184CodegenFnAttrs::EMPTY185 };
186let is_extern = codegen_attrs.contains_extern_indicator();
187// Right now, the only way to get "foreign item symbol aliases" is by being an EII-implementation.
188 // EII implementations will generate under their own name but also under the name of some foreign item
189 // (hence alias) that may be in another crate. These functions are marked as always-reachable since
190 // it's very hard to track whether the original foreign item was reachable. It may live in another crate
191 // and may be reachable from sibling crates.
192let has_foreign_aliases_eii = !codegen_attrs.foreign_item_symbol_aliases.is_empty();
193if is_extern || has_foreign_aliases_eii {
194self.reachable_symbols.insert(search_item);
195 }
196 } else {
197// If we are building a library, then reachable symbols will
198 // continue to participate in linkage after this product is
199 // produced. In this case, we traverse the ast node, recursing on
200 // all reachable nodes from this one.
201self.reachable_symbols.insert(search_item);
202 }
203204match *node {
205 Node::Item(item) => {
206match item.kind {
207 hir::ItemKind::Fn { body, .. } => {
208if recursively_reachable(self.tcx, item.owner_id.into()) {
209self.visit_nested_body(body);
210 }
211 }
212// For `type const` we want to evaluate the RHS.
213hir::ItemKind::Const(_, _, _, init @ hir::ConstItemRhs::TypeConst(_)) => {
214self.visit_const_item_rhs(init);
215 }
216 hir::ItemKind::Const(_, _, _, init) => {
217if self.tcx.generics_of(item.owner_id).own_requires_monomorphization() {
218// In this case, we don't want to evaluate the const initializer.
219 // In lieu of that, we have to consider everything mentioned in it
220 // as reachable, since it *may* end up in the final value.
221self.visit_const_item_rhs(init);
222return;
223 }
224225match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) {
226Ok(alloc) => {
227// Only things actually ending up in the final constant value are
228 // reachable for codegen. Everything else is only needed during
229 // const-eval, so even if const-eval happens in a downstream crate,
230 // all they need is `mir_for_ctfe`.
231let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory();
232self.propagate_from_alloc(alloc);
233 }
234// Trivially unsatisfiable bounds on the item prevented us from
235 // normalizing the initializer. Similar to the other case, we have to
236 // everything mentioned in it as reachable.
237Err(ErrorHandled::TooGeneric(_)) => self.visit_const_item_rhs(init),
238// If there was an error evaluating the const, nothing can be reachable
239 // via it, and anyway compilation will fail.
240Err(ErrorHandled::Reported(..)) => {}
241 }
242 }
243 hir::ItemKind::Static(..) => {
244if let Ok(alloc) = self.tcx.eval_static_initializer(item.owner_id.def_id) {
245self.propagate_from_alloc(alloc);
246 }
247 }
248249// These are normal, nothing reachable about these
250 // inherently and their children are already in the
251 // worklist, as determined by the privacy pass
252hir::ItemKind::ExternCrate(..)
253 | hir::ItemKind::Use(..)
254 | hir::ItemKind::TyAlias(..)
255 | hir::ItemKind::Macro(..)
256 | hir::ItemKind::Mod(..)
257 | hir::ItemKind::ForeignMod { .. }
258 | hir::ItemKind::Impl { .. }
259 | hir::ItemKind::Trait(..)
260 | hir::ItemKind::TraitAlias(..)
261 | hir::ItemKind::Struct(..)
262 | hir::ItemKind::Enum(..)
263 | hir::ItemKind::Union(..)
264 | hir::ItemKind::GlobalAsm { .. } => {}
265 }
266 }
267 Node::TraitItem(trait_method) => {
268match trait_method.kind {
269 hir::TraitItemKind::Const(_, None, _)
270 | hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
271// Keep going, nothing to get exported
272}
273 hir::TraitItemKind::Const(_, Some(rhs), _) => self.visit_const_item_rhs(rhs),
274 hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body_id)) => {
275self.visit_nested_body(body_id);
276 }
277 hir::TraitItemKind::Type(..) => {}
278 }
279 }
280 Node::ImplItem(impl_item) => match impl_item.kind {
281 hir::ImplItemKind::Const(_, rhs) => {
282self.visit_const_item_rhs(rhs);
283 }
284 hir::ImplItemKind::Fn(_, body) => {
285if recursively_reachable(self.tcx, impl_item.hir_id().owner.to_def_id()) {
286self.visit_nested_body(body)
287 }
288 }
289 hir::ImplItemKind::Type(_) => {}
290 },
291 Node::Expr(&hir::Expr {
292 kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
293 ..
294 }) => {
295self.visit_nested_body(body);
296 }
297// Nothing to recurse on for these
298Node::ForeignItem(_)
299 | Node::Variant(_)
300 | Node::Ctor(..)
301 | Node::Field(_)
302 | Node::Ty(_)
303 | Node::Crate(_)
304 | Node::Synthetic305 | Node::OpaqueTy(..) => {}
306_ => {
307::rustc_middle::util::bug::bug_fmt(format_args!("found unexpected node kind in worklist: {0} ({1:?})",
self.tcx.hir_id_to_string(self.tcx.local_def_id_to_hir_id(search_item)),
node));bug!(
308"found unexpected node kind in worklist: {} ({:?})",
309self.tcx.hir_id_to_string(self.tcx.local_def_id_to_hir_id(search_item)),
310 node,
311 );
312 }
313 }
314 }
315316/// Finds things to add to `reachable_symbols` within allocations.
317 /// In contrast to visit_nested_body this ignores things that were only needed to evaluate
318 /// the allocation.
319fn propagate_from_alloc(&mut self, alloc: ConstAllocation<'tcx>) {
320if !self.any_library {
321return;
322 }
323for (_, prov) in alloc.0.provenance().ptrs().iter() {
324match self.tcx.global_alloc(prov.alloc_id()) {
325 GlobalAlloc::Static(def_id) => {
326self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
327 }
328 GlobalAlloc::Function { instance, .. } => {
329// Manually visit to actually see the instance's `DefId`. Type visitors won't see it
330self.propagate_item(Res::Def(
331self.tcx.def_kind(instance.def_id()),
332 instance.def_id(),
333 ));
334self.visit(instance.args);
335 }
336 GlobalAlloc::VTable(ty, dyn_ty) => {
337self.visit(ty);
338// Manually visit to actually see the trait's `DefId`. Type visitors won't see it
339if let Some(trait_ref) = dyn_ty.principal() {
340let ExistentialTraitRef { def_id, args, .. } = trait_ref.skip_binder();
341self.visit_def_id(def_id, "", &"");
342self.visit(args);
343 }
344 }
345 GlobalAlloc::TypeId { ty, .. } => self.visit(ty),
346 GlobalAlloc::Memory(alloc) => self.propagate_from_alloc(alloc),
347 }
348 }
349 }
350351fn propagate_item(&mut self, res: Res) {
352let Res::Def(kind, def_id) = reselse { return };
353let Some(def_id) = def_id.as_local() else { return };
354match kind {
355 DefKind::Static { nested: true, .. } => {
356// This is the main purpose of this function: add the def_id we find
357 // to `reachable_symbols`.
358if self.reachable_symbols.insert(def_id) {
359if let Ok(alloc) = self.tcx.eval_static_initializer(def_id) {
360// This cannot cause infinite recursion, because we abort by inserting into the
361 // work list once we hit a normal static. Nested statics, even if they somehow
362 // become recursive, are also not infinitely recursing, because of the
363 // `reachable_symbols` check above.
364 // We still need to protect against stack overflow due to deeply nested statics.
365ensure_sufficient_stack(|| self.propagate_from_alloc(alloc));
366 }
367 }
368 }
369// Reachable constants and reachable statics can have their contents inlined
370 // into other crates. Mark them as reachable and recurse into their body.
371DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::Static { .. } => {
372self.worklist.push(def_id);
373 }
374_ => {
375if self.is_recursively_reachable_local(def_id.to_def_id()) {
376self.worklist.push(def_id);
377 } else {
378self.reachable_symbols.insert(def_id);
379 }
380 }
381 }
382 }
383}
384385impl<'tcx> DefIdVisitor<'tcx> for ReachableContext<'tcx> {
386type Result = ();
387388fn tcx(&self) -> TyCtxt<'tcx> {
389self.tcx
390 }
391392fn visit_def_id(
393&mut self,
394 def_id: DefId,
395 _kind: &str,
396 _descr: &dyn std::fmt::Display,
397 ) -> Self::Result {
398self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
399 }
400}
401402fn check_item<'tcx>(
403 tcx: TyCtxt<'tcx>,
404 id: hir::ItemId,
405 worklist: &mut Vec<LocalDefId>,
406 effective_visibilities: &privacy::EffectiveVisibilities,
407) {
408if has_custom_linkage(tcx, id.owner_id.def_id) {
409worklist.push(id.owner_id.def_id);
410 }
411412if !#[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(id.owner_id) {
DefKind::Impl { of_trait: true } => true,
_ => false,
}matches!(tcx.def_kind(id.owner_id), DefKind::Impl { of_trait: true }) {
413return;
414 }
415416// We need only trait impls here, not inherent impls, and only non-exported ones
417if effective_visibilities.is_reachable(id.owner_id.def_id) {
418return;
419 }
420421let items = tcx.associated_item_def_ids(id.owner_id);
422worklist.extend(items.iter().map(|ii_ref| ii_ref.expect_local()));
423424let trait_def_id = tcx.impl_trait_id(id.owner_id.to_def_id());
425426if !trait_def_id.is_local() {
427return;
428 }
429430worklist431 .extend(tcx.provided_trait_methods(trait_def_id).map(|assoc| assoc.def_id.expect_local()));
432}
433434fn has_custom_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
435// Anything which has custom linkage gets thrown on the worklist no
436 // matter where it is in the crate, along with "special std symbols"
437 // which are currently akin to allocator symbols.
438if !tcx.def_kind(def_id).has_codegen_attrs() {
439return false;
440 }
441442let codegen_attrs = tcx.codegen_fn_attrs(def_id);
443codegen_attrs.contains_extern_indicator()
444// FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by
445 // `linked_symbols` in cg_ssa. They won't be exported in binary or cdylib due to their
446 // `SymbolExportLevel::Rust` export level but may end up being exported in dylibs.
447|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
448 || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
449// Right now, the only way to get "foreign item symbol aliases" is by being an EII-implementation.
450 // EII implementations will generate under their own name but also under the name of some foreign item
451 // (hence alias) that may be in another crate. These functions are marked as always-reachable since
452 // it's very hard to track whether the original foreign item was reachable. It may live in another crate
453 // and may be reachable from sibling crates.
454|| !codegen_attrs.foreign_item_symbol_aliases.is_empty()
455}
456457/// See module-level doc comment above.
458fn reachable_set(tcx: TyCtxt<'_>, (): ()) -> LocalDefIdSet {
459let effective_visibilities = &tcx.effective_visibilities(());
460461let any_library = tcx.crate_types().iter().any(|ty| {
462*ty == CrateType::Rlib463 || *ty == CrateType::Dylib464 || *ty == CrateType::ProcMacro465 || *ty == CrateType::Sdylib466 });
467let mut reachable_context = ReachableContext {
468tcx,
469 maybe_typeck_results: None,
470 reachable_symbols: Default::default(),
471 worklist: Vec::new(),
472any_library,
473 };
474475// Step 1: Seed the worklist with all nodes which were found to be public as
476 // a result of the privacy pass along with all local lang items and impl items.
477 // If other crates link to us, they're going to expect to be able to
478 // use the lang items, so we need to be sure to mark them as
479 // exported.
480reachable_context.worklist = effective_visibilities481 .iter()
482 .filter_map(|(&id, effective_vis)| {
483effective_vis.is_public_at_level(Level::ReachableThroughImplTrait).then_some(id)
484 })
485 .collect::<Vec<_>>();
486487for (_, def_id) in tcx.lang_items().iter() {
488if let Some(def_id) = def_id.as_local() {
489 reachable_context.worklist.push(def_id);
490 }
491 }
492 {
493// As explained above, we have to mark all functions called from reachable
494 // `item_might_be_inlined` items as reachable. The issue is, when those functions are
495 // generic and call a trait method, we have no idea where that call goes! So, we
496 // conservatively mark all trait impl items as reachable.
497 // FIXME: One possible strategy for pruning the reachable set is to avoid marking impl
498 // items of non-exported traits (or maybe all local traits?) unless their respective
499 // trait items are used from inlinable code through method call syntax or UFCS, or their
500 // trait is a lang item.
501 // (But if you implement this, don't forget to take into account that vtables can also
502 // make trait methods reachable!)
503let crate_items = tcx.hir_crate_items(());
504505for id in crate_items.free_items() {
506 check_item(tcx, id, &mut reachable_context.worklist, effective_visibilities);
507 }
508509for id in crate_items.impl_items() {
510if has_custom_linkage(tcx, id.owner_id.def_id) {
511 reachable_context.worklist.push(id.owner_id.def_id);
512 }
513 }
514 }
515516// Step 2: Mark all symbols that the symbols on the worklist touch.
517reachable_context.propagate();
518519{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_passes/src/reachable.rs:519",
"rustc_passes::reachable", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_passes/src/reachable.rs"),
::tracing_core::__macro_support::Option::Some(519u32),
::tracing_core::__macro_support::Option::Some("rustc_passes::reachable"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Inline reachability shows: {0:?}",
reachable_context.reachable_symbols) as &dyn Value))])
});
} else { ; }
};debug!("Inline reachability shows: {:?}", reachable_context.reachable_symbols);
520521// Return the set of reachable symbols.
522reachable_context.reachable_symbols
523}
524525pub(crate) fn provide(providers: &mut Providers) {
526*providers = Providers { reachable_set, ..*providers };
527}