1 // Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use back::bytecode::{self, RLIB_BYTECODE_EXTENSION};
13 use back::lto::{self, ThinBuffer, SerializedModule};
14 use back::link::{self, get_linker, remove};
18 use rustc_incremental::{copy_cgu_workproducts_to_incr_comp_cache_dir,
19 in_incr_comp_dir, in_incr_comp_dir_sess};
20 use rustc::dep_graph::{WorkProduct, WorkProductId, WorkProductFileKind};
21 use rustc::dep_graph::cgu_reuse_tracker::CguReuseTracker;
22 use rustc::middle::cstore::EncodedMetadata;
23 use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Sanitizer, Lto};
24 use rustc::session::Session;
25 use rustc::util::nodemap::FxHashMap;
26 use time_graph::{self, TimeGraph, Timeline};
27 use llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic, BasicBlock};
29 use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
32 use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
33 use rustc::ty::TyCtxt;
34 use rustc::util::common::{time_ext, time_depth, set_time_depth, print_time_passes_entry};
35 use rustc_fs_util::{path2cstr, link_or_copy};
36 use rustc_data_structures::small_c_str::SmallCStr;
37 use rustc_data_structures::svh::Svh;
38 use rustc_codegen_utils::command::Command;
39 use rustc_codegen_utils::linker::LinkerInfo;
40 use rustc_codegen_utils::symbol_export::ExportedSymbols;
41 use errors::{self, Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
42 use errors::emitter::{Emitter};
44 use syntax::ext::hygiene::Mark;
45 use syntax_pos::MultiSpan;
46 use syntax_pos::symbol::Symbol;
48 use context::{is_pie_binary, get_reloc_model};
49 use interfaces::{Backend, CommonWriteMethods};
51 use jobserver::{Client, Acquired};
54 use std::marker::PhantomData;
57 use std::ffi::{CString, CStr};
59 use std::io::{self, Write};
61 use std::path::{Path, PathBuf};
64 use std::sync::mpsc::{channel, Sender, Receiver};
66 use std::time::Instant;
68 use libc::{c_uint, c_void, c_char, size_t};
70 pub const RELOC_MODEL_ARGS : [(&str, llvm::RelocMode); 7] = [
71 ("pic", llvm::RelocMode::PIC),
72 ("static", llvm::RelocMode::Static),
73 ("default", llvm::RelocMode::Default),
74 ("dynamic-no-pic", llvm::RelocMode::DynamicNoPic),
75 ("ropi", llvm::RelocMode::ROPI),
76 ("rwpi", llvm::RelocMode::RWPI),
77 ("ropi-rwpi", llvm::RelocMode::ROPI_RWPI),
80 pub const CODE_GEN_MODEL_ARGS: &[(&str, llvm::CodeModel)] = &[
81 ("small", llvm::CodeModel::Small),
82 ("kernel", llvm::CodeModel::Kernel),
83 ("medium", llvm::CodeModel::Medium),
84 ("large", llvm::CodeModel::Large),
87 pub const TLS_MODEL_ARGS : [(&str, llvm::ThreadLocalMode); 4] = [
88 ("global-dynamic", llvm::ThreadLocalMode::GeneralDynamic),
89 ("local-dynamic", llvm::ThreadLocalMode::LocalDynamic),
90 ("initial-exec", llvm::ThreadLocalMode::InitialExec),
91 ("local-exec", llvm::ThreadLocalMode::LocalExec),
94 const PRE_THIN_LTO_BC_EXT: &str = "pre-thin-lto.bc";
96 pub fn llvm_err(handler: &errors::Handler, msg: &str) -> FatalError {
97 match llvm::last_error() {
98 Some(err) => handler.fatal(&format!("{}: {}", msg, err)),
99 None => handler.fatal(&msg),
103 pub fn write_output_file(
104 handler: &errors::Handler,
105 target: &'ll llvm::TargetMachine,
106 pm: &llvm::PassManager<'ll>,
107 m: &'ll llvm::Module,
109 file_type: llvm::FileType) -> Result<(), FatalError> {
111 let output_c = path2cstr(output);
112 let result = llvm::LLVMRustWriteOutputFile(target, pm, m, output_c.as_ptr(), file_type);
113 if result.into_result().is_err() {
114 let msg = format!("could not write output to {}", output.display());
115 Err(llvm_err(handler, &msg))
122 fn get_llvm_opt_level(optimize: config::OptLevel) -> llvm::CodeGenOptLevel {
124 config::OptLevel::No => llvm::CodeGenOptLevel::None,
125 config::OptLevel::Less => llvm::CodeGenOptLevel::Less,
126 config::OptLevel::Default => llvm::CodeGenOptLevel::Default,
127 config::OptLevel::Aggressive => llvm::CodeGenOptLevel::Aggressive,
128 _ => llvm::CodeGenOptLevel::Default,
132 fn get_llvm_opt_size(optimize: config::OptLevel) -> llvm::CodeGenOptSize {
134 config::OptLevel::Size => llvm::CodeGenOptSizeDefault,
135 config::OptLevel::SizeMin => llvm::CodeGenOptSizeAggressive,
136 _ => llvm::CodeGenOptSizeNone,
140 pub fn create_target_machine(
143 ) -> &'static mut llvm::TargetMachine {
144 target_machine_factory(sess, find_features)().unwrap_or_else(|err| {
145 llvm_err(sess.diagnostic(), &err).raise()
149 // If find_features is true this won't access `sess.crate_types` by assuming
150 // that `is_pie_binary` is false. When we discover LLVM target features
151 // `sess.crate_types` is uninitialized so we cannot access it.
152 pub fn target_machine_factory(sess: &Session, find_features: bool)
153 -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync>
155 let reloc_model = get_reloc_model(sess);
157 let opt_level = get_llvm_opt_level(sess.opts.optimize);
158 let use_softfp = sess.opts.cg.soft_float;
160 let ffunction_sections = sess.target.target.options.function_sections;
161 let fdata_sections = ffunction_sections;
163 let code_model_arg = sess.opts.cg.code_model.as_ref().or(
164 sess.target.target.options.code_model.as_ref(),
167 let code_model = match code_model_arg {
169 match CODE_GEN_MODEL_ARGS.iter().find(|arg| arg.0 == s) {
172 sess.err(&format!("{:?} is not a valid code model",
174 sess.abort_if_errors();
179 None => llvm::CodeModel::None,
182 let features = attributes::llvm_target_features(sess).collect::<Vec<_>>();
183 let mut singlethread = sess.target.target.options.singlethread;
185 // On the wasm target once the `atomics` feature is enabled that means that
186 // we're no longer single-threaded, or otherwise we don't want LLVM to
187 // lower atomic operations to single-threaded operations.
189 sess.target.target.llvm_target.contains("wasm32") &&
190 features.iter().any(|s| *s == "+atomics")
192 singlethread = false;
195 let triple = SmallCStr::new(&sess.target.target.llvm_target);
196 let cpu = SmallCStr::new(llvm_util::target_cpu(sess));
197 let features = features.join(",");
198 let features = CString::new(features).unwrap();
199 let is_pie_binary = !find_features && is_pie_binary(sess);
200 let trap_unreachable = sess.target.target.options.trap_unreachable;
201 let emit_stack_size_section = sess.opts.debugging_opts.emit_stack_sizes;
203 let asm_comments = sess.asm_comments();
207 llvm::LLVMRustCreateTargetMachine(
208 triple.as_ptr(), cpu.as_ptr(), features.as_ptr(),
219 emit_stack_size_section,
224 format!("Could not create LLVM TargetMachine for triple: {}",
225 triple.to_str().unwrap())
230 /// Module-specific configuration for `optimize_and_codegen`.
231 pub struct ModuleConfig {
232 /// Names of additional optimization passes to run.
234 /// Some(level) to optimize at a certain level, or None to run
235 /// absolutely no optimizations (used for the metadata module).
236 pub opt_level: Option<llvm::CodeGenOptLevel>,
238 /// Some(level) to optimize binary size, or None to not affect program size.
239 opt_size: Option<llvm::CodeGenOptSize>,
241 pgo_gen: Option<String>,
244 // Flags indicating which outputs to produce.
245 pub emit_pre_thin_lto_bc: bool,
246 emit_no_opt_bc: bool,
248 emit_bc_compressed: bool,
253 // Miscellaneous flags. These are mostly copied from command-line
255 pub verify_llvm_ir: bool,
256 no_prepopulate_passes: bool,
259 vectorize_loop: bool,
261 merge_functions: bool,
262 inline_threshold: Option<usize>,
263 // Instead of creating an object file by doing LLVM codegen, just
264 // make the object file bitcode. Provides easy compatibility with
265 // emscripten's ecc compiler, when used as the linker.
266 obj_is_bitcode: bool,
267 no_integrated_as: bool,
269 embed_bitcode_marker: bool,
273 fn new(passes: Vec<String>) -> ModuleConfig {
280 pgo_use: String::new(),
282 emit_no_opt_bc: false,
283 emit_pre_thin_lto_bc: false,
285 emit_bc_compressed: false,
290 obj_is_bitcode: false,
291 embed_bitcode: false,
292 embed_bitcode_marker: false,
293 no_integrated_as: false,
295 verify_llvm_ir: false,
296 no_prepopulate_passes: false,
299 vectorize_loop: false,
300 vectorize_slp: false,
301 merge_functions: false,
302 inline_threshold: None
306 fn set_flags(&mut self, sess: &Session, no_builtins: bool) {
307 self.verify_llvm_ir = sess.verify_llvm_ir();
308 self.no_prepopulate_passes = sess.opts.cg.no_prepopulate_passes;
309 self.no_builtins = no_builtins || sess.target.target.options.no_builtins;
310 self.time_passes = sess.time_passes();
311 self.inline_threshold = sess.opts.cg.inline_threshold;
312 self.obj_is_bitcode = sess.target.target.options.obj_is_bitcode ||
313 sess.opts.debugging_opts.cross_lang_lto.enabled();
314 let embed_bitcode = sess.target.target.options.embed_bitcode ||
315 sess.opts.debugging_opts.embed_bitcode;
317 match sess.opts.optimize {
318 config::OptLevel::No |
319 config::OptLevel::Less => {
320 self.embed_bitcode_marker = embed_bitcode;
322 _ => self.embed_bitcode = embed_bitcode,
326 // Copy what clang does by turning on loop vectorization at O2 and
327 // slp vectorization at O3. Otherwise configure other optimization aspects
328 // of this pass manager builder.
329 // Turn off vectorization for emscripten, as it's not very well supported.
330 self.vectorize_loop = !sess.opts.cg.no_vectorize_loops &&
331 (sess.opts.optimize == config::OptLevel::Default ||
332 sess.opts.optimize == config::OptLevel::Aggressive) &&
333 !sess.target.target.options.is_like_emscripten;
335 self.vectorize_slp = !sess.opts.cg.no_vectorize_slp &&
336 sess.opts.optimize == config::OptLevel::Aggressive &&
337 !sess.target.target.options.is_like_emscripten;
339 self.merge_functions = sess.opts.optimize == config::OptLevel::Default ||
340 sess.opts.optimize == config::OptLevel::Aggressive;
343 pub fn bitcode_needed(&self) -> bool {
344 self.emit_bc || self.obj_is_bitcode
345 || self.emit_bc_compressed || self.embed_bitcode
349 /// Assembler name and command used by codegen when no_integrated_as is enabled
350 struct AssemblerCommand {
355 /// Additional resources used by optimize_and_codegen (not module specific)
357 pub struct CodegenContext<'ll> {
358 // Resources needed when running LTO
359 pub time_passes: bool,
361 pub no_landing_pads: bool,
362 pub save_temps: bool,
363 pub fewer_names: bool,
364 pub exported_symbols: Option<Arc<ExportedSymbols>>,
365 pub opts: Arc<config::Options>,
366 pub crate_types: Vec<config::CrateType>,
367 pub each_linked_rlib_for_lto: Vec<(CrateNum, PathBuf)>,
368 output_filenames: Arc<OutputFilenames>,
369 regular_module_config: Arc<ModuleConfig>,
370 metadata_module_config: Arc<ModuleConfig>,
371 allocator_module_config: Arc<ModuleConfig>,
372 pub tm_factory: Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync>,
373 pub msvc_imps_needed: bool,
374 pub target_pointer_width: String,
375 debuginfo: config::DebugInfo,
377 // Number of cgus excluding the allocator/metadata modules
378 pub total_cgus: usize,
379 // Handler to use for diagnostics produced during codegen.
380 pub diag_emitter: SharedEmitter,
381 // LLVM passes added by plugins.
382 pub plugin_passes: Vec<String>,
383 // LLVM optimizations for which we want to print remarks.
385 // Worker thread number
387 // The incremental compilation session directory, or None if we are not
388 // compiling incrementally
389 pub incr_comp_session_dir: Option<PathBuf>,
390 // Used to update CGU re-use information during the thinlto phase.
391 pub cgu_reuse_tracker: CguReuseTracker,
392 // Channel back to the main control thread to send messages to
393 coordinator_send: Sender<Box<dyn Any + Send>>,
394 // A reference to the TimeGraph so we can register timings. None means that
395 // measuring is disabled.
396 time_graph: Option<TimeGraph>,
397 // The assembler command if no_integrated_as option is enabled, None otherwise
398 assembler_cmd: Option<Arc<AssemblerCommand>>,
399 // This field is used to give a lifetime parameter to the struct so that it can implement
400 // the Backend trait.
401 phantom: PhantomData<&'ll ()>
404 impl CodegenContext<'ll> {
405 pub fn create_diag_handler(&self) -> Handler {
406 Handler::with_emitter(true, false, Box::new(self.diag_emitter.clone()))
409 pub(crate) fn config(&self, kind: ModuleKind) -> &ModuleConfig {
411 ModuleKind::Regular => &self.regular_module_config,
412 ModuleKind::Metadata => &self.metadata_module_config,
413 ModuleKind::Allocator => &self.allocator_module_config,
417 pub(crate) fn save_temp_bitcode(&self, module: &ModuleCodegen, name: &str) {
418 if !self.save_temps {
422 let ext = format!("{}.bc", name);
423 let cgu = Some(&module.name[..]);
424 let path = self.output_filenames.temp_path_ext(&ext, cgu);
425 let cstr = path2cstr(&path);
426 let llmod = module.module_llvm.llmod();
427 llvm::LLVMWriteBitcodeToFile(llmod, cstr.as_ptr());
432 impl<'ll> Backend for CodegenContext<'ll> {
433 type Value = &'ll Value;
434 type BasicBlock = &'ll BasicBlock;
435 type Type = &'ll Type;
436 type Context = &'ll llvm::Context;
437 type TypeKind = llvm::TypeKind;
440 impl CommonWriteMethods for CodegenContext<'ll> {
441 fn val_ty(&self, v: &'ll Value) -> &'ll Type {
445 fn const_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
446 common::const_bytes_in_context(llcx, bytes)
449 fn const_struct_in_context(
451 llcx: &'a llvm::Context,
455 common::const_struct_in_context(llcx, elts, packed)
459 impl CodegenContext<'ll> {
460 pub fn ptr_to(&self, ty: &'ll Type) -> &'ll Type {
462 llvm::LLVMPointerType(ty, 0)
468 pub struct DiagnosticHandlers<'a> {
469 data: *mut (&'a CodegenContext<'a>, &'a Handler),
470 llcx: &'a llvm::Context,
473 impl<'a> DiagnosticHandlers<'a> {
474 pub fn new(cgcx: &'a CodegenContext<'a>,
475 handler: &'a Handler,
476 llcx: &'a llvm::Context) -> Self {
477 let data = Box::into_raw(Box::new((cgcx, handler)));
479 llvm::LLVMRustSetInlineAsmDiagnosticHandler(llcx, inline_asm_handler, data as *mut _);
480 llvm::LLVMContextSetDiagnosticHandler(llcx, diagnostic_handler, data as *mut _);
482 DiagnosticHandlers { data, llcx }
486 impl<'a> Drop for DiagnosticHandlers<'a> {
488 use std::ptr::null_mut;
490 llvm::LLVMRustSetInlineAsmDiagnosticHandler(self.llcx, inline_asm_handler, null_mut());
491 llvm::LLVMContextSetDiagnosticHandler(self.llcx, diagnostic_handler, null_mut());
492 drop(Box::from_raw(self.data));
497 unsafe extern "C" fn report_inline_asm<'a, 'b>(cgcx: &'a CodegenContext,
500 cgcx.diag_emitter.inline_asm_error(cookie as u32, msg.to_owned());
503 unsafe extern "C" fn inline_asm_handler(diag: &SMDiagnostic,
509 let (cgcx, _) = *(user as *const (&CodegenContext, &Handler));
511 let msg = llvm::build_string(|s| llvm::LLVMRustWriteSMDiagnosticToString(diag, s))
512 .expect("non-UTF8 SMDiagnostic");
514 report_inline_asm(cgcx, &msg, cookie);
517 unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void) {
521 let (cgcx, diag_handler) = *(user as *const (&CodegenContext, &Handler));
523 match llvm::diagnostic::Diagnostic::unpack(info) {
524 llvm::diagnostic::InlineAsm(inline) => {
525 report_inline_asm(cgcx,
526 &llvm::twine_to_string(inline.message),
530 llvm::diagnostic::Optimization(opt) => {
531 let enabled = match cgcx.remark {
533 Passes::Some(ref v) => v.iter().any(|s| *s == opt.pass_name),
537 diag_handler.note_without_error(&format!("optimization {} for {} at {}:{}:{}: {}",
546 llvm::diagnostic::PGO(diagnostic_ref) |
547 llvm::diagnostic::Linker(diagnostic_ref) => {
548 let msg = llvm::build_string(|s| {
549 llvm::LLVMRustWriteDiagnosticInfoToString(diagnostic_ref, s)
550 }).expect("non-UTF8 diagnostic");
551 diag_handler.warn(&msg);
553 llvm::diagnostic::UnknownDiagnostic(..) => {},
557 // Unsafe due to LLVM calls.
558 unsafe fn optimize(cgcx: &CodegenContext,
559 diag_handler: &Handler,
560 module: &ModuleCodegen,
561 config: &ModuleConfig,
562 timeline: &mut Timeline)
563 -> Result<(), FatalError>
565 let llmod = module.module_llvm.llmod();
566 let llcx = &*module.module_llvm.llcx;
567 let tm = &*module.module_llvm.tm;
568 let _handlers = DiagnosticHandlers::new(cgcx, diag_handler, llcx);
570 let module_name = module.name.clone();
571 let module_name = Some(&module_name[..]);
573 if config.emit_no_opt_bc {
574 let out = cgcx.output_filenames.temp_path_ext("no-opt.bc", module_name);
575 let out = path2cstr(&out);
576 llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr());
579 if config.opt_level.is_some() {
580 // Create the two optimizing pass managers. These mirror what clang
581 // does, and are by populated by LLVM's default PassManagerBuilder.
582 // Each manager has a different set of passes, but they also share
583 // some common passes.
584 let fpm = llvm::LLVMCreateFunctionPassManagerForModule(llmod);
585 let mpm = llvm::LLVMCreatePassManager();
588 // If we're verifying or linting, add them to the function pass
590 let addpass = |pass_name: &str| {
591 let pass_name = SmallCStr::new(pass_name);
592 let pass = match llvm::LLVMRustFindAndCreatePass(pass_name.as_ptr()) {
594 None => return false,
596 let pass_manager = match llvm::LLVMRustPassKind(pass) {
597 llvm::PassKind::Function => &*fpm,
598 llvm::PassKind::Module => &*mpm,
599 llvm::PassKind::Other => {
600 diag_handler.err("Encountered LLVM pass kind we can't handle");
604 llvm::LLVMRustAddPass(pass_manager, pass);
608 if config.verify_llvm_ir { assert!(addpass("verify")); }
610 // Some options cause LLVM bitcode to be emitted, which uses ThinLTOBuffers, so we need
611 // to make sure we run LLVM's NameAnonGlobals pass when emitting bitcode; otherwise
612 // we'll get errors in LLVM.
613 let using_thin_buffers = config.bitcode_needed();
614 let mut have_name_anon_globals_pass = false;
615 if !config.no_prepopulate_passes {
616 llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
617 llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod);
618 let opt_level = config.opt_level.unwrap_or(llvm::CodeGenOptLevel::None);
619 let prepare_for_thin_lto = cgcx.lto == Lto::Thin || cgcx.lto == Lto::ThinLocal ||
620 (cgcx.lto != Lto::Fat && cgcx.opts.debugging_opts.cross_lang_lto.enabled());
621 have_name_anon_globals_pass = have_name_anon_globals_pass || prepare_for_thin_lto;
622 if using_thin_buffers && !prepare_for_thin_lto {
623 assert!(addpass("name-anon-globals"));
624 have_name_anon_globals_pass = true;
626 with_llvm_pmb(llmod, &config, opt_level, prepare_for_thin_lto, &mut |b| {
627 llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm);
628 llvm::LLVMPassManagerBuilderPopulateModulePassManager(b, mpm);
632 for pass in &config.passes {
634 diag_handler.warn(&format!("unknown pass `{}`, ignoring", pass));
636 if pass == "name-anon-globals" {
637 have_name_anon_globals_pass = true;
641 for pass in &cgcx.plugin_passes {
643 diag_handler.err(&format!("a plugin asked for LLVM pass \
644 `{}` but LLVM does not \
645 recognize it", pass));
647 if pass == "name-anon-globals" {
648 have_name_anon_globals_pass = true;
652 if using_thin_buffers && !have_name_anon_globals_pass {
653 // As described above, this will probably cause an error in LLVM
654 if config.no_prepopulate_passes {
655 diag_handler.err("The current compilation is going to use thin LTO buffers \
656 without running LLVM's NameAnonGlobals pass. \
657 This will likely cause errors in LLVM. Consider adding \
658 -C passes=name-anon-globals to the compiler command line.");
660 bug!("We are using thin LTO buffers without running the NameAnonGlobals pass. \
661 This will likely cause errors in LLVM and should never happen.");
666 diag_handler.abort_if_errors();
668 // Finally, run the actual optimization passes
669 time_ext(config.time_passes,
671 &format!("llvm function passes [{}]", module_name.unwrap()),
673 llvm::LLVMRustRunFunctionPassManager(fpm, llmod)
675 timeline.record("fpm");
676 time_ext(config.time_passes,
678 &format!("llvm module passes [{}]", module_name.unwrap()),
680 llvm::LLVMRunPassManager(mpm, llmod)
683 // Deallocate managers that we're now done with
684 llvm::LLVMDisposePassManager(fpm);
685 llvm::LLVMDisposePassManager(mpm);
690 fn generate_lto_work(cgcx: &CodegenContext,
691 modules: Vec<ModuleCodegen>,
692 import_only_modules: Vec<(SerializedModule, WorkProduct)>)
693 -> Vec<(WorkItem, u64)>
695 let mut timeline = cgcx.time_graph.as_ref().map(|tg| {
696 tg.start(CODEGEN_WORKER_TIMELINE,
697 CODEGEN_WORK_PACKAGE_KIND,
699 }).unwrap_or(Timeline::noop());
700 let (lto_modules, copy_jobs) = lto::run(cgcx, modules, import_only_modules, &mut timeline)
701 .unwrap_or_else(|e| e.raise());
703 let lto_modules = lto_modules.into_iter().map(|module| {
704 let cost = module.cost();
705 (WorkItem::LTO(module), cost)
708 let copy_jobs = copy_jobs.into_iter().map(|wp| {
709 (WorkItem::CopyPostLtoArtifacts(CachedModuleCodegen {
710 name: wp.cgu_name.clone(),
715 lto_modules.chain(copy_jobs).collect()
718 unsafe fn codegen(cgcx: &CodegenContext,
719 diag_handler: &Handler,
720 module: ModuleCodegen,
721 config: &ModuleConfig,
722 timeline: &mut Timeline)
723 -> Result<CompiledModule, FatalError>
725 timeline.record("codegen");
727 let llmod = module.module_llvm.llmod();
728 let llcx = &*module.module_llvm.llcx;
729 let tm = &*module.module_llvm.tm;
730 let module_name = module.name.clone();
731 let module_name = Some(&module_name[..]);
732 let handlers = DiagnosticHandlers::new(cgcx, diag_handler, llcx);
734 if cgcx.msvc_imps_needed {
735 create_msvc_imps(cgcx, llcx, llmod);
738 // A codegen-specific pass manager is used to generate object
739 // files for an LLVM module.
741 // Apparently each of these pass managers is a one-shot kind of
742 // thing, so we create a new one for each type of output. The
743 // pass manager passed to the closure should be ensured to not
744 // escape the closure itself, and the manager should only be
746 unsafe fn with_codegen<'ll, F, R>(tm: &'ll llvm::TargetMachine,
747 llmod: &'ll llvm::Module,
750 where F: FnOnce(&'ll mut PassManager<'ll>) -> R,
752 let cpm = llvm::LLVMCreatePassManager();
753 llvm::LLVMRustAddAnalysisPasses(tm, cpm, llmod);
754 llvm::LLVMRustAddLibraryInfo(cpm, llmod, no_builtins);
758 // If we don't have the integrated assembler, then we need to emit asm
759 // from LLVM and use `gcc` to create the object file.
760 let asm_to_obj = config.emit_obj && config.no_integrated_as;
762 // Change what we write and cleanup based on whether obj files are
763 // just llvm bitcode. In that case write bitcode, and possibly
764 // delete the bitcode if it wasn't requested. Don't generate the
765 // machine code, instead copy the .o file from the .bc
766 let write_bc = config.emit_bc || config.obj_is_bitcode;
767 let rm_bc = !config.emit_bc && config.obj_is_bitcode;
768 let write_obj = config.emit_obj && !config.obj_is_bitcode && !asm_to_obj;
769 let copy_bc_to_obj = config.emit_obj && config.obj_is_bitcode;
771 let bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
772 let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
775 if write_bc || config.emit_bc_compressed || config.embed_bitcode {
776 let thin = ThinBuffer::new(llmod);
777 let data = thin.data();
778 timeline.record("make-bc");
781 if let Err(e) = fs::write(&bc_out, data) {
782 diag_handler.err(&format!("failed to write bytecode: {}", e));
784 timeline.record("write-bc");
787 if config.embed_bitcode {
788 embed_bitcode(cgcx, llcx, llmod, Some(data));
789 timeline.record("embed-bc");
792 if config.emit_bc_compressed {
793 let dst = bc_out.with_extension(RLIB_BYTECODE_EXTENSION);
794 let data = bytecode::encode(&module.name, data);
795 if let Err(e) = fs::write(&dst, data) {
796 diag_handler.err(&format!("failed to write bytecode: {}", e));
798 timeline.record("compress-bc");
800 } else if config.embed_bitcode_marker {
801 embed_bitcode(cgcx, llcx, llmod, None);
804 time_ext(config.time_passes, None, &format!("codegen passes [{}]", module_name.unwrap()),
805 || -> Result<(), FatalError> {
807 let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
808 let out = path2cstr(&out);
810 extern "C" fn demangle_callback(input_ptr: *const c_char,
812 output_ptr: *mut c_char,
813 output_len: size_t) -> size_t {
815 slice::from_raw_parts(input_ptr as *const u8, input_len as usize)
818 let input = match str::from_utf8(input) {
823 let output = unsafe {
824 slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize)
826 let mut cursor = io::Cursor::new(output);
828 let demangled = match rustc_demangle::try_demangle(input) {
833 if let Err(_) = write!(cursor, "{:#}", demangled) {
834 // Possible only if provided buffer is not big enough
838 cursor.position() as size_t
841 with_codegen(tm, llmod, config.no_builtins, |cpm| {
842 llvm::LLVMRustPrintModule(cpm, llmod, out.as_ptr(), demangle_callback);
843 llvm::LLVMDisposePassManager(cpm);
845 timeline.record("ir");
848 if config.emit_asm || asm_to_obj {
849 let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
851 // We can't use the same module for asm and binary output, because that triggers
852 // various errors like invalid IR or broken binaries, so we might have to clone the
853 // module to produce the asm output
854 let llmod = if config.emit_obj {
855 llvm::LLVMCloneModule(llmod)
859 with_codegen(tm, llmod, config.no_builtins, |cpm| {
860 write_output_file(diag_handler, tm, cpm, llmod, &path,
861 llvm::FileType::AssemblyFile)
863 timeline.record("asm");
867 with_codegen(tm, llmod, config.no_builtins, |cpm| {
868 write_output_file(diag_handler, tm, cpm, llmod, &obj_out,
869 llvm::FileType::ObjectFile)
871 timeline.record("obj");
872 } else if asm_to_obj {
873 let assembly = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
874 run_assembler(cgcx, diag_handler, &assembly, &obj_out);
875 timeline.record("asm_to_obj");
877 if !config.emit_asm && !cgcx.save_temps {
878 drop(fs::remove_file(&assembly));
886 debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
887 if let Err(e) = link_or_copy(&bc_out, &obj_out) {
888 diag_handler.err(&format!("failed to copy bitcode to object file: {}", e));
893 debug!("removing_bitcode {:?}", bc_out);
894 if let Err(e) = fs::remove_file(&bc_out) {
895 diag_handler.err(&format!("failed to remove bitcode: {}", e));
901 Ok(module.into_compiled_module(config.emit_obj,
903 config.emit_bc_compressed,
904 &cgcx.output_filenames))
907 /// Embed the bitcode of an LLVM module in the LLVM module itself.
909 /// This is done primarily for iOS where it appears to be standard to compile C
910 /// code at least with `-fembed-bitcode` which creates two sections in the
913 /// * __LLVM,__bitcode
914 /// * __LLVM,__cmdline
916 /// It appears *both* of these sections are necessary to get the linker to
917 /// recognize what's going on. For us though we just always throw in an empty
920 /// Furthermore debug/O1 builds don't actually embed bitcode but rather just
921 /// embed an empty section.
923 /// Basically all of this is us attempting to follow in the footsteps of clang
924 /// on iOS. See #35968 for lots more info.
925 unsafe fn embed_bitcode(cgcx: &CodegenContext,
926 llcx: &llvm::Context,
927 llmod: &llvm::Module,
928 bitcode: Option<&[u8]>) {
929 let llconst = cgcx.const_bytes_in_context(llcx, bitcode.unwrap_or(&[]));
930 let llglobal = llvm::LLVMAddGlobal(
932 cgcx.val_ty(llconst),
933 "rustc.embedded.module\0".as_ptr() as *const _,
935 llvm::LLVMSetInitializer(llglobal, llconst);
937 let is_apple = cgcx.opts.target_triple.triple().contains("-ios") ||
938 cgcx.opts.target_triple.triple().contains("-darwin");
940 let section = if is_apple {
945 llvm::LLVMSetSection(llglobal, section.as_ptr() as *const _);
946 llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
947 llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
949 let llconst = cgcx.const_bytes_in_context(llcx, &[]);
950 let llglobal = llvm::LLVMAddGlobal(
952 cgcx.val_ty(llconst),
953 "rustc.embedded.cmdline\0".as_ptr() as *const _,
955 llvm::LLVMSetInitializer(llglobal, llconst);
956 let section = if is_apple {
961 llvm::LLVMSetSection(llglobal, section.as_ptr() as *const _);
962 llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
965 pub(crate) struct CompiledModules {
966 pub modules: Vec<CompiledModule>,
967 pub metadata_module: CompiledModule,
968 pub allocator_module: Option<CompiledModule>,
971 fn need_crate_bitcode_for_rlib(sess: &Session) -> bool {
972 sess.crate_types.borrow().contains(&config::CrateType::Rlib) &&
973 sess.opts.output_types.contains_key(&OutputType::Exe)
976 fn need_pre_thin_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
977 if sess.opts.incremental.is_none() {
985 Lto::ThinLocal => true,
989 pub fn start_async_codegen(tcx: TyCtxt,
990 time_graph: Option<TimeGraph>,
991 metadata: EncodedMetadata,
992 coordinator_receive: Receiver<Box<dyn Any + Send>>,
996 let crate_name = tcx.crate_name(LOCAL_CRATE);
997 let crate_hash = tcx.crate_hash(LOCAL_CRATE);
998 let no_builtins = attr::contains_name(&tcx.hir.krate().attrs, "no_builtins");
999 let subsystem = attr::first_attr_value_str_by_name(&tcx.hir.krate().attrs,
1000 "windows_subsystem");
1001 let windows_subsystem = subsystem.map(|subsystem| {
1002 if subsystem != "windows" && subsystem != "console" {
1003 tcx.sess.fatal(&format!("invalid windows subsystem `{}`, only \
1004 `windows` and `console` are allowed",
1007 subsystem.to_string()
1010 let linker_info = LinkerInfo::new(tcx);
1011 let crate_info = CrateInfo::new(tcx);
1013 // Figure out what we actually need to build.
1014 let mut modules_config = ModuleConfig::new(sess.opts.cg.passes.clone());
1015 let mut metadata_config = ModuleConfig::new(vec![]);
1016 let mut allocator_config = ModuleConfig::new(vec![]);
1018 if let Some(ref sanitizer) = sess.opts.debugging_opts.sanitizer {
1020 Sanitizer::Address => {
1021 modules_config.passes.push("asan".to_owned());
1022 modules_config.passes.push("asan-module".to_owned());
1024 Sanitizer::Memory => {
1025 modules_config.passes.push("msan".to_owned())
1027 Sanitizer::Thread => {
1028 modules_config.passes.push("tsan".to_owned())
1034 if sess.opts.debugging_opts.profile {
1035 modules_config.passes.push("insert-gcov-profiling".to_owned())
1038 modules_config.pgo_gen = sess.opts.debugging_opts.pgo_gen.clone();
1039 modules_config.pgo_use = sess.opts.debugging_opts.pgo_use.clone();
1041 modules_config.opt_level = Some(get_llvm_opt_level(sess.opts.optimize));
1042 modules_config.opt_size = Some(get_llvm_opt_size(sess.opts.optimize));
1044 // Save all versions of the bytecode if we're saving our temporaries.
1045 if sess.opts.cg.save_temps {
1046 modules_config.emit_no_opt_bc = true;
1047 modules_config.emit_pre_thin_lto_bc = true;
1048 modules_config.emit_bc = true;
1049 modules_config.emit_lto_bc = true;
1050 metadata_config.emit_bc = true;
1051 allocator_config.emit_bc = true;
1054 // Emit compressed bitcode files for the crate if we're emitting an rlib.
1055 // Whenever an rlib is created, the bitcode is inserted into the archive in
1056 // order to allow LTO against it.
1057 if need_crate_bitcode_for_rlib(sess) {
1058 modules_config.emit_bc_compressed = true;
1059 allocator_config.emit_bc_compressed = true;
1062 modules_config.emit_pre_thin_lto_bc =
1063 need_pre_thin_lto_bitcode_for_incr_comp(sess);
1065 modules_config.no_integrated_as = tcx.sess.opts.cg.no_integrated_as ||
1066 tcx.sess.target.target.options.no_integrated_as;
1068 for output_type in sess.opts.output_types.keys() {
1069 match *output_type {
1070 OutputType::Bitcode => { modules_config.emit_bc = true; }
1071 OutputType::LlvmAssembly => { modules_config.emit_ir = true; }
1072 OutputType::Assembly => {
1073 modules_config.emit_asm = true;
1074 // If we're not using the LLVM assembler, this function
1075 // could be invoked specially with output_type_assembly, so
1076 // in this case we still want the metadata object file.
1077 if !sess.opts.output_types.contains_key(&OutputType::Assembly) {
1078 metadata_config.emit_obj = true;
1079 allocator_config.emit_obj = true;
1082 OutputType::Object => { modules_config.emit_obj = true; }
1083 OutputType::Metadata => { metadata_config.emit_obj = true; }
1084 OutputType::Exe => {
1085 modules_config.emit_obj = true;
1086 metadata_config.emit_obj = true;
1087 allocator_config.emit_obj = true;
1089 OutputType::Mir => {}
1090 OutputType::DepInfo => {}
1094 modules_config.set_flags(sess, no_builtins);
1095 metadata_config.set_flags(sess, no_builtins);
1096 allocator_config.set_flags(sess, no_builtins);
1098 // Exclude metadata and allocator modules from time_passes output, since
1099 // they throw off the "LLVM passes" measurement.
1100 metadata_config.time_passes = false;
1101 allocator_config.time_passes = false;
1103 let (shared_emitter, shared_emitter_main) = SharedEmitter::new();
1104 let (codegen_worker_send, codegen_worker_receive) = channel();
1106 let coordinator_thread = start_executing_work(tcx,
1109 codegen_worker_send,
1110 coordinator_receive,
1112 sess.jobserver.clone(),
1114 Arc::new(modules_config),
1115 Arc::new(metadata_config),
1116 Arc::new(allocator_config));
1127 coordinator_send: tcx.tx_to_llvm_workers.lock().clone(),
1128 codegen_worker_receive,
1129 shared_emitter_main,
1130 future: coordinator_thread,
1131 output_filenames: tcx.output_filenames(LOCAL_CRATE),
1135 fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
1137 compiled_modules: &CompiledModules,
1138 ) -> FxHashMap<WorkProductId, WorkProduct> {
1139 let mut work_products = FxHashMap::default();
1141 if sess.opts.incremental.is_none() {
1142 return work_products;
1145 for module in compiled_modules.modules.iter().filter(|m| m.kind == ModuleKind::Regular) {
1146 let mut files = vec![];
1148 if let Some(ref path) = module.object {
1149 files.push((WorkProductFileKind::Object, path.clone()));
1151 if let Some(ref path) = module.bytecode {
1152 files.push((WorkProductFileKind::Bytecode, path.clone()));
1154 if let Some(ref path) = module.bytecode_compressed {
1155 files.push((WorkProductFileKind::BytecodeCompressed, path.clone()));
1158 if let Some((id, product)) =
1159 copy_cgu_workproducts_to_incr_comp_cache_dir(sess, &module.name, &files)
1161 work_products.insert(id, product);
1168 fn produce_final_output_artifacts(sess: &Session,
1169 compiled_modules: &CompiledModules,
1170 crate_output: &OutputFilenames) {
1171 let mut user_wants_bitcode = false;
1172 let mut user_wants_objects = false;
1174 // Produce final compile outputs.
1175 let copy_gracefully = |from: &Path, to: &Path| {
1176 if let Err(e) = fs::copy(from, to) {
1177 sess.err(&format!("could not copy {:?} to {:?}: {}", from, to, e));
1181 let copy_if_one_unit = |output_type: OutputType,
1182 keep_numbered: bool| {
1183 if compiled_modules.modules.len() == 1 {
1184 // 1) Only one codegen unit. In this case it's no difficulty
1185 // to copy `foo.0.x` to `foo.x`.
1186 let module_name = Some(&compiled_modules.modules[0].name[..]);
1187 let path = crate_output.temp_path(output_type, module_name);
1188 copy_gracefully(&path,
1189 &crate_output.path(output_type));
1190 if !sess.opts.cg.save_temps && !keep_numbered {
1191 // The user just wants `foo.x`, not `foo.#module-name#.x`.
1192 remove(sess, &path);
1195 let ext = crate_output.temp_path(output_type, None)
1202 if crate_output.outputs.contains_key(&output_type) {
1203 // 2) Multiple codegen units, with `--emit foo=some_name`. We have
1204 // no good solution for this case, so warn the user.
1205 sess.warn(&format!("ignoring emit path because multiple .{} files \
1206 were produced", ext));
1207 } else if crate_output.single_output_file.is_some() {
1208 // 3) Multiple codegen units, with `-o some_name`. We have
1209 // no good solution for this case, so warn the user.
1210 sess.warn(&format!("ignoring -o because multiple .{} files \
1211 were produced", ext));
1213 // 4) Multiple codegen units, but no explicit name. We
1214 // just leave the `foo.0.x` files in place.
1215 // (We don't have to do any work in this case.)
1220 // Flag to indicate whether the user explicitly requested bitcode.
1221 // Otherwise, we produced it only as a temporary output, and will need
1222 // to get rid of it.
1223 for output_type in crate_output.outputs.keys() {
1224 match *output_type {
1225 OutputType::Bitcode => {
1226 user_wants_bitcode = true;
1227 // Copy to .bc, but always keep the .0.bc. There is a later
1228 // check to figure out if we should delete .0.bc files, or keep
1229 // them for making an rlib.
1230 copy_if_one_unit(OutputType::Bitcode, true);
1232 OutputType::LlvmAssembly => {
1233 copy_if_one_unit(OutputType::LlvmAssembly, false);
1235 OutputType::Assembly => {
1236 copy_if_one_unit(OutputType::Assembly, false);
1238 OutputType::Object => {
1239 user_wants_objects = true;
1240 copy_if_one_unit(OutputType::Object, true);
1243 OutputType::Metadata |
1245 OutputType::DepInfo => {}
1249 // Clean up unwanted temporary files.
1251 // We create the following files by default:
1252 // - #crate#.#module-name#.bc
1253 // - #crate#.#module-name#.o
1254 // - #crate#.crate.metadata.bc
1255 // - #crate#.crate.metadata.o
1256 // - #crate#.o (linked from crate.##.o)
1257 // - #crate#.bc (copied from crate.##.bc)
1258 // We may create additional files if requested by the user (through
1259 // `-C save-temps` or `--emit=` flags).
1261 if !sess.opts.cg.save_temps {
1262 // Remove the temporary .#module-name#.o objects. If the user didn't
1263 // explicitly request bitcode (with --emit=bc), and the bitcode is not
1264 // needed for building an rlib, then we must remove .#module-name#.bc as
1267 // Specific rules for keeping .#module-name#.bc:
1268 // - If the user requested bitcode (`user_wants_bitcode`), and
1269 // codegen_units > 1, then keep it.
1270 // - If the user requested bitcode but codegen_units == 1, then we
1271 // can toss .#module-name#.bc because we copied it to .bc earlier.
1272 // - If we're not building an rlib and the user didn't request
1273 // bitcode, then delete .#module-name#.bc.
1274 // If you change how this works, also update back::link::link_rlib,
1275 // where .#module-name#.bc files are (maybe) deleted after making an
1277 let needs_crate_object = crate_output.outputs.contains_key(&OutputType::Exe);
1279 let keep_numbered_bitcode = user_wants_bitcode && sess.codegen_units() > 1;
1281 let keep_numbered_objects = needs_crate_object ||
1282 (user_wants_objects && sess.codegen_units() > 1);
1284 for module in compiled_modules.modules.iter() {
1285 if let Some(ref path) = module.object {
1286 if !keep_numbered_objects {
1291 if let Some(ref path) = module.bytecode {
1292 if !keep_numbered_bitcode {
1298 if !user_wants_bitcode {
1299 if let Some(ref path) = compiled_modules.metadata_module.bytecode {
1300 remove(sess, &path);
1303 if let Some(ref allocator_module) = compiled_modules.allocator_module {
1304 if let Some(ref path) = allocator_module.bytecode {
1311 // We leave the following files around by default:
1313 // - #crate#.crate.metadata.o
1315 // These are used in linking steps and will be cleaned up afterward.
1318 pub(crate) fn dump_incremental_data(_codegen_results: &CodegenResults) {
1319 // FIXME(mw): This does not work at the moment because the situation has
1320 // become more complicated due to incremental LTO. Now a CGU
1321 // can have more than two caching states.
1322 // println!("[incremental] Re-using {} out of {} modules",
1323 // codegen_results.modules.iter().filter(|m| m.pre_existing).count(),
1324 // codegen_results.modules.len());
1328 /// Optimize a newly codegened, totally unoptimized module.
1329 Optimize(ModuleCodegen),
1330 /// Copy the post-LTO artifacts from the incremental cache to the output
1332 CopyPostLtoArtifacts(CachedModuleCodegen),
1333 /// Perform (Thin)LTO on the given module.
1334 LTO(lto::LtoModuleCodegen),
1338 fn module_kind(&self) -> ModuleKind {
1340 WorkItem::Optimize(ref m) => m.kind,
1341 WorkItem::CopyPostLtoArtifacts(_) |
1342 WorkItem::LTO(_) => ModuleKind::Regular,
1346 fn name(&self) -> String {
1348 WorkItem::Optimize(ref m) => format!("optimize: {}", m.name),
1349 WorkItem::CopyPostLtoArtifacts(ref m) => format!("copy post LTO artifacts: {}", m.name),
1350 WorkItem::LTO(ref m) => format!("lto: {}", m.name()),
1355 enum WorkItemResult {
1356 Compiled(CompiledModule),
1357 NeedsLTO(ModuleCodegen),
1360 fn execute_work_item(cgcx: &CodegenContext,
1361 work_item: WorkItem,
1362 timeline: &mut Timeline)
1363 -> Result<WorkItemResult, FatalError>
1365 let module_config = cgcx.config(work_item.module_kind());
1368 WorkItem::Optimize(module) => {
1369 execute_optimize_work_item(cgcx, module, module_config, timeline)
1371 WorkItem::CopyPostLtoArtifacts(module) => {
1372 execute_copy_from_cache_work_item(cgcx, module, module_config, timeline)
1374 WorkItem::LTO(module) => {
1375 execute_lto_work_item(cgcx, module, module_config, timeline)
1380 fn execute_optimize_work_item(cgcx: &CodegenContext,
1381 module: ModuleCodegen,
1382 module_config: &ModuleConfig,
1383 timeline: &mut Timeline)
1384 -> Result<WorkItemResult, FatalError>
1386 let diag_handler = cgcx.create_diag_handler();
1389 optimize(cgcx, &diag_handler, &module, module_config, timeline)?;
1392 let linker_does_lto = cgcx.opts.debugging_opts.cross_lang_lto.enabled();
1394 // After we've done the initial round of optimizations we need to
1395 // decide whether to synchronously codegen this module or ship it
1396 // back to the coordinator thread for further LTO processing (which
1397 // has to wait for all the initial modules to be optimized).
1399 // Here we dispatch based on the `cgcx.lto` and kind of module we're
1401 let needs_lto = match cgcx.lto {
1404 // If the linker does LTO, we don't have to do it. Note that we
1405 // keep doing full LTO, if it is requested, as not to break the
1406 // assumption that the output will be a single module.
1407 Lto::Thin | Lto::ThinLocal if linker_does_lto => false,
1409 // Here we've got a full crate graph LTO requested. We ignore
1410 // this, however, if the crate type is only an rlib as there's
1411 // no full crate graph to process, that'll happen later.
1413 // This use case currently comes up primarily for targets that
1414 // require LTO so the request for LTO is always unconditionally
1415 // passed down to the backend, but we don't actually want to do
1416 // anything about it yet until we've got a final product.
1417 Lto::Fat | Lto::Thin => {
1418 cgcx.crate_types.len() != 1 ||
1419 cgcx.crate_types[0] != config::CrateType::Rlib
1422 // When we're automatically doing ThinLTO for multi-codegen-unit
1423 // builds we don't actually want to LTO the allocator modules if
1424 // it shows up. This is due to various linker shenanigans that
1425 // we'll encounter later.
1427 module.kind != ModuleKind::Allocator
1431 // Metadata modules never participate in LTO regardless of the lto
1433 let needs_lto = needs_lto && module.kind != ModuleKind::Metadata;
1436 Ok(WorkItemResult::NeedsLTO(module))
1438 let module = unsafe {
1439 codegen(cgcx, &diag_handler, module, module_config, timeline)?
1441 Ok(WorkItemResult::Compiled(module))
1445 fn execute_copy_from_cache_work_item(cgcx: &CodegenContext,
1446 module: CachedModuleCodegen,
1447 module_config: &ModuleConfig,
1449 -> Result<WorkItemResult, FatalError>
1451 let incr_comp_session_dir = cgcx.incr_comp_session_dir
1454 let mut object = None;
1455 let mut bytecode = None;
1456 let mut bytecode_compressed = None;
1457 for (kind, saved_file) in &module.source.saved_files {
1458 let obj_out = match kind {
1459 WorkProductFileKind::Object => {
1460 let path = cgcx.output_filenames.temp_path(OutputType::Object,
1461 Some(&module.name));
1462 object = Some(path.clone());
1465 WorkProductFileKind::Bytecode => {
1466 let path = cgcx.output_filenames.temp_path(OutputType::Bitcode,
1467 Some(&module.name));
1468 bytecode = Some(path.clone());
1471 WorkProductFileKind::BytecodeCompressed => {
1472 let path = cgcx.output_filenames.temp_path(OutputType::Bitcode,
1474 .with_extension(RLIB_BYTECODE_EXTENSION);
1475 bytecode_compressed = Some(path.clone());
1479 let source_file = in_incr_comp_dir(&incr_comp_session_dir,
1481 debug!("copying pre-existing module `{}` from {:?} to {}",
1485 if let Err(err) = link_or_copy(&source_file, &obj_out) {
1486 let diag_handler = cgcx.create_diag_handler();
1487 diag_handler.err(&format!("unable to copy {} to {}: {}",
1488 source_file.display(),
1494 assert_eq!(object.is_some(), module_config.emit_obj);
1495 assert_eq!(bytecode.is_some(), module_config.emit_bc);
1496 assert_eq!(bytecode_compressed.is_some(), module_config.emit_bc_compressed);
1498 Ok(WorkItemResult::Compiled(CompiledModule {
1500 kind: ModuleKind::Regular,
1503 bytecode_compressed,
1507 fn execute_lto_work_item(cgcx: &CodegenContext,
1508 mut module: lto::LtoModuleCodegen,
1509 module_config: &ModuleConfig,
1510 timeline: &mut Timeline)
1511 -> Result<WorkItemResult, FatalError>
1513 let diag_handler = cgcx.create_diag_handler();
1516 let module = module.optimize(cgcx, timeline)?;
1517 let module = codegen(cgcx, &diag_handler, module, module_config, timeline)?;
1518 Ok(WorkItemResult::Compiled(module))
1523 Token(io::Result<Acquired>),
1525 result: ModuleCodegen,
1529 result: Result<CompiledModule, ()>,
1533 llvm_work_item: WorkItem,
1536 AddImportOnlyModule {
1537 module_data: SerializedModule,
1538 work_product: WorkProduct,
1547 code: Option<DiagnosticId>,
1551 #[derive(PartialEq, Clone, Copy, Debug)]
1552 enum MainThreadWorkerState {
1558 fn start_executing_work(tcx: TyCtxt,
1559 crate_info: &CrateInfo,
1560 shared_emitter: SharedEmitter,
1561 codegen_worker_send: Sender<Message>,
1562 coordinator_receive: Receiver<Box<dyn Any + Send>>,
1565 time_graph: Option<TimeGraph>,
1566 modules_config: Arc<ModuleConfig>,
1567 metadata_config: Arc<ModuleConfig>,
1568 allocator_config: Arc<ModuleConfig>)
1569 -> thread::JoinHandle<Result<CompiledModules, ()>> {
1570 let coordinator_send = tcx.tx_to_llvm_workers.lock().clone();
1571 let sess = tcx.sess;
1573 // Compute the set of symbols we need to retain when doing LTO (if we need to)
1574 let exported_symbols = {
1575 let mut exported_symbols = FxHashMap::default();
1577 let copy_symbols = |cnum| {
1578 let symbols = tcx.exported_symbols(cnum)
1580 .map(|&(s, lvl)| (s.symbol_name(tcx).to_string(), lvl))
1588 exported_symbols.insert(LOCAL_CRATE, copy_symbols(LOCAL_CRATE));
1589 Some(Arc::new(exported_symbols))
1591 Lto::Fat | Lto::Thin => {
1592 exported_symbols.insert(LOCAL_CRATE, copy_symbols(LOCAL_CRATE));
1593 for &cnum in tcx.crates().iter() {
1594 exported_symbols.insert(cnum, copy_symbols(cnum));
1596 Some(Arc::new(exported_symbols))
1601 // First up, convert our jobserver into a helper thread so we can use normal
1602 // mpsc channels to manage our messages and such.
1603 // After we've requested tokens then we'll, when we can,
1604 // get tokens on `coordinator_receive` which will
1605 // get managed in the main loop below.
1606 let coordinator_send2 = coordinator_send.clone();
1607 let helper = jobserver.into_helper_thread(move |token| {
1608 drop(coordinator_send2.send(Box::new(Message::Token(token))));
1609 }).expect("failed to spawn helper thread");
1611 let mut each_linked_rlib_for_lto = Vec::new();
1612 drop(link::each_linked_rlib(sess, crate_info, &mut |cnum, path| {
1613 if link::ignored_for_lto(sess, crate_info, cnum) {
1616 each_linked_rlib_for_lto.push((cnum, path.to_path_buf()));
1619 let assembler_cmd = if modules_config.no_integrated_as {
1620 // HACK: currently we use linker (gcc) as our assembler
1621 let (linker, flavor) = link::linker_and_flavor(sess);
1623 let (name, mut cmd) = get_linker(sess, &linker, flavor);
1624 cmd.args(&sess.target.target.options.asm_args);
1626 Some(Arc::new(AssemblerCommand { name, cmd }))
1631 let cgcx = CodegenContext {
1632 crate_types: sess.crate_types.borrow().clone(),
1633 each_linked_rlib_for_lto,
1635 no_landing_pads: sess.no_landing_pads(),
1636 fewer_names: sess.fewer_names(),
1637 save_temps: sess.opts.cg.save_temps,
1638 opts: Arc::new(sess.opts.clone()),
1639 time_passes: sess.time_passes(),
1641 plugin_passes: sess.plugin_llvm_passes.borrow().clone(),
1642 remark: sess.opts.cg.remark.clone(),
1644 incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),
1645 cgu_reuse_tracker: sess.cgu_reuse_tracker.clone(),
1647 diag_emitter: shared_emitter.clone(),
1649 output_filenames: tcx.output_filenames(LOCAL_CRATE),
1650 regular_module_config: modules_config,
1651 metadata_module_config: metadata_config,
1652 allocator_module_config: allocator_config,
1653 tm_factory: target_machine_factory(tcx.sess, false),
1655 msvc_imps_needed: msvc_imps_needed(tcx),
1656 target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(),
1657 debuginfo: tcx.sess.opts.debuginfo,
1659 phantom: PhantomData
1662 // This is the "main loop" of parallel work happening for parallel codegen.
1663 // It's here that we manage parallelism, schedule work, and work with
1664 // messages coming from clients.
1666 // There are a few environmental pre-conditions that shape how the system
1669 // - Error reporting only can happen on the main thread because that's the
1670 // only place where we have access to the compiler `Session`.
1671 // - LLVM work can be done on any thread.
1672 // - Codegen can only happen on the main thread.
1673 // - Each thread doing substantial work most be in possession of a `Token`
1674 // from the `Jobserver`.
1675 // - The compiler process always holds one `Token`. Any additional `Tokens`
1676 // have to be requested from the `Jobserver`.
1680 // The error reporting restriction is handled separately from the rest: We
1681 // set up a `SharedEmitter` the holds an open channel to the main thread.
1682 // When an error occurs on any thread, the shared emitter will send the
1683 // error message to the receiver main thread (`SharedEmitterMain`). The
1684 // main thread will periodically query this error message queue and emit
1685 // any error messages it has received. It might even abort compilation if
1686 // has received a fatal error. In this case we rely on all other threads
1687 // being torn down automatically with the main thread.
1688 // Since the main thread will often be busy doing codegen work, error
1689 // reporting will be somewhat delayed, since the message queue can only be
1690 // checked in between to work packages.
1692 // Work Processing Infrastructure
1693 // ==============================
1694 // The work processing infrastructure knows three major actors:
1696 // - the coordinator thread,
1697 // - the main thread, and
1698 // - LLVM worker threads
1700 // The coordinator thread is running a message loop. It instructs the main
1701 // thread about what work to do when, and it will spawn off LLVM worker
1702 // threads as open LLVM WorkItems become available.
1704 // The job of the main thread is to codegen CGUs into LLVM work package
1705 // (since the main thread is the only thread that can do this). The main
1706 // thread will block until it receives a message from the coordinator, upon
1707 // which it will codegen one CGU, send it to the coordinator and block
1708 // again. This way the coordinator can control what the main thread is
1711 // The coordinator keeps a queue of LLVM WorkItems, and when a `Token` is
1712 // available, it will spawn off a new LLVM worker thread and let it process
1713 // that a WorkItem. When a LLVM worker thread is done with its WorkItem,
1714 // it will just shut down, which also frees all resources associated with
1715 // the given LLVM module, and sends a message to the coordinator that the
1716 // has been completed.
1720 // The scheduler's goal is to minimize the time it takes to complete all
1721 // work there is, however, we also want to keep memory consumption low
1722 // if possible. These two goals are at odds with each other: If memory
1723 // consumption were not an issue, we could just let the main thread produce
1724 // LLVM WorkItems at full speed, assuring maximal utilization of
1725 // Tokens/LLVM worker threads. However, since codegen usual is faster
1726 // than LLVM processing, the queue of LLVM WorkItems would fill up and each
1727 // WorkItem potentially holds on to a substantial amount of memory.
1729 // So the actual goal is to always produce just enough LLVM WorkItems as
1730 // not to starve our LLVM worker threads. That means, once we have enough
1731 // WorkItems in our queue, we can block the main thread, so it does not
1732 // produce more until we need them.
1734 // Doing LLVM Work on the Main Thread
1735 // ----------------------------------
1736 // Since the main thread owns the compiler processes implicit `Token`, it is
1737 // wasteful to keep it blocked without doing any work. Therefore, what we do
1738 // in this case is: We spawn off an additional LLVM worker thread that helps
1739 // reduce the queue. The work it is doing corresponds to the implicit
1740 // `Token`. The coordinator will mark the main thread as being busy with
1741 // LLVM work. (The actual work happens on another OS thread but we just care
1742 // about `Tokens`, not actual threads).
1744 // When any LLVM worker thread finishes while the main thread is marked as
1745 // "busy with LLVM work", we can do a little switcheroo: We give the Token
1746 // of the just finished thread to the LLVM worker thread that is working on
1747 // behalf of the main thread's implicit Token, thus freeing up the main
1748 // thread again. The coordinator can then again decide what the main thread
1749 // should do. This allows the coordinator to make decisions at more points
1752 // Striking a Balance between Throughput and Memory Consumption
1753 // ------------------------------------------------------------
1754 // Since our two goals, (1) use as many Tokens as possible and (2) keep
1755 // memory consumption as low as possible, are in conflict with each other,
1756 // we have to find a trade off between them. Right now, the goal is to keep
1757 // all workers busy, which means that no worker should find the queue empty
1758 // when it is ready to start.
1759 // How do we do achieve this? Good question :) We actually never know how
1760 // many `Tokens` are potentially available so it's hard to say how much to
1761 // fill up the queue before switching the main thread to LLVM work. Also we
1762 // currently don't have a means to estimate how long a running LLVM worker
1763 // will still be busy with it's current WorkItem. However, we know the
1764 // maximal count of available Tokens that makes sense (=the number of CPU
1765 // cores), so we can take a conservative guess. The heuristic we use here
1766 // is implemented in the `queue_full_enough()` function.
1768 // Some Background on Jobservers
1769 // -----------------------------
1770 // It's worth also touching on the management of parallelism here. We don't
1771 // want to just spawn a thread per work item because while that's optimal
1772 // parallelism it may overload a system with too many threads or violate our
1773 // configuration for the maximum amount of cpu to use for this process. To
1774 // manage this we use the `jobserver` crate.
1776 // Job servers are an artifact of GNU make and are used to manage
1777 // parallelism between processes. A jobserver is a glorified IPC semaphore
1778 // basically. Whenever we want to run some work we acquire the semaphore,
1779 // and whenever we're done with that work we release the semaphore. In this
1780 // manner we can ensure that the maximum number of parallel workers is
1781 // capped at any one point in time.
1783 // LTO and the coordinator thread
1784 // ------------------------------
1786 // The final job the coordinator thread is responsible for is managing LTO
1787 // and how that works. When LTO is requested what we'll to is collect all
1788 // optimized LLVM modules into a local vector on the coordinator. Once all
1789 // modules have been codegened and optimized we hand this to the `lto`
1790 // module for further optimization. The `lto` module will return back a list
1791 // of more modules to work on, which the coordinator will continue to spawn
1794 // Each LLVM module is automatically sent back to the coordinator for LTO if
1795 // necessary. There's already optimizations in place to avoid sending work
1796 // back to the coordinator if LTO isn't requested.
1797 return thread::spawn(move || {
1798 // We pretend to be within the top-level LLVM time-passes task here:
1801 let max_workers = ::num_cpus::get();
1802 let mut worker_id_counter = 0;
1803 let mut free_worker_ids = Vec::new();
1804 let mut get_worker_id = |free_worker_ids: &mut Vec<usize>| {
1805 if let Some(id) = free_worker_ids.pop() {
1808 let id = worker_id_counter;
1809 worker_id_counter += 1;
1814 // This is where we collect codegen units that have gone all the way
1815 // through codegen and LLVM.
1816 let mut compiled_modules = vec![];
1817 let mut compiled_metadata_module = None;
1818 let mut compiled_allocator_module = None;
1819 let mut needs_lto = Vec::new();
1820 let mut lto_import_only_modules = Vec::new();
1821 let mut started_lto = false;
1822 let mut codegen_aborted = false;
1824 // This flag tracks whether all items have gone through codegens
1825 let mut codegen_done = false;
1827 // This is the queue of LLVM work items that still need processing.
1828 let mut work_items = Vec::<(WorkItem, u64)>::new();
1830 // This are the Jobserver Tokens we currently hold. Does not include
1831 // the implicit Token the compiler process owns no matter what.
1832 let mut tokens = Vec::new();
1834 let mut main_thread_worker_state = MainThreadWorkerState::Idle;
1835 let mut running = 0;
1837 let mut llvm_start_time = None;
1839 // Run the message loop while there's still anything that needs message
1840 // processing. Note that as soon as codegen is aborted we simply want to
1841 // wait for all existing work to finish, so many of the conditions here
1842 // only apply if codegen hasn't been aborted as they represent pending
1844 while !codegen_done ||
1846 (!codegen_aborted && (
1847 work_items.len() > 0 ||
1848 needs_lto.len() > 0 ||
1849 lto_import_only_modules.len() > 0 ||
1850 main_thread_worker_state != MainThreadWorkerState::Idle
1854 // While there are still CGUs to be codegened, the coordinator has
1855 // to decide how to utilize the compiler processes implicit Token:
1856 // For codegenning more CGU or for running them through LLVM.
1858 if main_thread_worker_state == MainThreadWorkerState::Idle {
1859 if !queue_full_enough(work_items.len(), running, max_workers) {
1860 // The queue is not full enough, codegen more items:
1861 if let Err(_) = codegen_worker_send.send(Message::CodegenItem) {
1862 panic!("Could not send Message::CodegenItem to main thread")
1864 main_thread_worker_state = MainThreadWorkerState::Codegenning;
1866 // The queue is full enough to not let the worker
1867 // threads starve. Use the implicit Token to do some
1869 let (item, _) = work_items.pop()
1870 .expect("queue empty - queue_full_enough() broken?");
1871 let cgcx = CodegenContext {
1872 worker: get_worker_id(&mut free_worker_ids),
1875 maybe_start_llvm_timer(cgcx.config(item.module_kind()),
1876 &mut llvm_start_time);
1877 main_thread_worker_state = MainThreadWorkerState::LLVMing;
1878 spawn_work(cgcx, item);
1881 } else if codegen_aborted {
1882 // don't queue up any more work if codegen was aborted, we're
1883 // just waiting for our existing children to finish
1885 // If we've finished everything related to normal codegen
1886 // then it must be the case that we've got some LTO work to do.
1887 // Perform the serial work here of figuring out what we're
1888 // going to LTO and then push a bunch of work items onto our
1890 if work_items.len() == 0 &&
1892 main_thread_worker_state == MainThreadWorkerState::Idle {
1893 assert!(!started_lto);
1894 assert!(needs_lto.len() + lto_import_only_modules.len() > 0);
1896 let modules = mem::replace(&mut needs_lto, Vec::new());
1897 let import_only_modules =
1898 mem::replace(&mut lto_import_only_modules, Vec::new());
1899 for (work, cost) in generate_lto_work(&cgcx, modules, import_only_modules) {
1900 let insertion_index = work_items
1901 .binary_search_by_key(&cost, |&(_, cost)| cost)
1902 .unwrap_or_else(|e| e);
1903 work_items.insert(insertion_index, (work, cost));
1904 if !cgcx.opts.debugging_opts.no_parallel_llvm {
1905 helper.request_token();
1910 // In this branch, we know that everything has been codegened,
1911 // so it's just a matter of determining whether the implicit
1912 // Token is free to use for LLVM work.
1913 match main_thread_worker_state {
1914 MainThreadWorkerState::Idle => {
1915 if let Some((item, _)) = work_items.pop() {
1916 let cgcx = CodegenContext {
1917 worker: get_worker_id(&mut free_worker_ids),
1920 maybe_start_llvm_timer(cgcx.config(item.module_kind()),
1921 &mut llvm_start_time);
1922 main_thread_worker_state = MainThreadWorkerState::LLVMing;
1923 spawn_work(cgcx, item);
1925 // There is no unstarted work, so let the main thread
1926 // take over for a running worker. Otherwise the
1927 // implicit token would just go to waste.
1928 // We reduce the `running` counter by one. The
1929 // `tokens.truncate()` below will take care of
1930 // giving the Token back.
1931 debug_assert!(running > 0);
1933 main_thread_worker_state = MainThreadWorkerState::LLVMing;
1936 MainThreadWorkerState::Codegenning => {
1937 bug!("codegen worker should not be codegenning after \
1938 codegen was already completed")
1940 MainThreadWorkerState::LLVMing => {
1941 // Already making good use of that token
1946 // Spin up what work we can, only doing this while we've got available
1947 // parallelism slots and work left to spawn.
1948 while !codegen_aborted && work_items.len() > 0 && running < tokens.len() {
1949 let (item, _) = work_items.pop().unwrap();
1951 maybe_start_llvm_timer(cgcx.config(item.module_kind()),
1952 &mut llvm_start_time);
1954 let cgcx = CodegenContext {
1955 worker: get_worker_id(&mut free_worker_ids),
1959 spawn_work(cgcx, item);
1963 // Relinquish accidentally acquired extra tokens
1964 tokens.truncate(running);
1966 let msg = coordinator_receive.recv().unwrap();
1967 match *msg.downcast::<Message>().ok().unwrap() {
1968 // Save the token locally and the next turn of the loop will use
1969 // this to spawn a new unit of work, or it may get dropped
1970 // immediately if we have no more work to spawn.
1971 Message::Token(token) => {
1976 if main_thread_worker_state == MainThreadWorkerState::LLVMing {
1977 // If the main thread token is used for LLVM work
1978 // at the moment, we turn that thread into a regular
1979 // LLVM worker thread, so the main thread is free
1980 // to react to codegen demand.
1981 main_thread_worker_state = MainThreadWorkerState::Idle;
1986 let msg = &format!("failed to acquire jobserver token: {}", e);
1987 shared_emitter.fatal(msg);
1988 // Exit the coordinator thread
1994 Message::CodegenDone { llvm_work_item, cost } => {
1995 // We keep the queue sorted by estimated processing cost,
1996 // so that more expensive items are processed earlier. This
1997 // is good for throughput as it gives the main thread more
1998 // time to fill up the queue and it avoids scheduling
1999 // expensive items to the end.
2000 // Note, however, that this is not ideal for memory
2001 // consumption, as LLVM module sizes are not evenly
2003 let insertion_index =
2004 work_items.binary_search_by_key(&cost, |&(_, cost)| cost);
2005 let insertion_index = match insertion_index {
2006 Ok(idx) | Err(idx) => idx
2008 work_items.insert(insertion_index, (llvm_work_item, cost));
2010 if !cgcx.opts.debugging_opts.no_parallel_llvm {
2011 helper.request_token();
2013 assert!(!codegen_aborted);
2014 assert_eq!(main_thread_worker_state,
2015 MainThreadWorkerState::Codegenning);
2016 main_thread_worker_state = MainThreadWorkerState::Idle;
2019 Message::CodegenComplete => {
2020 codegen_done = true;
2021 assert!(!codegen_aborted);
2022 assert_eq!(main_thread_worker_state,
2023 MainThreadWorkerState::Codegenning);
2024 main_thread_worker_state = MainThreadWorkerState::Idle;
2027 // If codegen is aborted that means translation was aborted due
2028 // to some normal-ish compiler error. In this situation we want
2029 // to exit as soon as possible, but we want to make sure all
2030 // existing work has finished. Flag codegen as being done, and
2031 // then conditions above will ensure no more work is spawned but
2032 // we'll keep executing this loop until `running` hits 0.
2033 Message::CodegenAborted => {
2034 assert!(!codegen_aborted);
2035 codegen_done = true;
2036 codegen_aborted = true;
2037 assert_eq!(main_thread_worker_state,
2038 MainThreadWorkerState::Codegenning);
2041 // If a thread exits successfully then we drop a token associated
2042 // with that worker and update our `running` count. We may later
2043 // re-acquire a token to continue running more work. We may also not
2044 // actually drop a token here if the worker was running with an
2045 // "ephemeral token"
2047 // Note that if the thread failed that means it panicked, so we
2048 // abort immediately.
2049 Message::Done { result: Ok(compiled_module), worker_id } => {
2050 if main_thread_worker_state == MainThreadWorkerState::LLVMing {
2051 main_thread_worker_state = MainThreadWorkerState::Idle;
2056 free_worker_ids.push(worker_id);
2058 match compiled_module.kind {
2059 ModuleKind::Regular => {
2060 compiled_modules.push(compiled_module);
2062 ModuleKind::Metadata => {
2063 assert!(compiled_metadata_module.is_none());
2064 compiled_metadata_module = Some(compiled_module);
2066 ModuleKind::Allocator => {
2067 assert!(compiled_allocator_module.is_none());
2068 compiled_allocator_module = Some(compiled_module);
2072 Message::NeedsLTO { result, worker_id } => {
2073 assert!(!started_lto);
2074 if main_thread_worker_state == MainThreadWorkerState::LLVMing {
2075 main_thread_worker_state = MainThreadWorkerState::Idle;
2079 free_worker_ids.push(worker_id);
2080 needs_lto.push(result);
2082 Message::AddImportOnlyModule { module_data, work_product } => {
2083 assert!(!started_lto);
2084 assert!(!codegen_done);
2085 assert_eq!(main_thread_worker_state,
2086 MainThreadWorkerState::Codegenning);
2087 lto_import_only_modules.push((module_data, work_product));
2088 main_thread_worker_state = MainThreadWorkerState::Idle;
2090 Message::Done { result: Err(()), worker_id: _ } => {
2091 bug!("worker thread panicked");
2093 Message::CodegenItem => {
2094 bug!("the coordinator should not receive codegen requests")
2099 if let Some(llvm_start_time) = llvm_start_time {
2100 let total_llvm_time = Instant::now().duration_since(llvm_start_time);
2101 // This is the top-level timing for all of LLVM, set the time-depth
2104 print_time_passes_entry(cgcx.time_passes,
2109 // Regardless of what order these modules completed in, report them to
2110 // the backend in the same order every time to ensure that we're handing
2111 // out deterministic results.
2112 compiled_modules.sort_by(|a, b| a.name.cmp(&b.name));
2114 let compiled_metadata_module = compiled_metadata_module
2115 .expect("Metadata module not compiled?");
2117 Ok(CompiledModules {
2118 modules: compiled_modules,
2119 metadata_module: compiled_metadata_module,
2120 allocator_module: compiled_allocator_module,
2124 // A heuristic that determines if we have enough LLVM WorkItems in the
2125 // queue so that the main thread can do LLVM work instead of codegen
2126 fn queue_full_enough(items_in_queue: usize,
2127 workers_running: usize,
2128 max_workers: usize) -> bool {
2130 items_in_queue > 0 &&
2131 items_in_queue >= max_workers.saturating_sub(workers_running / 2)
2134 fn maybe_start_llvm_timer(config: &ModuleConfig,
2135 llvm_start_time: &mut Option<Instant>) {
2136 // We keep track of the -Ztime-passes output manually,
2137 // since the closure-based interface does not fit well here.
2138 if config.time_passes {
2139 if llvm_start_time.is_none() {
2140 *llvm_start_time = Some(Instant::now());
2146 pub const CODEGEN_WORKER_ID: usize = ::std::usize::MAX;
2147 pub const CODEGEN_WORKER_TIMELINE: time_graph::TimelineId =
2148 time_graph::TimelineId(CODEGEN_WORKER_ID);
2149 pub const CODEGEN_WORK_PACKAGE_KIND: time_graph::WorkPackageKind =
2150 time_graph::WorkPackageKind(&["#DE9597", "#FED1D3", "#FDC5C7", "#B46668", "#88494B"]);
2151 const LLVM_WORK_PACKAGE_KIND: time_graph::WorkPackageKind =
2152 time_graph::WorkPackageKind(&["#7DB67A", "#C6EEC4", "#ACDAAA", "#579354", "#3E6F3C"]);
2154 fn spawn_work(cgcx: CodegenContext<'static>, work: WorkItem) {
2155 let depth = time_depth();
2157 thread::spawn(move || {
2158 set_time_depth(depth);
2160 // Set up a destructor which will fire off a message that we're done as
2163 coordinator_send: Sender<Box<dyn Any + Send>>,
2164 result: Option<WorkItemResult>,
2167 impl Drop for Bomb {
2168 fn drop(&mut self) {
2169 let worker_id = self.worker_id;
2170 let msg = match self.result.take() {
2171 Some(WorkItemResult::Compiled(m)) => {
2172 Message::Done { result: Ok(m), worker_id }
2174 Some(WorkItemResult::NeedsLTO(m)) => {
2175 Message::NeedsLTO { result: m, worker_id }
2177 None => Message::Done { result: Err(()), worker_id }
2179 drop(self.coordinator_send.send(Box::new(msg)));
2183 let mut bomb = Bomb {
2184 coordinator_send: cgcx.coordinator_send.clone(),
2186 worker_id: cgcx.worker,
2189 // Execute the work itself, and if it finishes successfully then flag
2190 // ourselves as a success as well.
2192 // Note that we ignore any `FatalError` coming out of `execute_work_item`,
2193 // as a diagnostic was already sent off to the main thread - just
2194 // surface that there was an error in this worker.
2196 let timeline = cgcx.time_graph.as_ref().map(|tg| {
2197 tg.start(time_graph::TimelineId(cgcx.worker),
2198 LLVM_WORK_PACKAGE_KIND,
2201 let mut timeline = timeline.unwrap_or(Timeline::noop());
2202 execute_work_item(&cgcx, work, &mut timeline).ok()
2207 pub fn run_assembler(cgcx: &CodegenContext, handler: &Handler, assembly: &Path, object: &Path) {
2208 let assembler = cgcx.assembler_cmd
2210 .expect("cgcx.assembler_cmd is missing?");
2212 let pname = &assembler.name;
2213 let mut cmd = assembler.cmd.clone();
2214 cmd.arg("-c").arg("-o").arg(object).arg(assembly);
2215 debug!("{:?}", cmd);
2217 match cmd.output() {
2219 if !prog.status.success() {
2220 let mut note = prog.stderr.clone();
2221 note.extend_from_slice(&prog.stdout);
2223 handler.struct_err(&format!("linking with `{}` failed: {}",
2226 .note(&format!("{:?}", &cmd))
2227 .note(str::from_utf8(¬e[..]).unwrap())
2229 handler.abort_if_errors();
2233 handler.err(&format!("could not exec the linker `{}`: {}", pname.display(), e));
2234 handler.abort_if_errors();
2239 pub unsafe fn with_llvm_pmb(llmod: &llvm::Module,
2240 config: &ModuleConfig,
2241 opt_level: llvm::CodeGenOptLevel,
2242 prepare_for_thin_lto: bool,
2243 f: &mut dyn FnMut(&llvm::PassManagerBuilder)) {
2246 // Create the PassManagerBuilder for LLVM. We configure it with
2247 // reasonable defaults and prepare it to actually populate the pass
2249 let builder = llvm::LLVMPassManagerBuilderCreate();
2250 let opt_size = config.opt_size.unwrap_or(llvm::CodeGenOptSizeNone);
2251 let inline_threshold = config.inline_threshold;
2253 let pgo_gen_path = config.pgo_gen.as_ref().map(|s| {
2254 let s = if s.is_empty() { "default_%m.profraw" } else { s };
2255 CString::new(s.as_bytes()).unwrap()
2258 let pgo_use_path = if config.pgo_use.is_empty() {
2261 Some(CString::new(config.pgo_use.as_bytes()).unwrap())
2264 llvm::LLVMRustConfigurePassManagerBuilder(
2267 config.merge_functions,
2268 config.vectorize_slp,
2269 config.vectorize_loop,
2270 prepare_for_thin_lto,
2271 pgo_gen_path.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
2272 pgo_use_path.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
2275 llvm::LLVMPassManagerBuilderSetSizeLevel(builder, opt_size as u32);
2277 if opt_size != llvm::CodeGenOptSizeNone {
2278 llvm::LLVMPassManagerBuilderSetDisableUnrollLoops(builder, 1);
2281 llvm::LLVMRustAddBuilderLibraryInfo(builder, llmod, config.no_builtins);
2283 // Here we match what clang does (kinda). For O0 we only inline
2284 // always-inline functions (but don't add lifetime intrinsics), at O1 we
2285 // inline with lifetime intrinsics, and O2+ we add an inliner with a
2286 // thresholds copied from clang.
2287 match (opt_level, opt_size, inline_threshold) {
2289 llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t as u32);
2291 (llvm::CodeGenOptLevel::Aggressive, ..) => {
2292 llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 275);
2294 (_, llvm::CodeGenOptSizeDefault, _) => {
2295 llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 75);
2297 (_, llvm::CodeGenOptSizeAggressive, _) => {
2298 llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 25);
2300 (llvm::CodeGenOptLevel::None, ..) => {
2301 llvm::LLVMRustAddAlwaysInlinePass(builder, false);
2303 (llvm::CodeGenOptLevel::Less, ..) => {
2304 llvm::LLVMRustAddAlwaysInlinePass(builder, true);
2306 (llvm::CodeGenOptLevel::Default, ..) => {
2307 llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 225);
2309 (llvm::CodeGenOptLevel::Other, ..) => {
2310 bug!("CodeGenOptLevel::Other selected")
2315 llvm::LLVMPassManagerBuilderDispose(builder);
2319 enum SharedEmitterMessage {
2320 Diagnostic(Diagnostic),
2321 InlineAsmError(u32, String),
2327 pub struct SharedEmitter {
2328 sender: Sender<SharedEmitterMessage>,
2331 pub struct SharedEmitterMain {
2332 receiver: Receiver<SharedEmitterMessage>,
2335 impl SharedEmitter {
2336 pub fn new() -> (SharedEmitter, SharedEmitterMain) {
2337 let (sender, receiver) = channel();
2339 (SharedEmitter { sender }, SharedEmitterMain { receiver })
2342 fn inline_asm_error(&self, cookie: u32, msg: String) {
2343 drop(self.sender.send(SharedEmitterMessage::InlineAsmError(cookie, msg)));
2346 fn fatal(&self, msg: &str) {
2347 drop(self.sender.send(SharedEmitterMessage::Fatal(msg.to_string())));
2351 impl Emitter for SharedEmitter {
2352 fn emit(&mut self, db: &DiagnosticBuilder) {
2353 drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
2355 code: db.code.clone(),
2358 for child in &db.children {
2359 drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
2360 msg: child.message(),
2365 drop(self.sender.send(SharedEmitterMessage::AbortIfErrors));
2369 impl SharedEmitterMain {
2370 pub fn check(&self, sess: &Session, blocking: bool) {
2372 let message = if blocking {
2373 match self.receiver.recv() {
2374 Ok(message) => Ok(message),
2378 match self.receiver.try_recv() {
2379 Ok(message) => Ok(message),
2385 Ok(SharedEmitterMessage::Diagnostic(diag)) => {
2386 let handler = sess.diagnostic();
2389 handler.emit_with_code(&MultiSpan::new(),
2395 handler.emit(&MultiSpan::new(),
2401 Ok(SharedEmitterMessage::InlineAsmError(cookie, msg)) => {
2402 match Mark::from_u32(cookie).expn_info() {
2403 Some(ei) => sess.span_err(ei.call_site, &msg),
2404 None => sess.err(&msg),
2407 Ok(SharedEmitterMessage::AbortIfErrors) => {
2408 sess.abort_if_errors();
2410 Ok(SharedEmitterMessage::Fatal(msg)) => {
2422 pub struct OngoingCodegen {
2425 metadata: EncodedMetadata,
2426 windows_subsystem: Option<String>,
2427 linker_info: LinkerInfo,
2428 crate_info: CrateInfo,
2429 time_graph: Option<TimeGraph>,
2430 coordinator_send: Sender<Box<dyn Any + Send>>,
2431 codegen_worker_receive: Receiver<Message>,
2432 shared_emitter_main: SharedEmitterMain,
2433 future: thread::JoinHandle<Result<CompiledModules, ()>>,
2434 output_filenames: Arc<OutputFilenames>,
2437 impl OngoingCodegen {
2441 ) -> (CodegenResults, FxHashMap<WorkProductId, WorkProduct>) {
2442 self.shared_emitter_main.check(sess, true);
2443 let compiled_modules = match self.future.join() {
2444 Ok(Ok(compiled_modules)) => compiled_modules,
2446 sess.abort_if_errors();
2447 panic!("expected abort due to worker thread errors")
2450 bug!("panic during codegen/LLVM phase");
2454 sess.cgu_reuse_tracker.check_expected_reuse(sess);
2456 sess.abort_if_errors();
2458 if let Some(time_graph) = self.time_graph {
2459 time_graph.dump(&format!("{}-timings", self.crate_name));
2463 copy_all_cgu_workproducts_to_incr_comp_cache_dir(sess,
2465 produce_final_output_artifacts(sess,
2467 &self.output_filenames);
2469 // FIXME: time_llvm_passes support - does this use a global context or
2471 if sess.codegen_units() == 1 && sess.time_llvm_passes() {
2472 unsafe { llvm::LLVMRustPrintPassTimings(); }
2476 crate_name: self.crate_name,
2477 crate_hash: self.crate_hash,
2478 metadata: self.metadata,
2479 windows_subsystem: self.windows_subsystem,
2480 linker_info: self.linker_info,
2481 crate_info: self.crate_info,
2483 modules: compiled_modules.modules,
2484 allocator_module: compiled_modules.allocator_module,
2485 metadata_module: compiled_modules.metadata_module,
2489 pub(crate) fn submit_pre_codegened_module_to_llvm(&self,
2491 module: ModuleCodegen) {
2492 self.wait_for_signal_to_codegen_item();
2493 self.check_for_errors(tcx.sess);
2495 // These are generally cheap and won't through off scheduling.
2497 submit_codegened_module_to_llvm(tcx, module, cost);
2500 pub fn codegen_finished(&self, tcx: TyCtxt) {
2501 self.wait_for_signal_to_codegen_item();
2502 self.check_for_errors(tcx.sess);
2503 drop(self.coordinator_send.send(Box::new(Message::CodegenComplete)));
2506 /// Consume this context indicating that codegen was entirely aborted, and
2507 /// we need to exit as quickly as possible.
2509 /// This method blocks the current thread until all worker threads have
2510 /// finished, and all worker threads should have exited or be real close to
2511 /// exiting at this point.
2512 pub fn codegen_aborted(self) {
2513 // Signal to the coordinator it should spawn no more work and start
2515 drop(self.coordinator_send.send(Box::new(Message::CodegenAborted)));
2516 drop(self.future.join());
2519 pub fn check_for_errors(&self, sess: &Session) {
2520 self.shared_emitter_main.check(sess, false);
2523 pub fn wait_for_signal_to_codegen_item(&self) {
2524 match self.codegen_worker_receive.recv() {
2525 Ok(Message::CodegenItem) => {
2528 Ok(_) => panic!("unexpected message"),
2530 // One of the LLVM threads must have panicked, fall through so
2531 // error handling can be reached.
2537 // impl Drop for OngoingCodegen {
2538 // fn drop(&mut self) {
2542 pub(crate) fn submit_codegened_module_to_llvm(tcx: TyCtxt,
2543 module: ModuleCodegen,
2545 let llvm_work_item = WorkItem::Optimize(module);
2546 drop(tcx.tx_to_llvm_workers.lock().send(Box::new(Message::CodegenDone {
2552 pub(crate) fn submit_post_lto_module_to_llvm(tcx: TyCtxt,
2553 module: CachedModuleCodegen) {
2554 let llvm_work_item = WorkItem::CopyPostLtoArtifacts(module);
2555 drop(tcx.tx_to_llvm_workers.lock().send(Box::new(Message::CodegenDone {
2561 pub(crate) fn submit_pre_lto_module_to_llvm(tcx: TyCtxt,
2562 module: CachedModuleCodegen) {
2563 let filename = pre_lto_bitcode_filename(&module.name);
2564 let bc_path = in_incr_comp_dir_sess(tcx.sess, &filename);
2565 let file = fs::File::open(&bc_path).unwrap_or_else(|e| {
2566 panic!("failed to open bitcode file `{}`: {}", bc_path.display(), e)
2570 memmap::Mmap::map(&file).unwrap_or_else(|e| {
2571 panic!("failed to mmap bitcode file `{}`: {}", bc_path.display(), e)
2575 // Schedule the module to be loaded
2576 drop(tcx.tx_to_llvm_workers.lock().send(Box::new(Message::AddImportOnlyModule {
2577 module_data: SerializedModule::FromUncompressedFile(mmap),
2578 work_product: module.source,
2582 pub(super) fn pre_lto_bitcode_filename(module_name: &str) -> String {
2583 format!("{}.{}", module_name, PRE_THIN_LTO_BC_EXT)
2586 fn msvc_imps_needed(tcx: TyCtxt) -> bool {
2587 // This should never be true (because it's not supported). If it is true,
2588 // something is wrong with commandline arg validation.
2589 assert!(!(tcx.sess.opts.debugging_opts.cross_lang_lto.enabled() &&
2590 tcx.sess.target.target.options.is_like_msvc &&
2591 tcx.sess.opts.cg.prefer_dynamic));
2593 tcx.sess.target.target.options.is_like_msvc &&
2594 tcx.sess.crate_types.borrow().iter().any(|ct| *ct == config::CrateType::Rlib) &&
2595 // ThinLTO can't handle this workaround in all cases, so we don't
2596 // emit the `__imp_` symbols. Instead we make them unnecessary by disallowing
2597 // dynamic linking when cross-language LTO is enabled.
2598 !tcx.sess.opts.debugging_opts.cross_lang_lto.enabled()
2601 // Create a `__imp_<symbol> = &symbol` global for every public static `symbol`.
2602 // This is required to satisfy `dllimport` references to static data in .rlibs
2603 // when using MSVC linker. We do this only for data, as linker can fix up
2604 // code references on its own.
2605 // See #26591, #27438
2606 fn create_msvc_imps(cgcx: &CodegenContext, llcx: &llvm::Context, llmod: &llvm::Module) {
2607 if !cgcx.msvc_imps_needed {
2610 // The x86 ABI seems to require that leading underscores are added to symbol
2611 // names, so we need an extra underscore on 32-bit. There's also a leading
2612 // '\x01' here which disables LLVM's symbol mangling (e.g. no extra
2613 // underscores added in front).
2614 let prefix = if cgcx.target_pointer_width == "32" {
2620 let i8p_ty = Type::i8p_llcx(cgcx, llcx);
2621 let globals = base::iter_globals(llmod)
2623 llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage &&
2624 llvm::LLVMIsDeclaration(val) == 0
2627 let name = CStr::from_ptr(llvm::LLVMGetValueName(val));
2628 let mut imp_name = prefix.as_bytes().to_vec();
2629 imp_name.extend(name.to_bytes());
2630 let imp_name = CString::new(imp_name).unwrap();
2633 .collect::<Vec<_>>();
2634 for (imp_name, val) in globals {
2635 let imp = llvm::LLVMAddGlobal(llmod,
2637 imp_name.as_ptr() as *const _);
2638 llvm::LLVMSetInitializer(imp, consts::ptrcast(val, i8p_ty));
2639 llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);