]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/back/write.rs
Autoderef in librustc_trans
[rust.git] / src / librustc_trans / 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 back::lto;
12 use back::link::{get_linker, remove};
13 use session::config::{OutputFilenames, Passes, SomePasses, AllPasses};
14 use session::Session;
15 use session::config::{self, OutputType};
16 use llvm;
17 use llvm::{ModuleRef, TargetMachineRef, PassManagerRef, DiagnosticInfoRef, ContextRef};
18 use llvm::SMDiagnosticRef;
19 use trans::{CrateTranslation, ModuleTranslation};
20 use util::common::time;
21 use util::common::path2cstr;
22 use syntax::codemap;
23 use syntax::errors::{self, Handler, Level};
24 use syntax::errors::emitter::Emitter;
25
26 use std::collections::HashMap;
27 use std::ffi::{CStr, CString};
28 use std::fs;
29 use std::path::{Path, PathBuf};
30 use std::str;
31 use std::sync::{Arc, Mutex};
32 use std::sync::mpsc::channel;
33 use std::thread;
34 use libc::{c_uint, c_int, c_void};
35
36 pub fn llvm_err(handler: &errors::Handler, msg: String) -> ! {
37     match llvm::last_error() {
38         Some(err) => panic!(handler.fatal(&format!("{}: {}", msg, err))),
39         None => panic!(handler.fatal(&msg)),
40     }
41 }
42
43 pub fn write_output_file(
44         handler: &errors::Handler,
45         target: llvm::TargetMachineRef,
46         pm: llvm::PassManagerRef,
47         m: ModuleRef,
48         output: &Path,
49         file_type: llvm::FileType) {
50     unsafe {
51         let output_c = path2cstr(output);
52         let result = llvm::LLVMRustWriteOutputFile(
53                 target, pm, m, output_c.as_ptr(), file_type);
54         if !result {
55             llvm_err(handler, format!("could not write output to {}", output.display()));
56         }
57     }
58 }
59
60
61 struct Diagnostic {
62     msg: String,
63     code: Option<String>,
64     lvl: Level,
65 }
66
67 // We use an Arc instead of just returning a list of diagnostics from the
68 // child thread because we need to make sure that the messages are seen even
69 // if the child thread panics (for example, when `fatal` is called).
70 #[derive(Clone)]
71 struct SharedEmitter {
72     buffer: Arc<Mutex<Vec<Diagnostic>>>,
73 }
74
75 impl SharedEmitter {
76     fn new() -> SharedEmitter {
77         SharedEmitter {
78             buffer: Arc::new(Mutex::new(Vec::new())),
79         }
80     }
81
82     fn dump(&mut self, handler: &Handler) {
83         let mut buffer = self.buffer.lock().unwrap();
84         for diag in &*buffer {
85             match diag.code {
86                 Some(ref code) => {
87                     handler.emit_with_code(None,
88                                            &diag.msg,
89                                            &code[..],
90                                            diag.lvl);
91                 },
92                 None => {
93                     handler.emit(None,
94                                  &diag.msg,
95                                  diag.lvl);
96                 },
97             }
98         }
99         buffer.clear();
100     }
101 }
102
103 impl Emitter for SharedEmitter {
104     fn emit(&mut self, sp: Option<&codemap::MultiSpan>,
105             msg: &str, code: Option<&str>, lvl: Level) {
106         assert!(sp.is_none(), "SharedEmitter doesn't support spans");
107
108         self.buffer.lock().unwrap().push(Diagnostic {
109             msg: msg.to_string(),
110             code: code.map(|s| s.to_string()),
111             lvl: lvl,
112         });
113     }
114
115     fn custom_emit(&mut self, _sp: &errors::RenderSpan, _msg: &str, _lvl: Level) {
116         panic!("SharedEmitter doesn't support custom_emit");
117     }
118 }
119
120
121 // On android, we by default compile for armv7 processors. This enables
122 // things like double word CAS instructions (rather than emulating them)
123 // which are *far* more efficient. This is obviously undesirable in some
124 // cases, so if any sort of target feature is specified we don't append v7
125 // to the feature list.
126 //
127 // On iOS only armv7 and newer are supported. So it is useful to
128 // get all hardware potential via VFP3 (hardware floating point)
129 // and NEON (SIMD) instructions supported by LLVM.
130 // Note that without those flags various linking errors might
131 // arise as some of intrinsics are converted into function calls
132 // and nobody provides implementations those functions
133 fn target_feature(sess: &Session) -> String {
134     format!("{},{}", sess.target.target.options.features, sess.opts.cg.target_feature)
135 }
136
137 fn get_llvm_opt_level(optimize: config::OptLevel) -> llvm::CodeGenOptLevel {
138     match optimize {
139       config::OptLevel::No => llvm::CodeGenLevelNone,
140       config::OptLevel::Less => llvm::CodeGenLevelLess,
141       config::OptLevel::Default => llvm::CodeGenLevelDefault,
142       config::OptLevel::Aggressive => llvm::CodeGenLevelAggressive,
143     }
144 }
145
146 pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
147     let reloc_model_arg = match sess.opts.cg.relocation_model {
148         Some(ref s) => &s[..],
149         None => &sess.target.target.options.relocation_model[..],
150     };
151     let reloc_model = match reloc_model_arg {
152         "pic" => llvm::RelocPIC,
153         "static" => llvm::RelocStatic,
154         "default" => llvm::RelocDefault,
155         "dynamic-no-pic" => llvm::RelocDynamicNoPic,
156         _ => {
157             sess.err(&format!("{:?} is not a valid relocation mode",
158                              sess.opts
159                                  .cg
160                                  .relocation_model));
161             sess.abort_if_errors();
162             unreachable!();
163         }
164     };
165
166     let opt_level = get_llvm_opt_level(sess.opts.optimize);
167     let use_softfp = sess.opts.cg.soft_float;
168
169     let any_library = sess.crate_types.borrow().iter().any(|ty| {
170         *ty != config::CrateTypeExecutable
171     });
172
173     let ffunction_sections = sess.target.target.options.function_sections;
174     let fdata_sections = ffunction_sections;
175
176     let code_model_arg = match sess.opts.cg.code_model {
177         Some(ref s) => &s[..],
178         None => &sess.target.target.options.code_model[..],
179     };
180
181     let code_model = match code_model_arg {
182         "default" => llvm::CodeModelDefault,
183         "small" => llvm::CodeModelSmall,
184         "kernel" => llvm::CodeModelKernel,
185         "medium" => llvm::CodeModelMedium,
186         "large" => llvm::CodeModelLarge,
187         _ => {
188             sess.err(&format!("{:?} is not a valid code model",
189                              sess.opts
190                                  .cg
191                                  .code_model));
192             sess.abort_if_errors();
193             unreachable!();
194         }
195     };
196
197     let triple = &sess.target.target.llvm_target;
198
199     let tm = unsafe {
200         let triple = CString::new(triple.as_bytes()).unwrap();
201         let cpu = match sess.opts.cg.target_cpu {
202             Some(ref s) => &**s,
203             None => &*sess.target.target.options.cpu
204         };
205         let cpu = CString::new(cpu.as_bytes()).unwrap();
206         let features = CString::new(target_feature(sess).as_bytes()).unwrap();
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             !any_library && reloc_model == llvm::RelocPIC,
214             ffunction_sections,
215             fdata_sections,
216         )
217     };
218
219     if tm.is_null() {
220         llvm_err(sess.diagnostic(),
221                  format!("Could not create LLVM TargetMachine for triple: {}",
222                          triple).to_string());
223     } else {
224         return tm;
225     };
226 }
227
228
229 /// Module-specific configuration for `optimize_and_codegen`.
230 #[derive(Clone)]
231 pub struct ModuleConfig {
232     /// LLVM TargetMachine to use for codegen.
233     tm: TargetMachineRef,
234     /// Names of additional optimization passes to run.
235     passes: Vec<String>,
236     /// Some(level) to optimize at a certain level, or None to run
237     /// absolutely no optimizations (used for the metadata module).
238     opt_level: Option<llvm::CodeGenOptLevel>,
239
240     // Flags indicating which outputs to produce.
241     emit_no_opt_bc: bool,
242     emit_bc: bool,
243     emit_lto_bc: bool,
244     emit_ir: bool,
245     emit_asm: bool,
246     emit_obj: bool,
247     // Miscellaneous flags.  These are mostly copied from command-line
248     // options.
249     no_verify: bool,
250     no_prepopulate_passes: bool,
251     no_builtins: bool,
252     time_passes: bool,
253     vectorize_loop: bool,
254     vectorize_slp: bool,
255     merge_functions: bool,
256     inline_threshold: Option<usize>,
257     // Instead of creating an object file by doing LLVM codegen, just
258     // make the object file bitcode. Provides easy compatibility with
259     // emscripten's ecc compiler, when used as the linker.
260     obj_is_bitcode: bool,
261 }
262
263 unsafe impl Send for ModuleConfig { }
264
265 impl ModuleConfig {
266     fn new(tm: TargetMachineRef, passes: Vec<String>) -> ModuleConfig {
267         ModuleConfig {
268             tm: tm,
269             passes: passes,
270             opt_level: None,
271
272             emit_no_opt_bc: false,
273             emit_bc: false,
274             emit_lto_bc: false,
275             emit_ir: false,
276             emit_asm: false,
277             emit_obj: false,
278             obj_is_bitcode: false,
279
280             no_verify: false,
281             no_prepopulate_passes: false,
282             no_builtins: false,
283             time_passes: false,
284             vectorize_loop: false,
285             vectorize_slp: false,
286             merge_functions: false,
287             inline_threshold: None
288         }
289     }
290
291     fn set_flags(&mut self, sess: &Session, trans: &CrateTranslation) {
292         self.no_verify = sess.no_verify();
293         self.no_prepopulate_passes = sess.opts.cg.no_prepopulate_passes;
294         self.no_builtins = trans.no_builtins;
295         self.time_passes = sess.time_passes();
296         self.inline_threshold = sess.opts.cg.inline_threshold;
297         self.obj_is_bitcode = sess.target.target.options.obj_is_bitcode;
298
299         // Copy what clang does by turning on loop vectorization at O2 and
300         // slp vectorization at O3. Otherwise configure other optimization aspects
301         // of this pass manager builder.
302         self.vectorize_loop = !sess.opts.cg.no_vectorize_loops &&
303                              (sess.opts.optimize == config::OptLevel::Default ||
304                               sess.opts.optimize == config::OptLevel::Aggressive);
305         self.vectorize_slp = !sess.opts.cg.no_vectorize_slp &&
306                             sess.opts.optimize == config::OptLevel::Aggressive;
307
308         self.merge_functions = sess.opts.optimize == config::OptLevel::Default ||
309                                sess.opts.optimize == config::OptLevel::Aggressive;
310     }
311 }
312
313 /// Additional resources used by optimize_and_codegen (not module specific)
314 struct CodegenContext<'a> {
315     // Extra resources used for LTO: (sess, reachable).  This will be `None`
316     // when running in a worker thread.
317     lto_ctxt: Option<(&'a Session, &'a [String])>,
318     // Handler to use for diagnostics produced during codegen.
319     handler: &'a Handler,
320     // LLVM passes added by plugins.
321     plugin_passes: Vec<String>,
322     // LLVM optimizations for which we want to print remarks.
323     remark: Passes,
324     // Worker thread number
325     worker: usize,
326 }
327
328 impl<'a> CodegenContext<'a> {
329     fn new_with_session(sess: &'a Session, reachable: &'a [String]) -> CodegenContext<'a> {
330         CodegenContext {
331             lto_ctxt: Some((sess, reachable)),
332             handler: sess.diagnostic(),
333             plugin_passes: sess.plugin_llvm_passes.borrow().clone(),
334             remark: sess.opts.cg.remark.clone(),
335             worker: 0,
336         }
337     }
338 }
339
340 struct HandlerFreeVars<'a> {
341     llcx: ContextRef,
342     cgcx: &'a CodegenContext<'a>,
343 }
344
345 unsafe extern "C" fn report_inline_asm<'a, 'b>(cgcx: &'a CodegenContext<'a>,
346                                                msg: &'b str,
347                                                cookie: c_uint) {
348     use syntax::codemap::ExpnId;
349
350     match cgcx.lto_ctxt {
351         Some((sess, _)) => {
352             sess.codemap().with_expn_info(ExpnId::from_u32(cookie), |info| match info {
353                 Some(ei) => sess.span_err(ei.call_site, msg),
354                 None     => sess.err(msg),
355             });
356         }
357
358         None => {
359             cgcx.handler.struct_err(msg)
360                         .note("build without -C codegen-units for more exact errors")
361                         .emit();
362         }
363     }
364 }
365
366 unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef,
367                                         user: *const c_void,
368                                         cookie: c_uint) {
369     let HandlerFreeVars { cgcx, .. } = *(user as *const HandlerFreeVars);
370
371     let msg = llvm::build_string(|s| llvm::LLVMWriteSMDiagnosticToString(diag, s))
372         .expect("non-UTF8 SMDiagnostic");
373
374     report_inline_asm(cgcx, &msg[..], cookie);
375 }
376
377 unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef, user: *mut c_void) {
378     let HandlerFreeVars { llcx, cgcx } = *(user as *const HandlerFreeVars);
379
380     match llvm::diagnostic::Diagnostic::unpack(info) {
381         llvm::diagnostic::InlineAsm(inline) => {
382             report_inline_asm(cgcx,
383                               &llvm::twine_to_string(inline.message),
384                               inline.cookie);
385         }
386
387         llvm::diagnostic::Optimization(opt) => {
388             let pass_name = str::from_utf8(CStr::from_ptr(opt.pass_name).to_bytes())
389                                 .ok()
390                                 .expect("got a non-UTF8 pass name from LLVM");
391             let enabled = match cgcx.remark {
392                 AllPasses => true,
393                 SomePasses(ref v) => v.iter().any(|s| *s == pass_name),
394             };
395
396             if enabled {
397                 let loc = llvm::debug_loc_to_string(llcx, opt.debug_loc);
398                 cgcx.handler.note_without_error(&format!("optimization {} for {} at {}: {}",
399                                                 opt.kind.describe(),
400                                                 pass_name,
401                                                 if loc.is_empty() { "[unknown]" } else { &*loc },
402                                                 llvm::twine_to_string(opt.message)));
403             }
404         }
405
406         _ => (),
407     }
408 }
409
410 // Unsafe due to LLVM calls.
411 unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
412                                mtrans: ModuleTranslation,
413                                config: ModuleConfig,
414                                name_extra: String,
415                                output_names: OutputFilenames) {
416     let ModuleTranslation { llmod, llcx } = mtrans;
417     let tm = config.tm;
418
419     // llcx doesn't outlive this function, so we can put this on the stack.
420     let fv = HandlerFreeVars {
421         llcx: llcx,
422         cgcx: cgcx,
423     };
424     let fv = &fv as *const HandlerFreeVars as *mut c_void;
425
426     llvm::LLVMSetInlineAsmDiagnosticHandler(llcx, inline_asm_handler, fv);
427     llvm::LLVMContextSetDiagnosticHandler(llcx, diagnostic_handler, fv);
428
429     if config.emit_no_opt_bc {
430         let ext = format!("{}.no-opt.bc", name_extra);
431         let out = output_names.with_extension(&ext);
432         let out = path2cstr(&out);
433         llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr());
434     }
435
436     if config.opt_level.is_some() {
437         // Create the two optimizing pass managers. These mirror what clang
438         // does, and are by populated by LLVM's default PassManagerBuilder.
439         // Each manager has a different set of passes, but they also share
440         // some common passes.
441         let fpm = llvm::LLVMCreateFunctionPassManagerForModule(llmod);
442         let mpm = llvm::LLVMCreatePassManager();
443
444         // If we're verifying or linting, add them to the function pass
445         // manager.
446         let addpass = |pass_name: &str| {
447             let pass_name = CString::new(pass_name).unwrap();
448             let pass = llvm::LLVMRustFindAndCreatePass(pass_name.as_ptr());
449             if pass.is_null() {
450                 return false;
451             }
452             let pass_manager = match llvm::LLVMRustPassKind(pass) {
453                 llvm::SupportedPassKind::Function => fpm,
454                 llvm::SupportedPassKind::Module => mpm,
455                 llvm::SupportedPassKind::Unsupported => {
456                     cgcx.handler.err("Encountered LLVM pass kind we can't handle");
457                     return true
458                 },
459             };
460             llvm::LLVMRustAddPass(pass_manager, pass);
461             true
462         };
463
464         if !config.no_verify { assert!(addpass("verify")); }
465         if !config.no_prepopulate_passes {
466             llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
467             llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod);
468             with_llvm_pmb(llmod, &config, &mut |b| {
469                 llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm);
470                 llvm::LLVMPassManagerBuilderPopulateModulePassManager(b, mpm);
471             })
472         }
473
474         for pass in &config.passes {
475             if !addpass(pass) {
476                 cgcx.handler.warn(&format!("unknown pass `{}`, ignoring",
477                                            pass));
478             }
479         }
480
481         for pass in &cgcx.plugin_passes {
482             if !addpass(pass) {
483                 cgcx.handler.err(&format!("a plugin asked for LLVM pass \
484                                            `{}` but LLVM does not \
485                                            recognize it", pass));
486             }
487         }
488
489         cgcx.handler.abort_if_errors();
490
491         // Finally, run the actual optimization passes
492         time(config.time_passes, &format!("llvm function passes [{}]", cgcx.worker), ||
493              llvm::LLVMRustRunFunctionPassManager(fpm, llmod));
494         time(config.time_passes, &format!("llvm module passes [{}]", cgcx.worker), ||
495              llvm::LLVMRunPassManager(mpm, llmod));
496
497         // Deallocate managers that we're now done with
498         llvm::LLVMDisposePassManager(fpm);
499         llvm::LLVMDisposePassManager(mpm);
500
501         match cgcx.lto_ctxt {
502             Some((sess, reachable)) if sess.lto() =>  {
503                 time(sess.time_passes(), "all lto passes", ||
504                      lto::run(sess, llmod, tm, reachable, &config,
505                               &name_extra, &output_names));
506
507                 if config.emit_lto_bc {
508                     let name = format!("{}.lto.bc", name_extra);
509                     let out = output_names.with_extension(&name);
510                     let out = path2cstr(&out);
511                     llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr());
512                 }
513             },
514             _ => {},
515         }
516     }
517
518     // A codegen-specific pass manager is used to generate object
519     // files for an LLVM module.
520     //
521     // Apparently each of these pass managers is a one-shot kind of
522     // thing, so we create a new one for each type of output. The
523     // pass manager passed to the closure should be ensured to not
524     // escape the closure itself, and the manager should only be
525     // used once.
526     unsafe fn with_codegen<F>(tm: TargetMachineRef,
527                               llmod: ModuleRef,
528                               no_builtins: bool,
529                               f: F) where
530         F: FnOnce(PassManagerRef),
531     {
532         let cpm = llvm::LLVMCreatePassManager();
533         llvm::LLVMRustAddAnalysisPasses(tm, cpm, llmod);
534         llvm::LLVMRustAddLibraryInfo(cpm, llmod, no_builtins);
535         f(cpm);
536     }
537
538     // Change what we write and cleanup based on whether obj files are
539     // just llvm bitcode. In that case write bitcode, and possibly
540     // delete the bitcode if it wasn't requested. Don't generate the
541     // machine code, instead copy the .o file from the .bc
542     let write_bc = config.emit_bc || config.obj_is_bitcode;
543     let rm_bc = !config.emit_bc && config.obj_is_bitcode;
544     let write_obj = config.emit_obj && !config.obj_is_bitcode;
545     let copy_bc_to_obj = config.emit_obj && config.obj_is_bitcode;
546
547     let bc_out = output_names.with_extension(&format!("{}.bc", name_extra));
548     let obj_out = output_names.with_extension(&format!("{}.o", name_extra));
549
550     if write_bc {
551         let bc_out_c = path2cstr(&bc_out);
552         llvm::LLVMWriteBitcodeToFile(llmod, bc_out_c.as_ptr());
553     }
554
555     time(config.time_passes, &format!("codegen passes [{}]", cgcx.worker), || {
556         if config.emit_ir {
557             let ext = format!("{}.ll", name_extra);
558             let out = output_names.with_extension(&ext);
559             let out = path2cstr(&out);
560             with_codegen(tm, llmod, config.no_builtins, |cpm| {
561                 llvm::LLVMRustPrintModule(cpm, llmod, out.as_ptr());
562                 llvm::LLVMDisposePassManager(cpm);
563             })
564         }
565
566         if config.emit_asm {
567             let path = output_names.with_extension(&format!("{}.s", name_extra));
568
569             // We can't use the same module for asm and binary output, because that triggers
570             // various errors like invalid IR or broken binaries, so we might have to clone the
571             // module to produce the asm output
572             let llmod = if config.emit_obj {
573                 llvm::LLVMCloneModule(llmod)
574             } else {
575                 llmod
576             };
577             with_codegen(tm, llmod, config.no_builtins, |cpm| {
578                 write_output_file(cgcx.handler, tm, cpm, llmod, &path,
579                                   llvm::AssemblyFileType);
580             });
581             if config.emit_obj {
582                 llvm::LLVMDisposeModule(llmod);
583             }
584         }
585
586         if write_obj {
587             with_codegen(tm, llmod, config.no_builtins, |cpm| {
588                 write_output_file(cgcx.handler, tm, cpm, llmod, &obj_out, llvm::ObjectFileType);
589             });
590         }
591     });
592
593     if copy_bc_to_obj {
594         debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
595         if let Err(e) = fs::copy(&bc_out, &obj_out) {
596             cgcx.handler.err(&format!("failed to copy bitcode to object file: {}", e));
597         }
598     }
599
600     if rm_bc {
601         debug!("removing_bitcode {:?}", bc_out);
602         if let Err(e) = fs::remove_file(&bc_out) {
603             cgcx.handler.err(&format!("failed to remove bitcode: {}", e));
604         }
605     }
606
607     llvm::LLVMDisposeModule(llmod);
608     llvm::LLVMContextDispose(llcx);
609     llvm::LLVMRustDisposeTargetMachine(tm);
610 }
611
612 pub fn run_passes(sess: &Session,
613                   trans: &CrateTranslation,
614                   output_types: &HashMap<OutputType, Option<PathBuf>>,
615                   crate_output: &OutputFilenames) {
616     // It's possible that we have `codegen_units > 1` but only one item in
617     // `trans.modules`.  We could theoretically proceed and do LTO in that
618     // case, but it would be confusing to have the validity of
619     // `-Z lto -C codegen-units=2` depend on details of the crate being
620     // compiled, so we complain regardless.
621     if sess.lto() && sess.opts.cg.codegen_units > 1 {
622         // This case is impossible to handle because LTO expects to be able
623         // to combine the entire crate and all its dependencies into a
624         // single compilation unit, but each codegen unit is in a separate
625         // LLVM context, so they can't easily be combined.
626         sess.fatal("can't perform LTO when using multiple codegen units");
627     }
628
629     // Sanity check
630     assert!(trans.modules.len() == sess.opts.cg.codegen_units);
631
632     let tm = create_target_machine(sess);
633
634     // Figure out what we actually need to build.
635
636     let mut modules_config = ModuleConfig::new(tm, sess.opts.cg.passes.clone());
637     let mut metadata_config = ModuleConfig::new(tm, vec!());
638
639     modules_config.opt_level = Some(get_llvm_opt_level(sess.opts.optimize));
640
641     // Save all versions of the bytecode if we're saving our temporaries.
642     if sess.opts.cg.save_temps {
643         modules_config.emit_no_opt_bc = true;
644         modules_config.emit_bc = true;
645         modules_config.emit_lto_bc = true;
646         metadata_config.emit_bc = true;
647     }
648
649     // Emit bitcode files for the crate if we're emitting an rlib.
650     // Whenever an rlib is created, the bitcode is inserted into the
651     // archive in order to allow LTO against it.
652     let needs_crate_bitcode =
653             sess.crate_types.borrow().contains(&config::CrateTypeRlib) &&
654             sess.opts.output_types.contains_key(&OutputType::Exe);
655     let needs_crate_object =
656             sess.opts.output_types.contains_key(&OutputType::Exe);
657     if needs_crate_bitcode {
658         modules_config.emit_bc = true;
659     }
660
661     for output_type in output_types.keys() {
662         match *output_type {
663             OutputType::Bitcode => { modules_config.emit_bc = true; },
664             OutputType::LlvmAssembly => { modules_config.emit_ir = true; },
665             OutputType::Assembly => {
666                 modules_config.emit_asm = true;
667                 // If we're not using the LLVM assembler, this function
668                 // could be invoked specially with output_type_assembly, so
669                 // in this case we still want the metadata object file.
670                 if !sess.opts.output_types.contains_key(&OutputType::Assembly) {
671                     metadata_config.emit_obj = true;
672                 }
673             },
674             OutputType::Object => { modules_config.emit_obj = true; },
675             OutputType::Exe => {
676                 modules_config.emit_obj = true;
677                 metadata_config.emit_obj = true;
678             },
679             OutputType::DepInfo => {}
680         }
681     }
682
683     modules_config.set_flags(sess, trans);
684     metadata_config.set_flags(sess, trans);
685
686
687     // Populate a buffer with a list of codegen threads.  Items are processed in
688     // LIFO order, just because it's a tiny bit simpler that way.  (The order
689     // doesn't actually matter.)
690     let mut work_items = Vec::with_capacity(1 + trans.modules.len());
691
692     {
693         let work = build_work_item(sess,
694                                    trans.metadata_module,
695                                    metadata_config.clone(),
696                                    crate_output.clone(),
697                                    "metadata".to_string());
698         work_items.push(work);
699     }
700
701     for (index, mtrans) in trans.modules.iter().enumerate() {
702         let work = build_work_item(sess,
703                                    *mtrans,
704                                    modules_config.clone(),
705                                    crate_output.clone(),
706                                    format!("{}", index));
707         work_items.push(work);
708     }
709
710     // Process the work items, optionally using worker threads.
711     if sess.opts.cg.codegen_units == 1 {
712         run_work_singlethreaded(sess, &trans.reachable, work_items);
713     } else {
714         run_work_multithreaded(sess, work_items, sess.opts.cg.codegen_units);
715     }
716
717     // All codegen is finished.
718     unsafe {
719         llvm::LLVMRustDisposeTargetMachine(tm);
720     }
721
722     // Produce final compile outputs.
723     let copy_gracefully = |from: &Path, to: &Path| {
724         if let Err(e) = fs::copy(from, to) {
725             sess.err(&format!("could not copy {:?} to {:?}: {}", from, to, e));
726         }
727     };
728
729     let copy_if_one_unit = |ext: &str,
730                             output_type: OutputType,
731                             keep_numbered: bool| {
732         if sess.opts.cg.codegen_units == 1 {
733             // 1) Only one codegen unit.  In this case it's no difficulty
734             //    to copy `foo.0.x` to `foo.x`.
735             copy_gracefully(&crate_output.with_extension(ext),
736                             &crate_output.path(output_type));
737             if !sess.opts.cg.save_temps && !keep_numbered {
738                 // The user just wants `foo.x`, not `foo.0.x`.
739                 remove(sess, &crate_output.with_extension(ext));
740             }
741         } else if crate_output.outputs.contains_key(&output_type) {
742             // 2) Multiple codegen units, with `--emit foo=some_name`.  We have
743             //    no good solution for this case, so warn the user.
744             sess.warn(&format!("ignoring emit path because multiple .{} files \
745                                 were produced", ext));
746         } else if crate_output.single_output_file.is_some() {
747             // 3) Multiple codegen units, with `-o some_name`.  We have
748             //    no good solution for this case, so warn the user.
749             sess.warn(&format!("ignoring -o because multiple .{} files \
750                                 were produced", ext));
751         } else {
752             // 4) Multiple codegen units, but no explicit name.  We
753             //    just leave the `foo.0.x` files in place.
754             // (We don't have to do any work in this case.)
755         }
756     };
757
758     // Flag to indicate whether the user explicitly requested bitcode.
759     // Otherwise, we produced it only as a temporary output, and will need
760     // to get rid of it.
761     let mut user_wants_bitcode = false;
762     let mut user_wants_objects = false;
763     for output_type in output_types.keys() {
764         match *output_type {
765             OutputType::Bitcode => {
766                 user_wants_bitcode = true;
767                 // Copy to .bc, but always keep the .0.bc.  There is a later
768                 // check to figure out if we should delete .0.bc files, or keep
769                 // them for making an rlib.
770                 copy_if_one_unit("0.bc", OutputType::Bitcode, true);
771             }
772             OutputType::LlvmAssembly => {
773                 copy_if_one_unit("0.ll", OutputType::LlvmAssembly, false);
774             }
775             OutputType::Assembly => {
776                 copy_if_one_unit("0.s", OutputType::Assembly, false);
777             }
778             OutputType::Object => {
779                 user_wants_objects = true;
780                 copy_if_one_unit("0.o", OutputType::Object, true);
781             }
782             OutputType::Exe |
783             OutputType::DepInfo => {}
784         }
785     }
786     let user_wants_bitcode = user_wants_bitcode;
787
788     // Clean up unwanted temporary files.
789
790     // We create the following files by default:
791     //  - crate.0.bc
792     //  - crate.0.o
793     //  - crate.metadata.bc
794     //  - crate.metadata.o
795     //  - crate.o (linked from crate.##.o)
796     //  - crate.bc (copied from crate.0.bc)
797     // We may create additional files if requested by the user (through
798     // `-C save-temps` or `--emit=` flags).
799
800     if !sess.opts.cg.save_temps {
801         // Remove the temporary .0.o objects.  If the user didn't
802         // explicitly request bitcode (with --emit=bc), and the bitcode is not
803         // needed for building an rlib, then we must remove .0.bc as well.
804
805         // Specific rules for keeping .0.bc:
806         //  - If we're building an rlib (`needs_crate_bitcode`), then keep
807         //    it.
808         //  - If the user requested bitcode (`user_wants_bitcode`), and
809         //    codegen_units > 1, then keep it.
810         //  - If the user requested bitcode but codegen_units == 1, then we
811         //    can toss .0.bc because we copied it to .bc earlier.
812         //  - If we're not building an rlib and the user didn't request
813         //    bitcode, then delete .0.bc.
814         // If you change how this works, also update back::link::link_rlib,
815         // where .0.bc files are (maybe) deleted after making an rlib.
816         let keep_numbered_bitcode = needs_crate_bitcode ||
817                 (user_wants_bitcode && sess.opts.cg.codegen_units > 1);
818
819         let keep_numbered_objects = needs_crate_object ||
820                 (user_wants_objects && sess.opts.cg.codegen_units > 1);
821
822         for i in 0..trans.modules.len() {
823             if modules_config.emit_obj && !keep_numbered_objects {
824                 let ext = format!("{}.o", i);
825                 remove(sess, &crate_output.with_extension(&ext));
826             }
827
828             if modules_config.emit_bc && !keep_numbered_bitcode {
829                 let ext = format!("{}.bc", i);
830                 remove(sess, &crate_output.with_extension(&ext));
831             }
832         }
833
834         if metadata_config.emit_bc && !user_wants_bitcode {
835             remove(sess, &crate_output.with_extension("metadata.bc"));
836         }
837     }
838
839     // We leave the following files around by default:
840     //  - crate.o
841     //  - crate.metadata.o
842     //  - crate.bc
843     // These are used in linking steps and will be cleaned up afterward.
844
845     // FIXME: time_llvm_passes support - does this use a global context or
846     // something?
847     if sess.opts.cg.codegen_units == 1 && sess.time_llvm_passes() {
848         unsafe { llvm::LLVMRustPrintPassTimings(); }
849     }
850 }
851
852 struct WorkItem {
853     mtrans: ModuleTranslation,
854     config: ModuleConfig,
855     output_names: OutputFilenames,
856     name_extra: String
857 }
858
859 fn build_work_item(sess: &Session,
860                    mtrans: ModuleTranslation,
861                    config: ModuleConfig,
862                    output_names: OutputFilenames,
863                    name_extra: String)
864                    -> WorkItem
865 {
866     let mut config = config;
867     config.tm = create_target_machine(sess);
868     WorkItem { mtrans: mtrans, config: config, output_names: output_names,
869                name_extra: name_extra }
870 }
871
872 fn execute_work_item(cgcx: &CodegenContext,
873                      work_item: WorkItem) {
874     unsafe {
875         optimize_and_codegen(cgcx, work_item.mtrans, work_item.config,
876                              work_item.name_extra, work_item.output_names);
877     }
878 }
879
880 fn run_work_singlethreaded(sess: &Session,
881                            reachable: &[String],
882                            work_items: Vec<WorkItem>) {
883     let cgcx = CodegenContext::new_with_session(sess, reachable);
884
885     // Since we're running single-threaded, we can pass the session to
886     // the proc, allowing `optimize_and_codegen` to perform LTO.
887     for work in work_items.into_iter().rev() {
888         execute_work_item(&cgcx, work);
889     }
890 }
891
892 fn run_work_multithreaded(sess: &Session,
893                           work_items: Vec<WorkItem>,
894                           num_workers: usize) {
895     // Run some workers to process the work items.
896     let work_items_arc = Arc::new(Mutex::new(work_items));
897     let mut diag_emitter = SharedEmitter::new();
898     let mut futures = Vec::with_capacity(num_workers);
899
900     for i in 0..num_workers {
901         let work_items_arc = work_items_arc.clone();
902         let diag_emitter = diag_emitter.clone();
903         let plugin_passes = sess.plugin_llvm_passes.borrow().clone();
904         let remark = sess.opts.cg.remark.clone();
905
906         let (tx, rx) = channel();
907         let mut tx = Some(tx);
908         futures.push(rx);
909
910         thread::Builder::new().name(format!("codegen-{}", i)).spawn(move || {
911             let diag_handler = Handler::with_emitter(true, false, box diag_emitter);
912
913             // Must construct cgcx inside the proc because it has non-Send
914             // fields.
915             let cgcx = CodegenContext {
916                 lto_ctxt: None,
917                 handler: &diag_handler,
918                 plugin_passes: plugin_passes,
919                 remark: remark,
920                 worker: i,
921             };
922
923             loop {
924                 // Avoid holding the lock for the entire duration of the match.
925                 let maybe_work = work_items_arc.lock().unwrap().pop();
926                 match maybe_work {
927                     Some(work) => {
928                         execute_work_item(&cgcx, work);
929
930                         // Make sure to fail the worker so the main thread can
931                         // tell that there were errors.
932                         cgcx.handler.abort_if_errors();
933                     }
934                     None => break,
935                 }
936             }
937
938             tx.take().unwrap().send(()).unwrap();
939         }).unwrap();
940     }
941
942     let mut panicked = false;
943     for rx in futures {
944         match rx.recv() {
945             Ok(()) => {},
946             Err(_) => {
947                 panicked = true;
948             },
949         }
950         // Display any new diagnostics.
951         diag_emitter.dump(sess.diagnostic());
952     }
953     if panicked {
954         sess.fatal("aborting due to worker thread panic");
955     }
956 }
957
958 pub fn run_assembler(sess: &Session, outputs: &OutputFilenames) {
959     let (pname, mut cmd) = get_linker(sess);
960
961     cmd.arg("-c").arg("-o").arg(&outputs.path(OutputType::Object))
962                            .arg(&outputs.temp_path(OutputType::Assembly));
963     debug!("{:?}", cmd);
964
965     match cmd.output() {
966         Ok(prog) => {
967             if !prog.status.success() {
968                 let mut note = prog.stderr.clone();
969                 note.extend_from_slice(&prog.stdout);
970
971                 sess.struct_err(&format!("linking with `{}` failed: {}",
972                                          pname,
973                                          prog.status))
974                     .note(&format!("{:?}", &cmd))
975                     .note(str::from_utf8(&note[..]).unwrap())
976                     .emit();
977                 sess.abort_if_errors();
978             }
979         },
980         Err(e) => {
981             sess.err(&format!("could not exec the linker `{}`: {}", pname, e));
982             sess.abort_if_errors();
983         }
984     }
985 }
986
987 pub unsafe fn configure_llvm(sess: &Session) {
988     let mut llvm_c_strs = Vec::new();
989     let mut llvm_args = Vec::new();
990
991     {
992         let mut add = |arg: &str| {
993             let s = CString::new(arg).unwrap();
994             llvm_args.push(s.as_ptr());
995             llvm_c_strs.push(s);
996         };
997         add("rustc"); // fake program name
998         if sess.time_llvm_passes() { add("-time-passes"); }
999         if sess.print_llvm_passes() { add("-debug-pass=Structure"); }
1000
1001         // FIXME #21627 disable faulty FastISel on AArch64 (even for -O0)
1002         if sess.target.target.arch == "aarch64" { add("-fast-isel=0"); }
1003
1004         for arg in &sess.opts.cg.llvm_args {
1005             add(&(*arg));
1006         }
1007     }
1008
1009     llvm::LLVMInitializePasses();
1010
1011     llvm::initialize_available_targets();
1012
1013     llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int,
1014                                  llvm_args.as_ptr());
1015 }
1016
1017 pub unsafe fn with_llvm_pmb(llmod: ModuleRef,
1018                             config: &ModuleConfig,
1019                             f: &mut FnMut(llvm::PassManagerBuilderRef)) {
1020     // Create the PassManagerBuilder for LLVM. We configure it with
1021     // reasonable defaults and prepare it to actually populate the pass
1022     // manager.
1023     let builder = llvm::LLVMPassManagerBuilderCreate();
1024     let opt = config.opt_level.unwrap_or(llvm::CodeGenLevelNone);
1025     let inline_threshold = config.inline_threshold;
1026
1027     llvm::LLVMRustConfigurePassManagerBuilder(builder, opt,
1028                                               config.merge_functions,
1029                                               config.vectorize_slp,
1030                                               config.vectorize_loop);
1031
1032     llvm::LLVMRustAddBuilderLibraryInfo(builder, llmod, config.no_builtins);
1033
1034     // Here we match what clang does (kinda). For O0 we only inline
1035     // always-inline functions (but don't add lifetime intrinsics), at O1 we
1036     // inline with lifetime intrinsics, and O2+ we add an inliner with a
1037     // thresholds copied from clang.
1038     match (opt, inline_threshold) {
1039         (_, Some(t)) => {
1040             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, t as u32);
1041         }
1042         (llvm::CodeGenLevelNone, _) => {
1043             llvm::LLVMRustAddAlwaysInlinePass(builder, false);
1044         }
1045         (llvm::CodeGenLevelLess, _) => {
1046             llvm::LLVMRustAddAlwaysInlinePass(builder, true);
1047         }
1048         (llvm::CodeGenLevelDefault, _) => {
1049             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 225);
1050         }
1051         (llvm::CodeGenLevelAggressive, _) => {
1052             llvm::LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 275);
1053         }
1054     }
1055
1056     f(builder);
1057     llvm::LLVMPassManagerBuilderDispose(builder);
1058 }