]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/back/write.rs
9eaafb1c21d3ed0acf1fe18eca78a8b06f3f083a
[rust.git] / src / librustc_codegen_llvm / back / write.rs
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.
4 //
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.
10
11 use attributes;
12 use back::bytecode::{self, RLIB_BYTECODE_EXTENSION};
13 use back::lto::{self, ThinBuffer, SerializedModule};
14 use back::link::{self, get_linker, remove};
15 use base;
16 use consts;
17 use memmap;
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};
28 use llvm_util;
29 use {CodegenResults, ModuleCodegen, CompiledModule, ModuleKind, // ModuleLlvm,
30      CachedModuleCodegen};
31 use CrateInfo;
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};
43 use syntax::attr;
44 use syntax::ext::hygiene::Mark;
45 use syntax_pos::MultiSpan;
46 use syntax_pos::symbol::Symbol;
47 use type_::Type;
48 use context::{is_pie_binary, get_reloc_model};
49 use interfaces::{Backend, CommonWriteMethods};
50 use common;
51 use jobserver::{Client, Acquired};
52 use rustc_demangle;
53 use value::Value;
54 use std::marker::PhantomData;
55
56 use std::any::Any;
57 use std::ffi::{CString, CStr};
58 use std::fs;
59 use std::io::{self, Write};
60 use std::mem;
61 use std::path::{Path, PathBuf};
62 use std::str;
63 use std::sync::Arc;
64 use std::sync::mpsc::{channel, Sender, Receiver};
65 use std::slice;
66 use std::time::Instant;
67 use std::thread;
68 use libc::{c_uint, c_void, c_char, size_t};
69
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),
78 ];
79
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),
85 ];
86
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),
92 ];
93
94 const PRE_THIN_LTO_BC_EXT: &str = "pre-thin-lto.bc";
95
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),
100     }
101 }
102
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,
108         output: &Path,
109         file_type: llvm::FileType) -> Result<(), FatalError> {
110     unsafe {
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))
116         } else {
117             Ok(())
118         }
119     }
120 }
121
122 fn get_llvm_opt_level(optimize: config::OptLevel) -> llvm::CodeGenOptLevel {
123     match optimize {
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,
129     }
130 }
131
132 fn get_llvm_opt_size(optimize: config::OptLevel) -> llvm::CodeGenOptSize {
133     match optimize {
134       config::OptLevel::Size => llvm::CodeGenOptSizeDefault,
135       config::OptLevel::SizeMin => llvm::CodeGenOptSizeAggressive,
136       _ => llvm::CodeGenOptSizeNone,
137     }
138 }
139
140 pub fn create_target_machine(
141     sess: &Session,
142     find_features: bool,
143 ) -> &'static mut llvm::TargetMachine {
144     target_machine_factory(sess, find_features)().unwrap_or_else(|err| {
145         llvm_err(sess.diagnostic(), &err).raise()
146     })
147 }
148
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>
154 {
155     let reloc_model = get_reloc_model(sess);
156
157     let opt_level = get_llvm_opt_level(sess.opts.optimize);
158     let use_softfp = sess.opts.cg.soft_float;
159
160     let ffunction_sections = sess.target.target.options.function_sections;
161     let fdata_sections = ffunction_sections;
162
163     let code_model_arg = sess.opts.cg.code_model.as_ref().or(
164         sess.target.target.options.code_model.as_ref(),
165     );
166
167     let code_model = match code_model_arg {
168         Some(s) => {
169             match CODE_GEN_MODEL_ARGS.iter().find(|arg| arg.0 == s) {
170                 Some(x) => x.1,
171                 _ => {
172                     sess.err(&format!("{:?} is not a valid code model",
173                                       code_model_arg));
174                     sess.abort_if_errors();
175                     bug!();
176                 }
177             }
178         }
179         None => llvm::CodeModel::None,
180     };
181
182     let features = attributes::llvm_target_features(sess).collect::<Vec<_>>();
183     let mut singlethread = sess.target.target.options.singlethread;
184
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.
188     if singlethread &&
189         sess.target.target.llvm_target.contains("wasm32") &&
190         features.iter().any(|s| *s == "+atomics")
191     {
192         singlethread = false;
193     }
194
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;
202
203     let asm_comments = sess.asm_comments();
204
205     Arc::new(move || {
206         let tm = unsafe {
207             llvm::LLVMRustCreateTargetMachine(
208                 triple.as_ptr(), cpu.as_ptr(), features.as_ptr(),
209                 code_model,
210                 reloc_model,
211                 opt_level,
212                 use_softfp,
213                 is_pie_binary,
214                 ffunction_sections,
215                 fdata_sections,
216                 trap_unreachable,
217                 singlethread,
218                 asm_comments,
219                 emit_stack_size_section,
220             )
221         };
222
223         tm.ok_or_else(|| {
224             format!("Could not create LLVM TargetMachine for triple: {}",
225                     triple.to_str().unwrap())
226         })
227     })
228 }
229
230 /// Module-specific configuration for `optimize_and_codegen`.
231 pub struct ModuleConfig {
232     /// Names of additional optimization passes to run.
233     passes: Vec<String>,
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>,
237
238     /// Some(level) to optimize binary size, or None to not affect program size.
239     opt_size: Option<llvm::CodeGenOptSize>,
240
241     pgo_gen: Option<String>,
242     pgo_use: String,
243
244     // Flags indicating which outputs to produce.
245     pub emit_pre_thin_lto_bc: bool,
246     emit_no_opt_bc: bool,
247     emit_bc: bool,
248     emit_bc_compressed: bool,
249     emit_lto_bc: bool,
250     emit_ir: bool,
251     emit_asm: bool,
252     emit_obj: bool,
253     // Miscellaneous flags.  These are mostly copied from command-line
254     // options.
255     pub verify_llvm_ir: bool,
256     no_prepopulate_passes: bool,
257     no_builtins: bool,
258     time_passes: bool,
259     vectorize_loop: bool,
260     vectorize_slp: 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,
268     embed_bitcode: bool,
269     embed_bitcode_marker: bool,
270 }
271
272 impl ModuleConfig {
273     fn new(passes: Vec<String>) -> ModuleConfig {
274         ModuleConfig {
275             passes,
276             opt_level: None,
277             opt_size: None,
278
279             pgo_gen: None,
280             pgo_use: String::new(),
281
282             emit_no_opt_bc: false,
283             emit_pre_thin_lto_bc: false,
284             emit_bc: false,
285             emit_bc_compressed: false,
286             emit_lto_bc: false,
287             emit_ir: false,
288             emit_asm: false,
289             emit_obj: false,
290             obj_is_bitcode: false,
291             embed_bitcode: false,
292             embed_bitcode_marker: false,
293             no_integrated_as: false,
294
295             verify_llvm_ir: false,
296             no_prepopulate_passes: false,
297             no_builtins: false,
298             time_passes: false,
299             vectorize_loop: false,
300             vectorize_slp: false,
301             merge_functions: false,
302             inline_threshold: None
303         }
304     }
305
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;
316         if embed_bitcode {
317             match sess.opts.optimize {
318                 config::OptLevel::No |
319                 config::OptLevel::Less => {
320                     self.embed_bitcode_marker = embed_bitcode;
321                 }
322                 _ => self.embed_bitcode = embed_bitcode,
323             }
324         }
325
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;
334
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;
338
339         self.merge_functions = sess.opts.optimize == config::OptLevel::Default ||
340                                sess.opts.optimize == config::OptLevel::Aggressive;
341     }
342
343     pub fn bitcode_needed(&self) -> bool {
344         self.emit_bc || self.obj_is_bitcode
345             || self.emit_bc_compressed || self.embed_bitcode
346     }
347 }
348
349 /// Assembler name and command used by codegen when no_integrated_as is enabled
350 struct AssemblerCommand {
351     name: PathBuf,
352     cmd: Command,
353 }
354
355 /// Additional resources used by optimize_and_codegen (not module specific)
356 #[derive(Clone)]
357 pub struct CodegenContext<'ll> {
358     // Resources needed when running LTO
359     pub time_passes: bool,
360     pub lto: Lto,
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,
376
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.
384     pub remark: Passes,
385     // Worker thread number
386     pub worker: usize,
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 ()>
402 }
403
404 impl CodegenContext<'ll> {
405     pub fn create_diag_handler(&self) -> Handler {
406         Handler::with_emitter(true, false, Box::new(self.diag_emitter.clone()))
407     }
408
409     pub(crate) fn config(&self, kind: ModuleKind) -> &ModuleConfig {
410         match kind {
411             ModuleKind::Regular => &self.regular_module_config,
412             ModuleKind::Metadata => &self.metadata_module_config,
413             ModuleKind::Allocator => &self.allocator_module_config,
414         }
415     }
416
417     pub(crate) fn save_temp_bitcode(&self, module: &ModuleCodegen, name: &str) {
418         if !self.save_temps {
419             return
420         }
421         unsafe {
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());
428         }
429     }
430 }
431
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;
438 }
439
440 impl CommonWriteMethods for CodegenContext<'ll> {
441     fn val_ty(&self, v: &'ll Value) -> &'ll Type {
442         common::val_ty(v)
443     }
444
445     fn const_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
446         common::const_bytes_in_context(llcx, bytes)
447     }
448
449     fn const_struct_in_context(
450         &self,
451         llcx: &'a llvm::Context,
452         elts: &[&'a Value],
453         packed: bool,
454     ) -> &'a Value {
455         common::const_struct_in_context(llcx, elts, packed)
456     }
457 }
458
459 impl CodegenContext<'ll> {
460     pub fn ptr_to(&self, ty: &'ll Type) -> &'ll Type {
461         unsafe {
462             llvm::LLVMPointerType(ty, 0)
463         }
464     }
465 }
466
467
468 pub struct DiagnosticHandlers<'a> {
469     data: *mut (&'a CodegenContext<'a>, &'a Handler),
470     llcx: &'a llvm::Context,
471 }
472
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)));
478         unsafe {
479             llvm::LLVMRustSetInlineAsmDiagnosticHandler(llcx, inline_asm_handler, data as *mut _);
480             llvm::LLVMContextSetDiagnosticHandler(llcx, diagnostic_handler, data as *mut _);
481         }
482         DiagnosticHandlers { data, llcx }
483     }
484 }
485
486 impl<'a> Drop for DiagnosticHandlers<'a> {
487     fn drop(&mut self) {
488         use std::ptr::null_mut;
489         unsafe {
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));
493         }
494     }
495 }
496
497 unsafe extern "C" fn report_inline_asm<'a, 'b>(cgcx: &'a CodegenContext,
498                                                msg: &'b str,
499                                                cookie: c_uint) {
500     cgcx.diag_emitter.inline_asm_error(cookie as u32, msg.to_owned());
501 }
502
503 unsafe extern "C" fn inline_asm_handler(diag: &SMDiagnostic,
504                                         user: *const c_void,
505                                         cookie: c_uint) {
506     if user.is_null() {
507         return
508     }
509     let (cgcx, _) = *(user as *const (&CodegenContext, &Handler));
510
511     let msg = llvm::build_string(|s| llvm::LLVMRustWriteSMDiagnosticToString(diag, s))
512         .expect("non-UTF8 SMDiagnostic");
513
514     report_inline_asm(cgcx, &msg, cookie);
515 }
516
517 unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void) {
518     if user.is_null() {
519         return
520     }
521     let (cgcx, diag_handler) = *(user as *const (&CodegenContext, &Handler));
522
523     match llvm::diagnostic::Diagnostic::unpack(info) {
524         llvm::diagnostic::InlineAsm(inline) => {
525             report_inline_asm(cgcx,
526                               &llvm::twine_to_string(inline.message),
527                               inline.cookie);
528         }
529
530         llvm::diagnostic::Optimization(opt) => {
531             let enabled = match cgcx.remark {
532                 Passes::All => true,
533                 Passes::Some(ref v) => v.iter().any(|s| *s == opt.pass_name),
534             };
535
536             if enabled {
537                 diag_handler.note_without_error(&format!("optimization {} for {} at {}:{}:{}: {}",
538                                                 opt.kind.describe(),
539                                                 opt.pass_name,
540                                                 opt.filename,
541                                                 opt.line,
542                                                 opt.column,
543                                                 opt.message));
544             }
545         }
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);
552         }
553         llvm::diagnostic::UnknownDiagnostic(..) => {},
554     }
555 }
556
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>
564 {
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);
569
570     let module_name = module.name.clone();
571     let module_name = Some(&module_name[..]);
572
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());
577     }
578
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();
586
587         {
588             // If we're verifying or linting, add them to the function pass
589             // manager.
590             let addpass = |pass_name: &str| {
591                 let pass_name = SmallCStr::new(pass_name);
592                 let pass = match llvm::LLVMRustFindAndCreatePass(pass_name.as_ptr()) {
593                     Some(pass) => pass,
594                     None => return false,
595                 };
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");
601                         return true
602                     },
603                 };
604                 llvm::LLVMRustAddPass(pass_manager, pass);
605                 true
606             };
607
608             if config.verify_llvm_ir { assert!(addpass("verify")); }
609
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;
625                 }
626                 with_llvm_pmb(llmod, &config, opt_level, prepare_for_thin_lto, &mut |b| {
627                     llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm);
628                     llvm::LLVMPassManagerBuilderPopulateModulePassManager(b, mpm);
629                 })
630             }
631
632             for pass in &config.passes {
633                 if !addpass(pass) {
634                     diag_handler.warn(&format!("unknown pass `{}`, ignoring", pass));
635                 }
636                 if pass == "name-anon-globals" {
637                     have_name_anon_globals_pass = true;
638                 }
639             }
640
641             for pass in &cgcx.plugin_passes {
642                 if !addpass(pass) {
643                     diag_handler.err(&format!("a plugin asked for LLVM pass \
644                                                `{}` but LLVM does not \
645                                                recognize it", pass));
646                 }
647                 if pass == "name-anon-globals" {
648                     have_name_anon_globals_pass = true;
649                 }
650             }
651
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.");
659                 } else {
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.");
662                 }
663             }
664         }
665
666         diag_handler.abort_if_errors();
667
668         // Finally, run the actual optimization passes
669         time_ext(config.time_passes,
670                  None,
671                  &format!("llvm function passes [{}]", module_name.unwrap()),
672                  || {
673             llvm::LLVMRustRunFunctionPassManager(fpm, llmod)
674         });
675         timeline.record("fpm");
676         time_ext(config.time_passes,
677                  None,
678                  &format!("llvm module passes [{}]", module_name.unwrap()),
679                  || {
680             llvm::LLVMRunPassManager(mpm, llmod)
681         });
682
683         // Deallocate managers that we're now done with
684         llvm::LLVMDisposePassManager(fpm);
685         llvm::LLVMDisposePassManager(mpm);
686     }
687     Ok(())
688 }
689
690 fn generate_lto_work(cgcx: &CodegenContext,
691                      modules: Vec<ModuleCodegen>,
692                      import_only_modules: Vec<(SerializedModule, WorkProduct)>)
693     -> Vec<(WorkItem, u64)>
694 {
695     let mut timeline = cgcx.time_graph.as_ref().map(|tg| {
696         tg.start(CODEGEN_WORKER_TIMELINE,
697                  CODEGEN_WORK_PACKAGE_KIND,
698                  "generate lto")
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());
702
703     let lto_modules = lto_modules.into_iter().map(|module| {
704         let cost = module.cost();
705         (WorkItem::LTO(module), cost)
706     });
707
708     let copy_jobs = copy_jobs.into_iter().map(|wp| {
709         (WorkItem::CopyPostLtoArtifacts(CachedModuleCodegen {
710             name: wp.cgu_name.clone(),
711             source: wp,
712         }), 0)
713     });
714
715     lto_modules.chain(copy_jobs).collect()
716 }
717
718 unsafe fn codegen(cgcx: &CodegenContext,
719                   diag_handler: &Handler,
720                   module: ModuleCodegen,
721                   config: &ModuleConfig,
722                   timeline: &mut Timeline)
723     -> Result<CompiledModule, FatalError>
724 {
725     timeline.record("codegen");
726     {
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);
733
734         if cgcx.msvc_imps_needed {
735             create_msvc_imps(cgcx, llcx, llmod);
736         }
737
738         // A codegen-specific pass manager is used to generate object
739         // files for an LLVM module.
740         //
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
745         // used once.
746         unsafe fn with_codegen<'ll, F, R>(tm: &'ll llvm::TargetMachine,
747                                           llmod: &'ll llvm::Module,
748                                           no_builtins: bool,
749                                           f: F) -> R
750             where F: FnOnce(&'ll mut PassManager<'ll>) -> R,
751         {
752             let cpm = llvm::LLVMCreatePassManager();
753             llvm::LLVMRustAddAnalysisPasses(tm, cpm, llmod);
754             llvm::LLVMRustAddLibraryInfo(cpm, llmod, no_builtins);
755             f(cpm)
756         }
757
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;
761
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;
770
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);
773
774
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");
779
780             if write_bc {
781                 if let Err(e) = fs::write(&bc_out, data) {
782                     diag_handler.err(&format!("failed to write bytecode: {}", e));
783                 }
784                 timeline.record("write-bc");
785             }
786
787             if config.embed_bitcode {
788                 embed_bitcode(cgcx, llcx, llmod, Some(data));
789                 timeline.record("embed-bc");
790             }
791
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));
797                 }
798                 timeline.record("compress-bc");
799             }
800         } else if config.embed_bitcode_marker {
801             embed_bitcode(cgcx, llcx, llmod, None);
802         }
803
804         time_ext(config.time_passes, None, &format!("codegen passes [{}]", module_name.unwrap()),
805             || -> Result<(), FatalError> {
806             if config.emit_ir {
807                 let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
808                 let out = path2cstr(&out);
809
810                 extern "C" fn demangle_callback(input_ptr: *const c_char,
811                                                 input_len: size_t,
812                                                 output_ptr: *mut c_char,
813                                                 output_len: size_t) -> size_t {
814                     let input = unsafe {
815                         slice::from_raw_parts(input_ptr as *const u8, input_len as usize)
816                     };
817
818                     let input = match str::from_utf8(input) {
819                         Ok(s) => s,
820                         Err(_) => return 0,
821                     };
822
823                     let output = unsafe {
824                         slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize)
825                     };
826                     let mut cursor = io::Cursor::new(output);
827
828                     let demangled = match rustc_demangle::try_demangle(input) {
829                         Ok(d) => d,
830                         Err(_) => return 0,
831                     };
832
833                     if let Err(_) = write!(cursor, "{:#}", demangled) {
834                         // Possible only if provided buffer is not big enough
835                         return 0;
836                     }
837
838                     cursor.position() as size_t
839                 }
840
841                 with_codegen(tm, llmod, config.no_builtins, |cpm| {
842                     llvm::LLVMRustPrintModule(cpm, llmod, out.as_ptr(), demangle_callback);
843                     llvm::LLVMDisposePassManager(cpm);
844                 });
845                 timeline.record("ir");
846             }
847
848             if config.emit_asm || asm_to_obj {
849                 let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
850
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)
856                 } else {
857                     llmod
858                 };
859                 with_codegen(tm, llmod, config.no_builtins, |cpm| {
860                     write_output_file(diag_handler, tm, cpm, llmod, &path,
861                                       llvm::FileType::AssemblyFile)
862                 })?;
863                 timeline.record("asm");
864             }
865
866             if write_obj {
867                 with_codegen(tm, llmod, config.no_builtins, |cpm| {
868                     write_output_file(diag_handler, tm, cpm, llmod, &obj_out,
869                                       llvm::FileType::ObjectFile)
870                 })?;
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");
876
877                 if !config.emit_asm && !cgcx.save_temps {
878                     drop(fs::remove_file(&assembly));
879                 }
880             }
881
882             Ok(())
883         })?;
884
885         if copy_bc_to_obj {
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));
889             }
890         }
891
892         if rm_bc {
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));
896             }
897         }
898
899         drop(handlers);
900     }
901     Ok(module.into_compiled_module(config.emit_obj,
902                                    config.emit_bc,
903                                    config.emit_bc_compressed,
904                                    &cgcx.output_filenames))
905 }
906
907 /// Embed the bitcode of an LLVM module in the LLVM module itself.
908 ///
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
911 /// executable:
912 ///
913 /// * __LLVM,__bitcode
914 /// * __LLVM,__cmdline
915 ///
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
918 /// cmdline section.
919 ///
920 /// Furthermore debug/O1 builds don't actually embed bitcode but rather just
921 /// embed an empty section.
922 ///
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(
931         llmod,
932         cgcx.val_ty(llconst),
933         "rustc.embedded.module\0".as_ptr() as *const _,
934     );
935     llvm::LLVMSetInitializer(llglobal, llconst);
936
937     let is_apple = cgcx.opts.target_triple.triple().contains("-ios") ||
938                    cgcx.opts.target_triple.triple().contains("-darwin");
939
940     let section = if is_apple {
941         "__LLVM,__bitcode\0"
942     } else {
943         ".llvmbc\0"
944     };
945     llvm::LLVMSetSection(llglobal, section.as_ptr() as *const _);
946     llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
947     llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
948
949     let llconst = cgcx.const_bytes_in_context(llcx, &[]);
950     let llglobal = llvm::LLVMAddGlobal(
951         llmod,
952         cgcx.val_ty(llconst),
953         "rustc.embedded.cmdline\0".as_ptr() as *const _,
954     );
955     llvm::LLVMSetInitializer(llglobal, llconst);
956     let section = if  is_apple {
957         "__LLVM,__cmdline\0"
958     } else {
959         ".llvmcmd\0"
960     };
961     llvm::LLVMSetSection(llglobal, section.as_ptr() as *const _);
962     llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
963 }
964
965 pub(crate) struct CompiledModules {
966     pub modules: Vec<CompiledModule>,
967     pub metadata_module: CompiledModule,
968     pub allocator_module: Option<CompiledModule>,
969 }
970
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)
974 }
975
976 fn need_pre_thin_lto_bitcode_for_incr_comp(sess: &Session) -> bool {
977     if sess.opts.incremental.is_none() {
978         return false
979     }
980
981     match sess.lto() {
982         Lto::Fat |
983         Lto::No => false,
984         Lto::Thin |
985         Lto::ThinLocal => true,
986     }
987 }
988
989 pub fn start_async_codegen(tcx: TyCtxt,
990                            time_graph: Option<TimeGraph>,
991                            metadata: EncodedMetadata,
992                            coordinator_receive: Receiver<Box<dyn Any + Send>>,
993                            total_cgus: usize)
994                            -> OngoingCodegen {
995     let sess = tcx.sess;
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",
1005                                     subsystem));
1006         }
1007         subsystem.to_string()
1008     });
1009
1010     let linker_info = LinkerInfo::new(tcx);
1011     let crate_info = CrateInfo::new(tcx);
1012
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![]);
1017
1018     if let Some(ref sanitizer) = sess.opts.debugging_opts.sanitizer {
1019         match *sanitizer {
1020             Sanitizer::Address => {
1021                 modules_config.passes.push("asan".to_owned());
1022                 modules_config.passes.push("asan-module".to_owned());
1023             }
1024             Sanitizer::Memory => {
1025                 modules_config.passes.push("msan".to_owned())
1026             }
1027             Sanitizer::Thread => {
1028                 modules_config.passes.push("tsan".to_owned())
1029             }
1030             _ => {}
1031         }
1032     }
1033
1034     if sess.opts.debugging_opts.profile {
1035         modules_config.passes.push("insert-gcov-profiling".to_owned())
1036     }
1037
1038     modules_config.pgo_gen = sess.opts.debugging_opts.pgo_gen.clone();
1039     modules_config.pgo_use = sess.opts.debugging_opts.pgo_use.clone();
1040
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));
1043
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;
1052     }
1053
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;
1060     }
1061
1062     modules_config.emit_pre_thin_lto_bc =
1063         need_pre_thin_lto_bitcode_for_incr_comp(sess);
1064
1065     modules_config.no_integrated_as = tcx.sess.opts.cg.no_integrated_as ||
1066         tcx.sess.target.target.options.no_integrated_as;
1067
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;
1080                 }
1081             }
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;
1088             },
1089             OutputType::Mir => {}
1090             OutputType::DepInfo => {}
1091         }
1092     }
1093
1094     modules_config.set_flags(sess, no_builtins);
1095     metadata_config.set_flags(sess, no_builtins);
1096     allocator_config.set_flags(sess, no_builtins);
1097
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;
1102
1103     let (shared_emitter, shared_emitter_main) = SharedEmitter::new();
1104     let (codegen_worker_send, codegen_worker_receive) = channel();
1105
1106     let coordinator_thread = start_executing_work(tcx,
1107                                                   &crate_info,
1108                                                   shared_emitter,
1109                                                   codegen_worker_send,
1110                                                   coordinator_receive,
1111                                                   total_cgus,
1112                                                   sess.jobserver.clone(),
1113                                                   time_graph.clone(),
1114                                                   Arc::new(modules_config),
1115                                                   Arc::new(metadata_config),
1116                                                   Arc::new(allocator_config));
1117
1118     OngoingCodegen {
1119         crate_name,
1120         crate_hash,
1121         metadata,
1122         windows_subsystem,
1123         linker_info,
1124         crate_info,
1125
1126         time_graph,
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),
1132     }
1133 }
1134
1135 fn copy_all_cgu_workproducts_to_incr_comp_cache_dir(
1136     sess: &Session,
1137     compiled_modules: &CompiledModules,
1138 ) -> FxHashMap<WorkProductId, WorkProduct> {
1139     let mut work_products = FxHashMap::default();
1140
1141     if sess.opts.incremental.is_none() {
1142         return work_products;
1143     }
1144
1145     for module in compiled_modules.modules.iter().filter(|m| m.kind == ModuleKind::Regular) {
1146         let mut files = vec![];
1147
1148         if let Some(ref path) = module.object {
1149             files.push((WorkProductFileKind::Object, path.clone()));
1150         }
1151         if let Some(ref path) = module.bytecode {
1152             files.push((WorkProductFileKind::Bytecode, path.clone()));
1153         }
1154         if let Some(ref path) = module.bytecode_compressed {
1155             files.push((WorkProductFileKind::BytecodeCompressed, path.clone()));
1156         }
1157
1158         if let Some((id, product)) =
1159             copy_cgu_workproducts_to_incr_comp_cache_dir(sess, &module.name, &files)
1160         {
1161             work_products.insert(id, product);
1162         }
1163     }
1164
1165     work_products
1166 }
1167
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;
1173
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));
1178         }
1179     };
1180
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);
1193             }
1194         } else {
1195             let ext = crate_output.temp_path(output_type, None)
1196                                   .extension()
1197                                   .unwrap()
1198                                   .to_str()
1199                                   .unwrap()
1200                                   .to_owned();
1201
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));
1212             } else {
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.)
1216             }
1217         }
1218     };
1219
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);
1231             }
1232             OutputType::LlvmAssembly => {
1233                 copy_if_one_unit(OutputType::LlvmAssembly, false);
1234             }
1235             OutputType::Assembly => {
1236                 copy_if_one_unit(OutputType::Assembly, false);
1237             }
1238             OutputType::Object => {
1239                 user_wants_objects = true;
1240                 copy_if_one_unit(OutputType::Object, true);
1241             }
1242             OutputType::Mir |
1243             OutputType::Metadata |
1244             OutputType::Exe |
1245             OutputType::DepInfo => {}
1246         }
1247     }
1248
1249     // Clean up unwanted temporary files.
1250
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).
1260
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
1265         // well.
1266
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
1276         // rlib.
1277         let needs_crate_object = crate_output.outputs.contains_key(&OutputType::Exe);
1278
1279         let keep_numbered_bitcode = user_wants_bitcode && sess.codegen_units() > 1;
1280
1281         let keep_numbered_objects = needs_crate_object ||
1282                 (user_wants_objects && sess.codegen_units() > 1);
1283
1284         for module in compiled_modules.modules.iter() {
1285             if let Some(ref path) = module.object {
1286                 if !keep_numbered_objects {
1287                     remove(sess, path);
1288                 }
1289             }
1290
1291             if let Some(ref path) = module.bytecode {
1292                 if !keep_numbered_bitcode {
1293                     remove(sess, path);
1294                 }
1295             }
1296         }
1297
1298         if !user_wants_bitcode {
1299             if let Some(ref path) = compiled_modules.metadata_module.bytecode {
1300                 remove(sess, &path);
1301             }
1302
1303             if let Some(ref allocator_module) = compiled_modules.allocator_module {
1304                 if let Some(ref path) = allocator_module.bytecode {
1305                     remove(sess, path);
1306                 }
1307             }
1308         }
1309     }
1310
1311     // We leave the following files around by default:
1312     //  - #crate#.o
1313     //  - #crate#.crate.metadata.o
1314     //  - #crate#.bc
1315     // These are used in linking steps and will be cleaned up afterward.
1316 }
1317
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());
1325 }
1326
1327 enum WorkItem {
1328     /// Optimize a newly codegened, totally unoptimized module.
1329     Optimize(ModuleCodegen),
1330     /// Copy the post-LTO artifacts from the incremental cache to the output
1331     /// directory.
1332     CopyPostLtoArtifacts(CachedModuleCodegen),
1333     /// Perform (Thin)LTO on the given module.
1334     LTO(lto::LtoModuleCodegen),
1335 }
1336
1337 impl WorkItem {
1338     fn module_kind(&self) -> ModuleKind {
1339         match *self {
1340             WorkItem::Optimize(ref m) => m.kind,
1341             WorkItem::CopyPostLtoArtifacts(_) |
1342             WorkItem::LTO(_) => ModuleKind::Regular,
1343         }
1344     }
1345
1346     fn name(&self) -> String {
1347         match *self {
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()),
1351         }
1352     }
1353 }
1354
1355 enum WorkItemResult {
1356     Compiled(CompiledModule),
1357     NeedsLTO(ModuleCodegen),
1358 }
1359
1360 fn execute_work_item(cgcx: &CodegenContext,
1361                      work_item: WorkItem,
1362                      timeline: &mut Timeline)
1363     -> Result<WorkItemResult, FatalError>
1364 {
1365     let module_config = cgcx.config(work_item.module_kind());
1366
1367     match work_item {
1368         WorkItem::Optimize(module) => {
1369             execute_optimize_work_item(cgcx, module, module_config, timeline)
1370         }
1371         WorkItem::CopyPostLtoArtifacts(module) => {
1372             execute_copy_from_cache_work_item(cgcx, module, module_config, timeline)
1373         }
1374         WorkItem::LTO(module) => {
1375             execute_lto_work_item(cgcx, module, module_config, timeline)
1376         }
1377     }
1378 }
1379
1380 fn execute_optimize_work_item(cgcx: &CodegenContext,
1381                               module: ModuleCodegen,
1382                               module_config: &ModuleConfig,
1383                               timeline: &mut Timeline)
1384     -> Result<WorkItemResult, FatalError>
1385 {
1386     let diag_handler = cgcx.create_diag_handler();
1387
1388     unsafe {
1389         optimize(cgcx, &diag_handler, &module, module_config, timeline)?;
1390     }
1391
1392     let linker_does_lto = cgcx.opts.debugging_opts.cross_lang_lto.enabled();
1393
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).
1398     //
1399     // Here we dispatch based on the `cgcx.lto` and kind of module we're
1400     // codegenning...
1401     let needs_lto = match cgcx.lto {
1402         Lto::No => false,
1403
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,
1408
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.
1412         //
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
1420         }
1421
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.
1426         Lto::ThinLocal => {
1427             module.kind != ModuleKind::Allocator
1428         }
1429     };
1430
1431     // Metadata modules never participate in LTO regardless of the lto
1432     // settings.
1433     let needs_lto = needs_lto && module.kind != ModuleKind::Metadata;
1434
1435     if needs_lto {
1436         Ok(WorkItemResult::NeedsLTO(module))
1437     } else {
1438         let module = unsafe {
1439             codegen(cgcx, &diag_handler, module, module_config, timeline)?
1440         };
1441         Ok(WorkItemResult::Compiled(module))
1442     }
1443 }
1444
1445 fn execute_copy_from_cache_work_item(cgcx: &CodegenContext,
1446                                      module: CachedModuleCodegen,
1447                                      module_config: &ModuleConfig,
1448                                      _: &mut Timeline)
1449     -> Result<WorkItemResult, FatalError>
1450 {
1451     let incr_comp_session_dir = cgcx.incr_comp_session_dir
1452                                     .as_ref()
1453                                     .unwrap();
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());
1463                 path
1464             }
1465             WorkProductFileKind::Bytecode => {
1466                 let path = cgcx.output_filenames.temp_path(OutputType::Bitcode,
1467                                                            Some(&module.name));
1468                 bytecode = Some(path.clone());
1469                 path
1470             }
1471             WorkProductFileKind::BytecodeCompressed => {
1472                 let path = cgcx.output_filenames.temp_path(OutputType::Bitcode,
1473                                                            Some(&module.name))
1474                     .with_extension(RLIB_BYTECODE_EXTENSION);
1475                 bytecode_compressed = Some(path.clone());
1476                 path
1477             }
1478         };
1479         let source_file = in_incr_comp_dir(&incr_comp_session_dir,
1480                                            &saved_file);
1481         debug!("copying pre-existing module `{}` from {:?} to {}",
1482                module.name,
1483                source_file,
1484                obj_out.display());
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(),
1489                                       obj_out.display(),
1490                                       err));
1491         }
1492     }
1493
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);
1497
1498     Ok(WorkItemResult::Compiled(CompiledModule {
1499         name: module.name,
1500         kind: ModuleKind::Regular,
1501         object,
1502         bytecode,
1503         bytecode_compressed,
1504     }))
1505 }
1506
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>
1512 {
1513     let diag_handler = cgcx.create_diag_handler();
1514
1515     unsafe {
1516         let module = module.optimize(cgcx, timeline)?;
1517         let module = codegen(cgcx, &diag_handler, module, module_config, timeline)?;
1518         Ok(WorkItemResult::Compiled(module))
1519     }
1520 }
1521
1522 enum Message {
1523     Token(io::Result<Acquired>),
1524     NeedsLTO {
1525         result: ModuleCodegen,
1526         worker_id: usize,
1527     },
1528     Done {
1529         result: Result<CompiledModule, ()>,
1530         worker_id: usize,
1531     },
1532     CodegenDone {
1533         llvm_work_item: WorkItem,
1534         cost: u64,
1535     },
1536     AddImportOnlyModule {
1537         module_data: SerializedModule,
1538         work_product: WorkProduct,
1539     },
1540     CodegenComplete,
1541     CodegenItem,
1542     CodegenAborted,
1543 }
1544
1545 struct Diagnostic {
1546     msg: String,
1547     code: Option<DiagnosticId>,
1548     lvl: Level,
1549 }
1550
1551 #[derive(PartialEq, Clone, Copy, Debug)]
1552 enum MainThreadWorkerState {
1553     Idle,
1554     Codegenning,
1555     LLVMing,
1556 }
1557
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>>,
1563                         total_cgus: usize,
1564                         jobserver: Client,
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;
1572
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();
1576
1577         let copy_symbols = |cnum| {
1578             let symbols = tcx.exported_symbols(cnum)
1579                              .iter()
1580                              .map(|&(s, lvl)| (s.symbol_name(tcx).to_string(), lvl))
1581                              .collect();
1582             Arc::new(symbols)
1583         };
1584
1585         match sess.lto() {
1586             Lto::No => None,
1587             Lto::ThinLocal => {
1588                 exported_symbols.insert(LOCAL_CRATE, copy_symbols(LOCAL_CRATE));
1589                 Some(Arc::new(exported_symbols))
1590             }
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));
1595                 }
1596                 Some(Arc::new(exported_symbols))
1597             }
1598         }
1599     };
1600
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");
1610
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) {
1614             return
1615         }
1616         each_linked_rlib_for_lto.push((cnum, path.to_path_buf()));
1617     }));
1618
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);
1622
1623         let (name, mut cmd) = get_linker(sess, &linker, flavor);
1624         cmd.args(&sess.target.target.options.asm_args);
1625
1626         Some(Arc::new(AssemblerCommand { name, cmd }))
1627     } else {
1628         None
1629     };
1630
1631     let cgcx = CodegenContext {
1632         crate_types: sess.crate_types.borrow().clone(),
1633         each_linked_rlib_for_lto,
1634         lto: sess.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(),
1640         exported_symbols,
1641         plugin_passes: sess.plugin_llvm_passes.borrow().clone(),
1642         remark: sess.opts.cg.remark.clone(),
1643         worker: 0,
1644         incr_comp_session_dir: sess.incr_comp_session_dir_opt().map(|r| r.clone()),
1645         cgu_reuse_tracker: sess.cgu_reuse_tracker.clone(),
1646         coordinator_send,
1647         diag_emitter: shared_emitter.clone(),
1648         time_graph,
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),
1654         total_cgus,
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,
1658         assembler_cmd,
1659         phantom: PhantomData
1660     };
1661
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.
1665     //
1666     // There are a few environmental pre-conditions that shape how the system
1667     // is set up:
1668     //
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`.
1677     //
1678     // Error Reporting
1679     // ===============
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.
1691     //
1692     // Work Processing Infrastructure
1693     // ==============================
1694     // The work processing infrastructure knows three major actors:
1695     //
1696     // - the coordinator thread,
1697     // - the main thread, and
1698     // - LLVM worker threads
1699     //
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.
1703     //
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
1709     // doing.
1710     //
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.
1717     //
1718     // Work Scheduling
1719     // ===============
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.
1728     //
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.
1733     //
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).
1743     //
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
1750     // in time.
1751     //
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.
1767     //
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.
1775     //
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.
1782     //
1783     // LTO and the coordinator thread
1784     // ------------------------------
1785     //
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
1792     // work for.
1793     //
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:
1799         set_time_depth(1);
1800
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() {
1806                 id
1807             } else {
1808                 let id = worker_id_counter;
1809                 worker_id_counter += 1;
1810                 id
1811             }
1812         };
1813
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;
1823
1824         // This flag tracks whether all items have gone through codegens
1825         let mut codegen_done = false;
1826
1827         // This is the queue of LLVM work items that still need processing.
1828         let mut work_items = Vec::<(WorkItem, u64)>::new();
1829
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();
1833
1834         let mut main_thread_worker_state = MainThreadWorkerState::Idle;
1835         let mut running = 0;
1836
1837         let mut llvm_start_time = None;
1838
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
1843         // work to be done.
1844         while !codegen_done ||
1845               running > 0 ||
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
1851               ))
1852         {
1853
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.
1857             if !codegen_done {
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")
1863                         }
1864                         main_thread_worker_state = MainThreadWorkerState::Codegenning;
1865                     } else {
1866                         // The queue is full enough to not let the worker
1867                         // threads starve. Use the implicit Token to do some
1868                         // LLVM work too.
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),
1873                             .. cgcx.clone()
1874                         };
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);
1879                     }
1880                 }
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
1884             } else {
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
1889                 // queue to do LTO
1890                 if work_items.len() == 0 &&
1891                    running == 0 &&
1892                    main_thread_worker_state == MainThreadWorkerState::Idle {
1893                     assert!(!started_lto);
1894                     assert!(needs_lto.len() + lto_import_only_modules.len() > 0);
1895                     started_lto = true;
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();
1906                         }
1907                     }
1908                 }
1909
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),
1918                                 .. cgcx.clone()
1919                             };
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);
1924                         } else {
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);
1932                             running -= 1;
1933                             main_thread_worker_state = MainThreadWorkerState::LLVMing;
1934                         }
1935                     }
1936                     MainThreadWorkerState::Codegenning => {
1937                         bug!("codegen worker should not be codegenning after \
1938                               codegen was already completed")
1939                     }
1940                     MainThreadWorkerState::LLVMing => {
1941                         // Already making good use of that token
1942                     }
1943                 }
1944             }
1945
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();
1950
1951                 maybe_start_llvm_timer(cgcx.config(item.module_kind()),
1952                                        &mut llvm_start_time);
1953
1954                 let cgcx = CodegenContext {
1955                     worker: get_worker_id(&mut free_worker_ids),
1956                     .. cgcx.clone()
1957                 };
1958
1959                 spawn_work(cgcx, item);
1960                 running += 1;
1961             }
1962
1963             // Relinquish accidentally acquired extra tokens
1964             tokens.truncate(running);
1965
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) => {
1972                     match token {
1973                         Ok(token) => {
1974                             tokens.push(token);
1975
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;
1982                                 running += 1;
1983                             }
1984                         }
1985                         Err(e) => {
1986                             let msg = &format!("failed to acquire jobserver token: {}", e);
1987                             shared_emitter.fatal(msg);
1988                             // Exit the coordinator thread
1989                             panic!("{}", msg)
1990                         }
1991                     }
1992                 }
1993
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
2002                     // distributed.
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
2007                     };
2008                     work_items.insert(insertion_index, (llvm_work_item, cost));
2009
2010                     if !cgcx.opts.debugging_opts.no_parallel_llvm {
2011                         helper.request_token();
2012                     }
2013                     assert!(!codegen_aborted);
2014                     assert_eq!(main_thread_worker_state,
2015                                MainThreadWorkerState::Codegenning);
2016                     main_thread_worker_state = MainThreadWorkerState::Idle;
2017                 }
2018
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;
2025                 }
2026
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);
2039                 }
2040
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"
2046                 //
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;
2052                     } else {
2053                         running -= 1;
2054                     }
2055
2056                     free_worker_ids.push(worker_id);
2057
2058                     match compiled_module.kind {
2059                         ModuleKind::Regular => {
2060                             compiled_modules.push(compiled_module);
2061                         }
2062                         ModuleKind::Metadata => {
2063                             assert!(compiled_metadata_module.is_none());
2064                             compiled_metadata_module = Some(compiled_module);
2065                         }
2066                         ModuleKind::Allocator => {
2067                             assert!(compiled_allocator_module.is_none());
2068                             compiled_allocator_module = Some(compiled_module);
2069                         }
2070                     }
2071                 }
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;
2076                     } else {
2077                         running -= 1;
2078                     }
2079                     free_worker_ids.push(worker_id);
2080                     needs_lto.push(result);
2081                 }
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;
2089                 }
2090                 Message::Done { result: Err(()), worker_id: _ } => {
2091                     bug!("worker thread panicked");
2092                 }
2093                 Message::CodegenItem => {
2094                     bug!("the coordinator should not receive codegen requests")
2095                 }
2096             }
2097         }
2098
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
2102             // to zero.
2103             set_time_depth(0);
2104             print_time_passes_entry(cgcx.time_passes,
2105                                     "LLVM passes",
2106                                     total_llvm_time);
2107         }
2108
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));
2113
2114         let compiled_metadata_module = compiled_metadata_module
2115             .expect("Metadata module not compiled?");
2116
2117         Ok(CompiledModules {
2118             modules: compiled_modules,
2119             metadata_module: compiled_metadata_module,
2120             allocator_module: compiled_allocator_module,
2121         })
2122     });
2123
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 {
2129         // Tune me, plz.
2130         items_in_queue > 0 &&
2131         items_in_queue >= max_workers.saturating_sub(workers_running / 2)
2132     }
2133
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());
2141             }
2142         }
2143     }
2144 }
2145
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"]);
2153
2154 fn spawn_work(cgcx: CodegenContext<'static>, work: WorkItem) {
2155     let depth = time_depth();
2156
2157     thread::spawn(move || {
2158         set_time_depth(depth);
2159
2160         // Set up a destructor which will fire off a message that we're done as
2161         // we exit.
2162         struct Bomb {
2163             coordinator_send: Sender<Box<dyn Any + Send>>,
2164             result: Option<WorkItemResult>,
2165             worker_id: usize,
2166         }
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 }
2173                     }
2174                     Some(WorkItemResult::NeedsLTO(m)) => {
2175                         Message::NeedsLTO { result: m, worker_id }
2176                     }
2177                     None => Message::Done { result: Err(()), worker_id }
2178                 };
2179                 drop(self.coordinator_send.send(Box::new(msg)));
2180             }
2181         }
2182
2183         let mut bomb = Bomb {
2184             coordinator_send: cgcx.coordinator_send.clone(),
2185             result: None,
2186             worker_id: cgcx.worker,
2187         };
2188
2189         // Execute the work itself, and if it finishes successfully then flag
2190         // ourselves as a success as well.
2191         //
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.
2195         bomb.result = {
2196             let timeline = cgcx.time_graph.as_ref().map(|tg| {
2197                 tg.start(time_graph::TimelineId(cgcx.worker),
2198                          LLVM_WORK_PACKAGE_KIND,
2199                          &work.name())
2200             });
2201             let mut timeline = timeline.unwrap_or(Timeline::noop());
2202             execute_work_item(&cgcx, work, &mut timeline).ok()
2203         };
2204     });
2205 }
2206
2207 pub fn run_assembler(cgcx: &CodegenContext, handler: &Handler, assembly: &Path, object: &Path) {
2208     let assembler = cgcx.assembler_cmd
2209         .as_ref()
2210         .expect("cgcx.assembler_cmd is missing?");
2211
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);
2216
2217     match cmd.output() {
2218         Ok(prog) => {
2219             if !prog.status.success() {
2220                 let mut note = prog.stderr.clone();
2221                 note.extend_from_slice(&prog.stdout);
2222
2223                 handler.struct_err(&format!("linking with `{}` failed: {}",
2224                                             pname.display(),
2225                                             prog.status))
2226                        .note(&format!("{:?}", &cmd))
2227                        .note(str::from_utf8(&note[..]).unwrap())
2228                        .emit();
2229                 handler.abort_if_errors();
2230             }
2231         },
2232         Err(e) => {
2233             handler.err(&format!("could not exec the linker `{}`: {}", pname.display(), e));
2234             handler.abort_if_errors();
2235         }
2236     }
2237 }
2238
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)) {
2244     use std::ptr;
2245
2246     // Create the PassManagerBuilder for LLVM. We configure it with
2247     // reasonable defaults and prepare it to actually populate the pass
2248     // manager.
2249     let builder = llvm::LLVMPassManagerBuilderCreate();
2250     let opt_size = config.opt_size.unwrap_or(llvm::CodeGenOptSizeNone);
2251     let inline_threshold = config.inline_threshold;
2252
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()
2256     });
2257
2258     let pgo_use_path = if config.pgo_use.is_empty() {
2259         None
2260     } else {
2261         Some(CString::new(config.pgo_use.as_bytes()).unwrap())
2262     };
2263
2264     llvm::LLVMRustConfigurePassManagerBuilder(
2265         builder,
2266         opt_level,
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()),
2273     );
2274
2275     llvm::LLVMPassManagerBuilderSetSizeLevel(builder, opt_size as u32);
2276
2277     if opt_size != llvm::CodeGenOptSizeNone {
2278         llvm::LLVMPassManagerBuilderSetDisableUnrollLoops(builder, 1);
2279     }
2280
2281     llvm::LLVMRustAddBuilderLibraryInfo(builder, llmod, config.no_builtins);
2282
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) {
2288         (.., Some(t)) => {
2289             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t as u32);
2290         }
2291         (llvm::CodeGenOptLevel::Aggressive, ..) => {
2292             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 275);
2293         }
2294         (_, llvm::CodeGenOptSizeDefault, _) => {
2295             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 75);
2296         }
2297         (_, llvm::CodeGenOptSizeAggressive, _) => {
2298             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 25);
2299         }
2300         (llvm::CodeGenOptLevel::None, ..) => {
2301             llvm::LLVMRustAddAlwaysInlinePass(builder, false);
2302         }
2303         (llvm::CodeGenOptLevel::Less, ..) => {
2304             llvm::LLVMRustAddAlwaysInlinePass(builder, true);
2305         }
2306         (llvm::CodeGenOptLevel::Default, ..) => {
2307             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 225);
2308         }
2309         (llvm::CodeGenOptLevel::Other, ..) => {
2310             bug!("CodeGenOptLevel::Other selected")
2311         }
2312     }
2313
2314     f(builder);
2315     llvm::LLVMPassManagerBuilderDispose(builder);
2316 }
2317
2318
2319 enum SharedEmitterMessage {
2320     Diagnostic(Diagnostic),
2321     InlineAsmError(u32, String),
2322     AbortIfErrors,
2323     Fatal(String),
2324 }
2325
2326 #[derive(Clone)]
2327 pub struct SharedEmitter {
2328     sender: Sender<SharedEmitterMessage>,
2329 }
2330
2331 pub struct SharedEmitterMain {
2332     receiver: Receiver<SharedEmitterMessage>,
2333 }
2334
2335 impl SharedEmitter {
2336     pub fn new() -> (SharedEmitter, SharedEmitterMain) {
2337         let (sender, receiver) = channel();
2338
2339         (SharedEmitter { sender }, SharedEmitterMain { receiver })
2340     }
2341
2342     fn inline_asm_error(&self, cookie: u32, msg: String) {
2343         drop(self.sender.send(SharedEmitterMessage::InlineAsmError(cookie, msg)));
2344     }
2345
2346     fn fatal(&self, msg: &str) {
2347         drop(self.sender.send(SharedEmitterMessage::Fatal(msg.to_string())));
2348     }
2349 }
2350
2351 impl Emitter for SharedEmitter {
2352     fn emit(&mut self, db: &DiagnosticBuilder) {
2353         drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
2354             msg: db.message(),
2355             code: db.code.clone(),
2356             lvl: db.level,
2357         })));
2358         for child in &db.children {
2359             drop(self.sender.send(SharedEmitterMessage::Diagnostic(Diagnostic {
2360                 msg: child.message(),
2361                 code: None,
2362                 lvl: child.level,
2363             })));
2364         }
2365         drop(self.sender.send(SharedEmitterMessage::AbortIfErrors));
2366     }
2367 }
2368
2369 impl SharedEmitterMain {
2370     pub fn check(&self, sess: &Session, blocking: bool) {
2371         loop {
2372             let message = if blocking {
2373                 match self.receiver.recv() {
2374                     Ok(message) => Ok(message),
2375                     Err(_) => Err(()),
2376                 }
2377             } else {
2378                 match self.receiver.try_recv() {
2379                     Ok(message) => Ok(message),
2380                     Err(_) => Err(()),
2381                 }
2382             };
2383
2384             match message {
2385                 Ok(SharedEmitterMessage::Diagnostic(diag)) => {
2386                     let handler = sess.diagnostic();
2387                     match diag.code {
2388                         Some(ref code) => {
2389                             handler.emit_with_code(&MultiSpan::new(),
2390                                                    &diag.msg,
2391                                                    code.clone(),
2392                                                    diag.lvl);
2393                         }
2394                         None => {
2395                             handler.emit(&MultiSpan::new(),
2396                                          &diag.msg,
2397                                          diag.lvl);
2398                         }
2399                     }
2400                 }
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),
2405                     }
2406                 }
2407                 Ok(SharedEmitterMessage::AbortIfErrors) => {
2408                     sess.abort_if_errors();
2409                 }
2410                 Ok(SharedEmitterMessage::Fatal(msg)) => {
2411                     sess.fatal(&msg);
2412                 }
2413                 Err(_) => {
2414                     break;
2415                 }
2416             }
2417
2418         }
2419     }
2420 }
2421
2422 pub struct OngoingCodegen {
2423     crate_name: Symbol,
2424     crate_hash: Svh,
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>,
2435 }
2436
2437 impl OngoingCodegen {
2438     pub(crate) fn join(
2439         self,
2440         sess: &Session
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,
2445             Ok(Err(())) => {
2446                 sess.abort_if_errors();
2447                 panic!("expected abort due to worker thread errors")
2448             },
2449             Err(_) => {
2450                 bug!("panic during codegen/LLVM phase");
2451             }
2452         };
2453
2454         sess.cgu_reuse_tracker.check_expected_reuse(sess);
2455
2456         sess.abort_if_errors();
2457
2458         if let Some(time_graph) = self.time_graph {
2459             time_graph.dump(&format!("{}-timings", self.crate_name));
2460         }
2461
2462         let work_products =
2463             copy_all_cgu_workproducts_to_incr_comp_cache_dir(sess,
2464                                                              &compiled_modules);
2465         produce_final_output_artifacts(sess,
2466                                        &compiled_modules,
2467                                        &self.output_filenames);
2468
2469         // FIXME: time_llvm_passes support - does this use a global context or
2470         // something?
2471         if sess.codegen_units() == 1 && sess.time_llvm_passes() {
2472             unsafe { llvm::LLVMRustPrintPassTimings(); }
2473         }
2474
2475         (CodegenResults {
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,
2482
2483             modules: compiled_modules.modules,
2484             allocator_module: compiled_modules.allocator_module,
2485             metadata_module: compiled_modules.metadata_module,
2486         }, work_products)
2487     }
2488
2489     pub(crate) fn submit_pre_codegened_module_to_llvm(&self,
2490                                                       tcx: TyCtxt,
2491                                                       module: ModuleCodegen) {
2492         self.wait_for_signal_to_codegen_item();
2493         self.check_for_errors(tcx.sess);
2494
2495         // These are generally cheap and won't through off scheduling.
2496         let cost = 0;
2497         submit_codegened_module_to_llvm(tcx, module, cost);
2498     }
2499
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)));
2504     }
2505
2506     /// Consume this context indicating that codegen was entirely aborted, and
2507     /// we need to exit as quickly as possible.
2508     ///
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
2514         // shutdown.
2515         drop(self.coordinator_send.send(Box::new(Message::CodegenAborted)));
2516         drop(self.future.join());
2517     }
2518
2519     pub fn check_for_errors(&self, sess: &Session) {
2520         self.shared_emitter_main.check(sess, false);
2521     }
2522
2523     pub fn wait_for_signal_to_codegen_item(&self) {
2524         match self.codegen_worker_receive.recv() {
2525             Ok(Message::CodegenItem) => {
2526                 // Nothing to do
2527             }
2528             Ok(_) => panic!("unexpected message"),
2529             Err(_) => {
2530                 // One of the LLVM threads must have panicked, fall through so
2531                 // error handling can be reached.
2532             }
2533         }
2534     }
2535 }
2536
2537 // impl Drop for OngoingCodegen {
2538 //     fn drop(&mut self) {
2539 //     }
2540 // }
2541
2542 pub(crate) fn submit_codegened_module_to_llvm(tcx: TyCtxt,
2543                                               module: ModuleCodegen,
2544                                               cost: u64) {
2545     let llvm_work_item = WorkItem::Optimize(module);
2546     drop(tcx.tx_to_llvm_workers.lock().send(Box::new(Message::CodegenDone {
2547         llvm_work_item,
2548         cost,
2549     })));
2550 }
2551
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 {
2556         llvm_work_item,
2557         cost: 0,
2558     })));
2559 }
2560
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)
2567     });
2568
2569     let mmap = unsafe {
2570         memmap::Mmap::map(&file).unwrap_or_else(|e| {
2571             panic!("failed to mmap bitcode file `{}`: {}", bc_path.display(), e)
2572         })
2573     };
2574
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,
2579     })));
2580 }
2581
2582 pub(super) fn pre_lto_bitcode_filename(module_name: &str) -> String {
2583     format!("{}.{}", module_name, PRE_THIN_LTO_BC_EXT)
2584 }
2585
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));
2592
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()
2599 }
2600
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 {
2608         return
2609     }
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" {
2615         "\x01__imp__"
2616     } else {
2617         "\x01__imp_"
2618     };
2619     unsafe {
2620         let i8p_ty = Type::i8p_llcx(cgcx, llcx);
2621         let globals = base::iter_globals(llmod)
2622             .filter(|&val| {
2623                 llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage &&
2624                     llvm::LLVMIsDeclaration(val) == 0
2625             })
2626             .map(move |val| {
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();
2631                 (imp_name, val)
2632             })
2633             .collect::<Vec<_>>();
2634         for (imp_name, val) in globals {
2635             let imp = llvm::LLVMAddGlobal(llmod,
2636                                           i8p_ty,
2637                                           imp_name.as_ptr() as *const _);
2638             llvm::LLVMSetInitializer(imp, consts::ptrcast(val, i8p_ty));
2639             llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
2640         }
2641     }
2642 }