Skip to content

Commit 829154f

Browse files
authored
Rollup merge of #67797 - Aaron1011:feature/instance-query, r=nikomatsakis
Query-ify Instance::resolve Split off from #65989 Instance::resolve is now a wrapper for a new `resolve_instance` query. This greatly improves performance on several benchmarks
2 parents e6cef04 + 63d6ef6 commit 829154f

File tree

7 files changed

+24
-25
lines changed

7 files changed

+24
-25
lines changed

src/librustc_interface/callbacks.rs

-1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,4 @@ pub fn setup_callbacks() {
5858
rustc_span::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
5959
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
6060
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_)));
61-
rustc_middle::ty::RESOLVE_INSTANCE.swap(&(rustc_ty::instance::resolve_instance as _));
6261
}

src/librustc_middle/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1257,5 +1257,9 @@ rustc_queries! {
12571257
eval_always
12581258
desc { "looking up enabled feature gates" }
12591259
}
1260+
1261+
query resolve_instance(key: (ty::ParamEnv<'tcx>, DefId, SubstsRef<'tcx>)) -> Option<ty::Instance<'tcx>> {
1262+
desc { "resolving instance `{:?}` `{:?}` with {:?}", key.1, key.2, key.0 }
1263+
}
12601264
}
12611265
}

src/librustc_middle/ty/instance.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
22
use crate::ty::print::{FmtPrinter, Printer};
33
use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable};
4-
use rustc_data_structures::AtomicRef;
54
use rustc_hir::def::Namespace;
65
use rustc_hir::def_id::{CrateNum, DefId};
76
use rustc_hir::lang_items::DropInPlaceFnLangItem;
@@ -289,7 +288,9 @@ impl<'tcx> Instance<'tcx> {
289288
def_id: DefId,
290289
substs: SubstsRef<'tcx>,
291290
) -> Option<Instance<'tcx>> {
292-
(*RESOLVE_INSTANCE)(tcx, param_env, def_id, substs)
291+
// All regions in the result of this query are erased, so it's
292+
// fine to erase all of the input regions.
293+
tcx.resolve_instance((tcx.erase_regions(&param_env), def_id, tcx.erase_regions(&substs)))
293294
}
294295

295296
pub fn resolve_for_fn_ptr(
@@ -440,21 +441,3 @@ fn needs_fn_once_adapter_shim(
440441
(ty::ClosureKind::FnMut, _) | (ty::ClosureKind::FnOnce, _) => Err(()),
441442
}
442443
}
443-
444-
fn resolve_instance_default(
445-
_tcx: TyCtxt<'tcx>,
446-
_param_env: ty::ParamEnv<'tcx>,
447-
_def_id: DefId,
448-
_substs: SubstsRef<'tcx>,
449-
) -> Option<Instance<'tcx>> {
450-
unimplemented!()
451-
}
452-
453-
pub static RESOLVE_INSTANCE: AtomicRef<
454-
for<'tcx> fn(
455-
TyCtxt<'tcx>,
456-
ty::ParamEnv<'tcx>,
457-
DefId,
458-
SubstsRef<'tcx>,
459-
) -> Option<Instance<'tcx>>,
460-
> = AtomicRef::new(&(resolve_instance_default as _));

src/librustc_middle/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub use self::context::{
8181
CtxtInterners, GeneratorInteriorTypeCause, GlobalCtxt, Lift, TypeckTables,
8282
};
8383

84-
pub use self::instance::RESOLVE_INSTANCE;
8584
pub use self::instance::{Instance, InstanceDef};
8685

8786
pub use self::trait_def::TraitDef;

src/librustc_middle/ty/query/keys.rs

+11
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,14 @@ impl Key for (Symbol, u32, u32) {
296296
DUMMY_SP
297297
}
298298
}
299+
300+
impl<'tcx> Key for (ty::ParamEnv<'tcx>, DefId, SubstsRef<'tcx>) {
301+
type CacheSelector = DefaultCacheSelector;
302+
303+
fn query_crate(&self) -> CrateNum {
304+
self.1.krate
305+
}
306+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
307+
tcx.def_span(self.1)
308+
}
309+
}

src/librustc_ty/instance.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use log::debug;
1111

1212
pub fn resolve_instance<'tcx>(
1313
tcx: TyCtxt<'tcx>,
14-
param_env: ty::ParamEnv<'tcx>,
15-
def_id: DefId,
16-
substs: SubstsRef<'tcx>,
14+
(param_env, def_id, substs): (ty::ParamEnv<'tcx>, DefId, SubstsRef<'tcx>),
1715
) -> Option<Instance<'tcx>> {
1816
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
1917
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
@@ -199,3 +197,7 @@ fn resolve_associated_item<'tcx>(
199197
traits::VtableAutoImpl(..) | traits::VtableParam(..) | traits::VtableTraitAlias(..) => None,
200198
}
201199
}
200+
201+
pub fn provide(providers: &mut ty::query::Providers<'_>) {
202+
*providers = ty::query::Providers { resolve_instance, ..*providers };
203+
}

src/librustc_ty/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ pub fn provide(providers: &mut Providers<'_>) {
2525
common_traits::provide(providers);
2626
needs_drop::provide(providers);
2727
ty::provide(providers);
28+
instance::provide(providers);
2829
}

0 commit comments

Comments
 (0)