Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce TyInterner #83142

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4546,6 +4546,7 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_serialize",
"smallvec 1.6.1",
]

[[package]]
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_arena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,10 +733,10 @@ macro_rules! declare_arena {
self.dropless.alloc_slice(value)
}

pub fn alloc_from_iter<'a, T: ArenaAllocatable<'tcx, U>, U>(
&'a self,
iter: impl ::std::iter::IntoIterator<Item = T>,
) -> &'a mut [T] {
pub fn alloc_from_iter<T: ArenaAllocatable<'tcx, U>, U, V: IntoIterator<Item = T>>(
&self,
iter: V,
) -> &mut [T] {
T::allocate_from_iter(self, iter)
}
}
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,10 @@ impl MetaItemKind {
fn token_trees_and_spacings(&self, span: Span) -> Vec<TreeAndSpacing> {
match *self {
MetaItemKind::Word => vec![],
MetaItemKind::NameValue(ref lit) => {
vec![
TokenTree::token(token::Eq, span).into(),
TokenTree::Token(lit.to_token()).into(),
]
}
MetaItemKind::NameValue(ref lit) => vec![
TokenTree::token(token::Eq, span).into(),
TokenTree::Token(lit.to_token()).into(),
],
MetaItemKind::List(ref list) => {
let mut tokens = Vec::new();
for (i, item) in list.iter().enumerate() {
Expand Down
17 changes: 6 additions & 11 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use rustc_middle::middle::cstore::{ForeignModule, LinkagePreference, NativeLib};
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
use rustc_middle::mir::{self, Body, Promoted};
use rustc_middle::ty::codec::TyDecoder;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{self, codec::TyDecoder, Interner, Ty, TyCtxt, TyInterner};
use rustc_serialize::{opaque, Decodable, Decoder};
use rustc_session::Session;
use rustc_span::hygiene::ExpnDataDecodeMode;
Expand Down Expand Up @@ -245,10 +244,6 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable<DecodeContext<'a, 'tcx>>> Lazy<[T]> {
}

impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx.expect("missing TyCtxt in DecodeContext")
}

fn cdata(&self) -> CrateMetadataRef<'a> {
self.cdata.expect("missing CrateMetadata in DecodeContext")
}
Expand Down Expand Up @@ -277,8 +272,8 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
const CLEAR_CROSS_CRATE: bool = true;

#[inline]
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx.expect("missing TyCtxt in DecodeContext")
fn interner(&self) -> TyInterner<'tcx> {
self.tcx.expect("missing TyCtxt in DecodeContext").interner()
}

#[inline]
Expand All @@ -299,16 +294,16 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
where
F: FnOnce(&mut Self) -> Result<Ty<'tcx>, Self::Error>,
{
let tcx = self.tcx();
let mut interner = self.interner();

let key = ty::CReaderCacheKey { cnum: self.cdata().cnum, pos: shorthand };

if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) {
if let Some(ty) = interner.get_cached_ty(key) {
return Ok(ty);
}

let ty = or_insert_with(self)?;
tcx.ty_rcache.borrow_mut().insert(key, ty);
interner.insert_cached_ty(key, ty);
Ok(ty)
}

Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_middle/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ macro_rules! arena_types {
// AdtDef are interned and compared by address
[] adt_def: rustc_middle::ty::AdtDef,
[] steal_mir: rustc_data_structures::steal::Steal<rustc_middle::mir::Body<$tcx>>,
[decode] mir: rustc_middle::mir::Body<$tcx>,
[] mir: rustc_middle::mir::Body<$tcx>,
[] steal_promoted:
rustc_data_structures::steal::Steal<
rustc_index::vec::IndexVec<
rustc_middle::mir::Promoted,
rustc_middle::mir::Body<$tcx>
>
>,
[decode] promoted:
[] promoted:
rustc_index::vec::IndexVec<
rustc_middle::mir::Promoted,
rustc_middle::mir::Body<$tcx>
>,
[decode] typeck_results: rustc_middle::ty::TypeckResults<$tcx>,
[decode] borrowck_result:
[] typeck_results: rustc_middle::ty::TypeckResults<$tcx>,
[] borrowck_result:
rustc_middle::mir::BorrowCheckResult<$tcx>,
[decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult,
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
[] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult,
[] code_region: rustc_middle::mir::coverage::CodeRegion,
[] const_allocs: rustc_middle::mir::interpret::Allocation,
// Required for the incremental on-disk cache
[few] mir_keys: rustc_hir::def_id::DefIdSet,
Expand Down Expand Up @@ -99,11 +99,11 @@ macro_rules! arena_types {
// Note that this deliberately duplicates items in the `rustc_hir::arena`,
// since we need to allocate this type on both the `rustc_hir` arena
// (during lowering) and the `librustc_middle` arena (for decoding MIR)
[decode] asm_template: rustc_ast::InlineAsmTemplatePiece,
[] asm_template: rustc_ast::InlineAsmTemplatePiece,

// This is used to decode the &'tcx [Span] for InlineAsm's line_spans.
[decode] span: rustc_span::Span,
[decode] used_trait_imports: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>,
[] span: rustc_span::Span,
[] used_trait_imports: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>,
], $tcx);
)
}
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_middle/src/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ use rustc_macros::HashStable;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_serialize::{Decodable, Encodable};
use rustc_target::abi::Endian;
use rustc_type_ir::Interner;

use crate::mir;
use crate::ty::codec::{TyDecoder, TyEncoder};
Expand Down Expand Up @@ -301,7 +302,7 @@ impl<'s> AllocDecodingSession<'s> {
AllocDiscriminant::Alloc => {
// If this is an allocation, we need to reserve an
// `AllocId` so we can decode cyclic graphs.
let alloc_id = decoder.tcx().reserve_alloc_id();
let alloc_id = decoder.interner().reserve_alloc_id();
*entry =
State::InProgress(TinyList::new_single(self.session_id), alloc_id);
Some(alloc_id)
Expand Down Expand Up @@ -345,23 +346,23 @@ impl<'s> AllocDecodingSession<'s> {
// We already have a reserved `AllocId`.
let alloc_id = alloc_id.unwrap();
trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
decoder.tcx().set_alloc_id_same_memory(alloc_id, alloc);
decoder.interner().set_alloc_id_same_memory(alloc_id, alloc);
Ok(alloc_id)
}
AllocDiscriminant::Fn => {
assert!(alloc_id.is_none());
trace!("creating fn alloc ID");
let instance = ty::Instance::decode(decoder)?;
trace!("decoded fn alloc instance: {:?}", instance);
let alloc_id = decoder.tcx().create_fn_alloc(instance);
let alloc_id = decoder.interner().create_fn_alloc(instance);
Ok(alloc_id)
}
AllocDiscriminant::Static => {
assert!(alloc_id.is_none());
trace!("creating extern static alloc ID");
let did = <DefId as Decodable<D>>::decode(decoder)?;
trace!("decoded static def-ID: {:?}", did);
let alloc_id = decoder.tcx().create_static_alloc(did);
let alloc_id = decoder.interner().create_static_alloc(did);
Ok(alloc_id)
}
}
Expand Down Expand Up @@ -545,7 +546,7 @@ impl<'tcx> TyCtxt<'tcx> {

/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
/// twice for the same `(AllocId, Allocation)` pair.
fn set_alloc_id_same_memory(self, id: AllocId, mem: &'tcx Allocation) {
pub fn set_alloc_id_same_memory(self, id: AllocId, mem: &'tcx Allocation) {
self.alloc_map.lock().alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
}
}
Expand Down
Loading