Skip to content

Commit 4c59c92

Browse files
committed
Auto merge of #40873 - cramertj:on-demandify-queries, r=nikomatsakis
On demandify reachability cc #40746 I tried following this guidance from #40746: > The following tasks currently execute before a tcx is built, but they could be easily converted into queries that are requested after tcx is built. The main reason they are the way they are was to avoid a gratuitious refcell (but using the refcell map seems fine)... but the result of moving `region_maps` out of `TyCtxt` and into a query caused a lot of churn, and seems like it could potentially result in a rather large performance hit, since it means a dep-graph lookup on every use of `region_maps` (rather than just a field access). Possibly `TyCtxt` could store a `RefCell<Option<RegionMap>>` internally and use that to prevent repeat lookups, but that feels like it's duplicating the work of the dep-graph. @nikomatsakis What did you have in mind for this?
2 parents b9c5197 + aab2cca commit 4c59c92

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/librustc/middle/reachable.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
// makes all other generics or inline functions that it references
1616
// reachable as well.
1717

18-
use dep_graph::DepNode;
1918
use hir::map as hir_map;
2019
use hir::def::Def;
21-
use hir::def_id::DefId;
20+
use hir::def_id::{DefId, CrateNum};
2221
use ty::{self, TyCtxt};
22+
use ty::maps::Providers;
2323
use middle::privacy;
2424
use session::config;
2525
use util::nodemap::{NodeSet, FxHashSet};
@@ -362,7 +362,11 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
362362
}
363363

364364
pub fn find_reachable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> NodeSet {
365-
let _task = tcx.dep_graph.in_task(DepNode::Reachability);
365+
ty::queries::reachable_set::get(tcx, DUMMY_SP, LOCAL_CRATE)
366+
}
367+
368+
fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) -> NodeSet {
369+
debug_assert!(crate_num == LOCAL_CRATE);
366370

367371
let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
368372

@@ -408,3 +412,10 @@ pub fn find_reachable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> NodeSet {
408412
// Return the set of reachable symbols.
409413
reachable_context.reachable_symbols
410414
}
415+
416+
pub fn provide(providers: &mut Providers) {
417+
*providers = Providers {
418+
reachable_set,
419+
..*providers
420+
};
421+
}

src/librustc/ty/maps.rs

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use middle::privacy::AccessLevels;
1515
use mir;
1616
use session::CompileResult;
1717
use ty::{self, CrateInherentImpls, Ty, TyCtxt};
18+
use util::nodemap::NodeSet;
1819

1920
use rustc_data_structures::indexed_vec::IndexVec;
2021
use std::cell::{RefCell, RefMut};
@@ -209,6 +210,11 @@ impl<'tcx> QueryDescription for queries::typeck_item_bodies<'tcx> {
209210
}
210211
}
211212

213+
impl<'tcx> QueryDescription for queries::reachable_set<'tcx> {
214+
fn describe(_: TyCtxt, _: CrateNum) -> String {
215+
format!("reachability")
216+
}
217+
}
212218

213219
macro_rules! define_maps {
214220
(<$tcx:tt>
@@ -440,6 +446,8 @@ define_maps! { <'tcx>
440446
/// Performs the privacy check and computes "access levels".
441447
pub privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Rc<AccessLevels>,
442448

449+
pub reachable_set: reachability_dep_node(CrateNum) -> NodeSet,
450+
443451
pub mir_shims: mir_shim(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>
444452
}
445453

@@ -451,6 +459,10 @@ fn crate_inherent_impls_dep_node(_: CrateNum) -> DepNode<DefId> {
451459
DepNode::Coherence
452460
}
453461

462+
fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
463+
DepNode::Reachability
464+
}
465+
454466
fn mir_shim(instance: ty::InstanceDef) -> DepNode<DefId> {
455467
instance.dep_node()
456468
}

src/librustc_driver/driver.rs

+1
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
889889
rustc_privacy::provide(&mut local_providers);
890890
typeck::provide(&mut local_providers);
891891
ty::provide(&mut local_providers);
892+
reachable::provide(&mut local_providers);
892893

893894
let mut extern_providers = ty::maps::Providers::default();
894895
cstore::provide(&mut extern_providers);

0 commit comments

Comments
 (0)