Skip to content

Commit c9a8687

Browse files
committed
Auto merge of #57916 - Zoxc:incr-passes4, r=michaelwoerister
Misc performance tweaks r? @michaelwoerister
2 parents 1aa2506 + ee229f7 commit c9a8687

File tree

24 files changed

+136
-117
lines changed

24 files changed

+136
-117
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ define_dep_nodes!( <'tcx>
479479
[] CheckModPrivacy(DefId),
480480
[] CheckModIntrinsics(DefId),
481481
[] CheckModLiveness(DefId),
482+
[] CheckModImplWf(DefId),
482483
[] CollectModItemTypes(DefId),
483484

484485
[] Reachability,

src/librustc/hir/check_attr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use ty::TyCtxt;
99
use ty::query::Providers;
10-
use ty::query::queries;
1110

1211
use hir;
1312
use hir::def_id::DefId;
@@ -355,7 +354,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckAttrVisitor<'a, 'tcx> {
355354

356355
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
357356
for &module in tcx.hir().krate().modules.keys() {
358-
queries::check_mod_attrs::ensure(tcx, tcx.hir().local_def_id(module));
357+
tcx.ensure().check_mod_attrs(tcx.hir().local_def_id(module));
359358
}
360359
}
361360

src/librustc/middle/intrinsicck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use hir::def::Def;
22
use hir::def_id::DefId;
33
use ty::{self, Ty, TyCtxt};
44
use ty::layout::{LayoutError, Pointer, SizeSkeleton, VariantIdx};
5-
use ty::query::{Providers, queries};
5+
use ty::query::Providers;
66

77
use rustc_target::spec::abi::Abi::RustIntrinsic;
88
use rustc_data_structures::indexed_vec::Idx;
@@ -12,7 +12,7 @@ use hir;
1212

1313
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
1414
for &module in tcx.hir().krate().modules.keys() {
15-
queries::check_mod_intrinsics::ensure(tcx, tcx.hir().local_def_id(module));
15+
tcx.ensure().check_mod_intrinsics(tcx.hir().local_def_id(module));
1616
}
1717
}
1818

src/librustc/middle/liveness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ use self::VarKind::*;
100100
use hir::def::*;
101101
use hir::Node;
102102
use ty::{self, TyCtxt};
103-
use ty::query::{Providers, queries};
103+
use ty::query::Providers;
104104
use lint;
105105
use errors::Applicability;
106106
use util::nodemap::{NodeMap, HirIdMap, HirIdSet};
@@ -187,7 +187,7 @@ fn check_mod_liveness<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
187187

188188
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
189189
for &module in tcx.hir().krate().modules.keys() {
190-
queries::check_mod_liveness::ensure(tcx, tcx.hir().local_def_id(module));
190+
tcx.ensure().check_mod_liveness(tcx.hir().local_def_id(module));
191191
}
192192
tcx.sess.abort_if_errors();
193193
}

src/librustc/middle/stability.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use hir::def::Def;
99
use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
1010
use hir::intravisit::{self, Visitor, NestedVisitorMap};
1111
use ty::query::Providers;
12-
use ty::query::queries;
1312
use middle::privacy::AccessLevels;
1413
use session::{DiagnosticMessageId, Session};
1514
use syntax::symbol::Symbol;
@@ -459,7 +458,7 @@ impl<'a, 'tcx> Index<'tcx> {
459458

460459
pub fn check_unstable_api_usage<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
461460
for &module in tcx.hir().krate().modules.keys() {
462-
queries::check_mod_unstable_api_usage::ensure(tcx, tcx.hir().local_def_id(module));
461+
tcx.ensure().check_mod_unstable_api_usage(tcx.hir().local_def_id(module));
463462
}
464463
}
465464

src/librustc/ty/query/config.rs

+34-22
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(super) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
5151
fn describe(tcx: TyCtxt<'_, '_, '_>, key: Self::Key) -> Cow<'static, str>;
5252

5353
#[inline]
54-
fn cache_on_disk(_: Self::Key) -> bool {
54+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, _: Self::Key) -> bool {
5555
false
5656
}
5757

@@ -136,6 +136,15 @@ impl<'tcx> QueryDescription<'tcx> for queries::check_mod_liveness<'tcx> {
136136
}
137137
}
138138

139+
impl<'tcx> QueryDescription<'tcx> for queries::check_mod_impl_wf<'tcx> {
140+
fn describe(
141+
tcx: TyCtxt<'_, '_, '_>,
142+
key: DefId,
143+
) -> Cow<'static, str> {
144+
format!("checking that impls are well-formed in {}", key.describe_as_module(tcx)).into()
145+
}
146+
}
147+
139148
impl<'tcx> QueryDescription<'tcx> for queries::collect_mod_item_types<'tcx> {
140149
fn describe(
141150
tcx: TyCtxt<'_, '_, '_>,
@@ -378,7 +387,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::const_eval<'tcx> {
378387
}
379388

380389
#[inline]
381-
fn cache_on_disk(_key: Self::Key) -> bool {
390+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, _key: Self::Key) -> bool {
382391
true
383392
}
384393

@@ -398,7 +407,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::const_eval_raw<'tcx> {
398407
}
399408

400409
#[inline]
401-
fn cache_on_disk(_key: Self::Key) -> bool {
410+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, _key: Self::Key) -> bool {
402411
true
403412
}
404413

@@ -422,7 +431,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::symbol_name<'tcx> {
422431
}
423432

424433
#[inline]
425-
fn cache_on_disk(_: Self::Key) -> bool {
434+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, _: Self::Key) -> bool {
426435
true
427436
}
428437

@@ -496,7 +505,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::const_is_rvalue_promotable_to_sta
496505
}
497506

498507
#[inline]
499-
fn cache_on_disk(_: Self::Key) -> bool {
508+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, _: Self::Key) -> bool {
500509
true
501510
}
502511

@@ -530,7 +539,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::codegen_fulfill_obligation<'tcx>
530539
}
531540

532541
#[inline]
533-
fn cache_on_disk(_: Self::Key) -> bool {
542+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, _: Self::Key) -> bool {
534543
true
535544
}
536545

@@ -868,7 +877,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::features_query<'tcx> {
868877

869878
impl<'tcx> QueryDescription<'tcx> for queries::typeck_tables_of<'tcx> {
870879
#[inline]
871-
fn cache_on_disk(def_id: Self::Key) -> bool {
880+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, def_id: Self::Key) -> bool {
872881
def_id.is_local()
873882
}
874883

@@ -885,7 +894,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::typeck_tables_of<'tcx> {
885894

886895
impl<'tcx> QueryDescription<'tcx> for queries::optimized_mir<'tcx> {
887896
#[inline]
888-
fn cache_on_disk(def_id: Self::Key) -> bool {
897+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, def_id: Self::Key) -> bool {
889898
def_id.is_local()
890899
}
891900

@@ -924,7 +933,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::instance_def_size_estimate<'tcx>
924933

925934
impl<'tcx> QueryDescription<'tcx> for queries::generics_of<'tcx> {
926935
#[inline]
927-
fn cache_on_disk(def_id: Self::Key) -> bool {
936+
fn cache_on_disk(_: TyCtxt<'_, 'tcx, 'tcx>, def_id: Self::Key) -> bool {
928937
def_id.is_local()
929938
}
930939

@@ -974,10 +983,10 @@ impl<'tcx> QueryDescription<'tcx> for queries::backend_optimization_level<'tcx>
974983
}
975984

976985
macro_rules! impl_disk_cacheable_query(
977-
($query_name:ident, |$key:tt| $cond:expr) => {
986+
($query_name:ident, |$tcx:tt, $key:tt| $cond:expr) => {
978987
impl<'tcx> QueryDescription<'tcx> for queries::$query_name<'tcx> {
979988
#[inline]
980-
fn cache_on_disk($key: Self::Key) -> bool {
989+
fn cache_on_disk($tcx: TyCtxt<'_, 'tcx, 'tcx>, $key: Self::Key) -> bool {
981990
$cond
982991
}
983992

@@ -991,14 +1000,17 @@ macro_rules! impl_disk_cacheable_query(
9911000
}
9921001
);
9931002

994-
impl_disk_cacheable_query!(unsafety_check_result, |def_id| def_id.is_local());
995-
impl_disk_cacheable_query!(borrowck, |def_id| def_id.is_local());
996-
impl_disk_cacheable_query!(mir_borrowck, |def_id| def_id.is_local());
997-
impl_disk_cacheable_query!(mir_const_qualif, |def_id| def_id.is_local());
998-
impl_disk_cacheable_query!(check_match, |def_id| def_id.is_local());
999-
impl_disk_cacheable_query!(def_symbol_name, |_| true);
1000-
impl_disk_cacheable_query!(type_of, |def_id| def_id.is_local());
1001-
impl_disk_cacheable_query!(predicates_of, |def_id| def_id.is_local());
1002-
impl_disk_cacheable_query!(used_trait_imports, |def_id| def_id.is_local());
1003-
impl_disk_cacheable_query!(codegen_fn_attrs, |_| true);
1004-
impl_disk_cacheable_query!(specialization_graph_of, |_| true);
1003+
impl_disk_cacheable_query!(mir_borrowck, |tcx, def_id| {
1004+
def_id.is_local() && tcx.is_closure(def_id)
1005+
});
1006+
1007+
impl_disk_cacheable_query!(unsafety_check_result, |_, def_id| def_id.is_local());
1008+
impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
1009+
impl_disk_cacheable_query!(mir_const_qualif, |_, def_id| def_id.is_local());
1010+
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
1011+
impl_disk_cacheable_query!(def_symbol_name, |_, _| true);
1012+
impl_disk_cacheable_query!(type_of, |_, def_id| def_id.is_local());
1013+
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
1014+
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
1015+
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);
1016+
impl_disk_cacheable_query!(specialization_graph_of, |_, _| true);

src/librustc/ty/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ define_queries! { <'tcx>
270270

271271
[] fn check_mod_liveness: CheckModLiveness(DefId) -> (),
272272

273+
[] fn check_mod_impl_wf: CheckModImplWf(DefId) -> (),
274+
273275
[] fn collect_mod_item_types: CollectModItemTypes(DefId) -> (),
274276

275277
/// Caches CoerceUnsized kinds for impls on custom types.

src/librustc/ty/query/on_disk_cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'sess> OnDiskCache<'sess> {
230230
assert!(cache.active.is_empty());
231231
for (key, entry) in cache.results.iter() {
232232
use ty::query::config::QueryDescription;
233-
if const_eval::cache_on_disk(key.clone()) {
233+
if const_eval::cache_on_disk(tcx, key.clone()) {
234234
if let Ok(ref value) = entry.value {
235235
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
236236

@@ -1086,7 +1086,7 @@ fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10861086
let map = Q::query_cache(tcx).borrow();
10871087
assert!(map.active.is_empty());
10881088
for (key, entry) in map.results.iter() {
1089-
if Q::cache_on_disk(key.clone()) {
1089+
if Q::cache_on_disk(tcx, key.clone()) {
10901090
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
10911091

10921092
// Record position of the cache entry

src/librustc/ty/query/plumbing.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
434434
debug_assert!(self.dep_graph.is_green(dep_node));
435435

436436
// First we try to load the result from the on-disk cache
437-
let result = if Q::cache_on_disk(key.clone()) &&
437+
let result = if Q::cache_on_disk(self.global_tcx(), key.clone()) &&
438438
self.sess.opts.debugging_opts.incremental_queries {
439439
let result = Q::try_load_from_disk(self.global_tcx(), prev_dep_node_index);
440440

@@ -969,20 +969,20 @@ macro_rules! define_queries_inner {
969969
fn handle_cycle_error(tcx: TyCtxt<'_, 'tcx, '_>) -> Self::Value {
970970
handle_cycle_error!([$($modifiers)*][tcx])
971971
}
972+
})*
973+
974+
#[derive(Copy, Clone)]
975+
pub struct TyCtxtEnsure<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
976+
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
972977
}
973978

974-
impl<'a, $tcx, 'lcx> queries::$name<$tcx> {
975-
/// Ensure that either this query has all green inputs or been executed.
976-
/// Executing query::ensure(D) is considered a read of the dep-node D.
977-
///
978-
/// This function is particularly useful when executing passes for their
979-
/// side-effects -- e.g., in order to report errors for erroneous programs.
980-
///
981-
/// Note: The optimization is only available during incr. comp.
982-
pub fn ensure(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> () {
983-
tcx.ensure_query::<queries::$name<'_>>(key);
984-
}
985-
})*
979+
impl<'a, $tcx, 'lcx> TyCtxtEnsure<'a, $tcx, 'lcx> {
980+
$($(#[$attr])*
981+
#[inline(always)]
982+
pub fn $name(self, key: $K) {
983+
self.tcx.ensure_query::<queries::$name<'_>>(key)
984+
})*
985+
}
986986

987987
#[derive(Copy, Clone)]
988988
pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
@@ -999,6 +999,15 @@ macro_rules! define_queries_inner {
999999
}
10001000

10011001
impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
1002+
/// Return a transparent wrapper for `TyCtxt` which ensures queries
1003+
/// are executed instead of returing their result
1004+
#[inline(always)]
1005+
pub fn ensure(self) -> TyCtxtEnsure<'a, $tcx, 'lcx> {
1006+
TyCtxtEnsure {
1007+
tcx: self,
1008+
}
1009+
}
1010+
10021011
/// Return a transparent wrapper for `TyCtxt` which uses
10031012
/// `span` as the location of queries performed through it.
10041013
#[inline(always)]
@@ -1251,6 +1260,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
12511260
DepKind::CheckModPrivacy => { force!(check_mod_privacy, def_id!()); }
12521261
DepKind::CheckModIntrinsics => { force!(check_mod_intrinsics, def_id!()); }
12531262
DepKind::CheckModLiveness => { force!(check_mod_liveness, def_id!()); }
1263+
DepKind::CheckModImplWf => { force!(check_mod_impl_wf, def_id!()); }
12541264
DepKind::CollectModItemTypes => { force!(collect_mod_item_types, def_id!()); }
12551265
DepKind::Reachability => { force!(reachable_set, LOCAL_CRATE); }
12561266
DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
@@ -1433,7 +1443,7 @@ macro_rules! impl_load_from_cache {
14331443
match self.kind {
14341444
$(DepKind::$dep_kind => {
14351445
let def_id = self.extract_def_id(tcx).unwrap();
1436-
queries::$query_name::cache_on_disk(def_id)
1446+
queries::$query_name::cache_on_disk(tcx.global_tcx(), def_id)
14371447
})*
14381448
_ => false
14391449
}

src/librustc/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
402402
return None;
403403
};
404404

405-
ty::query::queries::coherent_trait::ensure(self, drop_trait);
405+
self.ensure().coherent_trait(drop_trait);
406406

407407
let mut dtor_did = None;
408408
let ty = self.type_of(adt_did);

src/librustc_borrowck/borrowck/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub type LoanDataFlow<'a, 'tcx> = DataFlowContext<'a, 'tcx, LoanDataFlowOperator
5757

5858
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
5959
tcx.par_body_owners(|body_owner_def_id| {
60-
tcx.borrowck(body_owner_def_id);
60+
tcx.ensure().borrowck(body_owner_def_id);
6161
});
6262
}
6363

@@ -121,7 +121,7 @@ fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId)
121121
// Note that `mir_validated` is a "stealable" result; the
122122
// thief, `optimized_mir()`, forces borrowck, so we know that
123123
// is not yet stolen.
124-
ty::query::queries::mir_validated::ensure(tcx, owner_def_id);
124+
tcx.ensure().mir_validated(owner_def_id);
125125

126126
// option dance because you can't capture an uninitialized variable
127127
// by mut-ref.

src/librustc_driver/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ where
12831283

12841284
time(sess,
12851285
"MIR borrow checking",
1286-
|| tcx.par_body_owners(|def_id| { tcx.mir_borrowck(def_id); }));
1286+
|| tcx.par_body_owners(|def_id| { tcx.ensure().mir_borrowck(def_id); }));
12871287

12881288
time(sess, "dumping chalk-like clauses", || {
12891289
rustc_traits::lowering::dump_program_clauses(tcx);

src/librustc_mir/hair/pattern/check_match.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,10 @@ use syntax::ast;
2828
use syntax::ptr::P;
2929
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
3030

31-
struct OuterVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx> }
32-
33-
impl<'a, 'tcx> Visitor<'tcx> for OuterVisitor<'a, 'tcx> {
34-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
35-
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
36-
}
37-
38-
fn visit_body(&mut self, body: &'tcx hir::Body) {
39-
intravisit::walk_body(self, body);
40-
let def_id = self.tcx.hir().body_owner_def_id(body.id());
41-
let _ = self.tcx.check_match(def_id);
42-
}
43-
}
44-
4531
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
46-
tcx.hir().krate().visit_all_item_likes(&mut OuterVisitor { tcx }.as_deep_visitor());
32+
for def_id in tcx.body_owners() {
33+
tcx.ensure().check_match(def_id);
34+
}
4735
tcx.sess.abort_if_errors();
4836
}
4937

src/librustc_mir/transform/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
228228
fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
229229
// (Mir-)Borrowck uses `mir_validated`, so we have to force it to
230230
// execute before we can steal.
231-
let _ = tcx.mir_borrowck(def_id);
231+
tcx.ensure().mir_borrowck(def_id);
232232

233233
if tcx.use_ast_borrowck() {
234-
let _ = tcx.borrowck(def_id);
234+
tcx.ensure().borrowck(def_id);
235235
}
236236

237237
let mut mir = tcx.mir_validated(def_id).steal();

0 commit comments

Comments
 (0)