compiler/rustc_codegen_ssa/src/back/write.rs RUST 2,318 lines View on github.com → Search inside
File is large — showing lines 1–2,000 of 2,318.
1use std::marker::PhantomData;2use std::panic::AssertUnwindSafe;3use std::path::{Path, PathBuf};4use std::sync::Arc;5use std::sync::mpsc::{Receiver, Sender, channel};6use std::{assert_matches, fs, io, mem, str, thread};78use rustc_abi::Size;9use rustc_data_structures::fx::FxIndexMap;10use rustc_data_structures::jobserver::{self, Acquired};11use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};12use rustc_errors::emitter::Emitter;13use rustc_errors::{14    Diag, DiagArgMap, DiagCtxt, DiagCtxtHandle, DiagMessage, ErrCode, FatalError, FatalErrorMarker,15    Level, MultiSpan, Style, Suggestions, catch_fatal_errors,16};17use rustc_fs_util::link_or_copy;18use rustc_hir::find_attr;19use rustc_incremental::{20    copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir, in_incr_comp_dir_sess,21};22use rustc_macros::{Decodable, Encodable};23use rustc_metadata::fs::copy_to_stdout;24use rustc_middle::bug;25use rustc_middle::dep_graph::{WorkProduct, WorkProductId};26use rustc_middle::ty::TyCtxt;27use rustc_session::Session;28use rustc_session::config::{29    self, CrateType, Lto, OptLevel, OutFileName, OutputFilenames, OutputType, Passes,30    SwitchWithOptPath,31};32use rustc_span::source_map::SourceMap;33use rustc_span::{FileName, InnerSpan, Span, SpanData};34use rustc_target::spec::{MergeFunctions, SanitizerSet};35use tracing::debug;3637use crate::back::link::{self, ensure_removed};38use crate::back::lto::{self, SerializedModule, check_lto_allowed};39use crate::errors::ErrorCreatingRemarkDir;40use crate::traits::*;41use crate::{42    CachedModuleCodegen, CompiledModule, CompiledModules, CrateInfo, ModuleCodegen, ModuleKind,43    errors,44};4546const PRE_LTO_BC_EXT: &str = "pre-lto.bc";4748/// What kind of object file to emit.49#[derive(Clone, Copy, PartialEq, Encodable, Decodable)]50pub enum EmitObj {51    // No object file.52    None,5354    // Just uncompressed llvm bitcode. Provides easy compatibility with55    // emscripten's ecc compiler, when used as the linker.56    Bitcode,5758    // Object code, possibly augmented with a bitcode section.59    ObjectCode(BitcodeSection),60}6162/// What kind of llvm bitcode section to embed in an object file.63#[derive(Clone, Copy, PartialEq, Encodable, Decodable)]64pub enum BitcodeSection {65    // No bitcode section.66    None,6768    // A full, uncompressed bitcode section.69    Full,70}7172/// Module-specific configuration for `optimize_and_codegen`.73#[derive(Encodable, Decodable)]74pub struct ModuleConfig {75    /// Names of additional optimization passes to run.76    pub passes: Vec<String>,77    /// Some(level) to optimize at a certain level, or None to run78    /// absolutely no optimizations (used for the allocator module).79    pub opt_level: Option<config::OptLevel>,8081    pub pgo_gen: SwitchWithOptPath,82    pub pgo_use: Option<PathBuf>,83    pub pgo_sample_use: Option<PathBuf>,84    pub debug_info_for_profiling: bool,85    pub instrument_coverage: bool,8687    pub sanitizer: SanitizerSet,88    pub sanitizer_recover: SanitizerSet,89    pub sanitizer_dataflow_abilist: Vec<String>,90    pub sanitizer_memory_track_origins: usize,9192    // Flags indicating which outputs to produce.93    pub emit_pre_lto_bc: bool,94    pub emit_bc: bool,95    pub emit_ir: bool,96    pub emit_asm: bool,97    pub emit_obj: EmitObj,98    pub emit_thin_lto_summary: bool,99100    // Miscellaneous flags. These are mostly copied from command-line101    // options.102    pub verify_llvm_ir: bool,103    pub lint_llvm_ir: bool,104    pub no_prepopulate_passes: bool,105    pub no_builtins: bool,106    pub vectorize_loop: bool,107    pub vectorize_slp: bool,108    pub merge_functions: bool,109    pub emit_lifetime_markers: bool,110    pub llvm_plugins: Vec<String>,111    pub autodiff: Vec<config::AutoDiff>,112    pub offload: Vec<config::Offload>,113}114115impl ModuleConfig {116    fn new(kind: ModuleKind, tcx: TyCtxt<'_>, no_builtins: bool) -> ModuleConfig {117        // If it's a regular module, use `$regular`, otherwise use `$other`.118        // `$regular` and `$other` are evaluated lazily.119        macro_rules! if_regular {120            ($regular: expr, $other: expr) => {121                if let ModuleKind::Regular = kind { $regular } else { $other }122            };123        }124125        let sess = tcx.sess;126        let opt_level_and_size = if_regular!(Some(sess.opts.optimize), None);127128        let save_temps = sess.opts.cg.save_temps;129130        let should_emit_obj = sess.opts.output_types.contains_key(&OutputType::Exe)131            || match kind {132                ModuleKind::Regular => sess.opts.output_types.contains_key(&OutputType::Object),133                ModuleKind::Allocator => false,134            };135136        let emit_obj = if !should_emit_obj {137            EmitObj::None138        } else if sess.target.obj_is_bitcode139            || (sess.opts.cg.linker_plugin_lto.enabled() && !no_builtins)140        {141            // This case is selected if the target uses objects as bitcode, or142            // if linker plugin LTO is enabled. In the linker plugin LTO case143            // the assumption is that the final link-step will read the bitcode144            // and convert it to object code. This may be done by either the145            // native linker or rustc itself.146            //147            // Note, however, that the linker-plugin-lto requested here is148            // explicitly ignored for `#![no_builtins]` crates. These crates are149            // specifically ignored by rustc's LTO passes and wouldn't work if150            // loaded into the linker. These crates define symbols that LLVM151            // lowers intrinsics to, and these symbol dependencies aren't known152            // until after codegen. As a result any crate marked153            // `#![no_builtins]` is assumed to not participate in LTO and154            // instead goes on to generate object code.155            EmitObj::Bitcode156        } else if need_bitcode_in_object(tcx) || sess.target.requires_lto {157            EmitObj::ObjectCode(BitcodeSection::Full)158        } else {159            EmitObj::ObjectCode(BitcodeSection::None)160        };161162        ModuleConfig {163            passes: if_regular!(sess.opts.cg.passes.clone(), vec![]),164165            opt_level: opt_level_and_size,166167            pgo_gen: if_regular!(168                sess.opts.cg.profile_generate.clone(),169                SwitchWithOptPath::Disabled170            ),171            pgo_use: if_regular!(sess.opts.cg.profile_use.clone(), None),172            pgo_sample_use: if_regular!(sess.opts.unstable_opts.profile_sample_use.clone(), None),173            debug_info_for_profiling: sess.opts.unstable_opts.debug_info_for_profiling,174            instrument_coverage: if_regular!(sess.instrument_coverage(), false),175176            sanitizer: if_regular!(sess.sanitizers(), SanitizerSet::empty()),177            sanitizer_dataflow_abilist: if_regular!(178                sess.opts.unstable_opts.sanitizer_dataflow_abilist.clone(),179                Vec::new()180            ),181            sanitizer_recover: if_regular!(182                sess.opts.unstable_opts.sanitizer_recover,183                SanitizerSet::empty()184            ),185            sanitizer_memory_track_origins: if_regular!(186                sess.opts.unstable_opts.sanitizer_memory_track_origins,187                0188            ),189190            emit_pre_lto_bc: if_regular!(191                save_temps || need_pre_lto_bitcode_for_incr_comp(sess),192                false193            ),194            emit_bc: if_regular!(195                save_temps || sess.opts.output_types.contains_key(&OutputType::Bitcode),196                save_temps197            ),198            emit_ir: if_regular!(199                sess.opts.output_types.contains_key(&OutputType::LlvmAssembly),200                false201            ),202            emit_asm: if_regular!(203                sess.opts.output_types.contains_key(&OutputType::Assembly),204                false205            ),206            emit_obj,207            emit_thin_lto_summary: if_regular!(208                sess.opts.output_types.contains_key(&OutputType::ThinLinkBitcode),209                false210            ),211212            verify_llvm_ir: sess.verify_llvm_ir(),213            lint_llvm_ir: sess.opts.unstable_opts.lint_llvm_ir,214            no_prepopulate_passes: sess.opts.cg.no_prepopulate_passes,215            no_builtins: no_builtins || sess.target.no_builtins,216217            // Copy what clang does by turning on loop vectorization at O2 and218            // slp vectorization at O3.219            vectorize_loop: !sess.opts.cg.no_vectorize_loops220                && (sess.opts.optimize == config::OptLevel::More221                    || sess.opts.optimize == config::OptLevel::Aggressive),222            vectorize_slp: !sess.opts.cg.no_vectorize_slp223                && sess.opts.optimize == config::OptLevel::Aggressive,224225            // Some targets (namely, NVPTX) interact badly with the226            // MergeFunctions pass. This is because MergeFunctions can generate227            // new function calls which may interfere with the target calling228            // convention; e.g. for the NVPTX target, PTX kernels should not229            // call other PTX kernels. MergeFunctions can also be configured to230            // generate aliases instead, but aliases are not supported by some231            // backends (again, NVPTX). Therefore, allow targets to opt out of232            // the MergeFunctions pass, but otherwise keep the pass enabled (at233            // O2 and O3) since it can be useful for reducing code size.234            merge_functions: match sess235                .opts236                .unstable_opts237                .merge_functions238                .unwrap_or(sess.target.merge_functions)239            {240                MergeFunctions::Disabled => false,241                MergeFunctions::Trampolines | MergeFunctions::Aliases => {242                    use config::OptLevel::*;243                    match sess.opts.optimize {244                        Aggressive | More | SizeMin | Size => true,245                        Less | No => false,246                    }247                }248            },249250            emit_lifetime_markers: sess.emit_lifetime_markers(),251            llvm_plugins: if_regular!(sess.opts.unstable_opts.llvm_plugins.clone(), vec![]),252            autodiff: if_regular!(sess.opts.unstable_opts.autodiff.clone(), vec![]),253            offload: if_regular!(sess.opts.unstable_opts.offload.clone(), vec![]),254        }255    }256257    pub fn bitcode_needed(&self) -> bool {258        self.emit_bc259            || self.emit_thin_lto_summary260            || self.emit_obj == EmitObj::Bitcode261            || self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)262    }263264    pub fn embed_bitcode(&self) -> bool {265        self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)266    }267}268269/// Configuration passed to the function returned by the `target_machine_factory`.270pub struct TargetMachineFactoryConfig {271    /// Split DWARF is enabled in LLVM by checking that `TM.MCOptions.SplitDwarfFile` isn't empty,272    /// so the path to the dwarf object has to be provided when we create the target machine.273    /// This can be ignored by backends which do not need it for their Split DWARF support.274    pub split_dwarf_file: Option<PathBuf>,275276    /// The name of the output object file. Used for setting OutputFilenames in target options277    /// so that LLVM can emit the CodeView S_OBJNAME record in pdb files278    pub output_obj_file: Option<PathBuf>,279}280281impl TargetMachineFactoryConfig {282    pub fn new(cgcx: &CodegenContext, module_name: &str) -> TargetMachineFactoryConfig {283        let split_dwarf_file = if cgcx.target_can_use_split_dwarf {284            cgcx.output_filenames.split_dwarf_path(285                cgcx.split_debuginfo,286                cgcx.split_dwarf_kind,287                module_name,288                cgcx.invocation_temp.as_deref(),289            )290        } else {291            None292        };293294        let output_obj_file = Some(cgcx.output_filenames.temp_path_for_cgu(295            OutputType::Object,296            module_name,297            cgcx.invocation_temp.as_deref(),298        ));299        TargetMachineFactoryConfig { split_dwarf_file, output_obj_file }300    }301}302303pub type TargetMachineFactoryFn<B> = Arc<304    dyn Fn(305            DiagCtxtHandle<'_>,306            TargetMachineFactoryConfig,307        ) -> <B as WriteBackendMethods>::TargetMachine308        + Send309        + Sync,310>;311312/// Additional resources used by optimize_and_codegen (not module specific)313#[derive(Clone, Encodable, Decodable)]314pub struct CodegenContext {315    // Resources needed when running LTO316    pub lto: Lto,317    pub use_linker_plugin_lto: bool,318    pub dylib_lto: bool,319    pub prefer_dynamic: bool,320    pub save_temps: bool,321    pub fewer_names: bool,322    pub time_trace: bool,323    pub crate_types: Vec<CrateType>,324    pub output_filenames: Arc<OutputFilenames>,325    pub invocation_temp: Option<String>,326    pub module_config: Arc<ModuleConfig>,327    pub opt_level: OptLevel,328    pub backend_features: Vec<String>,329    pub msvc_imps_needed: bool,330    pub is_pe_coff: bool,331    pub target_can_use_split_dwarf: bool,332    pub target_arch: String,333    pub target_is_like_darwin: bool,334    pub target_is_like_aix: bool,335    pub target_is_like_gpu: bool,336    pub split_debuginfo: rustc_target::spec::SplitDebuginfo,337    pub split_dwarf_kind: rustc_session::config::SplitDwarfKind,338    pub pointer_size: Size,339340    /// LLVM optimizations for which we want to print remarks.341    pub remark: Passes,342    /// Directory into which should the LLVM optimization remarks be written.343    /// If `None`, they will be written to stderr.344    pub remark_dir: Option<PathBuf>,345    /// The incremental compilation session directory, or None if we are not346    /// compiling incrementally347    pub incr_comp_session_dir: Option<PathBuf>,348    /// `true` if the codegen should be run in parallel.349    ///350    /// Depends on [`ExtraBackendMethods::supports_parallel()`] and `-Zno_parallel_backend`.351    pub parallel: bool,352}353354fn generate_thin_lto_work<B: WriteBackendMethods>(355    cgcx: &CodegenContext,356    prof: &SelfProfilerRef,357    dcx: DiagCtxtHandle<'_>,358    exported_symbols_for_lto: &[String],359    each_linked_rlib_for_lto: &[PathBuf],360    needs_thin_lto: Vec<ThinLtoInput<B>>,361) -> Vec<(ThinLtoWorkItem<B>, u64)> {362    let _prof_timer = prof.generic_activity("codegen_thin_generate_lto_work");363364    let (lto_modules, copy_jobs) = B::run_thin_lto(365        cgcx,366        prof,367        dcx,368        exported_symbols_for_lto,369        each_linked_rlib_for_lto,370        needs_thin_lto,371    );372    lto_modules373        .into_iter()374        .map(|module| {375            let cost = module.cost();376            (ThinLtoWorkItem::ThinLto(module), cost)377        })378        .chain(copy_jobs.into_iter().map(|wp| {379            (380                ThinLtoWorkItem::CopyPostLtoArtifacts(CachedModuleCodegen {381                    name: wp.cgu_name.clone(),382                    source: wp,383                }),384                0, // copying is very cheap385            )386        }))387        .collect()388}389390enum MaybeLtoModules<B: WriteBackendMethods> {391    NoLto(CompiledModules),392    FatLto {393        cgcx: CodegenContext,394        exported_symbols_for_lto: Arc<Vec<String>>,395        each_linked_rlib_file_for_lto: Vec<PathBuf>,396        needs_fat_lto: Vec<FatLtoInput<B>>,397    },398    ThinLto {399        cgcx: CodegenContext,400        exported_symbols_for_lto: Arc<Vec<String>>,401        each_linked_rlib_file_for_lto: Vec<PathBuf>,402        needs_thin_lto: Vec<ThinLtoInput<B>>,403    },404}405406fn need_bitcode_in_object(tcx: TyCtxt<'_>) -> bool {407    let sess = tcx.sess;408    sess.opts.cg.embed_bitcode409        && tcx.crate_types().contains(&CrateType::Rlib)410        && sess.opts.output_types.contains_key(&OutputType::Exe)411}412413fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {414    if sess.opts.incremental.is_none() {415        return false;416    }417418    match sess.lto() {419        Lto::No => false,420        Lto::Fat | Lto::Thin | Lto::ThinLocal => true,421    }422}423424pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(425    backend: B,426    tcx: TyCtxt<'_>,427    crate_info: &CrateInfo,428    allocator_module: Option<ModuleCodegen<B::Module>>,429) -> OngoingCodegen<B> {430    let (coordinator_send, coordinator_receive) = channel();431432    let no_builtins = find_attr!(tcx, crate, NoBuiltins);433434    let regular_config = ModuleConfig::new(ModuleKind::Regular, tcx, no_builtins);435    let allocator_config = ModuleConfig::new(ModuleKind::Allocator, tcx, no_builtins);436437    let (shared_emitter, shared_emitter_main) = SharedEmitter::new();438    let (codegen_worker_send, codegen_worker_receive) = channel();439440    let coordinator_thread = start_executing_work(441        backend.clone(),442        tcx,443        crate_info,444        shared_emitter,445        codegen_worker_send,446        coordinator_receive,447        Arc::new(regular_config),448        Arc::new(allocator_config),449        allocator_module,450        coordinator_send.clone(),451    );452453    OngoingCodegen {454        backend,455456        codegen_worker_receive,457        shared_emitter_main,458        coordinator: Coordinator {459            sender: coordinator_send,460            future: Some(coordinator_thread),461            phantom: PhantomData,462        },463        output_filenames: Arc::clone(tcx.output_filenames(())),464    }465}466467fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(468    sess: &Session,469    compiled_modules: &CompiledModules,470) -> FxIndexMap<WorkProductId, WorkProduct> {471    let mut work_products = FxIndexMap::default();472473    if sess.opts.incremental.is_none() {474        return work_products;475    }476477    let _timer = sess.timer("copy_all_cgu_workproducts_to_incr_comp_cache_dir");478479    for module in compiled_modules.modules.iter().filter(|m| m.kind == ModuleKind::Regular) {480        let mut files = Vec::new();481        if let Some(object_file_path) = &module.object {482            files.push((OutputType::Object.extension(), object_file_path.as_path()));483        }484        if let Some(dwarf_object_file_path) = &module.dwarf_object {485            files.push(("dwo", dwarf_object_file_path.as_path()));486        }487        if let Some(path) = &module.assembly {488            files.push((OutputType::Assembly.extension(), path.as_path()));489        }490        if let Some(path) = &module.llvm_ir {491            files.push((OutputType::LlvmAssembly.extension(), path.as_path()));492        }493        if let Some(path) = &module.bytecode {494            files.push((OutputType::Bitcode.extension(), path.as_path()));495        }496        if let Some((id, product)) = copy_cgu_workproduct_to_incr_comp_cache_dir(497            sess,498            &module.name,499            files.as_slice(),500            &module.links_from_incr_cache,501        ) {502            work_products.insert(id, product);503        }504    }505506    work_products507}508509pub fn produce_final_output_artifacts(510    sess: &Session,511    compiled_modules: &CompiledModules,512    crate_output: &OutputFilenames,513) {514    let mut user_wants_bitcode = false;515    let mut user_wants_objects = false;516517    // Produce final compile outputs.518    let copy_gracefully = |from: &Path, to: &OutFileName| match to {519        OutFileName::Stdout if let Err(e) = copy_to_stdout(from) => {520            sess.dcx().emit_err(errors::CopyPath::new(from, to.as_path(), e));521        }522        OutFileName::Real(path) if let Err(e) = fs::copy(from, path) => {523            sess.dcx().emit_err(errors::CopyPath::new(from, path, e));524        }525        _ => {}526    };527528    let copy_if_one_unit = |output_type: OutputType, keep_numbered: bool| {529        if let [module] = &compiled_modules.modules[..] {530            // 1) Only one codegen unit. In this case it's no difficulty531            //    to copy `foo.0.x` to `foo.x`.532            let path = crate_output.temp_path_for_cgu(533                output_type,534                &module.name,535                sess.invocation_temp.as_deref(),536            );537            let output = crate_output.path(output_type);538            if !output_type.is_text_output() && output.is_tty() {539                sess.dcx()540                    .emit_err(errors::BinaryOutputToTty { shorthand: output_type.shorthand() });541            } else {542                copy_gracefully(&path, &output);543            }544            if !sess.opts.cg.save_temps && !keep_numbered {545                // The user just wants `foo.x`, not `foo.#module-name#.x`.546                ensure_removed(sess.dcx(), &path);547            }548        } else {549            if crate_output.outputs.contains_explicit_name(&output_type) {550                // 2) Multiple codegen units, with `--emit foo=some_name`. We have551                //    no good solution for this case, so warn the user.552                sess.dcx()553                    .emit_warn(errors::IgnoringEmitPath { extension: output_type.extension() });554            } else if crate_output.single_output_file.is_some() {555                // 3) Multiple codegen units, with `-o some_name`. We have556                //    no good solution for this case, so warn the user.557                sess.dcx().emit_warn(errors::IgnoringOutput { extension: output_type.extension() });558            } else {559                // 4) Multiple codegen units, but no explicit name. We560                //    just leave the `foo.0.x` files in place.561                // (We don't have to do any work in this case.)562            }563        }564    };565566    // Flag to indicate whether the user explicitly requested bitcode.567    // Otherwise, we produced it only as a temporary output, and will need568    // to get rid of it.569    for output_type in crate_output.outputs.keys() {570        match *output_type {571            OutputType::Bitcode => {572                user_wants_bitcode = true;573                // Copy to .bc, but always keep the .0.bc. There is a later574                // check to figure out if we should delete .0.bc files, or keep575                // them for making an rlib.576                copy_if_one_unit(OutputType::Bitcode, true);577            }578            OutputType::ThinLinkBitcode => {579                copy_if_one_unit(OutputType::ThinLinkBitcode, false);580            }581            OutputType::LlvmAssembly => {582                copy_if_one_unit(OutputType::LlvmAssembly, false);583            }584            OutputType::Assembly => {585                copy_if_one_unit(OutputType::Assembly, false);586            }587            OutputType::Object => {588                user_wants_objects = true;589                copy_if_one_unit(OutputType::Object, true);590            }591            OutputType::Mir | OutputType::Metadata | OutputType::Exe | OutputType::DepInfo => {}592        }593    }594595    // Clean up unwanted temporary files.596597    // We create the following files by default:598    //  - #crate#.#module-name#.rcgu.bc599    //  - #crate#.#module-name#.rcgu.o600    //  - #crate#.o (linked from crate.##.rcgu.o)601    //  - #crate#.bc (copied from crate.##.rcgu.bc)602    // We may create additional files if requested by the user (through603    // `-C save-temps` or `--emit=` flags).604605    if !sess.opts.cg.save_temps {606        // Remove the temporary .#module-name#.rcgu.o objects. If the user didn't607        // explicitly request bitcode (with --emit=bc), and the bitcode is not608        // needed for building an rlib, then we must remove .#module-name#.bc as609        // well.610611        // Specific rules for keeping .#module-name#.rcgu.bc:612        //  - If the user requested bitcode (`user_wants_bitcode`), and613        //    codegen_units > 1, then keep it.614        //  - If the user requested bitcode but codegen_units == 1, then we615        //    can toss .#module-name#.rcgu.bc because we copied it to .bc earlier.616        //  - If we're not building an rlib and the user didn't request617        //    bitcode, then delete .#module-name#.rcgu.bc.618        // If you change how this works, also update back::link::link_rlib,619        // where .#module-name#.rcgu.bc files are (maybe) deleted after making an620        // rlib.621        let needs_crate_object = crate_output.outputs.contains_key(&OutputType::Exe);622623        let keep_numbered_bitcode = user_wants_bitcode && sess.codegen_units().as_usize() > 1;624625        let keep_numbered_objects =626            needs_crate_object || (user_wants_objects && sess.codegen_units().as_usize() > 1);627628        for module in compiled_modules.modules.iter() {629            if !keep_numbered_objects {630                if let Some(ref path) = module.object {631                    ensure_removed(sess.dcx(), path);632                }633634                if let Some(ref path) = module.dwarf_object {635                    ensure_removed(sess.dcx(), path);636                }637            }638639            if let Some(ref path) = module.bytecode {640                if !keep_numbered_bitcode {641                    ensure_removed(sess.dcx(), path);642                }643            }644        }645646        if !user_wants_bitcode647            && let Some(ref allocator_module) = compiled_modules.allocator_module648            && let Some(ref path) = allocator_module.bytecode649        {650            ensure_removed(sess.dcx(), path);651        }652    }653654    if sess.opts.json_artifact_notifications {655        if let [module] = &compiled_modules.modules[..] {656            module.for_each_output(|_path, ty| {657                if sess.opts.output_types.contains_key(&ty) {658                    let descr = ty.shorthand();659                    // for single cgu file is renamed to drop cgu specific suffix660                    // so we regenerate it the same way661                    let path = crate_output.path(ty);662                    sess.dcx().emit_artifact_notification(path.as_path(), descr);663                }664            });665        } else {666            for module in &compiled_modules.modules {667                module.for_each_output(|path, ty| {668                    if sess.opts.output_types.contains_key(&ty) {669                        let descr = ty.shorthand();670                        sess.dcx().emit_artifact_notification(&path, descr);671                    }672                });673            }674        }675    }676677    // We leave the following files around by default:678    //  - #crate#.o679    //  - #crate#.bc680    // These are used in linking steps and will be cleaned up afterward.681}682683pub(crate) enum WorkItem<B: WriteBackendMethods> {684    /// Optimize a newly codegened, totally unoptimized module.685    Optimize(ModuleCodegen<B::Module>),686    /// Copy the post-LTO artifacts from the incremental cache to the output687    /// directory.688    CopyPostLtoArtifacts(CachedModuleCodegen),689}690691enum ThinLtoWorkItem<B: WriteBackendMethods> {692    /// Copy the post-LTO artifacts from the incremental cache to the output693    /// directory.694    CopyPostLtoArtifacts(CachedModuleCodegen),695    /// Performs thin-LTO on the given module.696    ThinLto(lto::ThinModule<B>),697}698699// `pthread_setname()` on *nix ignores anything beyond the first 15700// bytes. Use short descriptions to maximize the space available for701// the module name.702#[cfg(not(windows))]703fn desc(short: &str, _long: &str, name: &str) -> String {704    // The short label is three bytes, and is followed by a space. That705    // leaves 11 bytes for the CGU name. How we obtain those 11 bytes706    // depends on the CGU name form.707    //708    // - Non-incremental, e.g. `regex.f10ba03eb5ec7975-cgu.0`: the part709    //   before the `-cgu.0` is the same for every CGU, so use the710    //   `cgu.0` part. The number suffix will be different for each711    //   CGU.712    //713    // - Incremental (normal), e.g. `2i52vvl2hco29us0`: use the whole714    //   name because each CGU will have a unique ASCII hash, and the715    //   first 11 bytes will be enough to identify it.716    //717    // - Incremental (with `-Zhuman-readable-cgu-names`), e.g.718    //   `regex.f10ba03eb5ec7975-re_builder.volatile`: use the whole719    //   name. The first 11 bytes won't be enough to uniquely identify720    //   it, but no obvious substring will, and this is a rarely used721    //   option so it doesn't matter much.722    //723    assert_eq!(short.len(), 3);724    let name = if let Some(index) = name.find("-cgu.") {725        &name[index + 1..] // +1 skips the leading '-'.726    } else {727        name728    };729    format!("{short} {name}")730}731732// Windows has no thread name length limit, so use more descriptive names.733#[cfg(windows)]734fn desc(_short: &str, long: &str, name: &str) -> String {735    format!("{long} {name}")736}737738impl<B: WriteBackendMethods> WorkItem<B> {739    /// Generate a short description of this work item suitable for use as a thread name.740    fn short_description(&self) -> String {741        match self {742            WorkItem::Optimize(m) => desc("opt", "optimize module", &m.name),743            WorkItem::CopyPostLtoArtifacts(m) => desc("cpy", "copy LTO artifacts for", &m.name),744        }745    }746}747748impl<B: WriteBackendMethods> ThinLtoWorkItem<B> {749    /// Generate a short description of this work item suitable for use as a thread name.750    fn short_description(&self) -> String {751        match self {752            ThinLtoWorkItem::CopyPostLtoArtifacts(m) => {753                desc("cpy", "copy LTO artifacts for", &m.name)754            }755            ThinLtoWorkItem::ThinLto(m) => desc("lto", "thin-LTO module", m.name()),756        }757    }758}759760/// A result produced by the backend.761pub(crate) enum WorkItemResult<B: WriteBackendMethods> {762    /// The backend has finished compiling a CGU, nothing more required.763    Finished(CompiledModule),764765    /// The backend has finished compiling a CGU, which now needs to go through766    /// fat LTO.767    NeedsFatLto(FatLtoInput<B>),768769    /// The backend has finished compiling a CGU, which now needs to go through770    /// thin LTO.771    NeedsThinLto(String, B::ModuleBuffer),772}773774pub enum FatLtoInput<B: WriteBackendMethods> {775    Serialized { name: String, bitcode_path: PathBuf },776    InMemory(ModuleCodegen<B::Module>),777}778779pub enum ThinLtoInput<B: WriteBackendMethods> {780    Red { name: String, buffer: SerializedModule<B::ModuleBuffer> },781    Green { wp: WorkProduct, bitcode_path: PathBuf },782}783784/// Actual LTO type we end up choosing based on multiple factors.785pub(crate) enum ComputedLtoType {786    No,787    Thin,788    Fat,789}790791pub(crate) fn compute_per_cgu_lto_type(792    sess_lto: &Lto,793    linker_does_lto: bool,794    sess_crate_types: &[CrateType],795) -> ComputedLtoType {796    // If the linker does LTO, we don't have to do it. Note that we797    // keep doing full LTO, if it is requested, as not to break the798    // assumption that the output will be a single module.799800    // We ignore a request for full crate graph LTO if the crate type801    // is only an rlib, as there is no full crate graph to process,802    // that'll happen later.803    //804    // This use case currently comes up primarily for targets that805    // require LTO so the request for LTO is always unconditionally806    // passed down to the backend, but we don't actually want to do807    // anything about it yet until we've got a final product.808    let is_rlib = matches!(sess_crate_types, [CrateType::Rlib]);809810    match sess_lto {811        Lto::ThinLocal if !linker_does_lto => ComputedLtoType::Thin,812        Lto::Thin if !linker_does_lto && !is_rlib => ComputedLtoType::Thin,813        Lto::Fat if !is_rlib => ComputedLtoType::Fat,814        _ => ComputedLtoType::No,815    }816}817818fn execute_optimize_work_item<B: WriteBackendMethods>(819    cgcx: &CodegenContext,820    prof: &SelfProfilerRef,821    shared_emitter: SharedEmitter,822    mut module: ModuleCodegen<B::Module>,823) -> WorkItemResult<B> {824    let _timer = prof.generic_activity_with_arg("codegen_module_optimize", &*module.name);825826    B::optimize(cgcx, prof, &shared_emitter, &mut module, &cgcx.module_config);827828    // After we've done the initial round of optimizations we need to829    // decide whether to synchronously codegen this module or ship it830    // back to the coordinator thread for further LTO processing (which831    // has to wait for all the initial modules to be optimized).832833    let lto_type =834        compute_per_cgu_lto_type(&cgcx.lto, cgcx.use_linker_plugin_lto, &cgcx.crate_types);835836    // If we're doing some form of incremental LTO then we need to be sure to837    // save our module to disk first.838    let bitcode = if cgcx.module_config.emit_pre_lto_bc {839        let filename = pre_lto_bitcode_filename(&module.name);840        cgcx.incr_comp_session_dir.as_ref().map(|path| path.join(&filename))841    } else {842        None843    };844845    match lto_type {846        ComputedLtoType::No => {847            let module = B::codegen(cgcx, &prof, &shared_emitter, module, &cgcx.module_config);848            WorkItemResult::Finished(module)849        }850        ComputedLtoType::Thin => {851            let thin_buffer = B::serialize_module(module.module_llvm, true);852            if let Some(path) = bitcode {853                fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| {854                    panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);855                });856            }857            WorkItemResult::NeedsThinLto(module.name, thin_buffer)858        }859        ComputedLtoType::Fat => match bitcode {860            Some(path) => {861                let buffer = B::serialize_module(module.module_llvm, false);862                fs::write(&path, buffer.data()).unwrap_or_else(|e| {863                    panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);864                });865                WorkItemResult::NeedsFatLto(FatLtoInput::Serialized {866                    name: module.name,867                    bitcode_path: path,868                })869            }870            None => WorkItemResult::NeedsFatLto(FatLtoInput::InMemory(module)),871        },872    }873}874875fn execute_copy_from_cache_work_item(876    cgcx: &CodegenContext,877    prof: &SelfProfilerRef,878    shared_emitter: SharedEmitter,879    module: CachedModuleCodegen,880) -> CompiledModule {881    let _timer =882        prof.generic_activity_with_arg("codegen_copy_artifacts_from_incr_cache", &*module.name);883884    let dcx = DiagCtxt::new(Box::new(shared_emitter));885    let dcx = dcx.handle();886887    let incr_comp_session_dir = cgcx.incr_comp_session_dir.as_ref().unwrap();888889    let mut links_from_incr_cache = Vec::new();890891    let mut load_from_incr_comp_dir = |output_path: PathBuf, saved_path: &str| {892        let source_file = in_incr_comp_dir(incr_comp_session_dir, saved_path);893        debug!(894            "copying preexisting module `{}` from {:?} to {}",895            module.name,896            source_file,897            output_path.display()898        );899        match link_or_copy(&source_file, &output_path) {900            Ok(_) => {901                links_from_incr_cache.push(source_file);902                Some(output_path)903            }904            Err(error) => {905                dcx.emit_err(errors::CopyPathBuf { source_file, output_path, error });906                None907            }908        }909    };910911    let dwarf_object =912        module.source.saved_files.get("dwo").as_ref().and_then(|saved_dwarf_object_file| {913            let dwarf_obj_out = cgcx914                .output_filenames915                .split_dwarf_path(916                    cgcx.split_debuginfo,917                    cgcx.split_dwarf_kind,918                    &module.name,919                    cgcx.invocation_temp.as_deref(),920                )921                .expect(922                    "saved dwarf object in work product but `split_dwarf_path` returned `None`",923                );924            load_from_incr_comp_dir(dwarf_obj_out, saved_dwarf_object_file)925        });926927    let mut load_from_incr_cache = |perform, output_type: OutputType| {928        if perform {929            let saved_file = module.source.saved_files.get(output_type.extension())?;930            let output_path = cgcx.output_filenames.temp_path_for_cgu(931                output_type,932                &module.name,933                cgcx.invocation_temp.as_deref(),934            );935            load_from_incr_comp_dir(output_path, &saved_file)936        } else {937            None938        }939    };940941    let module_config = &cgcx.module_config;942    let should_emit_obj = module_config.emit_obj != EmitObj::None;943    let assembly = load_from_incr_cache(module_config.emit_asm, OutputType::Assembly);944    let llvm_ir = load_from_incr_cache(module_config.emit_ir, OutputType::LlvmAssembly);945    let bytecode = load_from_incr_cache(module_config.emit_bc, OutputType::Bitcode);946    let object = load_from_incr_cache(should_emit_obj, OutputType::Object);947    if should_emit_obj && object.is_none() {948        dcx.emit_fatal(errors::NoSavedObjectFile { cgu_name: &module.name })949    }950951    CompiledModule {952        links_from_incr_cache,953        kind: ModuleKind::Regular,954        name: module.name,955        object,956        dwarf_object,957        bytecode,958        assembly,959        llvm_ir,960    }961}962963fn do_fat_lto<B: WriteBackendMethods>(964    sess: &Session,965    cgcx: &CodegenContext,966    shared_emitter: SharedEmitter,967    tm_factory: TargetMachineFactoryFn<B>,968    exported_symbols_for_lto: &[String],969    each_linked_rlib_for_lto: &[PathBuf],970    needs_fat_lto: Vec<FatLtoInput<B>>,971) -> CompiledModule {972    let _timer = sess.prof.verbose_generic_activity("LLVM_fatlto");973974    let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));975    let dcx = dcx.handle();976977    check_lto_allowed(&cgcx, dcx);978979    B::optimize_and_codegen_fat_lto(980        sess,981        cgcx,982        &shared_emitter,983        tm_factory,984        exported_symbols_for_lto,985        each_linked_rlib_for_lto,986        needs_fat_lto,987    )988}989990fn do_thin_lto<B: WriteBackendMethods>(991    cgcx: &CodegenContext,992    prof: &SelfProfilerRef,993    shared_emitter: SharedEmitter,994    tm_factory: TargetMachineFactoryFn<B>,995    exported_symbols_for_lto: Arc<Vec<String>>,996    each_linked_rlib_for_lto: Vec<PathBuf>,997    needs_thin_lto: Vec<ThinLtoInput<B>>,998) -> Vec<CompiledModule> {999    let _timer = prof.verbose_generic_activity("LLVM_thinlto");10001001    let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));1002    let dcx = dcx.handle();10031004    check_lto_allowed(&cgcx, dcx);10051006    let (coordinator_send, coordinator_receive) = channel();10071008    // First up, convert our jobserver into a helper thread so we can use normal1009    // mpsc channels to manage our messages and such.1010    // After we've requested tokens then we'll, when we can,1011    // get tokens on `coordinator_receive` which will1012    // get managed in the main loop below.1013    let coordinator_send2 = coordinator_send.clone();1014    let helper = jobserver::client()1015        .into_helper_thread(move |token| {1016            drop(coordinator_send2.send(ThinLtoMessage::Token(token)));1017        })1018        .expect("failed to spawn helper thread");10191020    let mut work_items = vec![];10211022    // We have LTO work to do. Perform the serial work here of1023    // figuring out what we're going to LTO and then push a1024    // bunch of work items onto our queue to do LTO. This all1025    // happens on the coordinator thread but it's very quick so1026    // we don't worry about tokens.1027    for (work, cost) in generate_thin_lto_work::<B>(1028        cgcx,1029        prof,1030        dcx,1031        &exported_symbols_for_lto,1032        &each_linked_rlib_for_lto,1033        needs_thin_lto,1034    ) {1035        let insertion_index =1036            work_items.binary_search_by_key(&cost, |&(_, cost)| cost).unwrap_or_else(|e| e);1037        work_items.insert(insertion_index, (work, cost));1038        if cgcx.parallel {1039            helper.request_token();1040        }1041    }10421043    let mut codegen_aborted = None;10441045    // These are the Jobserver Tokens we currently hold. Does not include1046    // the implicit Token the compiler process owns no matter what.1047    let mut tokens = vec![];10481049    // Amount of tokens that are used (including the implicit token).1050    let mut used_token_count = 0;10511052    let mut compiled_modules = vec![];10531054    // Run the message loop while there's still anything that needs message1055    // processing. Note that as soon as codegen is aborted we simply want to1056    // wait for all existing work to finish, so many of the conditions here1057    // only apply if codegen hasn't been aborted as they represent pending1058    // work to be done.1059    loop {1060        if codegen_aborted.is_none() {1061            if used_token_count == 0 && work_items.is_empty() {1062                // All codegen work is done.1063                break;1064            }10651066            // Spin up what work we can, only doing this while we've got available1067            // parallelism slots and work left to spawn.1068            while used_token_count < tokens.len() + 11069                && let Some((item, _)) = work_items.pop()1070            {1071                spawn_thin_lto_work(1072                    &cgcx,1073                    prof,1074                    shared_emitter.clone(),1075                    Arc::clone(&tm_factory),1076                    coordinator_send.clone(),1077                    item,1078                );1079                used_token_count += 1;1080            }1081        } else {1082            // Don't queue up any more work if codegen was aborted, we're1083            // just waiting for our existing children to finish.1084            if used_token_count == 0 {1085                break;1086            }1087        }10881089        // Relinquish accidentally acquired extra tokens. Subtract 1 for the implicit token.1090        tokens.truncate(used_token_count.saturating_sub(1));10911092        match coordinator_receive.recv().unwrap() {1093            // Save the token locally and the next turn of the loop will use1094            // this to spawn a new unit of work, or it may get dropped1095            // immediately if we have no more work to spawn.1096            ThinLtoMessage::Token(token) => match token {1097                Ok(token) => {1098                    tokens.push(token);1099                }1100                Err(e) => {1101                    let msg = &format!("failed to acquire jobserver token: {e}");1102                    shared_emitter.fatal(msg);1103                    codegen_aborted = Some(FatalError);1104                }1105            },11061107            ThinLtoMessage::WorkItem { result } => {1108                // If a thread exits successfully then we drop a token associated1109                // with that worker and update our `used_token_count` count.1110                // We may later re-acquire a token to continue running more work.1111                // We may also not actually drop a token here if the worker was1112                // running with an "ephemeral token".1113                used_token_count -= 1;11141115                match result {1116                    Ok(compiled_module) => compiled_modules.push(compiled_module),1117                    Err(Some(WorkerFatalError)) => {1118                        // Like `CodegenAborted`, wait for remaining work to finish.1119                        codegen_aborted = Some(FatalError);1120                    }1121                    Err(None) => {1122                        // If the thread failed that means it panicked, so1123                        // we abort immediately.1124                        bug!("worker thread panicked");1125                    }1126                }1127            }1128        }1129    }11301131    if let Some(codegen_aborted) = codegen_aborted {1132        codegen_aborted.raise();1133    }11341135    compiled_modules1136}11371138fn execute_thin_lto_work_item<B: WriteBackendMethods>(1139    cgcx: &CodegenContext,1140    prof: &SelfProfilerRef,1141    shared_emitter: SharedEmitter,1142    tm_factory: TargetMachineFactoryFn<B>,1143    module: lto::ThinModule<B>,1144) -> CompiledModule {1145    let _timer = prof.generic_activity_with_arg("codegen_module_perform_lto", module.name());11461147    B::optimize_and_codegen_thin(cgcx, prof, &shared_emitter, tm_factory, module)1148}11491150/// Messages sent to the coordinator.1151pub(crate) enum Message<B: WriteBackendMethods> {1152    /// A jobserver token has become available. Sent from the jobserver helper1153    /// thread.1154    Token(io::Result<Acquired>),11551156    /// The backend has finished processing a work item for a codegen unit.1157    /// Sent from a backend worker thread.1158    WorkItem { result: Result<WorkItemResult<B>, Option<WorkerFatalError>> },11591160    /// The frontend has finished generating something (backend IR or a1161    /// post-LTO artifact) for a codegen unit, and it should be passed to the1162    /// backend. Sent from the main thread.1163    CodegenDone { llvm_work_item: WorkItem<B>, cost: u64 },11641165    /// Similar to `CodegenDone`, but for reusing a pre-LTO artifact1166    /// Sent from the main thread.1167    AddImportOnlyModule { bitcode_path: PathBuf, work_product: WorkProduct },11681169    /// The frontend has finished generating everything for all codegen units.1170    /// Sent from the main thread.1171    CodegenComplete,11721173    /// Some normal-ish compiler error occurred, and codegen should be wound1174    /// down. Sent from the main thread.1175    CodegenAborted,1176}11771178/// Messages sent to the coordinator.1179pub(crate) enum ThinLtoMessage {1180    /// A jobserver token has become available. Sent from the jobserver helper1181    /// thread.1182    Token(io::Result<Acquired>),11831184    /// The backend has finished processing a work item for a codegen unit.1185    /// Sent from a backend worker thread.1186    WorkItem { result: Result<CompiledModule, Option<WorkerFatalError>> },1187}11881189/// A message sent from the coordinator thread to the main thread telling it to1190/// process another codegen unit.1191pub struct CguMessage;11921193// A cut-down version of `rustc_errors::DiagInner` that impls `Send`, which1194// can be used to send diagnostics from codegen threads to the main thread.1195// It's missing the following fields from `rustc_errors::DiagInner`.1196// - `span`: it doesn't impl `Send`.1197// - `suggestions`: it doesn't impl `Send`, and isn't used for codegen1198//   diagnostics.1199// - `sort_span`: it doesn't impl `Send`.1200// - `is_lint`: lints aren't relevant during codegen.1201// - `emitted_at`: not used for codegen diagnostics.1202struct Diagnostic {1203    span: Vec<SpanData>,1204    level: Level,1205    messages: Vec<(DiagMessage, Style)>,1206    code: Option<ErrCode>,1207    children: Vec<Subdiagnostic>,1208    args: DiagArgMap,1209}12101211// A cut-down version of `rustc_errors::Subdiag` that impls `Send`. It's1212// missing the following fields from `rustc_errors::Subdiag`.1213// - `span`: it doesn't impl `Send`.1214struct Subdiagnostic {1215    level: Level,1216    messages: Vec<(DiagMessage, Style)>,1217}12181219#[derive(PartialEq, Clone, Copy, Debug)]1220enum MainThreadState {1221    /// Doing nothing.1222    Idle,12231224    /// Doing codegen, i.e. MIR-to-LLVM-IR conversion.1225    Codegenning,12261227    /// Idle, but lending the compiler process's Token to an LLVM thread so it can do useful work.1228    Lending,1229}12301231fn start_executing_work<B: ExtraBackendMethods>(1232    backend: B,1233    tcx: TyCtxt<'_>,1234    crate_info: &CrateInfo,1235    shared_emitter: SharedEmitter,1236    codegen_worker_send: Sender<CguMessage>,1237    coordinator_receive: Receiver<Message<B>>,1238    regular_config: Arc<ModuleConfig>,1239    allocator_config: Arc<ModuleConfig>,1240    mut allocator_module: Option<ModuleCodegen<B::Module>>,1241    coordinator_send: Sender<Message<B>>,1242) -> thread::JoinHandle<Result<MaybeLtoModules<B>, ()>> {1243    let sess = tcx.sess;1244    let prof = sess.prof.clone();12451246    let mut each_linked_rlib_for_lto = Vec::new();1247    let mut each_linked_rlib_file_for_lto = Vec::new();1248    if sess.lto() != Lto::No && sess.lto() != Lto::ThinLocal {1249        drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {1250            if link::ignored_for_lto(sess, crate_info, cnum) {1251                return;1252            }12531254            each_linked_rlib_for_lto.push(cnum);1255            each_linked_rlib_file_for_lto.push(path.to_path_buf());1256        }));1257    }12581259    // Compute the set of symbols we need to retain when doing LTO (if we need to)1260    let exported_symbols_for_lto =1261        Arc::new(lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto));12621263    // First up, convert our jobserver into a helper thread so we can use normal1264    // mpsc channels to manage our messages and such.1265    // After we've requested tokens then we'll, when we can,1266    // get tokens on `coordinator_receive` which will1267    // get managed in the main loop below.1268    let coordinator_send2 = coordinator_send.clone();1269    let helper = jobserver::client()1270        .into_helper_thread(move |token| {1271            drop(coordinator_send2.send(Message::Token::<B>(token)));1272        })1273        .expect("failed to spawn helper thread");12741275    let opt_level = tcx.backend_optimization_level(());1276    let backend_features = tcx.global_backend_features(()).clone();1277    let tm_factory = backend.target_machine_factory(tcx.sess, opt_level, &backend_features);12781279    let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {1280        let result = fs::create_dir_all(dir).and_then(|_| dir.canonicalize());1281        match result {1282            Ok(dir) => Some(dir),1283            Err(error) => sess.dcx().emit_fatal(ErrorCreatingRemarkDir { error }),1284        }1285    } else {1286        None1287    };12881289    let cgcx = CodegenContext {1290        crate_types: tcx.crate_types().to_vec(),1291        lto: sess.lto(),1292        use_linker_plugin_lto: sess.opts.cg.linker_plugin_lto.enabled(),1293        dylib_lto: sess.opts.unstable_opts.dylib_lto,1294        prefer_dynamic: sess.opts.cg.prefer_dynamic,1295        fewer_names: sess.fewer_names(),1296        save_temps: sess.opts.cg.save_temps,1297        time_trace: sess.opts.unstable_opts.llvm_time_trace,1298        remark: sess.opts.cg.remark.clone(),1299        remark_dir,1300        incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),1301        output_filenames: Arc::clone(tcx.output_filenames(())),1302        module_config: regular_config,1303        opt_level,1304        backend_features,1305        msvc_imps_needed: msvc_imps_needed(tcx),1306        is_pe_coff: tcx.sess.target.is_like_windows,1307        target_can_use_split_dwarf: tcx.sess.target_can_use_split_dwarf(),1308        target_arch: tcx.sess.target.arch.to_string(),1309        target_is_like_darwin: tcx.sess.target.is_like_darwin,1310        target_is_like_aix: tcx.sess.target.is_like_aix,1311        target_is_like_gpu: tcx.sess.target.is_like_gpu,1312        split_debuginfo: tcx.sess.split_debuginfo(),1313        split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind,1314        parallel: backend.supports_parallel() && !sess.opts.unstable_opts.no_parallel_backend,1315        pointer_size: tcx.data_layout.pointer_size(),1316        invocation_temp: sess.invocation_temp.clone(),1317    };13181319    // This is the "main loop" of parallel work happening for parallel codegen.1320    // It's here that we manage parallelism, schedule work, and work with1321    // messages coming from clients.1322    //1323    // There are a few environmental pre-conditions that shape how the system1324    // is set up:1325    //1326    // - Error reporting can only happen on the main thread because that's the1327    //   only place where we have access to the compiler `Session`.1328    // - LLVM work can be done on any thread.1329    // - Codegen can only happen on the main thread.1330    // - Each thread doing substantial work must be in possession of a `Token`1331    //   from the `Jobserver`.1332    // - The compiler process always holds one `Token`. Any additional `Tokens`1333    //   have to be requested from the `Jobserver`.1334    //1335    // Error Reporting1336    // ===============1337    // The error reporting restriction is handled separately from the rest: We1338    // set up a `SharedEmitter` that holds an open channel to the main thread.1339    // When an error occurs on any thread, the shared emitter will send the1340    // error message to the receiver main thread (`SharedEmitterMain`). The1341    // main thread will periodically query this error message queue and emit1342    // any error messages it has received. It might even abort compilation if1343    // it has received a fatal error. In this case we rely on all other threads1344    // being torn down automatically with the main thread.1345    // Since the main thread will often be busy doing codegen work, error1346    // reporting will be somewhat delayed, since the message queue can only be1347    // checked in between two work packages.1348    //1349    // Work Processing Infrastructure1350    // ==============================1351    // The work processing infrastructure knows three major actors:1352    //1353    // - the coordinator thread,1354    // - the main thread, and1355    // - LLVM worker threads1356    //1357    // The coordinator thread is running a message loop. It instructs the main1358    // thread about what work to do when, and it will spawn off LLVM worker1359    // threads as open LLVM WorkItems become available.1360    //1361    // The job of the main thread is to codegen CGUs into LLVM work packages1362    // (since the main thread is the only thread that can do this). The main1363    // thread will block until it receives a message from the coordinator, upon1364    // which it will codegen one CGU, send it to the coordinator and block1365    // again. This way the coordinator can control what the main thread is1366    // doing.1367    //1368    // The coordinator keeps a queue of LLVM WorkItems, and when a `Token` is1369    // available, it will spawn off a new LLVM worker thread and let it process1370    // a WorkItem. When a LLVM worker thread is done with its WorkItem,1371    // it will just shut down, which also frees all resources associated with1372    // the given LLVM module, and sends a message to the coordinator that the1373    // WorkItem has been completed.1374    //1375    // Work Scheduling1376    // ===============1377    // The scheduler's goal is to minimize the time it takes to complete all1378    // work there is, however, we also want to keep memory consumption low1379    // if possible. These two goals are at odds with each other: If memory1380    // consumption were not an issue, we could just let the main thread produce1381    // LLVM WorkItems at full speed, assuring maximal utilization of1382    // Tokens/LLVM worker threads. However, since codegen is usually faster1383    // than LLVM processing, the queue of LLVM WorkItems would fill up and each1384    // WorkItem potentially holds on to a substantial amount of memory.1385    //1386    // So the actual goal is to always produce just enough LLVM WorkItems as1387    // not to starve our LLVM worker threads. That means, once we have enough1388    // WorkItems in our queue, we can block the main thread, so it does not1389    // produce more until we need them.1390    //1391    // Doing LLVM Work on the Main Thread1392    // ----------------------------------1393    // Since the main thread owns the compiler process's implicit `Token`, it is1394    // wasteful to keep it blocked without doing any work. Therefore, what we do1395    // in this case is: We spawn off an additional LLVM worker thread that helps1396    // reduce the queue. The work it is doing corresponds to the implicit1397    // `Token`. The coordinator will mark the main thread as being busy with1398    // LLVM work. (The actual work happens on another OS thread but we just care1399    // about `Tokens`, not actual threads).1400    //1401    // When any LLVM worker thread finishes while the main thread is marked as1402    // "busy with LLVM work", we can do a little switcheroo: We give the Token1403    // of the just finished thread to the LLVM worker thread that is working on1404    // behalf of the main thread's implicit Token, thus freeing up the main1405    // thread again. The coordinator can then again decide what the main thread1406    // should do. This allows the coordinator to make decisions at more points1407    // in time.1408    //1409    // Striking a Balance between Throughput and Memory Consumption1410    // ------------------------------------------------------------1411    // Since our two goals, (1) use as many Tokens as possible and (2) keep1412    // memory consumption as low as possible, are in conflict with each other,1413    // we have to find a trade off between them. Right now, the goal is to keep1414    // all workers busy, which means that no worker should find the queue empty1415    // when it is ready to start.1416    // How do we do achieve this? Good question :) We actually never know how1417    // many `Tokens` are potentially available so it's hard to say how much to1418    // fill up the queue before switching the main thread to LLVM work. Also we1419    // currently don't have a means to estimate how long a running LLVM worker1420    // will still be busy with it's current WorkItem. However, we know the1421    // maximal count of available Tokens that makes sense (=the number of CPU1422    // cores), so we can take a conservative guess. The heuristic we use here1423    // is implemented in the `queue_full_enough()` function.1424    //1425    // Some Background on Jobservers1426    // -----------------------------1427    // It's worth also touching on the management of parallelism here. We don't1428    // want to just spawn a thread per work item because while that's optimal1429    // parallelism it may overload a system with too many threads or violate our1430    // configuration for the maximum amount of cpu to use for this process. To1431    // manage this we use the `jobserver` crate.1432    //1433    // Job servers are an artifact of GNU make and are used to manage1434    // parallelism between processes. A jobserver is a glorified IPC semaphore1435    // basically. Whenever we want to run some work we acquire the semaphore,1436    // and whenever we're done with that work we release the semaphore. In this1437    // manner we can ensure that the maximum number of parallel workers is1438    // capped at any one point in time.1439    //1440    // LTO and the coordinator thread1441    // ------------------------------1442    //1443    // The final job the coordinator thread is responsible for is managing LTO1444    // and how that works. When LTO is requested what we'll do is collect all1445    // optimized LLVM modules into a local vector on the coordinator. Once all1446    // modules have been codegened and optimized we hand this to the `lto`1447    // module for further optimization. The `lto` module will return back a list1448    // of more modules to work on, which the coordinator will continue to spawn1449    // work for.1450    //1451    // Each LLVM module is automatically sent back to the coordinator for LTO if1452    // necessary. There's already optimizations in place to avoid sending work1453    // back to the coordinator if LTO isn't requested.1454    let f = move || {1455        let _profiler = if cgcx.time_trace { B::thread_profiler() } else { Box::new(()) };14561457        // This is where we collect codegen units that have gone all the way1458        // through codegen and LLVM.1459        let mut compiled_modules = vec![];1460        let mut needs_fat_lto = Vec::new();1461        let mut needs_thin_lto = Vec::new();1462        let mut lto_import_only_modules = Vec::new();14631464        /// Possible state transitions:1465        /// - Ongoing -> Completed1466        /// - Ongoing -> Aborted1467        /// - Completed -> Aborted1468        #[derive(Debug, PartialEq)]1469        enum CodegenState {1470            Ongoing,1471            Completed,1472            Aborted,1473        }1474        use CodegenState::*;1475        let mut codegen_state = Ongoing;14761477        // This is the queue of LLVM work items that still need processing.1478        let mut work_items = Vec::<(WorkItem<B>, u64)>::new();14791480        // This are the Jobserver Tokens we currently hold. Does not include1481        // the implicit Token the compiler process owns no matter what.1482        let mut tokens = Vec::new();14831484        let mut main_thread_state = MainThreadState::Idle;14851486        // How many LLVM worker threads are running while holding a Token. This1487        // *excludes* any that the main thread is lending a Token to.1488        let mut running_with_own_token = 0;14891490        // How many LLVM worker threads are running in total. This *includes*1491        // any that the main thread is lending a Token to.1492        let running_with_any_token = |main_thread_state, running_with_own_token| {1493            running_with_own_token1494                + if main_thread_state == MainThreadState::Lending { 1 } else { 0 }1495        };14961497        let mut llvm_start_time: Option<VerboseTimingGuard<'_>> = None;14981499        if let Some(allocator_module) = &mut allocator_module {1500            B::optimize(&cgcx, &prof, &shared_emitter, allocator_module, &allocator_config);1501        }15021503        // Run the message loop while there's still anything that needs message1504        // processing. Note that as soon as codegen is aborted we simply want to1505        // wait for all existing work to finish, so many of the conditions here1506        // only apply if codegen hasn't been aborted as they represent pending1507        // work to be done.1508        loop {1509            // While there are still CGUs to be codegened, the coordinator has1510            // to decide how to utilize the compiler processes implicit Token:1511            // For codegenning more CGU or for running them through LLVM.1512            if codegen_state == Ongoing {1513                if main_thread_state == MainThreadState::Idle {1514                    // Compute the number of workers that will be running once we've taken as many1515                    // items from the work queue as we can, plus one for the main thread. It's not1516                    // critically important that we use this instead of just1517                    // `running_with_own_token`, but it prevents the `queue_full_enough` heuristic1518                    // from fluctuating just because a worker finished up and we decreased the1519                    // `running_with_own_token` count, even though we're just going to increase it1520                    // right after this when we put a new worker to work.1521                    let extra_tokens = tokens.len().checked_sub(running_with_own_token).unwrap();1522                    let additional_running = std::cmp::min(extra_tokens, work_items.len());1523                    let anticipated_running = running_with_own_token + additional_running + 1;15241525                    if !queue_full_enough(work_items.len(), anticipated_running) {1526                        // The queue is not full enough, process more codegen units:1527                        if codegen_worker_send.send(CguMessage).is_err() {1528                            panic!("Could not send CguMessage to main thread")1529                        }1530                        main_thread_state = MainThreadState::Codegenning;1531                    } else {1532                        // The queue is full enough to not let the worker1533                        // threads starve. Use the implicit Token to do some1534                        // LLVM work too.1535                        let (item, _) =1536                            work_items.pop().expect("queue empty - queue_full_enough() broken?");1537                        main_thread_state = MainThreadState::Lending;1538                        spawn_work(1539                            &cgcx,1540                            &prof,1541                            shared_emitter.clone(),1542                            coordinator_send.clone(),1543                            &mut llvm_start_time,1544                            item,1545                        );1546                    }1547                }1548            } else if codegen_state == Completed {1549                if running_with_any_token(main_thread_state, running_with_own_token) == 01550                    && work_items.is_empty()1551                {1552                    // All codegen work is done.1553                    break;1554                }15551556                // In this branch, we know that everything has been codegened,1557                // so it's just a matter of determining whether the implicit1558                // Token is free to use for LLVM work.1559                match main_thread_state {1560                    MainThreadState::Idle => {1561                        if let Some((item, _)) = work_items.pop() {1562                            main_thread_state = MainThreadState::Lending;1563                            spawn_work(1564                                &cgcx,1565                                &prof,1566                                shared_emitter.clone(),1567                                coordinator_send.clone(),1568                                &mut llvm_start_time,1569                                item,1570                            );1571                        } else {1572                            // There is no unstarted work, so let the main thread1573                            // take over for a running worker. Otherwise the1574                            // implicit token would just go to waste.1575                            // We reduce the `running` counter by one. The1576                            // `tokens.truncate()` below will take care of1577                            // giving the Token back.1578                            assert!(running_with_own_token > 0);1579                            running_with_own_token -= 1;1580                            main_thread_state = MainThreadState::Lending;1581                        }1582                    }1583                    MainThreadState::Codegenning => bug!(1584                        "codegen worker should not be codegenning after \1585                              codegen was already completed"1586                    ),1587                    MainThreadState::Lending => {1588                        // Already making good use of that token1589                    }1590                }1591            } else {1592                // Don't queue up any more work if codegen was aborted, we're1593                // just waiting for our existing children to finish.1594                assert!(codegen_state == Aborted);1595                if running_with_any_token(main_thread_state, running_with_own_token) == 0 {1596                    break;1597                }1598            }15991600            // Spin up what work we can, only doing this while we've got available1601            // parallelism slots and work left to spawn.1602            if codegen_state != Aborted {1603                while running_with_own_token < tokens.len()1604                    && let Some((item, _)) = work_items.pop()1605                {1606                    spawn_work(1607                        &cgcx,1608                        &prof,1609                        shared_emitter.clone(),1610                        coordinator_send.clone(),1611                        &mut llvm_start_time,1612                        item,1613                    );1614                    running_with_own_token += 1;1615                }1616            }16171618            // Relinquish accidentally acquired extra tokens.1619            tokens.truncate(running_with_own_token);16201621            match coordinator_receive.recv().unwrap() {1622                // Save the token locally and the next turn of the loop will use1623                // this to spawn a new unit of work, or it may get dropped1624                // immediately if we have no more work to spawn.1625                Message::Token(token) => {1626                    match token {1627                        Ok(token) => {1628                            tokens.push(token);16291630                            if main_thread_state == MainThreadState::Lending {1631                                // If the main thread token is used for LLVM work1632                                // at the moment, we turn that thread into a regular1633                                // LLVM worker thread, so the main thread is free1634                                // to react to codegen demand.1635                                main_thread_state = MainThreadState::Idle;1636                                running_with_own_token += 1;1637                            }1638                        }1639                        Err(e) => {1640                            let msg = &format!("failed to acquire jobserver token: {e}");1641                            shared_emitter.fatal(msg);1642                            codegen_state = Aborted;1643                        }1644                    }1645                }16461647                Message::CodegenDone { llvm_work_item, cost } => {1648                    // We keep the queue sorted by estimated processing cost,1649                    // so that more expensive items are processed earlier. This1650                    // is good for throughput as it gives the main thread more1651                    // time to fill up the queue and it avoids scheduling1652                    // expensive items to the end.1653                    // Note, however, that this is not ideal for memory1654                    // consumption, as LLVM module sizes are not evenly1655                    // distributed.1656                    let insertion_index = work_items.binary_search_by_key(&cost, |&(_, cost)| cost);1657                    let insertion_index = match insertion_index {1658                        Ok(idx) | Err(idx) => idx,1659                    };1660                    work_items.insert(insertion_index, (llvm_work_item, cost));16611662                    if cgcx.parallel {1663                        helper.request_token();1664                    }1665                    assert_eq!(main_thread_state, MainThreadState::Codegenning);1666                    main_thread_state = MainThreadState::Idle;1667                }16681669                Message::CodegenComplete => {1670                    if codegen_state != Aborted {1671                        codegen_state = Completed;1672                    }1673                    assert_eq!(main_thread_state, MainThreadState::Codegenning);1674                    main_thread_state = MainThreadState::Idle;1675                }16761677                // If codegen is aborted that means translation was aborted due1678                // to some normal-ish compiler error. In this situation we want1679                // to exit as soon as possible, but we want to make sure all1680                // existing work has finished. Flag codegen as being done, and1681                // then conditions above will ensure no more work is spawned but1682                // we'll keep executing this loop until `running_with_own_token`1683                // hits 0.1684                Message::CodegenAborted => {1685                    codegen_state = Aborted;1686                }16871688                Message::WorkItem { result } => {1689                    // If a thread exits successfully then we drop a token associated1690                    // with that worker and update our `running_with_own_token` count.1691                    // We may later re-acquire a token to continue running more work.1692                    // We may also not actually drop a token here if the worker was1693                    // running with an "ephemeral token".1694                    if main_thread_state == MainThreadState::Lending {1695                        main_thread_state = MainThreadState::Idle;1696                    } else {1697                        running_with_own_token -= 1;1698                    }16991700                    match result {1701                        Ok(WorkItemResult::Finished(compiled_module)) => {1702                            compiled_modules.push(compiled_module);1703                        }1704                        Ok(WorkItemResult::NeedsFatLto(fat_lto_input)) => {1705                            assert!(needs_thin_lto.is_empty());1706                            needs_fat_lto.push(fat_lto_input);1707                        }1708                        Ok(WorkItemResult::NeedsThinLto(name, thin_buffer)) => {1709                            assert!(needs_fat_lto.is_empty());1710                            needs_thin_lto.push(ThinLtoInput::Red {1711                                name,1712                                buffer: SerializedModule::Local(thin_buffer),1713                            });1714                        }1715                        Err(Some(WorkerFatalError)) => {1716                            // Like `CodegenAborted`, wait for remaining work to finish.1717                            codegen_state = Aborted;1718                        }1719                        Err(None) => {1720                            // If the thread failed that means it panicked, so1721                            // we abort immediately.1722                            bug!("worker thread panicked");1723                        }1724                    }1725                }17261727                Message::AddImportOnlyModule { bitcode_path, work_product } => {1728                    assert_eq!(codegen_state, Ongoing);1729                    assert_eq!(main_thread_state, MainThreadState::Codegenning);1730                    lto_import_only_modules.push((bitcode_path, work_product));1731                    main_thread_state = MainThreadState::Idle;1732                }1733            }1734        }17351736        // Drop to print timings1737        drop(llvm_start_time);17381739        if codegen_state == Aborted {1740            return Err(());1741        }17421743        drop(codegen_state);1744        drop(tokens);1745        drop(helper);1746        assert!(work_items.is_empty());17471748        if !needs_fat_lto.is_empty() {1749            assert!(compiled_modules.is_empty());1750            assert!(needs_thin_lto.is_empty());17511752            if let Some(allocator_module) = allocator_module.take() {1753                needs_fat_lto.push(FatLtoInput::InMemory(allocator_module));1754            }17551756            for (bitcode_path, wp) in lto_import_only_modules {1757                needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, bitcode_path })1758            }17591760            return Ok(MaybeLtoModules::FatLto {1761                cgcx,1762                exported_symbols_for_lto,1763                each_linked_rlib_file_for_lto,1764                needs_fat_lto,1765            });1766        } else if !needs_thin_lto.is_empty() || !lto_import_only_modules.is_empty() {1767            assert!(compiled_modules.is_empty());1768            assert!(needs_fat_lto.is_empty());17691770            for (bitcode_path, wp) in lto_import_only_modules {1771                needs_thin_lto.push(ThinLtoInput::Green { wp, bitcode_path })1772            }17731774            if cgcx.lto == Lto::ThinLocal {1775                compiled_modules.extend(do_thin_lto::<B>(1776                    &cgcx,1777                    &prof,1778                    shared_emitter.clone(),1779                    tm_factory,1780                    exported_symbols_for_lto,1781                    each_linked_rlib_file_for_lto,1782                    needs_thin_lto,1783                ));1784            } else {1785                if let Some(allocator_module) = allocator_module.take() {1786                    let thin_buffer = B::serialize_module(allocator_module.module_llvm, true);1787                    needs_thin_lto.push(ThinLtoInput::Red {1788                        name: allocator_module.name,1789                        buffer: SerializedModule::Local(thin_buffer),1790                    });1791                }17921793                return Ok(MaybeLtoModules::ThinLto {1794                    cgcx,1795                    exported_symbols_for_lto,1796                    each_linked_rlib_file_for_lto,1797                    needs_thin_lto,1798                });1799            }1800        }18011802        Ok(MaybeLtoModules::NoLto(CompiledModules {1803            modules: compiled_modules,1804            allocator_module: allocator_module.map(|allocator_module| {1805                B::codegen(&cgcx, &prof, &shared_emitter, allocator_module, &allocator_config)1806            }),1807        }))1808    };1809    return std::thread::Builder::new()1810        .name("coordinator".to_owned())1811        .spawn(f)1812        .expect("failed to spawn coordinator thread");18131814    // A heuristic that determines if we have enough LLVM WorkItems in the1815    // queue so that the main thread can do LLVM work instead of codegen1816    fn queue_full_enough(items_in_queue: usize, workers_running: usize) -> bool {1817        // This heuristic scales ahead-of-time codegen according to available1818        // concurrency, as measured by `workers_running`. The idea is that the1819        // more concurrency we have available, the more demand there will be for1820        // work items, and the fuller the queue should be kept to meet demand.1821        // An important property of this approach is that we codegen ahead of1822        // time only as much as necessary, so as to keep fewer LLVM modules in1823        // memory at once, thereby reducing memory consumption.1824        //1825        // When the number of workers running is less than the max concurrency1826        // available to us, this heuristic can cause us to instruct the main1827        // thread to work on an LLVM item (that is, tell it to "LLVM") instead1828        // of codegen, even though it seems like it *should* be codegenning so1829        // that we can create more work items and spawn more LLVM workers.1830        //1831        // But this is not a problem. When the main thread is told to LLVM,1832        // according to this heuristic and how work is scheduled, there is1833        // always at least one item in the queue, and therefore at least one1834        // pending jobserver token request. If there *is* more concurrency1835        // available, we will immediately receive a token, which will upgrade1836        // the main thread's LLVM worker to a real one (conceptually), and free1837        // up the main thread to codegen if necessary. On the other hand, if1838        // there isn't more concurrency, then the main thread working on an LLVM1839        // item is appropriate, as long as the queue is full enough for demand.1840        //1841        // Speaking of which, how full should we keep the queue? Probably less1842        // full than you'd think. A lot has to go wrong for the queue not to be1843        // full enough and for that to have a negative effect on compile times.1844        //1845        // Workers are unlikely to finish at exactly the same time, so when one1846        // finishes and takes another work item off the queue, we often have1847        // ample time to codegen at that point before the next worker finishes.1848        // But suppose that codegen takes so long that the workers exhaust the1849        // queue, and we have one or more workers that have nothing to work on.1850        // Well, it might not be so bad. Of all the LLVM modules we create and1851        // optimize, one has to finish last. It's not necessarily the case that1852        // by losing some concurrency for a moment, we delay the point at which1853        // that last LLVM module is finished and the rest of compilation can1854        // proceed. Also, when we can't take advantage of some concurrency, we1855        // give tokens back to the job server. That enables some other rustc to1856        // potentially make use of the available concurrency. That could even1857        // *decrease* overall compile time if we're lucky. But yes, if no other1858        // rustc can make use of the concurrency, then we've squandered it.1859        //1860        // However, keeping the queue full is also beneficial when we have a1861        // surge in available concurrency. Then items can be taken from the1862        // queue immediately, without having to wait for codegen.1863        //1864        // So, the heuristic below tries to keep one item in the queue for every1865        // four running workers. Based on limited benchmarking, this appears to1866        // be more than sufficient to avoid increasing compilation times.1867        let quarter_of_workers = workers_running - 3 * workers_running / 4;1868        items_in_queue > 0 && items_in_queue >= quarter_of_workers1869    }1870}18711872/// `FatalError` is explicitly not `Send`.1873#[must_use]1874pub(crate) struct WorkerFatalError;18751876fn spawn_work<'a, B: WriteBackendMethods>(1877    cgcx: &CodegenContext,1878    prof: &'a SelfProfilerRef,1879    shared_emitter: SharedEmitter,1880    coordinator_send: Sender<Message<B>>,1881    llvm_start_time: &mut Option<VerboseTimingGuard<'a>>,1882    work: WorkItem<B>,1883) {1884    if llvm_start_time.is_none() {1885        *llvm_start_time = Some(prof.verbose_generic_activity("LLVM_passes"));1886    }18871888    let cgcx = cgcx.clone();1889    let prof = prof.clone();18901891    let name = work.short_description();1892    let f = move || {1893        let _profiler = if cgcx.time_trace { B::thread_profiler() } else { Box::new(()) };18941895        let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work {1896            WorkItem::Optimize(m) => execute_optimize_work_item(&cgcx, &prof, shared_emitter, m),1897            WorkItem::CopyPostLtoArtifacts(m) => WorkItemResult::Finished(1898                execute_copy_from_cache_work_item(&cgcx, &prof, shared_emitter, m),1899            ),1900        }));19011902        let msg = match result {1903            Ok(result) => Message::WorkItem::<B> { result: Ok(result) },19041905            // We ignore any `FatalError` coming out of `execute_work_item`, as a1906            // diagnostic was already sent off to the main thread - just surface1907            // that there was an error in this worker.1908            Err(err) if err.is::<FatalErrorMarker>() => {1909                Message::WorkItem::<B> { result: Err(Some(WorkerFatalError)) }1910            }19111912            Err(_) => Message::WorkItem::<B> { result: Err(None) },1913        };1914        drop(coordinator_send.send(msg));1915    };1916    std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");1917}19181919fn spawn_thin_lto_work<B: WriteBackendMethods>(1920    cgcx: &CodegenContext,1921    prof: &SelfProfilerRef,1922    shared_emitter: SharedEmitter,1923    tm_factory: TargetMachineFactoryFn<B>,1924    coordinator_send: Sender<ThinLtoMessage>,1925    work: ThinLtoWorkItem<B>,1926) {1927    let cgcx = cgcx.clone();1928    let prof = prof.clone();19291930    let name = work.short_description();1931    let f = move || {1932        let _profiler = if cgcx.time_trace { B::thread_profiler() } else { Box::new(()) };19331934        let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work {1935            ThinLtoWorkItem::CopyPostLtoArtifacts(m) => {1936                execute_copy_from_cache_work_item(&cgcx, &prof, shared_emitter, m)1937            }1938            ThinLtoWorkItem::ThinLto(m) => {1939                execute_thin_lto_work_item(&cgcx, &prof, shared_emitter, tm_factory, m)1940            }1941        }));19421943        let msg = match result {1944            Ok(result) => ThinLtoMessage::WorkItem { result: Ok(result) },19451946            // We ignore any `FatalError` coming out of `execute_work_item`, as a1947            // diagnostic was already sent off to the main thread - just surface1948            // that there was an error in this worker.1949            Err(err) if err.is::<FatalErrorMarker>() => {1950                ThinLtoMessage::WorkItem { result: Err(Some(WorkerFatalError)) }1951            }19521953            Err(_) => ThinLtoMessage::WorkItem { result: Err(None) },1954        };1955        drop(coordinator_send.send(msg));1956    };1957    std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");1958}19591960enum SharedEmitterMessage {1961    Diagnostic(Diagnostic),1962    InlineAsmError(InlineAsmError),1963    Fatal(String),1964}19651966pub struct InlineAsmError {1967    pub span: SpanData,1968    pub msg: String,1969    pub level: Level,1970    pub source: Option<(String, Vec<InnerSpan>)>,1971}19721973#[derive(Clone)]1974pub struct SharedEmitter {1975    sender: Sender<SharedEmitterMessage>,1976}19771978pub struct SharedEmitterMain {1979    receiver: Receiver<SharedEmitterMessage>,1980}19811982impl SharedEmitter {1983    fn new() -> (SharedEmitter, SharedEmitterMain) {1984        let (sender, receiver) = channel();19851986        (SharedEmitter { sender }, SharedEmitterMain { receiver })1987    }19881989    pub fn inline_asm_error(&self, err: InlineAsmError) {1990        drop(self.sender.send(SharedEmitterMessage::InlineAsmError(err)));1991    }19921993    fn fatal(&self, msg: &str) {1994        drop(self.sender.send(SharedEmitterMessage::Fatal(msg.to_string())));1995    }1996}19971998impl Emitter for SharedEmitter {1999    fn emit_diagnostic(&mut self, mut diag: rustc_errors::DiagInner) {2000        // Check that we aren't missing anything interesting when converting to

Code quality findings 35

Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
/// Depends on [`ExtraBackendMethods::supports_parallel()`] and `-Zno_parallel_backend`.
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
if let [module] = &compiled_modules.modules[..] {
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
if let [module] = &compiled_modules.modules[..] {
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
&name[index + 1..] // +1 skips the leading '-'.
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
let incr_comp_session_dir = cgcx.incr_comp_session_dir.as_ref().unwrap();
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.expect(
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.expect("failed to spawn helper thread");
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
match coordinator_receive.recv().unwrap() {
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.expect("failed to spawn helper thread");
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
let extra_tokens = tokens.len().checked_sub(running_with_own_token).unwrap();
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
work_items.pop().expect("queue empty - queue_full_enough() broken?");
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
match coordinator_receive.recv().unwrap() {
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
.expect("failed to spawn coordinator thread");
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
std::thread::Builder::new().name(name).spawn(f).expect("failed to spawn work thread");
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
self.future.take().unwrap().join()
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
compiled_modules.expect("fatal error emitted but not sent to SharedEmitter");
Info: Wildcard imports (`use some::path::*;`) can obscure the origin of names and lead to conflicts. Prefer importing specific items explicitly.
info maintainability wildcard-import
use crate::traits::*;
Info: Wildcard imports (`use some::path::*;`) can obscure the origin of names and lead to conflicts. Prefer importing specific items explicitly.
info maintainability wildcard-import
use config::OptLevel::*;
Performance Info: Calling .push() repeatedly inside a loop without prior capacity reservation can lead to multiple reallocations. Consider using `Vec::with_capacity(n)` or `vec.reserve(n)` if the approximate number of elements is known.
info performance push-without-reserve
files.push((OutputType::Object.extension(), object_file_path.as_path()));
Performance Info: Calling .push() repeatedly inside a loop without prior capacity reservation can lead to multiple reallocations. Consider using `Vec::with_capacity(n)` or `vec.reserve(n)` if the approximate number of elements is known.
info performance push-without-reserve
files.push(("dwo", dwarf_object_file_path.as_path()));
Performance Info: Calling .push() repeatedly inside a loop without prior capacity reservation can lead to multiple reallocations. Consider using `Vec::with_capacity(n)` or `vec.reserve(n)` if the approximate number of elements is known.
info performance push-without-reserve
files.push((OutputType::Assembly.extension(), path.as_path()));
Info: Ensure 'match' statements are exhaustive. If matching on enums, consider adding a wildcard arm `_ => {}` only if necessary and intentional, as it suppresses warnings about unhandled variants.
info correctness match-wildcard
let copy_gracefully = |from: &Path, to: &OutFileName| match to {
Info: Ensure 'match' statements are exhaustive. If matching on enums, consider adding a wildcard arm `_ => {}` only if necessary and intentional, as it suppresses warnings about unhandled variants.
info correctness match-wildcard
match sess_lto {
Info: This standard library function returns a Result. Ensure the Result is handled properly (e.g., using '?', match, if let) rather than potentially panicking with .unwrap() or .expect().
info correctness unhandled-result
fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| {
Info: This standard library function returns a Result. Ensure the Result is handled properly (e.g., using '?', match, if let) rather than potentially panicking with .unwrap() or .expect().
info correctness unhandled-result
fs::write(&path, buffer.data()).unwrap_or_else(|e| {
Performance Info: Frequent cloning, especially of Strings, Vecs, or other heap-allocated types inside loops, can be expensive. Consider using references/borrowing where possible.
info performance clone-in-loop
shared_emitter.clone(),
Performance Info: Frequent cloning, especially of Strings, Vecs, or other heap-allocated types inside loops, can be expensive. Consider using references/borrowing where possible.
info performance clone-in-loop
coordinator_send.clone(),
Info: Wildcard imports (`use some::path::*;`) can obscure the origin of names and lead to conflicts. Prefer importing specific items explicitly.
info maintainability wildcard-import
use CodegenState::*;
Performance Info: Frequent cloning, especially of Strings, Vecs, or other heap-allocated types inside loops, can be expensive. Consider using references/borrowing where possible.
info performance clone-in-loop
shared_emitter.clone(),
Performance Info: Frequent cloning, especially of Strings, Vecs, or other heap-allocated types inside loops, can be expensive. Consider using references/borrowing where possible.
info performance clone-in-loop
coordinator_send.clone(),
Performance Info: Calling .push() repeatedly inside a loop without prior capacity reservation can lead to multiple reallocations. Consider using `Vec::with_capacity(n)` or `vec.reserve(n)` if the approximate number of elements is known.
info performance push-without-reserve
needs_fat_lto.push(FatLtoInput::InMemory(allocator_module));
Performance Info: Calling .push() repeatedly inside a loop without prior capacity reservation can lead to multiple reallocations. Consider using `Vec::with_capacity(n)` or `vec.reserve(n)` if the approximate number of elements is known.
info performance push-without-reserve
needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, bitcode_path })
Performance Info: Calling .push() repeatedly inside a loop without prior capacity reservation can lead to multiple reallocations. Consider using `Vec::with_capacity(n)` or `vec.reserve(n)` if the approximate number of elements is known.
info performance push-without-reserve
needs_thin_lto.push(ThinLtoInput::Green { wp, bitcode_path })
Performance Info: Frequent cloning, especially of Strings, Vecs, or other heap-allocated types inside loops, can be expensive. Consider using references/borrowing where possible.
info performance clone-in-loop
shared_emitter.clone(),

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.