1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Codegen the completed AST to the LLVM IR.
13 //! Some functions here, such as codegen_block and codegen_expr, return a value --
14 //! the result of the codegen to LLVM -- while others, such as codegen_fn
15 //! and mono_item, are called only for the side effect of adding a
16 //! particular definition to the LLVM IR output we're producing.
18 //! Hopefully useful general knowledge about codegen:
20 //! * There's no way to find out the Ty type of a Value. Doing so
21 //! would be "trying to get the eggs out of an omelette" (credit:
22 //! pcwalton). You can, instead, find out its llvm::Type by calling val_ty,
23 //! but one llvm::Type corresponds to many `Ty`s; for instance, tup(int, int,
24 //! int) and rec(x=int, y=int, z=int) will have the same llvm::Type.
26 use super::ModuleLlvm;
27 use super::ModuleCodegen;
28 use super::ModuleKind;
29 use super::CachedModuleCodegen;
32 use back::write::{self, OngoingCodegen};
33 use llvm::{self, TypeKind, get_param};
35 use rustc::dep_graph::cgu_reuse_tracker::CguReuse;
36 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
37 use rustc::middle::lang_items::StartFnLangItem;
38 use rustc::middle::weak_lang_items;
39 use rustc::mir::mono::{Linkage, Visibility, Stats, CodegenUnitNameBuilder};
40 use rustc::middle::cstore::{EncodedMetadata};
41 use rustc::ty::{self, Ty, TyCtxt};
42 use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx};
43 use rustc::ty::query::Providers;
44 use rustc::middle::cstore::{self, LinkagePreference};
45 use rustc::middle::exported_symbols;
46 use rustc::util::common::{time, print_time_passes_entry};
47 use rustc::util::profiling::ProfileCategory;
48 use rustc::session::config::{self, DebugInfo, EntryFnType, Lto};
49 use rustc::session::Session;
50 use rustc_incremental;
52 use mir::place::PlaceRef;
54 use builder::{Builder, MemFlags};
56 use rustc_mir::monomorphize::item::DefPathBasedNames;
57 use common::{self, IntPredicate, RealPredicate};
59 use context::CodegenCx;
64 use monomorphize::Instance;
65 use monomorphize::partitioning::{CodegenUnit, CodegenUnitExt};
66 use rustc_codegen_utils::symbol_names_test;
68 use mono_item::{MonoItem, MonoItemExt};
70 use type_of::LayoutLlvmExt;
71 use rustc::util::nodemap::FxHashMap;
73 use rustc_data_structures::small_c_str::SmallCStr;
74 use rustc_data_structures::sync::Lrc;
75 use rustc_data_structures::indexed_vec::Idx;
77 use interfaces::{BuilderMethods, ConstMethods, TypeMethods};
81 use std::ffi::CString;
82 use std::ops::{Deref, DerefMut};
84 use std::time::{Instant, Duration};
86 use syntax_pos::symbol::InternedString;
88 use rustc::hir::{self, CodegenFnAttrs};
92 use mir::operand::OperandValue;
94 use rustc_codegen_utils::check_for_rustc_errors_attr;
96 pub struct StatRecorder<'a, 'll: 'a, 'tcx: 'll> {
97 cx: &'a CodegenCx<'ll, 'tcx>,
102 impl StatRecorder<'a, 'll, 'tcx> {
103 pub fn new(cx: &'a CodegenCx<'ll, 'tcx>, name: String) -> Self {
104 let istart = cx.stats.borrow().n_llvm_insns;
113 impl Drop for StatRecorder<'a, 'll, 'tcx> {
115 if self.cx.sess().codegen_stats() {
116 let mut stats = self.cx.stats.borrow_mut();
117 let iend = stats.n_llvm_insns;
118 stats.fn_stats.push((self.name.take().unwrap(), iend - self.istart));
120 // Reset LLVM insn count to avoid compound costs.
121 stats.n_llvm_insns = self.istart;
126 pub fn bin_op_to_icmp_predicate(op: hir::BinOpKind,
130 hir::BinOpKind::Eq => IntPredicate::IntEQ,
131 hir::BinOpKind::Ne => IntPredicate::IntNE,
132 hir::BinOpKind::Lt => if signed { IntPredicate::IntSLT } else { IntPredicate::IntULT },
133 hir::BinOpKind::Le => if signed { IntPredicate::IntSLE } else { IntPredicate::IntULE },
134 hir::BinOpKind::Gt => if signed { IntPredicate::IntSGT } else { IntPredicate::IntUGT },
135 hir::BinOpKind::Ge => if signed { IntPredicate::IntSGE } else { IntPredicate::IntUGE },
137 bug!("comparison_op_to_icmp_predicate: expected comparison operator, \
144 pub fn bin_op_to_fcmp_predicate(op: hir::BinOpKind) -> RealPredicate {
146 hir::BinOpKind::Eq => RealPredicate::RealOEQ,
147 hir::BinOpKind::Ne => RealPredicate::RealUNE,
148 hir::BinOpKind::Lt => RealPredicate::RealOLT,
149 hir::BinOpKind::Le => RealPredicate::RealOLE,
150 hir::BinOpKind::Gt => RealPredicate::RealOGT,
151 hir::BinOpKind::Ge => RealPredicate::RealOGE,
153 bug!("comparison_op_to_fcmp_predicate: expected comparison operator, \
160 pub fn compare_simd_types<'a, 'll:'a, 'tcx:'ll, Builder: BuilderMethods<'a, 'll, 'tcx>>(
165 ret_ty: Builder::Type,
167 ) -> Builder::Value {
168 let signed = match t.sty {
170 let cmp = bin_op_to_fcmp_predicate(op);
171 return bx.sext(bx.fcmp(cmp, lhs, rhs), ret_ty);
173 ty::Uint(_) => false,
175 _ => bug!("compare_simd_types: invalid SIMD type"),
178 let cmp = bin_op_to_icmp_predicate(op, signed);
179 // LLVM outputs an `< size x i1 >`, so we need to perform a sign extension
180 // to get the correctly sized type. This will compile to a single instruction
181 // once the IR is converted to assembly if the SIMD instruction is supported
182 // by the target architecture.
183 bx.sext(bx.icmp(cmp, lhs, rhs), ret_ty)
186 /// Retrieve the information we are losing (making dynamic) in an unsizing
189 /// The `old_info` argument is a bit funny. It is intended for use
190 /// in an upcast, where the new vtable for an object will be derived
191 /// from the old one.
193 cx: &CodegenCx<'ll, 'tcx>,
196 old_info: Option<&'ll Value>,
198 let (source, target) = cx.tcx.struct_lockstep_tails(source, target);
199 match (&source.sty, &target.sty) {
200 (&ty::Array(_, len), &ty::Slice(_)) => {
201 cx.const_usize(len.unwrap_usize(cx.tcx))
203 (&ty::Dynamic(..), &ty::Dynamic(..)) => {
204 // For now, upcasts are limited to changes in marker
205 // traits, and hence never actually require an actual
206 // change to the vtable.
207 old_info.expect("unsized_info: missing old info for trait upcast")
209 (_, &ty::Dynamic(ref data, ..)) => {
210 let vtable_ptr = cx.layout_of(cx.tcx.mk_mut_ptr(target))
211 .field(cx, abi::FAT_PTR_EXTRA);
212 consts::ptrcast(meth::get_vtable(cx, source, data.principal()),
213 vtable_ptr.llvm_type(cx))
215 _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
221 /// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
222 pub fn unsize_thin_ptr(
223 bx: &Builder<'a, 'll, 'tcx>,
227 ) -> (&'ll Value, &'ll Value) {
228 debug!("unsize_thin_ptr: {:?} => {:?}", src_ty, dst_ty);
229 match (&src_ty.sty, &dst_ty.sty) {
233 &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) |
234 (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }),
235 &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
236 assert!(bx.cx().type_is_sized(a));
237 let ptr_ty = bx.cx().type_ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
238 (bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
240 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
241 let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
242 assert!(bx.cx().type_is_sized(a));
243 let ptr_ty = bx.cx().type_ptr_to(bx.cx().layout_of(b).llvm_type(bx.cx()));
244 (bx.pointercast(src, ptr_ty), unsized_info(bx.cx(), a, b, None))
246 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
247 assert_eq!(def_a, def_b);
249 let src_layout = bx.cx().layout_of(src_ty);
250 let dst_layout = bx.cx().layout_of(dst_ty);
251 let mut result = None;
252 for i in 0..src_layout.fields.count() {
253 let src_f = src_layout.field(bx.cx(), i);
254 assert_eq!(src_layout.fields.offset(i).bytes(), 0);
255 assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
259 assert_eq!(src_layout.size, src_f.size);
261 let dst_f = dst_layout.field(bx.cx(), i);
262 assert_ne!(src_f.ty, dst_f.ty);
263 assert_eq!(result, None);
264 result = Some(unsize_thin_ptr(bx, src, src_f.ty, dst_f.ty));
266 let (lldata, llextra) = result.unwrap();
267 // HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
268 (bx.bitcast(lldata, dst_layout.scalar_pair_element_llvm_type(bx.cx(), 0, true)),
269 bx.bitcast(llextra, dst_layout.scalar_pair_element_llvm_type(bx.cx(), 1, true)))
271 _ => bug!("unsize_thin_ptr: called on bad types"),
275 /// Coerce `src`, which is a reference to a value of type `src_ty`,
276 /// to a value of type `dst_ty` and store the result in `dst`
277 pub fn coerce_unsized_into(
278 bx: &Builder<'a, 'll, 'tcx>,
279 src: PlaceRef<'tcx, &'ll Value>,
280 dst: PlaceRef<'tcx, &'ll Value>
282 let src_ty = src.layout.ty;
283 let dst_ty = dst.layout.ty;
284 let coerce_ptr = || {
285 let (base, info) = match src.load(bx).val {
286 OperandValue::Pair(base, info) => {
287 // fat-ptr to fat-ptr unsize preserves the vtable
288 // i.e. &'a fmt::Debug+Send => &'a fmt::Debug
289 // So we need to pointercast the base to ensure
290 // the types match up.
291 let thin_ptr = dst.layout.field(bx.cx(), abi::FAT_PTR_ADDR);
292 (bx.pointercast(base, thin_ptr.llvm_type(bx.cx())), info)
294 OperandValue::Immediate(base) => {
295 unsize_thin_ptr(bx, base, src_ty, dst_ty)
297 OperandValue::Ref(..) => bug!()
299 OperandValue::Pair(base, info).store(bx, dst);
301 match (&src_ty.sty, &dst_ty.sty) {
302 (&ty::Ref(..), &ty::Ref(..)) |
303 (&ty::Ref(..), &ty::RawPtr(..)) |
304 (&ty::RawPtr(..), &ty::RawPtr(..)) => {
307 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
311 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
312 assert_eq!(def_a, def_b);
314 for i in 0..def_a.variants[VariantIdx::new(0)].fields.len() {
315 let src_f = src.project_field(bx, i);
316 let dst_f = dst.project_field(bx, i);
318 if dst_f.layout.is_zst() {
322 if src_f.layout.ty == dst_f.layout.ty {
323 memcpy_ty(bx, dst_f.llval, dst_f.align, src_f.llval, src_f.align,
324 src_f.layout, MemFlags::empty());
326 coerce_unsized_into(bx, src_f, dst_f);
330 _ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}",
336 pub fn cast_shift_expr_rhs(
337 bx: &Builder<'_, 'll, '_>, op: hir::BinOpKind, lhs: &'ll Value, rhs: &'ll Value
339 cast_shift_rhs(bx, op, lhs, rhs, |a, b| bx.trunc(a, b), |a, b| bx.zext(a, b))
342 fn cast_shift_rhs<'ll, F, G>(bx: &Builder<'_, 'll, '_>,
349 where F: FnOnce(&'ll Value, &'ll Type) -> &'ll Value,
350 G: FnOnce(&'ll Value, &'ll Type) -> &'ll Value
352 // Shifts may have any size int on the rhs
354 let mut rhs_llty = bx.cx().val_ty(rhs);
355 let mut lhs_llty = bx.cx().val_ty(lhs);
356 if bx.cx().type_kind(rhs_llty) == TypeKind::Vector {
357 rhs_llty = bx.cx().element_type(rhs_llty)
359 if bx.cx().type_kind(lhs_llty) == TypeKind::Vector {
360 lhs_llty = bx.cx().element_type(lhs_llty)
362 let rhs_sz = bx.cx().int_width(rhs_llty);
363 let lhs_sz = bx.cx().int_width(lhs_llty);
366 } else if lhs_sz > rhs_sz {
367 // FIXME (#1877: If in the future shifting by negative
368 // values is no longer undefined then this is wrong.
378 /// Returns whether this session's target will use SEH-based unwinding.
380 /// This is only true for MSVC targets, and even then the 64-bit MSVC target
381 /// currently uses SEH-ish unwinding with DWARF info tables to the side (same as
382 /// 64-bit MinGW) instead of "full SEH".
383 pub fn wants_msvc_seh(sess: &Session) -> bool {
384 sess.target.target.options.is_like_msvc
387 pub fn call_assume(bx: &Builder<'_, 'll, '_>, val: &'ll Value) {
388 let assume_intrinsic = bx.cx().get_intrinsic("llvm.assume");
389 bx.call(assume_intrinsic, &[val], None);
392 pub fn from_immediate<'a, 'll: 'a, 'tcx: 'll>(
393 bx: &Builder<'_ ,'ll, '_, &'ll Value>,
396 if bx.cx().val_ty(val) == bx.cx().type_i1() {
397 bx.zext(val, bx.cx().type_i8())
404 bx: &Builder<'_, 'll, '_>,
406 layout: layout::TyLayout,
408 if let layout::Abi::Scalar(ref scalar) = layout.abi {
409 return to_immediate_scalar(bx, val, scalar);
414 pub fn to_immediate_scalar(
415 bx: &Builder<'_, 'll, '_>,
417 scalar: &layout::Scalar,
419 if scalar.is_bool() {
420 return bx.trunc(val, bx.cx().type_i1());
425 pub fn call_memcpy<'a, 'll: 'a, 'tcx: 'll>(
426 bx: &Builder<'_ ,'ll, '_, &'ll Value>,
434 if flags.contains(MemFlags::NONTEMPORAL) {
435 // HACK(nox): This is inefficient but there is no nontemporal memcpy.
436 let val = bx.load(src, src_align);
437 let ptr = bx.pointercast(dst, bx.cx().type_ptr_to(bx.cx().val_ty(val)));
438 bx.store_with_flags(val, ptr, dst_align, flags);
442 let src_ptr = bx.pointercast(src, cx.type_i8p());
443 let dst_ptr = bx.pointercast(dst, cx.type_i8p());
444 let size = bx.intcast(n_bytes, cx.isize_ty, false);
445 let volatile = flags.contains(MemFlags::VOLATILE);
446 bx.memcpy(dst_ptr, dst_align.abi(), src_ptr, src_align.abi(), size, volatile);
449 pub fn memcpy_ty<'a, 'll: 'a, 'tcx: 'll>(
450 bx: &Builder<'_ ,'ll, '_, &'ll Value>,
455 layout: TyLayout<'tcx>,
458 let size = layout.size.bytes();
463 call_memcpy(bx, dst, dst_align, src, src_align, bx.cx().const_usize(size), flags);
467 bx: &Builder<'_, 'll, '_>,
469 fill_byte: &'ll Value,
474 let ptr_width = &bx.cx().sess().target.target.target_pointer_width;
475 let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
476 let llintrinsicfn = bx.cx().get_intrinsic(&intrinsic_key);
477 let volatile = bx.cx().const_bool(volatile);
478 bx.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None)
481 pub fn codegen_instance<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, instance: Instance<'tcx>) {
482 let _s = if cx.sess().codegen_stats() {
483 let mut instance_name = String::new();
484 DefPathBasedNames::new(cx.tcx, true, true)
485 .push_def_path(instance.def_id(), &mut instance_name);
486 Some(StatRecorder::new(cx, instance_name))
491 // this is an info! to allow collecting monomorphization statistics
492 // and to allow finding the last function before LLVM aborts from
494 info!("codegen_instance({})", instance);
496 let sig = instance.fn_sig(cx.tcx);
497 let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
499 let lldecl = cx.instances.borrow().get(&instance).cloned().unwrap_or_else(||
500 bug!("Instance `{:?}` not already declared", instance));
502 cx.stats.borrow_mut().n_closures += 1;
504 let mir = cx.tcx.instance_mir(instance.def);
505 mir::codegen_mir(cx, lldecl, &mir, instance, sig);
508 pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
509 let sect = match attrs.link_section {
514 let buf = SmallCStr::new(§.as_str());
515 llvm::LLVMSetSection(llval, buf.as_ptr());
519 /// Create the `main` function which will initialize the rust runtime and call
520 /// users main function.
521 fn maybe_create_entry_wrapper(cx: &CodegenCx) {
522 let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
523 Some((id, span, _)) => {
524 (cx.tcx.hir.local_def_id(id), span)
529 let instance = Instance::mono(cx.tcx, main_def_id);
531 if !cx.codegen_unit.contains_item(&MonoItem::Fn(instance)) {
532 // We want to create the wrapper in the same codegen unit as Rust's main
537 let main_llfn = callee::get_fn(cx, instance);
539 let et = cx.sess().entry_fn.get().map(|e| e.2);
541 Some(EntryFnType::Main) => create_entry_fn(cx, span, main_llfn, main_def_id, true),
542 Some(EntryFnType::Start) => create_entry_fn(cx, span, main_llfn, main_def_id, false),
543 None => {} // Do nothing.
547 cx: &CodegenCx<'ll, '_>,
549 rust_main: &'ll Value,
550 rust_main_def_id: DefId,
551 use_start_lang_item: bool,
554 cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int());
556 let main_ret_ty = cx.tcx.fn_sig(rust_main_def_id).output();
557 // Given that `main()` has no arguments,
558 // then its return type cannot have
559 // late-bound regions, since late-bound
560 // regions must appear in the argument
562 let main_ret_ty = cx.tcx.erase_regions(
563 &main_ret_ty.no_bound_vars().unwrap(),
566 if declare::get_defined_value(cx, "main").is_some() {
567 // FIXME: We should be smart and show a better diagnostic here.
568 cx.sess().struct_span_err(sp, "entry symbol `main` defined multiple times")
569 .help("did you use #[no_mangle] on `fn main`? Use #[start] instead")
571 cx.sess().abort_if_errors();
574 let llfn = declare::declare_cfn(cx, "main", llfty);
576 // `main` should respect same config for frame pointer elimination as rest of code
577 attributes::set_frame_pointer_elimination(cx, llfn);
578 attributes::apply_target_cpu_attr(cx, llfn);
580 let bx = Builder::new_block(cx, llfn, "top");
582 debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(&bx);
584 // Params from native main() used as args for rust start function
585 let param_argc = get_param(llfn, 0);
586 let param_argv = get_param(llfn, 1);
587 let arg_argc = bx.intcast(param_argc, cx.isize_ty, true);
588 let arg_argv = param_argv;
590 let (start_fn, args) = if use_start_lang_item {
591 let start_def_id = cx.tcx.require_lang_item(StartFnLangItem);
592 let start_fn = callee::resolve_and_get_fn(
595 cx.tcx.intern_substs(&[main_ret_ty.into()]),
597 (start_fn, vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())),
600 debug!("using user-defined start fn");
601 (rust_main, vec![arg_argc, arg_argv])
604 let result = bx.call(start_fn, &args, None);
605 bx.ret(bx.intcast(result, cx.type_int(), true));
609 fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
610 llvm_module: &ModuleLlvm)
613 use flate2::Compression;
614 use flate2::write::DeflateEncoder;
616 let (metadata_llcx, metadata_llmod) = (&*llvm_module.llcx, llvm_module.llmod());
618 #[derive(PartialEq, Eq, PartialOrd, Ord)]
625 let kind = tcx.sess.crate_types.borrow().iter().map(|ty| {
627 config::CrateType::Executable |
628 config::CrateType::Staticlib |
629 config::CrateType::Cdylib => MetadataKind::None,
631 config::CrateType::Rlib => MetadataKind::Uncompressed,
633 config::CrateType::Dylib |
634 config::CrateType::ProcMacro => MetadataKind::Compressed,
636 }).max().unwrap_or(MetadataKind::None);
638 if kind == MetadataKind::None {
639 return EncodedMetadata::new();
642 let metadata = tcx.encode_metadata();
643 if kind == MetadataKind::Uncompressed {
647 assert!(kind == MetadataKind::Compressed);
648 let mut compressed = tcx.metadata_encoding_version();
649 DeflateEncoder::new(&mut compressed, Compression::fast())
650 .write_all(&metadata.raw_data).unwrap();
652 let llmeta = common::bytes_in_context(metadata_llcx, &compressed);
653 let llconst = common::struct_in_context(metadata_llcx, &[llmeta], false);
654 let name = exported_symbols::metadata_symbol_name(tcx);
655 let buf = CString::new(name).unwrap();
656 let llglobal = unsafe {
657 llvm::LLVMAddGlobal(metadata_llmod, common::val_ty(llconst), buf.as_ptr())
660 llvm::LLVMSetInitializer(llglobal, llconst);
661 let section_name = metadata::metadata_section_name(&tcx.sess.target.target);
662 let name = SmallCStr::new(section_name);
663 llvm::LLVMSetSection(llglobal, name.as_ptr());
665 // Also generate a .section directive to force no
666 // flags, at least for ELF outputs, so that the
667 // metadata doesn't get loaded into memory.
668 let directive = format!(".section {}", section_name);
669 let directive = CString::new(directive).unwrap();
670 llvm::LLVMSetModuleInlineAsm(metadata_llmod, directive.as_ptr())
675 pub struct ValueIter<'ll> {
676 cur: Option<&'ll Value>,
677 step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
680 impl Iterator for ValueIter<'ll> {
681 type Item = &'ll Value;
683 fn next(&mut self) -> Option<&'ll Value> {
685 if let Some(old) = old {
686 self.cur = unsafe { (self.step)(old) };
692 pub fn iter_globals(llmod: &'ll llvm::Module) -> ValueIter<'ll> {
695 cur: llvm::LLVMGetFirstGlobal(llmod),
696 step: llvm::LLVMGetNextGlobal,
701 fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
702 cgu: &CodegenUnit<'tcx>)
704 if !tcx.dep_graph.is_fully_enabled() {
708 let work_product_id = &cgu.work_product_id();
709 if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
710 // We don't have anything cached for this CGU. This can happen
711 // if the CGU did not exist in the previous session.
715 // Try to mark the CGU as green. If it we can do so, it means that nothing
716 // affecting the LLVM module has changed and we can re-use a cached version.
717 // If we compile with any kind of LTO, this means we can re-use the bitcode
718 // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
719 // know that later). If we are not doing LTO, there is only one optimized
720 // version of each module, so we re-use that.
721 let dep_node = cgu.codegen_dep_node(tcx);
722 assert!(!tcx.dep_graph.dep_node_exists(&dep_node),
723 "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
726 if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() {
727 // We can re-use either the pre- or the post-thinlto state
728 if tcx.sess.lto() != Lto::No {
738 pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
739 rx: mpsc::Receiver<Box<dyn Any + Send>>)
742 check_for_rustc_errors_attr(tcx);
744 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
746 // Codegen the metadata.
747 tcx.sess.profiler(|p| p.start_activity(ProfileCategory::Codegen));
749 let metadata_cgu_name = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
751 Some("metadata")).as_str()
753 let metadata_llvm_module = ModuleLlvm::new(tcx.sess, &metadata_cgu_name);
754 let metadata = time(tcx.sess, "write metadata", || {
755 write_metadata(tcx, &metadata_llvm_module)
757 tcx.sess.profiler(|p| p.end_activity(ProfileCategory::Codegen));
759 let metadata_module = ModuleCodegen {
760 name: metadata_cgu_name,
761 module_llvm: metadata_llvm_module,
762 kind: ModuleKind::Metadata,
765 let time_graph = if tcx.sess.opts.debugging_opts.codegen_time_graph {
766 Some(time_graph::TimeGraph::new())
771 // Skip crate items and just output metadata in -Z no-codegen mode.
772 if tcx.sess.opts.debugging_opts.no_codegen ||
773 !tcx.sess.opts.output_types.should_codegen() {
774 let ongoing_codegen = write::start_async_codegen(
781 ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
782 ongoing_codegen.codegen_finished(tcx);
784 assert_and_save_dep_graph(tcx);
786 ongoing_codegen.check_for_errors(tcx.sess);
788 return ongoing_codegen;
791 // Run the monomorphization collector and partition the collected items into
793 let codegen_units = tcx.collect_and_partition_mono_items(LOCAL_CRATE).1;
794 let codegen_units = (*codegen_units).clone();
796 // Force all codegen_unit queries so they are already either red or green
797 // when compile_codegen_unit accesses them. We are not able to re-execute
798 // the codegen_unit query from just the DepNode, so an unknown color would
799 // lead to having to re-execute compile_codegen_unit, possibly
801 if tcx.dep_graph.is_fully_enabled() {
802 for cgu in &codegen_units {
803 tcx.codegen_unit(cgu.name().clone());
807 let ongoing_codegen = write::start_async_codegen(
812 codegen_units.len());
813 let ongoing_codegen = AbortCodegenOnDrop(Some(ongoing_codegen));
815 // Codegen an allocator shim, if necessary.
817 // If the crate doesn't have an `allocator_kind` set then there's definitely
818 // no shim to generate. Otherwise we also check our dependency graph for all
819 // our output crate types. If anything there looks like its a `Dynamic`
820 // linkage, then it's already got an allocator shim and we'll be using that
821 // one instead. If nothing exists then it's our job to generate the
823 let any_dynamic_crate = tcx.sess.dependency_formats.borrow()
826 use rustc::middle::dependency_format::Linkage;
827 list.iter().any(|&linkage| linkage == Linkage::Dynamic)
829 let allocator_module = if any_dynamic_crate {
831 } else if let Some(kind) = *tcx.sess.allocator_kind.get() {
832 let llmod_id = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
834 Some("allocator")).as_str()
836 let modules = ModuleLlvm::new(tcx.sess, &llmod_id);
837 time(tcx.sess, "write allocator module", || {
839 allocator::codegen(tcx, &modules, kind)
845 module_llvm: modules,
846 kind: ModuleKind::Allocator,
852 if let Some(allocator_module) = allocator_module {
853 ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, allocator_module);
856 ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, metadata_module);
858 // We sort the codegen units by size. This way we can schedule work for LLVM
859 // a bit more efficiently.
860 let codegen_units = {
861 let mut codegen_units = codegen_units;
862 codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
866 let mut total_codegen_time = Duration::new(0, 0);
867 let mut all_stats = Stats::default();
869 for cgu in codegen_units.into_iter() {
870 ongoing_codegen.wait_for_signal_to_codegen_item();
871 ongoing_codegen.check_for_errors(tcx.sess);
873 let cgu_reuse = determine_cgu_reuse(tcx, &cgu);
874 tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse);
878 let _timing_guard = time_graph.as_ref().map(|time_graph| {
879 time_graph.start(write::CODEGEN_WORKER_TIMELINE,
880 write::CODEGEN_WORK_PACKAGE_KIND,
881 &format!("codegen {}", cgu.name()))
883 let start_time = Instant::now();
884 let stats = compile_codegen_unit(tcx, *cgu.name());
885 all_stats.extend(stats);
886 total_codegen_time += start_time.elapsed();
889 CguReuse::PreLto => {
890 write::submit_pre_lto_module_to_llvm(tcx, CachedModuleCodegen {
891 name: cgu.name().to_string(),
892 source: cgu.work_product(tcx),
896 CguReuse::PostLto => {
897 write::submit_post_lto_module_to_llvm(tcx, CachedModuleCodegen {
898 name: cgu.name().to_string(),
899 source: cgu.work_product(tcx),
906 ongoing_codegen.codegen_finished(tcx);
908 // Since the main thread is sometimes blocked during codegen, we keep track
909 // -Ztime-passes output manually.
910 print_time_passes_entry(tcx.sess.time_passes(),
911 "codegen to LLVM IR",
914 rustc_incremental::assert_module_sources::assert_module_sources(tcx);
916 symbol_names_test::report_symbol_names(tcx);
918 if tcx.sess.codegen_stats() {
919 println!("--- codegen stats ---");
920 println!("n_glues_created: {}", all_stats.n_glues_created);
921 println!("n_null_glues: {}", all_stats.n_null_glues);
922 println!("n_real_glues: {}", all_stats.n_real_glues);
924 println!("n_fns: {}", all_stats.n_fns);
925 println!("n_inlines: {}", all_stats.n_inlines);
926 println!("n_closures: {}", all_stats.n_closures);
927 println!("fn stats:");
928 all_stats.fn_stats.sort_by_key(|&(_, insns)| insns);
929 for &(ref name, insns) in all_stats.fn_stats.iter() {
930 println!("{} insns, {}", insns, *name);
934 if tcx.sess.count_llvm_insns() {
935 for (k, v) in all_stats.llvm_insns.iter() {
936 println!("{:7} {}", *v, *k);
940 ongoing_codegen.check_for_errors(tcx.sess);
942 assert_and_save_dep_graph(tcx);
943 ongoing_codegen.into_inner()
946 /// A curious wrapper structure whose only purpose is to call `codegen_aborted`
947 /// when it's dropped abnormally.
949 /// In the process of working on rust-lang/rust#55238 a mysterious segfault was
950 /// stumbled upon. The segfault was never reproduced locally, but it was
951 /// suspected to be related to the fact that codegen worker threads were
952 /// sticking around by the time the main thread was exiting, causing issues.
954 /// This structure is an attempt to fix that issue where the `codegen_aborted`
955 /// message will block until all workers have finished. This should ensure that
956 /// even if the main codegen thread panics we'll wait for pending work to
957 /// complete before returning from the main thread, hopefully avoiding
960 /// If you see this comment in the code, then it means that this workaround
961 /// worked! We may yet one day track down the mysterious cause of that
963 struct AbortCodegenOnDrop(Option<OngoingCodegen>);
965 impl AbortCodegenOnDrop {
966 fn into_inner(mut self) -> OngoingCodegen {
967 self.0.take().unwrap()
971 impl Deref for AbortCodegenOnDrop {
972 type Target = OngoingCodegen;
974 fn deref(&self) -> &OngoingCodegen {
975 self.0.as_ref().unwrap()
979 impl DerefMut for AbortCodegenOnDrop {
980 fn deref_mut(&mut self) -> &mut OngoingCodegen {
981 self.0.as_mut().unwrap()
985 impl Drop for AbortCodegenOnDrop {
987 if let Some(codegen) = self.0.take() {
988 codegen.codegen_aborted();
993 fn assert_and_save_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
996 || rustc_incremental::assert_dep_graph(tcx));
999 "serialize dep graph",
1000 || rustc_incremental::save_dep_graph(tcx));
1004 pub fn new(tcx: TyCtxt) -> CrateInfo {
1005 let mut info = CrateInfo {
1006 panic_runtime: None,
1007 compiler_builtins: None,
1008 profiler_runtime: None,
1009 sanitizer_runtime: None,
1010 is_no_builtins: Default::default(),
1011 native_libraries: Default::default(),
1012 used_libraries: tcx.native_libraries(LOCAL_CRATE),
1013 link_args: tcx.link_args(LOCAL_CRATE),
1014 crate_name: Default::default(),
1015 used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
1016 used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
1017 used_crate_source: Default::default(),
1018 wasm_imports: Default::default(),
1019 lang_item_to_crate: Default::default(),
1020 missing_lang_items: Default::default(),
1022 let lang_items = tcx.lang_items();
1024 let load_wasm_items = tcx.sess.crate_types.borrow()
1026 .any(|c| *c != config::CrateType::Rlib) &&
1027 tcx.sess.opts.target_triple.triple() == "wasm32-unknown-unknown";
1029 if load_wasm_items {
1030 info.load_wasm_imports(tcx, LOCAL_CRATE);
1033 let crates = tcx.crates();
1035 let n_crates = crates.len();
1036 info.native_libraries.reserve(n_crates);
1037 info.crate_name.reserve(n_crates);
1038 info.used_crate_source.reserve(n_crates);
1039 info.missing_lang_items.reserve(n_crates);
1041 for &cnum in crates.iter() {
1042 info.native_libraries.insert(cnum, tcx.native_libraries(cnum));
1043 info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
1044 info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum));
1045 if tcx.is_panic_runtime(cnum) {
1046 info.panic_runtime = Some(cnum);
1048 if tcx.is_compiler_builtins(cnum) {
1049 info.compiler_builtins = Some(cnum);
1051 if tcx.is_profiler_runtime(cnum) {
1052 info.profiler_runtime = Some(cnum);
1054 if tcx.is_sanitizer_runtime(cnum) {
1055 info.sanitizer_runtime = Some(cnum);
1057 if tcx.is_no_builtins(cnum) {
1058 info.is_no_builtins.insert(cnum);
1060 if load_wasm_items {
1061 info.load_wasm_imports(tcx, cnum);
1063 let missing = tcx.missing_lang_items(cnum);
1064 for &item in missing.iter() {
1065 if let Ok(id) = lang_items.require(item) {
1066 info.lang_item_to_crate.insert(item, id.krate);
1070 // No need to look for lang items that are whitelisted and don't
1071 // actually need to exist.
1072 let missing = missing.iter()
1074 .filter(|&l| !weak_lang_items::whitelisted(tcx, l))
1076 info.missing_lang_items.insert(cnum, missing);
1082 fn load_wasm_imports(&mut self, tcx: TyCtxt, cnum: CrateNum) {
1083 self.wasm_imports.extend(tcx.wasm_import_module_map(cnum).iter().map(|(&id, module)| {
1084 let instance = Instance::mono(tcx, id);
1085 let import_name = tcx.symbol_name(instance);
1087 (import_name.to_string(), module.clone())
1092 fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1093 cgu_name: InternedString)
1095 let start_time = Instant::now();
1097 let dep_node = tcx.codegen_unit(cgu_name).codegen_dep_node(tcx);
1098 let ((stats, module), _) = tcx.dep_graph.with_task(dep_node,
1102 let time_to_codegen = start_time.elapsed();
1104 // We assume that the cost to run LLVM on a CGU is proportional to
1105 // the time we needed for codegenning it.
1106 let cost = time_to_codegen.as_secs() * 1_000_000_000 +
1107 time_to_codegen.subsec_nanos() as u64;
1109 write::submit_codegened_module_to_llvm(tcx,
1114 fn module_codegen<'a, 'tcx>(
1115 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1116 cgu_name: InternedString)
1117 -> (Stats, ModuleCodegen)
1119 let cgu = tcx.codegen_unit(cgu_name);
1121 // Instantiate monomorphizations without filling out definitions yet...
1122 let llvm_module = ModuleLlvm::new(tcx.sess, &cgu_name.as_str());
1124 let cx = CodegenCx::new(tcx, cgu, &llvm_module);
1125 let mono_items = cx.codegen_unit
1126 .items_in_deterministic_order(cx.tcx);
1127 for &(mono_item, (linkage, visibility)) in &mono_items {
1128 mono_item.predefine(&cx, linkage, visibility);
1131 // ... and now that we have everything pre-defined, fill out those definitions.
1132 for &(mono_item, _) in &mono_items {
1133 mono_item.define(&cx);
1136 // If this codegen unit contains the main function, also create the
1138 maybe_create_entry_wrapper(&cx);
1140 // Run replace-all-uses-with for statics that need it
1141 for &(old_g, new_g) in cx.statics_to_rauw.borrow().iter() {
1143 let bitcast = llvm::LLVMConstPointerCast(new_g, cx.val_ty(old_g));
1144 llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
1145 llvm::LLVMDeleteGlobal(old_g);
1149 // Create the llvm.used variable
1150 // This variable has type [N x i8*] and is stored in the llvm.metadata section
1151 if !cx.used_statics.borrow().is_empty() {
1152 let name = const_cstr!("llvm.used");
1153 let section = const_cstr!("llvm.metadata");
1154 let array = cx.const_array(
1155 &cx.type_ptr_to(cx.type_i8()),
1156 &*cx.used_statics.borrow()
1160 let g = llvm::LLVMAddGlobal(cx.llmod,
1163 llvm::LLVMSetInitializer(g, array);
1164 llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
1165 llvm::LLVMSetSection(g, section.as_ptr());
1169 // Finalize debuginfo
1170 if cx.sess().opts.debuginfo != DebugInfo::None {
1171 debuginfo::finalize(&cx);
1174 cx.stats.into_inner()
1177 (stats, ModuleCodegen {
1178 name: cgu_name.to_string(),
1179 module_llvm: llvm_module,
1180 kind: ModuleKind::Regular,
1185 pub fn provide_both(providers: &mut Providers) {
1186 providers.dllimport_foreign_items = |tcx, krate| {
1187 let module_map = tcx.foreign_modules(krate);
1188 let module_map = module_map.iter()
1189 .map(|lib| (lib.def_id, lib))
1190 .collect::<FxHashMap<_, _>>();
1192 let dllimports = tcx.native_libraries(krate)
1195 if lib.kind != cstore::NativeLibraryKind::NativeUnknown {
1198 let cfg = match lib.cfg {
1199 Some(ref cfg) => cfg,
1200 None => return true,
1202 attr::cfg_matches(cfg, &tcx.sess.parse_sess, None)
1204 .filter_map(|lib| lib.foreign_module)
1205 .map(|id| &module_map[&id])
1206 .flat_map(|module| module.foreign_items.iter().cloned())
1208 Lrc::new(dllimports)
1211 providers.is_dllimport_foreign_item = |tcx, def_id| {
1212 tcx.dllimport_foreign_items(def_id.krate).contains(&def_id)
1216 pub fn linkage_to_llvm(linkage: Linkage) -> llvm::Linkage {
1218 Linkage::External => llvm::Linkage::ExternalLinkage,
1219 Linkage::AvailableExternally => llvm::Linkage::AvailableExternallyLinkage,
1220 Linkage::LinkOnceAny => llvm::Linkage::LinkOnceAnyLinkage,
1221 Linkage::LinkOnceODR => llvm::Linkage::LinkOnceODRLinkage,
1222 Linkage::WeakAny => llvm::Linkage::WeakAnyLinkage,
1223 Linkage::WeakODR => llvm::Linkage::WeakODRLinkage,
1224 Linkage::Appending => llvm::Linkage::AppendingLinkage,
1225 Linkage::Internal => llvm::Linkage::InternalLinkage,
1226 Linkage::Private => llvm::Linkage::PrivateLinkage,
1227 Linkage::ExternalWeak => llvm::Linkage::ExternalWeakLinkage,
1228 Linkage::Common => llvm::Linkage::CommonLinkage,
1232 pub fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
1234 Visibility::Default => llvm::Visibility::Default,
1235 Visibility::Hidden => llvm::Visibility::Hidden,
1236 Visibility::Protected => llvm::Visibility::Protected,
1240 // FIXME(mw): Anything that is produced via DepGraph::with_task() must implement
1241 // the HashStable trait. Normally DepGraph::with_task() calls are
1242 // hidden behind queries, but CGU creation is a special case in two
1243 // ways: (1) it's not a query and (2) CGU are output nodes, so their
1244 // Fingerprints are not actually needed. It remains to be clarified
1245 // how exactly this case will be handled in the red/green system but
1246 // for now we content ourselves with providing a no-op HashStable
1247 // implementation for CGUs.
1248 mod temp_stable_hash_impls {
1249 use rustc_data_structures::stable_hasher::{StableHasherResult, StableHasher,
1253 impl<HCX> HashStable<HCX> for ModuleCodegen {
1254 fn hash_stable<W: StableHasherResult>(&self,
1256 _: &mut StableHasher<W>) {