Skip to content

Commit 21acf1e

Browse files
committed
Turn HIR lowering into a query
1 parent bbb84a4 commit 21acf1e

File tree

20 files changed

+433
-344
lines changed

20 files changed

+433
-344
lines changed

src/librustc/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ macro_rules! arena_types {
3131
rustc::hir::def_id::DefId,
3232
rustc::ty::subst::SubstsRef<$tcx>
3333
)>,
34+
[few] lowered_hir: rustc::hir::LoweredHir,
3435
[few] hir_map: rustc::hir::map::Map<$tcx>,
3536
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
3637
[decode] specialization_graph: rustc::traits::specialization_graph::Graph,

src/librustc/dep_graph/dep_node.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ macro_rules! define_dep_nodes {
310310
pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
311311
if self.kind.can_reconstruct_query_key() {
312312
let def_path_hash = DefPathHash(self.hash);
313-
tcx.def_path_hash_to_def_id.as_ref()?
314-
.get(&def_path_hash).cloned()
313+
tcx.def_path_hash_to_def_id()?.get(&def_path_hash).cloned()
315314
} else {
316315
None
317316
}
@@ -445,6 +444,12 @@ pub trait RecoverKey<'tcx>: Sized {
445444
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
446445
}
447446

447+
impl RecoverKey<'tcx> for () {
448+
fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
449+
Some(())
450+
}
451+
}
452+
448453
impl RecoverKey<'tcx> for CrateNum {
449454
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
450455
dep_node.extract_def_id(tcx).map(|id| id.krate)
@@ -539,6 +544,18 @@ impl<'tcx> DepNodeParams<'tcx> for CrateNum {
539544
}
540545
}
541546

547+
impl<'tcx> DepNodeParams<'tcx> for () {
548+
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
549+
550+
fn to_fingerprint(&self, _: TyCtxt<'_>) -> Fingerprint {
551+
Fingerprint::ZERO
552+
}
553+
554+
fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
555+
"<no-params>".to_string()
556+
}
557+
}
558+
542559
impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
543560
const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
544561

src/librustc/dep_graph/graph.rs

+21-19
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 {
@@ -193,7 +193,7 @@ impl DepGraph {
193193
cx: C,
194194
arg: A,
195195
task: fn(C, A) -> R,
196-
hash_result: impl FnOnce(&mut StableHashingContext<'_>, &R) -> Option<Fingerprint>,
196+
hash_result: Option<impl FnOnce(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
197197
) -> (R, DepNodeIndex)
198198
where
199199
C: DepGraphSafe + StableHashingContextProvider<'a>,
@@ -229,7 +229,8 @@ impl DepGraph {
229229
|data, key, fingerprint, _| {
230230
data.borrow_mut().alloc_node(key, SmallVec::new(), fingerprint)
231231
},
232-
hash_result::<R>)
232+
Some(hash_result::<R>)
233+
)
233234
}
234235

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

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

266264
let result = if no_tcx {
267265
task(cx, arg)
@@ -279,10 +277,12 @@ impl DepGraph {
279277
};
280278

281279
if cfg!(debug_assertions) {
282-
profq_msg(hcx.sess(), ProfileQueriesMsg::TaskEnd)
280+
hcx.as_ref().map(|hcx| profq_msg(hcx.sess(), ProfileQueriesMsg::TaskEnd));
283281
};
284282

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

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

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

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

src/librustc/hir/map/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use crate::hir::itemlikevisit::ItemLikeVisitor;
2222
use crate::hir::print::Nested;
2323
use crate::util::nodemap::FxHashMap;
2424
use crate::util::common::time;
25-
use crate::ich::StableHashingContext;
2625

2726
use std::io;
2827
use std::result::Result::Err;
@@ -1258,23 +1257,22 @@ impl Named for TraitItem { fn name(&self) -> Name { self.ident.name } }
12581257
impl Named for ImplItem { fn name(&self) -> Name { self.ident.name } }
12591258

12601259
pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
1260+
// FIXME: Error handling here?
1261+
let hir = tcx.lowered_hir();
1262+
12611263
// Build the reverse mapping of `node_to_hir_id`.
1262-
let hir_to_node_id = tcx.hir_defs.node_to_hir_id.iter_enumerated()
1264+
let hir_to_node_id = hir.defs.node_to_hir_id.iter_enumerated()
12631265
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect();
12641266

12651267
let (map, crate_hash) = {
1266-
let krate = tcx.hir_forest.untracked_krate();
1267-
let hcx = StableHashingContext::new(
1268-
tcx.sess,
1269-
krate,
1270-
&tcx.hir_defs,
1271-
tcx.cstore
1272-
);
1268+
let hcx = tcx.create_stable_hashing_context();
1269+
let krate = hir.forest.untracked_krate();
1270+
12731271
let mut collector = NodeCollector::root(
12741272
tcx.sess,
12751273
krate,
12761274
&tcx.dep_graph,
1277-
&tcx.hir_defs,
1275+
&hir.defs,
12781276
&hir_to_node_id,
12791277
hcx
12801278
);
@@ -1290,12 +1288,12 @@ pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
12901288
};
12911289

12921290
let map = Map {
1293-
forest: &tcx.hir_forest,
1291+
forest: &hir.forest,
12941292
dep_graph: tcx.dep_graph.clone(),
12951293
crate_hash,
12961294
map,
12971295
hir_to_node_id,
1298-
definitions: &tcx.hir_defs,
1296+
definitions: &hir.defs,
12991297
};
13001298

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

src/librustc/hir/mod.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ pub use self::UnsafeSource::*;
1212

1313
use crate::hir::def::{Res, DefKind};
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;
@@ -32,6 +34,8 @@ use crate::ty::query::Providers;
3234

3335
use rustc_data_structures::sync::{par_for_each_in, 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};
@@ -65,6 +69,35 @@ pub mod pat_util;
6569
pub mod print;
6670
pub mod upvars;
6771

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

src/librustc/query/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ use syntax_pos::symbol::InternedString;
3131
// as they will raise an fatal error on query cycles instead.
3232
rustc_queries! {
3333
Other {
34+
query prepare_outputs(_: ()) -> Result<Arc<OutputFilenames>, ErrorReported> {
35+
no_hash
36+
eval_always
37+
desc { "preparing outputs" }
38+
}
39+
40+
query lower_ast_to_hir(_: ()) -> Result<&'tcx hir::LoweredHir, ErrorReported> {
41+
no_hash
42+
eval_always
43+
desc { "lowering AST to HIR" }
44+
}
45+
3446
query hir_map(_: CrateNum) -> &'tcx hir::map::Map<'tcx> {
3547
no_hash
3648
eval_always

src/librustc/session/config.rs

+9
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ impl BorrowckMode {
496496
}
497497
}
498498

499+
#[derive(Clone)]
500+
pub struct InputsAndOutputs {
501+
pub input: Input,
502+
pub input_path: Option<PathBuf>,
503+
pub output_dir: Option<PathBuf>,
504+
pub output_file: Option<PathBuf>,
505+
}
506+
507+
#[derive(Clone)]
499508
pub enum Input {
500509
/// Loads source from file
501510
File(PathBuf),

0 commit comments

Comments
 (0)