From 8748e37d35e380f390ef8673b38a63f8111686dc Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 7 Jun 2021 12:18:28 +0200 Subject: [PATCH 1/2] Remove LibSource The information is stored in used_crate_source too anyway --- .../rustc_codegen_cranelift/src/driver/jit.rs | 2 +- compiler/rustc_codegen_ssa/src/back/link.rs | 47 ++++++++++++------- compiler/rustc_codegen_ssa/src/back/rpath.rs | 19 +++----- .../rustc_codegen_ssa/src/back/rpath/tests.rs | 4 +- compiler/rustc_codegen_ssa/src/base.rs | 6 +-- compiler/rustc_codegen_ssa/src/lib.rs | 5 +- .../rustc_metadata/src/dependency_format.rs | 16 +++++-- compiler/rustc_middle/src/middle/cstore.rs | 41 ++-------------- 8 files changed, 59 insertions(+), 81 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/driver/jit.rs b/compiler/rustc_codegen_cranelift/src/driver/jit.rs index 4a99cb727c8..3f96e741d35 100644 --- a/compiler/rustc_codegen_cranelift/src/driver/jit.rs +++ b/compiler/rustc_codegen_cranelift/src/driver/jit.rs @@ -185,7 +185,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> { .find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable) .unwrap() .1; - for &(cnum, _) in &crate_info.used_crates_dynamic { + for &cnum in &crate_info.used_crates { let src = &crate_info.used_crate_source[&cnum]; match data[cnum.as_usize() - 1] { Linkage::NotLinked | Linkage::IncludedFromDylib => {} diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index d47624da79a..3c448b685c1 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -3,7 +3,7 @@ use rustc_errors::Handler; use rustc_fs_util::fix_windows_verbatim_for_gcc; use rustc_hir::def_id::CrateNum; -use rustc_middle::middle::cstore::{DllImport, LibSource}; +use rustc_middle::middle::cstore::DllImport; use rustc_middle::middle::dependency_format::Linkage; use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip}; use rustc_session::config::{OutputFilenames, OutputType, PrintRequest}; @@ -238,7 +238,7 @@ pub fn each_linked_rlib( info: &CrateInfo, f: &mut dyn FnMut(CrateNum, &Path), ) -> Result<(), String> { - let crates = info.used_crates_static.iter(); + let crates = info.used_crates.iter(); let mut fmts = None; for (ty, list) in info.dependency_formats.iter() { match ty { @@ -256,22 +256,22 @@ pub fn each_linked_rlib( Some(f) => f, None => return Err("could not find formats for rlibs".to_string()), }; - for &(cnum, ref path) in crates { + for &cnum in crates { match fmts.get(cnum.as_usize() - 1) { Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue, Some(_) => {} None => return Err("could not find formats for rlibs".to_string()), } let name = &info.crate_name[&cnum]; - let path = match *path { - LibSource::Some(ref p) => p, - LibSource::MetadataOnly => { - return Err(format!( - "could not find rlib for: `{}`, found rmeta (metadata) file", - name - )); - } - LibSource::None => return Err(format!("could not find rlib for: `{}`", name)), + let path = if let Some((path, _)) = &info.used_crate_source[&cnum].rlib { + path + } else if info.used_crate_source[&cnum].rmeta.is_some() { + return Err(format!( + "could not find rlib for: `{}`, found rmeta (metadata) file", + name + )); + } else { + return Err(format!("could not find rlib for: `{}`", name)); }; f(cnum, &path); } @@ -1759,8 +1759,19 @@ fn add_rpath_args( // where extern libraries might live, based on the // add_lib_search_paths if sess.opts.cg.rpath { + let libs = codegen_results + .crate_info + .used_crates + .iter() + .filter_map(|cnum| { + codegen_results.crate_info.used_crate_source[cnum] + .dylib + .as_ref() + .map(|(path, _)| &**path) + }) + .collect::>(); let mut rpath_config = RPathConfig { - used_crates: &codegen_results.crate_info.used_crates_dynamic, + libs: &*libs, out_filename: out_filename.to_path_buf(), has_rpath: sess.target.has_rpath, is_like_osx: sess.target.is_like_osx, @@ -2121,7 +2132,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>( // Invoke get_used_crates to ensure that we get a topological sorting of // crates. - let deps = &codegen_results.crate_info.used_crates_dynamic; + let deps = &codegen_results.crate_info.used_crates; // There's a few internal crates in the standard library (aka libcore and // libstd) which actually have a circular dependence upon one another. This @@ -2149,7 +2160,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>( let mut required = FxHashSet::default(); let info = &codegen_results.crate_info; - for &(cnum, _) in deps.iter().rev() { + for &cnum in deps.iter().rev() { if let Some(missing) = info.missing_lang_items.get(&cnum) { let missing_crates = missing.iter().map(|i| info.lang_item_to_crate.get(i).copied()); required.extend(missing_crates); @@ -2176,7 +2187,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>( let mut compiler_builtins = None; - for &(cnum, _) in deps.iter() { + for &cnum in deps.iter() { if group_start == Some(cnum) { cmd.group_start(); } @@ -2388,9 +2399,9 @@ fn add_upstream_native_libraries( .find(|(ty, _)| *ty == crate_type) .expect("failed to find crate type in dependency format list"); - let crates = &codegen_results.crate_info.used_crates_static; + let crates = &codegen_results.crate_info.used_crates; let mut last = (NativeLibKind::Unspecified, None); - for &(cnum, _) in crates { + for &cnum in crates { for lib in codegen_results.crate_info.native_libraries[&cnum].iter() { let name = match lib.name { Some(l) => l, diff --git a/compiler/rustc_codegen_ssa/src/back/rpath.rs b/compiler/rustc_codegen_ssa/src/back/rpath.rs index 39b0ccd120d..61c3ef62fb1 100644 --- a/compiler/rustc_codegen_ssa/src/back/rpath.rs +++ b/compiler/rustc_codegen_ssa/src/back/rpath.rs @@ -4,11 +4,8 @@ use std::fs; use std::path::{Path, PathBuf}; -use rustc_hir::def_id::CrateNum; -use rustc_middle::middle::cstore::LibSource; - pub struct RPathConfig<'a> { - pub used_crates: &'a [(CrateNum, LibSource)], + pub libs: &'a [&'a Path], pub out_filename: PathBuf, pub is_like_osx: bool, pub has_rpath: bool, @@ -23,9 +20,7 @@ pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec { debug!("preparing the RPATH!"); - let libs = config.used_crates; - let libs = libs.iter().filter_map(|&(_, ref l)| l.option()).collect::>(); - let rpaths = get_rpaths(config, &libs); + let rpaths = get_rpaths(config); let mut flags = rpaths_to_flags(&rpaths); // Use DT_RUNPATH instead of DT_RPATH if available @@ -52,17 +47,17 @@ fn rpaths_to_flags(rpaths: &[String]) -> Vec { ret } -fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec { +fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec { debug!("output: {:?}", config.out_filename.display()); debug!("libs:"); - for libpath in libs { + for libpath in config.libs { debug!(" {:?}", libpath.display()); } // Use relative paths to the libraries. Binaries can be moved // as long as they maintain the relative relationship to the // crates they depend on. - let rpaths = get_rpaths_relative_to_output(config, libs); + let rpaths = get_rpaths_relative_to_output(config); debug!("rpaths:"); for rpath in &rpaths { @@ -73,8 +68,8 @@ fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec { minimize_rpaths(&rpaths) } -fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec { - libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect() +fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec { + config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect() } fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String { diff --git a/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs b/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs index 24c362db122..604f19144a6 100644 --- a/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs +++ b/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs @@ -35,7 +35,7 @@ fn test_minimize2() { fn test_rpath_relative() { if cfg!(target_os = "macos") { let config = &mut RPathConfig { - used_crates: &[], + libs: &[], has_rpath: true, is_like_osx: true, linker_is_gnu: false, @@ -45,7 +45,7 @@ fn test_rpath_relative() { assert_eq!(res, "@loader_path/../lib"); } else { let config = &mut RPathConfig { - used_crates: &[], + libs: &[], out_filename: PathBuf::from("bin/rustc"), has_rpath: true, is_like_osx: false, diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index ec1aa4fd58e..0036935be67 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -19,8 +19,7 @@ use rustc_hir::lang_items::LangItem; use rustc_index::vec::Idx; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs; -use rustc_middle::middle::cstore::EncodedMetadata; -use rustc_middle::middle::cstore::{self, LinkagePreference}; +use rustc_middle::middle::cstore::{self, EncodedMetadata}; use rustc_middle::middle::lang_items; use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem}; use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout}; @@ -779,8 +778,7 @@ pub fn new(tcx: TyCtxt<'_>) -> CrateInfo { native_libraries: Default::default(), used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(), crate_name: Default::default(), - used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic), - used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic), + used_crates: cstore::used_crates(tcx), used_crate_source: Default::default(), lang_item_to_crate: Default::default(), missing_lang_items: Default::default(), diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index b6de12fa35e..6413e03bea4 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -24,7 +24,7 @@ use rustc_hir::def_id::CrateNum; use rustc_hir::LangItem; use rustc_middle::dep_graph::WorkProduct; -use rustc_middle::middle::cstore::{self, CrateSource, LibSource}; +use rustc_middle::middle::cstore::{self, CrateSource}; use rustc_middle::middle::dependency_format::Dependencies; use rustc_middle::ty::query::Providers; use rustc_session::config::{OutputFilenames, OutputType, RUST_CGU_EXT}; @@ -144,8 +144,7 @@ pub struct CrateInfo { pub crate_name: FxHashMap, pub used_libraries: Vec, pub used_crate_source: FxHashMap>, - pub used_crates_static: Vec<(CrateNum, LibSource)>, - pub used_crates_dynamic: Vec<(CrateNum, LibSource)>, + pub used_crates: Vec, pub lang_item_to_crate: FxHashMap, pub missing_lang_items: FxHashMap>, pub dependency_formats: Lrc, diff --git a/compiler/rustc_metadata/src/dependency_format.rs b/compiler/rustc_metadata/src/dependency_format.rs index f7778d67d20..2d4deb1d8d5 100644 --- a/compiler/rustc_metadata/src/dependency_format.rs +++ b/compiler/rustc_metadata/src/dependency_format.rs @@ -55,8 +55,8 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::CrateNum; +use rustc_middle::middle::cstore::CrateDepKind; use rustc_middle::middle::cstore::LinkagePreference::{self, RequireDynamic, RequireStatic}; -use rustc_middle::middle::cstore::{self, CrateDepKind}; use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage}; use rustc_middle::ty::TyCtxt; use rustc_session::config::CrateType; @@ -274,8 +274,18 @@ fn add_library( } fn attempt_static(tcx: TyCtxt<'_>) -> Option { - let crates = cstore::used_crates(tcx, RequireStatic); - if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) { + let all_crates_available_as_rlib = tcx + .crates(()) + .iter() + .cloned() + .filter_map(|cnum| { + if tcx.dep_kind(cnum).macros_only() { + return None; + } + Some(tcx.used_crate_source(cnum).rlib.is_some()) + }) + .all(|is_rlib| is_rlib); + if !all_crates_available_as_rlib { return None; } diff --git a/compiler/rustc_middle/src/middle/cstore.rs b/compiler/rustc_middle/src/middle/cstore.rs index e9bcbe016c7..d9d475899d6 100644 --- a/compiler/rustc_middle/src/middle/cstore.rs +++ b/compiler/rustc_middle/src/middle/cstore.rs @@ -60,26 +60,6 @@ pub fn macros_only(self) -> bool { } } -#[derive(PartialEq, Clone, Debug, Encodable, Decodable)] -pub enum LibSource { - Some(PathBuf), - MetadataOnly, - None, -} - -impl LibSource { - pub fn is_some(&self) -> bool { - matches!(self, LibSource::Some(_)) - } - - pub fn option(&self) -> Option { - match *self { - LibSource::Some(ref p) => Some(p.clone()), - LibSource::MetadataOnly | LibSource::None => None, - } - } -} - #[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable)] pub enum LinkagePreference { RequireDynamic, @@ -227,7 +207,7 @@ fn def_path_hash_to_def_id( // In order to get this left-to-right dependency ordering, we perform a // topological sort of all crates putting the leaves at the right-most // positions. -pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> { +pub fn used_crates(tcx: TyCtxt<'_>) -> Vec { let mut libs = tcx .crates(()) .iter() @@ -236,26 +216,11 @@ pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum, if tcx.dep_kind(cnum).macros_only() { return None; } - let source = tcx.used_crate_source(cnum); - let path = match prefer { - LinkagePreference::RequireDynamic => source.dylib.clone().map(|p| p.0), - LinkagePreference::RequireStatic => source.rlib.clone().map(|p| p.0), - }; - let path = match path { - Some(p) => LibSource::Some(p), - None => { - if source.rmeta.is_some() { - LibSource::MetadataOnly - } else { - LibSource::None - } - } - }; - Some((cnum, path)) + Some(cnum) }) .collect::>(); let mut ordering = tcx.postorder_cnums(()).to_owned(); ordering.reverse(); - libs.sort_by_cached_key(|&(a, _)| ordering.iter().position(|x| *x == a)); + libs.sort_by_cached_key(|&a| ordering.iter().position(|x| *x == a)); libs } From 5added38c956f3a9be2104ff387120a9487e7c75 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 5 Jul 2021 17:37:45 +0200 Subject: [PATCH 2/2] Deduplicate info.used_crate_source indexing --- compiler/rustc_codegen_ssa/src/back/link.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 3c448b685c1..8c246f1dac3 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -263,9 +263,10 @@ pub fn each_linked_rlib( None => return Err("could not find formats for rlibs".to_string()), } let name = &info.crate_name[&cnum]; - let path = if let Some((path, _)) = &info.used_crate_source[&cnum].rlib { + let used_crate_source = &info.used_crate_source[&cnum]; + let path = if let Some((path, _)) = &used_crate_source.rlib { path - } else if info.used_crate_source[&cnum].rmeta.is_some() { + } else if used_crate_source.rmeta.is_some() { return Err(format!( "could not find rlib for: `{}`, found rmeta (metadata) file", name