Skip to content

Commit

Permalink
Turn HIR lowering into a query
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jul 8, 2019
1 parent d5fbdd2 commit a7c010d
Show file tree
Hide file tree
Showing 20 changed files with 433 additions and 344 deletions.
1 change: 1 addition & 0 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ macro_rules! arena_types {
rustc::hir::def_id::DefId,
rustc::ty::subst::SubstsRef<$tcx>
)>,
[few] lowered_hir: rustc::hir::LoweredHir,
[few] hir_map: rustc::hir::map::Map<$tcx>,
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
[decode] specialization_graph: rustc::traits::specialization_graph::Graph,
Expand Down
21 changes: 19 additions & 2 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ macro_rules! define_dep_nodes {
pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
if self.kind.can_reconstruct_query_key() {
let def_path_hash = DefPathHash(self.hash);
tcx.def_path_hash_to_def_id.as_ref()?
.get(&def_path_hash).cloned()
tcx.def_path_hash_to_def_id()?.get(&def_path_hash).cloned()
} else {
None
}
Expand Down Expand Up @@ -439,6 +438,12 @@ pub trait RecoverKey<'tcx>: Sized {
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
}

impl RecoverKey<'tcx> for () {
fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
Some(())
}
}

impl RecoverKey<'tcx> for CrateNum {
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
dep_node.extract_def_id(tcx).map(|id| id.krate)
Expand Down Expand Up @@ -533,6 +538,18 @@ impl<'tcx> DepNodeParams<'tcx> for CrateNum {
}
}

impl<'tcx> DepNodeParams<'tcx> for () {
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;

fn to_fingerprint(&self, _: TyCtxt<'_>) -> Fingerprint {
Fingerprint::ZERO
}

fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
"<no-params>".to_string()
}
}

impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
const CAN_RECONSTRUCT_QUERY_KEY: bool = false;

Expand Down
40 changes: 21 additions & 19 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ struct DepGraphData {
loaded_from_cache: Lock<FxHashMap<DepNodeIndex, bool>>,
}

pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Option<Fingerprint>
pub fn hash_result<R>(hcx: &mut StableHashingContext<'_>, result: &R) -> Fingerprint
where
R: for<'a> HashStable<StableHashingContext<'a>>,
{
let mut stable_hasher = StableHasher::new();
result.hash_stable(hcx, &mut stable_hasher);

Some(stable_hasher.finish())
stable_hasher.finish()
}

impl DepGraph {
Expand Down Expand Up @@ -193,7 +193,7 @@ impl DepGraph {
cx: C,
arg: A,
task: fn(C, A) -> R,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex)
where
C: DepGraphSafe + StableHashingContextProvider<'a>,
Expand Down Expand Up @@ -229,7 +229,8 @@ impl DepGraph {
|data, key, fingerprint, _| {
data.borrow_mut().alloc_node(key, SmallVec::new(), fingerprint)
},
hash_result::<R>)
Some(hash_result::<R>)
)
}

fn with_task_impl<'a, C, A, R>(
Expand All @@ -244,24 +245,21 @@ impl DepGraph {
DepNode,
Fingerprint,
Option<TaskDeps>) -> DepNodeIndex,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex)
where
C: DepGraphSafe + StableHashingContextProvider<'a>,
{
if let Some(ref data) = self.data {
let task_deps = create_task(key).map(|deps| Lock::new(deps));

// In incremental mode, hash the result of the task. We don't
// do anything with the hash yet, but we are computing it
// anyway so that
// - we make sure that the infrastructure works and
// - we can get an idea of the runtime cost.
let mut hcx = cx.get_stable_hashing_context();

if cfg!(debug_assertions) {
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskBegin(key.clone()))
};
let hcx = hash_result.as_ref().map(|_| {
let hcx = cx.get_stable_hashing_context();
if cfg!(debug_assertions) {
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskBegin(key.clone()))
};
hcx
});

let result = if no_tcx {
task(cx, arg)
Expand All @@ -279,10 +277,12 @@ impl DepGraph {
};

if cfg!(debug_assertions) {
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskEnd)
hcx.as_ref().map(|hcx| profq_msg(hcx.sess(), ProfileQueriesMsg::TaskEnd));
};

let current_fingerprint = hash_result(&mut hcx, &result);
let current_fingerprint = hash_result.map(|hash_result| {
hash_result(&mut hcx.unwrap(), &result)
});

let dep_node_index = finish_task_and_alloc_depnode(
&data.current,
Expand All @@ -291,7 +291,9 @@ impl DepGraph {
task_deps.map(|lock| lock.into_inner()),
);

let print_status = cfg!(debug_assertions) && hcx.sess().opts.debugging_opts.dep_tasks;
let print_status = cfg!(debug_assertions) && ty::tls::with_opt(|tcx| {
tcx.map(|tcx| tcx.sess.opts.debugging_opts.dep_tasks).unwrap_or(false)
});

// Determine the color of the new DepNode.
if let Some(prev_index) = data.previous.node_to_index_opt(&key) {
Expand Down Expand Up @@ -378,7 +380,7 @@ impl DepGraph {
cx: C,
arg: A,
task: fn(C, A) -> R,
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex)
where
C: DepGraphSafe + StableHashingContextProvider<'a>,
Expand Down
22 changes: 10 additions & 12 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::hir::itemlikevisit::ItemLikeVisitor;
use crate::hir::print::Nested;
use crate::util::nodemap::FxHashMap;
use crate::util::common::time;
use crate::ich::StableHashingContext;

use std::result::Result::Err;
use crate::ty::query::Providers;
Expand Down Expand Up @@ -1136,23 +1135,22 @@ impl Named for TraitItem { fn name(&self) -> Name { self.ident.name } }
impl Named for ImplItem { fn name(&self) -> Name { self.ident.name } }

pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
// FIXME: Error handling here?
let hir = tcx.lowered_hir();

// Build the reverse mapping of `node_to_hir_id`.
let hir_to_node_id = tcx.hir_defs.node_to_hir_id.iter_enumerated()
let hir_to_node_id = hir.defs.node_to_hir_id.iter_enumerated()
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect();

let (map, crate_hash) = {
let krate = tcx.hir_forest.untracked_krate();
let hcx = StableHashingContext::new(
tcx.sess,
krate,
&tcx.hir_defs,
tcx.cstore
);
let hcx = tcx.create_stable_hashing_context();
let krate = hir.forest.untracked_krate();

let mut collector = NodeCollector::root(
tcx.sess,
krate,
&tcx.dep_graph,
&tcx.hir_defs,
&hir.defs,
&hir_to_node_id,
hcx
);
Expand All @@ -1168,12 +1166,12 @@ pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
};

let map = Map {
forest: &tcx.hir_forest,
forest: &hir.forest,
dep_graph: tcx.dep_graph.clone(),
crate_hash,
map,
hir_to_node_id,
definitions: &tcx.hir_defs,
definitions: &hir.defs,
};

time(tcx.sess, "validate hir map", || {
Expand Down
35 changes: 34 additions & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub use self::UnsafeSource::*;
use crate::hir::def::{Res, DefKind};
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
use crate::hir::ptr::P;
use crate::util::nodemap::{NodeMap, FxHashSet};
use crate::hir::map::definitions::DefPathHash;
use crate::hir::def::Export;
use crate::util::nodemap::NodeMap;
use crate::mir::mono::Linkage;

use errors::FatalError;
Expand All @@ -32,6 +34,8 @@ use crate::ty::query::Providers;

use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::StableVec;
use rustc_macros::HashStable;

use serialize::{self, Encoder, Encodable, Decoder, Decodable};
Expand Down Expand Up @@ -66,6 +70,35 @@ pub mod print;
pub mod ptr;
pub mod upvars;

pub struct LoweredHir {
pub forest: map::Forest,
pub defs: map::Definitions,

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

pub maybe_unused_trait_imports: FxHashSet<DefId>,
pub maybe_unused_extern_crates: Vec<(DefId, Span)>,

/// A map of glob use to a set of names it actually imports. Currently only
/// used in save-analysis.
pub glob_map: FxHashMap<DefId, FxHashSet<ast::Name>>,
/// Extern prelude entries. The value is `true` if the entry was introduced
/// via `extern crate` item and not `--extern` option or compiler built-in.
pub extern_prelude: FxHashMap<ast::Name, bool>,

/// A map from DefPathHash -> DefId. Includes DefIds from the local crate
/// as well as all upstream crates. Only populated in incremental mode.
pub def_path_hash_to_def_id: Option<FxHashMap<DefPathHash, DefId>>,

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

}

/// Uniquely identifies a node in the HIR of the current crate. It is
/// composed of the `owner`, which is the `DefIndex` of the directly enclosing
/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
Expand Down
12 changes: 12 additions & 0 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ use syntax_pos::symbol::InternedString;
// as they will raise an fatal error on query cycles instead.
rustc_queries! {
Other {
query prepare_outputs(_: ()) -> Result<Arc<OutputFilenames>, ErrorReported> {
no_hash
eval_always
desc { "preparing outputs" }
}

query lower_ast_to_hir(_: ()) -> Result<&'tcx hir::LoweredHir, ErrorReported> {
no_hash
eval_always
desc { "lowering AST to HIR" }
}

query hir_map(_: CrateNum) -> &'tcx hir::map::Map<'tcx> {
no_hash
eval_always
Expand Down
9 changes: 9 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ impl BorrowckMode {
}
}

#[derive(Clone)]
pub struct InputsAndOutputs {
pub input: Input,
pub input_path: Option<PathBuf>,
pub output_dir: Option<PathBuf>,
pub output_file: Option<PathBuf>,
}

#[derive(Clone)]
pub enum Input {
/// Loads source from file
File(PathBuf),
Expand Down
Loading

0 comments on commit a7c010d

Please sign in to comment.