From 3ecde6f5db72c0a99824ec91f57411442026141d Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 8 Mar 2021 15:01:53 +0000 Subject: [PATCH] Directly intern allocations so that we can write a `Lift` impl for them --- compiler/rustc_middle/src/ty/context.rs | 35 ++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index e1d79248171a..7fbd75ca165d 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -94,6 +94,8 @@ pub struct CtxtInterners<'tcx> { projs: InternedSet<'tcx, List>, place_elems: InternedSet<'tcx, List>>, const_: InternedSet<'tcx, Const<'tcx>>, + /// Const allocations. + allocation: InternedSet<'tcx, Allocation>, } impl<'tcx> CtxtInterners<'tcx> { @@ -111,6 +113,7 @@ impl<'tcx> CtxtInterners<'tcx> { projs: Default::default(), place_elems: Default::default(), const_: Default::default(), + allocation: Default::default(), } } @@ -1013,9 +1016,6 @@ pub struct GlobalCtxt<'tcx> { /// `#[rustc_const_stable]` and `#[rustc_const_unstable]` attributes const_stability_interner: ShardedHashMap<&'tcx attr::ConstStability, ()>, - /// Stores the value of constants (and deduplicates the actual memory) - allocation_interner: ShardedHashMap<&'tcx Allocation, ()>, - /// Stores memory for globals (statics/consts). pub(crate) alloc_map: Lock>, @@ -1058,7 +1058,10 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn intern_const_alloc(self, alloc: Allocation) -> &'tcx Allocation { - self.allocation_interner.intern(alloc, |alloc| self.arena.alloc(alloc)) + self.interners + .allocation + .intern(alloc, |alloc| Interned(self.interners.arena.alloc(alloc))) + .0 } /// Allocates a read-only byte or string literal for `mir::interpret`. @@ -1174,7 +1177,6 @@ impl<'tcx> TyCtxt<'tcx> { layout_interner: Default::default(), stability_interner: Default::default(), const_stability_interner: Default::default(), - allocation_interner: Default::default(), alloc_map: Lock::new(interpret::AllocMap::new()), output_filenames: Arc::new(output_filenames.clone()), } @@ -1610,6 +1612,7 @@ macro_rules! nop_list_lift { nop_lift! {type_; Ty<'a> => Ty<'tcx>} nop_lift! {region; Region<'a> => Region<'tcx>} nop_lift! {const_; &'a Const<'a> => &'tcx Const<'tcx>} +nop_lift! {allocation; &'a Allocation => &'tcx Allocation} nop_lift! {predicate; &'a PredicateInner<'a> => &'tcx PredicateInner<'tcx>} nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>} @@ -1900,7 +1903,7 @@ impl<'tcx> TyCtxt<'tcx> { "Const Stability interner: #{}", self.0.const_stability_interner.len() )?; - writeln!(fmt, "Allocation interner: #{}", self.0.allocation_interner.len())?; + writeln!(fmt, "Allocation interner: #{}", self.0.interners.allocation.len())?; writeln!(fmt, "Layout interner: #{}", self.0.layout_interner.len())?; Ok(()) @@ -2001,6 +2004,26 @@ impl<'tcx> Borrow> for Interned<'tcx, Const<'tcx>> { } } +impl<'tcx> Borrow for Interned<'tcx, Allocation> { + fn borrow<'a>(&'a self) -> &'a Allocation { + &self.0 + } +} + +impl<'tcx> PartialEq for Interned<'tcx, Allocation> { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +impl<'tcx> Eq for Interned<'tcx, Allocation> {} + +impl<'tcx> Hash for Interned<'tcx, Allocation> { + fn hash(&self, s: &mut H) { + self.0.hash(s) + } +} + macro_rules! direct_interners { ($($name:ident: $method:ident($ty:ty),)+) => { $(impl<'tcx> PartialEq for Interned<'tcx, $ty> {