1 use crate::back::metadata::create_compressed_metadata_file;
2 use crate::back::write::{
3 compute_per_cgu_lto_type, start_async_codegen, submit_codegened_module_to_llvm,
4 submit_post_lto_module_to_llvm, submit_pre_lto_module_to_llvm, ComputedLtoType, OngoingCodegen,
6 use crate::common::{IntPredicate, RealPredicate, TypeKind};
9 use crate::mir::operand::OperandValue;
10 use crate::mir::place::PlaceRef;
12 use crate::{CachedModuleCodegen, CompiledModule, CrateInfo, MemFlags, ModuleCodegen, ModuleKind};
14 use rustc_attr as attr;
15 use rustc_data_structures::fx::FxHashMap;
16 use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
18 use rustc_data_structures::sync::par_iter;
19 #[cfg(parallel_compiler)]
20 use rustc_data_structures::sync::ParallelIterator;
22 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
23 use rustc_hir::lang_items::LangItem;
24 use rustc_index::vec::Idx;
25 use rustc_metadata::EncodedMetadata;
26 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
27 use rustc_middle::middle::exported_symbols;
28 use rustc_middle::middle::lang_items;
29 use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
30 use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
31 use rustc_middle::ty::query::Providers;
32 use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
33 use rustc_session::cgu_reuse_tracker::CguReuse;
34 use rustc_session::config::{self, CrateType, EntryFnType, OutputType};
35 use rustc_session::Session;
36 use rustc_span::symbol::sym;
37 use rustc_span::{DebuggerVisualizerFile, DebuggerVisualizerType};
38 use rustc_target::abi::{Align, VariantIdx};
40 use std::collections::BTreeSet;
41 use std::convert::TryFrom;
42 use std::ops::{Deref, DerefMut};
43 use std::time::{Duration, Instant};
45 use itertools::Itertools;
47 pub fn bin_op_to_icmp_predicate(op: hir::BinOpKind, signed: bool) -> IntPredicate {
49 hir::BinOpKind::Eq => IntPredicate::IntEQ,
50 hir::BinOpKind::Ne => IntPredicate::IntNE,
51 hir::BinOpKind::Lt => {
58 hir::BinOpKind::Le => {
65 hir::BinOpKind::Gt => {
72 hir::BinOpKind::Ge => {
80 "comparison_op_to_icmp_predicate: expected comparison operator, \
87 pub fn bin_op_to_fcmp_predicate(op: hir::BinOpKind) -> RealPredicate {
89 hir::BinOpKind::Eq => RealPredicate::RealOEQ,
90 hir::BinOpKind::Ne => RealPredicate::RealUNE,
91 hir::BinOpKind::Lt => RealPredicate::RealOLT,
92 hir::BinOpKind::Le => RealPredicate::RealOLE,
93 hir::BinOpKind::Gt => RealPredicate::RealOGT,
94 hir::BinOpKind::Ge => RealPredicate::RealOGE,
97 "comparison_op_to_fcmp_predicate: expected comparison operator, \
105 pub fn compare_simd_types<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
113 let signed = match t.kind() {
115 let cmp = bin_op_to_fcmp_predicate(op);
116 let cmp = bx.fcmp(cmp, lhs, rhs);
117 return bx.sext(cmp, ret_ty);
119 ty::Uint(_) => false,
121 _ => bug!("compare_simd_types: invalid SIMD type"),
124 let cmp = bin_op_to_icmp_predicate(op, signed);
125 let cmp = bx.icmp(cmp, lhs, rhs);
126 // LLVM outputs an `< size x i1 >`, so we need to perform a sign extension
127 // to get the correctly sized type. This will compile to a single instruction
128 // once the IR is converted to assembly if the SIMD instruction is supported
129 // by the target architecture.
133 /// Retrieves the information we are losing (making dynamic) in an unsizing
136 /// The `old_info` argument is a bit odd. It is intended for use in an upcast,
137 /// where the new vtable for an object will be derived from the old one.
138 pub fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
142 old_info: Option<Bx::Value>,
145 let (source, target) =
146 cx.tcx().struct_lockstep_tails_erasing_lifetimes(source, target, bx.param_env());
147 match (source.kind(), target.kind()) {
148 (&ty::Array(_, len), &ty::Slice(_)) => {
149 cx.const_usize(len.eval_usize(cx.tcx(), ty::ParamEnv::reveal_all()))
151 (&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
153 old_info.expect("unsized_info: missing old info for trait upcasting coercion");
154 if data_a.principal_def_id() == data_b.principal_def_id() {
158 // trait upcasting coercion
161 cx.tcx().vtable_trait_upcasting_coercion_new_vptr_slot((source, target));
163 if let Some(entry_idx) = vptr_entry_idx {
164 let ptr_ty = cx.type_i8p();
165 let ptr_align = cx.tcx().data_layout.pointer_align.abi;
166 let llvtable = bx.pointercast(old_info, bx.type_ptr_to(ptr_ty));
167 let gep = bx.inbounds_gep(
170 &[bx.const_usize(u64::try_from(entry_idx).unwrap())],
172 let new_vptr = bx.load(ptr_ty, gep, ptr_align);
173 bx.nonnull_metadata(new_vptr);
174 // Vtable loads are invariant.
175 bx.set_invariant_load(new_vptr);
181 (_, &ty::Dynamic(ref data, ..)) => {
182 let vtable_ptr_ty = cx.scalar_pair_element_backend_type(
183 cx.layout_of(cx.tcx().mk_mut_ptr(target)),
187 cx.const_ptrcast(meth::get_vtable(cx, source, data.principal()), vtable_ptr_ty)
189 _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}", source, target),
193 /// Coerces `src` to `dst_ty`. `src_ty` must be a pointer.
194 pub fn unsize_ptr<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
199 old_info: Option<Bx::Value>,
200 ) -> (Bx::Value, Bx::Value) {
201 debug!("unsize_ptr: {:?} => {:?}", src_ty, dst_ty);
202 match (src_ty.kind(), dst_ty.kind()) {
203 (&ty::Ref(_, a, _), &ty::Ref(_, b, _) | &ty::RawPtr(ty::TypeAndMut { ty: b, .. }))
204 | (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }), &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
205 assert_eq!(bx.cx().type_is_sized(a), old_info.is_none());
206 let ptr_ty = bx.cx().type_ptr_to(bx.cx().backend_type(bx.cx().layout_of(b)));
207 (bx.pointercast(src, ptr_ty), unsized_info(bx, a, b, old_info))
209 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
210 assert_eq!(def_a, def_b);
211 let src_layout = bx.cx().layout_of(src_ty);
212 let dst_layout = bx.cx().layout_of(dst_ty);
213 if src_ty == dst_ty {
214 return (src, old_info.unwrap());
216 let mut result = None;
217 for i in 0..src_layout.fields.count() {
218 let src_f = src_layout.field(bx.cx(), i);
223 assert_eq!(src_layout.fields.offset(i).bytes(), 0);
224 assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
225 assert_eq!(src_layout.size, src_f.size);
227 let dst_f = dst_layout.field(bx.cx(), i);
228 assert_ne!(src_f.ty, dst_f.ty);
229 assert_eq!(result, None);
230 result = Some(unsize_ptr(bx, src, src_f.ty, dst_f.ty, old_info));
232 let (lldata, llextra) = result.unwrap();
233 let lldata_ty = bx.cx().scalar_pair_element_backend_type(dst_layout, 0, true);
234 let llextra_ty = bx.cx().scalar_pair_element_backend_type(dst_layout, 1, true);
235 // HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
236 (bx.bitcast(lldata, lldata_ty), bx.bitcast(llextra, llextra_ty))
238 _ => bug!("unsize_ptr: called on bad types"),
242 /// Coerces `src`, which is a reference to a value of type `src_ty`,
243 /// to a value of type `dst_ty`, and stores the result in `dst`.
244 pub fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
246 src: PlaceRef<'tcx, Bx::Value>,
247 dst: PlaceRef<'tcx, Bx::Value>,
249 let src_ty = src.layout.ty;
250 let dst_ty = dst.layout.ty;
251 match (src_ty.kind(), dst_ty.kind()) {
252 (&ty::Ref(..), &ty::Ref(..) | &ty::RawPtr(..)) | (&ty::RawPtr(..), &ty::RawPtr(..)) => {
253 let (base, info) = match bx.load_operand(src).val {
254 OperandValue::Pair(base, info) => unsize_ptr(bx, base, src_ty, dst_ty, Some(info)),
255 OperandValue::Immediate(base) => unsize_ptr(bx, base, src_ty, dst_ty, None),
256 OperandValue::Ref(..) => bug!(),
258 OperandValue::Pair(base, info).store(bx, dst);
261 (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
262 assert_eq!(def_a, def_b);
264 for i in 0..def_a.variant(VariantIdx::new(0)).fields.len() {
265 let src_f = src.project_field(bx, i);
266 let dst_f = dst.project_field(bx, i);
268 if dst_f.layout.is_zst() {
272 if src_f.layout.ty == dst_f.layout.ty {
283 coerce_unsized_into(bx, src_f, dst_f);
287 _ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}", src_ty, dst_ty,),
291 pub fn cast_shift_expr_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
297 cast_shift_rhs(bx, op, lhs, rhs)
300 fn cast_shift_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
306 // Shifts may have any size int on the rhs
308 let mut rhs_llty = bx.cx().val_ty(rhs);
309 let mut lhs_llty = bx.cx().val_ty(lhs);
310 if bx.cx().type_kind(rhs_llty) == TypeKind::Vector {
311 rhs_llty = bx.cx().element_type(rhs_llty)
313 if bx.cx().type_kind(lhs_llty) == TypeKind::Vector {
314 lhs_llty = bx.cx().element_type(lhs_llty)
316 let rhs_sz = bx.cx().int_width(rhs_llty);
317 let lhs_sz = bx.cx().int_width(lhs_llty);
319 bx.trunc(rhs, lhs_llty)
320 } else if lhs_sz > rhs_sz {
321 // FIXME (#1877: If in the future shifting by negative
322 // values is no longer undefined then this is wrong.
323 bx.zext(rhs, lhs_llty)
332 /// Returns `true` if this session's target will use SEH-based unwinding.
334 /// This is only true for MSVC targets, and even then the 64-bit MSVC target
335 /// currently uses SEH-ish unwinding with DWARF info tables to the side (same as
336 /// 64-bit MinGW) instead of "full SEH".
337 pub fn wants_msvc_seh(sess: &Session) -> bool {
338 sess.target.is_like_msvc
341 pub fn memcpy_ty<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
347 layout: TyAndLayout<'tcx>,
350 let size = layout.size.bytes();
355 bx.memcpy(dst, dst_align, src, src_align, bx.cx().const_usize(size), flags);
358 pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
359 cx: &'a Bx::CodegenCx,
360 instance: Instance<'tcx>,
362 // this is an info! to allow collecting monomorphization statistics
363 // and to allow finding the last function before LLVM aborts from
365 info!("codegen_instance({})", instance);
367 mir::codegen_mir::<Bx>(cx, instance);
370 /// Creates the `main` function which will initialize the rust runtime and call
371 /// users main function.
372 pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
373 cx: &'a Bx::CodegenCx,
374 ) -> Option<Bx::Function> {
375 let (main_def_id, entry_type) = cx.tcx().entry_fn(())?;
376 let main_is_local = main_def_id.is_local();
377 let instance = Instance::mono(cx.tcx(), main_def_id);
380 // We want to create the wrapper in the same codegen unit as Rust's main
382 if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
385 } else if !cx.codegen_unit().is_primary() {
386 // We want to create the wrapper only when the codegen unit is the primary one
390 let main_llfn = cx.get_fn_addr(instance);
392 let use_start_lang_item = EntryFnType::Start != entry_type;
393 let entry_fn = create_entry_fn::<Bx>(cx, main_llfn, main_def_id, use_start_lang_item);
394 return Some(entry_fn);
396 fn create_entry_fn<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
397 cx: &'a Bx::CodegenCx,
398 rust_main: Bx::Value,
399 rust_main_def_id: DefId,
400 use_start_lang_item: bool,
402 // The entry function is either `int main(void)` or `int main(int argc, char **argv)`,
403 // depending on whether the target needs `argc` and `argv` to be passed in.
404 let llfty = if cx.sess().target.main_needs_argc_argv {
405 cx.type_func(&[cx.type_int(), cx.type_ptr_to(cx.type_i8p())], cx.type_int())
407 cx.type_func(&[], cx.type_int())
410 let main_ret_ty = cx.tcx().fn_sig(rust_main_def_id).output();
411 // Given that `main()` has no arguments,
412 // then its return type cannot have
413 // late-bound regions, since late-bound
414 // regions must appear in the argument
416 let main_ret_ty = cx.tcx().normalize_erasing_regions(
417 ty::ParamEnv::reveal_all(),
418 main_ret_ty.no_bound_vars().unwrap(),
421 let Some(llfn) = cx.declare_c_main(llfty) else {
422 // FIXME: We should be smart and show a better diagnostic here.
423 let span = cx.tcx().def_span(rust_main_def_id);
425 .struct_span_err(span, "entry symbol `main` declared multiple times")
426 .help("did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead")
428 cx.sess().abort_if_errors();
432 // `main` should respect same config for frame pointer elimination as rest of code
433 cx.set_frame_pointer_type(llfn);
434 cx.apply_target_cpu_attr(llfn);
436 let llbb = Bx::append_block(&cx, llfn, "top");
437 let mut bx = Bx::build(&cx, llbb);
439 bx.insert_reference_to_gdb_debug_scripts_section_global();
441 let isize_ty = cx.type_isize();
442 let i8pp_ty = cx.type_ptr_to(cx.type_i8p());
443 let (arg_argc, arg_argv) = get_argc_argv(cx, &mut bx);
445 let (start_fn, start_ty, args) = if use_start_lang_item {
446 let start_def_id = cx.tcx().require_lang_item(LangItem::Start, None);
447 let start_fn = cx.get_fn_addr(
448 ty::Instance::resolve(
450 ty::ParamEnv::reveal_all(),
452 cx.tcx().intern_substs(&[main_ret_ty.into()]),
457 let start_ty = cx.type_func(&[cx.val_ty(rust_main), isize_ty, i8pp_ty], isize_ty);
458 (start_fn, start_ty, vec![rust_main, arg_argc, arg_argv])
460 debug!("using user-defined start fn");
461 let start_ty = cx.type_func(&[isize_ty, i8pp_ty], isize_ty);
462 (rust_main, start_ty, vec![arg_argc, arg_argv])
465 let result = bx.call(start_ty, start_fn, &args, None);
466 let cast = bx.intcast(result, cx.type_int(), true);
473 /// Obtain the `argc` and `argv` values to pass to the rust start function.
474 fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
475 cx: &'a Bx::CodegenCx,
477 ) -> (Bx::Value, Bx::Value) {
478 if cx.sess().target.main_needs_argc_argv {
479 // Params from native `main()` used as args for rust start function
480 let param_argc = bx.get_param(0);
481 let param_argv = bx.get_param(1);
482 let arg_argc = bx.intcast(param_argc, cx.type_isize(), true);
483 let arg_argv = param_argv;
486 // The Rust start function doesn't need `argc` and `argv`, so just pass zeros.
487 let arg_argc = bx.const_int(cx.type_int(), 0);
488 let arg_argv = bx.const_null(cx.type_ptr_to(cx.type_i8p()));
493 /// This function returns all of the debugger visualizers specified for the
494 /// current crate as well as all upstream crates transitively that match the
495 /// `visualizer_type` specified.
496 pub fn collect_debugger_visualizers_transitive(
498 visualizer_type: DebuggerVisualizerType,
499 ) -> BTreeSet<DebuggerVisualizerFile> {
500 tcx.debugger_visualizers(LOCAL_CRATE)
506 let used_crate_source = tcx.used_crate_source(*cnum);
507 used_crate_source.rlib.is_some() || used_crate_source.rmeta.is_some()
509 .flat_map(|&cnum| tcx.debugger_visualizers(cnum)),
511 .filter(|visualizer| visualizer.visualizer_type == visualizer_type)
513 .collect::<BTreeSet<_>>()
516 pub fn codegen_crate<B: ExtraBackendMethods>(
520 metadata: EncodedMetadata,
521 need_metadata_module: bool,
522 ) -> OngoingCodegen<B> {
523 // Skip crate items and just output metadata in -Z no-codegen mode.
524 if tcx.sess.opts.unstable_opts.no_codegen || !tcx.sess.opts.output_types.should_codegen() {
525 let ongoing_codegen = start_async_codegen(backend, tcx, target_cpu, metadata, None, 1);
527 ongoing_codegen.codegen_finished(tcx);
529 ongoing_codegen.check_for_errors(tcx.sess);
531 return ongoing_codegen;
534 let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
536 // Run the monomorphization collector and partition the collected items into
538 let codegen_units = tcx.collect_and_partition_mono_items(()).1;
540 // Force all codegen_unit queries so they are already either red or green
541 // when compile_codegen_unit accesses them. We are not able to re-execute
542 // the codegen_unit query from just the DepNode, so an unknown color would
543 // lead to having to re-execute compile_codegen_unit, possibly
545 if tcx.dep_graph.is_fully_enabled() {
546 for cgu in codegen_units {
547 tcx.ensure().codegen_unit(cgu.name());
551 let metadata_module = if need_metadata_module {
552 // Emit compressed metadata object.
553 let metadata_cgu_name =
554 cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata")).to_string();
555 tcx.sess.time("write_compressed_metadata", || {
557 tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
558 let data = create_compressed_metadata_file(
561 &exported_symbols::metadata_symbol_name(tcx),
563 if let Err(err) = std::fs::write(&file_name, data) {
564 tcx.sess.fatal(&format!("error writing metadata object file: {}", err));
566 Some(CompiledModule {
567 name: metadata_cgu_name,
568 kind: ModuleKind::Metadata,
569 object: Some(file_name),
578 let ongoing_codegen = start_async_codegen(
586 let ongoing_codegen = AbortCodegenOnDrop::<B>(Some(ongoing_codegen));
588 // Codegen an allocator shim, if necessary.
590 // If the crate doesn't have an `allocator_kind` set then there's definitely
591 // no shim to generate. Otherwise we also check our dependency graph for all
592 // our output crate types. If anything there looks like its a `Dynamic`
593 // linkage, then it's already got an allocator shim and we'll be using that
594 // one instead. If nothing exists then it's our job to generate the
596 let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
597 use rustc_middle::middle::dependency_format::Linkage;
598 list.iter().any(|&linkage| linkage == Linkage::Dynamic)
600 let allocator_module = if any_dynamic_crate {
602 } else if let Some(kind) = tcx.allocator_kind(()) {
604 cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
605 let module_llvm = tcx.sess.time("write_allocator_module", || {
606 backend.codegen_allocator(tcx, &llmod_id, kind, tcx.lang_items().oom().is_some())
609 Some(ModuleCodegen { name: llmod_id, module_llvm, kind: ModuleKind::Allocator })
614 if let Some(allocator_module) = allocator_module {
615 ongoing_codegen.submit_pre_codegened_module_to_llvm(tcx, allocator_module);
618 // For better throughput during parallel processing by LLVM, we used to sort
619 // CGUs largest to smallest. This would lead to better thread utilization
620 // by, for example, preventing a large CGU from being processed last and
621 // having only one LLVM thread working while the rest remained idle.
623 // However, this strategy would lead to high memory usage, as it meant the
624 // LLVM-IR for all of the largest CGUs would be resident in memory at once.
626 // Instead, we can compromise by ordering CGUs such that the largest and
627 // smallest are first, second largest and smallest are next, etc. If there
628 // are large size variations, this can reduce memory usage significantly.
629 let codegen_units: Vec<_> = {
630 let mut sorted_cgus = codegen_units.iter().collect::<Vec<_>>();
631 sorted_cgus.sort_by_cached_key(|cgu| cgu.size_estimate());
633 let (first_half, second_half) = sorted_cgus.split_at(sorted_cgus.len() / 2);
634 second_half.iter().rev().interleave(first_half).copied().collect()
637 // Calculate the CGU reuse
638 let cgu_reuse = tcx.sess.time("find_cgu_reuse", || {
639 codegen_units.iter().map(|cgu| determine_cgu_reuse(tcx, &cgu)).collect::<Vec<_>>()
642 let mut total_codegen_time = Duration::new(0, 0);
643 let start_rss = tcx.sess.time_passes().then(|| get_resident_set_size());
645 // The non-parallel compiler can only translate codegen units to LLVM IR
646 // on a single thread, leading to a staircase effect where the N LLVM
647 // threads have to wait on the single codegen threads to generate work
648 // for them. The parallel compiler does not have this restriction, so
649 // we can pre-load the LLVM queue in parallel before handing off
650 // coordination to the OnGoingCodegen scheduler.
652 // This likely is a temporary measure. Once we don't have to support the
653 // non-parallel compiler anymore, we can compile CGUs end-to-end in
654 // parallel and get rid of the complicated scheduling logic.
655 let mut pre_compiled_cgus = if cfg!(parallel_compiler) {
656 tcx.sess.time("compile_first_CGU_batch", || {
657 // Try to find one CGU to compile per thread.
658 let cgus: Vec<_> = cgu_reuse
661 .filter(|&(_, reuse)| reuse == &CguReuse::No)
662 .take(tcx.sess.threads())
665 // Compile the found CGUs in parallel.
666 let start_time = Instant::now();
668 let pre_compiled_cgus = par_iter(cgus)
670 let module = backend.compile_codegen_unit(tcx, codegen_units[i].name());
675 total_codegen_time += start_time.elapsed();
683 for (i, cgu) in codegen_units.iter().enumerate() {
684 ongoing_codegen.wait_for_signal_to_codegen_item();
685 ongoing_codegen.check_for_errors(tcx.sess);
687 let cgu_reuse = cgu_reuse[i];
688 tcx.sess.cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
692 let (module, cost) = if let Some(cgu) = pre_compiled_cgus.remove(&i) {
695 let start_time = Instant::now();
696 let module = backend.compile_codegen_unit(tcx, cgu.name());
697 total_codegen_time += start_time.elapsed();
700 // This will unwind if there are errors, which triggers our `AbortCodegenOnDrop`
701 // guard. Unfortunately, just skipping the `submit_codegened_module_to_llvm` makes
702 // compilation hang on post-monomorphization errors.
703 tcx.sess.abort_if_errors();
705 submit_codegened_module_to_llvm(
707 &ongoing_codegen.coordinator_send,
713 CguReuse::PreLto => {
714 submit_pre_lto_module_to_llvm(
717 &ongoing_codegen.coordinator_send,
718 CachedModuleCodegen {
719 name: cgu.name().to_string(),
720 source: cgu.previous_work_product(tcx),
725 CguReuse::PostLto => {
726 submit_post_lto_module_to_llvm(
728 &ongoing_codegen.coordinator_send,
729 CachedModuleCodegen {
730 name: cgu.name().to_string(),
731 source: cgu.previous_work_product(tcx),
739 ongoing_codegen.codegen_finished(tcx);
741 // Since the main thread is sometimes blocked during codegen, we keep track
742 // -Ztime-passes output manually.
743 if tcx.sess.time_passes() {
744 let end_rss = get_resident_set_size();
746 print_time_passes_entry(
747 "codegen_to_LLVM_IR",
754 ongoing_codegen.check_for_errors(tcx.sess);
756 ongoing_codegen.into_inner()
759 /// A curious wrapper structure whose only purpose is to call `codegen_aborted`
760 /// when it's dropped abnormally.
762 /// In the process of working on rust-lang/rust#55238 a mysterious segfault was
763 /// stumbled upon. The segfault was never reproduced locally, but it was
764 /// suspected to be related to the fact that codegen worker threads were
765 /// sticking around by the time the main thread was exiting, causing issues.
767 /// This structure is an attempt to fix that issue where the `codegen_aborted`
768 /// message will block until all workers have finished. This should ensure that
769 /// even if the main codegen thread panics we'll wait for pending work to
770 /// complete before returning from the main thread, hopefully avoiding
773 /// If you see this comment in the code, then it means that this workaround
774 /// worked! We may yet one day track down the mysterious cause of that
776 struct AbortCodegenOnDrop<B: ExtraBackendMethods>(Option<OngoingCodegen<B>>);
778 impl<B: ExtraBackendMethods> AbortCodegenOnDrop<B> {
779 fn into_inner(mut self) -> OngoingCodegen<B> {
780 self.0.take().unwrap()
784 impl<B: ExtraBackendMethods> Deref for AbortCodegenOnDrop<B> {
785 type Target = OngoingCodegen<B>;
787 fn deref(&self) -> &OngoingCodegen<B> {
788 self.0.as_ref().unwrap()
792 impl<B: ExtraBackendMethods> DerefMut for AbortCodegenOnDrop<B> {
793 fn deref_mut(&mut self) -> &mut OngoingCodegen<B> {
794 self.0.as_mut().unwrap()
798 impl<B: ExtraBackendMethods> Drop for AbortCodegenOnDrop<B> {
800 if let Some(codegen) = self.0.take() {
801 codegen.codegen_aborted();
807 pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> CrateInfo {
808 let exported_symbols = tcx
812 .map(|&c| (c, crate::back::linker::exported_symbols(tcx, c)))
814 let linked_symbols = tcx
818 .map(|&c| (c, crate::back::linker::linked_symbols(tcx, c)))
820 let local_crate_name = tcx.crate_name(LOCAL_CRATE);
821 let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
822 let subsystem = tcx.sess.first_attr_value_str_by_name(crate_attrs, sym::windows_subsystem);
823 let windows_subsystem = subsystem.map(|subsystem| {
824 if subsystem != sym::windows && subsystem != sym::console {
825 tcx.sess.fatal(&format!(
826 "invalid windows subsystem `{}`, only \
827 `windows` and `console` are allowed",
831 subsystem.to_string()
834 // This list is used when generating the command line to pass through to
835 // system linker. The linker expects undefined symbols on the left of the
836 // command line to be defined in libraries on the right, not the other way
837 // around. For more info, see some comments in the add_used_library function
840 // In order to get this left-to-right dependency ordering, we use the reverse
841 // postorder of all crates putting the leaves at the right-most positions.
842 let used_crates = tcx
847 .filter(|&cnum| !tcx.dep_kind(cnum).macros_only())
850 let mut info = CrateInfo {
855 compiler_builtins: None,
856 profiler_runtime: None,
857 is_no_builtins: Default::default(),
858 native_libraries: Default::default(),
859 used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
860 crate_name: Default::default(),
862 used_crate_source: Default::default(),
863 lang_item_to_crate: Default::default(),
864 missing_lang_items: Default::default(),
865 dependency_formats: tcx.dependency_formats(()).clone(),
867 natvis_debugger_visualizers: Default::default(),
869 let lang_items = tcx.lang_items();
871 let crates = tcx.crates(());
873 let n_crates = crates.len();
874 info.native_libraries.reserve(n_crates);
875 info.crate_name.reserve(n_crates);
876 info.used_crate_source.reserve(n_crates);
877 info.missing_lang_items.reserve(n_crates);
879 for &cnum in crates.iter() {
880 info.native_libraries
881 .insert(cnum, tcx.native_libraries(cnum).iter().map(Into::into).collect());
882 info.crate_name.insert(cnum, tcx.crate_name(cnum));
884 let used_crate_source = tcx.used_crate_source(cnum);
885 info.used_crate_source.insert(cnum, used_crate_source.clone());
886 if tcx.is_compiler_builtins(cnum) {
887 info.compiler_builtins = Some(cnum);
889 if tcx.is_profiler_runtime(cnum) {
890 info.profiler_runtime = Some(cnum);
892 if tcx.is_no_builtins(cnum) {
893 info.is_no_builtins.insert(cnum);
895 let missing = tcx.missing_lang_items(cnum);
896 for &item in missing.iter() {
897 if let Ok(id) = lang_items.require(item) {
898 info.lang_item_to_crate.insert(item, id.krate);
902 // No need to look for lang items that don't actually need to exist.
904 missing.iter().cloned().filter(|&l| lang_items::required(tcx, l)).collect();
905 info.missing_lang_items.insert(cnum, missing);
908 let embed_visualizers = tcx.sess.crate_types().iter().any(|&crate_type| match crate_type {
909 CrateType::Executable | CrateType::Dylib | CrateType::Cdylib => {
910 // These are crate types for which we invoke the linker and can embed
911 // NatVis visualizers.
914 CrateType::ProcMacro => {
915 // We could embed NatVis for proc macro crates too (to improve the debugging
916 // experience for them) but it does not seem like a good default, since
917 // this is a rare use case and we don't want to slow down the common case.
920 CrateType::Staticlib | CrateType::Rlib => {
921 // We don't invoke the linker for these, so we don't need to collect the NatVis for them.
926 if tcx.sess.target.is_like_msvc && embed_visualizers {
927 info.natvis_debugger_visualizers =
928 collect_debugger_visualizers_transitive(tcx, DebuggerVisualizerType::Natvis);
935 pub fn provide(providers: &mut Providers) {
936 providers.backend_optimization_level = |tcx, cratenum| {
937 let for_speed = match tcx.sess.opts.optimize {
938 // If globally no optimisation is done, #[optimize] has no effect.
940 // This is done because if we ended up "upgrading" to `-O2` here, we’d populate the
941 // pass manager and it is likely that some module-wide passes (such as inliner or
942 // cross-function constant propagation) would ignore the `optnone` annotation we put
943 // on the functions, thus necessarily involving these functions into optimisations.
944 config::OptLevel::No => return config::OptLevel::No,
945 // If globally optimise-speed is already specified, just use that level.
946 config::OptLevel::Less => return config::OptLevel::Less,
947 config::OptLevel::Default => return config::OptLevel::Default,
948 config::OptLevel::Aggressive => return config::OptLevel::Aggressive,
949 // If globally optimize-for-size has been requested, use -O2 instead (if optimize(size)
951 config::OptLevel::Size => config::OptLevel::Default,
952 config::OptLevel::SizeMin => config::OptLevel::Default,
955 let (defids, _) = tcx.collect_and_partition_mono_items(cratenum);
957 let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
959 attr::OptimizeAttr::None => continue,
960 attr::OptimizeAttr::Size => continue,
961 attr::OptimizeAttr::Speed => {
966 tcx.sess.opts.optimize
970 fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
971 if !tcx.dep_graph.is_fully_enabled() {
975 let work_product_id = &cgu.work_product_id();
976 if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
977 // We don't have anything cached for this CGU. This can happen
978 // if the CGU did not exist in the previous session.
982 // Try to mark the CGU as green. If it we can do so, it means that nothing
983 // affecting the LLVM module has changed and we can re-use a cached version.
984 // If we compile with any kind of LTO, this means we can re-use the bitcode
985 // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
986 // know that later). If we are not doing LTO, there is only one optimized
987 // version of each module, so we re-use that.
988 let dep_node = cgu.codegen_dep_node(tcx);
990 !tcx.dep_graph.dep_node_exists(&dep_node),
991 "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
995 if tcx.try_mark_green(&dep_node) {
996 // We can re-use either the pre- or the post-thinlto state. If no LTO is
997 // being performed then we can use post-LTO artifacts, otherwise we must
998 // reuse pre-LTO artifacts
999 match compute_per_cgu_lto_type(
1002 &tcx.sess.crate_types(),
1003 ModuleKind::Regular,
1005 ComputedLtoType::No => CguReuse::PostLto,
1006 _ => CguReuse::PreLto,