11# Queries: demand-driven compilation
22
3+ <!-- toc -->
4+
35As described in [ the high-level overview of the compiler] [ hl ] , the Rust compiler
46is still (as of <!-- date: 2021-07 --> July 2021) transitioning from a
5- traditional "pass-based" setup to a "demand-driven" system. ** The Compiler Query
6- System is the key to our new demand-driven organization.** The idea is pretty
7- simple. You have various queries that compute things about the input – for
8- example, there is a query called ` type_of(def_id) ` that, given the [ def-id] of
7+ traditional "pass-based" setup to a "demand-driven" system. The compiler query
8+ system is the key to rustc's demand-driven organization.
9+ The idea is pretty simple. Instead of entirely independent passes
10+ (parsing, type-checking, etc.), a set of function-like * queries*
11+ compute information about the input source. For example,
12+ there is a query called ` type_of ` that, given the [ ` DefId ` ] of
913some item, will compute the type of that item and return it to you.
1014
11- [ def-id ] : appendix/glossary.md#def-id
15+ [ `DefId` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/def_id/struct.DefId.html
1216[ hl ] : ./compiler-src.md
1317
14- Query execution is ** memoized** – so the first time you invoke a
18+ Query execution is * memoized* . The first time you invoke a
1519query, it will go do the computation, but the next time, the result is
1620returned from a hashtable. Moreover, query execution fits nicely into
17- ** incremental computation** ; the idea is roughly that, when you do a
18- query, the result ** may* * be returned to you by loading stored data
19- from disk (but that's a separate topic we won't discuss further here).
21+ * incremental computation* ; the idea is roughly that, when you invoke a
22+ query, the result * may* be returned to you by loading stored data
23+ from disk. [ ^ incr-comp-detail ]
2024
21- The overall vision is that, eventually, the entire compiler
22- control-flow will be query driven. There will effectively be one
23- top-level query (" compile" ) that will run compilation on a crate; this
25+ Eventually, we want the entire compiler
26+ control-flow to be query driven. There will effectively be one
27+ top-level query (` compile ` ) that will run compilation on a crate; this
2428will in turn demand information about that crate, starting from the
2529* end* . For example:
2630
27- - This " compile" query might demand to get a list of codegen-units
31+ - The ` compile ` query might demand to get a list of codegen-units
2832 (i.e. modules that need to be compiled by LLVM).
2933- But computing the list of codegen-units would invoke some subquery
3034 that returns the list of all modules defined in the Rust source.
3135- That query in turn would invoke something asking for the HIR.
3236- This keeps going further and further back until we wind up doing the
3337 actual parsing.
3438
35- However, that vision is not fully realized. Still, big chunks of the
36- compiler (for example, generating MIR) work exactly like this.
37-
38- ### Incremental Compilation in Detail
39+ Although this vision is not fully realized, large sections of the
40+ compiler (for example, generating [ MIR] ( ./mir/ ) ) currently work exactly like this.
3941
40- The [ Incremental Compilation in Detail] [ query-model ] chapter gives a more
42+ [ ^ incr-comp-detail ] : The [ " Incremental Compilation in Detail] ( queries/incremental-compilation-in-detail.md ) chapter gives a more
4143in-depth description of what queries are and how they work.
4244If you intend to write a query of your own, this is a good read.
4345
44- [ query-model ] : queries/incremental-compilation-in-detail.md
45-
4646### Invoking queries
4747
48- To invoke a query is simple. The tcx ("type context") offers a method
49- for each defined query. So, for example, to invoke the ` type_of `
48+ Invoking a query is simple. The [ ` TyCtxt ` ] ("type context") struct offers a method
49+ for each defined query. For example, to invoke the ` type_of `
5050query, you would just do this:
5151
5252``` rust,ignore
5353let ty = tcx.type_of(some_def_id);
5454```
5555
56+ [ `TyTcx` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html
57+
5658### How the compiler executes a query
5759
5860So you may be wondering what happens when you invoke a query
@@ -162,13 +164,13 @@ they define both a `provide` and a `provide_extern` function, through
162164[ rustc_metadata ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/index.html
163165[ wasm_import_module_map ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/back/symbol_export/fn.wasm_import_module_map.html
164166
165- ### Adding a new kind of query
167+ ### Adding a new query
166168
167- So suppose you want to add a new kind of query, how do you do so ?
168- Well, defining a query takes place in two steps:
169+ How do you add a new query?
170+ Defining a query takes place in two steps:
169171
170- 1 . first, you have to specify the query name and arguments; and then,
171- 2 . you have to supply query providers where needed.
172+ 1 . Specify the query name and its arguments.
173+ 2 . Supply query providers where needed.
172174
173175To specify the query name and arguments, you simply add an entry to
174176the big macro invocation in
@@ -190,21 +192,22 @@ rustc_queries! {
190192```
191193
192194Queries are grouped into categories (` Other ` , ` Codegen ` , ` TypeChecking ` , etc.).
193- Each group contains one or more queries. Each query definition is broken up like
194- this:
195+ Each group contains one or more queries.
196+
197+ A query definition has the following form:
195198
196199``` rust,ignore
197200query type_of(key: DefId) -> Ty<'tcx> { ... }
198- ^^ ^^^^^^^ ^^^^^ ^^^^^^^^ ^^^
201+ ^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^ ^^^
199202| | | | |
200203| | | | query modifiers
201- | | | result type of query
204+ | | | result type
202205| | query key type
203206| name of query
204207query keyword
205208```
206209
207- Let's go over them one by one:
210+ Let's go over these elements one by one:
208211
209212- ** Query keyword:** indicates a start of a query definition.
210213- ** Name of query:** the name of the query method
@@ -217,11 +220,7 @@ Let's go over them one by one:
217220- ** Result type of query:** the type produced by this query. This type
218221 should (a) not use ` RefCell ` or other interior mutability and (b) be
219222 cheaply cloneable. Interning or using ` Rc ` or ` Arc ` is recommended for
220- non-trivial data types.
221- - The one exception to those rules is the ` ty::steal::Steal ` type,
222- which is used to cheaply modify MIR in place. See the definition
223- of ` Steal ` for more details. New uses of ` Steal ` should ** not** be
224- added without alerting ` @rust-lang/compiler ` .
223+ non-trivial data types.[ ^ steal ]
225224- ** Query modifiers:** various flags and options that customize how the
226225 query is processed (mostly with respect to [ incremental compilation] [ incrcomp ] ).
227226
@@ -234,11 +233,16 @@ So, to add a query:
234233- Link the provider by modifying the appropriate ` provide ` method;
235234 or add a new one if needed and ensure that ` rustc_driver ` is invoking it.
236235
236+ [ ^ steal ] : The one exception to those rules is the ` ty::steal::Steal ` type,
237+ which is used to cheaply modify MIR in place. See the definition
238+ of ` Steal ` for more details. New uses of ` Steal ` should ** not** be
239+ added without alerting ` @rust-lang/compiler ` .
240+
237241#### Query structs and descriptions
238242
239- For each kind , the ` rustc_queries ` macro will generate a "query struct"
240- named after the query. This struct is a kind of a place-holder
241- describing the query. Each such struct implements the
243+ For each query , the ` rustc_queries ` macro will generate a "query struct"
244+ named after the query. This struct is a kind of placeholder
245+ describing the query. Each query struct implements the
242246[ ` self::config::QueryConfig ` ] [ QueryConfig ] trait, which has associated types for the
243247key/value of that particular query. Basically the code generated looks something
244248like this:
@@ -308,3 +312,4 @@ More discussion and issues:
308312[ GitHub issue #42633 ] : https://github.com/rust-lang/rust/issues/42633
309313[ Incremental Compilation Beta ] : https://internals.rust-lang.org/t/incremental-compilation-beta/4721
310314[ Incremental Compilation Announcement ] : https://blog.rust-lang.org/2016/09/08/incremental.html
315+
0 commit comments