1 use crate::{ModuleCodegen
, ModuleKind
, CachedModuleCodegen
, CompiledModule
, CrateInfo
,
2 CodegenResults
, RLIB_BYTECODE_EXTENSION
};
3 use super::linker
::LinkerInfo
;
4 use super::lto
::{self, SerializedModule}
;
5 use super::link
::{self, remove, get_linker}
;
6 use super::command
::Command
;
7 use super::symbol_export
::ExportedSymbols
;
10 use rustc_incremental
::{copy_cgu_workproducts_to_incr_comp_cache_dir
,
11 in_incr_comp_dir
, in_incr_comp_dir_sess
};
12 use rustc
::dep_graph
::{WorkProduct, WorkProductId, WorkProductFileKind}
;
13 use rustc_session
::cgu_reuse_tracker
::CguReuseTracker
;
14 use rustc
::middle
::cstore
::EncodedMetadata
;
15 use rustc
::session
::config
::{self, OutputFilenames
, OutputType
, Passes
, Lto
,
16 Sanitizer
, SwitchWithOptPath
};
17 use rustc
::session
::Session
;
18 use rustc
::util
::nodemap
::FxHashMap
;
19 use rustc
::hir
::def_id
::{CrateNum, LOCAL_CRATE}
;
20 use rustc
::ty
::TyCtxt
;
21 use rustc
::util
::common
::{time_depth, set_time_depth, print_time_passes_entry}
;
22 use rustc_data_structures
::profiling
::SelfProfilerRef
;
23 use rustc_fs_util
::link_or_copy
;
24 use rustc_data_structures
::svh
::Svh
;
25 use rustc_data_structures
::sync
::Lrc
;
26 use rustc_errors
::{Handler, Level, FatalError, DiagnosticId}
;
27 use syntax_pos
::source_map
::SourceMap
;
28 use rustc_errors
::emitter
::{Emitter}
;
29 use rustc_target
::spec
::MergeFunctions
;
31 use syntax_pos
::hygiene
::ExpnId
;
32 use syntax_pos
::symbol
::{Symbol, sym}
;
33 use jobserver
::{Client, Acquired}
;
39 use std
::path
::{Path, PathBuf}
;
42 use std
::sync
::mpsc
::{channel, Sender, Receiver}
;
43 use std
::time
::Instant
;
46 const PRE_LTO_BC_EXT
: &str = "pre-lto.bc";
48 /// Module-specific configuration for `optimize_and_codegen`.
49 pub struct ModuleConfig
{
50 /// Names of additional optimization passes to run.
51 pub passes
: Vec
<String
>,
52 /// Some(level) to optimize at a certain level, or None to run
53 /// absolutely no optimizations (used for the metadata module).
54 pub opt_level
: Option
<config
::OptLevel
>,
56 /// Some(level) to optimize binary size, or None to not affect program size.
57 pub opt_size
: Option
<config
::OptLevel
>,
59 pub pgo_gen
: SwitchWithOptPath
,
60 pub pgo_use
: Option
<PathBuf
>,
62 pub sanitizer
: Option
<Sanitizer
>,
63 pub sanitizer_recover
: Vec
<Sanitizer
>,
64 pub sanitizer_memory_track_origins
: usize,
66 // Flags indicating which outputs to produce.
67 pub emit_pre_lto_bc
: bool
,
68 pub emit_no_opt_bc
: bool
,
70 pub emit_bc_compressed
: bool
,
71 pub emit_lto_bc
: bool
,
75 // Miscellaneous flags. These are mostly copied from command-line
77 pub verify_llvm_ir
: bool
,
78 pub no_prepopulate_passes
: bool
,
79 pub no_builtins
: bool
,
80 pub time_passes
: bool
,
81 pub vectorize_loop
: bool
,
82 pub vectorize_slp
: bool
,
83 pub merge_functions
: bool
,
84 pub inline_threshold
: Option
<usize>,
85 // Instead of creating an object file by doing LLVM codegen, just
86 // make the object file bitcode. Provides easy compatibility with
87 // emscripten's ecc compiler, when used as the linker.
88 pub obj_is_bitcode
: bool
,
89 pub no_integrated_as
: bool
,
90 pub embed_bitcode
: bool
,
91 pub embed_bitcode_marker
: bool
,
95 fn new(passes
: Vec
<String
>) -> ModuleConfig
{
101 pgo_gen
: SwitchWithOptPath
::Disabled
,
105 sanitizer_recover
: Default
::default(),
106 sanitizer_memory_track_origins
: 0,
108 emit_no_opt_bc
: false,
109 emit_pre_lto_bc
: false,
111 emit_bc_compressed
: false,
116 obj_is_bitcode
: false,
117 embed_bitcode
: false,
118 embed_bitcode_marker
: false,
119 no_integrated_as
: false,
121 verify_llvm_ir
: false,
122 no_prepopulate_passes
: false,
125 vectorize_loop
: false,
126 vectorize_slp
: false,
127 merge_functions
: false,
128 inline_threshold
: None
132 fn set_flags(&mut self, sess
: &Session
, no_builtins
: bool
) {
133 self.verify_llvm_ir
= sess
.verify_llvm_ir();
134 self.no_prepopulate_passes
= sess
.opts
.cg
.no_prepopulate_passes
;
135 self.no_builtins
= no_builtins
|| sess
.target
.target
.options
.no_builtins
;
136 self.time_passes
= sess
.time_extended();
137 self.inline_threshold
= sess
.opts
.cg
.inline_threshold
;
138 self.obj_is_bitcode
= sess
.target
.target
.options
.obj_is_bitcode
||
139 sess
.opts
.cg
.linker_plugin_lto
.enabled();
140 let embed_bitcode
= sess
.target
.target
.options
.embed_bitcode
||
141 sess
.opts
.debugging_opts
.embed_bitcode
;
143 match sess
.opts
.optimize
{
144 config
::OptLevel
::No
|
145 config
::OptLevel
::Less
=> {
146 self.embed_bitcode_marker
= embed_bitcode
;
148 _
=> self.embed_bitcode
= embed_bitcode
,
152 // Copy what clang does by turning on loop vectorization at O2 and
153 // slp vectorization at O3. Otherwise configure other optimization aspects
154 // of this pass manager builder.
155 self.vectorize_loop
= !sess
.opts
.cg
.no_vectorize_loops
&&
156 (sess
.opts
.optimize
== config
::OptLevel
::Default
||
157 sess
.opts
.optimize
== config
::OptLevel
::Aggressive
);
159 self.vectorize_slp
= !sess
.opts
.cg
.no_vectorize_slp
&&
160 sess
.opts
.optimize
== config
::OptLevel
::Aggressive
;
162 // Some targets (namely, NVPTX) interact badly with the MergeFunctions
163 // pass. This is because MergeFunctions can generate new function calls
164 // which may interfere with the target calling convention; e.g. for the
165 // NVPTX target, PTX kernels should not call other PTX kernels.
166 // MergeFunctions can also be configured to generate aliases instead,
167 // but aliases are not supported by some backends (again, NVPTX).
168 // Therefore, allow targets to opt out of the MergeFunctions pass,
169 // but otherwise keep the pass enabled (at O2 and O3) since it can be
170 // useful for reducing code size.
171 self.merge_functions
= match sess
.opts
.debugging_opts
.merge_functions
172 .unwrap_or(sess
.target
.target
.options
.merge_functions
) {
173 MergeFunctions
::Disabled
=> false,
174 MergeFunctions
::Trampolines
|
175 MergeFunctions
::Aliases
=> {
176 sess
.opts
.optimize
== config
::OptLevel
::Default
||
177 sess
.opts
.optimize
== config
::OptLevel
::Aggressive
182 pub fn bitcode_needed(&self) -> bool
{
183 self.emit_bc
|| self.obj_is_bitcode
184 || self.emit_bc_compressed
|| self.embed_bitcode
188 /// Assembler name and command used by codegen when no_integrated_as is enabled
189 pub struct AssemblerCommand
{
194 // HACK(eddyb) work around `#[derive]` producing wrong bounds for `Clone`.
195 pub struct TargetMachineFactory
<B
: WriteBackendMethods
>(
196 pub Arc
<dyn Fn() -> Result
<B
::TargetMachine
, String
> + Send
+ Sync
>,
199 impl<B
: WriteBackendMethods
> Clone
for TargetMachineFactory
<B
> {
200 fn clone(&self) -> Self {
201 TargetMachineFactory(self.0.clone())
205 /// Additional resources used by optimize_and_codegen (not module specific)
207 pub struct CodegenContext
<B
: WriteBackendMethods
> {
208 // Resources needed when running LTO
210 pub time_passes
: bool
,
211 pub prof
: SelfProfilerRef
,
213 pub no_landing_pads
: bool
,
214 pub save_temps
: bool
,
215 pub fewer_names
: bool
,
216 pub exported_symbols
: Option
<Arc
<ExportedSymbols
>>,
217 pub opts
: Arc
<config
::Options
>,
218 pub crate_types
: Vec
<config
::CrateType
>,
219 pub each_linked_rlib_for_lto
: Vec
<(CrateNum
, PathBuf
)>,
220 pub output_filenames
: Arc
<OutputFilenames
>,
221 pub regular_module_config
: Arc
<ModuleConfig
>,
222 pub metadata_module_config
: Arc
<ModuleConfig
>,
223 pub allocator_module_config
: Arc
<ModuleConfig
>,
224 pub tm_factory
: TargetMachineFactory
<B
>,
225 pub msvc_imps_needed
: bool
,
226 pub target_pointer_width
: String
,
227 pub target_arch
: String
,
228 pub debuginfo
: config
::DebugInfo
,
230 // Number of cgus excluding the allocator/metadata modules
231 pub total_cgus
: usize,
232 // Handler to use for diagnostics produced during codegen.
233 pub diag_emitter
: SharedEmitter
,
234 // LLVM optimizations for which we want to print remarks.
236 // Worker thread number
238 // The incremental compilation session directory, or None if we are not
239 // compiling incrementally
240 pub incr_comp_session_dir
: Option
<PathBuf
>,
241 // Used to update CGU re-use information during the thinlto phase.
242 pub cgu_reuse_tracker
: CguReuseTracker
,
243 // Channel back to the main control thread to send messages to
244 pub coordinator_send
: Sender
<Box
<dyn Any
+ Send
>>,
245 // The assembler command if no_integrated_as option is enabled, None otherwise
246 pub assembler_cmd
: Option
<Arc
<AssemblerCommand
>>
249 impl<B
: WriteBackendMethods
> CodegenContext
<B
> {
250 pub fn create_diag_handler(&self) -> Handler
{
251 Handler
::with_emitter(true, None
, Box
::new(self.diag_emitter
.clone()))
254 pub fn config(&self, kind
: ModuleKind
) -> &ModuleConfig
{
256 ModuleKind
::Regular
=> &self.regular_module_config
,
257 ModuleKind
::Metadata
=> &self.metadata_module_config
,
258 ModuleKind
::Allocator
=> &self.allocator_module_config
,
263 fn generate_lto_work
<B
: ExtraBackendMethods
>(
264 cgcx
: &CodegenContext
<B
>,
265 needs_fat_lto
: Vec
<FatLTOInput
<B
>>,
266 needs_thin_lto
: Vec
<(String
, B
::ThinBuffer
)>,
267 import_only_modules
: Vec
<(SerializedModule
<B
::ModuleBuffer
>, WorkProduct
)>
268 ) -> Vec
<(WorkItem
<B
>, u64)> {
269 let _prof_timer
= cgcx
.prof
.generic_activity("codegen_generate_lto_work");
271 let (lto_modules
, copy_jobs
) = if !needs_fat_lto
.is_empty() {
272 assert
!(needs_thin_lto
.is_empty());
273 let lto_module
= B
::run_fat_lto(
278 .unwrap_or_else(|e
| e
.raise());
279 (vec
![lto_module
], vec
![])
281 assert
!(needs_fat_lto
.is_empty());
282 B
::run_thin_lto(cgcx
, needs_thin_lto
, import_only_modules
)
283 .unwrap_or_else(|e
| e
.raise())
286 let result
= lto_modules
.into_iter().map(|module
| {
287 let cost
= module
.cost();
288 (WorkItem
::LTO(module
), cost
)
289 }).chain(copy_jobs
.into_iter().map(|wp
| {
290 (WorkItem
::CopyPostLtoArtifacts(CachedModuleCodegen
{
291 name
: wp
.cgu_name
.clone(),
299 pub struct CompiledModules
{
300 pub modules
: Vec
<CompiledModule
>,
301 pub metadata_module
: Option
<CompiledModule
>,
302 pub allocator_module
: Option
<CompiledModule
>,
305 fn need_crate_bitcode_for_rlib(sess
: &Session
) -> bool
{
306 sess
.crate_types
.borrow().contains(&config
::CrateType
::Rlib
) &&
307 sess
.opts
.output_types
.contains_key(&OutputType
::Exe
)
310 fn need_pre_lto_bitcode_for_incr_comp(sess
: &Session
) -> bool
{
311 if sess
.opts
.incremental
.is_none() {
319 Lto
::ThinLocal
=> true,
323 pub fn start_async_codegen
<B
: ExtraBackendMethods
>(
326 metadata
: EncodedMetadata
,
328 ) -> OngoingCodegen
<B
> {
329 let (coordinator_send
, coordinator_receive
) = channel();
332 let crate_name
= tcx
.crate_name(LOCAL_CRATE
);
333 let crate_hash
= tcx
.crate_hash(LOCAL_CRATE
);
334 let no_builtins
= attr
::contains_name(&tcx
.hir().krate().attrs
, sym
::no_builtins
);
335 let subsystem
= attr
::first_attr_value_str_by_name(&tcx
.hir().krate().attrs
,
336 sym
::windows_subsystem
);
337 let windows_subsystem
= subsystem
.map(|subsystem
| {
338 if subsystem
!= sym
::windows
&& subsystem
!= sym
::console
{
339 tcx
.sess
.fatal(&format
!("invalid windows subsystem `{}`, only \
340 `windows` and `console` are allowed",
343 subsystem
.to_string()
346 let linker_info
= LinkerInfo
::new(tcx
);
347 let crate_info
= CrateInfo
::new(tcx
);
349 // Figure out what we actually need to build.
350 let mut modules_config
= ModuleConfig
::new(sess
.opts
.cg
.passes
.clone());
351 let mut metadata_config
= ModuleConfig
::new(vec
![]);
352 let mut allocator_config
= ModuleConfig
::new(vec
![]);
354 if sess
.opts
.debugging_opts
.profile
{
355 modules_config
.passes
.push("insert-gcov-profiling".to_owned())
358 modules_config
.pgo_gen
= sess
.opts
.cg
.profile_generate
.clone();
359 modules_config
.pgo_use
= sess
.opts
.cg
.profile_use
.clone();
360 modules_config
.sanitizer
= sess
.opts
.debugging_opts
.sanitizer
.clone();
361 modules_config
.sanitizer_recover
= sess
.opts
.debugging_opts
.sanitizer_recover
.clone();
362 modules_config
.sanitizer_memory_track_origins
=
363 sess
.opts
.debugging_opts
.sanitizer_memory_track_origins
;
364 modules_config
.opt_level
= Some(sess
.opts
.optimize
);
365 modules_config
.opt_size
= Some(sess
.opts
.optimize
);
367 // Save all versions of the bytecode if we're saving our temporaries.
368 if sess
.opts
.cg
.save_temps
{
369 modules_config
.emit_no_opt_bc
= true;
370 modules_config
.emit_pre_lto_bc
= true;
371 modules_config
.emit_bc
= true;
372 modules_config
.emit_lto_bc
= true;
373 metadata_config
.emit_bc
= true;
374 allocator_config
.emit_bc
= true;
377 // Emit compressed bitcode files for the crate if we're emitting an rlib.
378 // Whenever an rlib is created, the bitcode is inserted into the archive in
379 // order to allow LTO against it.
380 if need_crate_bitcode_for_rlib(sess
) {
381 modules_config
.emit_bc_compressed
= true;
382 allocator_config
.emit_bc_compressed
= true;
385 modules_config
.emit_pre_lto_bc
=
386 need_pre_lto_bitcode_for_incr_comp(sess
);
388 modules_config
.no_integrated_as
= tcx
.sess
.opts
.cg
.no_integrated_as
||
389 tcx
.sess
.target
.target
.options
.no_integrated_as
;
391 for output_type
in sess
.opts
.output_types
.keys() {
393 OutputType
::Bitcode
=> { modules_config.emit_bc = true; }
394 OutputType
::LlvmAssembly
=> { modules_config.emit_ir = true; }
395 OutputType
::Assembly
=> {
396 modules_config
.emit_asm
= true;
397 // If we're not using the LLVM assembler, this function
398 // could be invoked specially with output_type_assembly, so
399 // in this case we still want the metadata object file.
400 if !sess
.opts
.output_types
.contains_key(&OutputType
::Assembly
) {
401 metadata_config
.emit_obj
= true;
402 allocator_config
.emit_obj
= true;
405 OutputType
::Object
=> { modules_config.emit_obj = true; }
406 OutputType
::Metadata
=> { metadata_config.emit_obj = true; }
408 modules_config
.emit_obj
= true;
409 metadata_config
.emit_obj
= true;
410 allocator_config
.emit_obj
= true;
412 OutputType
::Mir
=> {}
413 OutputType
::DepInfo
=> {}
417 modules_config
.set_flags(sess
, no_builtins
);
418 metadata_config
.set_flags(sess
, no_builtins
);
419 allocator_config
.set_flags(sess
, no_builtins
);
421 // Exclude metadata and allocator modules from time_passes output, since
422 // they throw off the "LLVM passes" measurement.
423 metadata_config
.time_passes
= false;
424 allocator_config
.time_passes
= false;
426 let (shared_emitter
, shared_emitter_main
) = SharedEmitter
::new();
427 let (codegen_worker_send
, codegen_worker_receive
) = channel();
429 let coordinator_thread
= start_executing_work(backend
.clone(),
436 sess
.jobserver
.clone(),
437 Arc
::new(modules_config
),
438 Arc
::new(metadata_config
),
439 Arc
::new(allocator_config
),
440 coordinator_send
.clone());
452 codegen_worker_receive
,
454 future
: coordinator_thread
,
455 output_filenames
: tcx
.output_filenames(LOCAL_CRATE
),
459 fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
461 compiled_modules
: &CompiledModules
,
462 ) -> FxHashMap
<WorkProductId
, WorkProduct
> {
463 let mut work_products
= FxHashMap
::default();
465 if sess
.opts
.incremental
.is_none() {
466 return work_products
;
469 for module
in compiled_modules
.modules
.iter().filter(|m
| m
.kind
== ModuleKind
::Regular
) {
470 let mut files
= vec
![];
472 if let Some(ref path
) = module
.object
{
473 files
.push((WorkProductFileKind
::Object
, path
.clone()));
475 if let Some(ref path
) = module
.bytecode
{
476 files
.push((WorkProductFileKind
::Bytecode
, path
.clone()));
478 if let Some(ref path
) = module
.bytecode_compressed
{
479 files
.push((WorkProductFileKind
::BytecodeCompressed
, path
.clone()));
482 if let Some((id
, product
)) =
483 copy_cgu_workproducts_to_incr_comp_cache_dir(sess
, &module
.name
, &files
) {
484 work_products
.insert(id
, product
);
491 fn produce_final_output_artifacts(sess
: &Session
,
492 compiled_modules
: &CompiledModules
,
493 crate_output
: &OutputFilenames
) {
494 let mut user_wants_bitcode
= false;
495 let mut user_wants_objects
= false;
497 // Produce final compile outputs.
498 let copy_gracefully
= |from
: &Path
, to
: &Path
| {
499 if let Err(e
) = fs
::copy(from
, to
) {
500 sess
.err(&format
!("could not copy {:?} to {:?}: {}", from
, to
, e
));
504 let copy_if_one_unit
= |output_type
: OutputType
,
505 keep_numbered
: bool
| {
506 if compiled_modules
.modules
.len() == 1 {
507 // 1) Only one codegen unit. In this case it's no difficulty
508 // to copy `foo.0.x` to `foo.x`.
509 let module_name
= Some(&compiled_modules
.modules
[0].name
[..]);
510 let path
= crate_output
.temp_path(output_type
, module_name
);
511 copy_gracefully(&path
,
512 &crate_output
.path(output_type
));
513 if !sess
.opts
.cg
.save_temps
&& !keep_numbered
{
514 // The user just wants `foo.x`, not `foo.#module-name#.x`.
518 let ext
= crate_output
.temp_path(output_type
, None
)
525 if crate_output
.outputs
.contains_key(&output_type
) {
526 // 2) Multiple codegen units, with `--emit foo=some_name`. We have
527 // no good solution for this case, so warn the user.
528 sess
.warn(&format
!("ignoring emit path because multiple .{} files \
529 were produced", ext
));
530 } else if crate_output
.single_output_file
.is_some() {
531 // 3) Multiple codegen units, with `-o some_name`. We have
532 // no good solution for this case, so warn the user.
533 sess
.warn(&format
!("ignoring -o because multiple .{} files \
534 were produced", ext
));
536 // 4) Multiple codegen units, but no explicit name. We
537 // just leave the `foo.0.x` files in place.
538 // (We don't have to do any work in this case.)
543 // Flag to indicate whether the user explicitly requested bitcode.
544 // Otherwise, we produced it only as a temporary output, and will need
546 for output_type
in crate_output
.outputs
.keys() {
548 OutputType
::Bitcode
=> {
549 user_wants_bitcode
= true;
550 // Copy to .bc, but always keep the .0.bc. There is a later
551 // check to figure out if we should delete .0.bc files, or keep
552 // them for making an rlib.
553 copy_if_one_unit(OutputType
::Bitcode
, true);
555 OutputType
::LlvmAssembly
=> {
556 copy_if_one_unit(OutputType
::LlvmAssembly
, false);
558 OutputType
::Assembly
=> {
559 copy_if_one_unit(OutputType
::Assembly
, false);
561 OutputType
::Object
=> {
562 user_wants_objects
= true;
563 copy_if_one_unit(OutputType
::Object
, true);
566 OutputType
::Metadata
|
568 OutputType
::DepInfo
=> {}
572 // Clean up unwanted temporary files.
574 // We create the following files by default:
575 // - #crate#.#module-name#.bc
576 // - #crate#.#module-name#.o
577 // - #crate#.crate.metadata.bc
578 // - #crate#.crate.metadata.o
579 // - #crate#.o (linked from crate.##.o)
580 // - #crate#.bc (copied from crate.##.bc)
581 // We may create additional files if requested by the user (through
582 // `-C save-temps` or `--emit=` flags).
584 if !sess
.opts
.cg
.save_temps
{
585 // Remove the temporary .#module-name#.o objects. If the user didn't
586 // explicitly request bitcode (with --emit=bc), and the bitcode is not
587 // needed for building an rlib, then we must remove .#module-name#.bc as
590 // Specific rules for keeping .#module-name#.bc:
591 // - If the user requested bitcode (`user_wants_bitcode`), and
592 // codegen_units > 1, then keep it.
593 // - If the user requested bitcode but codegen_units == 1, then we
594 // can toss .#module-name#.bc because we copied it to .bc earlier.
595 // - If we're not building an rlib and the user didn't request
596 // bitcode, then delete .#module-name#.bc.
597 // If you change how this works, also update back::link::link_rlib,
598 // where .#module-name#.bc files are (maybe) deleted after making an
600 let needs_crate_object
= crate_output
.outputs
.contains_key(&OutputType
::Exe
);
602 let keep_numbered_bitcode
= user_wants_bitcode
&& sess
.codegen_units() > 1;
604 let keep_numbered_objects
= needs_crate_object
||
605 (user_wants_objects
&& sess
.codegen_units() > 1);
607 for module
in compiled_modules
.modules
.iter() {
608 if let Some(ref path
) = module
.object
{
609 if !keep_numbered_objects
{
614 if let Some(ref path
) = module
.bytecode
{
615 if !keep_numbered_bitcode
{
621 if !user_wants_bitcode
{
622 if let Some(ref metadata_module
) = compiled_modules
.metadata_module
{
623 if let Some(ref path
) = metadata_module
.bytecode
{
628 if let Some(ref allocator_module
) = compiled_modules
.allocator_module
{
629 if let Some(ref path
) = allocator_module
.bytecode
{
636 // We leave the following files around by default:
638 // - #crate#.crate.metadata.o
640 // These are used in linking steps and will be cleaned up afterward.
643 pub fn dump_incremental_data(_codegen_results
: &CodegenResults
) {
644 // FIXME(mw): This does not work at the moment because the situation has
645 // become more complicated due to incremental LTO. Now a CGU
646 // can have more than two caching states.
647 // println!("[incremental] Re-using {} out of {} modules",
648 // codegen_results.modules.iter().filter(|m| m.pre_existing).count(),
649 // codegen_results.modules.len());
652 pub enum WorkItem
<B
: WriteBackendMethods
> {
653 /// Optimize a newly codegened, totally unoptimized module.
654 Optimize(ModuleCodegen
<B
::Module
>),
655 /// Copy the post-LTO artifacts from the incremental cache to the output
657 CopyPostLtoArtifacts(CachedModuleCodegen
),
658 /// Performs (Thin)LTO on the given module.
659 LTO(lto
::LtoModuleCodegen
<B
>),
662 impl<B
: WriteBackendMethods
> WorkItem
<B
> {
663 pub fn module_kind(&self) -> ModuleKind
{
665 WorkItem
::Optimize(ref m
) => m
.kind
,
666 WorkItem
::CopyPostLtoArtifacts(_
) |
667 WorkItem
::LTO(_
) => ModuleKind
::Regular
,
671 fn profiling_event_id(&self) -> &'
static str {
673 WorkItem
::Optimize(_
) => "codegen_module_optimize",
674 WorkItem
::CopyPostLtoArtifacts(_
) => "codegen_copy_artifacts_from_incr_cache",
675 WorkItem
::LTO(_
) => "codegen_module_perform_lto",
680 enum WorkItemResult
<B
: WriteBackendMethods
> {
681 Compiled(CompiledModule
),
682 NeedsFatLTO(FatLTOInput
<B
>),
683 NeedsThinLTO(String
, B
::ThinBuffer
),
686 pub enum FatLTOInput
<B
: WriteBackendMethods
> {
689 buffer
: B
::ModuleBuffer
,
691 InMemory(ModuleCodegen
<B
::Module
>),
694 fn execute_work_item
<B
: ExtraBackendMethods
>(
695 cgcx
: &CodegenContext
<B
>,
696 work_item
: WorkItem
<B
>,
697 ) -> Result
<WorkItemResult
<B
>, FatalError
> {
698 let module_config
= cgcx
.config(work_item
.module_kind());
701 WorkItem
::Optimize(module
) => {
702 execute_optimize_work_item(cgcx
, module
, module_config
)
704 WorkItem
::CopyPostLtoArtifacts(module
) => {
705 execute_copy_from_cache_work_item(cgcx
, module
, module_config
)
707 WorkItem
::LTO(module
) => {
708 execute_lto_work_item(cgcx
, module
, module_config
)
713 // Actual LTO type we end up chosing based on multiple factors.
714 enum ComputedLtoType
{
720 fn execute_optimize_work_item
<B
: ExtraBackendMethods
>(
721 cgcx
: &CodegenContext
<B
>,
722 module
: ModuleCodegen
<B
::Module
>,
723 module_config
: &ModuleConfig
,
724 ) -> Result
<WorkItemResult
<B
>, FatalError
> {
725 let diag_handler
= cgcx
.create_diag_handler();
728 B
::optimize(cgcx
, &diag_handler
, &module
, module_config
)?
;
731 // After we've done the initial round of optimizations we need to
732 // decide whether to synchronously codegen this module or ship it
733 // back to the coordinator thread for further LTO processing (which
734 // has to wait for all the initial modules to be optimized).
736 // If the linker does LTO, we don't have to do it. Note that we
737 // keep doing full LTO, if it is requested, as not to break the
738 // assumption that the output will be a single module.
739 let linker_does_lto
= cgcx
.opts
.cg
.linker_plugin_lto
.enabled();
741 // When we're automatically doing ThinLTO for multi-codegen-unit
742 // builds we don't actually want to LTO the allocator modules if
743 // it shows up. This is due to various linker shenanigans that
744 // we'll encounter later.
745 let is_allocator
= module
.kind
== ModuleKind
::Allocator
;
747 // We ignore a request for full crate grath LTO if the cate type
748 // is only an rlib, as there is no full crate graph to process,
749 // that'll happen later.
751 // This use case currently comes up primarily for targets that
752 // require LTO so the request for LTO is always unconditionally
753 // passed down to the backend, but we don't actually want to do
754 // anything about it yet until we've got a final product.
755 let is_rlib
= cgcx
.crate_types
.len() == 1
756 && cgcx
.crate_types
[0] == config
::CrateType
::Rlib
;
758 // Metadata modules never participate in LTO regardless of the lto
760 let lto_type
= if module
.kind
== ModuleKind
::Metadata
{
764 Lto
::ThinLocal
if !linker_does_lto
&& !is_allocator
765 => ComputedLtoType
::Thin
,
766 Lto
::Thin
if !linker_does_lto
&& !is_rlib
767 => ComputedLtoType
::Thin
,
768 Lto
::Fat
if !is_rlib
=> ComputedLtoType
::Fat
,
769 _
=> ComputedLtoType
::No
,
773 // If we're doing some form of incremental LTO then we need to be sure to
774 // save our module to disk first.
775 let bitcode
= if cgcx
.config(module
.kind
).emit_pre_lto_bc
{
776 let filename
= pre_lto_bitcode_filename(&module
.name
);
777 cgcx
.incr_comp_session_dir
.as_ref().map(|path
| path
.join(&filename
))
783 ComputedLtoType
::No
=> {
784 let module
= unsafe {
785 B
::codegen(cgcx
, &diag_handler
, module
, module_config
)?
787 WorkItemResult
::Compiled(module
)
789 ComputedLtoType
::Thin
=> {
790 let (name
, thin_buffer
) = B
::prepare_thin(module
);
791 if let Some(path
) = bitcode
{
792 fs
::write(&path
, thin_buffer
.data()).unwrap_or_else(|e
| {
793 panic
!("Error writing pre-lto-bitcode file `{}`: {}",
798 WorkItemResult
::NeedsThinLTO(name
, thin_buffer
)
800 ComputedLtoType
::Fat
=> {
803 let (name
, buffer
) = B
::serialize_module(module
);
804 fs
::write(&path
, buffer
.data()).unwrap_or_else(|e
| {
805 panic
!("Error writing pre-lto-bitcode file `{}`: {}",
809 WorkItemResult
::NeedsFatLTO(FatLTOInput
::Serialized { name, buffer }
)
811 None
=> WorkItemResult
::NeedsFatLTO(FatLTOInput
::InMemory(module
)),
817 fn execute_copy_from_cache_work_item
<B
: ExtraBackendMethods
>(
818 cgcx
: &CodegenContext
<B
>,
819 module
: CachedModuleCodegen
,
820 module_config
: &ModuleConfig
,
821 ) -> Result
<WorkItemResult
<B
>, FatalError
> {
822 let incr_comp_session_dir
= cgcx
.incr_comp_session_dir
825 let mut object
= None
;
826 let mut bytecode
= None
;
827 let mut bytecode_compressed
= None
;
828 for (kind
, saved_file
) in &module
.source
.saved_files
{
829 let obj_out
= match kind
{
830 WorkProductFileKind
::Object
=> {
831 let path
= cgcx
.output_filenames
.temp_path(OutputType
::Object
,
833 object
= Some(path
.clone());
836 WorkProductFileKind
::Bytecode
=> {
837 let path
= cgcx
.output_filenames
.temp_path(OutputType
::Bitcode
,
839 bytecode
= Some(path
.clone());
842 WorkProductFileKind
::BytecodeCompressed
=> {
843 let path
= cgcx
.output_filenames
.temp_path(OutputType
::Bitcode
,
845 .with_extension(RLIB_BYTECODE_EXTENSION
);
846 bytecode_compressed
= Some(path
.clone());
850 let source_file
= in_incr_comp_dir(&incr_comp_session_dir
,
852 debug
!("copying pre-existing module `{}` from {:?} to {}",
856 if let Err(err
) = link_or_copy(&source_file
, &obj_out
) {
857 let diag_handler
= cgcx
.create_diag_handler();
858 diag_handler
.err(&format
!("unable to copy {} to {}: {}",
859 source_file
.display(),
865 assert_eq
!(object
.is_some(), module_config
.emit_obj
);
866 assert_eq
!(bytecode
.is_some(), module_config
.emit_bc
);
867 assert_eq
!(bytecode_compressed
.is_some(), module_config
.emit_bc_compressed
);
869 Ok(WorkItemResult
::Compiled(CompiledModule
{
871 kind
: ModuleKind
::Regular
,
878 fn execute_lto_work_item
<B
: ExtraBackendMethods
>(
879 cgcx
: &CodegenContext
<B
>,
880 mut module
: lto
::LtoModuleCodegen
<B
>,
881 module_config
: &ModuleConfig
,
882 ) -> Result
<WorkItemResult
<B
>, FatalError
> {
883 let diag_handler
= cgcx
.create_diag_handler();
886 let module
= module
.optimize(cgcx
)?
;
887 let module
= B
::codegen(cgcx
, &diag_handler
, module
, module_config
)?
;
888 Ok(WorkItemResult
::Compiled(module
))
892 pub enum Message
<B
: WriteBackendMethods
> {
893 Token(io
::Result
<Acquired
>),
895 result
: FatLTOInput
<B
>,
900 thin_buffer
: B
::ThinBuffer
,
904 result
: Result
<CompiledModule
, ()>,
908 llvm_work_item
: WorkItem
<B
>,
911 AddImportOnlyModule
{
912 module_data
: SerializedModule
<B
::ModuleBuffer
>,
913 work_product
: WorkProduct
,
922 code
: Option
<DiagnosticId
>,
926 #[derive(PartialEq, Clone, Copy, Debug)]
927 enum MainThreadWorkerState
{
933 fn start_executing_work
<B
: ExtraBackendMethods
>(
936 crate_info
: &CrateInfo
,
937 shared_emitter
: SharedEmitter
,
938 codegen_worker_send
: Sender
<Message
<B
>>,
939 coordinator_receive
: Receiver
<Box
<dyn Any
+ Send
>>,
942 modules_config
: Arc
<ModuleConfig
>,
943 metadata_config
: Arc
<ModuleConfig
>,
944 allocator_config
: Arc
<ModuleConfig
>,
945 tx_to_llvm_workers
: Sender
<Box
<dyn Any
+ Send
>>,
946 ) -> thread
::JoinHandle
<Result
<CompiledModules
, ()>> {
947 let coordinator_send
= tx_to_llvm_workers
;
950 // Compute the set of symbols we need to retain when doing LTO (if we need to)
951 let exported_symbols
= {
952 let mut exported_symbols
= FxHashMap
::default();
954 let copy_symbols
= |cnum
| {
955 let symbols
= tcx
.exported_symbols(cnum
)
957 .map(|&(s
, lvl
)| (s
.symbol_name(tcx
).to_string(), lvl
))
965 exported_symbols
.insert(LOCAL_CRATE
, copy_symbols(LOCAL_CRATE
));
966 Some(Arc
::new(exported_symbols
))
968 Lto
::Fat
| Lto
::Thin
=> {
969 exported_symbols
.insert(LOCAL_CRATE
, copy_symbols(LOCAL_CRATE
));
970 for &cnum
in tcx
.crates().iter() {
971 exported_symbols
.insert(cnum
, copy_symbols(cnum
));
973 Some(Arc
::new(exported_symbols
))
978 // First up, convert our jobserver into a helper thread so we can use normal
979 // mpsc channels to manage our messages and such.
980 // After we've requested tokens then we'll, when we can,
981 // get tokens on `coordinator_receive` which will
982 // get managed in the main loop below.
983 let coordinator_send2
= coordinator_send
.clone();
984 let helper
= jobserver
.into_helper_thread(move |token
| {
985 drop(coordinator_send2
.send(Box
::new(Message
::Token
::<B
>(token
))));
986 }).expect("failed to spawn helper thread");
988 let mut each_linked_rlib_for_lto
= Vec
::new();
989 drop(link
::each_linked_rlib(crate_info
, &mut |cnum
, path
| {
990 if link
::ignored_for_lto(sess
, crate_info
, cnum
) {
993 each_linked_rlib_for_lto
.push((cnum
, path
.to_path_buf()));
996 let assembler_cmd
= if modules_config
.no_integrated_as
{
997 // HACK: currently we use linker (gcc) as our assembler
998 let (linker
, flavor
) = link
::linker_and_flavor(sess
);
1000 let (name
, mut cmd
) = get_linker(sess
, &linker
, flavor
);
1001 cmd
.args(&sess
.target
.target
.options
.asm_args
);
1002 Some(Arc
::new(AssemblerCommand
{
1010 let ol
= if tcx
.sess
.opts
.debugging_opts
.no_codegen
1011 || !tcx
.sess
.opts
.output_types
.should_codegen() {
1012 // If we know that we won’t be doing codegen, create target machines without optimisation.
1013 config
::OptLevel
::No
1015 tcx
.backend_optimization_level(LOCAL_CRATE
)
1017 let cgcx
= CodegenContext
::<B
> {
1018 backend
: backend
.clone(),
1019 crate_types
: sess
.crate_types
.borrow().clone(),
1020 each_linked_rlib_for_lto
,
1022 no_landing_pads
: sess
.no_landing_pads(),
1023 fewer_names
: sess
.fewer_names(),
1024 save_temps
: sess
.opts
.cg
.save_temps
,
1025 opts
: Arc
::new(sess
.opts
.clone()),
1026 time_passes
: sess
.time_extended(),
1027 prof
: sess
.prof
.clone(),
1029 remark
: sess
.opts
.cg
.remark
.clone(),
1031 incr_comp_session_dir
: sess
.incr_comp_session_dir_opt().map(|r
| r
.clone()),
1032 cgu_reuse_tracker
: sess
.cgu_reuse_tracker
.clone(),
1034 diag_emitter
: shared_emitter
.clone(),
1035 output_filenames
: tcx
.output_filenames(LOCAL_CRATE
),
1036 regular_module_config
: modules_config
,
1037 metadata_module_config
: metadata_config
,
1038 allocator_module_config
: allocator_config
,
1039 tm_factory
: TargetMachineFactory(backend
.target_machine_factory(tcx
.sess
, ol
, false)),
1041 msvc_imps_needed
: msvc_imps_needed(tcx
),
1042 target_pointer_width
: tcx
.sess
.target
.target
.target_pointer_width
.clone(),
1043 target_arch
: tcx
.sess
.target
.target
.arch
.clone(),
1044 debuginfo
: tcx
.sess
.opts
.debuginfo
,
1048 // This is the "main loop" of parallel work happening for parallel codegen.
1049 // It's here that we manage parallelism, schedule work, and work with
1050 // messages coming from clients.
1052 // There are a few environmental pre-conditions that shape how the system
1055 // - Error reporting only can happen on the main thread because that's the
1056 // only place where we have access to the compiler `Session`.
1057 // - LLVM work can be done on any thread.
1058 // - Codegen can only happen on the main thread.
1059 // - Each thread doing substantial work most be in possession of a `Token`
1060 // from the `Jobserver`.
1061 // - The compiler process always holds one `Token`. Any additional `Tokens`
1062 // have to be requested from the `Jobserver`.
1066 // The error reporting restriction is handled separately from the rest: We
1067 // set up a `SharedEmitter` the holds an open channel to the main thread.
1068 // When an error occurs on any thread, the shared emitter will send the
1069 // error message to the receiver main thread (`SharedEmitterMain`). The
1070 // main thread will periodically query this error message queue and emit
1071 // any error messages it has received. It might even abort compilation if
1072 // has received a fatal error. In this case we rely on all other threads
1073 // being torn down automatically with the main thread.
1074 // Since the main thread will often be busy doing codegen work, error
1075 // reporting will be somewhat delayed, since the message queue can only be
1076 // checked in between to work packages.
1078 // Work Processing Infrastructure
1079 // ==============================
1080 // The work processing infrastructure knows three major actors:
1082 // - the coordinator thread,
1083 // - the main thread, and
1084 // - LLVM worker threads
1086 // The coordinator thread is running a message loop. It instructs the main
1087 // thread about what work to do when, and it will spawn off LLVM worker
1088 // threads as open LLVM WorkItems become available.
1090 // The job of the main thread is to codegen CGUs into LLVM work package
1091 // (since the main thread is the only thread that can do this). The main
1092 // thread will block until it receives a message from the coordinator, upon
1093 // which it will codegen one CGU, send it to the coordinator and block
1094 // again. This way the coordinator can control what the main thread is
1097 // The coordinator keeps a queue of LLVM WorkItems, and when a `Token` is
1098 // available, it will spawn off a new LLVM worker thread and let it process
1099 // that a WorkItem. When a LLVM worker thread is done with its WorkItem,
1100 // it will just shut down, which also frees all resources associated with
1101 // the given LLVM module, and sends a message to the coordinator that the
1102 // has been completed.
1106 // The scheduler's goal is to minimize the time it takes to complete all
1107 // work there is, however, we also want to keep memory consumption low
1108 // if possible. These two goals are at odds with each other: If memory
1109 // consumption were not an issue, we could just let the main thread produce
1110 // LLVM WorkItems at full speed, assuring maximal utilization of
1111 // Tokens/LLVM worker threads. However, since codegen usual is faster
1112 // than LLVM processing, the queue of LLVM WorkItems would fill up and each
1113 // WorkItem potentially holds on to a substantial amount of memory.
1115 // So the actual goal is to always produce just enough LLVM WorkItems as
1116 // not to starve our LLVM worker threads. That means, once we have enough
1117 // WorkItems in our queue, we can block the main thread, so it does not
1118 // produce more until we need them.
1120 // Doing LLVM Work on the Main Thread
1121 // ----------------------------------
1122 // Since the main thread owns the compiler processes implicit `Token`, it is
1123 // wasteful to keep it blocked without doing any work. Therefore, what we do
1124 // in this case is: We spawn off an additional LLVM worker thread that helps
1125 // reduce the queue. The work it is doing corresponds to the implicit
1126 // `Token`. The coordinator will mark the main thread as being busy with
1127 // LLVM work. (The actual work happens on another OS thread but we just care
1128 // about `Tokens`, not actual threads).
1130 // When any LLVM worker thread finishes while the main thread is marked as
1131 // "busy with LLVM work", we can do a little switcheroo: We give the Token
1132 // of the just finished thread to the LLVM worker thread that is working on
1133 // behalf of the main thread's implicit Token, thus freeing up the main
1134 // thread again. The coordinator can then again decide what the main thread
1135 // should do. This allows the coordinator to make decisions at more points
1138 // Striking a Balance between Throughput and Memory Consumption
1139 // ------------------------------------------------------------
1140 // Since our two goals, (1) use as many Tokens as possible and (2) keep
1141 // memory consumption as low as possible, are in conflict with each other,
1142 // we have to find a trade off between them. Right now, the goal is to keep
1143 // all workers busy, which means that no worker should find the queue empty
1144 // when it is ready to start.
1145 // How do we do achieve this? Good question :) We actually never know how
1146 // many `Tokens` are potentially available so it's hard to say how much to
1147 // fill up the queue before switching the main thread to LLVM work. Also we
1148 // currently don't have a means to estimate how long a running LLVM worker
1149 // will still be busy with it's current WorkItem. However, we know the
1150 // maximal count of available Tokens that makes sense (=the number of CPU
1151 // cores), so we can take a conservative guess. The heuristic we use here
1152 // is implemented in the `queue_full_enough()` function.
1154 // Some Background on Jobservers
1155 // -----------------------------
1156 // It's worth also touching on the management of parallelism here. We don't
1157 // want to just spawn a thread per work item because while that's optimal
1158 // parallelism it may overload a system with too many threads or violate our
1159 // configuration for the maximum amount of cpu to use for this process. To
1160 // manage this we use the `jobserver` crate.
1162 // Job servers are an artifact of GNU make and are used to manage
1163 // parallelism between processes. A jobserver is a glorified IPC semaphore
1164 // basically. Whenever we want to run some work we acquire the semaphore,
1165 // and whenever we're done with that work we release the semaphore. In this
1166 // manner we can ensure that the maximum number of parallel workers is
1167 // capped at any one point in time.
1169 // LTO and the coordinator thread
1170 // ------------------------------
1172 // The final job the coordinator thread is responsible for is managing LTO
1173 // and how that works. When LTO is requested what we'll to is collect all
1174 // optimized LLVM modules into a local vector on the coordinator. Once all
1175 // modules have been codegened and optimized we hand this to the `lto`
1176 // module for further optimization. The `lto` module will return back a list
1177 // of more modules to work on, which the coordinator will continue to spawn
1180 // Each LLVM module is automatically sent back to the coordinator for LTO if
1181 // necessary. There's already optimizations in place to avoid sending work
1182 // back to the coordinator if LTO isn't requested.
1183 return thread
::spawn(move || {
1184 // We pretend to be within the top-level LLVM time-passes task here:
1187 let max_workers
= ::num_cpus
::get();
1188 let mut worker_id_counter
= 0;
1189 let mut free_worker_ids
= Vec
::new();
1190 let mut get_worker_id
= |free_worker_ids
: &mut Vec
<usize>| {
1191 if let Some(id
) = free_worker_ids
.pop() {
1194 let id
= worker_id_counter
;
1195 worker_id_counter
+= 1;
1200 // This is where we collect codegen units that have gone all the way
1201 // through codegen and LLVM.
1202 let mut compiled_modules
= vec
![];
1203 let mut compiled_metadata_module
= None
;
1204 let mut compiled_allocator_module
= None
;
1205 let mut needs_fat_lto
= Vec
::new();
1206 let mut needs_thin_lto
= Vec
::new();
1207 let mut lto_import_only_modules
= Vec
::new();
1208 let mut started_lto
= false;
1209 let mut codegen_aborted
= false;
1211 // This flag tracks whether all items have gone through codegens
1212 let mut codegen_done
= false;
1214 // This is the queue of LLVM work items that still need processing.
1215 let mut work_items
= Vec
::<(WorkItem
<B
>, u64)>::new();
1217 // This are the Jobserver Tokens we currently hold. Does not include
1218 // the implicit Token the compiler process owns no matter what.
1219 let mut tokens
= Vec
::new();
1221 let mut main_thread_worker_state
= MainThreadWorkerState
::Idle
;
1222 let mut running
= 0;
1224 let mut llvm_start_time
= None
;
1226 // Run the message loop while there's still anything that needs message
1227 // processing. Note that as soon as codegen is aborted we simply want to
1228 // wait for all existing work to finish, so many of the conditions here
1229 // only apply if codegen hasn't been aborted as they represent pending
1231 while !codegen_done
||
1233 (!codegen_aborted
&& (
1234 work_items
.len() > 0 ||
1235 needs_fat_lto
.len() > 0 ||
1236 needs_thin_lto
.len() > 0 ||
1237 lto_import_only_modules
.len() > 0 ||
1238 main_thread_worker_state
!= MainThreadWorkerState
::Idle
1242 // While there are still CGUs to be codegened, the coordinator has
1243 // to decide how to utilize the compiler processes implicit Token:
1244 // For codegenning more CGU or for running them through LLVM.
1246 if main_thread_worker_state
== MainThreadWorkerState
::Idle
{
1247 if !queue_full_enough(work_items
.len(), running
, max_workers
) {
1248 // The queue is not full enough, codegen more items:
1249 if let Err(_
) = codegen_worker_send
.send(Message
::CodegenItem
) {
1250 panic
!("Could not send Message::CodegenItem to main thread")
1252 main_thread_worker_state
= MainThreadWorkerState
::Codegenning
;
1254 // The queue is full enough to not let the worker
1255 // threads starve. Use the implicit Token to do some
1257 let (item
, _
) = work_items
.pop()
1258 .expect("queue empty - queue_full_enough() broken?");
1259 let cgcx
= CodegenContext
{
1260 worker
: get_worker_id(&mut free_worker_ids
),
1263 maybe_start_llvm_timer(cgcx
.config(item
.module_kind()),
1264 &mut llvm_start_time
);
1265 main_thread_worker_state
= MainThreadWorkerState
::LLVMing
;
1266 spawn_work(cgcx
, item
);
1269 } else if codegen_aborted
{
1270 // don't queue up any more work if codegen was aborted, we're
1271 // just waiting for our existing children to finish
1273 // If we've finished everything related to normal codegen
1274 // then it must be the case that we've got some LTO work to do.
1275 // Perform the serial work here of figuring out what we're
1276 // going to LTO and then push a bunch of work items onto our
1278 if work_items
.len() == 0 &&
1280 main_thread_worker_state
== MainThreadWorkerState
::Idle
{
1281 assert
!(!started_lto
);
1284 let needs_fat_lto
= mem
::take(&mut needs_fat_lto
);
1285 let needs_thin_lto
= mem
::take(&mut needs_thin_lto
);
1286 let import_only_modules
= mem
::take(&mut lto_import_only_modules
);
1288 for (work
, cost
) in generate_lto_work(&cgcx
, needs_fat_lto
,
1289 needs_thin_lto
, import_only_modules
) {
1290 let insertion_index
= work_items
1291 .binary_search_by_key(&cost
, |&(_
, cost
)| cost
)
1292 .unwrap_or_else(|e
| e
);
1293 work_items
.insert(insertion_index
, (work
, cost
));
1294 if !cgcx
.opts
.debugging_opts
.no_parallel_llvm
{
1295 helper
.request_token();
1300 // In this branch, we know that everything has been codegened,
1301 // so it's just a matter of determining whether the implicit
1302 // Token is free to use for LLVM work.
1303 match main_thread_worker_state
{
1304 MainThreadWorkerState
::Idle
=> {
1305 if let Some((item
, _
)) = work_items
.pop() {
1306 let cgcx
= CodegenContext
{
1307 worker
: get_worker_id(&mut free_worker_ids
),
1310 maybe_start_llvm_timer(cgcx
.config(item
.module_kind()),
1311 &mut llvm_start_time
);
1312 main_thread_worker_state
= MainThreadWorkerState
::LLVMing
;
1313 spawn_work(cgcx
, item
);
1315 // There is no unstarted work, so let the main thread
1316 // take over for a running worker. Otherwise the
1317 // implicit token would just go to waste.
1318 // We reduce the `running` counter by one. The
1319 // `tokens.truncate()` below will take care of
1320 // giving the Token back.
1321 debug_assert
!(running
> 0);
1323 main_thread_worker_state
= MainThreadWorkerState
::LLVMing
;
1326 MainThreadWorkerState
::Codegenning
=> {
1327 bug
!("codegen worker should not be codegenning after \
1328 codegen was already completed")
1330 MainThreadWorkerState
::LLVMing
=> {
1331 // Already making good use of that token
1336 // Spin up what work we can, only doing this while we've got available
1337 // parallelism slots and work left to spawn.
1338 while !codegen_aborted
&& work_items
.len() > 0 && running
< tokens
.len() {
1339 let (item
, _
) = work_items
.pop().unwrap();
1341 maybe_start_llvm_timer(cgcx
.config(item
.module_kind()),
1342 &mut llvm_start_time
);
1344 let cgcx
= CodegenContext
{
1345 worker
: get_worker_id(&mut free_worker_ids
),
1349 spawn_work(cgcx
, item
);
1353 // Relinquish accidentally acquired extra tokens
1354 tokens
.truncate(running
);
1356 // If a thread exits successfully then we drop a token associated
1357 // with that worker and update our `running` count. We may later
1358 // re-acquire a token to continue running more work. We may also not
1359 // actually drop a token here if the worker was running with an
1360 // "ephemeral token"
1361 let mut free_worker
= |worker_id
| {
1362 if main_thread_worker_state
== MainThreadWorkerState
::LLVMing
{
1363 main_thread_worker_state
= MainThreadWorkerState
::Idle
;
1368 free_worker_ids
.push(worker_id
);
1371 let msg
= coordinator_receive
.recv().unwrap();
1372 match *msg
.downcast
::<Message
<B
>>().ok().unwrap() {
1373 // Save the token locally and the next turn of the loop will use
1374 // this to spawn a new unit of work, or it may get dropped
1375 // immediately if we have no more work to spawn.
1376 Message
::Token(token
) => {
1381 if main_thread_worker_state
== MainThreadWorkerState
::LLVMing
{
1382 // If the main thread token is used for LLVM work
1383 // at the moment, we turn that thread into a regular
1384 // LLVM worker thread, so the main thread is free
1385 // to react to codegen demand.
1386 main_thread_worker_state
= MainThreadWorkerState
::Idle
;
1391 let msg
= &format
!("failed to acquire jobserver token: {}", e
);
1392 shared_emitter
.fatal(msg
);
1393 // Exit the coordinator thread
1399 Message
::CodegenDone { llvm_work_item, cost }
=> {
1400 // We keep the queue sorted by estimated processing cost,
1401 // so that more expensive items are processed earlier. This
1402 // is good for throughput as it gives the main thread more
1403 // time to fill up the queue and it avoids scheduling
1404 // expensive items to the end.
1405 // Note, however, that this is not ideal for memory
1406 // consumption, as LLVM module sizes are not evenly
1408 let insertion_index
=
1409 work_items
.binary_search_by_key(&cost
, |&(_
, cost
)| cost
);
1410 let insertion_index
= match insertion_index
{
1411 Ok(idx
) | Err(idx
) => idx
1413 work_items
.insert(insertion_index
, (llvm_work_item
, cost
));
1415 if !cgcx
.opts
.debugging_opts
.no_parallel_llvm
{
1416 helper
.request_token();
1418 assert
!(!codegen_aborted
);
1419 assert_eq
!(main_thread_worker_state
,
1420 MainThreadWorkerState
::Codegenning
);
1421 main_thread_worker_state
= MainThreadWorkerState
::Idle
;
1424 Message
::CodegenComplete
=> {
1425 codegen_done
= true;
1426 assert
!(!codegen_aborted
);
1427 assert_eq
!(main_thread_worker_state
,
1428 MainThreadWorkerState
::Codegenning
);
1429 main_thread_worker_state
= MainThreadWorkerState
::Idle
;
1432 // If codegen is aborted that means translation was aborted due
1433 // to some normal-ish compiler error. In this situation we want
1434 // to exit as soon as possible, but we want to make sure all
1435 // existing work has finished. Flag codegen as being done, and
1436 // then conditions above will ensure no more work is spawned but
1437 // we'll keep executing this loop until `running` hits 0.
1438 Message
::CodegenAborted
=> {
1439 assert
!(!codegen_aborted
);
1440 codegen_done
= true;
1441 codegen_aborted
= true;
1442 assert_eq
!(main_thread_worker_state
,
1443 MainThreadWorkerState
::Codegenning
);
1445 Message
::Done { result: Ok(compiled_module), worker_id }
=> {
1446 free_worker(worker_id
);
1447 match compiled_module
.kind
{
1448 ModuleKind
::Regular
=> {
1449 compiled_modules
.push(compiled_module
);
1451 ModuleKind
::Metadata
=> {
1452 assert
!(compiled_metadata_module
.is_none());
1453 compiled_metadata_module
= Some(compiled_module
);
1455 ModuleKind
::Allocator
=> {
1456 assert
!(compiled_allocator_module
.is_none());
1457 compiled_allocator_module
= Some(compiled_module
);
1461 Message
::NeedsFatLTO { result, worker_id }
=> {
1462 assert
!(!started_lto
);
1463 free_worker(worker_id
);
1464 needs_fat_lto
.push(result
);
1466 Message
::NeedsThinLTO { name, thin_buffer, worker_id }
=> {
1467 assert
!(!started_lto
);
1468 free_worker(worker_id
);
1469 needs_thin_lto
.push((name
, thin_buffer
));
1471 Message
::AddImportOnlyModule { module_data, work_product }
=> {
1472 assert
!(!started_lto
);
1473 assert
!(!codegen_done
);
1474 assert_eq
!(main_thread_worker_state
,
1475 MainThreadWorkerState
::Codegenning
);
1476 lto_import_only_modules
.push((module_data
, work_product
));
1477 main_thread_worker_state
= MainThreadWorkerState
::Idle
;
1479 // If the thread failed that means it panicked, so we abort immediately.
1480 Message
::Done { result: Err(()), worker_id: _ }
=> {
1481 bug
!("worker thread panicked");
1483 Message
::CodegenItem
=> {
1484 bug
!("the coordinator should not receive codegen requests")
1489 if let Some(llvm_start_time
) = llvm_start_time
{
1490 let total_llvm_time
= Instant
::now().duration_since(llvm_start_time
);
1491 // This is the top-level timing for all of LLVM, set the time-depth
1494 print_time_passes_entry(cgcx
.time_passes
,
1499 // Regardless of what order these modules completed in, report them to
1500 // the backend in the same order every time to ensure that we're handing
1501 // out deterministic results.
1502 compiled_modules
.sort_by(|a
, b
| a
.name
.cmp(&b
.name
));
1504 Ok(CompiledModules
{
1505 modules
: compiled_modules
,
1506 metadata_module
: compiled_metadata_module
,
1507 allocator_module
: compiled_allocator_module
,
1511 // A heuristic that determines if we have enough LLVM WorkItems in the
1512 // queue so that the main thread can do LLVM work instead of codegen
1513 fn queue_full_enough(items_in_queue
: usize,
1514 workers_running
: usize,
1515 max_workers
: usize) -> bool
{
1517 items_in_queue
> 0 &&
1518 items_in_queue
>= max_workers
.saturating_sub(workers_running
/ 2)
1521 fn maybe_start_llvm_timer(config
: &ModuleConfig
,
1522 llvm_start_time
: &mut Option
<Instant
>) {
1523 // We keep track of the -Ztime-passes output manually,
1524 // since the closure-based interface does not fit well here.
1525 if config
.time_passes
{
1526 if llvm_start_time
.is_none() {
1527 *llvm_start_time
= Some(Instant
::now());
1533 pub const CODEGEN_WORKER_ID
: usize = ::std
::usize::MAX
;
1535 fn spawn_work
<B
: ExtraBackendMethods
>(
1536 cgcx
: CodegenContext
<B
>,
1539 let depth
= time_depth();
1541 thread
::spawn(move || {
1542 set_time_depth(depth
);
1544 // Set up a destructor which will fire off a message that we're done as
1546 struct Bomb
<B
: ExtraBackendMethods
> {
1547 coordinator_send
: Sender
<Box
<dyn Any
+ Send
>>,
1548 result
: Option
<WorkItemResult
<B
>>,
1551 impl<B
: ExtraBackendMethods
> Drop
for Bomb
<B
> {
1552 fn drop(&mut self) {
1553 let worker_id
= self.worker_id
;
1554 let msg
= match self.result
.take() {
1555 Some(WorkItemResult
::Compiled(m
)) => {
1556 Message
::Done
::<B
> { result: Ok(m), worker_id }
1558 Some(WorkItemResult
::NeedsFatLTO(m
)) => {
1559 Message
::NeedsFatLTO
::<B
> { result: m, worker_id }
1561 Some(WorkItemResult
::NeedsThinLTO(name
, thin_buffer
)) => {
1562 Message
::NeedsThinLTO
::<B
> { name, thin_buffer, worker_id }
1564 None
=> Message
::Done
::<B
> { result: Err(()), worker_id }
1566 drop(self.coordinator_send
.send(Box
::new(msg
)));
1570 let mut bomb
= Bomb
::<B
> {
1571 coordinator_send
: cgcx
.coordinator_send
.clone(),
1573 worker_id
: cgcx
.worker
,
1576 // Execute the work itself, and if it finishes successfully then flag
1577 // ourselves as a success as well.
1579 // Note that we ignore any `FatalError` coming out of `execute_work_item`,
1580 // as a diagnostic was already sent off to the main thread - just
1581 // surface that there was an error in this worker.
1583 let _prof_timer
= cgcx
.prof
.generic_activity(work
.profiling_event_id());
1584 execute_work_item(&cgcx
, work
).ok()
1589 pub fn run_assembler
<B
: ExtraBackendMethods
>(
1590 cgcx
: &CodegenContext
<B
>,
1595 let assembler
= cgcx
.assembler_cmd
1597 .expect("cgcx.assembler_cmd is missing?");
1599 let pname
= &assembler
.name
;
1600 let mut cmd
= assembler
.cmd
.clone();
1601 cmd
.arg("-c").arg("-o").arg(object
).arg(assembly
);
1602 debug
!("{:?}", cmd
);
1604 match cmd
.output() {
1606 if !prog
.status
.success() {
1607 let mut note
= prog
.stderr
.clone();
1608 note
.extend_from_slice(&prog
.stdout
);
1610 handler
.struct_err(&format
!("linking with `{}` failed: {}",
1613 .note(&format
!("{:?}", &cmd
))
1614 .note(str::from_utf8(¬e
[..]).unwrap())
1616 handler
.abort_if_errors();
1620 handler
.err(&format
!("could not exec the linker `{}`: {}", pname
.display(), e
));
1621 handler
.abort_if_errors();
1627 enum SharedEmitterMessage
{
1628 Diagnostic(Diagnostic
),
1629 InlineAsmError(u32, String
),
1635 pub struct SharedEmitter
{
1636 sender
: Sender
<SharedEmitterMessage
>,
1639 pub struct SharedEmitterMain
{
1640 receiver
: Receiver
<SharedEmitterMessage
>,
1643 impl SharedEmitter
{
1644 pub fn new() -> (SharedEmitter
, SharedEmitterMain
) {
1645 let (sender
, receiver
) = channel();
1647 (SharedEmitter { sender }
, SharedEmitterMain { receiver }
)
1650 pub fn inline_asm_error(&self, cookie
: u32, msg
: String
) {
1651 drop(self.sender
.send(SharedEmitterMessage
::InlineAsmError(cookie
, msg
)));
1654 pub fn fatal(&self, msg
: &str) {
1655 drop(self.sender
.send(SharedEmitterMessage
::Fatal(msg
.to_string())));
1659 impl Emitter
for SharedEmitter
{
1660 fn emit_diagnostic(&mut self, diag
: &rustc_errors
::Diagnostic
) {
1661 drop(self.sender
.send(SharedEmitterMessage
::Diagnostic(Diagnostic
{
1662 msg
: diag
.message(),
1663 code
: diag
.code
.clone(),
1666 for child
in &diag
.children
{
1667 drop(self.sender
.send(SharedEmitterMessage
::Diagnostic(Diagnostic
{
1668 msg
: child
.message(),
1673 drop(self.sender
.send(SharedEmitterMessage
::AbortIfErrors
));
1675 fn source_map(&self) -> Option
<&Lrc
<SourceMap
>> {
1680 impl SharedEmitterMain
{
1681 pub fn check(&self, sess
: &Session
, blocking
: bool
) {
1683 let message
= if blocking
{
1684 match self.receiver
.recv() {
1685 Ok(message
) => Ok(message
),
1689 match self.receiver
.try_recv() {
1690 Ok(message
) => Ok(message
),
1696 Ok(SharedEmitterMessage
::Diagnostic(diag
)) => {
1697 let handler
= sess
.diagnostic();
1698 let mut d
= rustc_errors
::Diagnostic
::new(diag
.lvl
, &diag
.msg
);
1699 if let Some(code
) = diag
.code
{
1702 handler
.emit_diagnostic(&d
);
1703 handler
.abort_if_errors_and_should_abort();
1705 Ok(SharedEmitterMessage
::InlineAsmError(cookie
, msg
)) => {
1706 sess
.span_err(ExpnId
::from_u32(cookie
).expn_data().call_site
, &msg
)
1708 Ok(SharedEmitterMessage
::AbortIfErrors
) => {
1709 sess
.abort_if_errors();
1711 Ok(SharedEmitterMessage
::Fatal(msg
)) => {
1723 pub struct OngoingCodegen
<B
: ExtraBackendMethods
> {
1725 pub crate_name
: Symbol
,
1726 pub crate_hash
: Svh
,
1727 pub metadata
: EncodedMetadata
,
1728 pub windows_subsystem
: Option
<String
>,
1729 pub linker_info
: LinkerInfo
,
1730 pub crate_info
: CrateInfo
,
1731 pub coordinator_send
: Sender
<Box
<dyn Any
+ Send
>>,
1732 pub codegen_worker_receive
: Receiver
<Message
<B
>>,
1733 pub shared_emitter_main
: SharedEmitterMain
,
1734 pub future
: thread
::JoinHandle
<Result
<CompiledModules
, ()>>,
1735 pub output_filenames
: Arc
<OutputFilenames
>,
1738 impl<B
: ExtraBackendMethods
> OngoingCodegen
<B
> {
1742 ) -> (CodegenResults
, FxHashMap
<WorkProductId
, WorkProduct
>) {
1743 self.shared_emitter_main
.check(sess
, true);
1744 let compiled_modules
= match self.future
.join() {
1745 Ok(Ok(compiled_modules
)) => compiled_modules
,
1747 sess
.abort_if_errors();
1748 panic
!("expected abort due to worker thread errors")
1751 bug
!("panic during codegen/LLVM phase");
1755 sess
.cgu_reuse_tracker
.check_expected_reuse(sess
.diagnostic());
1757 sess
.abort_if_errors();
1760 copy_all_cgu_workproducts_to_incr_comp_cache_dir(sess
,
1762 produce_final_output_artifacts(sess
,
1764 &self.output_filenames
);
1766 // FIXME: time_llvm_passes support - does this use a global context or
1768 if sess
.codegen_units() == 1 && sess
.time_llvm_passes() {
1769 self.backend
.print_pass_timings()
1773 crate_name
: self.crate_name
,
1774 crate_hash
: self.crate_hash
,
1775 metadata
: self.metadata
,
1776 windows_subsystem
: self.windows_subsystem
,
1777 linker_info
: self.linker_info
,
1778 crate_info
: self.crate_info
,
1780 modules
: compiled_modules
.modules
,
1781 allocator_module
: compiled_modules
.allocator_module
,
1782 metadata_module
: compiled_modules
.metadata_module
,
1786 pub fn submit_pre_codegened_module_to_llvm(
1789 module
: ModuleCodegen
<B
::Module
>,
1791 self.wait_for_signal_to_codegen_item();
1792 self.check_for_errors(tcx
.sess
);
1794 // These are generally cheap and won't throw off scheduling.
1796 submit_codegened_module_to_llvm(&self.backend
, &self.coordinator_send
, module
, cost
);
1799 pub fn codegen_finished(&self, tcx
: TyCtxt
<'_
>) {
1800 self.wait_for_signal_to_codegen_item();
1801 self.check_for_errors(tcx
.sess
);
1802 drop(self.coordinator_send
.send(Box
::new(Message
::CodegenComplete
::<B
>)));
1805 /// Consumes this context indicating that codegen was entirely aborted, and
1806 /// we need to exit as quickly as possible.
1808 /// This method blocks the current thread until all worker threads have
1809 /// finished, and all worker threads should have exited or be real close to
1810 /// exiting at this point.
1811 pub fn codegen_aborted(self) {
1812 // Signal to the coordinator it should spawn no more work and start
1814 drop(self.coordinator_send
.send(Box
::new(Message
::CodegenAborted
::<B
>)));
1815 drop(self.future
.join());
1818 pub fn check_for_errors(&self, sess
: &Session
) {
1819 self.shared_emitter_main
.check(sess
, false);
1822 pub fn wait_for_signal_to_codegen_item(&self) {
1823 match self.codegen_worker_receive
.recv() {
1824 Ok(Message
::CodegenItem
) => {
1827 Ok(_
) => panic
!("unexpected message"),
1829 // One of the LLVM threads must have panicked, fall through so
1830 // error handling can be reached.
1836 pub fn submit_codegened_module_to_llvm
<B
: ExtraBackendMethods
>(
1838 tx_to_llvm_workers
: &Sender
<Box
<dyn Any
+ Send
>>,
1839 module
: ModuleCodegen
<B
::Module
>,
1842 let llvm_work_item
= WorkItem
::Optimize(module
);
1843 drop(tx_to_llvm_workers
.send(Box
::new(Message
::CodegenDone
::<B
> {
1849 pub fn submit_post_lto_module_to_llvm
<B
: ExtraBackendMethods
>(
1851 tx_to_llvm_workers
: &Sender
<Box
<dyn Any
+ Send
>>,
1852 module
: CachedModuleCodegen
,
1854 let llvm_work_item
= WorkItem
::CopyPostLtoArtifacts(module
);
1855 drop(tx_to_llvm_workers
.send(Box
::new(Message
::CodegenDone
::<B
> {
1861 pub fn submit_pre_lto_module_to_llvm
<B
: ExtraBackendMethods
>(
1864 tx_to_llvm_workers
: &Sender
<Box
<dyn Any
+ Send
>>,
1865 module
: CachedModuleCodegen
,
1867 let filename
= pre_lto_bitcode_filename(&module
.name
);
1868 let bc_path
= in_incr_comp_dir_sess(tcx
.sess
, &filename
);
1869 let file
= fs
::File
::open(&bc_path
).unwrap_or_else(|e
| {
1870 panic
!("failed to open bitcode file `{}`: {}", bc_path
.display(), e
)
1874 memmap
::Mmap
::map(&file
).unwrap_or_else(|e
| {
1875 panic
!("failed to mmap bitcode file `{}`: {}", bc_path
.display(), e
)
1878 // Schedule the module to be loaded
1879 drop(tx_to_llvm_workers
.send(Box
::new(Message
::AddImportOnlyModule
::<B
> {
1880 module_data
: SerializedModule
::FromUncompressedFile(mmap
),
1881 work_product
: module
.source
,
1885 pub fn pre_lto_bitcode_filename(module_name
: &str) -> String
{
1886 format
!("{}.{}", module_name
, PRE_LTO_BC_EXT
)
1889 fn msvc_imps_needed(tcx
: TyCtxt
<'_
>) -> bool
{
1890 // This should never be true (because it's not supported). If it is true,
1891 // something is wrong with commandline arg validation.
1892 assert
!(!(tcx
.sess
.opts
.cg
.linker_plugin_lto
.enabled() &&
1893 tcx
.sess
.target
.target
.options
.is_like_msvc
&&
1894 tcx
.sess
.opts
.cg
.prefer_dynamic
));
1896 tcx
.sess
.target
.target
.options
.is_like_msvc
&&
1897 tcx
.sess
.crate_types
.borrow().iter().any(|ct
| *ct
== config
::CrateType
::Rlib
) &&
1898 // ThinLTO can't handle this workaround in all cases, so we don't
1899 // emit the `__imp_` symbols. Instead we make them unnecessary by disallowing
1900 // dynamic linking when linker plugin LTO is enabled.
1901 !tcx
.sess
.opts
.cg
.linker_plugin_lto
.enabled()