Skip to content

Commit b3869b9

Browse files
committed
Auto merge of rust-lang#152791 - nnethercote:rm-const-FLAGS, r=Zalathar
Remove `const FLAGS`. *[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/152791)* The performance wins provided by these types are meagre, and I don't think they justify the code complexity they introduce. r? @Zalathar
2 parents eeb94be + 5aebfd6 commit b3869b9

5 files changed

Lines changed: 194 additions & 291 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Debug;
1+
use std::fmt;
22
use std::ops::Deref;
33

44
use rustc_data_structures::fingerprint::Fingerprint;
@@ -12,7 +12,7 @@ use rustc_span::{ErrorGuaranteed, Span};
1212
pub use sealed::IntoQueryParam;
1313

1414
use crate::dep_graph;
15-
use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
15+
use crate::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
1616
use crate::ich::StableHashingContext;
1717
use crate::queries::{
1818
ExternProviders, PerQueryVTables, Providers, QueryArenas, QueryCaches, QueryEngine, QueryStates,
@@ -104,7 +104,16 @@ pub enum QueryMode {
104104
/// Stores function pointers and other metadata for a particular query.
105105
pub struct QueryVTable<'tcx, C: QueryCache> {
106106
pub name: &'static str,
107+
108+
/// True if this query has the `anon` modifier.
109+
pub anon: bool,
110+
/// True if this query has the `eval_always` modifier.
107111
pub eval_always: bool,
112+
/// True if this query has the `depth_limit` modifier.
113+
pub depth_limit: bool,
114+
/// True if this query has the `feedable` modifier.
115+
pub feedable: bool,
116+
108117
pub dep_kind: DepKind,
109118
/// How this query deals with query cycle errors.
110119
pub cycle_error_handling: CycleErrorHandling,
@@ -142,6 +151,85 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
142151
pub description_fn: fn(TyCtxt<'tcx>, C::Key) -> String,
143152
}
144153

154+
impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
155+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156+
// When debug-printing a query vtable (e.g. for ICE or tracing),
157+
// just print the query name to know what query we're dealing with.
158+
// The other fields and flags are probably just unhelpful noise.
159+
//
160+
// If there is need for a more detailed dump of all flags and fields,
161+
// consider writing a separate dump method and calling it explicitly.
162+
f.write_str(self.name)
163+
}
164+
}
165+
166+
impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
167+
#[inline(always)]
168+
pub fn will_cache_on_disk_for_key(&self, tcx: TyCtxt<'tcx>, key: &C::Key) -> bool {
169+
self.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
170+
}
171+
172+
// Don't use this method to access query results, instead use the methods on TyCtxt.
173+
#[inline(always)]
174+
pub fn query_state(&self, tcx: TyCtxt<'tcx>) -> &'tcx QueryState<'tcx, C::Key> {
175+
// Safety:
176+
// This is just manually doing the subfield referencing through pointer math.
177+
unsafe {
178+
&*(&tcx.query_system.states as *const QueryStates<'tcx>)
179+
.byte_add(self.query_state)
180+
.cast::<QueryState<'tcx, C::Key>>()
181+
}
182+
}
183+
184+
// Don't use this method to access query results, instead use the methods on TyCtxt.
185+
#[inline(always)]
186+
pub fn query_cache(&self, tcx: TyCtxt<'tcx>) -> &'tcx C {
187+
// Safety:
188+
// This is just manually doing the subfield referencing through pointer math.
189+
unsafe {
190+
&*(&tcx.query_system.caches as *const QueryCaches<'tcx>)
191+
.byte_add(self.query_cache)
192+
.cast::<C>()
193+
}
194+
}
195+
196+
#[inline(always)]
197+
pub fn try_load_from_disk(
198+
&self,
199+
tcx: TyCtxt<'tcx>,
200+
key: &C::Key,
201+
prev_index: SerializedDepNodeIndex,
202+
index: DepNodeIndex,
203+
) -> Option<C::Value> {
204+
// `?` will return None immediately for queries that never cache to disk.
205+
self.try_load_from_disk_fn?(tcx, key, prev_index, index)
206+
}
207+
208+
#[inline]
209+
pub fn is_loadable_from_disk(
210+
&self,
211+
tcx: TyCtxt<'tcx>,
212+
key: &C::Key,
213+
index: SerializedDepNodeIndex,
214+
) -> bool {
215+
self.is_loadable_from_disk_fn.map_or(false, |f| f(tcx, key, index))
216+
}
217+
218+
/// Synthesize an error value to let compilation continue after a cycle.
219+
pub fn value_from_cycle_error(
220+
&self,
221+
tcx: TyCtxt<'tcx>,
222+
cycle_error: &CycleError,
223+
guar: ErrorGuaranteed,
224+
) -> C::Value {
225+
(self.value_from_cycle_error)(tcx, cycle_error, guar)
226+
}
227+
228+
pub fn construct_dep_node(&self, tcx: TyCtxt<'tcx>, key: &C::Key) -> DepNode {
229+
DepNode::construct(tcx, self.dep_kind, key)
230+
}
231+
}
232+
145233
pub struct QuerySystemFns {
146234
pub engine: QueryEngine,
147235
pub local_providers: Providers,

compiler/rustc_query_impl/src/dep_kind_vtables.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_middle::bug;
22
use rustc_middle::dep_graph::{DepKindVTable, DepNodeKey, KeyFingerprintStyle};
33
use rustc_middle::query::QueryCache;
44

5+
use crate::QueryDispatcherUnerased;
56
use crate::plumbing::{force_from_dep_node_inner, try_load_from_on_disk_cache_inner};
6-
use crate::{QueryDispatcherUnerased, QueryFlags};
77

88
/// [`DepKindVTable`] constructors for special dep kinds that aren't queries.
99
#[expect(non_snake_case, reason = "use non-snake case to avoid collision with query names")]
@@ -102,14 +102,14 @@ mod non_query {
102102

103103
/// Shared implementation of the [`DepKindVTable`] constructor for queries.
104104
/// Called from macro-generated code for each query.
105-
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, Cache, const FLAGS: QueryFlags>(
105+
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, Cache>(
106+
is_anon: bool,
106107
is_eval_always: bool,
107108
) -> DepKindVTable<'tcx>
108109
where
109-
Q: QueryDispatcherUnerased<'tcx, Cache, FLAGS>,
110+
Q: QueryDispatcherUnerased<'tcx, Cache>,
110111
Cache: QueryCache + 'tcx,
111112
{
112-
let is_anon = FLAGS.is_anon;
113113
let key_fingerprint_style = if is_anon {
114114
KeyFingerprintStyle::Opaque
115115
} else {
@@ -131,10 +131,10 @@ where
131131
is_eval_always,
132132
key_fingerprint_style,
133133
force_from_dep_node: Some(|tcx, dep_node, _| {
134-
force_from_dep_node_inner(Q::query_dispatcher(tcx), tcx, dep_node)
134+
force_from_dep_node_inner(Q::query_vtable(tcx), tcx, dep_node)
135135
}),
136136
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
137-
try_load_from_on_disk_cache_inner(Q::query_dispatcher(tcx), tcx, dep_node)
137+
try_load_from_on_disk_cache_inner(Q::query_vtable(tcx), tcx, dep_node)
138138
}),
139139
}
140140
}

0 commit comments

Comments
 (0)