Skip to content

Commit b8523fd

Browse files
committed
Erase regions before before performing const eval, to improve caching.
1 parent f77f07c commit b8523fd

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/librustc/mir/interpret/queries.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ impl<'tcx> TyCtxt<'tcx> {
1313
pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> {
1414
// In some situations def_id will have substitutions within scope, but they aren't allowed
1515
// to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions
16-
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any og them are
16+
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are
1717
// encountered.
1818
let substs = InternalSubsts::identity_for_item(self, def_id);
1919
let instance = ty::Instance::new(def_id, substs);
2020
let cid = GlobalId { instance, promoted: None };
2121
let param_env = self.param_env(def_id).with_reveal_all();
22-
self.const_eval_validated(param_env.and(cid))
22+
self.const_eval_global_id(param_env, cid, None)
2323
}
2424

2525
/// Resolves and evaluates a constant.
@@ -41,11 +41,8 @@ impl<'tcx> TyCtxt<'tcx> {
4141
) -> ConstEvalResult<'tcx> {
4242
let instance = ty::Instance::resolve(self, param_env, def_id, substs);
4343
if let Some(instance) = instance {
44-
if let Some(promoted) = promoted {
45-
self.const_eval_promoted(param_env, instance, promoted)
46-
} else {
47-
self.const_eval_instance(param_env, instance, span)
48-
}
44+
let cid = GlobalId { instance, promoted };
45+
self.const_eval_global_id(param_env, cid, span)
4946
} else {
5047
Err(ErrorHandled::TooGeneric)
5148
}
@@ -57,22 +54,23 @@ impl<'tcx> TyCtxt<'tcx> {
5754
instance: ty::Instance<'tcx>,
5855
span: Option<Span>,
5956
) -> ConstEvalResult<'tcx> {
60-
let cid = GlobalId { instance, promoted: None };
61-
if let Some(span) = span {
62-
self.at(span).const_eval_validated(param_env.and(cid))
63-
} else {
64-
self.const_eval_validated(param_env.and(cid))
65-
}
57+
self.const_eval_global_id(param_env, GlobalId { instance, promoted: None }, span)
6658
}
6759

68-
/// Evaluate a promoted constant.
69-
pub fn const_eval_promoted(
60+
/// Evaluate a constant.
61+
pub fn const_eval_global_id(
7062
self,
7163
param_env: ty::ParamEnv<'tcx>,
72-
instance: ty::Instance<'tcx>,
73-
promoted: mir::Promoted,
64+
cid: GlobalId<'tcx>,
65+
span: Option<Span>,
7466
) -> ConstEvalResult<'tcx> {
75-
let cid = GlobalId { instance, promoted: Some(promoted) };
76-
self.const_eval_validated(param_env.and(cid))
67+
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
68+
// improve caching of queries.
69+
let inputs = self.erase_regions(&param_env.and(cid));
70+
if let Some(span) = span {
71+
self.at(span).const_eval_validated(inputs)
72+
} else {
73+
self.const_eval_validated(inputs)
74+
}
7775
}
7876
}

src/librustc/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ rustc_queries! {
502502
/// returns a proper constant that is usable by the rest of the compiler.
503503
///
504504
/// **Do not use this** directly, use one of the following wrappers: `tcx.const_eval_poly`,
505-
/// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_promoted`.
505+
/// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_global_id`.
506506
query const_eval_validated(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
507507
-> ConstEvalResult<'tcx> {
508508
no_force

src/librustc_mir/interpret/eval_context.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -768,11 +768,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
768768
} else {
769769
self.param_env
770770
};
771-
let val = if let Some(promoted) = gid.promoted {
772-
self.tcx.const_eval_promoted(param_env, gid.instance, promoted)?
773-
} else {
774-
self.tcx.const_eval_instance(param_env, gid.instance, Some(self.tcx.span))?
775-
};
771+
let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;
776772

777773
// Even though `ecx.const_eval` is called from `eval_const_to_op` we can never have a
778774
// recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not

0 commit comments

Comments
 (0)