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, FatalErrorMarker, Level, MultiSpan, Style,
19 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.opts.unstable_opts.sanitizer, 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 tm_factory: TargetMachineFactoryFn<B>,
338 pub msvc_imps_needed: bool,
339 pub is_pe_coff: bool,
340 pub target_can_use_split_dwarf: bool,
341 pub target_arch: String,
342 pub target_is_like_darwin: bool,
343 pub target_is_like_aix: bool,
344 pub split_debuginfo: rustc_target::spec::SplitDebuginfo,
345 pub split_dwarf_kind: rustc_session::config::SplitDwarfKind,
346 pub pointer_size: Size,
347
348 pub expanded_args: Vec<String>,
353
354 pub diag_emitter: SharedEmitter,
356 pub remark: Passes,
358 pub remark_dir: Option<PathBuf>,
361 pub incr_comp_session_dir: Option<PathBuf>,
364 pub parallel: bool,
368}
369
370impl<B: WriteBackendMethods> CodegenContext<B> {
371 pub fn create_dcx(&self) -> DiagCtxt {
372 DiagCtxt::new(Box::new(self.diag_emitter.clone()))
373 }
374}
375
376fn generate_thin_lto_work<B: ExtraBackendMethods>(
377 cgcx: &CodegenContext<B>,
378 exported_symbols_for_lto: &[String],
379 each_linked_rlib_for_lto: &[PathBuf],
380 needs_thin_lto: Vec<(String, B::ThinBuffer)>,
381 import_only_modules: Vec<(SerializedModule<B::ModuleBuffer>, WorkProduct)>,
382) -> Vec<(WorkItem<B>, u64)> {
383 let _prof_timer = cgcx.prof.generic_activity("codegen_thin_generate_lto_work");
384
385 let (lto_modules, copy_jobs) = B::run_thin_lto(
386 cgcx,
387 exported_symbols_for_lto,
388 each_linked_rlib_for_lto,
389 needs_thin_lto,
390 import_only_modules,
391 );
392 lto_modules
393 .into_iter()
394 .map(|module| {
395 let cost = module.cost();
396 (WorkItem::ThinLto(module), cost)
397 })
398 .chain(copy_jobs.into_iter().map(|wp| {
399 (
400 WorkItem::CopyPostLtoArtifacts(CachedModuleCodegen {
401 name: wp.cgu_name.clone(),
402 source: wp,
403 }),
404 0, )
406 }))
407 .collect()
408}
409
410struct CompiledModules {
411 modules: Vec<CompiledModule>,
412 allocator_module: Option<CompiledModule>,
413}
414
415fn need_bitcode_in_object(tcx: TyCtxt<'_>) -> bool {
416 let sess = tcx.sess;
417 sess.opts.cg.embed_bitcode
418 && tcx.crate_types().contains(&CrateType::Rlib)
419 && sess.opts.output_types.contains_key(&OutputType::Exe)
420}
421
422fn need_pre_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
423 if sess.opts.incremental.is_none() {
424 return false;
425 }
426
427 match sess.lto() {
428 Lto::No => false,
429 Lto::Fat | Lto::Thin | Lto::ThinLocal => true,
430 }
431}
432
433pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
434 backend: B,
435 tcx: TyCtxt<'_>,
436 target_cpu: String,
437 allocator_module: Option<ModuleCodegen<B::Module>>,
438) -> OngoingCodegen<B> {
439 let (coordinator_send, coordinator_receive) = channel();
440
441 let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
442 let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
443
444 let crate_info = CrateInfo::new(tcx, target_cpu);
445
446 let regular_config = ModuleConfig::new(ModuleKind::Regular, tcx, no_builtins);
447 let allocator_config = ModuleConfig::new(ModuleKind::Allocator, tcx, no_builtins);
448
449 let (shared_emitter, shared_emitter_main) = SharedEmitter::new();
450 let (codegen_worker_send, codegen_worker_receive) = channel();
451
452 let coordinator_thread = start_executing_work(
453 backend.clone(),
454 tcx,
455 &crate_info,
456 shared_emitter,
457 codegen_worker_send,
458 coordinator_receive,
459 Arc::new(regular_config),
460 Arc::new(allocator_config),
461 allocator_module,
462 coordinator_send.clone(),
463 );
464
465 OngoingCodegen {
466 backend,
467 crate_info,
468
469 codegen_worker_receive,
470 shared_emitter_main,
471 coordinator: Coordinator {
472 sender: coordinator_send,
473 future: Some(coordinator_thread),
474 phantom: PhantomData,
475 },
476 output_filenames: Arc::clone(tcx.output_filenames(())),
477 }
478}
479
480fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
481 sess: &Session,
482 compiled_modules: &CompiledModules,
483) -> FxIndexMap<WorkProductId, WorkProduct> {
484 let mut work_products = FxIndexMap::default();
485
486 if sess.opts.incremental.is_none() {
487 return work_products;
488 }
489
490 let _timer = sess.timer("copy_all_cgu_workproducts_to_incr_comp_cache_dir");
491
492 for module in &compiled_modules.modules {
493 let mut files = Vec::new();
494 if let Some(object_file_path) = &module.object {
495 files.push((OutputType::Object.extension(), object_file_path.as_path()));
496 }
497 if let Some(dwarf_object_file_path) = &module.dwarf_object {
498 files.push(("dwo", dwarf_object_file_path.as_path()));
499 }
500 if let Some(path) = &module.assembly {
501 files.push((OutputType::Assembly.extension(), path.as_path()));
502 }
503 if let Some(path) = &module.llvm_ir {
504 files.push((OutputType::LlvmAssembly.extension(), path.as_path()));
505 }
506 if let Some(path) = &module.bytecode {
507 files.push((OutputType::Bitcode.extension(), path.as_path()));
508 }
509 if let Some((id, product)) = copy_cgu_workproduct_to_incr_comp_cache_dir(
510 sess,
511 &module.name,
512 files.as_slice(),
513 &module.links_from_incr_cache,
514 ) {
515 work_products.insert(id, product);
516 }
517 }
518
519 work_products
520}
521
522fn produce_final_output_artifacts(
523 sess: &Session,
524 compiled_modules: &CompiledModules,
525 crate_output: &OutputFilenames,
526) {
527 let mut user_wants_bitcode = false;
528 let mut user_wants_objects = false;
529
530 let copy_gracefully = |from: &Path, to: &OutFileName| match to {
532 OutFileName::Stdout if let Err(e) = copy_to_stdout(from) => {
533 sess.dcx().emit_err(errors::CopyPath::new(from, to.as_path(), e));
534 }
535 OutFileName::Real(path) if let Err(e) = fs::copy(from, path) => {
536 sess.dcx().emit_err(errors::CopyPath::new(from, path, e));
537 }
538 _ => {}
539 };
540
541 let copy_if_one_unit = |output_type: OutputType, keep_numbered: bool| {
542 if let [module] = &compiled_modules.modules[..] {
543 let path = crate_output.temp_path_for_cgu(
546 output_type,
547 &module.name,
548 sess.invocation_temp.as_deref(),
549 );
550 let output = crate_output.path(output_type);
551 if !output_type.is_text_output() && output.is_tty() {
552 sess.dcx()
553 .emit_err(errors::BinaryOutputToTty { shorthand: output_type.shorthand() });
554 } else {
555 copy_gracefully(&path, &output);
556 }
557 if !sess.opts.cg.save_temps && !keep_numbered {
558 ensure_removed(sess.dcx(), &path);
560 }
561 } else {
562 if crate_output.outputs.contains_explicit_name(&output_type) {
563 sess.dcx()
566 .emit_warn(errors::IgnoringEmitPath { extension: output_type.extension() });
567 } else if crate_output.single_output_file.is_some() {
568 sess.dcx().emit_warn(errors::IgnoringOutput { extension: output_type.extension() });
571 } else {
572 }
576 }
577 };
578
579 for output_type in crate_output.outputs.keys() {
583 match *output_type {
584 OutputType::Bitcode => {
585 user_wants_bitcode = true;
586 copy_if_one_unit(OutputType::Bitcode, true);
590 }
591 OutputType::ThinLinkBitcode => {
592 copy_if_one_unit(OutputType::ThinLinkBitcode, false);
593 }
594 OutputType::LlvmAssembly => {
595 copy_if_one_unit(OutputType::LlvmAssembly, false);
596 }
597 OutputType::Assembly => {
598 copy_if_one_unit(OutputType::Assembly, false);
599 }
600 OutputType::Object => {
601 user_wants_objects = true;
602 copy_if_one_unit(OutputType::Object, true);
603 }
604 OutputType::Mir | OutputType::Metadata | OutputType::Exe | OutputType::DepInfo => {}
605 }
606 }
607
608 if !sess.opts.cg.save_temps {
621 let needs_crate_object = crate_output.outputs.contains_key(&OutputType::Exe);
637
638 let keep_numbered_bitcode = user_wants_bitcode && sess.codegen_units().as_usize() > 1;
639
640 let keep_numbered_objects =
641 needs_crate_object || (user_wants_objects && sess.codegen_units().as_usize() > 1);
642
643 for module in compiled_modules.modules.iter() {
644 if !keep_numbered_objects {
645 if let Some(ref path) = module.object {
646 ensure_removed(sess.dcx(), path);
647 }
648
649 if let Some(ref path) = module.dwarf_object {
650 ensure_removed(sess.dcx(), path);
651 }
652 }
653
654 if let Some(ref path) = module.bytecode {
655 if !keep_numbered_bitcode {
656 ensure_removed(sess.dcx(), path);
657 }
658 }
659 }
660
661 if !user_wants_bitcode
662 && let Some(ref allocator_module) = compiled_modules.allocator_module
663 && let Some(ref path) = allocator_module.bytecode
664 {
665 ensure_removed(sess.dcx(), path);
666 }
667 }
668
669 if sess.opts.json_artifact_notifications {
670 if let [module] = &compiled_modules.modules[..] {
671 module.for_each_output(|_path, ty| {
672 if sess.opts.output_types.contains_key(&ty) {
673 let descr = ty.shorthand();
674 let path = crate_output.path(ty);
677 sess.dcx().emit_artifact_notification(path.as_path(), descr);
678 }
679 });
680 } else {
681 for module in &compiled_modules.modules {
682 module.for_each_output(|path, ty| {
683 if sess.opts.output_types.contains_key(&ty) {
684 let descr = ty.shorthand();
685 sess.dcx().emit_artifact_notification(&path, descr);
686 }
687 });
688 }
689 }
690 }
691
692 }
698
699pub(crate) enum WorkItem<B: WriteBackendMethods> {
700 Optimize(ModuleCodegen<B::Module>),
702 CopyPostLtoArtifacts(CachedModuleCodegen),
705 FatLto {
707 exported_symbols_for_lto: Arc<Vec<String>>,
708 each_linked_rlib_for_lto: Vec<PathBuf>,
709 needs_fat_lto: Vec<FatLtoInput<B>>,
710 import_only_modules: Vec<(SerializedModule<B::ModuleBuffer>, WorkProduct)>,
711 },
712 ThinLto(lto::ThinModule<B>),
714}
715
716impl<B: WriteBackendMethods> WorkItem<B> {
717 fn short_description(&self) -> String {
719 #[cfg(not(windows))]
723 fn desc(short: &str, _long: &str, name: &str) -> String {
724 assert_eq!(short.len(), 3);
744 let name = if let Some(index) = name.find("-cgu.") {
745 &name[index + 1..] } else {
747 name
748 };
749 format!("{short} {name}")
750 }
751
752 #[cfg(windows)]
754 fn desc(_short: &str, long: &str, name: &str) -> String {
755 format!("{long} {name}")
756 }
757
758 match self {
759 WorkItem::Optimize(m) => desc("opt", "optimize module", &m.name),
760 WorkItem::CopyPostLtoArtifacts(m) => desc("cpy", "copy LTO artifacts for", &m.name),
761 WorkItem::FatLto { .. } => desc("lto", "fat LTO module", "everything"),
762 WorkItem::ThinLto(m) => desc("lto", "thin-LTO module", m.name()),
763 }
764 }
765}
766
767pub(crate) enum WorkItemResult<B: WriteBackendMethods> {
769 Finished(CompiledModule),
771
772 NeedsFatLto(FatLtoInput<B>),
775
776 NeedsThinLto(String, B::ThinBuffer),
779}
780
781pub enum FatLtoInput<B: WriteBackendMethods> {
782 Serialized { name: String, buffer: SerializedModule<B::ModuleBuffer> },
783 InMemory(ModuleCodegen<B::Module>),
784}
785
786pub(crate) enum ComputedLtoType {
788 No,
789 Thin,
790 Fat,
791}
792
793pub(crate) fn compute_per_cgu_lto_type(
794 sess_lto: &Lto,
795 opts: &config::Options,
796 sess_crate_types: &[CrateType],
797) -> ComputedLtoType {
798 let linker_does_lto = opts.cg.linker_plugin_lto.enabled();
802
803 let is_rlib = matches!(sess_crate_types, [CrateType::Rlib]);
812
813 match sess_lto {
814 Lto::ThinLocal if !linker_does_lto => ComputedLtoType::Thin,
815 Lto::Thin if !linker_does_lto && !is_rlib => ComputedLtoType::Thin,
816 Lto::Fat if !is_rlib => ComputedLtoType::Fat,
817 _ => ComputedLtoType::No,
818 }
819}
820
821fn execute_optimize_work_item<B: ExtraBackendMethods>(
822 cgcx: &CodegenContext<B>,
823 mut module: ModuleCodegen<B::Module>,
824) -> WorkItemResult<B> {
825 let dcx = cgcx.create_dcx();
826 let dcx = dcx.handle();
827
828 B::optimize(cgcx, dcx, &mut module, &cgcx.module_config);
829
830 let lto_type = compute_per_cgu_lto_type(&cgcx.lto, &cgcx.opts, &cgcx.crate_types);
836
837 let bitcode = if cgcx.module_config.emit_pre_lto_bc {
840 let filename = pre_lto_bitcode_filename(&module.name);
841 cgcx.incr_comp_session_dir.as_ref().map(|path| path.join(&filename))
842 } else {
843 None
844 };
845
846 match lto_type {
847 ComputedLtoType::No => {
848 let module = B::codegen(cgcx, module, &cgcx.module_config);
849 WorkItemResult::Finished(module)
850 }
851 ComputedLtoType::Thin => {
852 let (name, thin_buffer) = B::prepare_thin(module, false);
853 if let Some(path) = bitcode {
854 fs::write(&path, thin_buffer.data()).unwrap_or_else(|e| {
855 panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);
856 });
857 }
858 WorkItemResult::NeedsThinLto(name, thin_buffer)
859 }
860 ComputedLtoType::Fat => match bitcode {
861 Some(path) => {
862 let (name, buffer) = B::serialize_module(module);
863 fs::write(&path, buffer.data()).unwrap_or_else(|e| {
864 panic!("Error writing pre-lto-bitcode file `{}`: {}", path.display(), e);
865 });
866 WorkItemResult::NeedsFatLto(FatLtoInput::Serialized {
867 name,
868 buffer: SerializedModule::Local(buffer),
869 })
870 }
871 None => WorkItemResult::NeedsFatLto(FatLtoInput::InMemory(module)),
872 },
873 }
874}
875
876fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
877 cgcx: &CodegenContext<B>,
878 module: CachedModuleCodegen,
879) -> WorkItemResult<B> {
880 let incr_comp_session_dir = cgcx.incr_comp_session_dir.as_ref().unwrap();
881
882 let mut links_from_incr_cache = Vec::new();
883
884 let mut load_from_incr_comp_dir = |output_path: PathBuf, saved_path: &str| {
885 let source_file = in_incr_comp_dir(incr_comp_session_dir, saved_path);
886 debug!(
887 "copying preexisting module `{}` from {:?} to {}",
888 module.name,
889 source_file,
890 output_path.display()
891 );
892 match link_or_copy(&source_file, &output_path) {
893 Ok(_) => {
894 links_from_incr_cache.push(source_file);
895 Some(output_path)
896 }
897 Err(error) => {
898 cgcx.create_dcx().handle().emit_err(errors::CopyPathBuf {
899 source_file,
900 output_path,
901 error,
902 });
903 None
904 }
905 }
906 };
907
908 let dwarf_object =
909 module.source.saved_files.get("dwo").as_ref().and_then(|saved_dwarf_object_file| {
910 let dwarf_obj_out = cgcx
911 .output_filenames
912 .split_dwarf_path(
913 cgcx.split_debuginfo,
914 cgcx.split_dwarf_kind,
915 &module.name,
916 cgcx.invocation_temp.as_deref(),
917 )
918 .expect(
919 "saved dwarf object in work product but `split_dwarf_path` returned `None`",
920 );
921 load_from_incr_comp_dir(dwarf_obj_out, saved_dwarf_object_file)
922 });
923
924 let mut load_from_incr_cache = |perform, output_type: OutputType| {
925 if perform {
926 let saved_file = module.source.saved_files.get(output_type.extension())?;
927 let output_path = cgcx.output_filenames.temp_path_for_cgu(
928 output_type,
929 &module.name,
930 cgcx.invocation_temp.as_deref(),
931 );
932 load_from_incr_comp_dir(output_path, &saved_file)
933 } else {
934 None
935 }
936 };
937
938 let module_config = &cgcx.module_config;
939 let should_emit_obj = module_config.emit_obj != EmitObj::None;
940 let assembly = load_from_incr_cache(module_config.emit_asm, OutputType::Assembly);
941 let llvm_ir = load_from_incr_cache(module_config.emit_ir, OutputType::LlvmAssembly);
942 let bytecode = load_from_incr_cache(module_config.emit_bc, OutputType::Bitcode);
943 let object = load_from_incr_cache(should_emit_obj, OutputType::Object);
944 if should_emit_obj && object.is_none() {
945 cgcx.create_dcx().handle().emit_fatal(errors::NoSavedObjectFile { cgu_name: &module.name })
946 }
947
948 WorkItemResult::Finished(CompiledModule {
949 links_from_incr_cache,
950 name: module.name,
951 object,
952 dwarf_object,
953 bytecode,
954 assembly,
955 llvm_ir,
956 })
957}
958
959fn execute_fat_lto_work_item<B: ExtraBackendMethods>(
960 cgcx: &CodegenContext<B>,
961 exported_symbols_for_lto: &[String],
962 each_linked_rlib_for_lto: &[PathBuf],
963 mut needs_fat_lto: Vec<FatLtoInput<B>>,
964 import_only_modules: Vec<(SerializedModule<B::ModuleBuffer>, WorkProduct)>,
965) -> WorkItemResult<B> {
966 for (module, wp) in import_only_modules {
967 needs_fat_lto.push(FatLtoInput::Serialized { name: wp.cgu_name, buffer: module })
968 }
969
970 let module = B::run_and_optimize_fat_lto(
971 cgcx,
972 exported_symbols_for_lto,
973 each_linked_rlib_for_lto,
974 needs_fat_lto,
975 );
976 let module = B::codegen(cgcx, module, &cgcx.module_config);
977 WorkItemResult::Finished(module)
978}
979
980fn execute_thin_lto_work_item<B: ExtraBackendMethods>(
981 cgcx: &CodegenContext<B>,
982 module: lto::ThinModule<B>,
983) -> WorkItemResult<B> {
984 let module = B::optimize_thin(cgcx, module);
985 let module = B::codegen(cgcx, module, &cgcx.module_config);
986 WorkItemResult::Finished(module)
987}
988
989pub(crate) enum Message<B: WriteBackendMethods> {
991 Token(io::Result<Acquired>),
994
995 WorkItem { result: Result<WorkItemResult<B>, Option<WorkerFatalError>> },
998
999 CodegenDone { llvm_work_item: WorkItem<B>, cost: u64 },
1003
1004 AddImportOnlyModule {
1007 module_data: SerializedModule<B::ModuleBuffer>,
1008 work_product: WorkProduct,
1009 },
1010
1011 CodegenComplete,
1014
1015 CodegenAborted,
1018}
1019
1020pub struct CguMessage;
1023
1024struct Diagnostic {
1034 level: Level,
1035 messages: Vec<(DiagMessage, Style)>,
1036 code: Option<ErrCode>,
1037 children: Vec<Subdiagnostic>,
1038 args: DiagArgMap,
1039}
1040
1041pub(crate) struct Subdiagnostic {
1045 level: Level,
1046 messages: Vec<(DiagMessage, Style)>,
1047}
1048
1049#[derive(PartialEq, Clone, Copy, Debug)]
1050enum MainThreadState {
1051 Idle,
1053
1054 Codegenning,
1056
1057 Lending,
1059}
1060
1061fn start_executing_work<B: ExtraBackendMethods>(
1062 backend: B,
1063 tcx: TyCtxt<'_>,
1064 crate_info: &CrateInfo,
1065 shared_emitter: SharedEmitter,
1066 codegen_worker_send: Sender<CguMessage>,
1067 coordinator_receive: Receiver<Message<B>>,
1068 regular_config: Arc<ModuleConfig>,
1069 allocator_config: Arc<ModuleConfig>,
1070 allocator_module: Option<ModuleCodegen<B::Module>>,
1071 tx_to_llvm_workers: Sender<Message<B>>,
1072) -> thread::JoinHandle<Result<CompiledModules, ()>> {
1073 let coordinator_send = tx_to_llvm_workers;
1074 let sess = tcx.sess;
1075
1076 let mut each_linked_rlib_for_lto = Vec::new();
1077 let mut each_linked_rlib_file_for_lto = Vec::new();
1078 drop(link::each_linked_rlib(crate_info, None, &mut |cnum, path| {
1079 if link::ignored_for_lto(sess, crate_info, cnum) {
1080 return;
1081 }
1082 each_linked_rlib_for_lto.push(cnum);
1083 each_linked_rlib_file_for_lto.push(path.to_path_buf());
1084 }));
1085
1086 let exported_symbols_for_lto =
1088 Arc::new(lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto));
1089
1090 let coordinator_send2 = coordinator_send.clone();
1096 let helper = jobserver::client()
1097 .into_helper_thread(move |token| {
1098 drop(coordinator_send2.send(Message::Token::<B>(token)));
1099 })
1100 .expect("failed to spawn helper thread");
1101
1102 let ol =
1103 if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
1104 config::OptLevel::No
1106 } else {
1107 tcx.backend_optimization_level(())
1108 };
1109 let backend_features = tcx.global_backend_features(());
1110
1111 let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {
1112 let result = fs::create_dir_all(dir).and_then(|_| dir.canonicalize());
1113 match result {
1114 Ok(dir) => Some(dir),
1115 Err(error) => sess.dcx().emit_fatal(ErrorCreatingRemarkDir { error }),
1116 }
1117 } else {
1118 None
1119 };
1120
1121 let cgcx = CodegenContext::<B> {
1122 crate_types: tcx.crate_types().to_vec(),
1123 lto: sess.lto(),
1124 fewer_names: sess.fewer_names(),
1125 save_temps: sess.opts.cg.save_temps,
1126 time_trace: sess.opts.unstable_opts.llvm_time_trace,
1127 opts: Arc::new(sess.opts.clone()),
1128 prof: sess.prof.clone(),
1129 remark: sess.opts.cg.remark.clone(),
1130 remark_dir,
1131 incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),
1132 expanded_args: tcx.sess.expanded_args.clone(),
1133 diag_emitter: shared_emitter.clone(),
1134 output_filenames: Arc::clone(tcx.output_filenames(())),
1135 module_config: regular_config,
1136 tm_factory: backend.target_machine_factory(tcx.sess, ol, backend_features),
1137 msvc_imps_needed: msvc_imps_needed(tcx),
1138 is_pe_coff: tcx.sess.target.is_like_windows,
1139 target_can_use_split_dwarf: tcx.sess.target_can_use_split_dwarf(),
1140 target_arch: tcx.sess.target.arch.to_string(),
1141 target_is_like_darwin: tcx.sess.target.is_like_darwin,
1142 target_is_like_aix: tcx.sess.target.is_like_aix,
1143 split_debuginfo: tcx.sess.split_debuginfo(),
1144 split_dwarf_kind: tcx.sess.opts.unstable_opts.split_dwarf_kind,
1145 parallel: backend.supports_parallel() && !sess.opts.unstable_opts.no_parallel_backend,
1146 pointer_size: tcx.data_layout.pointer_size(),
1147 invocation_temp: sess.invocation_temp.clone(),
1148 };
1149
1150 let compiled_allocator_module = allocator_module.map(|mut allocator_module| {
1151 B::optimize(&cgcx, tcx.sess.dcx(), &mut allocator_module, &allocator_config);
1152 B::codegen(&cgcx, allocator_module, &allocator_config)
1153 });
1154
1155 return B::spawn_named_thread(cgcx.time_trace, "coordinator".to_string(), move || {
1291 let mut compiled_modules = vec![];
1294 let mut needs_fat_lto = Vec::new();
1295 let mut needs_thin_lto = Vec::new();
1296 let mut lto_import_only_modules = Vec::new();
1297 let mut started_lto = false;
1298
1299 #[derive(Debug, PartialEq)]
1304 enum CodegenState {
1305 Ongoing,
1306 Completed,
1307 Aborted,
1308 }
1309 use CodegenState::*;
1310 let mut codegen_state = Ongoing;
1311
1312 let mut work_items = Vec::<(WorkItem<B>, u64)>::new();
1314
1315 let mut tokens = Vec::new();
1318
1319 let mut main_thread_state = MainThreadState::Idle;
1320
1321 let mut running_with_own_token = 0;
1324
1325 let running_with_any_token = |main_thread_state, running_with_own_token| {
1328 running_with_own_token
1329 + if main_thread_state == MainThreadState::Lending { 1 } else { 0 }
1330 };
1331
1332 let mut llvm_start_time: Option<VerboseTimingGuard<'_>> = None;
1333
1334 loop {
1340 if codegen_state == Ongoing {
1344 if main_thread_state == MainThreadState::Idle {
1345 let extra_tokens = tokens.len().checked_sub(running_with_own_token).unwrap();
1353 let additional_running = std::cmp::min(extra_tokens, work_items.len());
1354 let anticipated_running = running_with_own_token + additional_running + 1;
1355
1356 if !queue_full_enough(work_items.len(), anticipated_running) {
1357 if codegen_worker_send.send(CguMessage).is_err() {
1359 panic!("Could not send CguMessage to main thread")
1360 }
1361 main_thread_state = MainThreadState::Codegenning;
1362 } else {
1363 let (item, _) =
1367 work_items.pop().expect("queue empty - queue_full_enough() broken?");
1368 main_thread_state = MainThreadState::Lending;
1369 spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
1370 }
1371 }
1372 } else if codegen_state == Completed {
1373 if running_with_any_token(main_thread_state, running_with_own_token) == 0
1374 && work_items.is_empty()
1375 {
1376 if needs_fat_lto.is_empty()
1378 && needs_thin_lto.is_empty()
1379 && lto_import_only_modules.is_empty()
1380 {
1381 break;
1383 }
1384
1385 assert!(!started_lto);
1391 started_lto = true;
1392
1393 let needs_fat_lto = mem::take(&mut needs_fat_lto);
1394 let needs_thin_lto = mem::take(&mut needs_thin_lto);
1395 let import_only_modules = mem::take(&mut lto_import_only_modules);
1396 let each_linked_rlib_file_for_lto =
1397 mem::take(&mut each_linked_rlib_file_for_lto);
1398
1399 check_lto_allowed(&cgcx);
1400
1401 if !needs_fat_lto.is_empty() {
1402 assert!(needs_thin_lto.is_empty());
1403
1404 work_items.push((
1405 WorkItem::FatLto {
1406 exported_symbols_for_lto: Arc::clone(&exported_symbols_for_lto),
1407 each_linked_rlib_for_lto: each_linked_rlib_file_for_lto,
1408 needs_fat_lto,
1409 import_only_modules,
1410 },
1411 0,
1412 ));
1413 if cgcx.parallel {
1414 helper.request_token();
1415 }
1416 } else {
1417 for (work, cost) in generate_thin_lto_work(
1418 &cgcx,
1419 &exported_symbols_for_lto,
1420 &each_linked_rlib_file_for_lto,
1421 needs_thin_lto,
1422 import_only_modules,
1423 ) {
1424 let insertion_index = work_items
1425 .binary_search_by_key(&cost, |&(_, cost)| cost)
1426 .unwrap_or_else(|e| e);
1427 work_items.insert(insertion_index, (work, cost));
1428 if cgcx.parallel {
1429 helper.request_token();
1430 }
1431 }
1432 }
1433 }
1434
1435 match main_thread_state {
1439 MainThreadState::Idle => {
1440 if let Some((item, _)) = work_items.pop() {
1441 main_thread_state = MainThreadState::Lending;
1442 spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
1443 } else {
1444 assert!(running_with_own_token > 0);
1451 running_with_own_token -= 1;
1452 main_thread_state = MainThreadState::Lending;
1453 }
1454 }
1455 MainThreadState::Codegenning => bug!(
1456 "codegen worker should not be codegenning after \
1457 codegen was already completed"
1458 ),
1459 MainThreadState::Lending => {
1460 }
1462 }
1463 } else {
1464 assert!(codegen_state == Aborted);
1467 if running_with_any_token(main_thread_state, running_with_own_token) == 0 {
1468 break;
1469 }
1470 }
1471
1472 if codegen_state != Aborted {
1475 while running_with_own_token < tokens.len()
1476 && let Some((item, _)) = work_items.pop()
1477 {
1478 spawn_work(&cgcx, coordinator_send.clone(), &mut llvm_start_time, item);
1479 running_with_own_token += 1;
1480 }
1481 }
1482
1483 tokens.truncate(running_with_own_token);
1485
1486 match coordinator_receive.recv().unwrap() {
1487 Message::Token(token) => {
1491 match token {
1492 Ok(token) => {
1493 tokens.push(token);
1494
1495 if main_thread_state == MainThreadState::Lending {
1496 main_thread_state = MainThreadState::Idle;
1501 running_with_own_token += 1;
1502 }
1503 }
1504 Err(e) => {
1505 let msg = &format!("failed to acquire jobserver token: {e}");
1506 shared_emitter.fatal(msg);
1507 codegen_state = Aborted;
1508 }
1509 }
1510 }
1511
1512 Message::CodegenDone { llvm_work_item, cost } => {
1513 let insertion_index = work_items.binary_search_by_key(&cost, |&(_, cost)| cost);
1522 let insertion_index = match insertion_index {
1523 Ok(idx) | Err(idx) => idx,
1524 };
1525 work_items.insert(insertion_index, (llvm_work_item, cost));
1526
1527 if cgcx.parallel {
1528 helper.request_token();
1529 }
1530 assert_eq!(main_thread_state, MainThreadState::Codegenning);
1531 main_thread_state = MainThreadState::Idle;
1532 }
1533
1534 Message::CodegenComplete => {
1535 if codegen_state != Aborted {
1536 codegen_state = Completed;
1537 }
1538 assert_eq!(main_thread_state, MainThreadState::Codegenning);
1539 main_thread_state = MainThreadState::Idle;
1540 }
1541
1542 Message::CodegenAborted => {
1550 codegen_state = Aborted;
1551 }
1552
1553 Message::WorkItem { result } => {
1554 if main_thread_state == MainThreadState::Lending {
1560 main_thread_state = MainThreadState::Idle;
1561 } else {
1562 running_with_own_token -= 1;
1563 }
1564
1565 match result {
1566 Ok(WorkItemResult::Finished(compiled_module)) => {
1567 compiled_modules.push(compiled_module);
1568 }
1569 Ok(WorkItemResult::NeedsFatLto(fat_lto_input)) => {
1570 assert!(!started_lto);
1571 assert!(needs_thin_lto.is_empty());
1572 needs_fat_lto.push(fat_lto_input);
1573 }
1574 Ok(WorkItemResult::NeedsThinLto(name, thin_buffer)) => {
1575 assert!(!started_lto);
1576 assert!(needs_fat_lto.is_empty());
1577 needs_thin_lto.push((name, thin_buffer));
1578 }
1579 Err(Some(WorkerFatalError)) => {
1580 codegen_state = Aborted;
1582 }
1583 Err(None) => {
1584 bug!("worker thread panicked");
1587 }
1588 }
1589 }
1590
1591 Message::AddImportOnlyModule { module_data, work_product } => {
1592 assert!(!started_lto);
1593 assert_eq!(codegen_state, Ongoing);
1594 assert_eq!(main_thread_state, MainThreadState::Codegenning);
1595 lto_import_only_modules.push((module_data, work_product));
1596 main_thread_state = MainThreadState::Idle;
1597 }
1598 }
1599 }
1600
1601 if codegen_state == Aborted {
1602 return Err(());
1603 }
1604
1605 drop(llvm_start_time);
1607
1608 compiled_modules.sort_by(|a, b| a.name.cmp(&b.name));
1612
1613 Ok(CompiledModules {
1614 modules: compiled_modules,
1615 allocator_module: compiled_allocator_module,
1616 })
1617 })
1618 .expect("failed to spawn coordinator thread");
1619
1620 fn queue_full_enough(items_in_queue: usize, workers_running: usize) -> bool {
1623 let quarter_of_workers = workers_running - 3 * workers_running / 4;
1674 items_in_queue > 0 && items_in_queue >= quarter_of_workers
1675 }
1676}
1677
1678#[must_use]
1680pub(crate) struct WorkerFatalError;
1681
1682fn spawn_work<'a, B: ExtraBackendMethods>(
1683 cgcx: &'a CodegenContext<B>,
1684 coordinator_send: Sender<Message<B>>,
1685 llvm_start_time: &mut Option<VerboseTimingGuard<'a>>,
1686 work: WorkItem<B>,
1687) {
1688 if llvm_start_time.is_none() {
1689 *llvm_start_time = Some(cgcx.prof.verbose_generic_activity("LLVM_passes"));
1690 }
1691
1692 let cgcx = cgcx.clone();
1693
1694 B::spawn_named_thread(cgcx.time_trace, work.short_description(), move || {
1695 let result = std::panic::catch_unwind(AssertUnwindSafe(|| match work {
1696 WorkItem::Optimize(m) => {
1697 let _timer =
1698 cgcx.prof.generic_activity_with_arg("codegen_module_optimize", &*m.name);
1699 execute_optimize_work_item(&cgcx, m)
1700 }
1701 WorkItem::CopyPostLtoArtifacts(m) => {
1702 let _timer = cgcx
1703 .prof
1704 .generic_activity_with_arg("codegen_copy_artifacts_from_incr_cache", &*m.name);
1705 execute_copy_from_cache_work_item(&cgcx, m)
1706 }
1707 WorkItem::FatLto {
1708 exported_symbols_for_lto,
1709 each_linked_rlib_for_lto,
1710 needs_fat_lto,
1711 import_only_modules,
1712 } => {
1713 let _timer =
1714 cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", "everything");
1715 execute_fat_lto_work_item(
1716 &cgcx,
1717 &exported_symbols_for_lto,
1718 &each_linked_rlib_for_lto,
1719 needs_fat_lto,
1720 import_only_modules,
1721 )
1722 }
1723 WorkItem::ThinLto(m) => {
1724 let _timer =
1725 cgcx.prof.generic_activity_with_arg("codegen_module_perform_lto", m.name());
1726 execute_thin_lto_work_item(&cgcx, m)
1727 }
1728 }));
1729
1730 let msg = match result {
1731 Ok(result) => Message::WorkItem::<B> { result: Ok(result) },
1732
1733 Err(err) if err.is::<FatalErrorMarker>() => {
1737 Message::WorkItem::<B> { result: Err(Some(WorkerFatalError)) }
1738 }
1739
1740 Err(_) => Message::WorkItem::<B> { result: Err(None) },
1741 };
1742 drop(coordinator_send.send(msg));
1743 })
1744 .expect("failed to spawn work thread");
1745}
1746
1747enum SharedEmitterMessage {
1748 Diagnostic(Diagnostic),
1749 InlineAsmError(SpanData, String, Level, Option<(String, Vec<InnerSpan>)>),
1750 Fatal(String),
1751}
1752
1753#[derive(Clone)]
1754pub struct SharedEmitter {
1755 sender: Sender<SharedEmitterMessage>,
1756}
1757
1758pub struct SharedEmitterMain {
1759 receiver: Receiver<SharedEmitterMessage>,
1760}
1761
1762impl SharedEmitter {
1763 fn new() -> (SharedEmitter, SharedEmitterMain) {
1764 let (sender, receiver) = channel();
1765
1766 (SharedEmitter { sender }, SharedEmitterMain { receiver })
1767 }
1768
1769 pub fn inline_asm_error(
1770 &self,
1771 span: SpanData,
1772 msg: String,
1773 level: Level,
1774 source: Option<(String, Vec<InnerSpan>)>,
1775 ) {
1776 drop(self.sender.send(SharedEmitterMessage::InlineAsmError(span, msg, level, source)));
1777 }
1778
1779 fn fatal(&self, msg: &str) {
1780 drop(self.sender.send(SharedEmitterMessage::Fatal(msg.to_string())));
1781 }
1782}
1783
1784impl Emitter for SharedEmitter {
1785 fn emit_diagnostic(
1786 &mut self,
1787 mut diag: rustc_errors::DiagInner,
1788 _registry: &rustc_errors::registry::Registry,
1789 ) {
1790 assert_eq!(diag.span, MultiSpan::new());
1793 assert_eq!(diag.suggestions, Suggestions::Enabled(vec![]));
1794 assert_eq!(diag.sort_span, rustc_span::DUMMY_SP);
1795 assert_eq!(diag.is_lint, None);
1796 let args = mem::replace(&mut diag.args, DiagArgMap::default());
1799 drop(
1800 self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
1801 level: diag.level(),
1802 messages: diag.messages,
1803 code: diag.code,
1804 children: diag
1805 .children
1806 .into_iter()
1807 .map(|child| Subdiagnostic { level: child.level, messages: child.messages })
1808 .collect(),
1809 args,
1810 })),
1811 );
1812 }
1813
1814 fn source_map(&self) -> Option<&SourceMap> {
1815 None
1816 }
1817
1818 fn translator(&self) -> &Translator {
1819 panic!("shared emitter attempted to translate a diagnostic");
1820 }
1821}
1822
1823impl SharedEmitterMain {
1824 fn check(&self, sess: &Session, blocking: bool) {
1825 loop {
1826 let message = if blocking {
1827 match self.receiver.recv() {
1828 Ok(message) => Ok(message),
1829 Err(_) => Err(()),
1830 }
1831 } else {
1832 match self.receiver.try_recv() {
1833 Ok(message) => Ok(message),
1834 Err(_) => Err(()),
1835 }
1836 };
1837
1838 match message {
1839 Ok(SharedEmitterMessage::Diagnostic(diag)) => {
1840 let dcx = sess.dcx();
1843 let mut d =
1844 rustc_errors::DiagInner::new_with_messages(diag.level, diag.messages);
1845 d.code = diag.code; d.children = diag
1847 .children
1848 .into_iter()
1849 .map(|sub| rustc_errors::Subdiag {
1850 level: sub.level,
1851 messages: sub.messages,
1852 span: MultiSpan::new(),
1853 })
1854 .collect();
1855 d.args = diag.args;
1856 dcx.emit_diagnostic(d);
1857 sess.dcx().abort_if_errors();
1858 }
1859 Ok(SharedEmitterMessage::InlineAsmError(span, msg, level, source)) => {
1860 assert_matches!(level, Level::Error | Level::Warning | Level::Note);
1861 let mut err = Diag::<()>::new(sess.dcx(), level, msg);
1862 if !span.is_dummy() {
1863 err.span(span.span());
1864 }
1865
1866 if let Some((buffer, spans)) = source {
1868 let source = sess
1869 .source_map()
1870 .new_source_file(FileName::inline_asm_source_code(&buffer), buffer);
1871 let spans: Vec<_> = spans
1872 .iter()
1873 .map(|sp| {
1874 Span::with_root_ctxt(
1875 source.normalized_byte_pos(sp.start as u32),
1876 source.normalized_byte_pos(sp.end as u32),
1877 )
1878 })
1879 .collect();
1880 err.span_note(spans, "instantiated into assembly here");
1881 }
1882
1883 err.emit();
1884 }
1885 Ok(SharedEmitterMessage::Fatal(msg)) => {
1886 sess.dcx().fatal(msg);
1887 }
1888 Err(_) => {
1889 break;
1890 }
1891 }
1892 }
1893 }
1894}
1895
1896pub struct Coordinator<B: ExtraBackendMethods> {
1897 sender: Sender<Message<B>>,
1898 future: Option<thread::JoinHandle<Result<CompiledModules, ()>>>,
1899 phantom: PhantomData<B>,
1901}
1902
1903impl<B: ExtraBackendMethods> Coordinator<B> {
1904 fn join(mut self) -> std::thread::Result<Result<CompiledModules, ()>> {
1905 self.future.take().unwrap().join()
1906 }
1907}
1908
1909impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
1910 fn drop(&mut self) {
1911 if let Some(future) = self.future.take() {
1912 drop(self.sender.send(Message::CodegenAborted::<B>));
1915 drop(future.join());
1916 }
1917 }
1918}
1919
1920pub struct OngoingCodegen<B: ExtraBackendMethods> {
1921 pub backend: B,
1922 pub crate_info: CrateInfo,
1923 pub output_filenames: Arc<OutputFilenames>,
1924 pub coordinator: Coordinator<B>,
1928 pub codegen_worker_receive: Receiver<CguMessage>,
1929 pub shared_emitter_main: SharedEmitterMain,
1930}
1931
1932impl<B: ExtraBackendMethods> OngoingCodegen<B> {
1933 pub fn join(self, sess: &Session) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
1934 self.shared_emitter_main.check(sess, true);
1935 let compiled_modules = sess.time("join_worker_thread", || match self.coordinator.join() {
1936 Ok(Ok(compiled_modules)) => compiled_modules,
1937 Ok(Err(())) => {
1938 sess.dcx().abort_if_errors();
1939 panic!("expected abort due to worker thread errors")
1940 }
1941 Err(_) => {
1942 bug!("panic during codegen/LLVM phase");
1943 }
1944 });
1945
1946 sess.dcx().abort_if_errors();
1947
1948 let work_products =
1949 copy_all_cgu_workproducts_to_incr_comp_cache_dir(sess, &compiled_modules);
1950 produce_final_output_artifacts(sess, &compiled_modules, &self.output_filenames);
1951
1952 if sess.codegen_units().as_usize() == 1 && sess.opts.unstable_opts.time_llvm_passes {
1955 self.backend.print_pass_timings()
1956 }
1957
1958 if sess.print_llvm_stats() {
1959 self.backend.print_statistics()
1960 }
1961
1962 (
1963 CodegenResults {
1964 crate_info: self.crate_info,
1965
1966 modules: compiled_modules.modules,
1967 allocator_module: compiled_modules.allocator_module,
1968 },
1969 work_products,
1970 )
1971 }
1972
1973 pub(crate) fn codegen_finished(&self, tcx: TyCtxt<'_>) {
1974 self.wait_for_signal_to_codegen_item();
1975 self.check_for_errors(tcx.sess);
1976 drop(self.coordinator.sender.send(Message::CodegenComplete::<B>));
1977 }
1978
1979 pub(crate) fn check_for_errors(&self, sess: &Session) {
1980 self.shared_emitter_main.check(sess, false);
1981 }
1982
1983 pub(crate) fn wait_for_signal_to_codegen_item(&self) {
1984 match self.codegen_worker_receive.recv() {
1985 Ok(CguMessage) => {
1986 }
1988 Err(_) => {
1989 }
1992 }
1993 }
1994}
1995
1996pub(crate) fn submit_codegened_module_to_llvm<B: ExtraBackendMethods>(
1997 coordinator: &Coordinator<B>,
1998 module: ModuleCodegen<B::Module>,
1999 cost: u64,
2000) {
2001 let llvm_work_item = WorkItem::Optimize(module);
2002 drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost }));
2003}
2004
2005pub(crate) fn submit_post_lto_module_to_llvm<B: ExtraBackendMethods>(
2006 coordinator: &Coordinator<B>,
2007 module: CachedModuleCodegen,
2008) {
2009 let llvm_work_item = WorkItem::CopyPostLtoArtifacts(module);
2010 drop(coordinator.sender.send(Message::CodegenDone::<B> { llvm_work_item, cost: 0 }));
2011}
2012
2013pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
2014 tcx: TyCtxt<'_>,
2015 coordinator: &Coordinator<B>,
2016 module: CachedModuleCodegen,
2017) {
2018 let filename = pre_lto_bitcode_filename(&module.name);
2019 let bc_path = in_incr_comp_dir_sess(tcx.sess, &filename);
2020 let file = fs::File::open(&bc_path)
2021 .unwrap_or_else(|e| panic!("failed to open bitcode file `{}`: {}", bc_path.display(), e));
2022
2023 let mmap = unsafe {
2024 Mmap::map(file).unwrap_or_else(|e| {
2025 panic!("failed to mmap bitcode file `{}`: {}", bc_path.display(), e)
2026 })
2027 };
2028 drop(coordinator.sender.send(Message::AddImportOnlyModule::<B> {
2030 module_data: SerializedModule::FromUncompressedFile(mmap),
2031 work_product: module.source,
2032 }));
2033}
2034
2035fn pre_lto_bitcode_filename(module_name: &str) -> String {
2036 format!("{module_name}.{PRE_LTO_BC_EXT}")
2037}
2038
2039fn msvc_imps_needed(tcx: TyCtxt<'_>) -> bool {
2040 assert!(
2043 !(tcx.sess.opts.cg.linker_plugin_lto.enabled()
2044 && tcx.sess.target.is_like_windows
2045 && tcx.sess.opts.cg.prefer_dynamic)
2046 );
2047
2048 let can_have_static_objects =
2052 tcx.sess.lto() == Lto::Thin || tcx.crate_types().contains(&CrateType::Rlib);
2053
2054 tcx.sess.target.is_like_windows &&
2055 can_have_static_objects &&
2056 !tcx.sess.opts.cg.linker_plugin_lto.enabled()
2060}