Skip to content

Commit

Permalink
Auto merge of #45525 - MaikKlein:collector, r=eddyb
Browse files Browse the repository at this point in the history
Move collector to librustc_mir::monomorphize

cc #44334 and #45276

* I moved the collector to rustc_mir

*  I renamed `TransItem` to `MonoItem`. _(I still need to fix up comments and variable names)_

* I got rid of `common.rs` and `monomorphize.rs` from `librustc_trans_utils`. I moved most of the functionality into `TyCtxt`. I realized that the `librustc_trans_utils::common.rs` was just copy pasted from `librustc_trans::common.rs`.

Should I also get rid of the `librustc_trans::common.rs` in this PR? Most of the functionality seems a bit useless, I decided to put some of it into `TyCtxt` but maybe that is not the correct action here.

Should I also get rid of `librustc_trans_utils` completely here? Or should I do it in a separate PR?
  • Loading branch information
bors committed Dec 19, 2017
2 parents e7db42f + 6e78b66 commit b76f224
Show file tree
Hide file tree
Showing 31 changed files with 372 additions and 438 deletions.
2 changes: 2 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ pub mod middle {
pub mod recursion_limit;
pub mod resolve_lifetime;
pub mod stability;
pub mod trans;
pub mod weak_lang_items;
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod tcx;
pub mod visit;
pub mod traversal;
pub mod interpret;
pub mod mono;

/// Types for locals
type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/middle/trans.rs → src/librustc/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasherResult,
use ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};

#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum TransItem<'tcx> {
pub enum MonoItem<'tcx> {
Fn(Instance<'tcx>),
Static(NodeId),
GlobalAsm(NodeId),
}

impl<'tcx> HashStable<StableHashingContext<'tcx>> for TransItem<'tcx> {
impl<'tcx> HashStable<StableHashingContext<'tcx>> for MonoItem<'tcx> {
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'tcx>,
hasher: &mut StableHasher<W>) {
::std::mem::discriminant(self).hash_stable(hcx, hasher);

match *self {
TransItem::Fn(ref instance) => {
MonoItem::Fn(ref instance) => {
instance.hash_stable(hcx, hasher);
}
TransItem::Static(node_id) |
TransItem::GlobalAsm(node_id) => {
MonoItem::Static(node_id) |
MonoItem::GlobalAsm(node_id) => {
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
node_id.hash_stable(hcx, hasher);
})
Expand All @@ -49,7 +49,7 @@ pub struct CodegenUnit<'tcx> {
/// contain something unique to this crate (e.g., a module path)
/// as well as the crate name and disambiguator.
name: InternedString,
items: FxHashMap<TransItem<'tcx>, (Linkage, Visibility)>,
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
Expand Down Expand Up @@ -110,12 +110,12 @@ impl<'tcx> CodegenUnit<'tcx> {
self.name = name;
}

pub fn items(&self) -> &FxHashMap<TransItem<'tcx>, (Linkage, Visibility)> {
pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
&self.items
}

pub fn items_mut(&mut self)
-> &mut FxHashMap<TransItem<'tcx>, (Linkage, Visibility)>
-> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>
{
&mut self.items
}
Expand Down
8 changes: 7 additions & 1 deletion src/librustc/traits/trans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use dep_graph::{DepKind, DepTrackingMapConfig};
use infer::TransNormalize;
use std::marker::PhantomData;
use syntax_pos::DUMMY_SP;
use hir::def_id::DefId;
use traits::{FulfillmentContext, Obligation, ObligationCause, SelectionContext, Vtable};
use ty::{self, Ty, TyCtxt};
use ty::subst::{Subst, Substs};
Expand Down Expand Up @@ -119,6 +120,12 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
let substituted = self.erase_regions(&substituted);
AssociatedTypeNormalizerEnv::new(self, param_env).fold(&substituted)
}

pub fn trans_impl_self_ty(&self, def_id: DefId, substs: &'tcx Substs<'tcx>)
-> Ty<'tcx>
{
self.trans_apply_param_substs(substs, &self.type_of(def_id))
}
}

struct AssociatedTypeNormalizer<'a, 'gcx: 'a> {
Expand Down Expand Up @@ -214,4 +221,3 @@ impl<'gcx> DepTrackingMapConfig for ProjectionCache<'gcx> {
DepKind::TraitSelect
}
}

51 changes: 46 additions & 5 deletions src/librustc/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ pub enum InstanceDef<'tcx> {
CloneShim(DefId, Ty<'tcx>),
}

impl<'a, 'tcx> Instance<'tcx> {
pub fn ty(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Ty<'tcx>
{
let ty = tcx.type_of(self.def.def_id());
tcx.trans_apply_param_substs(self.substs, &ty)
}
}

impl<'tcx> InstanceDef<'tcx> {
#[inline]
pub fn def_id(&self) -> DefId {
Expand All @@ -59,15 +69,46 @@ impl<'tcx> InstanceDef<'tcx> {
}
}

#[inline]
pub fn def_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
tcx.type_of(self.def_id())
}

#[inline]
pub fn attrs<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::Attributes<'tcx> {
tcx.get_attrs(self.def_id())
}

pub fn is_inline<'a>(
&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>
) -> bool {
use hir::map::DefPathData;
let def_id = match *self {
ty::InstanceDef::Item(def_id) => def_id,
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
_ => return true
};
match tcx.def_key(def_id).disambiguated_data.data {
DefPathData::StructCtor |
DefPathData::EnumVariant(..) |
DefPathData::ClosureExpr => true,
_ => false
}
}

pub fn requires_local<'a>(
&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>
) -> bool {
use syntax::attr::requests_inline;
if self.is_inline(tcx) {
return true
}
if let ty::InstanceDef::DropGlue(..) = *self {
// Drop glue wants to be instantiated at every translation
// unit, but without an #[inline] hint. We should make this
// available to normal end-users.
return true
}
requests_inline(&self.attrs(tcx)[..]) ||
tcx.is_const_fn(self.def_id())
}
}

impl<'tcx> fmt::Display for Instance<'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use middle::resolve_lifetime::{ResolveLifetimes, Region, ObjectLifetimeDefault};
use middle::stability::{self, DeprecationEntry};
use middle::lang_items::{LanguageItems, LangItem};
use middle::exported_symbols::SymbolExportLevel;
use middle::trans::{CodegenUnit, Stats};
use mir::mono::{CodegenUnit, Stats};
use mir;
use session::{CompileResult, CrateDisambiguator};
use session::config::OutputFilenames;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
byteorder = { version = "1.1", features = ["i128"] }
rustc_apfloat = { path = "../librustc_apfloat" }
rustc_trans_utils = { path = "../librustc_trans_utils" }
4 changes: 1 addition & 3 deletions src/librustc_mir/interpret/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ pub fn eval_body<'a, 'tcx>(
if ecx.tcx.has_attr(instance.def_id(), "linkage") {
return Err(ConstEvalError::NotConst("extern global".to_string()).into());
}
// FIXME(eddyb) use `Instance::ty` when it becomes available.
let instance_ty =
ecx.monomorphize(instance.def.def_ty(tcx), instance.substs);
let instance_ty = instance.ty(tcx);
if tcx.interpret_interner.borrow().get_cached(cid).is_none() {
let mir = ecx.load_mir(instance.def)?;
let layout = ecx.layout_of(instance_ty)?;
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_mir/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
M::global_item_with_linkage(self, cid.instance, mutability)?;
return Ok(false);
}
// FIXME(eddyb) use `Instance::ty` when it becomes available.
let instance_ty =
self.monomorphize(instance.def.def_ty(self.tcx), instance.substs);
let instance_ty = instance.ty(self.tcx);
let layout = self.layout_of(instance_ty)?;
assert!(!layout.is_unsized());
let ptr = self.memory.allocate(
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_mir/interpret/terminator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
ty::TyFnPtr(sig) => {
let fn_ptr = self.value_to_primval(func)?.to_ptr()?;
let instance = self.memory.get_fn(fn_ptr)?;
// FIXME(eddyb) use `Instance::ty` when it becomes available.
let instance_ty =
self.monomorphize(instance.def.def_ty(self.tcx), instance.substs);
let instance_ty = instance.ty(self.tcx);
match instance_ty.sty {
ty::TyFnDef(..) => {
let real_sig = instance_ty.fn_sig(self.tcx);
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern crate core; // for NonZero
extern crate log_settings;
extern crate rustc_apfloat;
extern crate byteorder;
extern crate rustc_trans_utils;

mod diagnostics;

Expand All @@ -65,6 +66,7 @@ mod shim;
pub mod transform;
pub mod util;
pub mod interpret;
pub mod monomorphize;

use rustc::ty::maps::Providers;

Expand Down
Loading

0 comments on commit b76f224

Please sign in to comment.