1
Fork 0

Build jump table at runtime.

This commit is contained in:
Camille GILLOT 2021-10-16 21:12:34 +02:00
parent 602d3cbce3
commit bd5c107672
10 changed files with 112 additions and 131 deletions

View file

@ -838,7 +838,7 @@ pub fn create_global_ctxt<'tcx>(
dep_graph, dep_graph,
queries.on_disk_cache.as_ref().map(OnDiskCache::as_dyn), queries.on_disk_cache.as_ref().map(OnDiskCache::as_dyn),
queries.as_dyn(), queries.as_dyn(),
&rustc_query_impl::QUERY_CALLBACKS, rustc_query_impl::query_callbacks(arena),
crate_name, crate_name,
outputs, outputs,
) )

View file

@ -100,6 +100,8 @@ macro_rules! arena_types {
// This is used to decode the &'tcx [Span] for InlineAsm's line_spans. // This is used to decode the &'tcx [Span] for InlineAsm's line_spans.
[decode] span: rustc_span::Span, [decode] span: rustc_span::Span,
[decode] used_trait_imports: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>, [decode] used_trait_imports: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>,
[] dep_kind: rustc_middle::dep_graph::DepKindStruct,
], $tcx); ], $tcx);
) )
} }

View file

@ -87,7 +87,7 @@ pub struct DepKindStruct {
/// Whether the query key can be recovered from the hashed fingerprint. /// Whether the query key can be recovered from the hashed fingerprint.
/// See [DepNodeParams] trait for the behaviour of each key type. /// See [DepNodeParams] trait for the behaviour of each key type.
pub fingerprint_style: fn() -> FingerprintStyle, pub fingerprint_style: FingerprintStyle,
/// The red/green evaluation system will try to mark a specific DepNode in the /// The red/green evaluation system will try to mark a specific DepNode in the
/// dependency graph as green by recursively trying to mark the dependencies of /// dependency graph as green by recursively trying to mark the dependencies of
@ -131,10 +131,10 @@ pub struct DepKindStruct {
/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
/// add it to the "We don't have enough information to reconstruct..." group in /// add it to the "We don't have enough information to reconstruct..." group in
/// the match below. /// the match below.
pub force_from_dep_node: fn(tcx: TyCtxt<'_>, dep_node: &DepNode) -> bool, pub force_from_dep_node: Option<fn(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool>,
/// Invoke a query to put the on-disk cached value in memory. /// Invoke a query to put the on-disk cached value in memory.
pub try_load_from_on_disk_cache: fn(TyCtxt<'_>, &DepNode), pub try_load_from_on_disk_cache: Option<fn(TyCtxt<'_>, DepNode)>,
} }
impl DepKind { impl DepKind {
@ -145,8 +145,7 @@ impl DepKind {
if data.is_anon { if data.is_anon {
return FingerprintStyle::Opaque; return FingerprintStyle::Opaque;
} }
data.fingerprint_style
(data.fingerprint_style)()
} }
} }
@ -159,7 +158,7 @@ macro_rules! define_dep_nodes {
) => ( ) => (
#[macro_export] #[macro_export]
macro_rules! make_dep_kind_array { macro_rules! make_dep_kind_array {
($mod:ident) => {[ $(($mod::$variant),)* ]}; ($mod:ident) => {[ $($mod::$variant()),* ]};
} }
/// This enum serves as an index into arrays built by `make_dep_kind_array`. /// This enum serves as an index into arrays built by `make_dep_kind_array`.

View file

@ -100,7 +100,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
self.query_kind(kind).is_eval_always self.query_kind(kind).is_eval_always
} }
fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool { fn try_force_from_dep_node(&self, dep_node: DepNode) -> bool {
debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node); debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
// We must avoid ever having to call `force_from_dep_node()` for a // We must avoid ever having to call `force_from_dep_node()` for a
@ -122,11 +122,18 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
); );
let cb = self.query_kind(dep_node.kind); let cb = self.query_kind(dep_node.kind);
(cb.force_from_dep_node)(*self, dep_node) if let Some(f) = cb.force_from_dep_node {
f(*self, dep_node);
true
} else {
false
}
} }
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) { fn try_load_from_on_disk_cache(&self, dep_node: DepNode) {
let cb = self.query_kind(dep_node.kind); let cb = self.query_kind(dep_node.kind);
(cb.try_load_from_on_disk_cache)(*self, dep_node) if let Some(f) = cb.try_load_from_on_disk_cache {
f(*self, dep_node)
}
} }
} }

View file

@ -1186,7 +1186,7 @@ impl<'tcx> TyCtxt<'tcx> {
} }
} }
pub fn query_kind(self, k: DepKind) -> &'tcx DepKindStruct { crate fn query_kind(self, k: DepKind) -> &'tcx DepKindStruct {
&self.query_kinds[k as usize] &self.query_kinds[k as usize]
} }

View file

@ -17,6 +17,7 @@ extern crate rustc_middle;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::DiagnosticBuilder; use rustc_errors::DiagnosticBuilder;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::{self, DepKindStruct}; use rustc_middle::dep_graph::{self, DepKindStruct};
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values}; use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
use rustc_middle::ty::query::{Providers, QueryEngine}; use rustc_middle::ty::query::{Providers, QueryEngine};

View file

@ -331,7 +331,7 @@ macro_rules! define_queries {
} }
})* })*
#[allow(non_upper_case_globals)] #[allow(nonstandard_style)]
pub mod query_callbacks { pub mod query_callbacks {
use super::*; use super::*;
use rustc_middle::dep_graph::DepNode; use rustc_middle::dep_graph::DepNode;
@ -341,74 +341,83 @@ macro_rules! define_queries {
use rustc_query_system::dep_graph::FingerprintStyle; use rustc_query_system::dep_graph::FingerprintStyle;
// We use this for most things when incr. comp. is turned off. // We use this for most things when incr. comp. is turned off.
pub const Null: DepKindStruct = DepKindStruct { pub fn Null() -> DepKindStruct {
DepKindStruct {
is_anon: false, is_anon: false,
is_eval_always: false, is_eval_always: false,
fingerprint_style: FingerprintStyle::Unit,
force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
try_load_from_on_disk_cache: None,
}
}
fingerprint_style: || FingerprintStyle::Unit, pub fn TraitSelect() -> DepKindStruct {
force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node), DepKindStruct {
try_load_from_on_disk_cache: |_, _| {},
};
pub const TraitSelect: DepKindStruct = DepKindStruct {
is_anon: true, is_anon: true,
is_eval_always: false, is_eval_always: false,
fingerprint_style: FingerprintStyle::Unit,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
fingerprint_style: || FingerprintStyle::Unit, pub fn CompileCodegenUnit() -> DepKindStruct {
force_from_dep_node: |_, _| false, DepKindStruct {
try_load_from_on_disk_cache: |_, _| {},
};
pub const CompileCodegenUnit: DepKindStruct = DepKindStruct {
is_anon: false, is_anon: false,
is_eval_always: false, is_eval_always: false,
fingerprint_style: FingerprintStyle::Opaque,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
fingerprint_style: || FingerprintStyle::Opaque, pub fn CompileMonoItem() -> DepKindStruct {
force_from_dep_node: |_, _| false, DepKindStruct {
try_load_from_on_disk_cache: |_, _| {},
};
pub const CompileMonoItem: DepKindStruct = DepKindStruct {
is_anon: false, is_anon: false,
is_eval_always: false, is_eval_always: false,
fingerprint_style: FingerprintStyle::Opaque,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
fingerprint_style: || FingerprintStyle::Opaque, $(pub fn $name()-> DepKindStruct {
force_from_dep_node: |_, _| false, let is_anon = is_anon!([$($modifiers)*]);
try_load_from_on_disk_cache: |_, _| {}, let is_eval_always = is_eval_always!([$($modifiers)*]);
};
$(pub const $name: DepKindStruct = { let fingerprint_style =
const is_anon: bool = is_anon!([$($modifiers)*]); <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::fingerprint_style();
const is_eval_always: bool = is_eval_always!([$($modifiers)*]);
if is_anon || !fingerprint_style.reconstructible() {
return DepKindStruct {
is_anon,
is_eval_always,
fingerprint_style,
force_from_dep_node: None,
try_load_from_on_disk_cache: None,
}
}
#[inline(always)] #[inline(always)]
fn fingerprint_style() -> FingerprintStyle { fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> Option<query_keys::$name<'tcx>> {
<query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>> <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, &dep_node)
::fingerprint_style()
} }
fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$name<'tcx>> { fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool {
<query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node) if let Some(key) = recover(tcx, dep_node) {
}
fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: &DepNode) -> bool {
let tcx = QueryCtxt::from_tcx(tcx); let tcx = QueryCtxt::from_tcx(tcx);
force_query::<queries::$name<'_>, _>(tcx, dep_node) force_query::<queries::$name<'_>, _>(tcx, key, dep_node);
true
} else {
false
}
} }
fn try_load_from_on_disk_cache(tcx: TyCtxt<'_>, dep_node: &DepNode) { fn try_load_from_on_disk_cache(tcx: TyCtxt<'_>, dep_node: DepNode) {
debug_assert!(tcx.dep_graph.is_green(&dep_node));
let key = recover(tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
let tcx = QueryCtxt::from_tcx(tcx); let tcx = QueryCtxt::from_tcx(tcx);
if is_anon {
return
}
if !fingerprint_style().reconstructible() {
return
}
debug_assert!(tcx.dep_graph.is_green(dep_node));
let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
if queries::$name::cache_on_disk(tcx, &key, None) { if queries::$name::cache_on_disk(tcx, &key, None) {
let _ = tcx.$name(key); let _ = tcx.$name(key);
} }
@ -418,13 +427,15 @@ macro_rules! define_queries {
is_anon, is_anon,
is_eval_always, is_eval_always,
fingerprint_style, fingerprint_style,
force_from_dep_node, force_from_dep_node: Some(force_from_dep_node),
try_load_from_on_disk_cache, try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
} }
};)* })*
} }
pub static QUERY_CALLBACKS: &[DepKindStruct] = &make_dep_kind_array!(query_callbacks); pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
}
} }
} }

View file

@ -576,7 +576,7 @@ impl<K: DepKind> DepGraph<K> {
"try_mark_previous_green({:?}) --- trying to force dependency {:?}", "try_mark_previous_green({:?}) --- trying to force dependency {:?}",
dep_node, dep_dep_node dep_node, dep_dep_node
); );
if !tcx.dep_context().try_force_from_dep_node(dep_dep_node) { if !tcx.dep_context().try_force_from_dep_node(*dep_dep_node) {
// The DepNode could not be forced. // The DepNode could not be forced.
debug!( debug!(
"try_mark_previous_green({:?}) - END - dependency {:?} could not be forced", "try_mark_previous_green({:?}) - END - dependency {:?} could not be forced",
@ -749,7 +749,7 @@ impl<K: DepKind> DepGraph<K> {
match data.colors.get(prev_index) { match data.colors.get(prev_index) {
Some(DepNodeColor::Green(_)) => { Some(DepNodeColor::Green(_)) => {
let dep_node = data.previous.index_to_node(prev_index); let dep_node = data.previous.index_to_node(prev_index);
tcx.try_load_from_on_disk_cache(&dep_node); tcx.try_load_from_on_disk_cache(dep_node);
} }
None | Some(DepNodeColor::Red) => { None | Some(DepNodeColor::Red) => {
// We can skip red nodes because a node can only be marked // We can skip red nodes because a node can only be marked

View file

@ -39,10 +39,10 @@ pub trait DepContext: Copy {
fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle; fn fingerprint_style(&self, kind: Self::DepKind) -> FingerprintStyle;
/// Try to force a dep node to execute and see if it's green. /// Try to force a dep node to execute and see if it's green.
fn try_force_from_dep_node(&self, dep_node: &DepNode<Self::DepKind>) -> bool; fn try_force_from_dep_node(&self, dep_node: DepNode<Self::DepKind>) -> bool;
/// Load data from the on-disk cache. /// Load data from the on-disk cache.
fn try_load_from_on_disk_cache(&self, dep_node: &DepNode<Self::DepKind>); fn try_load_from_on_disk_cache(&self, dep_node: DepNode<Self::DepKind>);
} }
pub trait HasDepContext: Copy { pub trait HasDepContext: Copy {

View file

@ -665,41 +665,6 @@ where
} }
} }
#[inline(never)]
fn force_query_impl<CTX, C>(
tcx: CTX,
state: &QueryState<CTX::DepKind, C::Key>,
cache: &QueryCacheStore<C>,
key: C::Key,
dep_node: DepNode<CTX::DepKind>,
query: &QueryVtable<CTX, C::Key, C::Value>,
compute: fn(CTX::DepContext, C::Key) -> C::Value,
) -> bool
where
C: QueryCache,
C::Key: DepNodeParams<CTX::DepContext>,
CTX: QueryContext,
{
debug_assert!(!query.anon);
// We may be concurrently trying both execute and force a query.
// Ensure that only one of them runs the query.
let cached = cache.cache.lookup(cache, &key, |_, index| {
if unlikely!(tcx.dep_context().profiler().enabled()) {
tcx.dep_context().profiler().query_cache_hit(index.into());
}
});
let lookup = match cached {
Ok(()) => return true,
Err(lookup) => lookup,
};
let _ =
try_execute_query(tcx, state, cache, DUMMY_SP, key, lookup, Some(dep_node), query, compute);
true
}
pub enum QueryMode { pub enum QueryMode {
Get, Get,
Ensure, Ensure,
@ -747,34 +712,30 @@ where
Some(result) Some(result)
} }
pub fn force_query<Q, CTX>(tcx: CTX, dep_node: &DepNode<CTX::DepKind>) -> bool pub fn force_query<Q, CTX>(tcx: CTX, key: Q::Key, dep_node: DepNode<CTX::DepKind>)
where where
Q: QueryDescription<CTX>, Q: QueryDescription<CTX>,
Q::Key: DepNodeParams<CTX::DepContext>, Q::Key: DepNodeParams<CTX::DepContext>,
CTX: QueryContext, CTX: QueryContext,
{ {
if Q::ANON { let query = &Q::VTABLE;
return false; debug_assert!(!Q::ANON);
}
if !<Q::Key as DepNodeParams<CTX::DepContext>>::fingerprint_style().reconstructible() { // We may be concurrently trying both execute and force a query.
return false; // Ensure that only one of them runs the query.
let cache = Q::query_cache(tcx);
let cached = cache.cache.lookup(cache, &key, |_, index| {
if unlikely!(tcx.dep_context().profiler().enabled()) {
tcx.dep_context().profiler().query_cache_hit(index.into());
} }
});
let Some(key) = let lookup = match cached {
<Q::Key as DepNodeParams<CTX::DepContext>>::recover(*tcx.dep_context(), &dep_node) Ok(()) => return,
else { Err(lookup) => lookup,
return false;
}; };
let compute = Q::compute_fn(tcx, &key); let compute = Q::compute_fn(tcx, &key);
force_query_impl( let state = Q::query_state(tcx);
tcx, try_execute_query(tcx, state, cache, DUMMY_SP, key, lookup, Some(dep_node), query, compute);
Q::query_state(tcx),
Q::query_cache(tcx),
key,
*dep_node,
&Q::VTABLE,
compute,
)
} }