Skip to content

Commit

Permalink
Rollup merge of rust-lang#48353 - michaelwoerister:monoitem-static-de…
Browse files Browse the repository at this point in the history
…fid, r=eddyb

Allow for instantiating statics from upstream crates

This PR makes the infrastructure around translating statics a bit more flexible so that it can also instantiate statics from upstream crates if the need arises. This is preparatory work for a MIR-only RLIBs prototype, where the instantiation of a `static` may be deferred until a leaf crate.

r? @eddyb (feel free to assign to someone else if you're busy)
  • Loading branch information
Manishearth committed Feb 24, 2018
2 parents a1a1409 + 89b3ef3 commit 2483d84
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ define_dep_nodes!( <'tcx>
[eval_always] CollectAndPartitionTranslationItems,
[] ExportName(DefId),
[] ContainsExternIndicator(DefId),
[] IsTranslatedFunction(DefId),
[] IsTranslatedItem(DefId),
[] CodegenUnit(InternedString),
[] CompileCodegenUnit(InternedString),
[input] OutputFilenames,
Expand Down
98 changes: 98 additions & 0 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
use syntax::abi::Abi;
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
use syntax::codemap::Spanned;
use syntax::ext::base::MacroKind;
use syntax_pos::Span;

use hir::*;
Expand All @@ -32,13 +33,15 @@ use util::nodemap::{DefIdMap, FxHashMap};
use arena::TypedArena;
use std::cell::RefCell;
use std::io;
use ty::TyCtxt;

pub mod blocks;
mod collector;
mod def_collector;
pub mod definitions;
mod hir_id_validator;


pub const ITEM_LIKE_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::Low;
pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;

Expand Down Expand Up @@ -373,6 +376,92 @@ impl<'hir> Map<'hir> {
self.definitions.as_local_node_id(def_id.to_def_id()).unwrap()
}

pub fn describe_def(&self, node_id: NodeId) -> Option<Def> {
let node = if let Some(node) = self.find(node_id) {
node
} else {
return None
};

match node {
NodeItem(item) => {
let def_id = || {
self.local_def_id(item.id)
};

match item.node {
ItemStatic(_, m, _) => Some(Def::Static(def_id(),
m == MutMutable)),
ItemConst(..) => Some(Def::Const(def_id())),
ItemFn(..) => Some(Def::Fn(def_id())),
ItemMod(..) => Some(Def::Mod(def_id())),
ItemGlobalAsm(..) => Some(Def::GlobalAsm(def_id())),
ItemTy(..) => Some(Def::TyAlias(def_id())),
ItemEnum(..) => Some(Def::Enum(def_id())),
ItemStruct(..) => Some(Def::Struct(def_id())),
ItemUnion(..) => Some(Def::Union(def_id())),
ItemTrait(..) => Some(Def::Trait(def_id())),
ItemTraitAlias(..) => {
bug!("trait aliases are not yet implemented (see issue #41517)")
},
ItemExternCrate(_) |
ItemUse(..) |
ItemForeignMod(..) |
ItemImpl(..) => None,
}
}
NodeForeignItem(item) => {
let def_id = self.local_def_id(item.id);
match item.node {
ForeignItemFn(..) => Some(Def::Fn(def_id)),
ForeignItemStatic(_, m) => Some(Def::Static(def_id, m)),
ForeignItemType => Some(Def::TyForeign(def_id)),
}
}
NodeTraitItem(item) => {
let def_id = self.local_def_id(item.id);
match item.node {
TraitItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
TraitItemKind::Method(..) => Some(Def::Method(def_id)),
TraitItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
}
}
NodeImplItem(item) => {
let def_id = self.local_def_id(item.id);
match item.node {
ImplItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
ImplItemKind::Method(..) => Some(Def::Method(def_id)),
ImplItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
}
}
NodeVariant(variant) => {
let def_id = self.local_def_id(variant.node.data.id());
Some(Def::Variant(def_id))
}
NodeField(_) |
NodeExpr(_) |
NodeStmt(_) |
NodeTy(_) |
NodeTraitRef(_) |
NodePat(_) |
NodeBinding(_) |
NodeStructCtor(_) |
NodeLifetime(_) |
NodeVisibility(_) |
NodeBlock(_) => None,
NodeLocal(local) => {
Some(Def::Local(local.id))
}
NodeMacroDef(macro_def) => {
Some(Def::Macro(self.local_def_id(macro_def.id),
MacroKind::Bang))
}
NodeTyParam(param) => {
Some(Def::TyParam(self.local_def_id(param.id)))
}
}
}

fn entry_count(&self) -> usize {
self.map.len()
}
Expand Down Expand Up @@ -1275,3 +1364,12 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
}
}
}

pub fn describe_def(tcx: TyCtxt, def_id: DefId) -> Option<Def> {
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
tcx.hir.describe_def(node_id)
} else {
bug!("Calling local describe_def query provider for upstream DefId: {:?}",
def_id)
}
}
6 changes: 6 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use syntax::tokenstream::TokenStream;
use syntax::util::ThinVec;
use syntax::util::parser::ExprPrecedence;
use ty::AdtKind;
use ty::maps::Providers;

use rustc_data_structures::indexed_vec;

Expand Down Expand Up @@ -2204,3 +2205,8 @@ pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
// Map from the NodeId of a glob import to a list of items which are actually
// imported.
pub type GlobMap = NodeMap<FxHashSet<Name>>;


pub fn provide(providers: &mut Providers) {
providers.describe_def = map::describe_def;
}
7 changes: 5 additions & 2 deletions src/librustc/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use hir::def_id::DefId;
use syntax::ast::NodeId;
use syntax::symbol::InternedString;
use ty::{Instance, TyCtxt};
Expand All @@ -21,7 +22,7 @@ use std::hash::Hash;
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
pub enum MonoItem<'tcx> {
Fn(Instance<'tcx>),
Static(NodeId),
Static(DefId),
GlobalAsm(NodeId),
}

Expand Down Expand Up @@ -50,7 +51,9 @@ impl<'tcx> HashStable<StableHashingContext<'tcx>> for MonoItem<'tcx> {
MonoItem::Fn(ref instance) => {
instance.hash_stable(hcx, hasher);
}
MonoItem::Static(node_id) |
MonoItem::Static(def_id) => {
def_id.hash_stable(hcx, hasher);
}
MonoItem::GlobalAsm(node_id) => {
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
node_id.hash_stable(hcx, hasher);
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 @@ -349,7 +349,7 @@ define_maps! { <'tcx>
[] fn export_name: ExportName(DefId) -> Option<Symbol>,
[] fn contains_extern_indicator: ContainsExternIndicator(DefId) -> bool,
[] fn symbol_export_level: GetSymbolExportLevel(DefId) -> SymbolExportLevel,
[] fn is_translated_function: IsTranslatedFunction(DefId) -> bool,
[] fn is_translated_item: IsTranslatedItem(DefId) -> bool,
[] fn codegen_unit: CodegenUnit(InternedString) -> Arc<CodegenUnit<'tcx>>,
[] fn compile_codegen_unit: CompileCodegenUnit(InternedString) -> Stats,
[] fn output_filenames: output_filenames_node(CrateNum)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
DepKind::ContainsExternIndicator => {
force!(contains_extern_indicator, def_id!());
}
DepKind::IsTranslatedFunction => { force!(is_translated_function, def_id!()); }
DepKind::IsTranslatedItem => { force!(is_translated_item, def_id!()); }
DepKind::OutputFilenames => { force!(output_filenames, LOCAL_CRATE); }

DepKind::TargetFeaturesWhitelist => { force!(target_features_whitelist, LOCAL_CRATE); }
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
}

pub fn default_provide(providers: &mut ty::maps::Providers) {
hir::provide(providers);
borrowck::provide(providers);
mir::provide(providers);
reachable::provide(providers);
Expand Down
12 changes: 5 additions & 7 deletions src/librustc_mir/monomorphize/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let recursion_depth_reset;

match starting_point {
MonoItem::Static(node_id) => {
let def_id = tcx.hir.local_def_id(node_id);
MonoItem::Static(def_id) => {
let instance = Instance::mono(tcx, def_id);

// Sanity check whether this ended up being collected accidentally
Expand Down Expand Up @@ -652,8 +651,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
let tcx = self.tcx;
let instance = Instance::mono(tcx, static_.def_id);
if should_monomorphize_locally(tcx, &instance) {
let node_id = tcx.hir.as_local_node_id(static_.def_id).unwrap();
self.output.push(MonoItem::Static(node_id));
self.output.push(MonoItem::Static(static_.def_id));
}

self.super_static(static_, context, location);
Expand Down Expand Up @@ -946,10 +944,10 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
self.output.push(MonoItem::GlobalAsm(item.id));
}
hir::ItemStatic(..) => {
let def_id = self.tcx.hir.local_def_id(item.id);
debug!("RootCollector: ItemStatic({})",
def_id_to_string(self.tcx,
self.tcx.hir.local_def_id(item.id)));
self.output.push(MonoItem::Static(item.id));
def_id_to_string(self.tcx, def_id));
self.output.push(MonoItem::Static(def_id));
}
hir::ItemConst(..) => {
// const items only generate mono items if they are
Expand Down
16 changes: 7 additions & 9 deletions src/librustc_mir/monomorphize/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
fn symbol_name(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::SymbolName {
match *self.as_mono_item() {
MonoItem::Fn(instance) => tcx.symbol_name(instance),
MonoItem::Static(node_id) => {
let def_id = tcx.hir.local_def_id(node_id);
MonoItem::Static(def_id) => {
tcx.symbol_name(Instance::mono(tcx, def_id))
}
MonoItem::GlobalAsm(node_id) => {
Expand Down Expand Up @@ -159,7 +158,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
fn explicit_linkage(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Linkage> {
let def_id = match *self.as_mono_item() {
MonoItem::Fn(ref instance) => instance.def_id(),
MonoItem::Static(node_id) => tcx.hir.local_def_id(node_id),
MonoItem::Static(def_id) => def_id,
MonoItem::GlobalAsm(..) => return None,
};

Expand Down Expand Up @@ -209,7 +208,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
debug!("is_instantiable({:?})", self);
let (def_id, substs) = match *self.as_mono_item() {
MonoItem::Fn(ref instance) => (instance.def_id(), instance.substs),
MonoItem::Static(node_id) => (tcx.hir.local_def_id(node_id), Substs::empty()),
MonoItem::Static(def_id) => (def_id, Substs::empty()),
// global asm never has predicates
MonoItem::GlobalAsm(..) => return true
};
Expand All @@ -218,14 +217,11 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
}

fn to_string(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> String {
let hir_map = &tcx.hir;

return match *self.as_mono_item() {
MonoItem::Fn(instance) => {
to_string_internal(tcx, "fn ", instance)
},
MonoItem::Static(node_id) => {
let def_id = hir_map.local_def_id(node_id);
MonoItem::Static(def_id) => {
let instance = Instance::new(def_id, tcx.intern_substs(&[]));
to_string_internal(tcx, "static ", instance)
},
Expand All @@ -251,7 +247,9 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
MonoItem::Fn(Instance { def, .. }) => {
tcx.hir.as_local_node_id(def.def_id())
}
MonoItem::Static(node_id) |
MonoItem::Static(def_id) => {
tcx.hir.as_local_node_id(def_id)
}
MonoItem::GlobalAsm(node_id) => {
Some(node_id)
}
Expand Down
16 changes: 13 additions & 3 deletions src/librustc_mir/monomorphize/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ pub trait CodegenUnitExt<'tcx> {
}
}
}
MonoItem::Static(node_id) |
MonoItem::Static(def_id) => {
tcx.hir.as_local_node_id(def_id)
}
MonoItem::GlobalAsm(node_id) => {
Some(node_id)
}
Expand Down Expand Up @@ -382,7 +384,15 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
};
(Linkage::External, visibility)
}
MonoItem::Static(node_id) |
MonoItem::Static(def_id) => {
let visibility = if tcx.is_exported_symbol(def_id) {
can_be_internalized = false;
default_visibility(def_id)
} else {
Visibility::Hidden
};
(Linkage::External, visibility)
}
MonoItem::GlobalAsm(node_id) => {
let def_id = tcx.hir.local_def_id(node_id);
let visibility = if tcx.is_exported_symbol(def_id) {
Expand Down Expand Up @@ -643,7 +653,7 @@ fn characteristic_def_id_of_trans_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

Some(def_id)
}
MonoItem::Static(node_id) |
MonoItem::Static(def_id) => Some(def_id),
MonoItem::GlobalAsm(node_id) => Some(tcx.hir.local_def_id(node_id)),
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ fn collect_and_partition_translation_items<'a, 'tcx>(
let translation_items: DefIdSet = items.iter().filter_map(|trans_item| {
match *trans_item {
MonoItem::Fn(ref instance) => Some(instance.def_id()),
MonoItem::Static(def_id) => Some(def_id),
_ => None,
}
}).collect();
Expand Down Expand Up @@ -1107,7 +1108,7 @@ impl CrateInfo {
}
}

fn is_translated_function(tcx: TyCtxt, id: DefId) -> bool {
fn is_translated_item(tcx: TyCtxt, id: DefId) -> bool {
let (all_trans_items, _) =
tcx.collect_and_partition_translation_items(LOCAL_CRATE);
all_trans_items.contains(&id)
Expand Down Expand Up @@ -1222,7 +1223,7 @@ pub fn provide(providers: &mut Providers) {
providers.collect_and_partition_translation_items =
collect_and_partition_translation_items;

providers.is_translated_function = is_translated_function;
providers.is_translated_item = is_translated_item;

providers.codegen_unit = |tcx, name| {
let (_, all) = tcx.collect_and_partition_translation_items(LOCAL_CRATE);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn get_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
unsafe {
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);

if cx.tcx.is_translated_function(instance_def_id) {
if cx.tcx.is_translated_item(instance_def_id) {
if instance_def_id.is_local() {
if !cx.tcx.is_exported_symbol(instance_def_id) {
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
Expand Down
Loading

0 comments on commit 2483d84

Please sign in to comment.