diff --git a/src/librustc/mir/mono.rs b/src/librustc/mir/mono.rs index 9ba0bb3eb2d3..79228a5c56f2 100644 --- a/src/librustc/mir/mono.rs +++ b/src/librustc/mir/mono.rs @@ -3,9 +3,10 @@ use crate::hir::HirId; use syntax::symbol::InternedString; use syntax::attr::InlineAttr; use syntax::source_map::Span; -use crate::ty::{Instance, TyCtxt, SymbolName, subst::InternalSubsts}; +use crate::ty::{Instance, InstanceDef, TyCtxt, SymbolName, subst::InternalSubsts}; use crate::util::nodemap::FxHashMap; use crate::ty::print::obsolete::DefPathBasedNames; +use crate::dep_graph::{WorkProductId, DepNode, WorkProduct, DepConstructor}; use rustc_data_structures::base_n; use rustc_data_structures::stable_hasher::{HashStable, StableHasherResult, StableHasher}; @@ -350,6 +351,73 @@ impl<'tcx> CodegenUnit<'tcx> { self.size_estimate = Some(size_estimate + delta); } } + + pub fn contains_item(&self, item: &MonoItem<'tcx>) -> bool { + self.items().contains_key(item) + } + + pub fn work_product_id(&self) -> WorkProductId { + WorkProductId::from_cgu_name(&self.name().as_str()) + } + + pub fn work_product(&self, tcx: TyCtxt<'_, '_, '_>) -> WorkProduct { + let work_product_id = self.work_product_id(); + tcx.dep_graph + .previous_work_product(&work_product_id) + .unwrap_or_else(|| { + panic!("Could not find work-product for CGU `{}`", self.name()) + }) + } + + pub fn items_in_deterministic_order<'a>(&self, + tcx: TyCtxt<'a, 'tcx, 'tcx>) + -> Vec<(MonoItem<'tcx>, + (Linkage, Visibility))> { + // The codegen tests rely on items being process in the same order as + // they appear in the file, so for local items, we sort by node_id first + #[derive(PartialEq, Eq, PartialOrd, Ord)] + pub struct ItemSortKey(Option, SymbolName); + + fn item_sort_key<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, + item: MonoItem<'tcx>) -> ItemSortKey { + ItemSortKey(match item { + MonoItem::Fn(ref instance) => { + match instance.def { + // We only want to take HirIds of user-defined + // instances into account. The others don't matter for + // the codegen tests and can even make item order + // unstable. + InstanceDef::Item(def_id) => { + tcx.hir().as_local_hir_id(def_id) + } + InstanceDef::VtableShim(..) | + InstanceDef::Intrinsic(..) | + InstanceDef::FnPtrShim(..) | + InstanceDef::Virtual(..) | + InstanceDef::ClosureOnceShim { .. } | + InstanceDef::DropGlue(..) | + InstanceDef::CloneShim(..) => { + None + } + } + } + MonoItem::Static(def_id) => { + tcx.hir().as_local_hir_id(def_id) + } + MonoItem::GlobalAsm(hir_id) => { + Some(hir_id) + } + }, item.symbol_name(tcx)) + } + + let mut items: Vec<_> = self.items().iter().map(|(&i, &l)| (i, l)).collect(); + items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i)); + items + } + + pub fn codegen_dep_node(&self, tcx: TyCtxt<'_, 'tcx, 'tcx>) -> DepNode { + DepNode::new(tcx, DepConstructor::CompileCodegenUnit(self.name().clone())) + } } impl<'a, 'tcx> HashStable> for CodegenUnit<'tcx> { diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs index b13d8df5525e..09156c0f9e74 100644 --- a/src/librustc_codegen_llvm/lib.rs +++ b/src/librustc_codegen_llvm/lib.rs @@ -32,7 +32,6 @@ extern crate flate2; #[macro_use] extern crate bitflags; extern crate libc; #[macro_use] extern crate rustc; -extern crate rustc_mir; extern crate rustc_allocator; extern crate rustc_target; #[macro_use] extern crate rustc_data_structures; diff --git a/src/librustc_codegen_ssa/base.rs b/src/librustc_codegen_ssa/base.rs index 34e5b0681de0..0cd29e0213ee 100644 --- a/src/librustc_codegen_ssa/base.rs +++ b/src/librustc_codegen_ssa/base.rs @@ -28,7 +28,6 @@ use rustc::middle::cstore::{self, LinkagePreference}; use rustc::util::common::{time, print_time_passes_entry}; use rustc::session::config::{self, EntryFnType, Lto}; use rustc::session::Session; -use rustc_mir::monomorphize::partitioning::CodegenUnitExt; use rustc::util::nodemap::FxHashMap; use rustc_data_structures::indexed_vec::Idx; use rustc_codegen_utils::{symbol_names_test, check_for_rustc_errors_attr}; diff --git a/src/librustc_mir/monomorphize/partitioning.rs b/src/librustc_mir/monomorphize/partitioning.rs index 6a7f67da1bf1..1895d4871552 100644 --- a/src/librustc_mir/monomorphize/partitioning.rs +++ b/src/librustc_mir/monomorphize/partitioning.rs @@ -97,8 +97,7 @@ use std::cmp; use std::sync::Arc; use syntax::symbol::InternedString; -use rustc::dep_graph::{WorkProductId, WorkProduct, DepNode, DepConstructor}; -use rustc::hir::{CodegenFnAttrFlags, HirId}; +use rustc::hir::CodegenFnAttrFlags; use rustc::hir::def::DefKind; use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX}; use rustc::mir::mono::{Linkage, Visibility, CodegenUnitNameBuilder, CodegenUnit}; @@ -121,93 +120,6 @@ pub enum PartitioningStrategy { FixedUnitCount(usize) } -pub trait CodegenUnitExt<'tcx> { - fn as_codegen_unit(&self) -> &CodegenUnit<'tcx>; - - fn contains_item(&self, item: &MonoItem<'tcx>) -> bool { - self.items().contains_key(item) - } - - fn name<'a>(&'a self) -> &'a InternedString - where 'tcx: 'a, - { - &self.as_codegen_unit().name() - } - - fn items(&self) -> &FxHashMap, (Linkage, Visibility)> { - &self.as_codegen_unit().items() - } - - fn work_product_id(&self) -> WorkProductId { - WorkProductId::from_cgu_name(&self.name().as_str()) - } - - fn work_product(&self, tcx: TyCtxt<'_, '_, '_>) -> WorkProduct { - let work_product_id = self.work_product_id(); - tcx.dep_graph - .previous_work_product(&work_product_id) - .unwrap_or_else(|| { - panic!("Could not find work-product for CGU `{}`", self.name()) - }) - } - - fn items_in_deterministic_order<'a>(&self, - tcx: TyCtxt<'a, 'tcx, 'tcx>) - -> Vec<(MonoItem<'tcx>, - (Linkage, Visibility))> { - // The codegen tests rely on items being process in the same order as - // they appear in the file, so for local items, we sort by node_id first - #[derive(PartialEq, Eq, PartialOrd, Ord)] - pub struct ItemSortKey(Option, ty::SymbolName); - - fn item_sort_key<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, - item: MonoItem<'tcx>) -> ItemSortKey { - ItemSortKey(match item { - MonoItem::Fn(ref instance) => { - match instance.def { - // We only want to take HirIds of user-defined - // instances into account. The others don't matter for - // the codegen tests and can even make item order - // unstable. - InstanceDef::Item(def_id) => { - tcx.hir().as_local_hir_id(def_id) - } - InstanceDef::VtableShim(..) | - InstanceDef::Intrinsic(..) | - InstanceDef::FnPtrShim(..) | - InstanceDef::Virtual(..) | - InstanceDef::ClosureOnceShim { .. } | - InstanceDef::DropGlue(..) | - InstanceDef::CloneShim(..) => { - None - } - } - } - MonoItem::Static(def_id) => { - tcx.hir().as_local_hir_id(def_id) - } - MonoItem::GlobalAsm(hir_id) => { - Some(hir_id) - } - }, item.symbol_name(tcx)) - } - - let mut items: Vec<_> = self.items().iter().map(|(&i, &l)| (i, l)).collect(); - items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i)); - items - } - - fn codegen_dep_node(&self, tcx: TyCtxt<'_, 'tcx, 'tcx>) -> DepNode { - DepNode::new(tcx, DepConstructor::CompileCodegenUnit(self.name().clone())) - } -} - -impl<'tcx> CodegenUnitExt<'tcx> for CodegenUnit<'tcx> { - fn as_codegen_unit(&self) -> &CodegenUnit<'tcx> { - self - } -} - // Anything we can't find a proper codegen unit for goes into this. fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder<'_, '_, '_>) -> InternedString { name_builder.build_cgu_name(LOCAL_CRATE, &["fallback"], Some("cgu"))