1
Fork 0

Pass target_cpu to LinkerInfo::new instead of link_binary

This is one step towards separating the linking code from codegen backends
This commit is contained in:
bjorn3 2021-03-28 22:14:09 +02:00
parent 673c1b6e49
commit 808090eb07
7 changed files with 26 additions and 40 deletions

View file

@ -295,7 +295,7 @@ pub(super) fn run_aot(
metadata_module, metadata_module,
metadata, metadata,
windows_subsystem, windows_subsystem,
linker_info: LinkerInfo::new(tcx), linker_info: LinkerInfo::new(tcx, crate::target_triple(tcx.sess).to_string()),
crate_info: CrateInfo::new(tcx), crate_info: CrateInfo::new(tcx),
}, },
work_products, work_products,

View file

@ -267,13 +267,11 @@ impl CodegenBackend for CraneliftCodegenBackend {
) -> Result<(), ErrorReported> { ) -> Result<(), ErrorReported> {
use rustc_codegen_ssa::back::link::link_binary; use rustc_codegen_ssa::back::link::link_binary;
let target_cpu = crate::target_triple(sess).to_string();
link_binary::<crate::archive::ArArchiveBuilder<'_>>( link_binary::<crate::archive::ArArchiveBuilder<'_>>(
sess, sess,
&codegen_results, &codegen_results,
outputs, outputs,
&codegen_results.crate_name.as_str(), &codegen_results.crate_name.as_str(),
&target_cpu,
); );
Ok(()) Ok(())

View file

@ -270,6 +270,7 @@ impl CodegenBackend for LlvmCodegenBackend {
Box::new(rustc_codegen_ssa::base::codegen_crate( Box::new(rustc_codegen_ssa::base::codegen_crate(
LlvmCodegenBackend(()), LlvmCodegenBackend(()),
tcx, tcx,
crate::llvm_util::target_cpu(tcx.sess).to_string(),
metadata, metadata,
need_metadata_module, need_metadata_module,
)) ))
@ -305,13 +306,11 @@ impl CodegenBackend for LlvmCodegenBackend {
// Run the linker on any artifacts that resulted from the LLVM run. // Run the linker on any artifacts that resulted from the LLVM run.
// This should produce either a finished executable or library. // This should produce either a finished executable or library.
let target_cpu = crate::llvm_util::target_cpu(sess);
link_binary::<LlvmArchiveBuilder<'_>>( link_binary::<LlvmArchiveBuilder<'_>>(
sess, sess,
&codegen_results, &codegen_results,
outputs, outputs,
&codegen_results.crate_name.as_str(), &codegen_results.crate_name.as_str(),
target_cpu,
); );
Ok(()) Ok(())

View file

@ -50,7 +50,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
codegen_results: &CodegenResults, codegen_results: &CodegenResults,
outputs: &OutputFilenames, outputs: &OutputFilenames,
crate_name: &str, crate_name: &str,
target_cpu: &str,
) { ) {
let _timer = sess.timer("link_binary"); let _timer = sess.timer("link_binary");
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata); let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
@ -100,7 +99,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
&out_filename, &out_filename,
codegen_results, codegen_results,
path.as_ref(), path.as_ref(),
target_cpu,
); );
} }
} }
@ -531,7 +529,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
out_filename: &Path, out_filename: &Path,
codegen_results: &CodegenResults, codegen_results: &CodegenResults,
tmpdir: &Path, tmpdir: &Path,
target_cpu: &str,
) { ) {
info!("preparing {:?} to {:?}", crate_type, out_filename); info!("preparing {:?} to {:?}", crate_type, out_filename);
let (linker_path, flavor) = linker_and_flavor(sess); let (linker_path, flavor) = linker_and_flavor(sess);
@ -543,7 +540,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
tmpdir, tmpdir,
out_filename, out_filename,
codegen_results, codegen_results,
target_cpu,
); );
linker::disable_localization(&mut cmd); linker::disable_localization(&mut cmd);
@ -1617,14 +1613,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
tmpdir: &Path, tmpdir: &Path,
out_filename: &Path, out_filename: &Path,
codegen_results: &CodegenResults, codegen_results: &CodegenResults,
target_cpu: &str,
) -> Command { ) -> Command {
let crt_objects_fallback = crt_objects_fallback(sess, crate_type); let crt_objects_fallback = crt_objects_fallback(sess, crate_type);
let base_cmd = get_linker(sess, path, flavor, crt_objects_fallback); let base_cmd = get_linker(sess, path, flavor, crt_objects_fallback);
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction // FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
// to the linker args construction. // to the linker args construction.
assert!(base_cmd.get_args().is_empty() || sess.target.vendor == "uwp"); assert!(base_cmd.get_args().is_empty() || sess.target.vendor == "uwp");
let cmd = &mut *codegen_results.linker_info.to_linker(base_cmd, &sess, flavor, target_cpu); let cmd = &mut *codegen_results.linker_info.to_linker(base_cmd, &sess, flavor);
let link_output_kind = link_output_kind(sess, crate_type); let link_output_kind = link_output_kind(sess, crate_type);
// NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT // NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT

View file

@ -37,12 +37,14 @@ pub fn disable_localization(linker: &mut Command) {
/// need out of the shared crate context before we get rid of it. /// need out of the shared crate context before we get rid of it.
#[derive(Encodable, Decodable)] #[derive(Encodable, Decodable)]
pub struct LinkerInfo { pub struct LinkerInfo {
target_cpu: String,
exports: FxHashMap<CrateType, Vec<String>>, exports: FxHashMap<CrateType, Vec<String>>,
} }
impl LinkerInfo { impl LinkerInfo {
pub fn new(tcx: TyCtxt<'_>) -> LinkerInfo { pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> LinkerInfo {
LinkerInfo { LinkerInfo {
target_cpu,
exports: tcx exports: tcx
.sess .sess
.crate_types() .crate_types()
@ -57,38 +59,31 @@ impl LinkerInfo {
cmd: Command, cmd: Command,
sess: &'a Session, sess: &'a Session,
flavor: LinkerFlavor, flavor: LinkerFlavor,
target_cpu: &'a str,
) -> Box<dyn Linker + 'a> { ) -> Box<dyn Linker + 'a> {
match flavor { match flavor {
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => { LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => {
Box::new(MsvcLinker { cmd, sess, info: self }) as Box<dyn Linker> Box::new(MsvcLinker { cmd, sess, info: self }) as Box<dyn Linker>
} }
LinkerFlavor::Em => Box::new(EmLinker { cmd, sess, info: self }) as Box<dyn Linker>, LinkerFlavor::Em => Box::new(EmLinker { cmd, sess, info: self }) as Box<dyn Linker>,
LinkerFlavor::Gcc => Box::new(GccLinker { LinkerFlavor::Gcc => {
cmd, Box::new(GccLinker { cmd, sess, info: self, hinted_static: false, is_ld: false })
sess, as Box<dyn Linker>
info: self, }
hinted_static: false,
is_ld: false,
target_cpu,
}) as Box<dyn Linker>,
LinkerFlavor::Lld(LldFlavor::Ld) LinkerFlavor::Lld(LldFlavor::Ld)
| LinkerFlavor::Lld(LldFlavor::Ld64) | LinkerFlavor::Lld(LldFlavor::Ld64)
| LinkerFlavor::Ld => Box::new(GccLinker { | LinkerFlavor::Ld => {
cmd, Box::new(GccLinker { cmd, sess, info: self, hinted_static: false, is_ld: true })
sess, as Box<dyn Linker>
info: self, }
hinted_static: false,
is_ld: true,
target_cpu,
}) as Box<dyn Linker>,
LinkerFlavor::Lld(LldFlavor::Wasm) => { LinkerFlavor::Lld(LldFlavor::Wasm) => {
Box::new(WasmLd::new(cmd, sess, self)) as Box<dyn Linker> Box::new(WasmLd::new(cmd, sess, self)) as Box<dyn Linker>
} }
LinkerFlavor::PtxLinker => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>, LinkerFlavor::PtxLinker => {
Box::new(PtxLinker { cmd, sess, info: self }) as Box<dyn Linker>
}
} }
} }
} }
@ -156,7 +151,6 @@ pub struct GccLinker<'a> {
hinted_static: bool, // Keeps track of the current hinting mode. hinted_static: bool, // Keeps track of the current hinting mode.
// Link as ld // Link as ld
is_ld: bool, is_ld: bool,
target_cpu: &'a str,
} }
impl<'a> GccLinker<'a> { impl<'a> GccLinker<'a> {
@ -228,8 +222,7 @@ impl<'a> GccLinker<'a> {
}; };
self.linker_arg(&format!("-plugin-opt={}", opt_level)); self.linker_arg(&format!("-plugin-opt={}", opt_level));
let target_cpu = self.target_cpu; self.linker_arg(&format!("-plugin-opt=mcpu={}", self.info.target_cpu));
self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
} }
fn build_dylib(&mut self, out_filename: &Path) { fn build_dylib(&mut self, out_filename: &Path) {
@ -1276,6 +1269,7 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
pub struct PtxLinker<'a> { pub struct PtxLinker<'a> {
cmd: Command, cmd: Command,
sess: &'a Session, sess: &'a Session,
info: &'a LinkerInfo,
} }
impl<'a> Linker for PtxLinker<'a> { impl<'a> Linker for PtxLinker<'a> {
@ -1321,10 +1315,7 @@ impl<'a> Linker for PtxLinker<'a> {
fn finalize(&mut self) { fn finalize(&mut self) {
// Provide the linker with fallback to internal `target-cpu`. // Provide the linker with fallback to internal `target-cpu`.
self.cmd.arg("--fallback-arch").arg(match self.sess.opts.cg.target_cpu { self.cmd.arg("--fallback-arch").arg(&self.info.target_cpu);
Some(ref s) => s,
None => &self.sess.target.cpu,
});
} }
fn link_dylib(&mut self, _lib: Symbol) { fn link_dylib(&mut self, _lib: Symbol) {

View file

@ -426,6 +426,7 @@ fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
pub fn start_async_codegen<B: ExtraBackendMethods>( pub fn start_async_codegen<B: ExtraBackendMethods>(
backend: B, backend: B,
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
target_cpu: String,
metadata: EncodedMetadata, metadata: EncodedMetadata,
total_cgus: usize, total_cgus: usize,
) -> OngoingCodegen<B> { ) -> OngoingCodegen<B> {
@ -448,7 +449,7 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
subsystem.to_string() subsystem.to_string()
}); });
let linker_info = LinkerInfo::new(tcx); let linker_info = LinkerInfo::new(tcx, target_cpu);
let crate_info = CrateInfo::new(tcx); let crate_info = CrateInfo::new(tcx);
let regular_config = let regular_config =

View file

@ -461,12 +461,13 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
pub fn codegen_crate<B: ExtraBackendMethods>( pub fn codegen_crate<B: ExtraBackendMethods>(
backend: B, backend: B,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
target_cpu: String,
metadata: EncodedMetadata, metadata: EncodedMetadata,
need_metadata_module: bool, need_metadata_module: bool,
) -> OngoingCodegen<B> { ) -> OngoingCodegen<B> {
// Skip crate items and just output metadata in -Z no-codegen mode. // Skip crate items and just output metadata in -Z no-codegen mode.
if tcx.sess.opts.debugging_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() { if tcx.sess.opts.debugging_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
let ongoing_codegen = start_async_codegen(backend, tcx, metadata, 1); let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, metadata, 1);
ongoing_codegen.codegen_finished(tcx); ongoing_codegen.codegen_finished(tcx);
@ -492,7 +493,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
} }
} }
let ongoing_codegen = start_async_codegen(backend.clone(), tcx, metadata, codegen_units.len()); let ongoing_codegen =
start_async_codegen(backend.clone(), tcx, target_cpu, metadata, codegen_units.len());
let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen)); let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen));
// Codegen an allocator shim, if necessary. // Codegen an allocator shim, if necessary.