Skip to content

Commit

Permalink
rustc: Remove HirId from queries
Browse files Browse the repository at this point in the history
This'll allow us to reconstruct query parameters purely from the `DepNode`
they're associated with. Some queries could move straight to `HirId` but others
that don't always have a correspondance between `HirId` and `DefId` moved to
two-level maps where the query operates over a `DefIndex`, returning a map,
which is then keyed off `ItemLocalId`.

Closes #44414
  • Loading branch information
alexcrichton committed Sep 11, 2017
1 parent ddd123e commit caaf365
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 121 deletions.
18 changes: 9 additions & 9 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
//! user of the `DepNode` API of having to know how to compute the expected
//! fingerprint for a given set of node parameters.
use hir::def_id::{CrateNum, DefId};
use hir::def_id::{CrateNum, DefId, DefIndex};
use hir::map::DefPathHash;
use hir::{HirId, ItemLocalId};

Expand Down Expand Up @@ -528,8 +528,8 @@ define_dep_nodes!( <'tcx>
[] ExternCrate(DefId),
[] LintLevels,
[] Specializes { impl1: DefId, impl2: DefId },
[] InScopeTraits(HirId),
[] ModuleExports(HirId),
[] InScopeTraits(DefIndex),
[] ModuleExports(DefId),
[] IsSanitizerRuntime(CrateNum),
[] IsProfilerRuntime(CrateNum),
[] GetPanicStrategy(CrateNum),
Expand All @@ -551,15 +551,15 @@ define_dep_nodes!( <'tcx>
[] NativeLibraryKind(DefId),
[] LinkArgs,

[] NamedRegion(HirId),
[] IsLateBound(HirId),
[] ObjectLifetimeDefaults(HirId),
[] NamedRegion(DefIndex),
[] IsLateBound(DefIndex),
[] ObjectLifetimeDefaults(DefIndex),

[] Visibility(DefId),
[] DepKind(CrateNum),
[] CrateName(CrateNum),
[] ItemChildren(DefId),
[] ExternModStmtCnum(HirId),
[] ExternModStmtCnum(DefId),
[] GetLangItems,
[] DefinedLangItems(CrateNum),
[] MissingLangItems(CrateNum),
Expand All @@ -570,8 +570,8 @@ define_dep_nodes!( <'tcx>
[] UsedCrateSource(CrateNum),
[] PostorderCnums,

[] Freevars(HirId),
[] MaybeUnusedTraitImport(HirId),
[] Freevars(DefId),
[] MaybeUnusedTraitImport(DefId),
[] MaybeUnusedExternCrates,
[] StabilityIndex,
[] AllCrateNums,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use hir::def_id::DefId;
use util::nodemap::NodeMap;
use util::nodemap::{NodeMap, DefIdMap};
use syntax::ast;
use syntax::ext::base::MacroKind;
use syntax_pos::Span;
Expand Down Expand Up @@ -114,7 +114,7 @@ pub type DefMap = NodeMap<PathResolution>;

/// This is the replacement export map. It maps a module to all of the exports
/// within.
pub type ExportMap = NodeMap<Vec<Export>>;
pub type ExportMap = DefIdMap<Vec<Export>>;

#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
pub struct Export {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,19 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ast::N
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
hasher: &mut StableHasher<W>) {
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
match hcx.node_id_hashing_mode {
NodeIdHashingMode::Ignore => {
// Most NodeIds in the HIR can be ignored, but if there is a
// corresponding entry in the `trait_map` we need to hash that.
// Make sure we don't ignore too much by checking that there is
// no entry in a debug_assert!().
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
debug_assert!(hcx.tcx.in_scope_traits(hir_id).is_none());
}
NodeIdHashingMode::HashDefPath => {
hcx.tcx.hir.definitions().node_to_hir_id(*self).hash_stable(hcx, hasher);
hir_id.hash_stable(hcx, hasher);
}
NodeIdHashingMode::HashTraitsInScope => {
let hir_id = hcx.tcx.hir.node_to_hir_id(*self);
if let Some(traits) = hcx.tcx.in_scope_traits(hir_id) {
// The ordering of the candidates is not fixed. So we hash
// the def-ids and then sort them and hash the collection.
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
// compiler-generated `extern crate` items have a dummy span.
if item.span == DUMMY_SP { return }

let hir_id = self.tcx.hir.node_to_hir_id(item.id);
let cnum = match self.tcx.extern_mod_stmt_cnum(hir_id) {
let def_id = self.tcx.hir.local_def_id(item.id);
let cnum = match self.tcx.extern_mod_stmt_cnum(def_id) {
Some(cnum) => cnum,
None => return,
};
Expand Down
114 changes: 78 additions & 36 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use dep_graph::DepGraph;
use errors::DiagnosticBuilder;
use session::Session;
use middle;
use hir::{TraitCandidate, HirId};
use hir::{TraitCandidate, HirId, ItemLocalId};
use hir::def::{Def, Export};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use hir::map as hir_map;
use hir::map::DefPathHash;
use lint::{self, Lint};
Expand Down Expand Up @@ -816,10 +816,10 @@ pub struct GlobalCtxt<'tcx> {

/// Map indicating what traits are in scope for places where this
/// is relevant; generated by resolve.
trait_map: FxHashMap<HirId, Rc<Vec<TraitCandidate>>>,
trait_map: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, Rc<Vec<TraitCandidate>>>>>,

/// Export map produced by name resolution.
export_map: FxHashMap<HirId, Rc<Vec<Export>>>,
export_map: FxHashMap<DefId, Rc<Vec<Export>>>,

named_region_map: NamedRegionMap,

Expand All @@ -836,11 +836,11 @@ pub struct GlobalCtxt<'tcx> {
// Records the free variables refrenced by every closure
// expression. Do not track deps for this, just recompute it from
// scratch every time.
freevars: FxHashMap<HirId, Rc<Vec<hir::Freevar>>>,
freevars: FxHashMap<DefId, Rc<Vec<hir::Freevar>>>,

maybe_unused_trait_imports: FxHashSet<HirId>,
maybe_unused_trait_imports: FxHashSet<DefId>,

maybe_unused_extern_crates: Vec<(HirId, Span)>,
maybe_unused_extern_crates: Vec<(DefId, Span)>,

// Internal cache for metadata decoding. No need to track deps on this.
pub rcache: RefCell<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
Expand Down Expand Up @@ -1031,6 +1031,35 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
None
};

let mut trait_map = FxHashMap();
for (k, v) in resolutions.trait_map {
let hir_id = hir.node_to_hir_id(k);
let map = trait_map.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashMap()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
}
let mut defs = FxHashMap();
for (k, v) in named_region_map.defs {
let hir_id = hir.node_to_hir_id(k);
let map = defs.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashMap()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id, v);
}
let mut late_bound = FxHashMap();
for k in named_region_map.late_bound {
let hir_id = hir.node_to_hir_id(k);
let map = late_bound.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashSet()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id);
}
let mut object_lifetime_defaults = FxHashMap();
for (k, v) in named_region_map.object_lifetime_defaults {
let hir_id = hir.node_to_hir_id(k);
let map = object_lifetime_defaults.entry(hir_id.owner)
.or_insert_with(|| Rc::new(FxHashMap()));
Rc::get_mut(map).unwrap().insert(hir_id.local_id, Rc::new(v));
}

tls::enter_global(GlobalCtxt {
sess: s,
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
Expand All @@ -1039,40 +1068,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
dep_graph: dep_graph.clone(),
types: common_types,
named_region_map: NamedRegionMap {
defs:
named_region_map.defs
.into_iter()
.map(|(k, v)| (hir.node_to_hir_id(k), v))
.collect(),
late_bound:
named_region_map.late_bound
.into_iter()
.map(|k| hir.node_to_hir_id(k))
.collect(),
object_lifetime_defaults:
named_region_map.object_lifetime_defaults
.into_iter()
.map(|(k, v)| (hir.node_to_hir_id(k), Rc::new(v)))
.collect(),
defs,
late_bound,
object_lifetime_defaults,
},
trait_map: resolutions.trait_map.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
}).collect(),
trait_map,
export_map: resolutions.export_map.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
(k, Rc::new(v))
}).collect(),
freevars: resolutions.freevars.into_iter().map(|(k, v)| {
(hir.node_to_hir_id(k), Rc::new(v))
(hir.local_def_id(k), Rc::new(v))
}).collect(),
maybe_unused_trait_imports:
resolutions.maybe_unused_trait_imports
.into_iter()
.map(|id| hir.node_to_hir_id(id))
.map(|id| hir.local_def_id(id))
.collect(),
maybe_unused_extern_crates:
resolutions.maybe_unused_extern_crates
.into_iter()
.map(|(id, sp)| (hir.node_to_hir_id(id), sp))
.map(|(id, sp)| (hir.local_def_id(id), sp))
.collect(),
hir,
def_path_hash_to_def_id,
Expand Down Expand Up @@ -1966,6 +1981,29 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let (level, src) = self.lint_level_at_node(lint, id);
lint::struct_lint_level(self.sess, lint, level, src, None, msg)
}

pub fn in_scope_traits(self, id: HirId) -> Option<Rc<Vec<TraitCandidate>>> {
self.in_scope_traits_map(id.owner)
.and_then(|map| map.get(&id.local_id).cloned())
}

pub fn named_region(self, id: HirId) -> Option<resolve_lifetime::Region> {
self.named_region_map(id.owner)
.and_then(|map| map.get(&id.local_id).cloned())
}

pub fn is_late_bound(self, id: HirId) -> bool {
self.is_late_bound_map(id.owner)
.map(|set| set.contains(&id.local_id))
.unwrap_or(false)
}

pub fn object_lifetime_defaults(self, id: HirId)
-> Option<Rc<Vec<ObjectLifetimeDefault>>>
{
self.object_lifetime_defaults_map(id.owner)
.and_then(|map| map.get(&id.local_id).cloned())
}
}

pub trait InternAs<T: ?Sized, R> {
Expand Down Expand Up @@ -2013,20 +2051,24 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
}

struct NamedRegionMap {
defs: FxHashMap<HirId, resolve_lifetime::Region>,
late_bound: FxHashSet<HirId>,
object_lifetime_defaults: FxHashMap<HirId, Rc<Vec<ObjectLifetimeDefault>>>,
defs: FxHashMap<DefIndex, Rc<FxHashMap<ItemLocalId, resolve_lifetime::Region>>>,
late_bound: FxHashMap<DefIndex, Rc<FxHashSet<ItemLocalId>>>,
object_lifetime_defaults:
FxHashMap<
DefIndex,
Rc<FxHashMap<ItemLocalId, Rc<Vec<ObjectLifetimeDefault>>>>,
>,
}

pub fn provide(providers: &mut ty::maps::Providers) {
// FIXME(#44234) - almost all of these queries have no sub-queries and
// therefore no actual inputs, they're just reading tables calculated in
// resolve! Does this work? Unsure! That's what the issue is about
providers.in_scope_traits = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id).cloned();
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).cloned();
providers.named_region = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
providers.is_late_bound = |tcx, id| tcx.gcx.named_region_map.late_bound.contains(&id);
providers.object_lifetime_defaults = |tcx, id| {
providers.named_region_map = |tcx, id| tcx.gcx.named_region_map.defs.get(&id).cloned();
providers.is_late_bound_map = |tcx, id| tcx.gcx.named_region_map.late_bound.get(&id).cloned();
providers.object_lifetime_defaults_map = |tcx, id| {
tcx.gcx.named_region_map.object_lifetime_defaults.get(&id).cloned()
};
providers.crate_name = |tcx, id| {
Expand Down
Loading

0 comments on commit caaf365

Please sign in to comment.