Skip to content
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

Turn HIR lowering into a query #59205

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,7 @@ name = "rustc_data_structures"
version = "0.0.0"
dependencies = [
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"ena 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"graphviz 0.0.0",
"indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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,
[] region_scope_tree: rustc::middle::region::ScopeTree,
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
44 changes: 22 additions & 22 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 Expand Up @@ -674,8 +676,6 @@ impl DepGraph {
}
} else {
match dep_dep_node.kind {
DepKind::Hir |
DepKind::HirBody |
DepKind::CrateMetadata => {
if dep_dep_node.extract_def_id(tcx).is_none() {
// If the node does not exist anymore, we
Expand Down Expand Up @@ -719,7 +719,7 @@ impl DepGraph {
None => {
if !tcx.sess.has_errors() {
bug!("try_mark_previous_green() - Forcing the DepNode \
should have set its color")
should have set its color - dep node {:?}", dep_dep_node)
} else {
// If the query we just forced has resulted
// in some kind of compilation error, we
Expand Down
51 changes: 26 additions & 25 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::dep_graph::{DepGraph, DepNode, DepKind, DepNodeIndex};

use crate::hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId};

use crate::middle::cstore::CrateStoreDyn;

use rustc_target::spec::abi::Abi;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::indexed_vec::IndexVec;
Expand All @@ -27,6 +25,7 @@ use crate::util::common::time;

use std::result::Result::Err;
use crate::ty::query::Providers;
use crate::ty::TyCtxt;

pub mod blocks;
mod collector;
Expand Down Expand Up @@ -1135,45 +1134,47 @@ impl Named for StructField { fn name(&self) -> Name { self.ident.name } }
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<'hir>(sess: &crate::session::Session,
cstore: &CrateStoreDyn,
forest: &'hir Forest,
definitions: &'hir Definitions)
-> Map<'hir> {
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 = definitions.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 hcx = crate::ich::StableHashingContext::new(sess, &forest.krate, definitions, cstore);

let mut collector = NodeCollector::root(sess,
&forest.krate,
&forest.dep_graph,
&definitions,
&hir_to_node_id,
hcx);
intravisit::walk_crate(&mut collector, &forest.krate);

let crate_disambiguator = sess.local_crate_disambiguator();
let cmdline_args = sess.opts.dep_tracking_hash();
let hcx = tcx.create_stable_hashing_context();
let krate = hir.forest.untracked_krate();

let mut collector = NodeCollector::root(
tcx.sess,
krate,
&tcx.dep_graph,
&hir.defs,
&hir_to_node_id,
hcx
);
intravisit::walk_crate(&mut collector, krate);

let crate_disambiguator = tcx.sess.local_crate_disambiguator();
let cmdline_args = tcx.sess.opts.dep_tracking_hash();
collector.finalize_and_compute_crate_hash(
crate_disambiguator,
cstore,
tcx.cstore,
cmdline_args
)
};

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

time(sess, "validate hir map", || {
time(tcx.sess, "validate hir map", || {
hir_id_validator::check_crate(&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
18 changes: 18 additions & 0 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ 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
desc { "indexing HIR" }
}

/// Records the type of every item.
query type_of(key: DefId) -> Ty<'tcx> {
cache_on_disk_if { key.is_local() }
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