2016-03-24 11:40:49 -04:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Partitioning Codegen Units for Incremental Compilation
|
|
|
|
//! ======================================================
|
|
|
|
//!
|
2018-05-08 16:10:16 +03:00
|
|
|
//! The task of this module is to take the complete set of monomorphizations of
|
2016-03-24 11:40:49 -04:00
|
|
|
//! a crate and produce a set of codegen units from it, where a codegen unit
|
2018-05-08 16:10:16 +03:00
|
|
|
//! is a named set of (mono-item, linkage) pairs. That is, this module
|
|
|
|
//! decides which monomorphization appears in which codegen units with which
|
2016-03-24 11:40:49 -04:00
|
|
|
//! linkage. The following paragraphs describe some of the background on the
|
|
|
|
//! partitioning scheme.
|
|
|
|
//!
|
|
|
|
//! The most important opportunity for saving on compilation time with
|
2018-05-08 16:10:16 +03:00
|
|
|
//! incremental compilation is to avoid re-codegenning and re-optimizing code.
|
|
|
|
//! Since the unit of codegen and optimization for LLVM is "modules" or, how
|
2016-03-24 11:40:49 -04:00
|
|
|
//! we call them "codegen units", the particulars of how much time can be saved
|
|
|
|
//! by incremental compilation are tightly linked to how the output program is
|
|
|
|
//! partitioned into these codegen units prior to passing it to LLVM --
|
|
|
|
//! especially because we have to treat codegen units as opaque entities once
|
|
|
|
//! they are created: There is no way for us to incrementally update an existing
|
|
|
|
//! LLVM module and so we have to build any such module from scratch if it was
|
|
|
|
//! affected by some change in the source code.
|
|
|
|
//!
|
|
|
|
//! From that point of view it would make sense to maximize the number of
|
|
|
|
//! codegen units by, for example, putting each function into its own module.
|
|
|
|
//! That way only those modules would have to be re-compiled that were actually
|
|
|
|
//! affected by some change, minimizing the number of functions that could have
|
|
|
|
//! been re-used but just happened to be located in a module that is
|
|
|
|
//! re-compiled.
|
|
|
|
//!
|
|
|
|
//! However, since LLVM optimization does not work across module boundaries,
|
|
|
|
//! using such a highly granular partitioning would lead to very slow runtime
|
|
|
|
//! code since it would effectively prohibit inlining and other inter-procedure
|
|
|
|
//! optimizations. We want to avoid that as much as possible.
|
|
|
|
//!
|
|
|
|
//! Thus we end up with a trade-off: The bigger the codegen units, the better
|
|
|
|
//! LLVM's optimizer can do its work, but also the smaller the compilation time
|
|
|
|
//! reduction we get from incremental compilation.
|
|
|
|
//!
|
|
|
|
//! Ideally, we would create a partitioning such that there are few big codegen
|
|
|
|
//! units with few interdependencies between them. For now though, we use the
|
|
|
|
//! following heuristic to determine the partitioning:
|
|
|
|
//!
|
|
|
|
//! - There are two codegen units for every source-level module:
|
|
|
|
//! - One for "stable", that is non-generic, code
|
|
|
|
//! - One for more "volatile" code, i.e. monomorphized instances of functions
|
|
|
|
//! defined in that module
|
|
|
|
//!
|
|
|
|
//! In order to see why this heuristic makes sense, let's take a look at when a
|
|
|
|
//! codegen unit can get invalidated:
|
|
|
|
//!
|
|
|
|
//! 1. The most straightforward case is when the BODY of a function or global
|
|
|
|
//! changes. Then any codegen unit containing the code for that item has to be
|
|
|
|
//! re-compiled. Note that this includes all codegen units where the function
|
|
|
|
//! has been inlined.
|
|
|
|
//!
|
|
|
|
//! 2. The next case is when the SIGNATURE of a function or global changes. In
|
|
|
|
//! this case, all codegen units containing a REFERENCE to that item have to be
|
|
|
|
//! re-compiled. This is a superset of case 1.
|
|
|
|
//!
|
|
|
|
//! 3. The final and most subtle case is when a REFERENCE to a generic function
|
|
|
|
//! is added or removed somewhere. Even though the definition of the function
|
|
|
|
//! might be unchanged, a new REFERENCE might introduce a new monomorphized
|
|
|
|
//! instance of this function which has to be placed and compiled somewhere.
|
|
|
|
//! Conversely, when removing a REFERENCE, it might have been the last one with
|
|
|
|
//! that particular set of generic arguments and thus we have to remove it.
|
|
|
|
//!
|
|
|
|
//! From the above we see that just using one codegen unit per source-level
|
|
|
|
//! module is not such a good idea, since just adding a REFERENCE to some
|
|
|
|
//! generic item somewhere else would invalidate everything within the module
|
|
|
|
//! containing the generic item. The heuristic above reduces this detrimental
|
|
|
|
//! side-effect of references a little by at least not touching the non-generic
|
|
|
|
//! code of the module.
|
|
|
|
//!
|
|
|
|
//! A Note on Inlining
|
|
|
|
//! ------------------
|
|
|
|
//! As briefly mentioned above, in order for LLVM to be able to inline a
|
|
|
|
//! function call, the body of the function has to be available in the LLVM
|
|
|
|
//! module where the call is made. This has a few consequences for partitioning:
|
|
|
|
//!
|
|
|
|
//! - The partitioning algorithm has to take care of placing functions into all
|
|
|
|
//! codegen units where they should be available for inlining. It also has to
|
|
|
|
//! decide on the correct linkage for these functions.
|
|
|
|
//!
|
|
|
|
//! - The partitioning algorithm has to know which functions are likely to get
|
|
|
|
//! inlined, so it can distribute function instantiations accordingly. Since
|
|
|
|
//! there is no way of knowing for sure which functions LLVM will decide to
|
|
|
|
//! inline in the end, we apply a heuristic here: Only functions marked with
|
2017-12-31 17:17:01 +01:00
|
|
|
//! `#[inline]` are considered for inlining by the partitioner. The current
|
2017-01-09 09:54:54 -05:00
|
|
|
//! implementation will not try to determine if a function is likely to be
|
|
|
|
//! inlined by looking at the functions definition.
|
2016-03-24 11:40:49 -04:00
|
|
|
//!
|
|
|
|
//! Note though that as a side-effect of creating a codegen units per
|
|
|
|
//! source-level module, functions from the same module will be available for
|
|
|
|
//! inlining, even when they are not marked #[inline].
|
|
|
|
|
2017-11-23 16:02:02 +01:00
|
|
|
use monomorphize::collector::InliningMap;
|
2018-08-20 17:13:01 +02:00
|
|
|
use rustc::dep_graph::{WorkProductId, WorkProduct, DepNode, DepConstructor};
|
2018-08-02 12:30:43 -07:00
|
|
|
use rustc::hir::CodegenFnAttrFlags;
|
2018-08-14 17:55:22 +02:00
|
|
|
use rustc::hir::def_id::{DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
|
2016-03-24 11:40:49 -04:00
|
|
|
use rustc::hir::map::DefPathData;
|
2018-08-14 17:55:22 +02:00
|
|
|
use rustc::mir::mono::{Linkage, Visibility, CodegenUnitNameBuilder};
|
2018-01-30 11:54:07 -08:00
|
|
|
use rustc::middle::exported_symbols::SymbolExportLevel;
|
2017-07-12 17:37:58 +02:00
|
|
|
use rustc::ty::{self, TyCtxt, InstanceDef};
|
2016-03-24 11:40:49 -04:00
|
|
|
use rustc::ty::item_path::characteristic_def_id_of_type;
|
2017-07-12 17:37:58 +02:00
|
|
|
use rustc::util::nodemap::{FxHashMap, FxHashSet};
|
2018-01-15 18:28:34 +00:00
|
|
|
use std::collections::hash_map::Entry;
|
2018-03-31 21:42:35 +01:00
|
|
|
use std::cmp;
|
2016-05-26 13:04:35 -04:00
|
|
|
use syntax::ast::NodeId;
|
2018-07-10 19:16:22 +02:00
|
|
|
use syntax::symbol::InternedString;
|
2017-11-23 16:02:02 +01:00
|
|
|
use rustc::mir::mono::MonoItem;
|
|
|
|
use monomorphize::item::{MonoItemExt, InstantiationMode};
|
2017-09-12 11:04:46 -07:00
|
|
|
|
2017-10-27 10:50:39 +02:00
|
|
|
pub use rustc::mir::mono::CodegenUnit;
|
2016-03-24 11:40:49 -04:00
|
|
|
|
2016-04-21 16:45:33 -04:00
|
|
|
pub enum PartitioningStrategy {
|
2016-04-22 14:07:23 -04:00
|
|
|
/// Generate one codegen unit per source-level module.
|
2016-04-21 16:45:33 -04:00
|
|
|
PerModule,
|
2016-04-22 14:07:23 -04:00
|
|
|
|
|
|
|
/// Partition the whole crate into a fixed number of codegen units.
|
2016-04-21 16:45:33 -04:00
|
|
|
FixedUnitCount(usize)
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
pub trait CodegenUnitExt<'tcx> {
|
|
|
|
fn as_codegen_unit(&self) -> &CodegenUnit<'tcx>;
|
2016-07-21 12:49:59 -04:00
|
|
|
|
2017-10-25 17:04:24 +02:00
|
|
|
fn contains_item(&self, item: &MonoItem<'tcx>) -> bool {
|
2017-09-12 11:04:46 -07:00
|
|
|
self.items().contains_key(item)
|
2016-07-21 12:49:59 -04:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
fn name<'a>(&'a self) -> &'a InternedString
|
|
|
|
where 'tcx: 'a,
|
|
|
|
{
|
|
|
|
&self.as_codegen_unit().name()
|
2016-07-21 12:49:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 17:04:24 +02:00
|
|
|
fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
|
2017-09-12 11:04:46 -07:00
|
|
|
&self.as_codegen_unit().items()
|
2016-07-21 12:49:59 -04:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
fn work_product_id(&self) -> WorkProductId {
|
2018-04-11 23:02:41 +02:00
|
|
|
WorkProductId::from_cgu_name(&self.name().as_str())
|
2016-07-21 12:49:59 -04:00
|
|
|
}
|
|
|
|
|
2018-08-20 17:13:01 +02:00
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
fn items_in_deterministic_order<'a>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>)
|
2017-10-25 17:04:24 +02:00
|
|
|
-> Vec<(MonoItem<'tcx>,
|
|
|
|
(Linkage, Visibility))> {
|
2016-05-26 13:04:35 -04:00
|
|
|
// 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
|
2017-04-24 19:35:47 +03:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct ItemSortKey(Option<NodeId>, ty::SymbolName);
|
2016-05-26 13:04:35 -04:00
|
|
|
|
2017-04-24 19:35:47 +03:00
|
|
|
fn item_sort_key<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-10-25 17:04:24 +02:00
|
|
|
item: MonoItem<'tcx>) -> ItemSortKey {
|
2017-04-24 19:35:47 +03:00
|
|
|
ItemSortKey(match item {
|
2017-10-25 17:04:24 +02:00
|
|
|
MonoItem::Fn(ref instance) => {
|
2017-10-10 17:11:08 +02:00
|
|
|
match instance.def {
|
|
|
|
// We only want to take NodeIds 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_node_id(def_id)
|
|
|
|
}
|
2018-09-10 22:54:48 +09:00
|
|
|
InstanceDef::VtableShim(..) |
|
2017-10-10 17:11:08 +02:00
|
|
|
InstanceDef::Intrinsic(..) |
|
|
|
|
InstanceDef::FnPtrShim(..) |
|
|
|
|
InstanceDef::Virtual(..) |
|
|
|
|
InstanceDef::ClosureOnceShim { .. } |
|
|
|
|
InstanceDef::DropGlue(..) |
|
|
|
|
InstanceDef::CloneShim(..) => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 13:04:35 -04:00
|
|
|
}
|
2018-07-13 11:30:47 -07:00
|
|
|
MonoItem::Static(def_id) => {
|
2018-02-19 13:29:22 +01:00
|
|
|
tcx.hir.as_local_node_id(def_id)
|
|
|
|
}
|
2017-10-25 17:04:24 +02:00
|
|
|
MonoItem::GlobalAsm(node_id) => {
|
2017-03-21 10:03:52 -05:00
|
|
|
Some(node_id)
|
|
|
|
}
|
2017-04-24 19:35:47 +03:00
|
|
|
}, item.symbol_name(tcx))
|
2016-05-26 13:04:35 -04:00
|
|
|
}
|
2017-04-24 19:35:47 +03:00
|
|
|
|
2018-04-01 13:28:47 +01:00
|
|
|
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
|
2016-05-26 11:43:53 -04:00
|
|
|
}
|
2018-08-20 13:42:07 +02:00
|
|
|
|
|
|
|
fn codegen_dep_node(&self, tcx: TyCtxt<'_, 'tcx, 'tcx>) -> DepNode {
|
|
|
|
DepNode::new(tcx, DepConstructor::CompileCodegenUnit(self.name().clone()))
|
|
|
|
}
|
2016-05-26 11:43:53 -04:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
impl<'tcx> CodegenUnitExt<'tcx> for CodegenUnit<'tcx> {
|
|
|
|
fn as_codegen_unit(&self) -> &CodegenUnit<'tcx> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 11:43:53 -04:00
|
|
|
|
2016-03-24 11:40:49 -04:00
|
|
|
// Anything we can't find a proper codegen unit for goes into this.
|
2018-08-14 17:55:22 +02:00
|
|
|
fn fallback_cgu_name(name_builder: &mut CodegenUnitNameBuilder) -> InternedString {
|
|
|
|
name_builder.build_cgu_name(LOCAL_CRATE, &["fallback"], Some("cgu"))
|
2018-01-08 12:30:52 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 08:32:50 -07:00
|
|
|
pub fn partition<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_items: I,
|
2016-05-03 04:56:42 +03:00
|
|
|
strategy: PartitioningStrategy,
|
2017-09-13 13:22:20 -07:00
|
|
|
inlining_map: &InliningMap<'tcx>)
|
2016-05-03 04:56:42 +03:00
|
|
|
-> Vec<CodegenUnit<'tcx>>
|
2017-10-25 17:04:24 +02:00
|
|
|
where I: Iterator<Item = MonoItem<'tcx>>
|
2016-03-24 11:40:49 -04:00
|
|
|
{
|
2018-05-08 16:10:16 +03:00
|
|
|
// In the first step, we place all regular monomorphizations into their
|
|
|
|
// respective 'home' codegen unit. Regular monomorphizations are all
|
2016-03-24 11:40:49 -04:00
|
|
|
// functions and statics defined in the local crate.
|
2018-07-10 19:16:22 +02:00
|
|
|
let mut initial_partitioning = place_root_mono_items(tcx, mono_items);
|
2016-04-21 16:45:33 -04:00
|
|
|
|
2018-01-15 18:28:34 +00:00
|
|
|
initial_partitioning.codegen_units.iter_mut().for_each(|cgu| cgu.estimate_size(&tcx));
|
|
|
|
|
2017-07-23 22:06:16 +07:00
|
|
|
debug_dump(tcx, "INITIAL PARTITIONING:", initial_partitioning.codegen_units.iter());
|
2016-05-09 23:56:49 -04:00
|
|
|
|
2016-04-22 14:07:23 -04:00
|
|
|
// If the partitioning should produce a fixed count of codegen units, merge
|
|
|
|
// until that count is reached.
|
2016-04-21 16:45:33 -04:00
|
|
|
if let PartitioningStrategy::FixedUnitCount(count) = strategy {
|
2018-07-10 19:16:22 +02:00
|
|
|
merge_codegen_units(tcx, &mut initial_partitioning, count);
|
2016-05-09 23:56:49 -04:00
|
|
|
|
2017-04-14 15:30:06 -04:00
|
|
|
debug_dump(tcx, "POST MERGING:", initial_partitioning.codegen_units.iter());
|
2016-04-21 16:45:33 -04:00
|
|
|
}
|
2016-03-24 11:40:49 -04:00
|
|
|
|
2017-07-23 22:06:16 +07:00
|
|
|
// In the next step, we use the inlining map to determine which additional
|
2018-05-08 16:10:16 +03:00
|
|
|
// monomorphizations have to go into each codegen unit. These additional
|
|
|
|
// monomorphizations can be drop-glue, functions from external crates, and
|
2016-03-24 11:40:49 -04:00
|
|
|
// local functions the definition of which is marked with #[inline].
|
2018-05-08 16:10:16 +03:00
|
|
|
let mut post_inlining = place_inlined_mono_items(initial_partitioning,
|
2017-07-12 17:37:58 +02:00
|
|
|
inlining_map);
|
|
|
|
|
2018-01-15 18:28:34 +00:00
|
|
|
post_inlining.codegen_units.iter_mut().for_each(|cgu| cgu.estimate_size(&tcx));
|
|
|
|
|
2017-07-12 17:37:58 +02:00
|
|
|
debug_dump(tcx, "POST INLINING:", post_inlining.codegen_units.iter());
|
2016-05-09 23:56:49 -04:00
|
|
|
|
2017-07-12 17:37:58 +02:00
|
|
|
// Next we try to make as many symbols "internal" as possible, so LLVM has
|
|
|
|
// more freedom to optimize.
|
2017-12-21 17:49:24 +01:00
|
|
|
if !tcx.sess.opts.cg.link_dead_code {
|
|
|
|
internalize_symbols(tcx, &mut post_inlining, inlining_map);
|
|
|
|
}
|
2016-05-09 23:56:49 -04:00
|
|
|
|
2016-05-26 11:43:53 -04:00
|
|
|
// Finally, sort by codegen unit name, so that we get deterministic results
|
2017-07-12 17:37:58 +02:00
|
|
|
let PostInliningPartitioning {
|
|
|
|
codegen_units: mut result,
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_item_placements: _,
|
2017-07-12 17:37:58 +02:00
|
|
|
internalization_candidates: _,
|
|
|
|
} = post_inlining;
|
|
|
|
|
2016-06-02 12:28:29 -04:00
|
|
|
result.sort_by(|cgu1, cgu2| {
|
2017-09-12 11:04:46 -07:00
|
|
|
cgu1.name().cmp(cgu2.name())
|
2016-05-26 11:43:53 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
result
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
|
2016-04-22 14:07:23 -04:00
|
|
|
struct PreInliningPartitioning<'tcx> {
|
2016-03-24 11:40:49 -04:00
|
|
|
codegen_units: Vec<CodegenUnit<'tcx>>,
|
2017-10-25 17:04:24 +02:00
|
|
|
roots: FxHashSet<MonoItem<'tcx>>,
|
|
|
|
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
/// For symbol internalization, we need to know whether a symbol/mono-item is
|
2017-07-12 17:37:58 +02:00
|
|
|
/// accessed from outside the codegen unit it is defined in. This type is used
|
|
|
|
/// to keep track of that.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2018-05-08 16:10:16 +03:00
|
|
|
enum MonoItemPlacement {
|
2017-07-12 17:37:58 +02:00
|
|
|
SingleCgu { cgu_name: InternedString },
|
|
|
|
MultipleCgus,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PostInliningPartitioning<'tcx> {
|
|
|
|
codegen_units: Vec<CodegenUnit<'tcx>>,
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_item_placements: FxHashMap<MonoItem<'tcx>, MonoItemPlacement>,
|
2017-10-25 17:04:24 +02:00
|
|
|
internalization_candidates: FxHashSet<MonoItem<'tcx>>,
|
2017-07-12 17:37:58 +02:00
|
|
|
}
|
2016-04-22 14:07:23 -04:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn place_root_mono_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mono_items: I)
|
2016-05-03 04:56:42 +03:00
|
|
|
-> PreInliningPartitioning<'tcx>
|
2017-10-25 17:04:24 +02:00
|
|
|
where I: Iterator<Item = MonoItem<'tcx>>
|
2016-03-24 11:40:49 -04:00
|
|
|
{
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut roots = FxHashSet::default();
|
|
|
|
let mut codegen_units = FxHashMap::default();
|
2017-01-09 09:52:08 -05:00
|
|
|
let is_incremental_build = tcx.sess.opts.incremental.is_some();
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut internalization_candidates = FxHashSet::default();
|
2018-03-14 16:45:06 +01:00
|
|
|
|
2018-08-02 12:30:43 -07:00
|
|
|
// Determine if monomorphizations instantiated in this crate will be made
|
|
|
|
// available to downstream crates. This depends on whether we are in
|
|
|
|
// share-generics mode and whether the current crate can even have
|
|
|
|
// downstream crates.
|
|
|
|
let export_generics = tcx.sess.opts.share_generics() &&
|
|
|
|
tcx.local_crate_exports_generics();
|
|
|
|
|
2018-08-14 17:55:22 +02:00
|
|
|
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
|
2018-10-16 10:44:26 +02:00
|
|
|
let cgu_name_cache = &mut FxHashMap::default();
|
2018-08-14 17:55:22 +02:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
for mono_item in mono_items {
|
|
|
|
match mono_item.instantiation_mode(tcx) {
|
2017-10-06 14:59:33 -07:00
|
|
|
InstantiationMode::GloballyShared { .. } => {}
|
|
|
|
InstantiationMode::LocalCopy => continue,
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
let characteristic_def_id = characteristic_def_id_of_mono_item(tcx, mono_item);
|
2017-10-06 14:59:33 -07:00
|
|
|
let is_volatile = is_incremental_build &&
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_item.is_generic_fn();
|
2017-10-06 14:59:33 -07:00
|
|
|
|
|
|
|
let codegen_unit_name = match characteristic_def_id {
|
2018-08-14 17:55:22 +02:00
|
|
|
Some(def_id) => compute_codegen_unit_name(tcx,
|
|
|
|
cgu_name_builder,
|
|
|
|
def_id,
|
|
|
|
is_volatile,
|
|
|
|
cgu_name_cache),
|
|
|
|
None => fallback_cgu_name(cgu_name_builder),
|
2017-10-06 14:59:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let codegen_unit = codegen_units.entry(codegen_unit_name.clone())
|
2018-08-02 12:06:57 -07:00
|
|
|
.or_insert_with(|| CodegenUnit::new(codegen_unit_name.clone()));
|
2017-10-06 14:59:33 -07:00
|
|
|
|
2017-12-26 14:26:03 -08:00
|
|
|
let mut can_be_internalized = true;
|
2018-08-02 12:06:57 -07:00
|
|
|
let (linkage, visibility) = mono_item_linkage_and_visibility(
|
|
|
|
tcx,
|
|
|
|
&mono_item,
|
|
|
|
&mut can_be_internalized,
|
2018-08-02 12:30:43 -07:00
|
|
|
export_generics,
|
2018-08-02 12:06:57 -07:00
|
|
|
);
|
2017-12-26 14:26:03 -08:00
|
|
|
if visibility == Visibility::Hidden && can_be_internalized {
|
2018-05-08 16:10:16 +03:00
|
|
|
internalization_candidates.insert(mono_item);
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
2017-10-06 14:59:33 -07:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
codegen_unit.items_mut().insert(mono_item, (linkage, visibility));
|
|
|
|
roots.insert(mono_item);
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 15:08:07 -04:00
|
|
|
// always ensure we have at least one CGU; otherwise, if we have a
|
2016-05-18 20:19:07 -04:00
|
|
|
// crate with just types (for example), we could wind up with no CGU
|
|
|
|
if codegen_units.is_empty() {
|
2018-08-14 17:55:22 +02:00
|
|
|
let codegen_unit_name = fallback_cgu_name(cgu_name_builder);
|
2017-07-12 17:37:58 +02:00
|
|
|
codegen_units.insert(codegen_unit_name.clone(),
|
2017-09-12 11:04:46 -07:00
|
|
|
CodegenUnit::new(codegen_unit_name.clone()));
|
2016-05-18 20:19:07 -04:00
|
|
|
}
|
|
|
|
|
2016-04-22 14:07:23 -04:00
|
|
|
PreInliningPartitioning {
|
2016-03-24 11:40:49 -04:00
|
|
|
codegen_units: codegen_units.into_iter()
|
|
|
|
.map(|(_, codegen_unit)| codegen_unit)
|
|
|
|
.collect(),
|
2017-07-12 17:37:58 +02:00
|
|
|
roots,
|
|
|
|
internalization_candidates,
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:06:57 -07:00
|
|
|
fn mono_item_linkage_and_visibility(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mono_item: &MonoItem<'tcx>,
|
|
|
|
can_be_internalized: &mut bool,
|
2018-08-02 12:30:43 -07:00
|
|
|
export_generics: bool,
|
2018-08-02 12:06:57 -07:00
|
|
|
) -> (Linkage, Visibility) {
|
|
|
|
if let Some(explicit_linkage) = mono_item.explicit_linkage(tcx) {
|
|
|
|
return (explicit_linkage, Visibility::Default)
|
|
|
|
}
|
2018-08-02 12:30:43 -07:00
|
|
|
let vis = mono_item_visibility(
|
|
|
|
tcx,
|
|
|
|
mono_item,
|
|
|
|
can_be_internalized,
|
|
|
|
export_generics,
|
|
|
|
);
|
2018-08-02 12:06:57 -07:00
|
|
|
(Linkage::External, vis)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mono_item_visibility(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mono_item: &MonoItem<'tcx>,
|
|
|
|
can_be_internalized: &mut bool,
|
2018-08-02 12:30:43 -07:00
|
|
|
export_generics: bool,
|
2018-08-02 12:06:57 -07:00
|
|
|
) -> Visibility {
|
|
|
|
let instance = match mono_item {
|
|
|
|
// This is pretty complicated, go below
|
|
|
|
MonoItem::Fn(instance) => instance,
|
|
|
|
|
|
|
|
// Misc handling for generics and such, but otherwise
|
|
|
|
MonoItem::Static(def_id) => {
|
|
|
|
return if tcx.is_reachable_non_generic(*def_id) {
|
|
|
|
*can_be_internalized = false;
|
2018-08-02 12:30:43 -07:00
|
|
|
default_visibility(tcx, *def_id, false)
|
2018-08-02 12:06:57 -07:00
|
|
|
} else {
|
|
|
|
Visibility::Hidden
|
|
|
|
};
|
|
|
|
}
|
|
|
|
MonoItem::GlobalAsm(node_id) => {
|
|
|
|
let def_id = tcx.hir.local_def_id(*node_id);
|
|
|
|
return if tcx.is_reachable_non_generic(def_id) {
|
|
|
|
*can_be_internalized = false;
|
2018-08-02 12:30:43 -07:00
|
|
|
default_visibility(tcx, def_id, false)
|
2018-08-02 12:06:57 -07:00
|
|
|
} else {
|
|
|
|
Visibility::Hidden
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let def_id = match instance.def {
|
|
|
|
InstanceDef::Item(def_id) => def_id,
|
|
|
|
|
|
|
|
// These are all compiler glue and such, never exported, always hidden.
|
2018-09-10 22:54:48 +09:00
|
|
|
InstanceDef::VtableShim(..) |
|
2018-08-02 12:06:57 -07:00
|
|
|
InstanceDef::FnPtrShim(..) |
|
|
|
|
InstanceDef::Virtual(..) |
|
|
|
|
InstanceDef::Intrinsic(..) |
|
|
|
|
InstanceDef::ClosureOnceShim { .. } |
|
|
|
|
InstanceDef::DropGlue(..) |
|
|
|
|
InstanceDef::CloneShim(..) => {
|
|
|
|
return Visibility::Hidden
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The `start_fn` lang item is actually a monomorphized instance of a
|
|
|
|
// function in the standard library, used for the `main` function. We don't
|
|
|
|
// want to export it so we tag it with `Hidden` visibility but this symbol
|
|
|
|
// is only referenced from the actual `main` symbol which we unfortunately
|
|
|
|
// don't know anything about during partitioning/collection. As a result we
|
|
|
|
// forcibly keep this symbol out of the `internalization_candidates` set.
|
|
|
|
//
|
|
|
|
// FIXME: eventually we don't want to always force this symbol to have
|
|
|
|
// hidden visibility, it should indeed be a candidate for
|
|
|
|
// internalization, but we have to understand that it's referenced
|
|
|
|
// from the `main` symbol we'll generate later.
|
2018-08-02 12:30:43 -07:00
|
|
|
//
|
|
|
|
// This may be fixable with a new `InstanceDef` perhaps? Unsure!
|
2018-08-02 12:06:57 -07:00
|
|
|
if tcx.lang_items().start_fn() == Some(def_id) {
|
|
|
|
*can_be_internalized = false;
|
|
|
|
return Visibility::Hidden
|
|
|
|
}
|
|
|
|
|
|
|
|
let is_generic = instance.substs.types().next().is_some();
|
|
|
|
|
|
|
|
// Upstream `DefId` instances get different handling than local ones
|
|
|
|
if !def_id.is_local() {
|
|
|
|
return if export_generics && is_generic {
|
|
|
|
// If it is a upstream monomorphization
|
|
|
|
// and we export generics, we must make
|
|
|
|
// it available to downstream crates.
|
|
|
|
*can_be_internalized = false;
|
2018-08-02 12:30:43 -07:00
|
|
|
default_visibility(tcx, def_id, true)
|
2018-08-02 12:06:57 -07:00
|
|
|
} else {
|
|
|
|
Visibility::Hidden
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_generic {
|
|
|
|
if export_generics {
|
|
|
|
if tcx.is_unreachable_local_definition(def_id) {
|
|
|
|
// This instance cannot be used
|
|
|
|
// from another crate.
|
|
|
|
Visibility::Hidden
|
|
|
|
} else {
|
|
|
|
// This instance might be useful in
|
|
|
|
// a downstream crate.
|
|
|
|
*can_be_internalized = false;
|
2018-08-02 12:30:43 -07:00
|
|
|
default_visibility(tcx, def_id, true)
|
2018-08-02 12:06:57 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We are not exporting generics or
|
|
|
|
// the definition is not reachable
|
|
|
|
// for downstream crates, we can
|
|
|
|
// internalize its instantiations.
|
|
|
|
Visibility::Hidden
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-02 12:30:43 -07:00
|
|
|
|
|
|
|
// If this isn't a generic function then we mark this a `Default` if
|
|
|
|
// this is a reachable item, meaning that it's a symbol other crates may
|
|
|
|
// access when they link to us.
|
2018-08-02 12:06:57 -07:00
|
|
|
if tcx.is_reachable_non_generic(def_id) {
|
|
|
|
*can_be_internalized = false;
|
|
|
|
debug_assert!(!is_generic);
|
2018-08-02 12:30:43 -07:00
|
|
|
return default_visibility(tcx, def_id, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't reachable then we're gonna tag this with `Hidden`
|
|
|
|
// visibility. In some situations though we'll want to prevent this
|
|
|
|
// symbol from being internalized.
|
|
|
|
//
|
|
|
|
// There's two categories of items here:
|
|
|
|
//
|
|
|
|
// * First is weak lang items. These are basically mechanisms for
|
|
|
|
// libcore to forward-reference symbols defined later in crates like
|
|
|
|
// the standard library or `#[panic_implementation]` definitions. The
|
|
|
|
// definition of these weak lang items needs to be referenceable by
|
|
|
|
// libcore, so we're no longer a candidate for internalization.
|
|
|
|
// Removal of these functions can't be done by LLVM but rather must be
|
|
|
|
// done by the linker as it's a non-local decision.
|
|
|
|
//
|
|
|
|
// * Second is "std internal symbols". Currently this is primarily used
|
|
|
|
// for allocator symbols. Allocators are a little weird in their
|
|
|
|
// implementation, but the idea is that the compiler, at the last
|
|
|
|
// minute, defines an allocator with an injected object file. The
|
|
|
|
// `alloc` crate references these symbols (`__rust_alloc`) and the
|
|
|
|
// definition doesn't get hooked up until a linked crate artifact is
|
|
|
|
// generated.
|
|
|
|
//
|
|
|
|
// The symbols synthesized by the compiler (`__rust_alloc`) are thin
|
|
|
|
// veneers around the actual implementation, some other symbol which
|
|
|
|
// implements the same ABI. These symbols (things like `__rg_alloc`,
|
|
|
|
// `__rdl_alloc`, `__rde_alloc`, etc), are all tagged with "std
|
|
|
|
// internal symbols".
|
|
|
|
//
|
|
|
|
// The std-internal symbols here **should not show up in a dll as an
|
|
|
|
// exported interface**, so they return `false` from
|
|
|
|
// `is_reachable_non_generic` above and we'll give them `Hidden`
|
|
|
|
// visibility below. Like the weak lang items, though, we can't let
|
|
|
|
// LLVM internalize them as this decision is left up to the linker to
|
|
|
|
// omit them, so prevent them from being internalized.
|
2018-08-23 00:33:32 -07:00
|
|
|
let attrs = tcx.codegen_fn_attrs(def_id);
|
|
|
|
if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
|
2018-08-02 12:30:43 -07:00
|
|
|
*can_be_internalized = false;
|
2018-08-02 12:06:57 -07:00
|
|
|
}
|
2018-08-02 12:30:43 -07:00
|
|
|
|
|
|
|
Visibility::Hidden
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_visibility(tcx: TyCtxt, id: DefId, is_generic: bool) -> Visibility {
|
|
|
|
if !tcx.sess.target.target.options.default_hidden_visibility {
|
|
|
|
return Visibility::Default
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic functions never have export level C
|
|
|
|
if is_generic {
|
|
|
|
return Visibility::Hidden
|
|
|
|
}
|
|
|
|
|
|
|
|
// Things with export level C don't get instantiated in
|
|
|
|
// downstream crates
|
|
|
|
if !id.is_local() {
|
|
|
|
return Visibility::Hidden
|
|
|
|
}
|
|
|
|
|
|
|
|
// C-export level items remain at `Default`, all other internal
|
|
|
|
// items become `Hidden`
|
|
|
|
match tcx.reachable_non_generics(id.krate).get(&id) {
|
|
|
|
Some(SymbolExportLevel::C) => Visibility::Default,
|
|
|
|
_ => Visibility::Hidden,
|
2018-08-02 12:06:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 19:16:22 +02:00
|
|
|
fn merge_codegen_units<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>,
|
|
|
|
initial_partitioning: &mut PreInliningPartitioning<'tcx>,
|
|
|
|
target_cgu_count: usize) {
|
2016-04-21 16:45:33 -04:00
|
|
|
assert!(target_cgu_count >= 1);
|
|
|
|
let codegen_units = &mut initial_partitioning.codegen_units;
|
|
|
|
|
2017-12-21 09:00:55 -08:00
|
|
|
// Note that at this point in time the `codegen_units` here may not be in a
|
|
|
|
// deterministic order (but we know they're deterministically the same set).
|
|
|
|
// We want this merging to produce a deterministic ordering of codegen units
|
|
|
|
// from the input.
|
|
|
|
//
|
|
|
|
// Due to basically how we've implemented the merging below (merge the two
|
|
|
|
// smallest into each other) we're sure to start off with a deterministic
|
|
|
|
// order (sorted by name). This'll mean that if two cgus have the same size
|
|
|
|
// the stable sort below will keep everything nice and deterministic.
|
|
|
|
codegen_units.sort_by_key(|cgu| cgu.name().clone());
|
|
|
|
|
2016-04-22 14:07:23 -04:00
|
|
|
// Merge the two smallest codegen units until the target size is reached.
|
2016-04-21 16:45:33 -04:00
|
|
|
while codegen_units.len() > target_cgu_count {
|
|
|
|
// Sort small cgus to the back
|
2018-03-31 21:42:35 +01:00
|
|
|
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
|
2017-09-12 11:04:46 -07:00
|
|
|
let mut smallest = codegen_units.pop().unwrap();
|
2016-04-21 16:45:33 -04:00
|
|
|
let second_smallest = codegen_units.last_mut().unwrap();
|
|
|
|
|
2018-01-15 18:28:34 +00:00
|
|
|
second_smallest.modify_size_estimate(smallest.size_estimate());
|
2017-09-12 11:04:46 -07:00
|
|
|
for (k, v) in smallest.items_mut().drain() {
|
|
|
|
second_smallest.items_mut().insert(k, v);
|
2016-04-21 16:45:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:55:22 +02:00
|
|
|
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
|
2016-04-21 16:45:33 -04:00
|
|
|
for (index, cgu) in codegen_units.iter_mut().enumerate() {
|
2018-08-14 17:55:22 +02:00
|
|
|
cgu.set_name(numbered_codegen_unit_name(cgu_name_builder, index));
|
2016-05-06 17:00:59 -04:00
|
|
|
}
|
2016-04-21 16:45:33 -04:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn place_inlined_mono_items<'tcx>(initial_partitioning: PreInliningPartitioning<'tcx>,
|
2018-08-14 17:55:22 +02:00
|
|
|
inlining_map: &InliningMap<'tcx>)
|
|
|
|
-> PostInliningPartitioning<'tcx> {
|
2016-04-22 14:07:23 -04:00
|
|
|
let mut new_partitioning = Vec::new();
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut mono_item_placements = FxHashMap::default();
|
2016-03-24 11:40:49 -04:00
|
|
|
|
2017-07-12 17:37:58 +02:00
|
|
|
let PreInliningPartitioning {
|
|
|
|
codegen_units: initial_cgus,
|
|
|
|
roots,
|
|
|
|
internalization_candidates,
|
|
|
|
} = initial_partitioning;
|
|
|
|
|
|
|
|
let single_codegen_unit = initial_cgus.len() == 1;
|
|
|
|
|
|
|
|
for old_codegen_unit in initial_cgus {
|
2016-03-24 11:40:49 -04:00
|
|
|
// Collect all items that need to be available in this codegen unit
|
2018-10-16 10:44:26 +02:00
|
|
|
let mut reachable = FxHashSet::default();
|
2017-09-12 11:04:46 -07:00
|
|
|
for root in old_codegen_unit.items().keys() {
|
2016-05-09 14:26:15 -04:00
|
|
|
follow_inlining(*root, inlining_map, &mut reachable);
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
let mut new_codegen_unit = CodegenUnit::new(old_codegen_unit.name().clone());
|
2016-03-24 11:40:49 -04:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
// Add all monomorphizations that are not already there
|
|
|
|
for mono_item in reachable {
|
|
|
|
if let Some(linkage) = old_codegen_unit.items().get(&mono_item) {
|
2016-03-24 11:40:49 -04:00
|
|
|
// This is a root, just copy it over
|
2018-05-08 16:10:16 +03:00
|
|
|
new_codegen_unit.items_mut().insert(mono_item, *linkage);
|
2016-03-24 11:40:49 -04:00
|
|
|
} else {
|
2018-05-08 16:10:16 +03:00
|
|
|
if roots.contains(&mono_item) {
|
|
|
|
bug!("GloballyShared mono-item inlined into other CGU: \
|
|
|
|
{:?}", mono_item);
|
2017-01-09 09:52:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a cgu-private copy
|
2017-09-12 11:04:46 -07:00
|
|
|
new_codegen_unit.items_mut().insert(
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_item,
|
2017-09-12 11:04:46 -07:00
|
|
|
(Linkage::Internal, Visibility::Default),
|
|
|
|
);
|
2017-07-12 17:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !single_codegen_unit {
|
|
|
|
// If there is more than one codegen unit, we need to keep track
|
2018-05-08 16:10:16 +03:00
|
|
|
// in which codegen units each monomorphization is placed:
|
|
|
|
match mono_item_placements.entry(mono_item) {
|
2017-07-12 17:37:58 +02:00
|
|
|
Entry::Occupied(e) => {
|
|
|
|
let placement = e.into_mut();
|
|
|
|
debug_assert!(match *placement {
|
2018-05-08 16:10:16 +03:00
|
|
|
MonoItemPlacement::SingleCgu { ref cgu_name } => {
|
2017-09-12 11:04:46 -07:00
|
|
|
*cgu_name != *new_codegen_unit.name()
|
2017-07-12 17:37:58 +02:00
|
|
|
}
|
2018-05-08 16:10:16 +03:00
|
|
|
MonoItemPlacement::MultipleCgus => true,
|
2017-07-12 17:37:58 +02:00
|
|
|
});
|
2018-05-08 16:10:16 +03:00
|
|
|
*placement = MonoItemPlacement::MultipleCgus;
|
2017-07-12 17:37:58 +02:00
|
|
|
}
|
|
|
|
Entry::Vacant(e) => {
|
2018-05-08 16:10:16 +03:00
|
|
|
e.insert(MonoItemPlacement::SingleCgu {
|
2017-09-12 11:04:46 -07:00
|
|
|
cgu_name: new_codegen_unit.name().clone()
|
2017-07-12 17:37:58 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 14:07:23 -04:00
|
|
|
new_partitioning.push(new_codegen_unit);
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
|
2017-07-12 17:37:58 +02:00
|
|
|
return PostInliningPartitioning {
|
|
|
|
codegen_units: new_partitioning,
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_item_placements,
|
2017-07-12 17:37:58 +02:00
|
|
|
internalization_candidates,
|
|
|
|
};
|
2016-03-24 11:40:49 -04:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn follow_inlining<'tcx>(mono_item: MonoItem<'tcx>,
|
2016-05-09 14:26:15 -04:00
|
|
|
inlining_map: &InliningMap<'tcx>,
|
2017-10-25 17:04:24 +02:00
|
|
|
visited: &mut FxHashSet<MonoItem<'tcx>>) {
|
2018-05-08 16:10:16 +03:00
|
|
|
if !visited.insert(mono_item) {
|
2016-03-24 11:40:49 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
inlining_map.with_inlining_candidates(mono_item, |target| {
|
2016-05-09 14:26:15 -04:00
|
|
|
follow_inlining(target, inlining_map, visited);
|
2016-04-22 14:07:23 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 17:37:58 +02:00
|
|
|
fn internalize_symbols<'a, 'tcx>(_tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
partitioning: &mut PostInliningPartitioning<'tcx>,
|
|
|
|
inlining_map: &InliningMap<'tcx>) {
|
|
|
|
if partitioning.codegen_units.len() == 1 {
|
|
|
|
// Fast path for when there is only one codegen unit. In this case we
|
|
|
|
// can internalize all candidates, since there is nowhere else they
|
|
|
|
// could be accessed from.
|
|
|
|
for cgu in &mut partitioning.codegen_units {
|
|
|
|
for candidate in &partitioning.internalization_candidates {
|
2017-09-12 11:04:46 -07:00
|
|
|
cgu.items_mut().insert(*candidate,
|
|
|
|
(Linkage::Internal, Visibility::Default));
|
2017-07-12 17:37:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
// Build a map from every monomorphization to all the monomorphizations that
|
2017-07-12 17:37:58 +02:00
|
|
|
// reference it.
|
2018-10-16 16:57:53 +02:00
|
|
|
let mut accessor_map: FxHashMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>> = Default::default();
|
2017-07-12 17:37:58 +02:00
|
|
|
inlining_map.iter_accesses(|accessor, accessees| {
|
|
|
|
for accessee in accessees {
|
|
|
|
accessor_map.entry(*accessee)
|
2018-07-21 22:43:31 +03:00
|
|
|
.or_default()
|
2017-07-12 17:37:58 +02:00
|
|
|
.push(accessor);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
let mono_item_placements = &partitioning.mono_item_placements;
|
2017-07-12 17:37:58 +02:00
|
|
|
|
|
|
|
// For each internalization candidates in each codegen unit, check if it is
|
|
|
|
// accessed from outside its defining codegen unit.
|
|
|
|
for cgu in &mut partitioning.codegen_units {
|
2018-05-08 16:10:16 +03:00
|
|
|
let home_cgu = MonoItemPlacement::SingleCgu {
|
2017-09-12 11:04:46 -07:00
|
|
|
cgu_name: cgu.name().clone()
|
2017-07-12 17:37:58 +02:00
|
|
|
};
|
|
|
|
|
2017-09-12 11:04:46 -07:00
|
|
|
for (accessee, linkage_and_visibility) in cgu.items_mut() {
|
2017-07-12 17:37:58 +02:00
|
|
|
if !partitioning.internalization_candidates.contains(accessee) {
|
|
|
|
// This item is no candidate for internalizing, so skip it.
|
|
|
|
continue
|
|
|
|
}
|
2018-05-08 16:10:16 +03:00
|
|
|
debug_assert_eq!(mono_item_placements[accessee], home_cgu);
|
2017-07-12 17:37:58 +02:00
|
|
|
|
|
|
|
if let Some(accessors) = accessor_map.get(accessee) {
|
|
|
|
if accessors.iter()
|
|
|
|
.filter_map(|accessor| {
|
|
|
|
// Some accessors might not have been
|
|
|
|
// instantiated. We can safely ignore those.
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_item_placements.get(accessor)
|
2017-07-12 17:37:58 +02:00
|
|
|
})
|
|
|
|
.any(|placement| *placement != home_cgu) {
|
|
|
|
// Found an accessor from another CGU, so skip to the next
|
|
|
|
// item without marking this one as internal.
|
2017-07-13 14:43:56 +02:00
|
|
|
continue
|
2017-07-12 17:37:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got here, we did not find any accesses from other CGUs,
|
2018-05-08 16:10:16 +03:00
|
|
|
// so it's fine to make this monomorphization internal.
|
2017-09-12 11:04:46 -07:00
|
|
|
*linkage_and_visibility = (Linkage::Internal, Visibility::Default);
|
2017-07-12 17:37:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
fn characteristic_def_id_of_mono_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mono_item: MonoItem<'tcx>)
|
2016-05-03 04:56:42 +03:00
|
|
|
-> Option<DefId> {
|
2018-05-08 16:10:16 +03:00
|
|
|
match mono_item {
|
2017-10-25 17:04:24 +02:00
|
|
|
MonoItem::Fn(instance) => {
|
2017-02-08 18:31:03 +01:00
|
|
|
let def_id = match instance.def {
|
|
|
|
ty::InstanceDef::Item(def_id) => def_id,
|
2018-09-10 22:54:48 +09:00
|
|
|
ty::InstanceDef::VtableShim(..) |
|
2017-03-08 01:41:26 +02:00
|
|
|
ty::InstanceDef::FnPtrShim(..) |
|
|
|
|
ty::InstanceDef::ClosureOnceShim { .. } |
|
|
|
|
ty::InstanceDef::Intrinsic(..) |
|
2017-03-14 01:08:21 +02:00
|
|
|
ty::InstanceDef::DropGlue(..) |
|
2017-08-04 14:44:12 +02:00
|
|
|
ty::InstanceDef::Virtual(..) |
|
2017-08-07 16:21:08 +02:00
|
|
|
ty::InstanceDef::CloneShim(..) => return None
|
2017-02-08 18:31:03 +01:00
|
|
|
};
|
|
|
|
|
2016-03-24 11:40:49 -04:00
|
|
|
// If this is a method, we want to put it into the same module as
|
|
|
|
// its self-type. If the self-type does not provide a characteristic
|
|
|
|
// DefId, we use the location of the impl after all.
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
if tcx.trait_of_item(def_id).is_some() {
|
2016-08-18 08:32:50 +03:00
|
|
|
let self_ty = instance.substs.type_at(0);
|
2016-03-24 11:40:49 -04:00
|
|
|
// This is an implementation of a trait method.
|
2017-02-08 18:31:03 +01:00
|
|
|
return characteristic_def_id_of_type(self_ty).or(Some(def_id));
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
|
2016-03-24 11:40:49 -04:00
|
|
|
// This is a method within an inherent impl, find out what the
|
|
|
|
// self-type is:
|
2018-03-09 12:53:17 -05:00
|
|
|
let impl_self_ty = tcx.subst_and_normalize_erasing_regions(
|
|
|
|
instance.substs,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
&tcx.type_of(impl_def_id),
|
|
|
|
);
|
2016-03-24 11:40:49 -04:00
|
|
|
if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
|
|
|
|
return Some(def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
Some(def_id)
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
2018-02-19 13:29:22 +01:00
|
|
|
MonoItem::Static(def_id) => Some(def_id),
|
2017-10-25 17:04:24 +02:00
|
|
|
MonoItem::GlobalAsm(node_id) => Some(tcx.hir.local_def_id(node_id)),
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 17:55:22 +02:00
|
|
|
type CguNameCache = FxHashMap<(DefId, bool), InternedString>;
|
|
|
|
|
|
|
|
fn compute_codegen_unit_name(tcx: TyCtxt,
|
|
|
|
name_builder: &mut CodegenUnitNameBuilder,
|
|
|
|
def_id: DefId,
|
|
|
|
volatile: bool,
|
|
|
|
cache: &mut CguNameCache)
|
|
|
|
-> InternedString {
|
|
|
|
// Find the innermost module that is not nested within a function
|
|
|
|
let mut current_def_id = def_id;
|
|
|
|
let mut cgu_def_id = None;
|
|
|
|
// Walk backwards from the item we want to find the module for:
|
|
|
|
loop {
|
|
|
|
let def_key = tcx.def_key(current_def_id);
|
|
|
|
|
|
|
|
match def_key.disambiguated_data.data {
|
|
|
|
DefPathData::Module(..) => {
|
|
|
|
if cgu_def_id.is_none() {
|
|
|
|
cgu_def_id = Some(current_def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DefPathData::CrateRoot { .. } => {
|
|
|
|
if cgu_def_id.is_none() {
|
|
|
|
// If we have not found a module yet, take the crate root.
|
|
|
|
cgu_def_id = Some(DefId {
|
|
|
|
krate: def_id.krate,
|
|
|
|
index: CRATE_DEF_INDEX,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// If we encounter something that is not a module, throw away
|
|
|
|
// any module that we've found so far because we now know that
|
|
|
|
// it is nested within something else.
|
|
|
|
cgu_def_id = None;
|
|
|
|
}
|
2018-07-10 19:16:22 +02:00
|
|
|
}
|
2016-03-24 11:40:49 -04:00
|
|
|
|
2018-08-14 17:55:22 +02:00
|
|
|
current_def_id.index = def_key.parent.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let cgu_def_id = cgu_def_id.unwrap();
|
|
|
|
|
|
|
|
cache.entry((cgu_def_id, volatile)).or_insert_with(|| {
|
|
|
|
let def_path = tcx.def_path(cgu_def_id);
|
|
|
|
|
|
|
|
let components = def_path
|
|
|
|
.data
|
|
|
|
.iter()
|
|
|
|
.map(|part| part.data.as_interned_str());
|
|
|
|
|
|
|
|
let volatile_suffix = if volatile {
|
|
|
|
Some("volatile")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-01-08 12:30:52 +01:00
|
|
|
|
2018-08-14 17:55:22 +02:00
|
|
|
name_builder.build_cgu_name(def_path.krate, components, volatile_suffix)
|
|
|
|
}).clone()
|
2016-03-24 11:40:49 -04:00
|
|
|
}
|
2016-05-19 12:35:36 -04:00
|
|
|
|
2018-08-14 17:55:22 +02:00
|
|
|
fn numbered_codegen_unit_name(name_builder: &mut CodegenUnitNameBuilder,
|
|
|
|
index: usize)
|
|
|
|
-> InternedString {
|
|
|
|
name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(index))
|
2016-05-19 12:35:36 -04:00
|
|
|
}
|
2016-05-09 23:56:49 -04:00
|
|
|
|
2017-04-14 15:30:06 -04:00
|
|
|
fn debug_dump<'a, 'b, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-09 23:56:49 -04:00
|
|
|
label: &str,
|
|
|
|
cgus: I)
|
|
|
|
where I: Iterator<Item=&'b CodegenUnit<'tcx>>,
|
|
|
|
'tcx: 'a + 'b
|
|
|
|
{
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
debug!("{}", label);
|
|
|
|
for cgu in cgus {
|
2017-09-12 11:04:46 -07:00
|
|
|
debug!("CodegenUnit {}:", cgu.name());
|
2016-05-09 23:56:49 -04:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
for (mono_item, linkage) in cgu.items() {
|
2018-05-26 15:12:38 +03:00
|
|
|
let symbol_name = mono_item.symbol_name(tcx).as_str();
|
2016-10-04 12:02:19 -04:00
|
|
|
let symbol_hash_start = symbol_name.rfind('h');
|
|
|
|
let symbol_hash = symbol_hash_start.map(|i| &symbol_name[i ..])
|
|
|
|
.unwrap_or("<no hash>");
|
|
|
|
|
|
|
|
debug!(" - {} [{:?}] [{}]",
|
2018-05-08 16:10:16 +03:00
|
|
|
mono_item.to_string(tcx),
|
2016-10-04 12:02:19 -04:00
|
|
|
linkage,
|
|
|
|
symbol_hash);
|
2016-05-09 23:56:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|