Skip to content

Commit b8c1493

Browse files
committed
Turn HIR lowering into a query
1 parent 43af481 commit b8c1493

File tree

21 files changed

+456
-361
lines changed

21 files changed

+456
-361
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
//! fingerprint for a given set of node parameters.
5151
5252
use crate::mir::interpret::GlobalId;
53-
use crate::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
53+
use crate::hir::def_id::{CrateNum, LocalCrate, DefId, DefIndex, CRATE_DEF_INDEX};
5454
use crate::hir::map::DefPathHash;
5555
use crate::hir::HirId;
5656

@@ -309,8 +309,7 @@ macro_rules! define_dep_nodes {
309309
pub fn extract_def_id(&self, tcx: TyCtxt<'_, '_, '_>) -> Option<DefId> {
310310
if self.kind.can_reconstruct_query_key() {
311311
let def_path_hash = DefPathHash(self.hash);
312-
tcx.def_path_hash_to_def_id.as_ref()?
313-
.get(&def_path_hash).cloned()
312+
tcx.def_path_hash_to_def_id()?.get(&def_path_hash).cloned()
314313
} else {
315314
None
316315
}
@@ -444,6 +443,12 @@ pub trait RecoverKey<'tcx>: Sized {
444443
fn recover(tcx: TyCtxt<'_, 'tcx, 'tcx>, dep_node: &DepNode) -> Option<Self>;
445444
}
446445

446+
impl RecoverKey<'tcx> for LocalCrate {
447+
fn recover(_: TyCtxt<'_, 'tcx, 'tcx>, _: &DepNode) -> Option<Self> {
448+
Some(LocalCrate)
449+
}
450+
}
451+
447452
impl RecoverKey<'tcx> for CrateNum {
448453
fn recover(tcx: TyCtxt<'_, 'tcx, 'tcx>, dep_node: &DepNode) -> Option<Self> {
449454
dep_node.extract_def_id(tcx).map(|id| id.krate)
@@ -537,6 +542,18 @@ impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for CrateNum {
537542
}
538543
}
539544

545+
impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for LocalCrate {
546+
const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
547+
548+
fn to_fingerprint(&self, _: TyCtxt<'_, '_, '_>) -> Fingerprint {
549+
Fingerprint::ZERO
550+
}
551+
552+
fn to_debug_str(&self, _: TyCtxt<'a, 'gcx, 'tcx>) -> String {
553+
"<local crate>".to_string()
554+
}
555+
}
556+
540557
impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for (DefId, DefId) {
541558
const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
542559

src/librustc/dep_graph/graph.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ struct DepGraphData {
7979
loaded_from_cache: Lock<FxHashMap<DepNodeIndex, bool>>,
8080
}
8181

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

89-
Some(stable_hasher.finish())
89+
stable_hasher.finish()
9090
}
9191

9292
impl DepGraph {
@@ -194,7 +194,7 @@ impl DepGraph {
194194
cx: C,
195195
arg: A,
196196
task: fn(C, A) -> R,
197-
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
197+
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
198198
) -> (R, DepNodeIndex)
199199
where
200200
C: DepGraphSafe + StableHashingContextProvider<'a>,
@@ -230,7 +230,8 @@ impl DepGraph {
230230
|data, key, fingerprint, _| {
231231
data.borrow_mut().alloc_node(key, SmallVec::new(), fingerprint)
232232
},
233-
hash_result::<R>)
233+
Some(hash_result::<R>)
234+
)
234235
}
235236

236237
fn with_task_impl<'a, C, A, R>(
@@ -245,24 +246,21 @@ impl DepGraph {
245246
DepNode,
246247
Fingerprint,
247248
Option<TaskDeps>) -> DepNodeIndex,
248-
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
249+
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
249250
) -> (R, DepNodeIndex)
250251
where
251252
C: DepGraphSafe + StableHashingContextProvider<'a>,
252253
{
253254
if let Some(ref data) = self.data {
254255
let task_deps = create_task(key).map(|deps| Lock::new(deps));
255256

256-
// In incremental mode, hash the result of the task. We don't
257-
// do anything with the hash yet, but we are computing it
258-
// anyway so that
259-
// - we make sure that the infrastructure works and
260-
// - we can get an idea of the runtime cost.
261-
let mut hcx = cx.get_stable_hashing_context();
262-
263-
if cfg!(debug_assertions) {
264-
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskBegin(key.clone()))
265-
};
257+
let hcx = hash_result.as_ref().map(|_| {
258+
let hcx = cx.get_stable_hashing_context();
259+
if cfg!(debug_assertions) {
260+
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskBegin(key.clone()))
261+
};
262+
hcx
263+
});
266264

267265
let result = if no_tcx {
268266
task(cx, arg)
@@ -280,10 +278,12 @@ impl DepGraph {
280278
};
281279

282280
if cfg!(debug_assertions) {
283-
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskEnd)
281+
hcx.as_ref().map(|hcx| profq_msg(hcx.sess(), ProfileQueriesMsg::TaskEnd));
284282
};
285283

286-
let current_fingerprint = hash_result(&mut hcx, &result);
284+
let current_fingerprint = hash_result.map(|hash_result| {
285+
hash_result(&mut hcx.unwrap(), &result)
286+
});
287287

288288
let dep_node_index = finish_task_and_alloc_depnode(
289289
&data.current,
@@ -292,7 +292,9 @@ impl DepGraph {
292292
task_deps.map(|lock| lock.into_inner()),
293293
);
294294

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

297299
// Determine the color of the new DepNode.
298300
if let Some(prev_index) = data.previous.node_to_index_opt(&key) {
@@ -379,7 +381,7 @@ impl DepGraph {
379381
cx: C,
380382
arg: A,
381383
task: fn(C, A) -> R,
382-
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
384+
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
383385
) -> (R, DepNodeIndex)
384386
where
385387
C: DepGraphSafe + StableHashingContextProvider<'a>,

src/librustc/hir/def_id.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::ty::{self, TyCtxt};
22
use crate::hir::map::definitions::FIRST_FREE_HIGH_DEF_INDEX;
33
use rustc_data_structures::indexed_vec::Idx;
4+
use rustc_macros::HashStable;
45
use serialize;
56
use std::fmt;
67
use std::u32;
@@ -96,6 +97,9 @@ impl fmt::Display for CrateNum {
9697
impl serialize::UseSpecializedEncodable for CrateNum {}
9798
impl serialize::UseSpecializedDecodable for CrateNum {}
9899

100+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, HashStable)]
101+
pub struct LocalCrate;
102+
99103
/// A DefIndex is an index into the hir-map for a crate, identifying a
100104
/// particular definition. It should really be considered an interned
101105
/// shorthand for a particular DefPath.

src/librustc/hir/map/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use crate::hir::itemlikevisit::ItemLikeVisitor;
1919
use crate::hir::print::Nested;
2020
use crate::util::nodemap::FxHashMap;
2121
use crate::util::common::time;
22-
use crate::ich::StableHashingContext;
2322

2423
use std::io;
2524
use std::result::Result::Err;
@@ -1231,23 +1230,22 @@ impl Named for TraitItem { fn name(&self) -> Name { self.ident.name } }
12311230
impl Named for ImplItem { fn name(&self) -> Name { self.ident.name } }
12321231

12331232
pub fn map_crate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> Map<'tcx> {
1233+
// FIXME: Error handling here?
1234+
let hir = tcx.lowered_hir();
1235+
12341236
// Build the reverse mapping of `node_to_hir_id`.
1235-
let hir_to_node_id = tcx.hir_defs.node_to_hir_id.iter_enumerated()
1237+
let hir_to_node_id = hir.defs.node_to_hir_id.iter_enumerated()
12361238
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect();
12371239

12381240
let (map, crate_hash) = {
1239-
let krate = tcx.hir_forest.untracked_krate();
1240-
let hcx = StableHashingContext::new(
1241-
tcx.sess,
1242-
krate,
1243-
&tcx.hir_defs,
1244-
tcx.cstore
1245-
);
1241+
let hcx = tcx.create_stable_hashing_context();
1242+
let krate = hir.forest.untracked_krate();
1243+
12461244
let mut collector = NodeCollector::root(
12471245
tcx.sess,
12481246
krate,
12491247
&tcx.dep_graph,
1250-
&tcx.hir_defs,
1248+
&hir.defs,
12511249
&hir_to_node_id,
12521250
hcx
12531251
);
@@ -1275,12 +1273,12 @@ pub fn map_crate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> Map<'tcx> {
12751273
}
12761274

12771275
let map = Map {
1278-
forest: &tcx.hir_forest,
1276+
forest: &hir.forest,
12791277
dep_graph: tcx.dep_graph.clone(),
12801278
crate_hash,
12811279
map,
12821280
hir_to_node_id,
1283-
definitions: &tcx.hir_defs,
1281+
definitions: &hir.defs,
12841282
};
12851283

12861284
time(tcx.sess, "validate hir map", || {

src/librustc/hir/mod.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ pub use self::UnsafeSource::*;
1212

1313
use crate::hir::def::Def;
1414
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
15-
use crate::util::nodemap::{NodeMap, FxHashSet};
15+
use crate::hir::map::definitions::DefPathHash;
16+
use crate::hir::def::Export;
17+
use crate::util::nodemap::NodeMap;
1618
use crate::mir::mono::Linkage;
1719

1820
use errors::FatalError;
@@ -30,8 +32,10 @@ use syntax::util::parser::ExprPrecedence;
3032
use crate::ty::AdtKind;
3133
use crate::ty::query::Providers;
3234

33-
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
35+
use rustc_data_structures::sync::{par_for_each_in, Lrc, Send, Sync};
3436
use rustc_data_structures::thin_vec::ThinVec;
37+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
38+
use rustc_data_structures::stable_hasher::StableVec;
3539
use rustc_macros::HashStable;
3640

3741
use serialize::{self, Encoder, Encodable, Decoder, Decodable};
@@ -63,6 +67,40 @@ pub mod map;
6367
pub mod pat_util;
6468
pub mod print;
6569

70+
pub struct LoweredHir {
71+
pub forest: map::Forest,
72+
pub defs: map::Definitions,
73+
74+
/// Export map produced by name resolution.
75+
pub export_map: FxHashMap<DefId, Lrc<Vec<Export>>>,
76+
77+
// Records the free variables referenced by every closure
78+
// expression. Do not track deps for this, just recompute it from
79+
// scratch every time.
80+
pub freevars: FxHashMap<DefId, Lrc<Vec<Freevar>>>,
81+
82+
pub maybe_unused_trait_imports: FxHashSet<DefId>,
83+
pub maybe_unused_extern_crates: Vec<(DefId, Span)>,
84+
85+
/// A map of glob use to a set of names it actually imports. Currently only
86+
/// used in save-analysis.
87+
pub glob_map: FxHashMap<DefId, FxHashSet<ast::Name>>,
88+
/// Extern prelude entries. The value is `true` if the entry was introduced
89+
/// via `extern crate` item and not `--extern` option or compiler built-in.
90+
pub extern_prelude: FxHashMap<ast::Name, bool>,
91+
92+
/// A map from DefPathHash -> DefId. Includes DefIds from the local crate
93+
/// as well as all upstream crates. Only populated in incremental mode.
94+
pub def_path_hash_to_def_id: Option<FxHashMap<DefPathHash, DefId>>,
95+
96+
/// Map indicating what traits are in scope for places where this
97+
/// is relevant; generated by resolve.
98+
pub trait_map: FxHashMap<DefIndex,
99+
Lrc<FxHashMap<ItemLocalId,
100+
Lrc<StableVec<TraitCandidate>>>>>,
101+
102+
}
103+
66104
/// Uniquely identifies a node in the HIR of the current crate. It is
67105
/// composed of the `owner`, which is the `DefIndex` of the directly enclosing
68106
/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),

src/librustc/query/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ty::query::queries;
33
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
44
use crate::ty::subst::SubstsRef;
55
use crate::dep_graph::SerializedDepNodeIndex;
6-
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
6+
use crate::hir::def_id::{CrateNum, DefId, DefIndex, LocalCrate};
77
use crate::mir::interpret::GlobalId;
88
use crate::traits;
99
use crate::traits::query::{
@@ -30,6 +30,18 @@ use syntax_pos::symbol::InternedString;
3030
// as they will raise an fatal error on query cycles instead.
3131
rustc_queries! {
3232
Other {
33+
query prepare_outputs(_: LocalCrate) -> Result<Arc<OutputFilenames>, ErrorReported> {
34+
no_hash
35+
eval_always
36+
desc { "preparing outputs" }
37+
}
38+
39+
query lower_ast_to_hir(_: LocalCrate) -> Result<&'tcx hir::LoweredHir, ErrorReported> {
40+
no_hash
41+
eval_always
42+
desc { "lowering AST to HIR" }
43+
}
44+
3345
query hir_map(_: CrateNum) -> &'tcx hir::map::Map<'tcx> {
3446
no_hash
3547
eval_always

src/librustc/session/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,15 @@ impl BorrowckMode {
475475
}
476476
}
477477

478+
#[derive(Clone)]
479+
pub struct InputsAndOutputs {
480+
pub input: Input,
481+
pub input_path: Option<PathBuf>,
482+
pub output_dir: Option<PathBuf>,
483+
pub output_file: Option<PathBuf>,
484+
}
485+
486+
#[derive(Clone)]
478487
pub enum Input {
479488
/// Loads source from file
480489
File(PathBuf),

0 commit comments

Comments
 (0)