1use std::assert_matches::assert_matches;
2use std::marker::PhantomData;
3use std::panic::AssertUnwindSafe;
4use std::path::{Path, PathBuf};
5use std::sync::Arc;
6use std::sync::mpsc::{Receiver, Sender, channel};
7use std::{fs, io, mem, str, thread};
8
9use rustc_abi::Size;
10use rustc_ast::attr;
11use rustc_data_structures::fx::FxIndexMap;
12use rustc_data_structures::jobserver::{self, Acquired};
13use rustc_data_structures::memmap::Mmap;
14use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};
15use rustc_errors::emitter::Emitter;
16use rustc_errors::translation::Translator;
17use rustc_errors::{
18 Diag, DiagArgMap, DiagCtxt, DiagMessage, ErrCode, FatalError, FatalErrorMarker, Level,
19 MultiSpan, Style, Suggestions,
20};
21use rustc_fs_util::link_or_copy;
22use rustc_incremental::{
23 copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir, in_incr_comp_dir_sess,
24};
25use rustc_metadata::fs::copy_to_stdout;
26use rustc_middle::bug;
27use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
28use rustc_middle::ty::TyCtxt;
29use rustc_session::Session;
30use rustc_session::config::{
31 self, CrateType, Lto, OutFileName, OutputFilenames, OutputType, Passes, SwitchWithOptPath,
32};
33use rustc_span::source_map::SourceMap;
34use rustc_span::{FileName, InnerSpan, Span, SpanData, sym};
35use rustc_target::spec::{MergeFunctions, SanitizerSet};
36use tracing::debug;
37
38use super::link::{self, ensure_removed};
39use super::lto::{self, SerializedModule};
40use crate::back::lto::check_lto_allowed;
41use crate::errors::ErrorCreatingRemarkDir;
42use crate::traits::*;
43use crate::{
44 CachedModuleCodegen, CodegenResults, CompiledModule, CrateInfo, ModuleCodegen, ModuleKind,
45 errors,
46};
47
48const PRE_LTO_BC_EXT: &str = "pre-lto.bc";
49
50#[derive(Clone, Copy, PartialEq)]
52pub enum EmitObj {
53 None,
55
56 Bitcode,
59
60 ObjectCode(BitcodeSection),
62}
63
64#[derive(Clone, Copy, PartialEq)]
66pub enum BitcodeSection {
67 None,
69
70 Full,
72}
73
74pub struct ModuleConfig {
76 pub passes: Vec<String>,
78 pub opt_level: Option<config::OptLevel>,
81
82 pub pgo_gen: SwitchWithOptPath,
83 pub pgo_use: Option<PathBuf>,
84 pub pgo_sample_use: Option<PathBuf>,
85 pub debug_info_for_profiling: bool,
86 pub instrument_coverage: bool,
87
88 pub sanitizer: SanitizerSet,
89 pub sanitizer_recover: SanitizerSet,
90 pub sanitizer_dataflow_abilist: Vec<String>,
91 pub sanitizer_memory_track_origins: usize,
92
93 pub emit_pre_lto_bc: bool,
95 pub emit_no_opt_bc: bool,
96 pub emit_bc: bool,
97 pub emit_ir: bool,
98 pub emit_asm: bool,
99 pub emit_obj: EmitObj,
100 pub emit_thin_lto: bool,
101 pub emit_thin_lto_summary: bool,
102
103 pub verify_llvm_ir: bool,
106 pub lint_llvm_ir: bool,
107 pub no_prepopulate_passes: bool,
108 pub no_builtins: bool,
109 pub vectorize_loop: bool,
110 pub vectorize_slp: bool,
111 pub merge_functions: bool,
112 pub emit_lifetime_markers: bool,
113 pub llvm_plugins: Vec<String>,
114 pub autodiff: Vec<config::AutoDiff>,
115 pub offload: Vec<config::Offload>,
116}
117
118impl ModuleConfig {
119 fn new(kind: ModuleKind, tcx: TyCtxt<'_>, no_builtins: bool) -> ModuleConfig {
120 macro_rules! if_regular {
123 ($regular: expr, $other: expr) => {
124 if let ModuleKind::Regular = kind { $regular } else { $other }
125 };
126 }
127
128 let sess = tcx.sess;
129 let opt_level_and_size = if_regular!(Some(sess.opts.optimize), None);
130
131 let save_temps = sess.opts.cg.save_temps;
132
133 let should_emit_obj = sess.opts.output_types.contains_key(&OutputType::Exe)
134 || match kind {
135 ModuleKind::Regular => sess.opts.output_types.contains_key(&OutputType::Object),
136 ModuleKind::Allocator => false,
137 };
138
139 let emit_obj = if !should_emit_obj {
140 EmitObj::None
141 } else if sess.target.obj_is_bitcode
142 || (sess.opts.cg.linker_plugin_lto.enabled() && !no_builtins)
143 {
144 EmitObj::Bitcode
159 } else if need_bitcode_in_object(tcx) {
160 EmitObj::ObjectCode(BitcodeSection::Full)
161 } else {
162 EmitObj::ObjectCode(BitcodeSection::None)
163 };
164
165 ModuleConfig {
166 passes: if_regular!(sess.opts.cg.passes.clone(), vec![]),
167
168 opt_level: opt_level_and_size,
169
170 pgo_gen: if_regular!(
171 sess.opts.cg.profile_generate.clone(),
172 SwitchWithOptPath::Disabled
173 ),
174 pgo_use: if_regular!(sess.opts.cg.profile_use.clone(), None),
175 pgo_sample_use: if_regular!(sess.opts.unstable_opts.profile_sample_use.clone(), None),
176 debug_info_for_profiling: sess.opts.unstable_opts.debug_info_for_profiling,
177 instrument_coverage: if_regular!(sess.instrument_coverage(), false),
178
179 sanitizer: if_regular!(sess.sanitizers(), SanitizerSet::empty()),
180 sanitizer_dataflow_abilist: if_regular!(
181 sess.opts.unstable_opts.sanitizer_dataflow_abilist.clone(),
182 Vec::new()
183 ),
184 sanitizer_recover: if_regular!(
185 sess.opts.unstable_opts.sanitizer_recover,
186 SanitizerSet::empty()
187 ),
188 sanitizer_memory_track_origins: if_regular!(
189 sess.opts.unstable_opts.sanitizer_memory_track_origins,
190 0
191 ),
192
193 emit_pre_lto_bc: if_regular!(
194 save_temps || need_pre_lto_bitcode_for_incr_comp(sess),
195 false
196 ),
197 emit_no_opt_bc: if_regular!(save_temps, false),
198 emit_bc: if_regular!(
199 save_temps || sess.opts.output_types.contains_key(&OutputType::Bitcode),
200 save_temps
201 ),
202 emit_ir: if_regular!(
203 sess.opts.output_types.contains_key(&OutputType::LlvmAssembly),
204 false
205 ),
206 emit_asm: if_regular!(
207 sess.opts.output_types.contains_key(&OutputType::Assembly),
208 false
209 ),
210 emit_obj,
211 emit_thin_lto: sess.opts.unstable_opts.emit_thin_lto && sess.lto() != Lto::Fat,
214 emit_thin_lto_summary: if_regular!(
215 sess.opts.output_types.contains_key(&OutputType::ThinLinkBitcode),
216 false
217 ),
218
219 verify_llvm_ir: sess.verify_llvm_ir(),
220 lint_llvm_ir: sess.opts.unstable_opts.lint_llvm_ir,
221 no_prepopulate_passes: sess.opts.cg.no_prepopulate_passes,
222 no_builtins: no_builtins || sess.target.no_builtins,
223
224 vectorize_loop: !sess.opts.cg.no_vectorize_loops
227 && (sess.opts.optimize == config::OptLevel::More
228 || sess.opts.optimize == config::OptLevel::Aggressive),
229 vectorize_slp: !sess.opts.cg.no_vectorize_slp
230 && sess.opts.optimize == config::OptLevel::Aggressive,
231
232 merge_functions: match sess
242 .opts
243 .unstable_opts
244 .merge_functions
245 .unwrap_or(sess.target.merge_functions)
246 {
247 MergeFunctions::Disabled => false,
248 MergeFunctions::Trampolines | MergeFunctions::Aliases => {
249 use config::OptLevel::*;
250 match sess.opts.optimize {
251 Aggressive | More | SizeMin | Size => true,
252 Less | No => false,
253 }
254 }
255 },
256
257 emit_lifetime_markers: sess.emit_lifetime_markers(),
258 llvm_plugins: if_regular!(sess.opts.unstable_opts.llvm_plugins.clone(), vec![]),
259 autodiff: if_regular!(sess.opts.unstable_opts.autodiff.clone(), vec![]),
260 offload: if_regular!(sess.opts.unstable_opts.offload.clone(), vec![]),
261 }
262 }
263
264 pub fn bitcode_needed(&self) -> bool {
265 self.emit_bc
266 || self.emit_thin_lto_summary
267 || self.emit_obj == EmitObj::Bitcode
268 || self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)
269 }
270
271 pub fn embed_bitcode(&self) -> bool {
272 self.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)
273 }
274}
275
276pub struct TargetMachineFactoryConfig {
278 pub split_dwarf_file: Option<PathBuf>,
282
283 pub output_obj_file: Option<PathBuf>,
286}
287
288impl TargetMachineFactoryConfig {
289 pub fn new(
290 cgcx: &CodegenContext<impl WriteBackendMethods>,
291 module_name: &str,
292 ) -> TargetMachineFactoryConfig {
293 let split_dwarf_file = if cgcx.target_can_use_split_dwarf {
294 cgcx.output_filenames.split_dwarf_path(
295 cgcx.split_debuginfo,
296 cgcx.split_dwarf_kind,
297 module_name,
298 cgcx.invocation_temp.as_deref(),
299 )
300 } else {
301 None
302 };
303
304 let output_obj_file = Some(cgcx.output_filenames.temp_path_for_cgu(
305 OutputType::Object,
306 module_name,
307 cgcx.invocation_temp.as_deref(),
308 ));
309 TargetMachineFactoryConfig { split_dwarf_file, output_obj_file }
310 }
311}
312
313pub type TargetMachineFactoryFn<B> = Arc<
314 dyn Fn(
315 TargetMachineFactoryConfig,
316 ) -> Result<
317 <B as WriteBackendMethods>::TargetMachine,
318 <B as WriteBackendMethods>::TargetMachineError,
319 > + Send
320 + Sync,
321>;
322
323#[derive(Clone)]
325pub struct CodegenContext<B: WriteBackendMethods> {
326 pub prof: SelfProfilerRef,
328 pub lto: Lto,
329 pub save_temps: bool,
330 pub fewer_names: bool,
331 pub time_trace: bool,
332 pub opts: Arc<config::Options>,
333 pub crate_types: Vec<CrateType>,
334 pub output_filenames: Arc<OutputFilenames>,
335 pub invocation_temp: Option<String>,
336 pub module_config: Arc<ModuleConfig>,
337 pub allocator_config: Arc<ModuleConfig>,
338 pub tm_factory: TargetMachineFactoryFn<B>,
339 pub msvc_imps_needed: bool,
340 pub is_pe_coff: bool,
341 pub target_can_use_split_dwarf: bool,
342 pub target_arch: String,
343 pub target_is_like_darwin: bool,
344 pub target_is_like_aix: bool,
345 pub target_is_like_gpu: bool,
346 pub split_debuginfo: rustc_target::spec::SplitDebuginfo,
347 pub split_dwarf_kind: rustc_session::config::SplitDwarfKind,
348 pub pointer_size: Size,
349
350 pub diag_emitter: SharedEmitter,
352 pub remark: Passes,
354 pub remark_dir: Option<PathBuf>,
357 pub incr_comp_session_dir: Option<PathBuf>,
360 pub parallel: bool,
364}
365
366impl<B: WriteBackendMethods> CodegenContext<B> {
367 pub fn create_dcx(&self) -> DiagCtxt {
368 DiagCtxt::new(Box::new(self.diag_emitter.clone()))
369 }
370}
371
372fn generate_thin_lto_work<B: ExtraBackendMethods>(
373 cgcx: &CodegenContext<B>,
374 exported_symbols_for_lto: &[String],
375 each_linked_rlib_for_lto: &[PathBuf],
376 needs_thin_lto: Vec<(String, B::ThinBuffer)>,
377 import_only_modules: Vec<(SerializedModule<B::ModuleBuffer>, WorkProduct)>,
378) -> Vec<(ThinLtoWorkItem<B>, u64)> {
379 let _prof_timer = cgcx.prof.generic_activity("codegen_thin_generate_lto_work");
380
381 let (lto_modules, copy_jobs) = B::run_thin_lto(
382 cgcx,
383 exported_symbols_for_lto,
384 each_linked_rlib_for_lto,
385 needs_thin_lto,
386 import_only_modules,
387 );
388 lto_modules
389 .into_iter()
390 .map(|module| {
391 let cost = module.cost();
392 (ThinLtoWorkItem::ThinLto(module), cost)
393 })
394 .chain(copy_jobs.into_iter().map(|wp| {
395 (
396 ThinLtoWorkItem::CopyPostLtoArtifacts(CachedModuleCodegen {
397 name: wp.cgu_name.clone(),
398 source: wp,
399 }),
400 0, )
402 }))
403 .collect()
404}
405
406struct CompiledModules {
407 modules: Vec<CompiledModule>,
408 allocator_module: Option<CompiledModule>,
409}
410
411fn need_bitcode_in_object(tcx: TyCtxt<'_>) -> bool {
412 let sess = tcx.sess;
413 sess.opts.cg.embed_bitcode
414 && tcx.crate_types().contains(&CrateType::Rlib)
415 && sess.opts.output_types.contains_key(&OutputType::Exe)
416}
417
418fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
419 if sess.opts.incremental.is_none() {
420 return false;
421 }
422
423 match sess.lto() {
424 Lto::No => false,
425 Lto::Fat | Lto::Thin | Lto::ThinLocal => true,
426 }
427}
428
429pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
430 backend: B,
431 tcx: TyCtxt<'_>,
432 target_cpu: String,
433 allocator_module: Option<ModuleCodegen<B::Module>>,
434) -> OngoingCodegen<B> {
435 let (coordinator_send, coordinator_receive) = channel();
436
437 let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
438 let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
439
440 let crate_info = CrateInfo::new(tcx, target_cpu);
441
442 let regular_config = ModuleConfig::new(ModuleKind::Regular, tcx, no_builtins);
443 let allocator_config = ModuleConfig::new(ModuleKind::Allocator, tcx, no_builtins);
444
445 let (shared_emitter, shared_emitter_main) = SharedEmitter::new();
446 let (codegen_worker_send, codegen_worker_receive) = channel();
447
448 let coordinator_thread = start_executing_work(
449 backend.clone(),
450 tcx,
451 &crate_info,
452 shared_emitter,
453 codegen_worker_send,
454 coordinator_receive,
455 Arc::new(regular_config),
456 Arc::new(allocator_config),
457 allocator_module,
458 coordinator_send.clone(),
459 );
460
461 OngoingCodegen {
462 backend,
463 crate_info,
464
465 codegen_worker_receive,
466 shared_emitter_main,
467 coordinator: Coordinator {
468 sender: coordinator_send,
469 future: Some(coordinator_thread),
470 phantom: PhantomData,
471 },
472 output_filenames: Arc::clone(tcx.output_filenames(())),
473 }
474}
475
476fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
477 sess: &Session,
478 compiled_modules: &CompiledModules,
479) -> FxIndexMap<WorkProductId, WorkProduct> {
480 let mut work_products = FxIndexMap::default();
481
482 if sess.opts.incremental.is_none() {
483 return work_products;
484 }
485
486 let _timer = sess.timer("copy_all_cgu_workproducts_to_incr_comp_cache_dir");
487
488 for module in compiled_modules.modules.iter().filter(|m| m.kind == ModuleKind::Regular) {
489 let mut files = Vec::new();
490 if let Some(object_file_path) = &module.object {
491 files.push((OutputType::Object.extension(), object_file_path.as_path()));
492 }
493 if let Some(dwarf_object_file_path) = &module.dwarf_object {
494 files.push(("dwo", dwarf_object_file_path.as_path()));
495 }
496 if let Some(path) = &module.assembly {
497 files.push((OutputType::Assembly.extension(), path.as_path()));
498 }
499 if let Some(path) = &module.llvm_ir {
500 files.push((OutputType::LlvmAssembly.extension(), path.as_path()));
501 }
502 if let Some(path) = &module.bytecode {
503 files.push((OutputType::Bitcode.extension(), path.as_path()));
504 }
505 if let Some((id, product)) = copy_cgu_workproduct_to_incr_comp_cache_dir(
506 sess,
507 &module.name,
508 files.as_slice(),
509 &module.links_from_incr_cache,
510 ) {
511 work_products.insert(id, product);
512 }
513 }
514
515 work_products
516}
517
518fn produce_final_output_artifacts(
519 sess: &Session,
520 compiled_modules: &CompiledModules,
521 crate_output: &OutputFilenames,
522) {
523 let mut user_wants_bitcode = false;
524 let mut user_wants_objects = false;
525
526 let copy_gracefully = |from: &Path, to: &OutFileName| match to {
528 OutFileName::Stdout if let Err(e) = copy_to_stdout(from) => {
529 sess.dcx().emit_err(errors::CopyPath::new(from, to.as_path(), e));
530 }
531 OutFileName::Real(path) if let Err(e) = fs::copy(from, path) => {
532 sess.dcx().emit_err(errors::CopyPath::new(from, path, e));
533 }
534 _ => {}
535 };
536
537 let copy_if_one_unit = |output_type: OutputType, keep_numbered: bool| {
538 if let [module] = &compiled_modules.modules[..] {
539 let path = crate_output.temp_path_for_cgu(
542 output_type,
543 &module.name,
544 sess.invocation_temp.as_deref(),
545 );
546 let output = crate_output.path(output_type);
547 if !output_type.is_text_output() && output.is_tty() {
548 sess.dcx()
549 .emit_err(errors::BinaryOutputToTty { shorthand: output_type.shorthand() });
550 } else {
551 copy_gracefully(&path, &output);
552 }
553 if !sess.opts.cg.save_temps && !keep_numbered {
554 ensure_removed(sess.dcx(), &path);
556 }
557 } else {
558 if crate_output.outputs.contains_explicit_name(&output_type) {
559 sess.dcx()
562 .emit_warn(errors::IgnoringEmitPath { extension: output_type.extension() });
563 } else if crate_output.single_output_file.is_some() {
564 sess.dcx().emit_warn(errors::IgnoringOutput { extension: output_type.extension() });
567 } else {
568 }
572 }
573 };
574
575 for output_type in crate_output.outputs.keys() {
579 match *output_type {
580 OutputType::Bitcode => {
581 user_wants_bitcode = true;
582 copy_if_one_unit(OutputType::Bitcode, true);
586 }
587 OutputType::ThinLinkBitcode => {
588 copy_if_one_unit(OutputType::ThinLinkBitcode, false);
589 }
590 OutputType::LlvmAssembly => {
591 copy_if_one_unit(OutputType::LlvmAssembly, false);
592 }
593 OutputType::Assembly => {
594 copy_if_one_unit(OutputType::Assembly, false);
595 }
596 OutputType::Object => {
597 user_wants_objects = true;
598 copy_if_one_unit(OutputType::Object, true);
599 }
600 OutputType::Mir | OutputType::Metadata | OutputType::Exe | OutputType::DepInfo => {}
601 }
602 }
603
604 if !sess.opts.cg.save_temps {
617 let needs_crate_object = crate_output.outputs.contains_key(&OutputType::Exe);
633
634 let keep_numbered_bitcode = user_wants_bitcode && sess.codegen_units().as_usize() > 1;
635
636 let keep_numbered_objects =
637 needs_crate_object || (user_wants_objects && sess.codegen_units().as_usize() > 1);
638
639 for module in compiled_modules.modules.iter() {
640 if !keep_numbered_objects {
641 if let Some(ref path) = module.object {
642 ensure_removed(sess.dcx(), path);
643 }
644
645 if let Some(ref path) = module.dwarf_object {
646 ensure_removed(sess.dcx(), path);
647 }
648 }
649
650 if let Some(ref path) = module.bytecode {
651 if !keep_numbered_bitcode {
652 ensure_removed(sess.dcx(), path);
653 }
654 }
655 }
656
657 if !user_wants_bitcode
658 && let Some(ref allocator_module) = compiled_modules.allocator_module
659 && let Some(ref path) = allocator_module.bytecode
660 {
661 ensure_removed(sess.dcx(), path);
662 }
663 }
664
665 if sess.opts.json_artifact_notifications {
666 if let [module] = &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 let path = crate_output.path(ty);
673 sess.dcx().emit_artifact_notification(path.as_path(), descr);
674 }
675 });
676 } else {
677 for module in &compiled_modules.modules {
678 module.for_each_output(|path, ty| {
679 if sess.opts.output_types.contains_key(&ty) {
680 let descr = ty.shorthand();
681 sess.dcx().emit_artifact_notification(&path, descr);
682 }
683 });
684 }
685 }
686 }
687
688 }
694
695pub(crate) enum WorkItem<B: WriteBackendMethods> {
696 Optimize(ModuleCodegen<B::Module>),
698 CopyPostLtoArtifacts(CachedModuleCodegen),
701}
702
703enum ThinLtoWorkItem<B: WriteBackendMethods> {
704 CopyPostLtoArtifacts(CachedModuleCodegen),
707 ThinLto(lto::ThinModule<B>),
709}
710
711#[cfg(not(windows))]
715fn desc(short: &str, _long: &str, name: &str) -> String {
716 assert_eq!(short.len(), 3);
736 let name = if let Some(index) = name.find("-cgu.") {
737 &name[index + 1..] } else {
739 name
740 };
741 format!("{short} {name}")
742}
743
744#[cfg(windows)]
746fn desc(_short: &str, long: &str, name: &str) -> String {
747 format!("{long} {name}")
748}
749
750impl<B: WriteBackendMethods> WorkItem<B> {
751 fn short_description(&self) -> String {
753 match self {
754 WorkItem::Optimize(m) => desc("opt", "optimize module", &m.name),
755 WorkItem::CopyPostLtoArtifacts(m) => desc("cpy", "copy LTO artifacts for", &m.name),
756 }
757 }
758}
759
760impl<B: WriteBackendMethods> ThinLtoWorkItem<B> {
761 fn short_description(&self) -> String {
763 match self {
764 ThinLtoWorkItem::CopyPostLtoArtifacts(m) => {
765 desc("cpy", "copy LTO artifacts for", &m.name)
766 }
767 ThinLtoWorkItem::ThinLto(m) => desc("lto", "thin-LTO module", m.name()),
768 }
769 }
770}
771
772pub(crate) enum WorkItemResult<B: WriteBackendMethods> {
774 Finished(CompiledModule),
776
777 NeedsFatLto(FatLtoInput<B>),
780
781 NeedsThinLto(String, B::ThinBuffer),
784}
785
786pub enum FatLtoInput<B: WriteBackendMethods> {
787 Serialized { name: String, buffer: SerializedModule<B::ModuleBuffer> },
788 InMemory(ModuleCodegen<B::Module>),
789}
790
791pub(crate) enum ComputedLtoType {
793 No,
794 Thin,
795 Fat,
796}
797
798pub(crate) fn compute_per_cgu_lto_type(
799 sess_lto: &Lto,
800 opts: &config::Options,
801 sess_crate_types: &[CrateType],
802 module_kind: ModuleKind,
803) -> ComputedLtoType {
804 let linker_does_lto = opts.cg.linker_plugin_lto.enabled();
808
809 let is_allocator = module_kind == ModuleKind::Allocator;
814
815 let is_rlib = matches!(sess_crate_types, [CrateType::Rlib]);
824
825 match sess_lto {
826 Lto::ThinLocal if !linker_does_lto && !is_allocator => ComputedLtoType::Thin,
827 Lto::Thin if !linker_does_lto && !is_rlib => ComputedLtoType::Thin,
828 Lto::Fat if !is_rlib => ComputedLtoType::Fat,
829 _ => ComputedLtoType::No,
830 }
831}
832
833fn execute_optimize_work_item<B: ExtraBackendMethods>(
834 cgcx: &CodegenContext<B>,
835 mut module: ModuleCodegen<B::Module>,
836) -> WorkItemResult<B> {
837 let _timer = cgcx.prof.generic_activity_with_arg("codegen_module_optimize", &*module.name);
838
839 let dcx = cgcx.create_dcx();
840 let dcx = dcx.handle();
841
842 let module_config = match module.kind {
843 ModuleKind::Regular => &cgcx.module_config,
844 ModuleKind::Allocator => &cgcx.allocator_config,
845 };
846
847 B::optimize(cgcx, dcx, &mut module, module_config);
848
849 let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types, module.kind);
855
856 let bitcode = if module_config.emit_pre_lto_bc {
859 let filename = pre_lto_bitcode_filename(&module.name);
860 cgcx.incr_comp_session_dir.as_ref().map(|path| path.join(&filename))
861 } else {
862 None
863 };
864
865 match lto_type {
866 ComputedLtoType::No => {
867 let module = B::codegen(cgcx, module, module_config);
868 WorkItemResult::Finished(module)
869 }
870 ComputedLtoType::Thin => {
871 let (name, thin_buffer) = B::prepare_thin(module);
872 if let Some(path) = bitcode {
873 fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| {
874 panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);
875 });
876 }
877 WorkItemResult::NeedsThinLto(name, thin_buffer)
878 }
879 ComputedLtoType::Fat => match bitcode {
880 Some(path) => {
881 let (name, buffer) = B::serialize_module(module);
882 fs::write(&path, buffer.data()).unwrap_or_else(|e| {
883 panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);
884 });
885 WorkItemResult::NeedsFatLto(FatLtoInput::Serialized {
886 name,
887 buffer: SerializedModule::Local(buffer),
888 })
889 }
890 None => WorkItemResult::NeedsFatLto(FatLtoInput::InMemory(module)),
891 },
892 }
893}
894
895fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
896 cgcx: &CodegenContext<B>,
897 module: CachedModuleCodegen,
898) -> CompiledModule {
899 let _timer = cgcx
900 .prof
901 .generic_activity_with_arg("codegen_copy_artifacts_from_incr_cache", &*module.name);
902
903 let incr_comp_session_dir = cgcx.incr_comp_session_dir.as_ref().unwrap();
904
905 let mut links_from_incr_cache = Vec::new();
906
907 let mut load_from_incr_comp_dir = |output_path: PathBuf, saved_path: &str| {
908 let source_file = in_incr_comp_dir(incr_comp_session_dir, saved_path);
909 debug!(
910 "copying preexisting module `{}` from {:?} to {}",
911 module.name,
912 source_file,
913 output_path.display()
914 );
915 match link_or_copy(&source_file, &output_path) {
916 Ok(_) => {
917 links_from_incr_cache.push(source_file);
918 Some(output_path)
919 }
920 Err(error) => {
921 cgcx.create_dcx().handle().emit_err(errors::CopyPathBuf {
922 source_file,
923 output_path,
924 error,
925 });
926 None
927 }
928 }
929 };
930
931 let dwarf_object =
932 module.source.saved_files.get("dwo").as_ref().and_then(|saved_dwarf_object_file| {
933 let dwarf_obj_out = cgcx
934 .output_filenames
935 .split_dwarf_path(
936 cgcx.split_debuginfo,
937 cgcx.split_dwarf_kind,
938 &module.name,
939 cgcx.invocation_temp.as_deref(),
940 )
941 .expect(
942 "saved dwarf object in work product but `split_dwarf_path` returned `None`",
943 );
944 load_from_incr_comp_dir(dwarf_obj_out, saved_dwarf_object_file)
945 });
946
947 let mut load_from_incr_cache = |perform, output_type: OutputType| {
948 if perform {
949 let saved_file = module.source.saved_files.get(output_type.extension())?;
950 let output_path = cgcx.output_filenames.temp_path_for_cgu(
951 output_type,
952 &module.name,
953 cgcx.invocation_temp.as_deref(),
954 );
955 load_from_incr_comp_dir(output_path, &saved_file)
956 } else {
957 None
958 }
959 };
960
961 let module_config = &cgcx.module_config;
962 let should_emit_obj = module_config.emit_obj != EmitObj::None;
963 let assembly = load_from_incr_cache(module_config.emit_asm, OutputType::Assembly);
964 let llvm_ir = load_from_incr_cache(module_config.emit_ir, OutputType::LlvmAssembly);
965 let bytecode = load_from_incr_cache(module_config.emit_bc, OutputType::Bitcode);
966 let object = load_from_incr_cache(should_emit_obj, OutputType::Object);
967 if should_emit_obj && object.is_none() {
968 cgcx.create_dcx().handle().emit_fatal(errors::NoSavedObjectFile { cgu_name: &module.name })
969 }
970
971 CompiledModule {
972 links_from_incr_cache,
973 kind: ModuleKind::Regular,
974 name: module.name,
975 object,
976 dwarf_object,
977 bytecode,
978 assembly,
979 llvm_ir,
980 }
981}
982
983fn do_fat_lto<B: ExtraBackendMethods>(
984 cgcx: &CodegenContext<B>,
985 exported_symbols_for_lto: &[String],
986 each_linked_rlib_for_lto: &[PathBuf],
987 mut needs_fat_lto: Vec<FatLtoInput<B>>,
988 import_only_modules: Vec<(SerializedModule<B::ModuleBuffer>, WorkProduct)>,
989) -> CompiledModule {
990 let _timer = cgcx.prof.verbose_generic_activity("LLVM_fatlto");
991
992 check_lto_allowed(&cgcx);
993
994 for (module, wp) in import_only_modules {
995 needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, buffer: module })
996 }
997
998 let module = B::run_and_optimize_fat_lto(
999 cgcx,
1000 exported_symbols_for_lto,
1001 each_linked_rlib_for_lto,
1002 needs_fat_lto,
1003 );
1004 B::codegen(cgcx, module, &cgcx.module_config)
1005}
1006
1007fn do_thin_lto<'a, B: ExtraBackendMethods>(
1008 cgcx: &'a CodegenContext<B>,
1009 exported_symbols_for_lto: Arc<Vec<String>>,
1010 each_linked_rlib_for_lto: Vec<PathBuf>,
1011 needs_thin_lto: Vec<(String, <B as WriteBackendMethods>::ThinBuffer)>,
1012 lto_import_only_modules: Vec<(
1013 SerializedModule<<B as WriteBackendMethods>::ModuleBuffer>,
1014 WorkProduct,
1015 )>,
1016) -> Vec<CompiledModule> {
1017 let _timer = cgcx.prof.verbose_generic_activity("LLVM_thinlto");
1018
1019 check_lto_allowed(&cgcx);
1020
1021 let (coordinator_send, coordinator_receive) = channel();
1022
1023 let coordinator_send2 = coordinator_send.clone();
1029 let helper = jobserver::client()
1030 .into_helper_thread(move |token| {
1031 drop(coordinator_send2.send(ThinLtoMessage::Token(token)));
1032 })
1033 .expect("failed to spawn helper thread");
1034
1035 let mut work_items = vec![];
1036
1037 for (work, cost) in generate_thin_lto_work(
1043 cgcx,
1044 &exported_symbols_for_lto,
1045 &each_linked_rlib_for_lto,
1046 needs_thin_lto,
1047 lto_import_only_modules,
1048 ) {
1049 let insertion_index =
1050 work_items.binary_search_by_key(&cost, |&(_, cost)| cost).unwrap_or_else(|e| e);
1051 work_items.insert(insertion_index, (work, cost));
1052 if cgcx.parallel {
1053 helper.request_token();
1054 }
1055 }
1056
1057 let mut codegen_aborted = None;
1058
1059 let mut tokens = vec![];
1062
1063 let mut used_token_count = 0;
1065
1066 let mut compiled_modules = vec![];
1067
1068 loop {
1074 if codegen_aborted.is_none() {
1075 if used_token_count == 0 && work_items.is_empty() {
1076 break;
1078 }
1079
1080 while used_token_count < tokens.len() + 1
1083 && let Some((item, _)) = work_items.pop()
1084 {
1085 spawn_thin_lto_work(&cgcx, coordinator_send.clone(), item);
1086 used_token_count += 1;
1087 }
1088 } else {
1089 if used_token_count == 0 {
1092 break;
1093 }
1094 }
1095
1096 tokens.truncate(used_token_count.saturating_sub(1));
1098
1099 match coordinator_receive.recv().unwrap() {
1100 ThinLtoMessage::Token(token) => match token {
1104 Ok(token) => {
1105 tokens.push(token);
1106 }
1107 Err(e) => {
1108 let msg = &format!("failed to acquire jobserver token: {e}");
1109 cgcx.diag_emitter.fatal(msg);
1110 codegen_aborted = Some(FatalError);
1111 }
1112 },
1113
1114 ThinLtoMessage::WorkItem { result } => {
1115 used_token_count -= 1;
1121
1122 match result {
1123 Ok(compiled_module) => compiled_modules.push(compiled_module),
1124 Err(Some(WorkerFatalError)) => {
1125 codegen_aborted = Some(FatalError);
1127 }
1128 Err(None) => {
1129 bug!("worker thread panicked");
1132 }
1133 }
1134 }
1135 }
1136 }
1137
1138 if let Some(codegen_aborted) = codegen_aborted {
1139 codegen_aborted.raise();
1140 }
1141
1142 compiled_modules
1143}
1144
1145fn execute_thin_lto_work_item<B: ExtraBackendMethods>(
1146 cgcx: &CodegenContext<B>,
1147 module: lto::ThinModule<B>,
1148) -> CompiledModule {
1149 let _timer = cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", module.name());
1150
1151 let module = B::optimize_thin(cgcx, module);
1152 B::codegen(cgcx, module, &cgcx.module_config)
1153}
1154
1155pub(crate) enum Message<B: WriteBackendMethods> {
1157 Token(io::Result<Acquired>),
1160
1161 WorkItem { result: Result<WorkItemResult<B>, Option<WorkerFatalError>> },
1164
1165 CodegenDone { llvm_work_item: WorkItem<B>, cost: u64 },
1169
1170 AddImportOnlyModule {
1173 module_data: SerializedModule<B::ModuleBuffer>,
1174 work_product: WorkProduct,
1175 },
1176
1177 CodegenComplete,
1180
1181 CodegenAborted,
1184}
1185
1186pub(crate) enum ThinLtoMessage {
1188 Token(io::Result<Acquired>),
1191
1192 WorkItem { result: Result<CompiledModule, Option<WorkerFatalError>> },
1195}
1196
1197pub struct CguMessage;
1200
1201struct Diagnostic {
1211 span: Vec<SpanData>,
1212 level: Level,
1213 messages: Vec<(DiagMessage, Style)>,
1214 code: Option<ErrCode>,
1215 children: Vec<Subdiagnostic>,
1216 args: DiagArgMap,
1217}
1218
1219struct Subdiagnostic {
1223 level: Level,
1224 messages: Vec<(DiagMessage, Style)>,
1225}
1226
1227#[derive(PartialEq, Clone, Copy, Debug)]
1228enum MainThreadState {
1229 Idle,
1231
1232 Codegenning,
1234
1235 Lending,
1237}
1238
1239fn start_executing_work<B: ExtraBackendMethods>(
1240 backend: B,
1241 tcx: TyCtxt<'_>,
1242 crate_info: &CrateInfo,
1243 shared_emitter: SharedEmitter,
1244 codegen_worker_send: Sender<CguMessage>,
1245 coordinator_receive: Receiver<Message<B>>,
1246 regular_config: Arc<ModuleConfig>,
1247 allocator_config: Arc<ModuleConfig>,
1248 allocator_module: Option<ModuleCodegen<B::Module>>,
1249 coordinator_send: Sender<Message<B>>,
1250) -> thread::JoinHandle<Result<CompiledModules, ()>> {
1251 let sess = tcx.sess;
1252
1253 let mut each_linked_rlib_for_lto = Vec::new();
1254 let mut each_linked_rlib_file_for_lto = Vec::new();
1255 drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
1256 if link::ignored_for_lto(sess, crate_info, cnum) {
1257 return;
1258 }
1259 each_linked_rlib_for_lto.push(cnum);
1260 each_linked_rlib_file_for_lto.push(path.to_path_buf());
1261 }));
1262
1263 let exported_symbols_for_lto =
1265 Arc::new(lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto));
1266
1267 let coordinator_send2 = coordinator_send.clone();
1273 let helper = jobserver::client()
1274 .into_helper_thread(move |token| {
1275 drop(coordinator_send2.send(Message::Token::<B>(token)));
1276 })
1277 .expect("failed to spawn helper thread");
1278
1279 let ol = tcx.backend_optimization_level(());
1280 let backend_features = tcx.global_backend_features(());
1281
1282 let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {
1283 let result = fs::create_dir_all(dir).and_then(|_| dir.canonicalize());
1284 match result {
1285 Ok(dir) => Some(dir),
1286 Err(error) => sess.dcx().emit_fatal(ErrorCreatingRemarkDir { error }),
1287 }
1288 } else {
1289 None
1290 };
1291
1292 let cgcx = CodegenContext::<B> {
1293 crate_types: tcx.crate_types().to_vec(),
1294 lto: sess.lto(),
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 opts: Arc::new(sess.opts.clone()),
1299 prof: sess.prof.clone(),
1300 remark: sess.opts.cg.remark.clone(),
1301 remark_dir,
1302 incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),
1303 diag_emitter: shared_emitter.clone(),
1304 output_filenames: Arc::clone(tcx.output_filenames(())),
1305 module_config: regular_config,
1306 allocator_config,
1307 tm_factory: backend.target_machine_factory(tcx.sess, ol, backend_features),
1308 msvc_imps_needed: msvc_imps_needed(tcx),
1309 is_pe_coff: tcx.sess.target.is_like_windows,
1310 target_can_use_split_dwarf: tcx.sess.target_can_use_split_dwarf(),
1311 target_arch: tcx.sess.target.arch.to_string(),
1312 target_is_like_darwin: tcx.sess.target.is_like_darwin,
1313 target_is_like_aix: tcx.sess.target.is_like_aix,
1314 target_is_like_gpu: tcx.sess.target.is_like_gpu,
1315 split_debuginfo: tcx.sess.split_debuginfo(),
1316 split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind,
1317 parallel: backend.supports_parallel() && !sess.opts.unstable_opts.no_parallel_backend,
1318 pointer_size: tcx.data_layout.pointer_size(),
1319 invocation_temp: sess.invocation_temp.clone(),
1320 };
1321
1322 return B::spawn_named_thread(cgcx.time_trace, "coordinator".to_string(), move || {
1458 let mut compiled_modules = vec![];
1461 let mut needs_fat_lto = Vec::new();
1462 let mut needs_thin_lto = Vec::new();
1463 let mut lto_import_only_modules = Vec::new();
1464
1465 #[derive(Debug, PartialEq)]
1470 enum CodegenState {
1471 Ongoing,
1472 Completed,
1473 Aborted,
1474 }
1475 use CodegenState::*;
1476 let mut codegen_state = Ongoing;
1477
1478 let mut work_items = Vec::<(WorkItem<B>, u64)>::new();
1480
1481 let mut tokens = Vec::new();
1484
1485 let mut main_thread_state = MainThreadState::Idle;
1486
1487 let mut running_with_own_token = 0;
1490
1491 let running_with_any_token = |main_thread_state, running_with_own_token| {
1494 running_with_own_token
1495 + if main_thread_state == MainThreadState::Lending { 1 } else { 0 }
1496 };
1497
1498 let mut llvm_start_time: Option<VerboseTimingGuard<'_>> = None;
1499
1500 let compiled_allocator_module = allocator_module.and_then(|allocator_module| {
1501 match execute_optimize_work_item(&cgcx, allocator_module) {
1502 WorkItemResult::Finished(compiled_module) => return Some(compiled_module),
1503 WorkItemResult::NeedsFatLto(fat_lto_input) => needs_fat_lto.push(fat_lto_input),
1504 WorkItemResult::NeedsThinLto(name, thin_buffer) => {
1505 needs_thin_lto.push((name, thin_buffer))
1506 }
1507 }
1508 None
1509 });
1510
1511 loop {
1517 if codegen_state == Ongoing {
1521 if main_thread_state == MainThreadState::Idle {
1522 let extra_tokens = tokens.len().checked_sub(running_with_own_token).unwrap();
1530 let additional_running = std::cmp::min(extra_tokens, work_items.len());
1531 let anticipated_running = running_with_own_token + additional_running + 1;
1532
1533 if !queue_full_enough(work_items.len(), anticipated_running) {
1534 if codegen_worker_send.send(CguMessage).is_err() {
1536 panic!("Could not send CguMessage to main thread")
1537 }
1538 main_thread_state = MainThreadState::Codegenning;
1539 } else {
1540 let (item, _) =
1544 work_items.pop().expect("queue empty - queue_full_enough() broken?");
1545 main_thread_state = MainThreadState::Lending;
1546 spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
1547 }
1548 }
1549 } else if codegen_state == Completed {
1550 if running_with_any_token(main_thread_state, running_with_own_token) == 0
1551 && work_items.is_empty()
1552 {
1553 break;
1555 }
1556
1557 match main_thread_state {
1561 MainThreadState::Idle => {
1562 if let Some((item, _)) = work_items.pop() {
1563 main_thread_state = MainThreadState::Lending;
1564 spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
1565 } else {
1566 assert!(running_with_own_token > 0);
1573 running_with_own_token -= 1;
1574 main_thread_state = MainThreadState::Lending;
1575 }
1576 }
1577 MainThreadState::Codegenning => bug!(
1578 "codegen worker should not be codegenning after \
1579 codegen was already completed"
1580 ),
1581 MainThreadState::Lending => {
1582 }
1584 }
1585 } else {
1586 assert!(codegen_state == Aborted);
1589 if running_with_any_token(main_thread_state, running_with_own_token) == 0 {
1590 break;
1591 }
1592 }
1593
1594 if codegen_state != Aborted {
1597 while running_with_own_token < tokens.len()
1598 && let Some((item, _)) = work_items.pop()
1599 {
1600 spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
1601 running_with_own_token += 1;
1602 }
1603 }
1604
1605 tokens.truncate(running_with_own_token);
1607
1608 match coordinator_receive.recv().unwrap() {
1609 Message::Token(token) => {
1613 match token {
1614 Ok(token) => {
1615 tokens.push(token);
1616
1617 if main_thread_state == MainThreadState::Lending {
1618 main_thread_state = MainThreadState::Idle;
1623 running_with_own_token += 1;
1624 }
1625 }
1626 Err(e) => {
1627 let msg = &format!("failed to acquire jobserver token: {e}");
1628 shared_emitter.fatal(msg);
1629 codegen_state = Aborted;
1630 }
1631 }
1632 }
1633
1634 Message::CodegenDone { llvm_work_item, cost } => {
1635 let insertion_index = work_items.binary_search_by_key(&cost, |&(_, cost)| cost);
1644 let insertion_index = match insertion_index {
1645 Ok(idx) | Err(idx) => idx,
1646 };
1647 work_items.insert(insertion_index, (llvm_work_item, cost));
1648
1649 if cgcx.parallel {
1650 helper.request_token();
1651 }
1652 assert_eq!(main_thread_state, MainThreadState::Codegenning);
1653 main_thread_state = MainThreadState::Idle;
1654 }
1655
1656 Message::CodegenComplete => {
1657 if codegen_state != Aborted {
1658 codegen_state = Completed;
1659 }
1660 assert_eq!(main_thread_state, MainThreadState::Codegenning);
1661 main_thread_state = MainThreadState::Idle;
1662 }
1663
1664 Message::CodegenAborted => {
1672 codegen_state = Aborted;
1673 }
1674
1675 Message::WorkItem { result } => {
1676 if main_thread_state == MainThreadState::Lending {
1682 main_thread_state = MainThreadState::Idle;
1683 } else {
1684 running_with_own_token -= 1;
1685 }
1686
1687 match result {
1688 Ok(WorkItemResult::Finished(compiled_module)) => {
1689 compiled_modules.push(compiled_module);
1690 }
1691 Ok(WorkItemResult::NeedsFatLto(fat_lto_input)) => {
1692 assert!(needs_thin_lto.is_empty());
1693 needs_fat_lto.push(fat_lto_input);
1694 }
1695 Ok(WorkItemResult::NeedsThinLto(name, thin_buffer)) => {
1696 assert!(needs_fat_lto.is_empty());
1697 needs_thin_lto.push((name, thin_buffer));
1698 }
1699 Err(Some(WorkerFatalError)) => {
1700 codegen_state = Aborted;
1702 }
1703 Err(None) => {
1704 bug!("worker thread panicked");
1707 }
1708 }
1709 }
1710
1711 Message::AddImportOnlyModule { module_data, work_product } => {
1712 assert_eq!(codegen_state, Ongoing);
1713 assert_eq!(main_thread_state, MainThreadState::Codegenning);
1714 lto_import_only_modules.push((module_data, work_product));
1715 main_thread_state = MainThreadState::Idle;
1716 }
1717 }
1718 }
1719
1720 drop(llvm_start_time);
1722
1723 if codegen_state == Aborted {
1724 return Err(());
1725 }
1726
1727 drop(codegen_state);
1728 drop(tokens);
1729 drop(helper);
1730 assert!(work_items.is_empty());
1731
1732 if !needs_fat_lto.is_empty() {
1733 assert!(compiled_modules.is_empty());
1734 assert!(needs_thin_lto.is_empty());
1735
1736 let module = do_fat_lto(
1738 &cgcx,
1739 &exported_symbols_for_lto,
1740 &each_linked_rlib_file_for_lto,
1741 needs_fat_lto,
1742 lto_import_only_modules,
1743 );
1744 compiled_modules.push(module);
1745 } else if !needs_thin_lto.is_empty() || !lto_import_only_modules.is_empty() {
1746 assert!(compiled_modules.is_empty());
1747 assert!(needs_fat_lto.is_empty());
1748
1749 compiled_modules.extend(do_thin_lto(
1750 &cgcx,
1751 exported_symbols_for_lto,
1752 each_linked_rlib_file_for_lto,
1753 needs_thin_lto,
1754 lto_import_only_modules,
1755 ));
1756 }
1757
1758 compiled_modules.sort_by(|a, b| a.name.cmp(&b.name));
1762
1763 Ok(CompiledModules {
1764 modules: compiled_modules,
1765 allocator_module: compiled_allocator_module,
1766 })
1767 })
1768 .expect("failed to spawn coordinator thread");
1769
1770 fn queue_full_enough(items_in_queue: usize, workers_running: usize) -> bool {
1773 let quarter_of_workers = workers_running - 3 * workers_running / 4;
1824 items_in_queue > 0 && items_in_queue >= quarter_of_workers
1825 }
1826}
1827
1828#[must_use]
1830pub(crate) struct WorkerFatalError;
1831
1832fn spawn_work<'a, B: ExtraBackendMethods>(
1833 cgcx: &'a CodegenContext<B>,
1834 coordinator_send: Sender<Message<B>>,
1835 llvm_start_time: &mut Option<VerboseTimingGuard<'a>>,
1836 work: WorkItem<B>,
1837) {
1838 if llvm_start_time.is_none() {
1839 *llvm_start_time = Some(cgcx.prof.verbose_generic_activity("LLVM_passes"));
1840 }
1841
1842 let cgcx = cgcx.clone();
1843
1844 B::spawn_named_thread(cgcx.time_trace, work.short_description(), move || {
1845 let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work {
1846 WorkItem::Optimize(m) => execute_optimize_work_item(&cgcx, m),
1847 WorkItem::CopyPostLtoArtifacts(m) => {
1848 WorkItemResult::Finished(execute_copy_from_cache_work_item(&cgcx, m))
1849 }
1850 }));
1851
1852 let msg = match result {
1853 Ok(result) => Message::WorkItem::<B> { result: Ok(result) },
1854
1855 Err(err) if err.is::<FatalErrorMarker>() => {
1859 Message::WorkItem::<B> { result: Err(Some(WorkerFatalError)) }
1860 }
1861
1862 Err(_) => Message::WorkItem::<B> { result: Err(None) },
1863 };
1864 drop(coordinator_send.send(msg));
1865 })
1866 .expect("failed to spawn work thread");
1867}
1868
1869fn spawn_thin_lto_work<'a, B: ExtraBackendMethods>(
1870 cgcx: &'a CodegenContext<B>,
1871 coordinator_send: Sender<ThinLtoMessage>,
1872 work: ThinLtoWorkItem<B>,
1873) {
1874 let cgcx = cgcx.clone();
1875
1876 B::spawn_named_thread(cgcx.time_trace, work.short_description(), move || {
1877 let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work {
1878 ThinLtoWorkItem::CopyPostLtoArtifacts(m) => execute_copy_from_cache_work_item(&cgcx, m),
1879 ThinLtoWorkItem::ThinLto(m) => execute_thin_lto_work_item(&cgcx, m),
1880 }));
1881
1882 let msg = match result {
1883 Ok(result) => ThinLtoMessage::WorkItem { result: Ok(result) },
1884
1885 Err(err) if err.is::<FatalErrorMarker>() => {
1889 ThinLtoMessage::WorkItem { result: Err(Some(WorkerFatalError)) }
1890 }
1891
1892 Err(_) => ThinLtoMessage::WorkItem { result: Err(None) },
1893 };
1894 drop(coordinator_send.send(msg));
1895 })
1896 .expect("failed to spawn work thread");
1897}
1898
1899enum SharedEmitterMessage {
1900 Diagnostic(Diagnostic),
1901 InlineAsmError(InlineAsmError),
1902 Fatal(String),
1903}
1904
1905pub struct InlineAsmError {
1906 pub span: SpanData,
1907 pub msg: String,
1908 pub level: Level,
1909 pub source: Option<(String, Vec<InnerSpan>)>,
1910}
1911
1912#[derive(Clone)]
1913pub struct SharedEmitter {
1914 sender: Sender<SharedEmitterMessage>,
1915}
1916
1917pub struct SharedEmitterMain {
1918 receiver: Receiver<SharedEmitterMessage>,
1919}
1920
1921impl SharedEmitter {
1922 fn new() -> (SharedEmitter, SharedEmitterMain) {
1923 let (sender, receiver) = channel();
1924
1925 (SharedEmitter { sender }, SharedEmitterMain { receiver })
1926 }
1927
1928 pub fn inline_asm_error(&self, err: InlineAsmError) {
1929 drop(self.sender.send(SharedEmitterMessage::InlineAsmError(err)));
1930 }
1931
1932 fn fatal(&self, msg: &str) {
1933 drop(self.sender.send(SharedEmitterMessage::Fatal(msg.to_string())));
1934 }
1935}
1936
1937impl Emitter for SharedEmitter {
1938 fn emit_diagnostic(
1939 &mut self,
1940 mut diag: rustc_errors::DiagInner,
1941 _registry: &rustc_errors::registry::Registry,
1942 ) {
1943 assert!(!diag.span.has_span_labels());
1946 assert_eq!(diag.suggestions, Suggestions::Enabled(vec![]));
1947 assert_eq!(diag.sort_span, rustc_span::DUMMY_SP);
1948 assert_eq!(diag.is_lint, None);
1949 let args = mem::replace(&mut diag.args, DiagArgMap::default());
1952 drop(
1953 self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
1954 span: diag.span.primary_spans().iter().map(|span| span.data()).collect::<Vec<_>>(),
1955 level: diag.level(),
1956 messages: diag.messages,
1957 code: diag.code,
1958 children: diag
1959 .children
1960 .into_iter()
1961 .map(|child| Subdiagnostic { level: child.level, messages: child.messages })
1962 .collect(),
1963 args,
1964 })),
1965 );
1966 }
1967
1968 fn source_map(&self) -> Option<&SourceMap> {
1969 None
1970 }
1971
1972 fn translator(&self) -> &Translator {
1973 panic!("shared emitter attempted to translate a diagnostic");
1974 }
1975}
1976
1977impl SharedEmitterMain {
1978 fn check(&self, sess: &Session, blocking: bool) {
1979 loop {
1980 let message = if blocking {
1981 match self.receiver.recv() {
1982 Ok(message) => Ok(message),
1983 Err(_) => Err(()),
1984 }
1985 } else {
1986 match self.receiver.try_recv() {
1987 Ok(message) => Ok(message),
1988 Err(_) => Err(()),
1989 }
1990 };
1991
1992 match message {
1993 Ok(SharedEmitterMessage::Diagnostic(diag)) => {
1994 let dcx = sess.dcx();
1997 let mut d =
1998 rustc_errors::DiagInner::new_with_messages(diag.level, diag.messages);
1999 d.span = MultiSpan::from_spans(
2000 diag.span.into_iter().map(|span| span.span()).collect(),
2001 );
2002 d.code = diag.code; d.children = diag
2004 .children
2005 .into_iter()
2006 .map(|sub| rustc_errors::Subdiag {
2007 level: sub.level,
2008 messages: sub.messages,
2009 span: MultiSpan::new(),
2010 })
2011 .collect();
2012 d.args = diag.args;
2013 dcx.emit_diagnostic(d);
2014 sess.dcx().abort_if_errors();
2015 }
2016 Ok(SharedEmitterMessage::InlineAsmError(inner)) => {
2017 assert_matches!(inner.level, Level::Error | Level::Warning | Level::Note);
2018 let mut err = Diag::<()>::new(sess.dcx(), inner.level, inner.msg);
2019 if !inner.span.is_dummy() {
2020 err.span(inner.span.span());
2021 }
2022
2023 if let Some((buffer, spans)) = inner.source {
2025 let source = sess
2026 .source_map()
2027 .new_source_file(FileName::inline_asm_source_code(&buffer), buffer);
2028 let spans: Vec<_> = spans
2029 .iter()
2030 .map(|sp| {
2031 Span::with_root_ctxt(
2032 source.normalized_byte_pos(sp.start as u32),
2033 source.normalized_byte_pos(sp.end as u32),
2034 )
2035 })
2036 .collect();
2037 err.span_note(spans, "instantiated into assembly here");
2038 }
2039
2040 err.emit();
2041 }
2042 Ok(SharedEmitterMessage::Fatal(msg)) => {
2043 sess.dcx().fatal(msg);
2044 }
2045 Err(_) => {
2046 break;
2047 }
2048 }
2049 }
2050 }
2051}
2052
2053pub struct Coordinator<B: ExtraBackendMethods> {
2054 sender: Sender<Message<B>>,
2055 future: Option<thread::JoinHandle<Result<CompiledModules, ()>>>,
2056 phantom: PhantomData<B>,
2058}
2059
2060impl<B: ExtraBackendMethods> Coordinator<B> {
2061 fn join(mut self) -> std::thread::Result<Result<CompiledModules, ()>> {
2062 self.future.take().unwrap().join()
2063 }
2064}
2065
2066impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
2067 fn drop(&mut self) {
2068 if let Some(future) = self.future.take() {
2069 drop(self.sender.send(Message::CodegenAborted::<B>));
2072 drop(future.join());
2073 }
2074 }
2075}
2076
2077pub struct OngoingCodegen<B: ExtraBackendMethods> {
2078 pub backend: B,
2079 pub crate_info: CrateInfo,
2080 pub output_filenames: Arc<OutputFilenames>,
2081 pub coordinator: Coordinator<B>,
2085 pub codegen_worker_receive: Receiver<CguMessage>,
2086 pub shared_emitter_main: SharedEmitterMain,
2087}
2088
2089impl<B: ExtraBackendMethods> OngoingCodegen<B> {
2090 pub fn join(self, sess: &Session) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
2091 self.shared_emitter_main.check(sess, true);
2092 let compiled_modules = sess.time("join_worker_thread", || match self.coordinator.join() {
2093 Ok(Ok(compiled_modules)) => compiled_modules,
2094 Ok(Err(())) => {
2095 sess.dcx().abort_if_errors();
2096 panic!("expected abort due to worker thread errors")
2097 }
2098 Err(_) => {
2099 bug!("panic during codegen/LLVM phase");
2100 }
2101 });
2102
2103 sess.dcx().abort_if_errors();
2104
2105 let work_products =
2106 copy_all_cgu_workproducts_to_incr_comp_cache_dir(sess, &compiled_modules);
2107 produce_final_output_artifacts(sess, &compiled_modules, &self.output_filenames);
2108
2109 if sess.codegen_units().as_usize() == 1 && sess.opts.unstable_opts.time_llvm_passes {
2112 self.backend.print_pass_timings()
2113 }
2114
2115 if sess.print_llvm_stats() {
2116 self.backend.print_statistics()
2117 }
2118
2119 (
2120 CodegenResults {
2121 crate_info: self.crate_info,
2122
2123 modules: compiled_modules.modules,
2124 allocator_module: compiled_modules.allocator_module,
2125 },
2126 work_products,
2127 )
2128 }
2129
2130 pub(crate) fn codegen_finished(&self, tcx: TyCtxt<'_>) {
2131 self.wait_for_signal_to_codegen_item();
2132 self.check_for_errors(tcx.sess);
2133 drop(self.coordinator.sender.send(Message::CodegenComplete::<B>));
2134 }
2135
2136 pub(crate) fn check_for_errors(&self, sess: &Session) {
2137 self.shared_emitter_main.check(sess, false);
2138 }
2139
2140 pub(crate) fn wait_for_signal_to_codegen_item(&self) {
2141 match self.codegen_worker_receive.recv() {
2142 Ok(CguMessage) => {
2143 }
2145 Err(_) => {
2146 }
2149 }
2150 }
2151}
2152
2153pub(crate) fn submit_codegened_module_to_llvm<B: ExtraBackendMethods>(
2154 coordinator: &Coordinator<B>,
2155 module: ModuleCodegen<B::Module>,
2156 cost: u64,
2157) {
2158 let llvm_work_item = WorkItem::Optimize(module);
2159 drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost }));
2160}
2161
2162pub(crate) fn submit_post_lto_module_to_llvm<B: ExtraBackendMethods>(
2163 coordinator: &Coordinator<B>,
2164 module: CachedModuleCodegen,
2165) {
2166 let llvm_work_item = WorkItem::CopyPostLtoArtifacts(module);
2167 drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost: 0 }));
2168}
2169
2170pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
2171 tcx: TyCtxt<'_>,
2172 coordinator: &Coordinator<B>,
2173 module: CachedModuleCodegen,
2174) {
2175 let filename = pre_lto_bitcode_filename(&module.name);
2176 let bc_path = in_incr_comp_dir_sess(tcx.sess, &filename);
2177 let file = fs::File::open(&bc_path)
2178 .unwrap_or_else(|e| panic!("failed to open bitcode file `{}`: {}", bc_path.display(), e));
2179
2180 let mmap = unsafe {
2181 Mmap::map(file).unwrap_or_else(|e| {
2182 panic!("failed to mmap bitcode file `{}`: {}", bc_path.display(), e)
2183 })
2184 };
2185 drop(coordinator.sender.send(Message::AddImportOnlyModule::<B> {
2187 module_data: SerializedModule::FromUncompressedFile(mmap),
2188 work_product: module.source,
2189 }));
2190}
2191
2192fn pre_lto_bitcode_filename(module_name: &str) -> String {
2193 format!("{module_name}.{PRE_LTO_BC_EXT}")
2194}
2195
2196fn msvc_imps_needed(tcx: TyCtxt<'_>) -> bool {
2197 assert!(
2200 !(tcx.sess.opts.cg.linker_plugin_lto.enabled()
2201 && tcx.sess.target.is_like_windows
2202 && tcx.sess.opts.cg.prefer_dynamic)
2203 );
2204
2205 let can_have_static_objects =
2209 tcx.sess.lto() == Lto::Thin || tcx.crate_types().contains(&CrateType::Rlib);
2210
2211 tcx.sess.target.is_like_windows &&
2212 can_have_static_objects &&
2213 !tcx.sess.opts.cg.linker_plugin_lto.enabled()
2217}