Skip to content

Commit

Permalink
Make dependency_formats an FxIndexMap rather than a list of tuples
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjorn3 committed Dec 13, 2024
1 parent ea9e8c1 commit 3198496
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 37 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_codegen_cranelift/src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,7 @@ fn dep_symbol_lookup_fn(

let mut dylib_paths = Vec::new();

let data = &crate_info
.dependency_formats
.iter()
.find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable)
.unwrap()
.1;
let data = &crate_info.dependency_formats[&rustc_session::config::CrateType::Executable].1;
// `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
// itself. This helps the dynamic linker to find dylibs not in the regular dynamic library
Expand Down
34 changes: 13 additions & 21 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@ pub fn each_linked_rlib(
) -> Result<(), errors::LinkRlibError> {
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) {
let (ty1, list1) = &combination[0];
let (ty2, list2) = &combination[1];
Expand All @@ -252,18 +258,7 @@ pub fn each_linked_rlib(
if info.dependency_formats.is_empty() {
return Err(errors::LinkRlibError::MissingFormat);
}
&info.dependency_formats[0].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
info.dependency_formats.first().unwrap().1
};

for &cnum in crates {
Expand Down Expand Up @@ -624,8 +619,7 @@ fn link_staticlib(
let fmts = codegen_results
.crate_info
.dependency_formats
.iter()
.find_map(|&(ty, ref list)| if ty == CrateType::Staticlib { Some(list) } else { None })
.get(&CrateType::Staticlib)
.expect("no dependency formats for staticlib");

let mut all_rust_dylibs = vec![];
Expand Down Expand Up @@ -2355,11 +2349,10 @@ fn linker_with_args(
// 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
// by the linker.
let (_, dependency_linkage) = codegen_results
let dependency_linkage = codegen_results
.crate_info
.dependency_formats
.iter()
.find(|(ty, _)| *ty == crate_type)
.get(&crate_type)
.expect("failed to find crate type in dependency format list");

// We sort the libraries below
Expand Down Expand Up @@ -2738,11 +2731,10 @@ fn add_upstream_rust_crates(
// 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
// involves just passing the right -l flag.
let (_, data) = codegen_results
let data = codegen_results
.crate_info
.dependency_formats
.iter()
.find(|(ty, _)| *ty == crate_type)
.get(&crate_type)
.expect("failed to find crate type in dependency format list");

if sess.target.is_like_aix {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ fn for_each_exported_symbols_include_dep<'tcx>(
}

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() {
let cnum = CrateNum::new(index + 1);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub(crate) fn calculate(tcx: TyCtxt<'_>) -> Dependencies {
verify_ok(tcx, &linkage);
(ty, linkage)
})
.collect::<Vec<_>>()
.collect()
}

fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,10 +2164,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> {
empty_proc_macro!(self);
let formats = self.tcx.dependency_formats(());
for (ty, arr) in formats.iter() {
if *ty != CrateType::Dylib {
continue;
}
if let Some(arr) = formats.get(&CrateType::Dylib) {
return self.lazy_array(arr.iter().map(|slot| match *slot {
Linkage::NotLinked | Linkage::IncludedFromDylib => None,

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/middle/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// FIXME: move this file to rustc_metadata::dependency_format, but
// 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_session::config::CrateType;

Expand All @@ -18,7 +19,7 @@ pub type DependencyList = Vec<Linkage>;
/// 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.
pub type Dependencies = Vec<(CrateType, DependencyList)>;
pub type Dependencies = FxIndexMap<CrateType, DependencyList>;

#[derive(Copy, Clone, PartialEq, Debug, HashStable, Encodable, Decodable)]
pub enum Linkage {
Expand Down
5 changes: 2 additions & 3 deletions src/tools/miri/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,9 @@ pub fn iter_exported_symbols<'tcx>(
let dependency_formats = tcx.dependency_formats(());
// Find the dependencies of the executable we are running.
let dependency_format = dependency_formats
.iter()
.find(|(crate_type, _)| *crate_type == CrateType::Executable)
.get(&CrateType::Executable)
.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
// calls `CrateNum::new`...
#[expect(clippy::arithmetic_side_effects)]
Expand Down

0 comments on commit 3198496

Please sign in to comment.