Skip to content

Document query internals #104011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jyn514 opened this issue Nov 5, 2022 · 0 comments
Open

Document query internals #104011

jyn514 opened this issue Nov 5, 2022 · 0 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Nov 5, 2022

rustc_query_impl and rustc_query_system are complicated. We should have a document somewhere that explains how they work internally.

We used to have a very short sketch of that in the dev-guide, but it was removed in rust-lang/rustc-dev-guide@552de58 because it wasn't clear that it was an implementation detail and not user-facing.

My short summary (I'll try and update this later):

  • Queries are defined in
    //! Defines the various compiler queries.
    //!
    //! For more information on the query system, see
    //! ["Queries: demand-driven compilation"](https://rustc-dev-guide.rust-lang.org/query.html).
    //! This chapter includes instructions for adding new queries.
    using the rustc_queries macro.
  • rustc_queries is defined in
    pub fn rustc_queries(input: TokenStream) -> TokenStream {
    It outputs another rustc_query_append macro, as well as two modules:
    #[macro_export]
    macro_rules! rustc_query_append {
    ($macro:ident! $( [$($other:tt)*] )?) => {
    $macro! {
    $( $($other)* )?
    #query_stream
    }
    }
    }
    pub mod descs {
    use super::*;
    #query_description_stream
    }
    pub mod cached {
    use super::*;
    #query_cached_stream
    }
  • rustc_query_append is called in various places around the compiler. It takes another macro as its input, then calls that macro with the token stream emitted by rustc_queries. This emulates eager expansion.
  • The primary callers of rustc_query_append are rustc_middle::define_callbacks
    macro_rules! define_callbacks {
    and rustc_query_impl::define_queries
    macro_rules! define_queries {

There are 3 main crates involved in defining queries, other than rustc_macros.

  1. rustc_query_system comes earliest in the dependency graph. It defines various traits along with some helper functions. It is very fast to compile. We may want to rename this to rustc_query_traits at some point.
  2. rustc_middle comes next and depends on query_system. It defines TyCtxt and various helper functions that are used elsewhere in the compiler; this is the first place we can invoke rustc_queries, since before this we don't have access to TyCtxt. This is slow to compile but not because of the query system, it's just big.
  3. rustc_query_impl depends on rustc_middle. It implements the traits in rustc_query_system for the queries declared in rustc_middle. It takes an extremely long time to compile.

Because query_impl takes so long to compile, we don't depend on it anywhere except for rustc_interface. In particular, we use function pointers and dynamic dispatch as necessary so that rustc_middle and later crates don't need to depend on query internals.

cc @cjgillot @Nilstrieb

@jyn514 jyn514 added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) labels Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant