Skip to content

Commit 0d4be3f

Browse files
committed
Auto merge of #57916 - Zoxc:incr-passes4, r=<try>
Misc performance tweaks r? @michaelwoerister
2 parents 01af120 + d3ddf1d commit 0d4be3f

File tree

23 files changed

+176
-120
lines changed

23 files changed

+176
-120
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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;
@@ -458,7 +457,7 @@ impl<'a, 'tcx> Index<'tcx> {
458457

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

src/librustc/ty/query/config.rs

Lines changed: 33 additions & 22 deletions
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,16 @@ 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!(borrowck, |_, def_id| def_id.is_local());
1008+
impl_disk_cacheable_query!(mir_const_qualif, |_, def_id| def_id.is_local());
1009+
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
1010+
impl_disk_cacheable_query!(def_symbol_name, |_, _| true);
1011+
impl_disk_cacheable_query!(type_of, |_, def_id| def_id.is_local());
1012+
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
1013+
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
1014+
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);
1015+
impl_disk_cacheable_query!(specialization_graph_of, |_, _| true);

src/librustc/ty/query/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ define_queries! { <'tcx>
244244

245245
TypeChecking {
246246
/// The result of unsafety-checking this def-id.
247-
[] fn unsafety_check_result: UnsafetyCheckResult(DefId) -> mir::UnsafetyCheckResult,
247+
[] fn unsafety_check_result: UnsafetyCheckResult(DefId) -> (),
248248

249249
/// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error
250250
[] fn unsafe_derive_on_repr_packed: UnsafeDeriveOnReprPacked(DefId) -> (),
@@ -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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ impl<'sess> OnDiskCache<'sess> {
213213
encode_query_results::<typeck_tables_of<'_>, _>(tcx, enc, qri)?;
214214
encode_query_results::<codegen_fulfill_obligation<'_>, _>(tcx, enc, qri)?;
215215
encode_query_results::<optimized_mir<'_>, _>(tcx, enc, qri)?;
216-
encode_query_results::<unsafety_check_result<'_>, _>(tcx, enc, qri)?;
217216
encode_query_results::<borrowck<'_>, _>(tcx, enc, qri)?;
218217
encode_query_results::<mir_borrowck<'_>, _>(tcx, enc, qri)?;
219218
encode_query_results::<mir_const_qualif<'_>, _>(tcx, enc, qri)?;
@@ -230,7 +229,7 @@ impl<'sess> OnDiskCache<'sess> {
230229
assert!(cache.active.is_empty());
231230
for (key, entry) in cache.results.iter() {
232231
use ty::query::config::QueryDescription;
233-
if const_eval::cache_on_disk(key.clone()) {
232+
if const_eval::cache_on_disk(tcx, key.clone()) {
234233
if let Ok(ref value) = entry.value {
235234
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
236235

@@ -1086,7 +1085,7 @@ fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10861085
let map = Q::query_cache(tcx).borrow();
10871086
assert!(map.active.is_empty());
10881087
for (key, entry) in map.results.iter() {
1089-
if Q::cache_on_disk(key.clone()) {
1088+
if Q::cache_on_disk(tcx, key.clone()) {
10901089
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
10911090

10921091
// Record position of the cache entry

src/librustc/ty/query/plumbing.rs

Lines changed: 44 additions & 11 deletions
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

@@ -606,6 +606,23 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
606606
}
607607
}
608608

609+
/// Check that a query has already executed
610+
pub(super) fn assert_done<Q: QueryDescription<'gcx>>(self, key: Q::Key) -> () {
611+
let dep_node = Q::to_dep_node(self, &key);
612+
613+
if self.dep_graph.try_mark_green(self, &dep_node).is_some() {
614+
// It is already green
615+
return;
616+
}
617+
618+
match JobOwner::<'_, 'gcx, Q>::try_get(self, DUMMY_SP, &key) {
619+
TryGetJob::NotYetStarted(_) => {
620+
bug!("query should already be done {}, key={:?}", Q::NAME, key);
621+
},
622+
TryGetJob::JobCompleted(..) => {}
623+
}
624+
}
625+
609626
#[allow(dead_code)]
610627
fn force_query<Q: QueryDescription<'gcx>>(
611628
self,
@@ -972,18 +989,24 @@ macro_rules! define_queries_inner {
972989
}
973990

974991
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);
992+
pub fn assert_done(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> () {
993+
tcx.assert_done::<queries::$name<'_>>(key);
984994
}
985995
})*
986996

997+
#[derive(Copy, Clone)]
998+
pub struct TyCtxtEnsure<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
999+
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
1000+
}
1001+
1002+
impl<'a, $tcx, 'lcx> TyCtxtEnsure<'a, $tcx, 'lcx> {
1003+
$($(#[$attr])*
1004+
#[inline(always)]
1005+
pub fn $name(self, key: $K) {
1006+
self.tcx.ensure_query::<queries::$name<'_>>(key)
1007+
})*
1008+
}
1009+
9871010
#[derive(Copy, Clone)]
9881011
pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
9891012
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
@@ -999,6 +1022,15 @@ macro_rules! define_queries_inner {
9991022
}
10001023

10011024
impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
1025+
/// Return a transparent wrapper for `TyCtxt` which ensures queries
1026+
/// are executed instead of returing their result
1027+
#[inline(always)]
1028+
pub fn ensure(self) -> TyCtxtEnsure<'a, $tcx, 'lcx> {
1029+
TyCtxtEnsure {
1030+
tcx: self,
1031+
}
1032+
}
1033+
10021034
/// Return a transparent wrapper for `TyCtxt` which uses
10031035
/// `span` as the location of queries performed through it.
10041036
#[inline(always)]
@@ -1251,6 +1283,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
12511283
DepKind::CheckModPrivacy => { force!(check_mod_privacy, def_id!()); }
12521284
DepKind::CheckModIntrinsics => { force!(check_mod_intrinsics, def_id!()); }
12531285
DepKind::CheckModLiveness => { force!(check_mod_liveness, def_id!()); }
1286+
DepKind::CheckModImplWf => { force!(check_mod_impl_wf, def_id!()); }
12541287
DepKind::CollectModItemTypes => { force!(collect_mod_item_types, def_id!()); }
12551288
DepKind::Reachability => { force!(reachable_set, LOCAL_CRATE); }
12561289
DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
@@ -1433,7 +1466,7 @@ macro_rules! impl_load_from_cache {
14331466
match self.kind {
14341467
$(DepKind::$dep_kind => {
14351468
let def_id = self.extract_def_id(tcx).unwrap();
1436-
queries::$query_name::cache_on_disk(def_id)
1469+
queries::$query_name::cache_on_disk(tcx.global_tcx(), def_id)
14371470
})*
14381471
_ => false
14391472
}

src/librustc/ty/util.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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.

0 commit comments

Comments
 (0)