From a03d19ef63d342c408d7ec8208bda5b4eb0bacf5 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 14 Feb 2024 23:43:00 +0000 Subject: [PATCH 1/3] Allow targets to override default codegen backend --- compiler/rustc_driver_impl/src/lib.rs | 19 +++++- compiler/rustc_interface/src/interface.rs | 76 +++++++++++++++++++---- compiler/rustc_interface/src/tests.rs | 14 ++++- compiler/rustc_interface/src/util.rs | 47 +++++++------- compiler/rustc_session/src/config.rs | 2 +- compiler/rustc_session/src/session.rs | 9 +-- compiler/rustc_target/src/spec/mod.rs | 9 +++ 7 files changed, 130 insertions(+), 46 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 60f11b1bdd490..bc485b0522320 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -35,7 +35,7 @@ use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS}; use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType}; use rustc_session::getopts::{self, Matches}; use rustc_session::lint::{Lint, LintId}; -use rustc_session::{config, EarlyDiagCtxt, Session}; +use rustc_session::{config, filesearch, EarlyDiagCtxt, Session}; use rustc_span::def_id::LOCAL_CRATE; use rustc_span::source_map::FileLoader; use rustc_span::symbol::sym; @@ -887,7 +887,13 @@ pub fn version_at_macro_invocation( let debug_flags = matches.opt_strs("Z"); let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend=")); - get_codegen_backend(early_dcx, &None, backend_name).print_version(); + let opts = config::Options::default(); + let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| { + filesearch::get_or_default_sysroot().expect("Failed finding sysroot") + }); + let target = config::build_target_config(early_dcx, &opts, None, &sysroot); + + get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version(); } } @@ -1092,7 +1098,14 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) -> if cg_flags.iter().any(|x| *x == "passes=list") { let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend=")); - get_codegen_backend(early_dcx, &None, backend_name).print_passes(); + + let opts = config::Options::default(); + let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| { + filesearch::get_or_default_sysroot().expect("Failed finding sysroot") + }); + let target = config::build_target_config(early_dcx, &opts, None, &sysroot); + + get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes(); return true; } diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 8a4705e0056e1..f53b320f2e0f8 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -16,7 +16,7 @@ use rustc_parse::maybe_new_parser_from_source_str; use rustc_query_impl::QueryCtxt; use rustc_query_system::query::print_query_stack; use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName}; -use rustc_session::filesearch::sysroot_candidates; +use rustc_session::filesearch::{self, sysroot_candidates}; use rustc_session::parse::ParseSess; use rustc_session::{lint, CompilerIO, EarlyDiagCtxt, Session}; use rustc_span::source_map::FileLoader; @@ -336,14 +336,66 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se let early_dcx = EarlyDiagCtxt::new(config.opts.error_format); - let codegen_backend = if let Some(make_codegen_backend) = config.make_codegen_backend { - make_codegen_backend(&config.opts) - } else { - util::get_codegen_backend( - &early_dcx, - &config.opts.maybe_sysroot, - config.opts.unstable_opts.codegen_backend.as_deref(), - ) + let sysroot = match &config.opts.maybe_sysroot { + Some(sysroot) => sysroot.clone(), + None => filesearch::get_or_default_sysroot().expect("Failed finding sysroot"), + }; + + let (codegen_backend, target_cfg) = match config.make_codegen_backend { + None => { + // Build a target without override, so that it can override the backend if needed + let target = + config::build_target_config(&early_dcx, &config.opts, None, &sysroot); + + let backend = util::get_codegen_backend( + &early_dcx, + &sysroot, + config.opts.unstable_opts.codegen_backend.as_deref(), + &target, + ); + + // target_override is documented to be called before init(), so this is okay + let target_override = backend.target_override(&config.opts); + + // Assert that we don't use target's override of the backend and + // backend's override of the target at the same time + if config.opts.unstable_opts.codegen_backend.is_none() + && target.default_codegen_backend.is_some() + && target_override.is_some() + { + rustc_middle::bug!( + "Codegen backend requested target override even though the target requested the backend" + ); + } + + // Re-build target with the (potential) override + let target = config::build_target_config( + &early_dcx, + &config.opts, + target_override, + &sysroot, + ); + + (backend, target) + } + Some(make_codegen_backend) => { + // N.B. `make_codegen_backend` takes precedence over `target.default_codegen_backend`, + // which is ignored in this case. + + let backend = make_codegen_backend(&config.opts); + + // target_override is documented to be called before init(), so this is okay + let target_override = backend.target_override(&config.opts); + + let target = config::build_target_config( + &early_dcx, + &config.opts, + target_override, + &sysroot, + ); + + (backend, target) + } }; let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from); @@ -364,9 +416,6 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se let mut locale_resources = Vec::from(config.locale_resources); locale_resources.push(codegen_backend.locale_resource()); - // target_override is documented to be called before init(), so this is okay - let target_override = codegen_backend.target_override(&config.opts); - let mut sess = rustc_session::build_session( early_dcx, config.opts, @@ -381,7 +430,8 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se locale_resources, config.lint_caps, config.file_loader, - target_override, + target_cfg, + sysroot, util::rustc_version_str().unwrap_or("unknown"), config.ice_file, config.using_internal_features, diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index bfc4fc07d4cc7..b1d874d72046b 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -37,6 +37,17 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) { output_file: None, temps_dir, }; + + let sysroot = match &sessopts.maybe_sysroot { + Some(sysroot) => sysroot.clone(), + None => { + rustc_session::filesearch::get_or_default_sysroot().expect("Failed finding sysroot") + } + }; + + let target_cfg = + rustc_session::config::build_target_config(&early_dcx, &sessopts, None, &sysroot); + let sess = build_session( early_dcx, sessopts, @@ -46,7 +57,8 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) { vec![], Default::default(), None, - None, + target_cfg, + sysroot, "", None, Arc::default(), diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 76b9e8de75fb0..38c5157d45010 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -14,14 +14,15 @@ use rustc_session::{filesearch, output, Session}; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edition::Edition; use rustc_span::symbol::{sym, Symbol}; +use rustc_target::spec::Target; use session::EarlyDiagCtxt; -use std::env; use std::env::consts::{DLL_PREFIX, DLL_SUFFIX}; use std::mem; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::OnceLock; use std::thread; +use std::{env, iter}; /// Function pointer type that constructs a new CodegenBackend. pub type MakeBackendFn = fn() -> Box; @@ -192,21 +193,25 @@ fn load_backend_from_dylib(early_dcx: &EarlyDiagCtxt, path: &Path) -> MakeBacken /// A name of `None` indicates that the default backend should be used. pub fn get_codegen_backend( early_dcx: &EarlyDiagCtxt, - maybe_sysroot: &Option, + sysroot: &Path, backend_name: Option<&str>, + target: &Target, ) -> Box { static LOAD: OnceLock Box> = OnceLock::new(); let load = LOAD.get_or_init(|| { - let default_codegen_backend = option_env!("CFG_DEFAULT_CODEGEN_BACKEND").unwrap_or("llvm"); + let backend = backend_name + .or(target.default_codegen_backend.as_deref()) + .or(option_env!("CFG_DEFAULT_CODEGEN_BACKEND")) + .unwrap_or("llvm"); - match backend_name.unwrap_or(default_codegen_backend) { + match backend { filename if filename.contains('.') => { load_backend_from_dylib(early_dcx, filename.as_ref()) } #[cfg(feature = "llvm")] "llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new, - backend_name => get_codegen_sysroot(early_dcx, maybe_sysroot, backend_name), + backend_name => get_codegen_sysroot(early_dcx, sysroot, backend_name), } }); @@ -240,7 +245,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option { fn get_codegen_sysroot( early_dcx: &EarlyDiagCtxt, - maybe_sysroot: &Option, + sysroot: &Path, backend_name: &str, ) -> MakeBackendFn { // For now we only allow this function to be called once as it'll dlopen a @@ -257,28 +262,28 @@ fn get_codegen_sysroot( let target = session::config::host_triple(); let sysroot_candidates = sysroot_candidates(); - let sysroot = maybe_sysroot - .iter() - .chain(sysroot_candidates.iter()) + let sysroot = iter::once(sysroot) + .chain(sysroot_candidates.iter().map(<_>::as_ref)) .map(|sysroot| { filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends") }) .find(|f| { info!("codegen backend candidate: {}", f.display()); f.exists() - }); - let sysroot = sysroot.unwrap_or_else(|| { - let candidates = sysroot_candidates - .iter() - .map(|p| p.display().to_string()) - .collect::>() - .join("\n* "); - let err = format!( - "failed to find a `codegen-backends` folder \ + }) + .unwrap_or_else(|| { + let candidates = sysroot_candidates + .iter() + .map(|p| p.display().to_string()) + .collect::>() + .join("\n* "); + let err = format!( + "failed to find a `codegen-backends` folder \ in the sysroot candidates:\n* {candidates}" - ); - early_dcx.early_fatal(err); - }); + ); + early_dcx.early_fatal(err); + }); + info!("probing {} for a codegen backend", sysroot.display()); let d = sysroot.read_dir().unwrap_or_else(|e| { diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index d35f951e2aea3..63bc902c813fc 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -1558,7 +1558,7 @@ pub fn build_configuration(sess: &Session, mut user_cfg: Cfg) -> Cfg { user_cfg } -pub(super) fn build_target_config( +pub fn build_target_config( early_dcx: &EarlyDiagCtxt, opts: &Options, target_override: Option, diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 25c20be8e627b..2a8b507a227bb 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1034,7 +1034,8 @@ pub fn build_session( fluent_resources: Vec<&'static str>, driver_lint_caps: FxHashMap, file_loader: Option>, - target_override: Option, + target_cfg: Target, + sysroot: PathBuf, cfg_version: &'static str, ice_file: Option, using_internal_features: Arc, @@ -1051,12 +1052,6 @@ pub fn build_session( let cap_lints_allow = sopts.lint_cap.is_some_and(|cap| cap == lint::Allow); let can_emit_warnings = !(warnings_allow || cap_lints_allow); - let sysroot = match &sopts.maybe_sysroot { - Some(sysroot) => sysroot.clone(), - None => filesearch::get_or_default_sysroot().expect("Failed finding sysroot"), - }; - - let target_cfg = config::build_target_config(&early_dcx, &sopts, target_override, &sysroot); let host_triple = TargetTriple::from_triple(config::host_triple()); let (host, target_warnings) = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| { early_dcx.early_fatal(format!("Error loading host specification: {e}")) diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index d04bcb2d78aac..ec48930b20b94 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -2044,6 +2044,14 @@ pub struct TargetOptions { /// Default number of codegen units to use in debug mode pub default_codegen_units: Option, + /// Default codegen backend used for this target. Defaults to `None`. + /// + /// If `None`, then `CFG_DEFAULT_CODEGEN_BACKEND` environmental variable captured when + /// compiling `rustc` will be used instead (or llvm if it is not set). + /// + /// N.B. when *using* the compiler, backend can always be overriden with `-Zcodegen-backend`. + pub default_codegen_backend: Option>, + /// Whether to generate trap instructions in places where optimization would /// otherwise produce control flow that falls through into unrelated memory. pub trap_unreachable: bool, @@ -2350,6 +2358,7 @@ impl Default for TargetOptions { stack_probes: StackProbeType::None, min_global_align: None, default_codegen_units: None, + default_codegen_backend: None, trap_unreachable: true, requires_lto: false, singlethread: false, From 5441523f0727639040fdd134abff1e298d88733e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 15 Feb 2024 00:03:34 +0000 Subject: [PATCH 2/3] Refactor out a repeating pattern with `get_or_default_sysroot` --- compiler/rustc_driver_impl/src/lib.rs | 8 ++------ compiler/rustc_interface/src/interface.rs | 5 +---- compiler/rustc_interface/src/tests.rs | 9 ++------- compiler/rustc_session/src/config.rs | 16 ++++------------ compiler/rustc_session/src/filesearch.rs | 6 ++++++ 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index bc485b0522320..0920505d86e30 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -888,9 +888,7 @@ pub fn version_at_macro_invocation( let debug_flags = matches.opt_strs("Z"); let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend=")); let opts = config::Options::default(); - let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| { - filesearch::get_or_default_sysroot().expect("Failed finding sysroot") - }); + let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone()); let target = config::build_target_config(early_dcx, &opts, None, &sysroot); get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version(); @@ -1100,9 +1098,7 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) -> let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend=")); let opts = config::Options::default(); - let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| { - filesearch::get_or_default_sysroot().expect("Failed finding sysroot") - }); + let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone()); let target = config::build_target_config(early_dcx, &opts, None, &sysroot); get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes(); diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index f53b320f2e0f8..a45762b2ce770 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -336,10 +336,7 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se let early_dcx = EarlyDiagCtxt::new(config.opts.error_format); - let sysroot = match &config.opts.maybe_sysroot { - Some(sysroot) => sysroot.clone(), - None => filesearch::get_or_default_sysroot().expect("Failed finding sysroot"), - }; + let sysroot = filesearch::materialize_sysroot(config.opts.maybe_sysroot.clone()); let (codegen_backend, target_cfg) = match config.make_codegen_backend { None => { diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index b1d874d72046b..0651950b967b2 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -13,7 +13,7 @@ use rustc_session::config::{ use rustc_session::lint::Level; use rustc_session::search_paths::SearchPath; use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind}; -use rustc_session::{build_session, getopts, CompilerIO, EarlyDiagCtxt, Session}; +use rustc_session::{build_session, filesearch, getopts, CompilerIO, EarlyDiagCtxt, Session}; use rustc_span::edition::{Edition, DEFAULT_EDITION}; use rustc_span::symbol::sym; use rustc_span::{FileName, SourceFileHashAlgorithm}; @@ -38,12 +38,7 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) { temps_dir, }; - let sysroot = match &sessopts.maybe_sysroot { - Some(sysroot) => sysroot.clone(), - None => { - rustc_session::filesearch::get_or_default_sysroot().expect("Failed finding sysroot") - } - }; + let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone()); let target_cfg = rustc_session::config::build_target_config(&early_dcx, &sessopts, None, &sysroot); diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 63bc902c813fc..2ad959d383297 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -6,7 +6,7 @@ pub use crate::options::*; use crate::errors::FileWriteFail; use crate::search_paths::SearchPath; use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind}; -use crate::{lint, HashStableContext}; +use crate::{filesearch, lint, HashStableContext}; use crate::{EarlyDiagCtxt, Session}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey}; @@ -2856,16 +2856,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M let logical_env = parse_logical_env(early_dcx, matches); - // Try to find a directory containing the Rust `src`, for more details see - // the doc comment on the `real_rust_source_base_dir` field. - let tmp_buf; - let sysroot = match &sysroot_opt { - Some(s) => s, - None => { - tmp_buf = crate::filesearch::get_or_default_sysroot().expect("Failed finding sysroot"); - &tmp_buf - } - }; + let sysroot = filesearch::materialize_sysroot(sysroot_opt); + let real_rust_source_base_dir = { // This is the location used by the `rust-src` `rustup` component. let mut candidate = sysroot.join("lib/rustlib/src/rust"); @@ -2909,7 +2901,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M describe_lints, output_types, search_paths, - maybe_sysroot: sysroot_opt, + maybe_sysroot: Some(sysroot), target_triple, test, incremental, diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index 6e459ac45d355..668acc0f76156 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -194,6 +194,12 @@ pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> { return sysroot_candidates; } +/// Returns the provided sysroot or calls [`get_or_default_sysroot`] if it's none. +/// Panics if [`get_or_default_sysroot`] returns an error. +pub fn materialize_sysroot(maybe_sysroot: Option) -> PathBuf { + maybe_sysroot.unwrap_or_else(|| get_or_default_sysroot().expect("Failed finding sysroot")) +} + /// This function checks if sysroot is found using env::args().next(), and if it /// is not found, finds sysroot from current rustc_driver dll. pub fn get_or_default_sysroot() -> Result { From 6d115f5d9a05380b983dd53d747150c8afbd48d8 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 18 Feb 2024 19:26:45 +0000 Subject: [PATCH 3/3] Refactor out another repeating pattern --- compiler/rustc_interface/src/interface.rs | 26 ++++++----------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index a45762b2ce770..9783327efd3df 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -338,7 +338,7 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se let sysroot = filesearch::materialize_sysroot(config.opts.maybe_sysroot.clone()); - let (codegen_backend, target_cfg) = match config.make_codegen_backend { + let (codegen_backend, target_override) = match config.make_codegen_backend { None => { // Build a target without override, so that it can override the backend if needed let target = @@ -365,36 +365,24 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se ); } - // Re-build target with the (potential) override - let target = config::build_target_config( - &early_dcx, - &config.opts, - target_override, - &sysroot, - ); - - (backend, target) + (backend, target_override) } Some(make_codegen_backend) => { // N.B. `make_codegen_backend` takes precedence over `target.default_codegen_backend`, // which is ignored in this case. - let backend = make_codegen_backend(&config.opts); // target_override is documented to be called before init(), so this is okay let target_override = backend.target_override(&config.opts); - let target = config::build_target_config( - &early_dcx, - &config.opts, - target_override, - &sysroot, - ); - - (backend, target) + (backend, target_override) } }; + // Re-build target with the (potential) override + let target_cfg = + config::build_target_config(&early_dcx, &config.opts, target_override, &sysroot); + let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from); let bundle = match rustc_errors::fluent_bundle(