-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Improve how QueryCaches and QueryStates are stored
#152835
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
nnethercote
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
nnethercote:fix-query-state-query-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
−139
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a7a2a68
Avoid using `QueryVTable` in `mk_query_stack_frame_extra`.
nnethercote a4be2f2
Improve how `QueryCache`s and `QueryState`s are stored.
nnethercote 156f355
Rename some `Cache` type variables as `C`.
nnethercote bc792c2
Merge `QueryEngine` into `QueryVTable`.
nnethercote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ use rustc_middle::bug; | |
| #[expect(unused_imports, reason = "used by doc comments")] | ||
| use rustc_middle::dep_graph::DepKindVTable; | ||
| use rustc_middle::dep_graph::{ | ||
| self, DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex, dep_kinds, | ||
| self, DepKind, DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex, dep_kinds, | ||
| }; | ||
| use rustc_middle::query::on_disk_cache::{ | ||
| AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex, | ||
|
|
@@ -123,7 +123,7 @@ pub fn collect_active_jobs_from_all_queries<'tcx>( | |
| if complete { Ok(job_map_out) } else { Err(job_map_out) } | ||
| } | ||
|
|
||
| pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool { | ||
| pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool { | ||
| tcx.dep_graph.try_mark_green(tcx, dep_node).is_some() | ||
| } | ||
|
|
||
|
|
@@ -273,12 +273,17 @@ macro_rules! item_if_cache_on_disk { | |
| } | ||
|
|
||
| /// The deferred part of a deferred query stack frame. | ||
| fn mk_query_stack_frame_extra<'tcx, Cache>( | ||
| (tcx, vtable, key): (TyCtxt<'tcx>, &'tcx QueryVTable<'tcx, Cache>, Cache::Key), | ||
| fn mk_query_stack_frame_extra<'tcx, K>( | ||
| (tcx, name, dep_kind, description_fn, key): ( | ||
| TyCtxt<'tcx>, | ||
| &'static str, | ||
| DepKind, | ||
| fn(TyCtxt<'tcx>, K) -> String, | ||
| K, | ||
| ), | ||
| ) -> QueryStackFrameExtra | ||
| where | ||
| Cache: QueryCache, | ||
| Cache::Key: Key, | ||
| K: Key + Copy, | ||
| { | ||
| let def_id = key.key_as_def_id(); | ||
|
|
||
|
|
@@ -287,21 +292,21 @@ where | |
| let reduce_queries = with_reduced_queries(); | ||
|
|
||
| // Avoid calling queries while formatting the description | ||
| let description = ty::print::with_no_queries!((vtable.description_fn)(tcx, key)); | ||
| let description = ty::print::with_no_queries!(description_fn(tcx, key)); | ||
| let description = if tcx.sess.verbose_internals() { | ||
| format!("{description} [{name:?}]", name = vtable.name) | ||
| format!("{description} [{name:?}]") | ||
| } else { | ||
| description | ||
| }; | ||
| let span = if vtable.dep_kind == dep_graph::dep_kinds::def_span || reduce_queries { | ||
| let span = if dep_kind == dep_graph::dep_kinds::def_span || reduce_queries { | ||
| // The `def_span` query is used to calculate `default_span`, | ||
| // so exit to avoid infinite recursion. | ||
| None | ||
| } else { | ||
| Some(key.default_span(tcx)) | ||
| }; | ||
|
|
||
| let def_kind = if vtable.dep_kind == dep_graph::dep_kinds::def_kind || reduce_queries { | ||
| let def_kind = if dep_kind == dep_graph::dep_kinds::def_kind || reduce_queries { | ||
| // Try to avoid infinite recursion. | ||
| None | ||
| } else { | ||
|
|
@@ -331,7 +336,10 @@ where | |
| let def_id: Option<DefId> = key.key_as_def_id(); | ||
| let def_id_for_ty_in_cycle: Option<DefId> = key.def_id_for_ty_in_cycle(); | ||
|
|
||
| let info = QueryStackDeferred::new((tcx, vtable, key), mk_query_stack_frame_extra); | ||
| let info = QueryStackDeferred::new( | ||
| (tcx, vtable.name, vtable.dep_kind, vtable.description_fn, key), | ||
| mk_query_stack_frame_extra, | ||
| ); | ||
| QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle) | ||
| } | ||
|
|
||
|
|
@@ -346,9 +354,8 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache, const FLAGS: Quer | |
| { | ||
| let _timer = tcx.prof.generic_activity_with_arg("encode_query_results_for", query.name()); | ||
|
|
||
| assert!(all_inactive(query.query_state(tcx))); | ||
| let cache = query.query_cache(tcx); | ||
| cache.iter(&mut |key, value, dep_node| { | ||
| assert!(all_inactive(query.query_state())); | ||
| query.query_cache().iter(&mut |key, value, dep_node| { | ||
| if query.will_cache_on_disk_for_key(tcx, key) { | ||
| let dep_node = SerializedDepNodeIndex::new(dep_node.index()); | ||
|
|
||
|
|
@@ -368,7 +375,7 @@ pub(crate) fn query_key_hash_verify<'tcx, C: QueryCache, const FLAGS: QueryFlags | |
| ) { | ||
| let _timer = tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name()); | ||
|
|
||
| let cache = query.query_cache(tcx); | ||
| let cache = query.query_cache(); | ||
| let mut map = UnordMap::with_capacity(cache.len()); | ||
| cache.iter(&mut |key, _, _| { | ||
| let node = DepNode::construct(tcx, query.dep_kind(), key); | ||
|
|
@@ -569,8 +576,8 @@ macro_rules! define_queries { | |
| eval_always: is_eval_always!([$($modifiers)*]), | ||
| dep_kind: dep_graph::dep_kinds::$name, | ||
| cycle_error_handling: cycle_error_handling!([$($modifiers)*]), | ||
| query_state: std::mem::offset_of!(QueryStates<'tcx>, $name), | ||
| query_cache: std::mem::offset_of!(QueryCaches<'tcx>, $name), | ||
| query_state: Default::default(), | ||
| query_cache: Default::default(), | ||
|
Comment on lines
+579
to
+580
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question/suggestion: Should we get rid of the redundant |
||
| will_cache_on_disk_for_key_fn: if_cache_on_disk!([$($modifiers)*] { | ||
| Some(::rustc_middle::queries::_cache_on_disk_if_fns::$name) | ||
| } { | ||
|
|
@@ -665,7 +672,8 @@ macro_rules! define_queries { | |
| }; | ||
|
|
||
| // Call `gather_active_jobs_inner` to do the actual work. | ||
| let res = crate::execution::gather_active_jobs_inner(&tcx.query_system.states.$name, | ||
| let res = crate::execution::gather_active_jobs_inner( | ||
| &tcx.query_system.query_vtables.$name.query_state, | ||
| tcx, | ||
| make_frame, | ||
| require_complete, | ||
|
|
@@ -691,7 +699,7 @@ macro_rules! define_queries { | |
| $crate::profiling_support::alloc_self_profile_query_strings_for_query_cache( | ||
| tcx, | ||
| stringify!($name), | ||
| &tcx.query_system.caches.$name, | ||
| &tcx.query_system.query_vtables.$name.query_cache, | ||
| string_cache, | ||
| ) | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Based on my local testing, I think this entire commit can be avoided by just adding a
QueryVTable<'tcx, C>: DynSyncbound tocreate_deferred_query_stack_frame.(This makes sense because the state and cache should be
DynSync, and the caller knows that this happens to be true of every concrete state and cache.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you apply this suggestion, the commit description of the next commit will need some adjustment.