Skip to content

Commit

Permalink
Compute LLVM-agnostic type layouts in rustc.
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/librustc/ty/layout.rs
  • Loading branch information
eddyb committed Apr 19, 2016
1 parent efd0ea5 commit fe48a4a
Show file tree
Hide file tree
Showing 5 changed files with 1,006 additions and 37 deletions.
25 changes: 24 additions & 1 deletion src/librustc/ty/context.rs
Expand Up @@ -31,7 +31,7 @@ use hir::FreevarMap;
use ty::{BareFnTy, InferTy, ParamTy, ProjectionTy, TraitTy};
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
use ty::TypeVariants::*;
use ty::layout::TargetDataLayout;
use ty::layout::{Layout, TargetDataLayout};
use ty::maps;
use util::common::MemoizationMap;
use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet};
Expand All @@ -56,6 +56,7 @@ pub struct CtxtArenas<'tcx> {
bare_fn: TypedArena<BareFnTy<'tcx>>,
region: TypedArena<Region>,
stability: TypedArena<attr::Stability>,
layout: TypedArena<Layout>,

// references
trait_defs: TypedArena<ty::TraitDef<'tcx>>,
Expand All @@ -70,6 +71,7 @@ impl<'tcx> CtxtArenas<'tcx> {
bare_fn: TypedArena::new(),
region: TypedArena::new(),
stability: TypedArena::new(),
layout: TypedArena::new(),

trait_defs: TypedArena::new(),
adt_defs: TypedArena::new()
Expand Down Expand Up @@ -230,6 +232,7 @@ pub struct TyCtxt<'tcx> {
bare_fn_interner: RefCell<FnvHashMap<&'tcx BareFnTy<'tcx>, &'tcx BareFnTy<'tcx>>>,
region_interner: RefCell<FnvHashMap<&'tcx Region, &'tcx Region>>,
stability_interner: RefCell<FnvHashMap<&'tcx attr::Stability, &'tcx attr::Stability>>,
layout_interner: RefCell<FnvHashMap<&'tcx Layout, &'tcx Layout>>,

pub dep_graph: DepGraph,

Expand Down Expand Up @@ -423,6 +426,9 @@ pub struct TyCtxt<'tcx> {

/// Data layout specification for the current target.
pub data_layout: TargetDataLayout,

/// Cache for layouts computed from types.
pub layout_cache: RefCell<FnvHashMap<Ty<'tcx>, &'tcx Layout>>,
}

impl<'tcx> TyCtxt<'tcx> {
Expand Down Expand Up @@ -504,6 +510,20 @@ impl<'tcx> TyCtxt<'tcx> {
interned
}

pub fn intern_layout(&self, layout: Layout) -> &'tcx Layout {
if let Some(layout) = self.layout_interner.borrow().get(&layout) {
return layout;
}

let interned = self.arenas.layout.alloc(layout);
if let Some(prev) = self.layout_interner
.borrow_mut()
.insert(interned, interned) {
bug!("Tried to overwrite interned Layout: {:?}", prev)
}
interned
}

pub fn store_free_region_map(&self, id: NodeId, map: FreeRegionMap) {
if self.free_region_maps.borrow_mut().insert(id, map).is_some() {
bug!("Tried to overwrite interned FreeRegionMap for NodeId {:?}", id)
Expand Down Expand Up @@ -547,6 +567,7 @@ impl<'tcx> TyCtxt<'tcx> {
bare_fn_interner: RefCell::new(FnvHashMap()),
region_interner: RefCell::new(FnvHashMap()),
stability_interner: RefCell::new(FnvHashMap()),
layout_interner: RefCell::new(FnvHashMap()),
dep_graph: dep_graph.clone(),
types: common_types,
named_region_map: named_region_map,
Expand Down Expand Up @@ -595,6 +616,7 @@ impl<'tcx> TyCtxt<'tcx> {
fragment_infos: RefCell::new(DefIdMap()),
crate_name: token::intern_and_get_ident(crate_name),
data_layout: data_layout,
layout_cache: RefCell::new(FnvHashMap()),
}, f)
}
}
Expand Down Expand Up @@ -768,6 +790,7 @@ impl<'tcx> TyCtxt<'tcx> {
println!("BareFnTy interner: #{}", self.bare_fn_interner.borrow().len());
println!("Region interner: #{}", self.region_interner.borrow().len());
println!("Stability interner: #{}", self.stability_interner.borrow().len());
println!("Layout interner: #{}", self.layout_interner.borrow().len());
}
}

Expand Down

0 comments on commit fe48a4a

Please sign in to comment.