Skip to content

Commit ac923d9

Browse files
committed
Auto merge of #83610 - bjorn3:driver_cleanup, r=cjgillot
rustc_driver cleanup Best reviewed one commit at a time.
2 parents c1e7e36 + 163b480 commit ac923d9

File tree

16 files changed

+168
-224
lines changed

16 files changed

+168
-224
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/driver/aot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ pub(crate) fn run_aot(
298298
metadata_module,
299299
metadata,
300300
windows_subsystem,
301-
linker_info: LinkerInfo::new(tcx),
301+
linker_info: LinkerInfo::new(tcx, crate::target_triple(tcx.sess).to_string()),
302302
crate_info: CrateInfo::new(tcx),
303303
},
304304
work_products,

Diff for: compiler/rustc_codegen_cranelift/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,11 @@ impl CodegenBackend for CraneliftCodegenBackend {
218218
) -> Result<(), ErrorReported> {
219219
use rustc_codegen_ssa::back::link::link_binary;
220220

221-
let target_cpu = crate::target_triple(sess).to_string();
222221
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
223222
sess,
224223
&codegen_results,
225224
outputs,
226225
&codegen_results.crate_name.as_str(),
227-
&target_cpu,
228226
);
229227

230228
Ok(())

Diff for: compiler/rustc_codegen_llvm/src/attributes.rs

-31
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ use std::ffi::CString;
44

55
use cstr::cstr;
66
use rustc_codegen_ssa::traits::*;
7-
use rustc_data_structures::fx::FxHashMap;
87
use rustc_data_structures::small_c_str::SmallCStr;
98
use rustc_hir::def_id::DefId;
109
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1110
use rustc_middle::ty::layout::HasTyCtxt;
12-
use rustc_middle::ty::query::Providers;
1311
use rustc_middle::ty::{self, TyCtxt};
1412
use rustc_session::config::OptLevel;
1513
use rustc_session::Session;
@@ -355,35 +353,6 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
355353
}
356354
}
357355

358-
pub fn provide_both(providers: &mut Providers) {
359-
providers.wasm_import_module_map = |tcx, cnum| {
360-
// Build up a map from DefId to a `NativeLib` structure, where
361-
// `NativeLib` internally contains information about
362-
// `#[link(wasm_import_module = "...")]` for example.
363-
let native_libs = tcx.native_libraries(cnum);
364-
365-
let def_id_to_native_lib = native_libs
366-
.iter()
367-
.filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
368-
.collect::<FxHashMap<_, _>>();
369-
370-
let mut ret = FxHashMap::default();
371-
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
372-
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
373-
let module = match module {
374-
Some(s) => s,
375-
None => continue,
376-
};
377-
ret.extend(lib.foreign_items.iter().map(|id| {
378-
assert_eq!(id.krate, cnum);
379-
(*id, module.to_string())
380-
}));
381-
}
382-
383-
ret
384-
};
385-
}
386-
387356
fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<CString> {
388357
tcx.wasm_import_module_map(id.krate).get(&id).map(|s| CString::new(&s[..]).unwrap())
389358
}

Diff for: compiler/rustc_codegen_llvm/src/lib.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,8 @@ impl CodegenBackend for LlvmCodegenBackend {
254254
Box::new(metadata::LlvmMetadataLoader)
255255
}
256256

257-
fn provide(&self, providers: &mut ty::query::Providers) {
258-
attributes::provide_both(providers);
259-
}
260-
261-
fn provide_extern(&self, providers: &mut ty::query::Providers) {
262-
attributes::provide_both(providers);
263-
}
257+
fn provide(&self, _providers: &mut ty::query::Providers) {}
258+
fn provide_extern(&self, _providers: &mut ty::query::Providers) {}
264259

265260
fn codegen_crate<'tcx>(
266261
&self,
@@ -271,6 +266,7 @@ impl CodegenBackend for LlvmCodegenBackend {
271266
Box::new(rustc_codegen_ssa::base::codegen_crate(
272267
LlvmCodegenBackend(()),
273268
tcx,
269+
crate::llvm_util::target_cpu(tcx.sess).to_string(),
274270
metadata,
275271
need_metadata_module,
276272
))
@@ -306,13 +302,11 @@ impl CodegenBackend for LlvmCodegenBackend {
306302

307303
// Run the linker on any artifacts that resulted from the LLVM run.
308304
// This should produce either a finished executable or library.
309-
let target_cpu = crate::llvm_util::target_cpu(sess);
310305
link_binary::<LlvmArchiveBuilder<'_>>(
311306
sess,
312307
&codegen_results,
313308
outputs,
314309
&codegen_results.crate_name.as_str(),
315-
target_cpu,
316310
);
317311

318312
Ok(())

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
5050
codegen_results: &CodegenResults,
5151
outputs: &OutputFilenames,
5252
crate_name: &str,
53-
target_cpu: &str,
5453
) {
5554
let _timer = sess.timer("link_binary");
5655
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
@@ -100,7 +99,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
10099
&out_filename,
101100
codegen_results,
102101
path.as_ref(),
103-
target_cpu,
104102
);
105103
}
106104
}
@@ -532,7 +530,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
532530
out_filename: &Path,
533531
codegen_results: &CodegenResults,
534532
tmpdir: &Path,
535-
target_cpu: &str,
536533
) {
537534
info!("preparing {:?} to {:?}", crate_type, out_filename);
538535
let (linker_path, flavor) = linker_and_flavor(sess);
@@ -544,7 +541,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
544541
tmpdir,
545542
out_filename,
546543
codegen_results,
547-
target_cpu,
548544
);
549545

550546
linker::disable_localization(&mut cmd);
@@ -1609,14 +1605,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
16091605
tmpdir: &Path,
16101606
out_filename: &Path,
16111607
codegen_results: &CodegenResults,
1612-
target_cpu: &str,
16131608
) -> Command {
16141609
let crt_objects_fallback = crt_objects_fallback(sess, crate_type);
16151610
let base_cmd = get_linker(sess, path, flavor, crt_objects_fallback);
16161611
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
16171612
// to the linker args construction.
16181613
assert!(base_cmd.get_args().is_empty() || sess.target.vendor == "uwp");
1619-
let cmd = &mut *codegen_results.linker_info.to_linker(base_cmd, &sess, flavor, target_cpu);
1614+
let cmd = &mut *codegen_results.linker_info.to_linker(base_cmd, &sess, flavor);
16201615
let link_output_kind = link_output_kind(sess, crate_type);
16211616

16221617
// NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT

Diff for: compiler/rustc_codegen_ssa/src/back/linker.rs

+17-26
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ pub fn disable_localization(linker: &mut Command) {
3737
/// need out of the shared crate context before we get rid of it.
3838
#[derive(Encodable, Decodable)]
3939
pub struct LinkerInfo {
40+
target_cpu: String,
4041
exports: FxHashMap<CrateType, Vec<String>>,
4142
}
4243

4344
impl LinkerInfo {
44-
pub fn new(tcx: TyCtxt<'_>) -> LinkerInfo {
45+
pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> LinkerInfo {
4546
LinkerInfo {
47+
target_cpu,
4648
exports: tcx
4749
.sess
4850
.crate_types()
@@ -57,38 +59,31 @@ impl LinkerInfo {
5759
cmd: Command,
5860
sess: &'a Session,
5961
flavor: LinkerFlavor,
60-
target_cpu: &'a str,
6162
) -> Box<dyn Linker + 'a> {
6263
match flavor {
6364
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => {
6465
Box::new(MsvcLinker { cmd, sess, info: self }) as Box<dyn Linker>
6566
}
6667
LinkerFlavor::Em => Box::new(EmLinker { cmd, sess, info: self }) as Box<dyn Linker>,
67-
LinkerFlavor::Gcc => Box::new(GccLinker {
68-
cmd,
69-
sess,
70-
info: self,
71-
hinted_static: false,
72-
is_ld: false,
73-
target_cpu,
74-
}) as Box<dyn Linker>,
68+
LinkerFlavor::Gcc => {
69+
Box::new(GccLinker { cmd, sess, info: self, hinted_static: false, is_ld: false })
70+
as Box<dyn Linker>
71+
}
7572

7673
LinkerFlavor::Lld(LldFlavor::Ld)
7774
| LinkerFlavor::Lld(LldFlavor::Ld64)
78-
| LinkerFlavor::Ld => Box::new(GccLinker {
79-
cmd,
80-
sess,
81-
info: self,
82-
hinted_static: false,
83-
is_ld: true,
84-
target_cpu,
85-
}) as Box<dyn Linker>,
75+
| LinkerFlavor::Ld => {
76+
Box::new(GccLinker { cmd, sess, info: self, hinted_static: false, is_ld: true })
77+
as Box<dyn Linker>
78+
}
8679

8780
LinkerFlavor::Lld(LldFlavor::Wasm) => {
8881
Box::new(WasmLd::new(cmd, sess, self)) as Box<dyn Linker>
8982
}
9083

91-
LinkerFlavor::PtxLinker => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
84+
LinkerFlavor::PtxLinker => {
85+
Box::new(PtxLinker { cmd, sess, info: self }) as Box<dyn Linker>
86+
}
9287
}
9388
}
9489
}
@@ -157,7 +152,6 @@ pub struct GccLinker<'a> {
157152
hinted_static: bool, // Keeps track of the current hinting mode.
158153
// Link as ld
159154
is_ld: bool,
160-
target_cpu: &'a str,
161155
}
162156

163157
impl<'a> GccLinker<'a> {
@@ -229,8 +223,7 @@ impl<'a> GccLinker<'a> {
229223
};
230224

231225
self.linker_arg(&format!("-plugin-opt={}", opt_level));
232-
let target_cpu = self.target_cpu;
233-
self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
226+
self.linker_arg(&format!("-plugin-opt=mcpu={}", self.info.target_cpu));
234227
}
235228

236229
fn build_dylib(&mut self, out_filename: &Path) {
@@ -1336,6 +1329,7 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
13361329
pub struct PtxLinker<'a> {
13371330
cmd: Command,
13381331
sess: &'a Session,
1332+
info: &'a LinkerInfo,
13391333
}
13401334

13411335
impl<'a> Linker for PtxLinker<'a> {
@@ -1381,10 +1375,7 @@ impl<'a> Linker for PtxLinker<'a> {
13811375

13821376
fn finalize(&mut self) {
13831377
// Provide the linker with fallback to internal `target-cpu`.
1384-
self.cmd.arg("--fallback-arch").arg(match self.sess.opts.cg.target_cpu {
1385-
Some(ref s) => s,
1386-
None => &self.sess.target.cpu,
1387-
});
1378+
self.cmd.arg("--fallback-arch").arg(&self.info.target_cpu);
13881379
}
13891380

13901381
fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {

Diff for: compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+29
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,13 @@ pub fn provide(providers: &mut Providers) {
370370
providers.upstream_monomorphizations = upstream_monomorphizations_provider;
371371
providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
372372
providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
373+
providers.wasm_import_module_map = wasm_import_module_map;
373374
}
374375

375376
pub fn provide_extern(providers: &mut Providers) {
376377
providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
377378
providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
379+
providers.wasm_import_module_map = wasm_import_module_map;
378380
}
379381

380382
fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel {
@@ -442,3 +444,30 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
442444
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
443445
}
444446
}
447+
448+
fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> FxHashMap<DefId, String> {
449+
// Build up a map from DefId to a `NativeLib` structure, where
450+
// `NativeLib` internally contains information about
451+
// `#[link(wasm_import_module = "...")]` for example.
452+
let native_libs = tcx.native_libraries(cnum);
453+
454+
let def_id_to_native_lib = native_libs
455+
.iter()
456+
.filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
457+
.collect::<FxHashMap<_, _>>();
458+
459+
let mut ret = FxHashMap::default();
460+
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
461+
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
462+
let module = match module {
463+
Some(s) => s,
464+
None => continue,
465+
};
466+
ret.extend(lib.foreign_items.iter().map(|id| {
467+
assert_eq!(id.krate, cnum);
468+
(*id, module.to_string())
469+
}));
470+
}
471+
472+
ret
473+
}

Diff for: compiler/rustc_codegen_ssa/src/back/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
419419
pub fn start_async_codegen<B: ExtraBackendMethods>(
420420
backend: B,
421421
tcx: TyCtxt<'_>,
422+
target_cpu: String,
422423
metadata: EncodedMetadata,
423424
total_cgus: usize,
424425
) -> OngoingCodegen<B> {
@@ -441,7 +442,7 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
441442
subsystem.to_string()
442443
});
443444

444-
let linker_info = LinkerInfo::new(tcx);
445+
let linker_info = LinkerInfo::new(tcx, target_cpu);
445446
let crate_info = CrateInfo::new(tcx);
446447

447448
let regular_config =

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,13 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
467467
pub fn codegen_crate<B: ExtraBackendMethods>(
468468
backend: B,
469469
tcx: TyCtxt<'tcx>,
470+
target_cpu: String,
470471
metadata: EncodedMetadata,
471472
need_metadata_module: bool,
472473
) -> OngoingCodegen<B> {
473474
// Skip crate items and just output metadata in -Z no-codegen mode.
474475
if tcx.sess.opts.debugging_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
475-
let ongoing_codegen = start_async_codegen(backend, tcx, metadata, 1);
476+
let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, metadata, 1);
476477

477478
ongoing_codegen.codegen_finished(tcx);
478479

@@ -498,7 +499,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
498499
}
499500
}
500501

501-
let ongoing_codegen = start_async_codegen(backend.clone(), tcx, metadata, codegen_units.len());
502+
let ongoing_codegen =
503+
start_async_codegen(backend.clone(), tcx, target_cpu, metadata, codegen_units.len());
502504
let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen));
503505

504506
// Codegen an allocator shim, if necessary.

0 commit comments

Comments
 (0)