Skip to content

Refactor the query system to reduce the amount of macro-expanded functions #96524

Closed
@cjgillot

Description

The current implementation of the query system relies on a lot of macro-expanded functions.
This issue tracks perspectives to reduce this use of macros.

The objective of this issue is to have the macros expand to a self-contained description of the query, instead of breadcrumbs in several files.

The interface code in rustc_middle::ty::query can be made responsible for calling it, it already does.

  • Simplify QueryDescription trait

TRY_LOAD_FROM_DISK, cache_on_disk and describe are only used for
the QueryVtable and make_query::$name. They can be made inherent
associated constant and functions on the queries::$name types.

This would allow to replace the function pointer for handle_cycle_error by a simple enum.

The call to opt_remap_env_constness! is unnecessary. The DepKind can be passed as a parameter. describe can be passed as a function pointer. key can be made an impl Key + HashStable.

We should consider moving part of the code into QueryState::try_collect_active_jobs which is the only user of these functions.

DepKindStruct is defined in rustc_middle::dep_graph::dep_node. It depends on TyCtxt and DepKind, but that can be replaced by a generic parameter CTX: DepContext and CTX::DepKind.

rustc_query_system would access it through a new DepContext::dep_kind_info providing fingerprint_style, is_eval_always, try_force_from_dep_node and try_load_from_on_disk_cacke, and replacing TyCtxt::query_kind.

If specialization allows it, we should aim to make most of the code in rustc_middle::dep_graph::dep_node generic over CTX: DepContext, and move it to rustc_query_system.

Having macro-unrolled loops prevents extension of the infrastructure to non-statically known instances. This struct is to be used in a similar way to DepKindStruct, but with types private to rustc_query_impl.

struct QueryStruct {
    try_collect_active_jobs: fn(QueryCtxt<'_>, &mut QueryMap) -> Option<()>,
    alloc_self_profile_query_strings: fn(QueryCtxt<'tcx>, &mut QueryKeyStringCache),
    encode_query_results: Option<fn(QueryCtxt<'_>, &mut CacheEncoder<...>, &mut EncodedDepNodeIndex)>
}

This will be used to:

  • replace the macro expansion in try_collect_active_jobs by a loop over function pointers;
  • replace the macro expansion in alloc_self_profile_query_strings by a loop over function pointers;
  • replace the macro expansion in encode_query_results by a loop over function pointers.

The array itself should be created using make_dep_kind_array! which handles the indices.
Possible variant: put the try_collect_active_jobs and alloc_self_profile_query_strings function pointers in DepKindStruct instead.

Please contact me on Zulip for further information.

Metadata

Assignees

No one assigned

    Labels

    A-incr-compArea: Incremental compilationA-query-systemArea: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html)E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions