Skip to content

Commit 5d531ae

Browse files
committed
rustc: Convert dependency_formats to a query
This commit converts a field of `Session`, `dependency_formats`, into a query of `TyCtxt`. This information then also needed to be threaded through to other remaining portions of the linker, but it's relatively straightforward. The only change here is that instead of `HashMap<CrateType, T>` the data structure changed to `Vec<(CrateType, T)>` to make it easier to deal with in queries.
1 parent 66bf391 commit 5d531ae

File tree

14 files changed

+455
-419
lines changed

14 files changed

+455
-419
lines changed

src/librustc/middle/dependency_format.rs

Lines changed: 6 additions & 372 deletions
Large diffs are not rendered by default.

src/librustc/query/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ rustc_queries! {
630630
-> &'tcx [(CrateNum, LinkagePreference)] {
631631
desc { "dylib dependency formats of crate" }
632632
}
633+
634+
query dependency_formats(_: CrateNum) -> Lrc<crate::middle::dependency_format::Dependencies> {
635+
desc { "get the linkage format of all dependencies" }
636+
}
633637
}
634638

635639
Codegen {

src/librustc/session/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ pub enum EntryFnType {
687687

688688
impl_stable_hash_via_hash!(EntryFnType);
689689

690-
#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug)]
690+
#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, HashStable)]
691691
pub enum CrateType {
692692
Executable,
693693
Dylib,

src/librustc/session/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_data_structures::fingerprint::Fingerprint;
77

88
use crate::lint;
99
use crate::lint::builtin::BuiltinLintDiagnostics;
10-
use crate::middle::dependency_format;
1110
use crate::session::config::{OutputType, PrintRequest, SwitchWithOptPath};
1211
use crate::session::search_paths::{PathKind, SearchPath};
1312
use crate::util::nodemap::{FxHashMap, FxHashSet};
@@ -91,7 +90,6 @@ pub struct Session {
9190
pub plugin_llvm_passes: OneThread<RefCell<Vec<String>>>,
9291
pub plugin_attributes: Lock<Vec<(Symbol, AttributeType)>>,
9392
pub crate_types: Once<Vec<config::CrateType>>,
94-
pub dependency_formats: Once<dependency_format::Dependencies>,
9593
/// The `crate_disambiguator` is constructed out of all the `-C metadata`
9694
/// arguments passed to the compiler. Its value together with the crate-name
9795
/// forms a unique global identifier for the crate. It is used to allow
@@ -1247,7 +1245,6 @@ fn build_session_(
12471245
plugin_llvm_passes: OneThread::new(RefCell::new(Vec::new())),
12481246
plugin_attributes: Lock::new(Vec::new()),
12491247
crate_types: Once::new(),
1250-
dependency_formats: Once::new(),
12511248
crate_disambiguator: Once::new(),
12521249
features: Once::new(),
12531250
recursion_limit: Once::new(),

src/librustc_codegen_ssa/back/link.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,24 @@ pub fn get_linker(sess: &Session, linker: &Path, flavor: LinkerFlavor) -> (PathB
219219
(linker.to_path_buf(), cmd)
220220
}
221221

222-
pub fn each_linked_rlib(sess: &Session,
223-
info: &CrateInfo,
224-
f: &mut dyn FnMut(CrateNum, &Path)) -> Result<(), String> {
222+
pub fn each_linked_rlib(
223+
info: &CrateInfo,
224+
f: &mut dyn FnMut(CrateNum, &Path),
225+
) -> Result<(), String> {
225226
let crates = info.used_crates_static.iter();
226-
let fmts = sess.dependency_formats.borrow();
227-
let fmts = fmts.get(&config::CrateType::Executable)
228-
.or_else(|| fmts.get(&config::CrateType::Staticlib))
229-
.or_else(|| fmts.get(&config::CrateType::Cdylib))
230-
.or_else(|| fmts.get(&config::CrateType::ProcMacro));
227+
let mut fmts = None;
228+
for (ty, list) in info.dependency_formats.iter() {
229+
match ty {
230+
config::CrateType::Executable |
231+
config::CrateType::Staticlib |
232+
config::CrateType::Cdylib |
233+
config::CrateType::ProcMacro => {
234+
fmts = Some(list);
235+
break;
236+
}
237+
_ => {}
238+
}
239+
}
231240
let fmts = match fmts {
232241
Some(f) => f,
233242
None => return Err("could not find formats for rlibs".to_string())
@@ -406,7 +415,7 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
406415
tempdir);
407416
let mut all_native_libs = vec![];
408417

409-
let res = each_linked_rlib(sess, &codegen_results.crate_info, &mut |cnum, path| {
418+
let res = each_linked_rlib(&codegen_results.crate_info, &mut |cnum, path| {
410419
let name = &codegen_results.crate_info.crate_name[&cnum];
411420
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
412421

@@ -1294,11 +1303,13 @@ pub fn add_local_native_libraries(cmd: &mut dyn Linker,
12941303
// Rust crates are not considered at all when creating an rlib output. All
12951304
// dependencies will be linked when producing the final output (instead of
12961305
// the intermediate rlib version)
1297-
fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(cmd: &mut dyn Linker,
1298-
sess: &'a Session,
1299-
codegen_results: &CodegenResults,
1300-
crate_type: config::CrateType,
1301-
tmpdir: &Path) {
1306+
fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
1307+
cmd: &mut dyn Linker,
1308+
sess: &'a Session,
1309+
codegen_results: &CodegenResults,
1310+
crate_type: config::CrateType,
1311+
tmpdir: &Path,
1312+
) {
13021313
// All of the heavy lifting has previously been accomplished by the
13031314
// dependency_format module of the compiler. This is just crawling the
13041315
// output of that module, adding crates as necessary.
@@ -1307,8 +1318,10 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(cmd: &mut dyn Linker,
13071318
// will slurp up the object files inside), and linking to a dynamic library
13081319
// involves just passing the right -l flag.
13091320

1310-
let formats = sess.dependency_formats.borrow();
1311-
let data = formats.get(&crate_type).unwrap();
1321+
let (_, data) = codegen_results.crate_info.dependency_formats
1322+
.iter()
1323+
.find(|(ty, _)| *ty == crate_type)
1324+
.expect("failed to find crate type in dependency format list");
13121325

13131326
// Invoke get_used_crates to ensure that we get a topological sorting of
13141327
// crates.
@@ -1620,10 +1633,12 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(cmd: &mut dyn Linker,
16201633
// generic function calls a native function, then the generic function must
16211634
// be instantiated in the target crate, meaning that the native symbol must
16221635
// also be resolved in the target crate.
1623-
pub fn add_upstream_native_libraries(cmd: &mut dyn Linker,
1624-
sess: &Session,
1625-
codegen_results: &CodegenResults,
1626-
crate_type: config::CrateType) {
1636+
pub fn add_upstream_native_libraries(
1637+
cmd: &mut dyn Linker,
1638+
sess: &Session,
1639+
codegen_results: &CodegenResults,
1640+
crate_type: config::CrateType,
1641+
) {
16271642
// Be sure to use a topological sorting of crates because there may be
16281643
// interdependencies between native libraries. When passing -nodefaultlibs,
16291644
// for example, almost all native libraries depend on libc, so we have to
@@ -1633,8 +1648,10 @@ pub fn add_upstream_native_libraries(cmd: &mut dyn Linker,
16331648
// This passes RequireStatic, but the actual requirement doesn't matter,
16341649
// we're just getting an ordering of crate numbers, we're not worried about
16351650
// the paths.
1636-
let formats = sess.dependency_formats.borrow();
1637-
let data = formats.get(&crate_type).unwrap();
1651+
let (_, data) = codegen_results.crate_info.dependency_formats
1652+
.iter()
1653+
.find(|(ty, _)| *ty == crate_type)
1654+
.expect("failed to find crate type in dependency format list");
16381655

16391656
let crates = &codegen_results.crate_info.used_crates_static;
16401657
for &(cnum, _) in crates {

src/librustc_codegen_ssa/back/linker.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,10 +1092,16 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
10921092
}
10931093
}
10941094

1095-
let formats = tcx.sess.dependency_formats.borrow();
1096-
let deps = formats[&crate_type].iter();
1095+
let formats = tcx.dependency_formats(LOCAL_CRATE);
1096+
let deps = formats.iter().filter_map(|(t, list)| {
1097+
if *t == crate_type {
1098+
Some(list)
1099+
} else {
1100+
None
1101+
}
1102+
}).next().unwrap();
10971103

1098-
for (index, dep_format) in deps.enumerate() {
1104+
for (index, dep_format) in deps.iter().enumerate() {
10991105
let cnum = CrateNum::new(index + 1);
11001106
// For each dependency that we are linking to statically ...
11011107
if *dep_format == Linkage::Static {

src/librustc_codegen_ssa/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
10481048
}).expect("failed to spawn helper thread");
10491049

10501050
let mut each_linked_rlib_for_lto = Vec::new();
1051-
drop(link::each_linked_rlib(sess, crate_info, &mut |cnum, path| {
1051+
drop(link::each_linked_rlib(crate_info, &mut |cnum, path| {
10521052
if link::ignored_for_lto(sess, crate_info, cnum) {
10531053
return
10541054
}

src/librustc_codegen_ssa/base.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
539539
// linkage, then it's already got an allocator shim and we'll be using that
540540
// one instead. If nothing exists then it's our job to generate the
541541
// allocator!
542-
let any_dynamic_crate = tcx.sess.dependency_formats.borrow()
542+
let any_dynamic_crate = tcx.dependency_formats(LOCAL_CRATE)
543543
.iter()
544544
.any(|(_, list)| {
545545
use rustc::middle::dependency_format::Linkage;
@@ -731,6 +731,7 @@ impl CrateInfo {
731731
used_crate_source: Default::default(),
732732
lang_item_to_crate: Default::default(),
733733
missing_lang_items: Default::default(),
734+
dependency_formats: tcx.dependency_formats(LOCAL_CRATE),
734735
};
735736
let lang_items = tcx.lang_items();
736737

src/librustc_codegen_ssa/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3333
use rustc_data_structures::sync::Lrc;
3434
use rustc_data_structures::svh::Svh;
3535
use rustc::middle::cstore::{LibSource, CrateSource, NativeLibrary};
36+
use rustc::middle::dependency_format::Dependencies;
3637
use syntax_pos::symbol::Symbol;
3738

3839
mod error_codes;
@@ -142,6 +143,7 @@ pub struct CrateInfo {
142143
pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
143144
pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
144145
pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
146+
pub dependency_formats: Lrc<Dependencies>,
145147
}
146148

147149

src/librustc_interface/passes.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,10 +1079,6 @@ pub fn start_codegen<'tcx>(
10791079
tcx.print_debug_stats();
10801080
}
10811081

1082-
time(tcx.sess, "resolving dependency formats", || {
1083-
middle::dependency_format::calculate(tcx)
1084-
});
1085-
10861082
let (metadata, need_metadata_module) = time(tcx.sess, "metadata encoding and writing", || {
10871083
encode_and_write_metadata(tcx, outputs)
10881084
});

src/librustc_metadata/cstore_impl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,11 @@ pub fn provide(providers: &mut Providers<'_>) {
370370
tcx.arena.alloc(visible_parent_map)
371371
},
372372

373+
dependency_formats: |tcx, cnum| {
374+
assert_eq!(cnum, LOCAL_CRATE);
375+
Lrc::new(crate::dependency_format::calculate(tcx))
376+
},
377+
373378
..*providers
374379
};
375380
}

0 commit comments

Comments
 (0)