Skip to content

Commit d8bf3fd

Browse files
authored
Rollup merge of rust-lang#124904 - RalfJung:reachable, r=tmiasko
reachable computation: extend explanation of what this does, and why Follow-up to rust-lang#122769. I had the time to think about this some more, in particular in the context of rust-lang#119214, so I felt it was worth extending these comments some more. I also gave up on the context of "externally reachable" as it is not called that way anywhere else in the compiler. Cc `@tmiasko` `@saethlin`
2 parents 881fa5b + fd2af9b commit d8bf3fd

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

compiler/rustc_passes/src/reachable.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
//! Finds local items that are externally reachable, which means that other crates need access to
2-
//! their compiled machine code or their MIR.
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.)
34
//!
4-
//! An item is "externally reachable" if it is relevant for other crates. This obviously includes
5-
//! all public items. However, some of these items cannot be compiled to machine code (because they
6-
//! are generic), and for some the machine code is not sufficient (because we want to cross-crate
7-
//! inline them). These items "need cross-crate MIR". When a reachable function `f` needs
8-
//! cross-crate MIR, then all the functions it calls also become reachable, as they will be
9-
//! necessary to use the MIR of `f` from another crate. Furthermore, an item can become "externally
10-
//! reachable" by having a `const`/`const fn` return a pointer to that item, so we also need to
11-
//! recurse into reachable `const`/`const fn`.
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.
1224
1325
use hir::def_id::LocalDefIdSet;
1426
use rustc_data_structures::stack::ensure_sufficient_stack;

0 commit comments

Comments
 (0)