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

Move collector to librustc_mir::monomorphize #45525

Merged
merged 26 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
094c021
Move collector to monomorphize
MaikKlein Oct 25, 2017
e579b06
Move trans_item and monomorphize to rustc_mir
MaikKlein Oct 25, 2017
1181f45
Rename TransItem to MonoItem
MaikKlein Oct 25, 2017
98b9eba
Rename TransItemCollectionMode to MonoItemCollectionMode
MaikKlein Oct 25, 2017
531c27d
Move common.rs functionality into TyCtxt
MaikKlein Oct 25, 2017
b9ab487
Rename TransItemExt to MonoItemExt
MaikKlein Oct 25, 2017
17bfd74
Rename more functions from trans to mono
MaikKlein Oct 26, 2017
28f7d22
Rename as_trans_item to as_mono_item
MaikKlein Oct 26, 2017
09ad6eb
Rename mono_item.rs to item.rs
MaikKlein Oct 26, 2017
ae468ab
Move middle::trans.rs to mir::mono.rs
MaikKlein Oct 27, 2017
c06e3aa
Refactor paths to middle::trans to mir::mono
MaikKlein Oct 27, 2017
d3c4142
Fix some comments to refer to `MonoItem`
MaikKlein Oct 27, 2017
dfbb6e8
Move instance related methods from TyCtxt to Instance
MaikKlein Oct 27, 2017
7996f63
Move meta_data into TyS
MaikKlein Oct 29, 2017
1df6f83
Remove duplicated functions from trans::common.rs
MaikKlein Oct 30, 2017
ab0f8fc
Test with trans_apply_param_substs
MaikKlein Nov 23, 2017
6e94a7a
Remove branch with has_metadata
MaikKlein Nov 23, 2017
282b231
Move has_metadata back to context.rs
MaikKlein Nov 23, 2017
116e43f
Prefer type_of().substs over instance::ty()
MaikKlein Nov 23, 2017
45f8a3b
Move partitioning.rs to rustc_mir
MaikKlein Nov 23, 2017
d4b372d
Rename trans to monomorphize in comments
MaikKlein Dec 1, 2017
b48ba02
Rename more functions from trans to monomorphize
MaikKlein Dec 5, 2017
88866b5
Update lockfile
MaikKlein Dec 14, 2017
f6fcfa3
normalize the results of `tcx.type_of` after substituting
arielb1 Dec 17, 2017
c847cf3
Fix incorrect rebase in collector::find_vtable_types
MaikKlein Dec 18, 2017
6e78b66
Add rustc_data_structures for trans_utils/lib.rs
MaikKlein Dec 18, 2017
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
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me sad but I guess I'm okay with it for now if it's necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just feels like less of a footgun than the other option.

}
}

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