Skip to content

Commit a7c010d

Browse files
committed
Turn HIR lowering into a query
1 parent d5fbdd2 commit a7c010d

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
@@ -304,8 +304,7 @@ macro_rules! define_dep_nodes {
304304
pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
305305
if self.kind.can_reconstruct_query_key() {
306306
let def_path_hash = DefPathHash(self.hash);
307-
tcx.def_path_hash_to_def_id.as_ref()?
308-
.get(&def_path_hash).cloned()
307+
tcx.def_path_hash_to_def_id()?.get(&def_path_hash).cloned()
309308
} else {
310309
None
311310
}
@@ -439,6 +438,12 @@ pub trait RecoverKey<'tcx>: Sized {
439438
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
440439
}
441440

441+
impl RecoverKey<'tcx> for () {
442+
fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
443+
Some(())
444+
}
445+
}
446+
442447
impl RecoverKey<'tcx> for CrateNum {
443448
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
444449
dep_node.extract_def_id(tcx).map(|id| id.krate)
@@ -533,6 +538,18 @@ impl<'tcx> DepNodeParams<'tcx> for CrateNum {
533538
}
534539
}
535540

541+
impl<'tcx> DepNodeParams<'tcx> for () {
542+
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
543+
544+
fn to_fingerprint(&self, _: TyCtxt<'_>) -> Fingerprint {
545+
Fingerprint::ZERO
546+
}
547+
548+
fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
549+
"<no-params>".to_string()
550+
}
551+
}
552+
536553
impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
537554
const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
538555

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::result::Result::Err;
2827
use crate::ty::query::Providers;
@@ -1136,23 +1135,22 @@ impl Named for TraitItem { fn name(&self) -> Name { self.ident.name } }
11361135
impl Named for ImplItem { fn name(&self) -> Name { self.ident.name } }
11371136

11381137
pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
1138+
// FIXME: Error handling here?
1139+
let hir = tcx.lowered_hir();
1140+
11391141
// Build the reverse mapping of `node_to_hir_id`.
1140-
let hir_to_node_id = tcx.hir_defs.node_to_hir_id.iter_enumerated()
1142+
let hir_to_node_id = hir.defs.node_to_hir_id.iter_enumerated()
11411143
.map(|(node_id, &hir_id)| (hir_id, node_id)).collect();
11421144

11431145
let (map, crate_hash) = {
1144-
let krate = tcx.hir_forest.untracked_krate();
1145-
let hcx = StableHashingContext::new(
1146-
tcx.sess,
1147-
krate,
1148-
&tcx.hir_defs,
1149-
tcx.cstore
1150-
);
1146+
let hcx = tcx.create_stable_hashing_context();
1147+
let krate = hir.forest.untracked_krate();
1148+
11511149
let mut collector = NodeCollector::root(
11521150
tcx.sess,
11531151
krate,
11541152
&tcx.dep_graph,
1155-
&tcx.hir_defs,
1153+
&hir.defs,
11561154
&hir_to_node_id,
11571155
hcx
11581156
);
@@ -1168,12 +1166,12 @@ pub fn map_crate(tcx: TyCtxt<'_>) -> Map<'_> {
11681166
};
11691167

11701168
let map = Map {
1171-
forest: &tcx.hir_forest,
1169+
forest: &hir.forest,
11721170
dep_graph: tcx.dep_graph.clone(),
11731171
crate_hash,
11741172
map,
11751173
hir_to_node_id,
1176-
definitions: &tcx.hir_defs,
1174+
definitions: &hir.defs,
11771175
};
11781176

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

src/librustc/hir/mod.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ pub use self::UnsafeSource::*;
1313
use crate::hir::def::{Res, DefKind};
1414
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
1515
use crate::hir::ptr::P;
16-
use crate::util::nodemap::{NodeMap, FxHashSet};
16+
use crate::hir::map::definitions::DefPathHash;
17+
use crate::hir::def::Export;
18+
use crate::util::nodemap::NodeMap;
1719
use crate::mir::mono::Linkage;
1820

1921
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};
@@ -66,6 +70,35 @@ pub mod print;
6670
pub mod ptr;
6771
pub mod upvars;
6872

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

485+
#[derive(Clone)]
486+
pub struct InputsAndOutputs {
487+
pub input: Input,
488+
pub input_path: Option<PathBuf>,
489+
pub output_dir: Option<PathBuf>,
490+
pub output_file: Option<PathBuf>,
491+
}
492+
493+
#[derive(Clone)]
485494
pub enum Input {
486495
/// Loads source from file
487496
File(PathBuf),

0 commit comments

Comments
 (0)