Skip to content

Commit ac70882

Browse files
committed
Auto merge of #55349 - bjorn3:rustc_mir_collect_and_partition_mono_items, r=oli-obk
Move collect_and_partition_mono_items to rustc_mir Most of the logic of it is inside rustc_mir anyway. Also removes the single function crate rustc_metadata_utils. Based on #55225
2 parents 86b88e6 + 9e479c2 commit ac70882

File tree

20 files changed

+280
-303
lines changed

20 files changed

+280
-303
lines changed

src/Cargo.lock

+3-11
Original file line numberDiff line numberDiff line change
@@ -2141,11 +2141,13 @@ dependencies = [
21412141
"flate2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
21422142
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
21432143
"rustc 0.0.0",
2144+
"rustc_allocator 0.0.0",
21442145
"rustc_data_structures 0.0.0",
21452146
"rustc_incremental 0.0.0",
2146-
"rustc_metadata_utils 0.0.0",
2147+
"rustc_metadata 0.0.0",
21472148
"rustc_mir 0.0.0",
21482149
"rustc_target 0.0.0",
2150+
"serialize 0.0.0",
21492151
"syntax 0.0.0",
21502152
"syntax_pos 0.0.0",
21512153
]
@@ -2289,23 +2291,13 @@ dependencies = [
22892291
"rustc 0.0.0",
22902292
"rustc_data_structures 0.0.0",
22912293
"rustc_errors 0.0.0",
2292-
"rustc_metadata_utils 0.0.0",
22932294
"rustc_target 0.0.0",
22942295
"serialize 0.0.0",
22952296
"syntax 0.0.0",
22962297
"syntax_ext 0.0.0",
22972298
"syntax_pos 0.0.0",
22982299
]
22992300

2300-
[[package]]
2301-
name = "rustc_metadata_utils"
2302-
version = "0.0.0"
2303-
dependencies = [
2304-
"rustc 0.0.0",
2305-
"syntax 0.0.0",
2306-
"syntax_pos 0.0.0",
2307-
]
2308-
23092301
[[package]]
23102302
name = "rustc_mir"
23112303
version = "0.0.0"

src/librustc_codegen_llvm/back/archive.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,6 @@ enum Addition {
5252
},
5353
}
5454

55-
pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session)
56-
-> PathBuf {
57-
// On Windows, static libraries sometimes show up as libfoo.a and other
58-
// times show up as foo.lib
59-
let oslibname = format!("{}{}{}",
60-
sess.target.target.options.staticlib_prefix,
61-
name,
62-
sess.target.target.options.staticlib_suffix);
63-
let unixlibname = format!("lib{}.a", name);
64-
65-
for path in search_paths {
66-
debug!("looking for {} inside {:?}", name, path);
67-
let test = path.join(&oslibname);
68-
if test.exists() { return test }
69-
if oslibname != unixlibname {
70-
let test = path.join(&unixlibname);
71-
if test.exists() { return test }
72-
}
73-
}
74-
sess.fatal(&format!("could not find native static library `{}`, \
75-
perhaps an -L flag is missing?", name));
76-
}
7755

7856
fn is_relevant_child(c: &Child) -> bool {
7957
match c.name() {
@@ -128,7 +106,7 @@ impl<'a> ArchiveBuilder<'a> {
128106
/// Adds all of the contents of a native library to this archive. This will
129107
/// search in the relevant locations for a library named `name`.
130108
pub fn add_native_library(&mut self, name: &str) {
131-
let location = find_library(name, &self.config.lib_search_paths,
109+
let location = ::rustc_codegen_utils::find_library(name, &self.config.lib_search_paths,
132110
self.config.sess);
133111
self.add_archive(&location, |_| false).unwrap_or_else(|e| {
134112
self.config.sess.fatal(&format!("failed to add native library {}: {}",

src/librustc_codegen_llvm/back/link.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use back::wasm;
1212
use cc::windows_registry;
1313
use super::archive::{ArchiveBuilder, ArchiveConfig};
1414
use super::bytecode::RLIB_BYTECODE_EXTENSION;
15-
use super::linker::Linker;
16-
use super::command::Command;
1715
use super::rpath::RPathConfig;
1816
use super::rpath;
1917
use metadata::METADATA_FILENAME;
@@ -31,6 +29,8 @@ use rustc::hir::def_id::CrateNum;
3129
use tempfile::{Builder as TempFileBuilder, TempDir};
3230
use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor};
3331
use rustc_data_structures::fx::FxHashSet;
32+
use rustc_codegen_utils::linker::Linker;
33+
use rustc_codegen_utils::command::Command;
3434
use context::get_reloc_model;
3535
use llvm;
3636

@@ -701,7 +701,8 @@ fn link_natively(sess: &Session,
701701
}
702702

703703
{
704-
let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor);
704+
let target_cpu = ::llvm_util::target_cpu(sess);
705+
let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor, target_cpu);
705706
link_args(&mut *linker, flavor, sess, crate_type, tmpdir,
706707
out_filename, codegen_results);
707708
cmd = linker.finalize();

src/librustc_codegen_llvm/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use back::bytecode::{DecodedBytecode, RLIB_BYTECODE_EXTENSION};
12-
use back::symbol_export;
1312
use back::write::{ModuleConfig, with_llvm_pmb, CodegenContext};
1413
use back::write::{self, DiagnosticHandlers, pre_lto_bitcode_filename};
1514
use errors::{FatalError, Handler};
@@ -24,6 +23,7 @@ use rustc::middle::exported_symbols::SymbolExportLevel;
2423
use rustc::session::config::{self, Lto};
2524
use rustc::util::common::time_ext;
2625
use rustc_data_structures::fx::FxHashMap;
26+
use rustc_codegen_utils::symbol_export;
2727
use time_graph::Timeline;
2828
use {ModuleCodegen, ModuleLlvm, ModuleKind};
2929

src/librustc_codegen_llvm/back/write.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ use attributes;
1212
use back::bytecode::{self, RLIB_BYTECODE_EXTENSION};
1313
use back::lto::{self, ModuleBuffer, ThinBuffer, SerializedModule};
1414
use back::link::{self, get_linker, remove};
15-
use back::command::Command;
16-
use back::linker::LinkerInfo;
17-
use back::symbol_export::ExportedSymbols;
1815
use base;
1916
use consts;
2017
use memmap;
@@ -38,6 +35,9 @@ use rustc::util::common::{time_ext, time_depth, set_time_depth, print_time_passe
3835
use rustc_fs_util::{path2cstr, link_or_copy};
3936
use rustc_data_structures::small_c_str::SmallCStr;
4037
use rustc_data_structures::svh::Svh;
38+
use rustc_codegen_utils::command::Command;
39+
use rustc_codegen_utils::linker::LinkerInfo;
40+
use rustc_codegen_utils::symbol_export::ExportedSymbols;
4141
use errors::{self, Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
4242
use errors::emitter::{Emitter};
4343
use syntax::attr;

src/librustc_codegen_llvm/base.rs

+4-151
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use attributes;
5454
use builder::{Builder, MemFlags};
5555
use callee;
5656
use common::{C_bool, C_bytes_in_context, C_i32, C_usize};
57-
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
5857
use rustc_mir::monomorphize::item::DefPathBasedNames;
5958
use common::{C_struct_in_context, C_array, val_ty};
6059
use consts;
@@ -64,13 +63,13 @@ use declare;
6463
use meth;
6564
use mir;
6665
use monomorphize::Instance;
67-
use monomorphize::partitioning::{self, PartitioningStrategy, CodegenUnit, CodegenUnitExt};
66+
use monomorphize::partitioning::{CodegenUnit, CodegenUnitExt};
6867
use rustc_codegen_utils::symbol_names_test;
6968
use time_graph;
70-
use mono_item::{MonoItem, BaseMonoItemExt, MonoItemExt};
69+
use mono_item::{MonoItem, MonoItemExt};
7170
use type_::Type;
7271
use type_of::LayoutLlvmExt;
73-
use rustc::util::nodemap::{FxHashMap, DefIdSet};
72+
use rustc::util::nodemap::FxHashMap;
7473
use CrateInfo;
7574
use rustc_data_structures::small_c_str::SmallCStr;
7675
use rustc_data_structures::sync::Lrc;
@@ -80,7 +79,6 @@ use std::cmp;
8079
use std::ffi::CString;
8180
use std::i32;
8281
use std::ops::{Deref, DerefMut};
83-
use std::sync::Arc;
8482
use std::sync::mpsc;
8583
use std::time::{Instant, Duration};
8684
use syntax_pos::Span;
@@ -1011,128 +1009,6 @@ fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
10111009
|| rustc_incremental::save_dep_graph(tcx));
10121010
}
10131011

1014-
fn collect_and_partition_mono_items<'a, 'tcx>(
1015-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1016-
cnum: CrateNum,
1017-
) -> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>)
1018-
{
1019-
assert_eq!(cnum, LOCAL_CRATE);
1020-
1021-
let collection_mode = match tcx.sess.opts.debugging_opts.print_mono_items {
1022-
Some(ref s) => {
1023-
let mode_string = s.to_lowercase();
1024-
let mode_string = mode_string.trim();
1025-
if mode_string == "eager" {
1026-
MonoItemCollectionMode::Eager
1027-
} else {
1028-
if mode_string != "lazy" {
1029-
let message = format!("Unknown codegen-item collection mode '{}'. \
1030-
Falling back to 'lazy' mode.",
1031-
mode_string);
1032-
tcx.sess.warn(&message);
1033-
}
1034-
1035-
MonoItemCollectionMode::Lazy
1036-
}
1037-
}
1038-
None => {
1039-
if tcx.sess.opts.cg.link_dead_code {
1040-
MonoItemCollectionMode::Eager
1041-
} else {
1042-
MonoItemCollectionMode::Lazy
1043-
}
1044-
}
1045-
};
1046-
1047-
let (items, inlining_map) =
1048-
time(tcx.sess, "monomorphization collection", || {
1049-
collector::collect_crate_mono_items(tcx, collection_mode)
1050-
});
1051-
1052-
tcx.sess.abort_if_errors();
1053-
1054-
::rustc_mir::monomorphize::assert_symbols_are_distinct(tcx, items.iter());
1055-
1056-
let strategy = if tcx.sess.opts.incremental.is_some() {
1057-
PartitioningStrategy::PerModule
1058-
} else {
1059-
PartitioningStrategy::FixedUnitCount(tcx.sess.codegen_units())
1060-
};
1061-
1062-
let codegen_units = time(tcx.sess, "codegen unit partitioning", || {
1063-
partitioning::partition(tcx,
1064-
items.iter().cloned(),
1065-
strategy,
1066-
&inlining_map)
1067-
.into_iter()
1068-
.map(Arc::new)
1069-
.collect::<Vec<_>>()
1070-
});
1071-
1072-
let mono_items: DefIdSet = items.iter().filter_map(|mono_item| {
1073-
match *mono_item {
1074-
MonoItem::Fn(ref instance) => Some(instance.def_id()),
1075-
MonoItem::Static(def_id) => Some(def_id),
1076-
_ => None,
1077-
}
1078-
}).collect();
1079-
1080-
if tcx.sess.opts.debugging_opts.print_mono_items.is_some() {
1081-
let mut item_to_cgus: FxHashMap<_, Vec<_>> = Default::default();
1082-
1083-
for cgu in &codegen_units {
1084-
for (&mono_item, &linkage) in cgu.items() {
1085-
item_to_cgus.entry(mono_item)
1086-
.or_default()
1087-
.push((cgu.name().clone(), linkage));
1088-
}
1089-
}
1090-
1091-
let mut item_keys: Vec<_> = items
1092-
.iter()
1093-
.map(|i| {
1094-
let mut output = i.to_string(tcx);
1095-
output.push_str(" @@");
1096-
let mut empty = Vec::new();
1097-
let cgus = item_to_cgus.get_mut(i).unwrap_or(&mut empty);
1098-
cgus.as_mut_slice().sort_by_key(|&(ref name, _)| name.clone());
1099-
cgus.dedup();
1100-
for &(ref cgu_name, (linkage, _)) in cgus.iter() {
1101-
output.push_str(" ");
1102-
output.push_str(&cgu_name.as_str());
1103-
1104-
let linkage_abbrev = match linkage {
1105-
Linkage::External => "External",
1106-
Linkage::AvailableExternally => "Available",
1107-
Linkage::LinkOnceAny => "OnceAny",
1108-
Linkage::LinkOnceODR => "OnceODR",
1109-
Linkage::WeakAny => "WeakAny",
1110-
Linkage::WeakODR => "WeakODR",
1111-
Linkage::Appending => "Appending",
1112-
Linkage::Internal => "Internal",
1113-
Linkage::Private => "Private",
1114-
Linkage::ExternalWeak => "ExternalWeak",
1115-
Linkage::Common => "Common",
1116-
};
1117-
1118-
output.push_str("[");
1119-
output.push_str(linkage_abbrev);
1120-
output.push_str("]");
1121-
}
1122-
output
1123-
})
1124-
.collect();
1125-
1126-
item_keys.sort();
1127-
1128-
for item in item_keys {
1129-
println!("MONO_ITEM {}", item);
1130-
}
1131-
}
1132-
1133-
(Arc::new(mono_items), Arc::new(codegen_units))
1134-
}
1135-
11361012
impl CrateInfo {
11371013
pub fn new(tcx: TyCtxt) -> CrateInfo {
11381014
let mut info = CrateInfo {
@@ -1222,12 +1098,6 @@ impl CrateInfo {
12221098
}
12231099
}
12241100

1225-
fn is_codegened_item(tcx: TyCtxt, id: DefId) -> bool {
1226-
let (all_mono_items, _) =
1227-
tcx.collect_and_partition_mono_items(LOCAL_CRATE);
1228-
all_mono_items.contains(&id)
1229-
}
1230-
12311101
fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12321102
cgu_name: InternedString)
12331103
-> Stats {
@@ -1318,24 +1188,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
13181188
}
13191189
}
13201190

1321-
pub fn provide(providers: &mut Providers) {
1322-
providers.collect_and_partition_mono_items =
1323-
collect_and_partition_mono_items;
1324-
1325-
providers.is_codegened_item = is_codegened_item;
1326-
1327-
providers.codegen_unit = |tcx, name| {
1328-
let (_, all) = tcx.collect_and_partition_mono_items(LOCAL_CRATE);
1329-
all.iter()
1330-
.find(|cgu| *cgu.name() == name)
1331-
.cloned()
1332-
.unwrap_or_else(|| panic!("failed to find cgu with name {:?}", name))
1333-
};
1334-
1335-
provide_extern(providers);
1336-
}
1337-
1338-
pub fn provide_extern(providers: &mut Providers) {
1191+
pub fn provide_both(providers: &mut Providers) {
13391192
providers.dllimport_foreign_items = |tcx, krate| {
13401193
let module_map = tcx.foreign_modules(krate);
13411194
let module_map = module_map.iter()

0 commit comments

Comments
 (0)