Skip to content

Commit 66a689c

Browse files
committed
address review
1 parent 78e4b6f commit 66a689c

File tree

2 files changed

+76
-150
lines changed

2 files changed

+76
-150
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,6 @@ impl<'mir, 'tcx, Tag: Provenance, Extra> Frame<'mir, 'tcx, Tag, Extra> {
266266
}
267267
}
268268

269-
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for InterpCx<'mir, 'tcx, M> {
270-
#[inline]
271-
fn data_layout(&self) -> &TargetDataLayout {
272-
&self.tcx.data_layout
273-
}
274-
}
275-
276269
impl<'tcx> fmt::Display for FrameInfo<'tcx> {
277270
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
278271
ty::tls::with(|tcx| {
@@ -299,6 +292,13 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
299292
}
300293
}
301294

295+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for InterpCx<'mir, 'tcx, M> {
296+
#[inline]
297+
fn data_layout(&self) -> &TargetDataLayout {
298+
&self.tcx.data_layout
299+
}
300+
}
301+
302302
impl<'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'mir, 'tcx, M>
303303
where
304304
M: Machine<'mir, 'tcx>,

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 69 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,11 @@ use rustc_ast::LitKind;
108108
use rustc_data_structures::fx::FxHashMap;
109109
use rustc_data_structures::sync::{HashMapExt, Lock};
110110
use rustc_data_structures::tiny_list::TinyList;
111-
use rustc_hir as hir;
112111
use rustc_hir::def_id::DefId;
113-
use rustc_hir::definitions::DefPathData;
114112
use rustc_middle::traits::Reveal;
115113
use rustc_middle::ty::print::with_no_trimmed_paths;
116114
use rustc_serialize::{Decodable, Encodable};
117-
use rustc_span::{Pos, Span};
115+
use rustc_span::Span;
118116
use rustc_target::abi::Endian;
119117

120118
use crate::mir;
@@ -451,40 +449,6 @@ impl<'tcx> AllocMap<'tcx> {
451449
}
452450
}
453451

454-
/// What we store about a frame in an interpreter backtrace.
455-
#[derive(Debug)]
456-
pub struct FrameInfo<'tcx> {
457-
pub instance: ty::Instance<'tcx>,
458-
pub span: Span,
459-
pub lint_root: Option<hir::HirId>,
460-
}
461-
462-
impl<'tcx> fmt::Display for FrameInfo<'tcx> {
463-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
464-
ty::tls::with(|tcx| {
465-
if tcx.def_key(self.instance.def_id()).disambiguated_data.data
466-
== DefPathData::ClosureExpr
467-
{
468-
write!(f, "inside closure")?;
469-
} else {
470-
write!(f, "inside `{}`", self.instance)?;
471-
}
472-
if !self.span.is_dummy() {
473-
let sm = tcx.sess.source_map();
474-
let lo = sm.lookup_char_pos(self.span.lo());
475-
write!(
476-
f,
477-
" at {}:{}:{}",
478-
sm.filename_for_diagnostics(&lo.file.name),
479-
lo.line,
480-
lo.col.to_usize() + 1
481-
)?;
482-
}
483-
Ok(())
484-
})
485-
}
486-
}
487-
488452
/// Used to store results of calls to `eval_to_allocation_raw` and
489453
/// `eval_to_const_value_raw`.
490454
#[derive(Debug)]
@@ -650,88 +614,13 @@ impl<'tcx> TyCtxt<'tcx> {
650614
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
651615
opt_span: Option<Span>,
652616
) -> EvalToAllocationRawResult<'tcx> {
653-
let (param_env, id) = key.into_parts();
654-
let dedup_const_map = self.dedup_const_map.lock();
655-
debug!("dedup_const_map: {:#?}", dedup_const_map);
656-
let alloc_map = dedup_const_map.alloc_map.borrow();
657-
debug!("alloc_map: {:#?}", alloc_map);
658-
659-
let dedup_reveal = alloc_map.get(&id);
660-
debug!(?dedup_reveal);
661-
662-
match param_env.reveal() {
663-
Reveal::Selection => match dedup_reveal {
664-
Some(Reveal::Selection) => {
665-
drop(alloc_map);
666-
drop(dedup_const_map);
667-
668-
// Use cached result of query
669-
return self.eval_to_allocation_raw(key);
670-
}
671-
Some(Reveal::UserFacing) => {
672-
drop(alloc_map);
673-
drop(dedup_const_map);
674-
675-
// can deduplicate query with Reveal::Selection from Reveal::UserFacing
676-
// since these only differ in the way errors are reported, but successful
677-
// query calls are equivalent.
678-
let new_key = param_env.with_user_facing().and(id);
679-
return self.eval_to_allocation_raw(new_key);
680-
}
681-
_ => {}
682-
},
683-
Reveal::UserFacing => match dedup_reveal {
684-
Some(Reveal::UserFacing) => {
685-
drop(alloc_map);
686-
drop(dedup_const_map);
687-
688-
return self.eval_to_allocation_raw(key);
689-
}
690-
Some(Reveal::Selection) => {
691-
drop(alloc_map);
692-
drop(dedup_const_map);
693-
694-
let new_key = param_env.with_reveal_selection().and(id);
695-
return self.eval_to_allocation_raw(new_key);
696-
}
697-
_ => {}
698-
},
699-
Reveal::All => match dedup_reveal {
700-
Some(Reveal::Selection) => {
701-
drop(alloc_map);
702-
drop(dedup_const_map);
703-
704-
let new_key = param_env.with_reveal_selection().and(id);
705-
return self.eval_to_allocation_raw(new_key);
706-
}
707-
Some(Reveal::UserFacing) => {
708-
drop(alloc_map);
709-
drop(dedup_const_map);
710-
711-
let new_key = param_env.with_user_facing().and(id);
712-
return self.eval_to_allocation_raw(new_key);
713-
}
714-
Some(Reveal::All) => {
715-
drop(alloc_map);
716-
drop(dedup_const_map);
717-
718-
return self.eval_to_allocation_raw(key);
719-
}
720-
_ => {}
721-
},
722-
}
723-
724-
// Important to drop the lock here
725-
drop(alloc_map);
726-
drop(dedup_const_map);
727-
728-
debug!("unable to deduplicate");
729-
730-
// We weren't able to deduplicate
731-
match opt_span {
732-
Some(span) => self.at(span).eval_to_allocation_raw(key),
733-
None => self.eval_to_allocation_raw(key),
734-
}
617+
self.dedup_eval(
618+
key,
619+
opt_span,
620+
EvalCtxt::Alloc,
621+
|key| self.eval_to_allocation_raw(key),
622+
|key, span| self.at(span).eval_to_allocation_raw(key),
623+
)
735624
}
736625

737626
/// Tries to deduplicate a call to `eval_to_const_value_raw`. If deduplication isn't
@@ -742,91 +631,128 @@ impl<'tcx> TyCtxt<'tcx> {
742631
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
743632
opt_span: Option<Span>,
744633
) -> EvalToConstValueResult<'tcx> {
634+
self.dedup_eval(
635+
key,
636+
opt_span,
637+
EvalCtxt::ConstVal,
638+
|key| self.eval_to_const_value_raw(key),
639+
|key, span| self.at(span).eval_to_const_value_raw(key),
640+
)
641+
}
642+
643+
/// Deduplicates the result of a query call.
644+
///
645+
/// Explanation for why we can deduplicate query calls in certain situations:
646+
///
647+
/// If we have stored a successful query result with `Reveal::Selection` then we can
648+
/// deduplicate that result on calls of the `dedup` version of the query with both
649+
/// `Reveal::Selection` and `Reveal::UserFacing` (since `Selection` and `UserFacing`
650+
/// only differ in that we keep certain errors silent with `Selection`, but successful
651+
/// allocations are always equivalent). This is also the reason why we can deduplicate
652+
/// from saved calls with `UserFacing`on calls with both `Selection` and `UserFacing`.
653+
///
654+
/// If we try to deduplicate with `Reveal::All` we are able to deduplicate
655+
/// from saved query results with both `Selection` and `UserFacing`, since `All`
656+
/// will always succeed if either `UserFacing` or `Selection` have succeeded.
657+
/// On the other hand if we have a saved result with `Reveal::All`, it's not
658+
/// guaranteed that query calls with `Reveal::Selection` or `Reveal::UserFacing`
659+
/// succeed, so we can't deduplicate from those.
660+
fn dedup_eval<T: Copy>(
661+
self,
662+
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
663+
opt_span: Option<Span>,
664+
eval_ctxt: EvalCtxt,
665+
query_call: impl Fn(ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>) -> T,
666+
query_call_span: impl Fn(ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>, Span) -> T,
667+
) -> T {
745668
let (param_env, id) = key.into_parts();
746669
let dedup_const_map = self.dedup_const_map.lock();
747-
debug!("dedup_const_map: {:#?}", dedup_const_map);
748-
let const_val_map = dedup_const_map.const_val_map.borrow();
749-
debug!("const_val_map: {:#?}", const_val_map);
670+
let dedup_query_map = match eval_ctxt {
671+
EvalCtxt::Alloc => dedup_const_map.alloc_map.borrow(),
672+
EvalCtxt::ConstVal => dedup_const_map.const_val_map.borrow(),
673+
};
750674

751-
let dedup_reveal = const_val_map.get(&id);
675+
let dedup_reveal = dedup_query_map.get(&id);
752676
debug!(?dedup_reveal);
753677

754678
match param_env.reveal() {
755679
Reveal::Selection => match dedup_reveal {
756680
Some(Reveal::Selection) => {
757-
drop(const_val_map);
681+
drop(dedup_query_map);
758682
drop(dedup_const_map);
759683

760684
// Use cached result of query
761-
return self.eval_to_const_value_raw(key);
685+
return query_call(key);
762686
}
763687
Some(Reveal::UserFacing) => {
764-
drop(const_val_map);
688+
drop(dedup_query_map);
765689
drop(dedup_const_map);
766690

767-
// can deduplicate query with Reveal::Selection from Reveal::UserFacing
768-
// since these only differ in the way errors are reported, but successful
769-
// query calls are equivalent.
770691
let new_key = param_env.with_user_facing().and(id);
771-
return self.eval_to_const_value_raw(new_key);
692+
return query_call(new_key);
772693
}
773694
_ => {}
774695
},
775696
Reveal::UserFacing => match dedup_reveal {
776697
Some(Reveal::UserFacing) => {
777-
drop(const_val_map);
698+
drop(dedup_query_map);
778699
drop(dedup_const_map);
779700

780-
return self.eval_to_const_value_raw(key);
701+
return query_call(key);
781702
}
782703
Some(Reveal::Selection) => {
783-
drop(const_val_map);
704+
drop(dedup_query_map);
784705
drop(dedup_const_map);
785706

786707
let new_key = param_env.with_reveal_selection().and(id);
787-
return self.eval_to_const_value_raw(new_key);
708+
return query_call(new_key);
788709
}
789710
_ => {}
790711
},
791712
Reveal::All => match dedup_reveal {
792713
Some(Reveal::Selection) => {
793-
drop(const_val_map);
714+
drop(dedup_query_map);
794715
drop(dedup_const_map);
795716

796717
let new_key = param_env.with_reveal_selection().and(id);
797-
return self.eval_to_const_value_raw(new_key);
718+
return query_call(new_key);
798719
}
799720
Some(Reveal::UserFacing) => {
800-
drop(const_val_map);
721+
drop(dedup_query_map);
801722
drop(dedup_const_map);
802723

803724
let new_key = param_env.with_user_facing().and(id);
804-
return self.eval_to_const_value_raw(new_key);
725+
return query_call(new_key);
805726
}
806727
Some(Reveal::All) => {
807-
drop(const_val_map);
728+
drop(dedup_query_map);
808729
drop(dedup_const_map);
809730

810-
return self.eval_to_const_value_raw(key);
731+
return query_call(key);
811732
}
812733
_ => {}
813734
},
814735
}
815736

816737
// Important to drop the lock here
817-
drop(const_val_map);
738+
drop(dedup_query_map);
818739
drop(dedup_const_map);
819740

820741
debug!("unable to deduplicate");
821742

822743
// We weren't able to deduplicate
823744
match opt_span {
824-
Some(span) => self.at(span).eval_to_const_value_raw(key),
825-
None => self.eval_to_const_value_raw(key),
745+
Some(span) => query_call_span(key, span),
746+
None => query_call(key),
826747
}
827748
}
828749
}
829750

751+
enum EvalCtxt {
752+
Alloc,
753+
ConstVal,
754+
}
755+
830756
////////////////////////////////////////////////////////////////////////////////
831757
// Methods to access integers in the target endianness
832758
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)