Skip to content

Commit

Permalink
Auto merge of #83610 - bjorn3:driver_cleanup, r=cjgillot
Browse files Browse the repository at this point in the history
rustc_driver cleanup

Best reviewed one commit at a time.
  • Loading branch information
bors committed May 12, 2021
2 parents c1e7e36 + 163b480 commit ac923d9
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 224 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub(crate) fn run_aot(
metadata_module,
metadata,
windows_subsystem,
linker_info: LinkerInfo::new(tcx),
linker_info: LinkerInfo::new(tcx, crate::target_triple(tcx.sess).to_string()),
crate_info: CrateInfo::new(tcx),
},
work_products,
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,11 @@ impl CodegenBackend for CraneliftCodegenBackend {
) -> Result<(), ErrorReported> {
use rustc_codegen_ssa::back::link::link_binary;

let target_cpu = crate::target_triple(sess).to_string();
link_binary::<crate::archive::ArArchiveBuilder<'_>>(
sess,
&codegen_results,
outputs,
&codegen_results.crate_name.as_str(),
&target_cpu,
);

Ok(())
Expand Down
31 changes: 0 additions & 31 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use std::ffi::CString;

use cstr::cstr;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::ty::layout::HasTyCtxt;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::config::OptLevel;
use rustc_session::Session;
Expand Down Expand Up @@ -355,35 +353,6 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
}
}

pub fn provide_both(providers: &mut Providers) {
providers.wasm_import_module_map = |tcx, cnum| {
// Build up a map from DefId to a `NativeLib` structure, where
// `NativeLib` internally contains information about
// `#[link(wasm_import_module = "...")]` for example.
let native_libs = tcx.native_libraries(cnum);

let def_id_to_native_lib = native_libs
.iter()
.filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
.collect::<FxHashMap<_, _>>();

let mut ret = FxHashMap::default();
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
let module = match module {
Some(s) => s,
None => continue,
};
ret.extend(lib.foreign_items.iter().map(|id| {
assert_eq!(id.krate, cnum);
(*id, module.to_string())
}));
}

ret
};
}

fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<CString> {
tcx.wasm_import_module_map(id.krate).get(&id).map(|s| CString::new(&s[..]).unwrap())
}
12 changes: 3 additions & 9 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,8 @@ impl CodegenBackend for LlvmCodegenBackend {
Box::new(metadata::LlvmMetadataLoader)
}

fn provide(&self, providers: &mut ty::query::Providers) {
attributes::provide_both(providers);
}

fn provide_extern(&self, providers: &mut ty::query::Providers) {
attributes::provide_both(providers);
}
fn provide(&self, _providers: &mut ty::query::Providers) {}
fn provide_extern(&self, _providers: &mut ty::query::Providers) {}

fn codegen_crate<'tcx>(
&self,
Expand All @@ -271,6 +266,7 @@ impl CodegenBackend for LlvmCodegenBackend {
Box::new(rustc_codegen_ssa::base::codegen_crate(
LlvmCodegenBackend(()),
tcx,
crate::llvm_util::target_cpu(tcx.sess).to_string(),
metadata,
need_metadata_module,
))
Expand Down Expand Up @@ -306,13 +302,11 @@ impl CodegenBackend for LlvmCodegenBackend {

// Run the linker on any artifacts that resulted from the LLVM run.
// This should produce either a finished executable or library.
let target_cpu = crate::llvm_util::target_cpu(sess);
link_binary::<LlvmArchiveBuilder<'_>>(
sess,
&codegen_results,
outputs,
&codegen_results.crate_name.as_str(),
target_cpu,
);

Ok(())
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
codegen_results: &CodegenResults,
outputs: &OutputFilenames,
crate_name: &str,
target_cpu: &str,
) {
let _timer = sess.timer("link_binary");
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
Expand Down Expand Up @@ -100,7 +99,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
&out_filename,
codegen_results,
path.as_ref(),
target_cpu,
);
}
}
Expand Down Expand Up @@ -532,7 +530,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
out_filename: &Path,
codegen_results: &CodegenResults,
tmpdir: &Path,
target_cpu: &str,
) {
info!("preparing {:?} to {:?}", crate_type, out_filename);
let (linker_path, flavor) = linker_and_flavor(sess);
Expand All @@ -544,7 +541,6 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(
tmpdir,
out_filename,
codegen_results,
target_cpu,
);

linker::disable_localization(&mut cmd);
Expand Down Expand Up @@ -1609,14 +1605,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
tmpdir: &Path,
out_filename: &Path,
codegen_results: &CodegenResults,
target_cpu: &str,
) -> Command {
let crt_objects_fallback = crt_objects_fallback(sess, crate_type);
let base_cmd = get_linker(sess, path, flavor, crt_objects_fallback);
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
// to the linker args construction.
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);

// NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
Expand Down
43 changes: 17 additions & 26 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ pub fn disable_localization(linker: &mut Command) {
/// need out of the shared crate context before we get rid of it.
#[derive(Encodable, Decodable)]
pub struct LinkerInfo {
target_cpu: String,
exports: FxHashMap<CrateType, Vec<String>>,
}

impl LinkerInfo {
pub fn new(tcx: TyCtxt<'_>) -> LinkerInfo {
pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> LinkerInfo {
LinkerInfo {
target_cpu,
exports: tcx
.sess
.crate_types()
Expand All @@ -57,38 +59,31 @@ impl LinkerInfo {
cmd: Command,
sess: &'a Session,
flavor: LinkerFlavor,
target_cpu: &'a str,
) -> Box<dyn Linker + 'a> {
match flavor {
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => {
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::Gcc => Box::new(GccLinker {
cmd,
sess,
info: self,
hinted_static: false,
is_ld: false,
target_cpu,
}) as Box<dyn Linker>,
LinkerFlavor::Gcc => {
Box::new(GccLinker { cmd, sess, info: self, hinted_static: false, is_ld: false })
as Box<dyn Linker>
}

LinkerFlavor::Lld(LldFlavor::Ld)
| LinkerFlavor::Lld(LldFlavor::Ld64)
| LinkerFlavor::Ld => Box::new(GccLinker {
cmd,
sess,
info: self,
hinted_static: false,
is_ld: true,
target_cpu,
}) as Box<dyn Linker>,
| LinkerFlavor::Ld => {
Box::new(GccLinker { cmd, sess, info: self, hinted_static: false, is_ld: true })
as Box<dyn Linker>
}

LinkerFlavor::Lld(LldFlavor::Wasm) => {
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>
}
}
}
}
Expand Down Expand Up @@ -157,7 +152,6 @@ pub struct GccLinker<'a> {
hinted_static: bool, // Keeps track of the current hinting mode.
// Link as ld
is_ld: bool,
target_cpu: &'a str,
}

impl<'a> GccLinker<'a> {
Expand Down Expand Up @@ -229,8 +223,7 @@ impl<'a> GccLinker<'a> {
};

self.linker_arg(&format!("-plugin-opt={}", opt_level));
let target_cpu = self.target_cpu;
self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
self.linker_arg(&format!("-plugin-opt=mcpu={}", self.info.target_cpu));
}

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

impl<'a> Linker for PtxLinker<'a> {
Expand Down Expand Up @@ -1381,10 +1375,7 @@ impl<'a> Linker for PtxLinker<'a> {

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

fn link_dylib(&mut self, _lib: Symbol, _verbatim: bool, _as_needed: bool) {
Expand Down
29 changes: 29 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,13 @@ pub fn provide(providers: &mut Providers) {
providers.upstream_monomorphizations = upstream_monomorphizations_provider;
providers.is_unreachable_local_definition = is_unreachable_local_definition_provider;
providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
providers.wasm_import_module_map = wasm_import_module_map;
}

pub fn provide_extern(providers: &mut Providers) {
providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
providers.wasm_import_module_map = wasm_import_module_map;
}

fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel {
Expand Down Expand Up @@ -442,3 +444,30 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
}
}

fn wasm_import_module_map(tcx: TyCtxt<'_>, cnum: CrateNum) -> FxHashMap<DefId, String> {
// Build up a map from DefId to a `NativeLib` structure, where
// `NativeLib` internally contains information about
// `#[link(wasm_import_module = "...")]` for example.
let native_libs = tcx.native_libraries(cnum);

let def_id_to_native_lib = native_libs
.iter()
.filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
.collect::<FxHashMap<_, _>>();

let mut ret = FxHashMap::default();
for (def_id, lib) in tcx.foreign_modules(cnum).iter() {
let module = def_id_to_native_lib.get(&def_id).and_then(|s| s.wasm_import_module);
let module = match module {
Some(s) => s,
None => continue,
};
ret.extend(lib.foreign_items.iter().map(|id| {
assert_eq!(id.krate, cnum);
(*id, module.to_string())
}));
}

ret
}
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
pub fn start_async_codegen<B: ExtraBackendMethods>(
backend: B,
tcx: TyCtxt<'_>,
target_cpu: String,
metadata: EncodedMetadata,
total_cgus: usize,
) -> OngoingCodegen<B> {
Expand All @@ -441,7 +442,7 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
subsystem.to_string()
});

let linker_info = LinkerInfo::new(tcx);
let linker_info = LinkerInfo::new(tcx, target_cpu);
let crate_info = CrateInfo::new(tcx);

let regular_config =
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,13 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
pub fn codegen_crate<B: ExtraBackendMethods>(
backend: B,
tcx: TyCtxt<'tcx>,
target_cpu: String,
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> OngoingCodegen<B> {
// 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() {
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);

Expand All @@ -498,7 +499,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));

// Codegen an allocator shim, if necessary.
Expand Down
Loading

0 comments on commit ac923d9

Please sign in to comment.