Refactor the query system to reduce the amount of macro-expanded functions #96524
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.
- Remove
opt_remap_env_constness
fromrustc_query_impl
(Remove opt_remap_env_constness from rustc_query_impl #100243)
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.
- Move
Value
trait torustc_query_system
(MakeHandleCycleError
an enum instead of a macro-generated closure #101303)
This would allow to replace the function pointer for handle_cycle_error
by a simple enum.
- Refactor
make_query::$name
functions to a generic version instead of a macro (Move most ofTyCtxtAt::$name
into a genericevaluate_query
function #101178)
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.
- Move
DepKindStruct
torustc_query_system
(Move DepKindStruct from rustc_middle to rustc_query_system #101710)
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
.
- Introduce an array of
QueryStruct
with function pointers (Use function pointers instead of macro-unrolled loops in rustc_query_impl #101785)
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.