1
Fork 0

Make dependency_formats an FxIndexMap rather than a list of tuples

It is treated as a map already. This is using FxIndexMap rather than
UnordMap because the latter doesn't provide an api to pick a single
value iff all values are equal, which each_linked_rlib depends on.
This commit is contained in:
bjorn3 2024-12-12 14:44:18 +00:00
parent ea9e8c13dc
commit 3198496385
7 changed files with 21 additions and 37 deletions

View file

@ -287,12 +287,7 @@ fn dep_symbol_lookup_fn(
let mut dylib_paths = Vec::new(); let mut dylib_paths = Vec::new();
let data = &crate_info let data = &crate_info.dependency_formats[&rustc_session::config::CrateType::Executable].1;
.dependency_formats
.iter()
.find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable)
.unwrap()
.1;
// `used_crates` is in reverse postorder in terms of dependencies. Reverse the order here to // `used_crates` is in reverse postorder in terms of dependencies. Reverse the order here to
// get a postorder which ensures that all dependencies of a dylib are loaded before the dylib // get a postorder which ensures that all dependencies of a dylib are loaded before the dylib
// itself. This helps the dynamic linker to find dylibs not in the regular dynamic library // itself. This helps the dynamic linker to find dylibs not in the regular dynamic library

View file

@ -236,7 +236,13 @@ pub fn each_linked_rlib(
) -> Result<(), errors::LinkRlibError> { ) -> Result<(), errors::LinkRlibError> {
let crates = info.used_crates.iter(); let crates = info.used_crates.iter();
let fmts = if crate_type.is_none() { let fmts = if let Some(crate_type) = crate_type {
let Some(fmts) = info.dependency_formats.get(&crate_type) else {
return Err(errors::LinkRlibError::MissingFormat);
};
fmts
} else {
for combination in info.dependency_formats.iter().combinations(2) { for combination in info.dependency_formats.iter().combinations(2) {
let (ty1, list1) = &combination[0]; let (ty1, list1) = &combination[0];
let (ty2, list2) = &combination[1]; let (ty2, list2) = &combination[1];
@ -252,18 +258,7 @@ pub fn each_linked_rlib(
if info.dependency_formats.is_empty() { if info.dependency_formats.is_empty() {
return Err(errors::LinkRlibError::MissingFormat); return Err(errors::LinkRlibError::MissingFormat);
} }
&info.dependency_formats[0].1 info.dependency_formats.first().unwrap().1
} else {
let fmts = info
.dependency_formats
.iter()
.find_map(|&(ty, ref list)| if Some(ty) == crate_type { Some(list) } else { None });
let Some(fmts) = fmts else {
return Err(errors::LinkRlibError::MissingFormat);
};
fmts
}; };
for &cnum in crates { for &cnum in crates {
@ -624,8 +619,7 @@ fn link_staticlib(
let fmts = codegen_results let fmts = codegen_results
.crate_info .crate_info
.dependency_formats .dependency_formats
.iter() .get(&CrateType::Staticlib)
.find_map(|&(ty, ref list)| if ty == CrateType::Staticlib { Some(list) } else { None })
.expect("no dependency formats for staticlib"); .expect("no dependency formats for staticlib");
let mut all_rust_dylibs = vec![]; let mut all_rust_dylibs = vec![];
@ -2355,11 +2349,10 @@ fn linker_with_args(
// they are used within inlined functions or instantiated generic functions. We do this *after* // they are used within inlined functions or instantiated generic functions. We do this *after*
// handling the raw-dylib symbols in the current crate to make sure that those are chosen first // handling the raw-dylib symbols in the current crate to make sure that those are chosen first
// by the linker. // by the linker.
let (_, dependency_linkage) = codegen_results let dependency_linkage = codegen_results
.crate_info .crate_info
.dependency_formats .dependency_formats
.iter() .get(&crate_type)
.find(|(ty, _)| *ty == crate_type)
.expect("failed to find crate type in dependency format list"); .expect("failed to find crate type in dependency format list");
// We sort the libraries below // We sort the libraries below
@ -2738,11 +2731,10 @@ fn add_upstream_rust_crates(
// Linking to a rlib involves just passing it to the linker (the linker // Linking to a rlib involves just passing it to the linker (the linker
// will slurp up the object files inside), and linking to a dynamic library // will slurp up the object files inside), and linking to a dynamic library
// involves just passing the right -l flag. // involves just passing the right -l flag.
let (_, data) = codegen_results let data = codegen_results
.crate_info .crate_info
.dependency_formats .dependency_formats
.iter() .get(&crate_type)
.find(|(ty, _)| *ty == crate_type)
.expect("failed to find crate type in dependency format list"); .expect("failed to find crate type in dependency format list");
if sess.target.is_like_aix { if sess.target.is_like_aix {

View file

@ -1749,7 +1749,7 @@ fn for_each_exported_symbols_include_dep<'tcx>(
} }
let formats = tcx.dependency_formats(()); let formats = tcx.dependency_formats(());
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap(); let deps = &formats[&crate_type];
for (index, dep_format) in deps.iter().enumerate() { for (index, dep_format) in deps.iter().enumerate() {
let cnum = CrateNum::new(index + 1); let cnum = CrateNum::new(index + 1);

View file

@ -77,7 +77,7 @@ pub(crate) fn calculate(tcx: TyCtxt<'_>) -> Dependencies {
verify_ok(tcx, &linkage); verify_ok(tcx, &linkage);
(ty, linkage) (ty, linkage)
}) })
.collect::<Vec<_>>() .collect()
} }
fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {

View file

@ -2164,10 +2164,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> { fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> {
empty_proc_macro!(self); empty_proc_macro!(self);
let formats = self.tcx.dependency_formats(()); let formats = self.tcx.dependency_formats(());
for (ty, arr) in formats.iter() { if let Some(arr) = formats.get(&CrateType::Dylib) {
if *ty != CrateType::Dylib {
continue;
}
return self.lazy_array(arr.iter().map(|slot| match *slot { return self.lazy_array(arr.iter().map(|slot| match *slot {
Linkage::NotLinked | Linkage::IncludedFromDylib => None, Linkage::NotLinked | Linkage::IncludedFromDylib => None,

View file

@ -7,6 +7,7 @@
// FIXME: move this file to rustc_metadata::dependency_format, but // FIXME: move this file to rustc_metadata::dependency_format, but
// this will introduce circular dependency between rustc_metadata and rustc_middle // this will introduce circular dependency between rustc_metadata and rustc_middle
use rustc_data_structures::fx::FxIndexMap;
use rustc_macros::{Decodable, Encodable, HashStable}; use rustc_macros::{Decodable, Encodable, HashStable};
use rustc_session::config::CrateType; use rustc_session::config::CrateType;
@ -18,7 +19,7 @@ pub type DependencyList = Vec<Linkage>;
/// A mapping of all required dependencies for a particular flavor of output. /// A mapping of all required dependencies for a particular flavor of output.
/// ///
/// This is local to the tcx, and is generally relevant to one session. /// This is local to the tcx, and is generally relevant to one session.
pub type Dependencies = Vec<(CrateType, DependencyList)>; pub type Dependencies = FxIndexMap<CrateType, DependencyList>;
#[derive(Copy, Clone, PartialEq, Debug, HashStable, Encodable, Decodable)] #[derive(Copy, Clone, PartialEq, Debug, HashStable, Encodable, Decodable)]
pub enum Linkage { pub enum Linkage {

View file

@ -149,10 +149,9 @@ pub fn iter_exported_symbols<'tcx>(
let dependency_formats = tcx.dependency_formats(()); let dependency_formats = tcx.dependency_formats(());
// Find the dependencies of the executable we are running. // Find the dependencies of the executable we are running.
let dependency_format = dependency_formats let dependency_format = dependency_formats
.iter() .get(&CrateType::Executable)
.find(|(crate_type, _)| *crate_type == CrateType::Executable)
.expect("interpreting a non-executable crate"); .expect("interpreting a non-executable crate");
for cnum in dependency_format.1.iter().enumerate().filter_map(|(num, &linkage)| { for cnum in dependency_format.iter().enumerate().filter_map(|(num, &linkage)| {
// We add 1 to the number because that's what rustc also does everywhere it // We add 1 to the number because that's what rustc also does everywhere it
// calls `CrateNum::new`... // calls `CrateNum::new`...
#[expect(clippy::arithmetic_side_effects)] #[expect(clippy::arithmetic_side_effects)]