Skip to content

Commit 808090e

Browse files
committed
Pass target_cpu to LinkerInfo::new instead of link_binary
This is one step towards separating the linking code from codegen backends
1 parent 673c1b6 commit 808090e

File tree

7 files changed

+26
-40
lines changed

7 files changed

+26
-40
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ pub(super) fn run_aot(
295295
metadata_module,
296296
metadata,
297297
windows_subsystem,
298-
linker_info: LinkerInfo::new(tcx),
298+
linker_info: LinkerInfo::new(tcx, crate::target_triple(tcx.sess).to_string()),
299299
crate_info: CrateInfo::new(tcx),
300300
},
301301
work_products,

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,11 @@ impl CodegenBackend for CraneliftCodegenBackend {
267267
) -> Result<(), ErrorReported> {
268268
use rustc_codegen_ssa::back::link::link_binary;
269269

270-
let target_cpu = crate::target_triple(sess).to_string();
271270
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
272271
sess,
273272
&codegen_results,
274273
outputs,
275274
&codegen_results.crate_name.as_str(),
276-
&target_cpu,
277275
);
278276

279277
Ok(())

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl CodegenBackend for LlvmCodegenBackend {
270270
Box::new(rustc_codegen_ssa::base::codegen_crate(
271271
LlvmCodegenBackend(()),
272272
tcx,
273+
crate::llvm_util::target_cpu(tcx.sess).to_string(),
273274
metadata,
274275
need_metadata_module,
275276
))
@@ -305,13 +306,11 @@ impl CodegenBackend for LlvmCodegenBackend {
305306

306307
// Run the linker on any artifacts that resulted from the LLVM run.
307308
// This should produce either a finished executable or library.
308-
let target_cpu = crate::llvm_util::target_cpu(sess);
309309
link_binary::<LlvmArchiveBuilder<'_>>(
310310
sess,
311311
&codegen_results,
312312
outputs,
313313
&codegen_results.crate_name.as_str(),
314-
target_cpu,
315314
);
316315

317316
Ok(())

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 6 deletions
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
}
@@ -531,7 +529,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
531529
out_filename: &Path,
532530
codegen_results: &CodegenResults,
533531
tmpdir: &Path,
534-
target_cpu: &str,
535532
) {
536533
info!("preparing {:?} to {:?}", crate_type, out_filename);
537534
let (linker_path, flavor) = linker_and_flavor(sess);
@@ -543,7 +540,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
543540
tmpdir,
544541
out_filename,
545542
codegen_results,
546-
target_cpu,
547543
);
548544

549545
linker::disable_localization(&mut cmd);
@@ -1617,14 +1613,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
16171613
tmpdir: &Path,
16181614
out_filename: &Path,
16191615
codegen_results: &CodegenResults,
1620-
target_cpu: &str,
16211616
) -> Command {
16221617
let crt_objects_fallback = crt_objects_fallback(sess, crate_type);
16231618
let base_cmd = get_linker(sess, path, flavor, crt_objects_fallback);
16241619
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
16251620
// to the linker args construction.
16261621
assert!(base_cmd.get_args().is_empty() || sess.target.vendor == "uwp");
1627-
let cmd = &mut *codegen_results.linker_info.to_linker(base_cmd, &sess, flavor, target_cpu);
1622+
let cmd = &mut *codegen_results.linker_info.to_linker(base_cmd, &sess, flavor);
16281623
let link_output_kind = link_output_kind(sess, crate_type);
16291624

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

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 17 additions & 26 deletions
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
}
@@ -156,7 +151,6 @@ pub struct GccLinker<'a> {
156151
hinted_static: bool, // Keeps track of the current hinting mode.
157152
// Link as ld
158153
is_ld: bool,
159-
target_cpu: &'a str,
160154
}
161155

162156
impl<'a> GccLinker<'a> {
@@ -228,8 +222,7 @@ impl<'a> GccLinker<'a> {
228222
};
229223

230224
self.linker_arg(&format!("-plugin-opt={}", opt_level));
231-
let target_cpu = self.target_cpu;
232-
self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
225+
self.linker_arg(&format!("-plugin-opt=mcpu={}", self.info.target_cpu));
233226
}
234227

235228
fn build_dylib(&mut self, out_filename: &Path) {
@@ -1276,6 +1269,7 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
12761269
pub struct PtxLinker<'a> {
12771270
cmd: Command,
12781271
sess: &'a Session,
1272+
info: &'a LinkerInfo,
12791273
}
12801274

12811275
impl<'a> Linker for PtxLinker<'a> {
@@ -1321,10 +1315,7 @@ impl<'a> Linker for PtxLinker<'a> {
13211315

13221316
fn finalize(&mut self) {
13231317
// Provide the linker with fallback to internal `target-cpu`.
1324-
self.cmd.arg("--fallback-arch").arg(match self.sess.opts.cg.target_cpu {
1325-
Some(ref s) => s,
1326-
None => &self.sess.target.cpu,
1327-
});
1318+
self.cmd.arg("--fallback-arch").arg(&self.info.target_cpu);
13281319
}
13291320

13301321
fn link_dylib(&mut self, _lib: Symbol) {

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
426426
pub fn start_async_codegen<B: ExtraBackendMethods>(
427427
backend: B,
428428
tcx: TyCtxt<'_>,
429+
target_cpu: String,
429430
metadata: EncodedMetadata,
430431
total_cgus: usize,
431432
) -> OngoingCodegen<B> {
@@ -448,7 +449,7 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
448449
subsystem.to_string()
449450
});
450451

451-
let linker_info = LinkerInfo::new(tcx);
452+
let linker_info = LinkerInfo::new(tcx, target_cpu);
452453
let crate_info = CrateInfo::new(tcx);
453454

454455
let regular_config =

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,13 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
461461
pub fn codegen_crate<B: ExtraBackendMethods>(
462462
backend: B,
463463
tcx: TyCtxt<'tcx>,
464+
target_cpu: String,
464465
metadata: EncodedMetadata,
465466
need_metadata_module: bool,
466467
) -> OngoingCodegen<B> {
467468
// Skip crate items and just output metadata in -Z no-codegen mode.
468469
if tcx.sess.opts.debugging_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
469-
let ongoing_codegen = start_async_codegen(backend, tcx, metadata, 1);
470+
let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, metadata, 1);
470471

471472
ongoing_codegen.codegen_finished(tcx);
472473

@@ -492,7 +493,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
492493
}
493494
}
494495

495-
let ongoing_codegen = start_async_codegen(backend.clone(), tcx, metadata, codegen_units.len());
496+
let ongoing_codegen =
497+
start_async_codegen(backend.clone(), tcx, target_cpu, metadata, codegen_units.len());
496498
let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen));
497499

498500
// Codegen an allocator shim, if necessary.

0 commit comments

Comments
 (0)