Auto merge of #85810 - bjorn3:further_driver_cleanup, r=varkor
Driver improvements This PR contains a couple of cleanups for the driver and a few small improvements for the custom codegen backend interface. It also implements `--version` and `-Cpasses=list` support for custom codegen backends.
This commit is contained in:
commit
cc9610bf5a
13 changed files with 103 additions and 135 deletions
|
@ -245,8 +245,7 @@ impl<'tcx> Queries<'tcx> {
|
|||
self.prepare_outputs.compute(|| {
|
||||
let expansion_result = self.expansion()?;
|
||||
let (krate, boxed_resolver, _) = &*expansion_result.peek();
|
||||
let crate_name = self.crate_name()?;
|
||||
let crate_name = crate_name.peek();
|
||||
let crate_name = self.crate_name()?.peek();
|
||||
passes::prepare_outputs(
|
||||
self.session(),
|
||||
self.compiler,
|
||||
|
@ -343,32 +342,36 @@ impl<'tcx> Queries<'tcx> {
|
|||
}
|
||||
|
||||
pub fn linker(&'tcx self) -> Result<Linker> {
|
||||
let dep_graph = self.dep_graph()?;
|
||||
let prepare_outputs = self.prepare_outputs()?;
|
||||
let crate_hash = self.global_ctxt()?.peek_mut().enter(|tcx| tcx.crate_hash(LOCAL_CRATE));
|
||||
let ongoing_codegen = self.ongoing_codegen()?;
|
||||
|
||||
let sess = self.session().clone();
|
||||
let codegen_backend = self.codegen_backend().clone();
|
||||
|
||||
let dep_graph = self.dep_graph()?.peek().clone();
|
||||
let prepare_outputs = self.prepare_outputs()?.take();
|
||||
let crate_hash = self.global_ctxt()?.peek_mut().enter(|tcx| tcx.crate_hash(LOCAL_CRATE));
|
||||
let ongoing_codegen = self.ongoing_codegen()?.take();
|
||||
|
||||
Ok(Linker {
|
||||
sess,
|
||||
dep_graph: dep_graph.peek().clone(),
|
||||
prepare_outputs: prepare_outputs.take(),
|
||||
crate_hash,
|
||||
ongoing_codegen: ongoing_codegen.take(),
|
||||
codegen_backend,
|
||||
|
||||
dep_graph,
|
||||
prepare_outputs,
|
||||
crate_hash,
|
||||
ongoing_codegen,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Linker {
|
||||
// compilation inputs
|
||||
sess: Lrc<Session>,
|
||||
codegen_backend: Lrc<Box<dyn CodegenBackend>>,
|
||||
|
||||
// compilation outputs
|
||||
dep_graph: DepGraph,
|
||||
prepare_outputs: OutputFilenames,
|
||||
crate_hash: Svh,
|
||||
ongoing_codegen: Box<dyn Any>,
|
||||
codegen_backend: Lrc<Box<dyn CodegenBackend>>,
|
||||
}
|
||||
|
||||
impl Linker {
|
||||
|
|
|
@ -32,7 +32,7 @@ use std::ops::DerefMut;
|
|||
use std::panic;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex, Once};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use tracing::info;
|
||||
|
||||
|
@ -73,7 +73,10 @@ pub fn create_session(
|
|||
let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
|
||||
make_codegen_backend(&sopts)
|
||||
} else {
|
||||
get_codegen_backend(&sopts)
|
||||
get_codegen_backend(
|
||||
&sopts.maybe_sysroot,
|
||||
sopts.debugging_opts.codegen_backend.as_ref().map(|name| &name[..]),
|
||||
)
|
||||
};
|
||||
|
||||
// target_override is documented to be called before init(), so this is okay
|
||||
|
@ -241,35 +244,34 @@ fn load_backend_from_dylib(path: &Path) -> fn() -> Box<dyn CodegenBackend> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_codegen_backend(sopts: &config::Options) -> Box<dyn CodegenBackend> {
|
||||
static INIT: Once = Once::new();
|
||||
/// Get the codegen backend based on the name and specified sysroot.
|
||||
///
|
||||
/// A name of `None` indicates that the default backend should be used.
|
||||
pub fn get_codegen_backend(
|
||||
maybe_sysroot: &Option<PathBuf>,
|
||||
backend_name: Option<&str>,
|
||||
) -> Box<dyn CodegenBackend> {
|
||||
static LOAD: SyncOnceCell<unsafe fn() -> Box<dyn CodegenBackend>> = SyncOnceCell::new();
|
||||
|
||||
static mut LOAD: fn() -> Box<dyn CodegenBackend> = || unreachable!();
|
||||
|
||||
INIT.call_once(|| {
|
||||
let load = LOAD.get_or_init(|| {
|
||||
#[cfg(feature = "llvm")]
|
||||
const DEFAULT_CODEGEN_BACKEND: &str = "llvm";
|
||||
|
||||
#[cfg(not(feature = "llvm"))]
|
||||
const DEFAULT_CODEGEN_BACKEND: &str = "cranelift";
|
||||
|
||||
let codegen_name = sopts
|
||||
.debugging_opts
|
||||
.codegen_backend
|
||||
.as_ref()
|
||||
.map(|name| &name[..])
|
||||
.unwrap_or(DEFAULT_CODEGEN_BACKEND);
|
||||
|
||||
let backend = match codegen_name {
|
||||
match backend_name.unwrap_or(DEFAULT_CODEGEN_BACKEND) {
|
||||
filename if filename.contains('.') => load_backend_from_dylib(filename.as_ref()),
|
||||
codegen_name => get_builtin_codegen_backend(&sopts.maybe_sysroot, codegen_name),
|
||||
};
|
||||
|
||||
unsafe {
|
||||
LOAD = backend;
|
||||
#[cfg(feature = "llvm")]
|
||||
"llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
|
||||
backend_name => get_codegen_sysroot(maybe_sysroot, backend_name),
|
||||
}
|
||||
});
|
||||
unsafe { LOAD() }
|
||||
|
||||
// SAFETY: In case of a builtin codegen backend this is safe. In case of an external codegen
|
||||
// backend we hope that the backend links against the same rustc_driver version. If this is not
|
||||
// the case, we get UB.
|
||||
unsafe { load() }
|
||||
}
|
||||
|
||||
// This is used for rustdoc, but it uses similar machinery to codegen backend
|
||||
|
@ -387,17 +389,6 @@ fn sysroot_candidates() -> Vec<PathBuf> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_builtin_codegen_backend(
|
||||
maybe_sysroot: &Option<PathBuf>,
|
||||
backend_name: &str,
|
||||
) -> fn() -> Box<dyn CodegenBackend> {
|
||||
match backend_name {
|
||||
#[cfg(feature = "llvm")]
|
||||
"llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
|
||||
_ => get_codegen_sysroot(maybe_sysroot, backend_name),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_codegen_sysroot(
|
||||
maybe_sysroot: &Option<PathBuf>,
|
||||
backend_name: &str,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue