Rollup merge of #69965 - mark-i-m:codegen-utils, r=eddyb

Refactorings to get rid of rustc_codegen_utils

r? @eddyb

cc #45276

After this, the only modules left in `rustc_codegen_utils` are
- `link`: a bunch of linking-related functions (many dealing with file names). These are mostly consumed by save analysis, rustc_driver, rustc_interface, and of course codegen. I assume they live here because we don't want a dependency of save analysis on codegen... Perhaps they can be moved to librustc?
- ~`symbol_names` and `symbol_names_test`: honestly it seems odd that `symbol_names_test` is not a submodule of `symbol_names`. It seems like these could honestly live in their own crate or move to librustc. Already name mangling is exported as the `symbol_name` query.~ (move it to its own crate)

I don't mind doing either of the above as part of this PR or a followup if you want.
This commit is contained in:
Mazdak Farrokhzad 2020-03-21 08:51:14 +01:00 committed by GitHub
commit 0b99489a89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 265 additions and 290 deletions

View file

@ -25,7 +25,7 @@ rustc_span = { path = "../librustc_span" }
rustc = { path = "../librustc" }
rustc_apfloat = { path = "../librustc_apfloat" }
rustc_attr = { path = "../librustc_attr" }
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
rustc_symbol_mangling = { path = "../librustc_symbol_mangling" }
rustc_data_structures = { path = "../librustc_data_structures"}
rustc_errors = { path = "../librustc_errors" }
rustc_fs_util = { path = "../librustc_fs_util" }
@ -34,3 +34,4 @@ rustc_incremental = { path = "../librustc_incremental" }
rustc_index = { path = "../librustc_index" }
rustc_target = { path = "../librustc_target" }
rustc_session = { path = "../librustc_session" }
rustc_metadata = { path = "../librustc_metadata" }

View file

@ -6,6 +6,7 @@ use rustc_hir::def_id::CrateNum;
use rustc_session::config::{
self, CFGuard, DebugInfo, OutputFilenames, OutputType, PrintRequest, Sanitizer,
};
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
use rustc_session::search_paths::PathKind;
/// For all the linkers we support, and information they might
/// need out of the shared crate context before we get rid of it.
@ -36,8 +37,6 @@ use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Output, Stdio};
use std::str;
pub use rustc_codegen_utils::link::*;
pub fn remove(sess: &Session, path: &Path) {
if let Err(e) = fs::remove_file(path) {
sess.err(&format!("failed to remove {}: {}", path.display(), e));

View file

@ -8,7 +8,6 @@ use rustc::ty::subst::{GenericArgKind, SubstsRef};
use rustc::ty::Instance;
use rustc::ty::{SymbolName, TyCtxt};
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
use rustc_codegen_utils::symbol_names;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
@ -423,17 +422,21 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
// This is something instantiated in an upstream crate, so we have to use
// the slower (because uncached) version of computing the symbol name.
match symbol {
ExportedSymbol::NonGeneric(def_id) => symbol_names::symbol_name_for_instance_in_crate(
tcx,
Instance::mono(tcx, def_id),
instantiating_crate,
),
ExportedSymbol::Generic(def_id, substs) => symbol_names::symbol_name_for_instance_in_crate(
tcx,
Instance::new(def_id, substs),
instantiating_crate,
),
ExportedSymbol::DropGlue(ty) => symbol_names::symbol_name_for_instance_in_crate(
ExportedSymbol::NonGeneric(def_id) => {
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::mono(tcx, def_id),
instantiating_crate,
)
}
ExportedSymbol::Generic(def_id, substs) => {
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::new(def_id, substs),
instantiating_crate,
)
}
ExportedSymbol::DropGlue(ty) => rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::resolve_drop_in_place(tcx, ty),
instantiating_crate,

View file

@ -36,7 +36,6 @@ use rustc::ty::layout::{FAT_PTR_ADDR, FAT_PTR_EXTRA};
use rustc::ty::query::Providers;
use rustc::ty::{self, Instance, Ty, TyCtxt};
use rustc_attr as attr;
use rustc_codegen_utils::{check_for_rustc_errors_attr, symbol_names_test};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::profiling::print_time_passes_entry;
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
@ -47,6 +46,7 @@ use rustc_session::cgu_reuse_tracker::CguReuse;
use rustc_session::config::{self, EntryFnType, Lto};
use rustc_session::Session;
use rustc_span::Span;
use rustc_symbol_mangling::test as symbol_names_test;
use std::cmp;
use std::ops::{Deref, DerefMut};
@ -514,8 +514,6 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> OngoingCodegen<B> {
check_for_rustc_errors_attr(tcx);
// 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);

View file

@ -2,15 +2,22 @@ use super::write::WriteBackendMethods;
use super::CodegenObject;
use crate::ModuleCodegen;
use rustc::middle::cstore::EncodedMetadata;
use rustc::dep_graph::DepGraph;
use rustc::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
use rustc::ty::layout::{HasTyCtxt, LayoutOf, TyLayout};
use rustc::ty::Ty;
use rustc::ty::TyCtxt;
use rustc::ty::query::Providers;
use rustc::ty::{Ty, TyCtxt};
use rustc::util::common::ErrorReported;
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_session::{config, Session};
use rustc_session::{
config::{self, OutputFilenames, PrintRequest},
Session,
};
use rustc_span::symbol::Symbol;
pub use rustc_data_structures::sync::MetadataRef;
use std::any::Any;
use std::sync::Arc;
pub trait BackendTypes {
@ -37,6 +44,50 @@ impl<'tcx, T> Backend<'tcx> for T where
{
}
pub trait CodegenBackend {
fn init(&self, _sess: &Session) {}
fn print(&self, _req: PrintRequest, _sess: &Session) {}
fn target_features(&self, _sess: &Session) -> Vec<Symbol> {
vec![]
}
fn print_passes(&self) {}
fn print_version(&self) {}
fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
fn provide(&self, _providers: &mut Providers<'_>);
fn provide_extern(&self, _providers: &mut Providers<'_>);
fn codegen_crate<'tcx>(
&self,
tcx: TyCtxt<'tcx>,
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> Box<dyn Any>;
/// This is called on the returned `Box<dyn Any>` from `codegen_backend`
///
/// # Panics
///
/// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
fn join_codegen(
&self,
ongoing_codegen: Box<dyn Any>,
sess: &Session,
dep_graph: &DepGraph,
) -> Result<Box<dyn Any>, ErrorReported>;
/// This is called on the returned `Box<dyn Any>` from `join_codegen`
///
/// # Panics
///
/// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
fn link(
&self,
sess: &Session,
codegen_results: Box<dyn Any>,
outputs: &OutputFilenames,
) -> Result<(), ErrorReported>;
}
pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
fn write_compressed_metadata<'tcx>(

View file

@ -29,7 +29,7 @@ mod write;
pub use self::abi::AbiBuilderMethods;
pub use self::asm::{AsmBuilderMethods, AsmMethods};
pub use self::backend::{Backend, BackendTypes, ExtraBackendMethods};
pub use self::backend::{Backend, BackendTypes, CodegenBackend, ExtraBackendMethods};
pub use self::builder::{BuilderMethods, OverflowOp};
pub use self::consts::ConstMethods;
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};