Skip to main content

rustc_codegen_ssa/
base.rs

1use std::cmp;
2use std::collections::BTreeSet;
3use std::sync::Arc;
4use std::time::{Duration, Instant};
5
6use itertools::Itertools;
7use rustc_abi::FIRST_VARIANT;
8use rustc_ast::expand::allocator::{
9    ALLOC_ERROR_HANDLER, ALLOCATOR_METHODS, AllocatorKind, AllocatorMethod, AllocatorMethodInput,
10    AllocatorTy,
11};
12use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
13use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
14use rustc_data_structures::sync::{IntoDynSyncSend, par_map};
15use rustc_data_structures::unord::UnordMap;
16use rustc_hir::attrs::{DebuggerVisualizerType, OptimizeAttr};
17use rustc_hir::def_id::{DefId, LOCAL_CRATE};
18use rustc_hir::lang_items::LangItem;
19use rustc_hir::{ItemId, Target, find_attr};
20use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
21use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
22use rustc_middle::middle::dependency_format::Dependencies;
23use rustc_middle::middle::exported_symbols::{self, SymbolExportKind};
24use rustc_middle::middle::lang_items;
25use rustc_middle::mir::BinOp;
26use rustc_middle::mir::interpret::ErrorHandled;
27use rustc_middle::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem, MonoItemPartitions};
28use rustc_middle::query::Providers;
29use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
30use rustc_middle::ty::{self, Instance, PatternKind, Ty, TyCtxt, Unnormalized};
31use rustc_middle::{bug, span_bug};
32use rustc_session::Session;
33use rustc_session::config::{self, CrateType, EntryFnType};
34use rustc_span::{DUMMY_SP, Symbol};
35use rustc_symbol_mangling::mangle_internal_symbol;
36use rustc_target::spec::{Arch, Os};
37use rustc_trait_selection::infer::{BoundRegionConversionTime, TyCtxtInferExt};
38use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
39use tracing::{debug, info};
40
41use crate::assert_module_sources::CguReuse;
42use crate::back::link::are_upstream_rust_objects_already_included;
43use crate::back::write::{
44    ComputedLtoType, OngoingCodegen, compute_per_cgu_lto_type, start_async_codegen,
45    submit_codegened_module_to_llvm, submit_post_lto_module_to_llvm, submit_pre_lto_module_to_llvm,
46};
47use crate::common::{self, IntPredicate, RealPredicate, TypeKind};
48use crate::meth::load_vtable;
49use crate::mir::operand::OperandValue;
50use crate::mir::place::PlaceRef;
51use crate::traits::*;
52use crate::{
53    CachedModuleCodegen, CodegenLintLevelSpecs, CrateInfo, ModuleCodegen, errors, meth, mir,
54};
55
56pub(crate) fn bin_op_to_icmp_predicate(op: BinOp, signed: bool) -> IntPredicate {
57    match (op, signed) {
58        (BinOp::Eq, _) => IntPredicate::IntEQ,
59        (BinOp::Ne, _) => IntPredicate::IntNE,
60        (BinOp::Lt, true) => IntPredicate::IntSLT,
61        (BinOp::Lt, false) => IntPredicate::IntULT,
62        (BinOp::Le, true) => IntPredicate::IntSLE,
63        (BinOp::Le, false) => IntPredicate::IntULE,
64        (BinOp::Gt, true) => IntPredicate::IntSGT,
65        (BinOp::Gt, false) => IntPredicate::IntUGT,
66        (BinOp::Ge, true) => IntPredicate::IntSGE,
67        (BinOp::Ge, false) => IntPredicate::IntUGE,
68        op => ::rustc_middle::util::bug::bug_fmt(format_args!("bin_op_to_icmp_predicate: expected comparison operator, found {0:?}",
        op))bug!("bin_op_to_icmp_predicate: expected comparison operator, found {:?}", op),
69    }
70}
71
72pub(crate) fn bin_op_to_fcmp_predicate(op: BinOp) -> RealPredicate {
73    match op {
74        BinOp::Eq => RealPredicate::RealOEQ,
75        BinOp::Ne => RealPredicate::RealUNE,
76        BinOp::Lt => RealPredicate::RealOLT,
77        BinOp::Le => RealPredicate::RealOLE,
78        BinOp::Gt => RealPredicate::RealOGT,
79        BinOp::Ge => RealPredicate::RealOGE,
80        op => ::rustc_middle::util::bug::bug_fmt(format_args!("bin_op_to_fcmp_predicate: expected comparison operator, found {0:?}",
        op))bug!("bin_op_to_fcmp_predicate: expected comparison operator, found {:?}", op),
81    }
82}
83
84pub fn compare_simd_types<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
85    bx: &mut Bx,
86    lhs: Bx::Value,
87    rhs: Bx::Value,
88    t: Ty<'tcx>,
89    ret_ty: Bx::Type,
90    op: BinOp,
91) -> Bx::Value {
92    let signed = match t.kind() {
93        ty::Float(_) => {
94            let cmp = bin_op_to_fcmp_predicate(op);
95            let cmp = bx.fcmp(cmp, lhs, rhs);
96            return bx.sext(cmp, ret_ty);
97        }
98        ty::Uint(_) => false,
99        ty::Int(_) => true,
100        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("compare_simd_types: invalid SIMD type"))bug!("compare_simd_types: invalid SIMD type"),
101    };
102
103    let cmp = bin_op_to_icmp_predicate(op, signed);
104    let cmp = bx.icmp(cmp, lhs, rhs);
105    // LLVM outputs an `< size x i1 >`, so we need to perform a sign extension
106    // to get the correctly sized type. This will compile to a single instruction
107    // once the IR is converted to assembly if the SIMD instruction is supported
108    // by the target architecture.
109    bx.sext(cmp, ret_ty)
110}
111
112/// Codegen takes advantage of the additional assumption, where if the
113/// principal trait def id of what's being casted doesn't change,
114/// then we don't need to adjust the vtable at all. This
115/// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
116/// requires that `A = B`; we don't allow *upcasting* objects
117/// between the same trait with different args. If we, for
118/// some reason, were to relax the `Unsize` trait, it could become
119/// unsound, so let's validate here that the trait refs are subtypes.
120pub fn validate_trivial_unsize<'tcx>(
121    tcx: TyCtxt<'tcx>,
122    source_data: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
123    target_data: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
124) -> bool {
125    match (source_data.principal(), target_data.principal()) {
126        (Some(hr_source_principal), Some(hr_target_principal)) => {
127            let (infcx, param_env) =
128                tcx.infer_ctxt().build_with_typing_env(ty::TypingEnv::fully_monomorphized());
129            let universe = infcx.universe();
130            let ocx = ObligationCtxt::new(&infcx);
131            infcx.enter_forall(hr_target_principal, |target_principal| {
132                let source_principal = infcx.instantiate_binder_with_fresh_vars(
133                    DUMMY_SP,
134                    BoundRegionConversionTime::HigherRankedType,
135                    hr_source_principal,
136                );
137                let Ok(()) = ocx.eq(
138                    &ObligationCause::dummy(),
139                    param_env,
140                    target_principal,
141                    source_principal,
142                ) else {
143                    return false;
144                };
145                if !ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
146                    return false;
147                }
148                infcx.leak_check(universe, None).is_ok()
149            })
150        }
151        (_, None) => true,
152        _ => false,
153    }
154}
155
156/// Retrieves the information we are losing (making dynamic) in an unsizing
157/// adjustment.
158///
159/// The `old_info` argument is a bit odd. It is intended for use in an upcast,
160/// where the new vtable for an object will be derived from the old one.
161fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
162    bx: &mut Bx,
163    source: Ty<'tcx>,
164    target: Ty<'tcx>,
165    old_info: Option<Bx::Value>,
166) -> Bx::Value {
167    let cx = bx.cx();
168    let (source, target) =
169        cx.tcx().struct_lockstep_tails_for_codegen(source, target, bx.typing_env());
170    match (source.kind(), target.kind()) {
171        (&ty::Array(_, len), &ty::Slice(_)) => cx.const_usize(
172            len.try_to_target_usize(cx.tcx()).expect("expected monomorphic const in codegen"),
173        ),
174        (&ty::Dynamic(data_a, _), &ty::Dynamic(data_b, _)) => {
175            let old_info =
176                old_info.expect("unsized_info: missing old info for trait upcasting coercion");
177            let b_principal_def_id = data_b.principal_def_id();
178            if data_a.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
179                // Codegen takes advantage of the additional assumption, where if the
180                // principal trait def id of what's being casted doesn't change,
181                // then we don't need to adjust the vtable at all. This
182                // corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
183                // requires that `A = B`; we don't allow *upcasting* objects
184                // between the same trait with different args. If we, for
185                // some reason, were to relax the `Unsize` trait, it could become
186                // unsound, so let's assert here that the trait refs are *equal*.
187                if true {
    if !validate_trivial_unsize(cx.tcx(), data_a, data_b) {
        {
            ::core::panicking::panic_fmt(format_args!("NOP unsize vtable changed principal trait ref: {0} -> {1}",
                    data_a, data_b));
        }
    };
};debug_assert!(
188                    validate_trivial_unsize(cx.tcx(), data_a, data_b),
189                    "NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
190                );
191
192                // A NOP cast that doesn't actually change anything, let's avoid any
193                // unnecessary work. This relies on the assumption that if the principal
194                // traits are equal, then the associated type bounds (`dyn Trait<Assoc=T>`)
195                // are also equal, which is ensured by the fact that normalization is
196                // a function and we do not allow overlapping impls.
197                return old_info;
198            }
199
200            // trait upcasting coercion
201
202            let vptr_entry_idx = cx.tcx().supertrait_vtable_slot((source, target));
203
204            if let Some(entry_idx) = vptr_entry_idx {
205                let ptr_size = bx.data_layout().pointer_size();
206                let vtable_byte_offset = u64::try_from(entry_idx).unwrap() * ptr_size.bytes();
207                load_vtable(bx, old_info, bx.type_ptr(), vtable_byte_offset, source, true)
208            } else {
209                old_info
210            }
211        }
212        (_, ty::Dynamic(data, _)) => meth::get_vtable(
213            cx,
214            source,
215            data.principal()
216                .map(|principal| bx.tcx().instantiate_bound_regions_with_erased(principal)),
217        ),
218        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("unsized_info: invalid unsizing {0:?} -> {1:?}",
        source, target))bug!("unsized_info: invalid unsizing {:?} -> {:?}", source, target),
219    }
220}
221
222/// Coerces `src` to `dst_ty`. `src_ty` must be a pointer.
223pub(crate) fn unsize_ptr<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
224    bx: &mut Bx,
225    src: Bx::Value,
226    src_ty: Ty<'tcx>,
227    dst_ty: Ty<'tcx>,
228    old_info: Option<Bx::Value>,
229) -> (Bx::Value, Bx::Value) {
230    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/base.rs:230",
                        "rustc_codegen_ssa::base", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/base.rs"),
                        ::tracing_core::__macro_support::Option::Some(230u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::base"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("unsize_ptr: {0:?} => {1:?}",
                                                    src_ty, dst_ty) as &dyn Value))])
            });
    } else { ; }
};debug!("unsize_ptr: {:?} => {:?}", src_ty, dst_ty);
231    match (src_ty.kind(), dst_ty.kind()) {
232        (&ty::Pat(a, _), &ty::Pat(b, _)) => unsize_ptr(bx, src, a, b, old_info),
233        (&ty::Ref(_, a, _), &ty::Ref(_, b, _) | &ty::RawPtr(b, _))
234        | (&ty::RawPtr(a, _), &ty::RawPtr(b, _)) => {
235            match (&bx.cx().type_is_sized(a), &old_info.is_none()) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(bx.cx().type_is_sized(a), old_info.is_none());
236            (src, unsized_info(bx, a, b, old_info))
237        }
238        (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
239            match (&def_a, &def_b) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(def_a, def_b); // implies same number of fields
240            let src_layout = bx.cx().layout_of(src_ty);
241            let dst_layout = bx.cx().layout_of(dst_ty);
242            if src_ty == dst_ty {
243                return (src, old_info.unwrap());
244            }
245            let mut result = None;
246            for i in 0..src_layout.fields.count() {
247                let src_f = src_layout.field(bx.cx(), i);
248                if src_f.is_1zst() {
249                    // We are looking for the one non-1-ZST field; this is not it.
250                    continue;
251                }
252
253                match (&src_layout.fields.offset(i).bytes(), &0) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(src_layout.fields.offset(i).bytes(), 0);
254                match (&dst_layout.fields.offset(i).bytes(), &0) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
255                match (&src_layout.size, &src_f.size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(src_layout.size, src_f.size);
256
257                let dst_f = dst_layout.field(bx.cx(), i);
258                match (&src_f.ty, &dst_f.ty) {
    (left_val, right_val) => {
        if *left_val == *right_val {
            let kind = ::core::panicking::AssertKind::Ne;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_ne!(src_f.ty, dst_f.ty);
259                match (&result, &None) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(result, None);
260                result = Some(unsize_ptr(bx, src, src_f.ty, dst_f.ty, old_info));
261            }
262            result.unwrap()
263        }
264        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("unsize_ptr: called on bad types"))bug!("unsize_ptr: called on bad types"),
265    }
266}
267
268/// Coerces `src`, which is a reference to a value of type `src_ty`,
269/// to a value of type `dst_ty`, and stores the result in `dst`.
270pub(crate) fn coerce_unsized_into<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
271    bx: &mut Bx,
272    src: PlaceRef<'tcx, Bx::Value>,
273    dst: PlaceRef<'tcx, Bx::Value>,
274) {
275    let src_ty = src.layout.ty;
276    let dst_ty = dst.layout.ty;
277    match (src_ty.kind(), dst_ty.kind()) {
278        (&ty::Pat(s, sp), &ty::Pat(d, dp))
279            if let (PatternKind::NotNull, PatternKind::NotNull) = (*sp, *dp) =>
280        {
281            let src = src.project_type(bx, s);
282            let dst = dst.project_type(bx, d);
283            coerce_unsized_into(bx, src, dst)
284        }
285        (&ty::Ref(..), &ty::Ref(..) | &ty::RawPtr(..)) | (&ty::RawPtr(..), &ty::RawPtr(..)) => {
286            let (base, info) = match bx.load_operand(src).val {
287                OperandValue::Pair(base, info) => unsize_ptr(bx, base, src_ty, dst_ty, Some(info)),
288                OperandValue::Immediate(base) => unsize_ptr(bx, base, src_ty, dst_ty, None),
289                OperandValue::Ref(..) | OperandValue::ZeroSized => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
290            };
291            OperandValue::Pair(base, info).store(bx, dst);
292        }
293
294        (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
295            match (&def_a, &def_b) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(def_a, def_b); // implies same number of fields
296
297            for i in def_a.variant(FIRST_VARIANT).fields.indices() {
298                let src_f = src.project_field(bx, i.as_usize());
299                let dst_f = dst.project_field(bx, i.as_usize());
300
301                if dst_f.layout.is_zst() {
302                    // No data here, nothing to copy/coerce.
303                    continue;
304                }
305
306                if src_f.layout.ty == dst_f.layout.ty {
307                    bx.typed_place_copy(dst_f.val, src_f.val, src_f.layout);
308                } else {
309                    coerce_unsized_into(bx, src_f, dst_f);
310                }
311            }
312        }
313        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("coerce_unsized_into: invalid coercion {0:?} -> {1:?}",
        src_ty, dst_ty))bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}", src_ty, dst_ty,),
314    }
315}
316
317/// Returns `rhs` sufficiently masked, truncated, and/or extended so that it can be used to shift
318/// `lhs`: it has the same size as `lhs`, and the value, when interpreted unsigned (no matter its
319/// type), will not exceed the size of `lhs`.
320///
321/// Shifts in MIR are all allowed to have mismatched LHS & RHS types, and signed RHS.
322/// The shift methods in `BuilderMethods`, however, are fully homogeneous
323/// (both parameters and the return type are all the same size) and assume an unsigned RHS.
324///
325/// If `is_unchecked` is false, this masks the RHS to ensure it stays in-bounds,
326/// as the `BuilderMethods` shifts are UB for out-of-bounds shift amounts.
327/// For 32- and 64-bit types, this matches the semantics
328/// of Java. (See related discussion on #1877 and #10183.)
329///
330/// If `is_unchecked` is true, this does no masking, and adds sufficient `assume`
331/// calls or operation flags to preserve as much freedom to optimize as possible.
332pub(crate) fn build_shift_expr_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
333    bx: &mut Bx,
334    lhs: Bx::Value,
335    mut rhs: Bx::Value,
336    is_unchecked: bool,
337) -> Bx::Value {
338    // Shifts may have any size int on the rhs
339    let mut rhs_llty = bx.cx().val_ty(rhs);
340    let mut lhs_llty = bx.cx().val_ty(lhs);
341
342    let mask = common::shift_mask_val(bx, lhs_llty, rhs_llty, false);
343    if !is_unchecked {
344        rhs = bx.and(rhs, mask);
345    }
346
347    if bx.cx().type_kind(rhs_llty) == TypeKind::Vector {
348        rhs_llty = bx.cx().element_type(rhs_llty)
349    }
350    if bx.cx().type_kind(lhs_llty) == TypeKind::Vector {
351        lhs_llty = bx.cx().element_type(lhs_llty)
352    }
353    let rhs_sz = bx.cx().int_width(rhs_llty);
354    let lhs_sz = bx.cx().int_width(lhs_llty);
355    if lhs_sz < rhs_sz {
356        if is_unchecked { bx.unchecked_utrunc(rhs, lhs_llty) } else { bx.trunc(rhs, lhs_llty) }
357    } else if lhs_sz > rhs_sz {
358        // We zero-extend even if the RHS is signed. So e.g. `(x: i32) << -1i8` will zero-extend the
359        // RHS to `255i32`. But then we mask the shift amount to be within the size of the LHS
360        // anyway so the result is `31` as it should be. All the extra bits introduced by zext
361        // are masked off so their value does not matter.
362        // FIXME: if we ever support 512bit integers, this will be wrong! For such large integers,
363        // the extra bits introduced by zext are *not* all masked away any more.
364        if !(lhs_sz <= 256) {
    ::core::panicking::panic("assertion failed: lhs_sz <= 256")
};assert!(lhs_sz <= 256);
365        bx.zext(rhs, lhs_llty)
366    } else {
367        rhs
368    }
369}
370
371// Returns `true` if this session's target will use native wasm
372// exceptions. This means that the VM does the unwinding for
373// us
374pub fn wants_wasm_eh(sess: &Session) -> bool {
375    sess.target.is_like_wasm
376        && (sess.target.os != Os::Emscripten || sess.opts.unstable_opts.emscripten_wasm_eh)
377}
378
379/// Returns `true` if this session's target will use SEH-based unwinding.
380///
381/// This is only true for MSVC targets, and even then the 64-bit MSVC target
382/// currently uses SEH-ish unwinding with DWARF info tables to the side (same as
383/// 64-bit MinGW) instead of "full SEH".
384pub fn wants_msvc_seh(sess: &Session) -> bool {
385    sess.target.is_like_msvc
386}
387
388/// Returns `true` if this session's target requires the new exception
389/// handling LLVM IR instructions (catchpad / cleanuppad / ... instead
390/// of landingpad)
391pub(crate) fn wants_new_eh_instructions(sess: &Session) -> bool {
392    wants_wasm_eh(sess) || wants_msvc_seh(sess)
393}
394
395pub(crate) fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
396    cx: &'a Bx::CodegenCx,
397    instance: Instance<'tcx>,
398) {
399    // this is an info! to allow collecting monomorphization statistics
400    // and to allow finding the last function before LLVM aborts from
401    // release builds.
402    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/base.rs:402",
                        "rustc_codegen_ssa::base", ::tracing::Level::INFO,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/base.rs"),
                        ::tracing_core::__macro_support::Option::Some(402u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::base"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("codegen_instance({0})",
                                                    instance) as &dyn Value))])
            });
    } else { ; }
};info!("codegen_instance({})", instance);
403
404    mir::codegen_mir::<Bx>(cx, instance);
405}
406
407pub fn codegen_global_asm<'tcx, Cx>(cx: &mut Cx, item_id: ItemId)
408where
409    Cx: LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>> + AsmCodegenMethods<'tcx>,
410{
411    let item = cx.tcx().hir_item(item_id);
412    if let rustc_hir::ItemKind::GlobalAsm { asm, .. } = item.kind {
413        let operands: Vec<_> = asm
414            .operands
415            .iter()
416            .map(|(op, op_sp)| match *op {
417                rustc_hir::InlineAsmOperand::Const { ref anon_const } => {
418                    match cx.tcx().const_eval_poly(anon_const.def_id.to_def_id()) {
419                        Ok(const_value) => {
420                            let ty =
421                                cx.tcx().typeck_body(anon_const.body).node_type(anon_const.hir_id);
422                            let string = common::asm_const_to_str(
423                                cx.tcx(),
424                                *op_sp,
425                                const_value,
426                                cx.layout_of(ty),
427                            );
428                            GlobalAsmOperandRef::Const { string }
429                        }
430                        Err(ErrorHandled::Reported { .. }) => {
431                            // An error has already been reported and
432                            // compilation is guaranteed to fail if execution
433                            // hits this path. So an empty string instead of
434                            // a stringified constant value will suffice.
435                            GlobalAsmOperandRef::Const { string: String::new() }
436                        }
437                        Err(ErrorHandled::TooGeneric(_)) => {
438                            ::rustc_middle::util::bug::span_bug_fmt(*op_sp,
    format_args!("asm const cannot be resolved; too generic"))span_bug!(*op_sp, "asm const cannot be resolved; too generic")
439                        }
440                    }
441                }
442                rustc_hir::InlineAsmOperand::SymFn { expr } => {
443                    let ty = cx.tcx().typeck(item_id.owner_id).expr_ty(expr);
444                    let instance = match ty.kind() {
445                        &ty::FnDef(def_id, args) => Instance::expect_resolve(
446                            cx.tcx(),
447                            ty::TypingEnv::fully_monomorphized(),
448                            def_id,
449                            args,
450                            expr.span,
451                        ),
452                        _ => ::rustc_middle::util::bug::span_bug_fmt(*op_sp,
    format_args!("asm sym is not a function"))span_bug!(*op_sp, "asm sym is not a function"),
453                    };
454
455                    GlobalAsmOperandRef::SymFn { instance }
456                }
457                rustc_hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
458                    GlobalAsmOperandRef::SymStatic { def_id }
459                }
460                rustc_hir::InlineAsmOperand::In { .. }
461                | rustc_hir::InlineAsmOperand::Out { .. }
462                | rustc_hir::InlineAsmOperand::InOut { .. }
463                | rustc_hir::InlineAsmOperand::SplitInOut { .. }
464                | rustc_hir::InlineAsmOperand::Label { .. } => {
465                    ::rustc_middle::util::bug::span_bug_fmt(*op_sp,
    format_args!("invalid operand type for global_asm!"))span_bug!(*op_sp, "invalid operand type for global_asm!")
466                }
467            })
468            .collect();
469
470        cx.codegen_global_asm(asm.template, &operands, asm.options, asm.line_spans);
471    } else {
472        ::rustc_middle::util::bug::span_bug_fmt(item.span,
    format_args!("Mismatch between hir::Item type and MonoItem type"))span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
473    }
474}
475
476/// Creates the `main` function which will initialize the rust runtime and call
477/// users main function.
478pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
479    cx: &'a Bx::CodegenCx,
480    cgu: &CodegenUnit<'tcx>,
481) -> Option<Bx::Function> {
482    let (main_def_id, entry_type) = cx.tcx().entry_fn(())?;
483    let main_is_local = main_def_id.is_local();
484    let instance = Instance::mono(cx.tcx(), main_def_id);
485
486    if main_is_local {
487        // We want to create the wrapper in the same codegen unit as Rust's main
488        // function.
489        if !cgu.contains_item(&MonoItem::Fn(instance)) {
490            return None;
491        }
492    } else if !cgu.is_primary() {
493        // We want to create the wrapper only when the codegen unit is the primary one
494        return None;
495    }
496
497    let main_llfn = cx.get_fn_addr(instance);
498
499    let entry_fn = create_entry_fn::<Bx>(cx, main_llfn, main_def_id, entry_type);
500    return Some(entry_fn);
501
502    fn create_entry_fn<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
503        cx: &'a Bx::CodegenCx,
504        rust_main: Bx::Value,
505        rust_main_def_id: DefId,
506        entry_type: EntryFnType,
507    ) -> Bx::Function {
508        // The entry function is either `int main(void)` or `int main(int argc, char **argv)`, or
509        // `usize efi_main(void *handle, void *system_table)` depending on the target.
510        let llfty = if cx.sess().target.os == Os::Uefi {
511            cx.type_func(&[cx.type_ptr(), cx.type_ptr()], cx.type_isize())
512        } else if cx.sess().target.main_needs_argc_argv {
513            cx.type_func(&[cx.type_int(), cx.type_ptr()], cx.type_int())
514        } else {
515            cx.type_func(&[], cx.type_int())
516        };
517
518        let main_ret_ty = cx.tcx().fn_sig(rust_main_def_id).no_bound_vars().unwrap().output();
519        // Given that `main()` has no arguments,
520        // then its return type cannot have
521        // late-bound regions, since late-bound
522        // regions must appear in the argument
523        // listing.
524        let main_ret_ty = cx.tcx().normalize_erasing_regions(
525            cx.typing_env(),
526            Unnormalized::new_wip(main_ret_ty.no_bound_vars().unwrap()),
527        );
528
529        let Some(llfn) = cx.declare_c_main(llfty) else {
530            // FIXME: We should be smart and show a better diagnostic here.
531            let span = cx.tcx().def_span(rust_main_def_id);
532            cx.tcx().dcx().emit_fatal(errors::MultipleMainFunctions { span });
533        };
534
535        // `main` should respect same config for frame pointer elimination as rest of code
536        cx.set_frame_pointer_type(llfn);
537        cx.apply_target_cpu_attr(llfn);
538
539        let llbb = Bx::append_block(cx, llfn, "top");
540        let mut bx = Bx::build(cx, llbb);
541
542        bx.insert_reference_to_gdb_debug_scripts_section_global();
543
544        let isize_ty = cx.type_isize();
545        let ptr_ty = cx.type_ptr();
546        let (arg_argc, arg_argv) = get_argc_argv(&mut bx);
547
548        let EntryFnType::Main { sigpipe } = entry_type;
549        let (start_fn, start_ty, args, instance) = {
550            let start_def_id = cx.tcx().require_lang_item(LangItem::Start, DUMMY_SP);
551            let start_instance = ty::Instance::expect_resolve(
552                cx.tcx(),
553                cx.typing_env(),
554                start_def_id,
555                cx.tcx().mk_args(&[main_ret_ty.into()]),
556                DUMMY_SP,
557            );
558            let start_fn = cx.get_fn_addr(start_instance);
559
560            let i8_ty = cx.type_i8();
561            let arg_sigpipe = bx.const_u8(sigpipe);
562
563            let start_ty = cx.type_func(&[cx.val_ty(rust_main), isize_ty, ptr_ty, i8_ty], isize_ty);
564            (
565                start_fn,
566                start_ty,
567                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [rust_main, arg_argc, arg_argv, arg_sigpipe]))vec![rust_main, arg_argc, arg_argv, arg_sigpipe],
568                Some(start_instance),
569            )
570        };
571
572        let result = bx.call(start_ty, None, None, start_fn, &args, None, instance);
573        if cx.sess().target.os == Os::Uefi {
574            bx.ret(result);
575        } else {
576            let cast = bx.intcast(result, cx.type_int(), true);
577            bx.ret(cast);
578        }
579
580        llfn
581    }
582}
583
584/// Obtain the `argc` and `argv` values to pass to the rust start function
585/// (i.e., the "start" lang item).
586fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(bx: &mut Bx) -> (Bx::Value, Bx::Value) {
587    if bx.cx().sess().target.os == Os::Uefi {
588        // Params for UEFI
589        let param_handle = bx.get_param(0);
590        let param_system_table = bx.get_param(1);
591        let ptr_size = bx.tcx().data_layout.pointer_size();
592        let ptr_align = bx.tcx().data_layout.pointer_align().abi;
593        let arg_argc = bx.const_int(bx.cx().type_isize(), 2);
594        let arg_argv = bx.alloca(2 * ptr_size, ptr_align);
595        bx.store(param_handle, arg_argv, ptr_align);
596        let arg_argv_el1 = bx.inbounds_ptradd(arg_argv, bx.const_usize(ptr_size.bytes()));
597        bx.store(param_system_table, arg_argv_el1, ptr_align);
598        (arg_argc, arg_argv)
599    } else if bx.cx().sess().target.main_needs_argc_argv {
600        // Params from native `main()` used as args for rust start function
601        let param_argc = bx.get_param(0);
602        let param_argv = bx.get_param(1);
603        let arg_argc = bx.intcast(param_argc, bx.cx().type_isize(), true);
604        let arg_argv = param_argv;
605        (arg_argc, arg_argv)
606    } else {
607        // The Rust start function doesn't need `argc` and `argv`, so just pass zeros.
608        let arg_argc = bx.const_int(bx.cx().type_int(), 0);
609        let arg_argv = bx.const_null(bx.cx().type_ptr());
610        (arg_argc, arg_argv)
611    }
612}
613
614/// This function returns all of the debugger visualizers specified for the
615/// current crate as well as all upstream crates transitively that match the
616/// `visualizer_type` specified.
617pub fn collect_debugger_visualizers_transitive(
618    tcx: TyCtxt<'_>,
619    visualizer_type: DebuggerVisualizerType,
620) -> BTreeSet<DebuggerVisualizerFile> {
621    tcx.debugger_visualizers(LOCAL_CRATE)
622        .iter()
623        .chain(
624            tcx.crates(())
625                .iter()
626                .filter(|&cnum| {
627                    let used_crate_source = tcx.used_crate_source(*cnum);
628                    used_crate_source.rlib.is_some() || used_crate_source.rmeta.is_some()
629                })
630                .flat_map(|&cnum| tcx.debugger_visualizers(cnum)),
631        )
632        .filter(|visualizer| visualizer.visualizer_type == visualizer_type)
633        .cloned()
634        .collect::<BTreeSet<_>>()
635}
636
637/// Decide allocator kind to codegen. If `Some(_)` this will be the same as
638/// `tcx.allocator_kind`, but it may be `None` in more cases (e.g. if using
639/// allocator definitions from a dylib dependency).
640pub fn allocator_kind_for_codegen(tcx: TyCtxt<'_>) -> Option<AllocatorKind> {
641    // If the crate doesn't have an `allocator_kind` set then there's definitely
642    // no shim to generate. Otherwise we also check our dependency graph for all
643    // our output crate types. If anything there looks like its a `Dynamic`
644    // linkage for all crate types we may link as, then it's already got an
645    // allocator shim and we'll be using that one instead. If nothing exists
646    // then it's our job to generate the allocator! If crate types disagree
647    // about whether an allocator shim is necessary or not, we generate one
648    // and let needs_allocator_shim_for_linking decide at link time whether or
649    // not to use it for any particular linker invocation.
650    let all_crate_types_any_dynamic_crate = tcx.dependency_formats(()).iter().all(|(_, list)| {
651        use rustc_middle::middle::dependency_format::Linkage;
652        list.iter().any(|&linkage| linkage == Linkage::Dynamic)
653    });
654    if all_crate_types_any_dynamic_crate { None } else { tcx.allocator_kind(()) }
655}
656
657/// Decide if this particular crate type needs an allocator shim linked in.
658/// This may return true even when allocator_kind_for_codegen returns false. In
659/// this case no allocator shim shall be linked.
660pub(crate) fn needs_allocator_shim_for_linking(
661    dependency_formats: &Dependencies,
662    crate_type: CrateType,
663) -> bool {
664    use rustc_middle::middle::dependency_format::Linkage;
665    let any_dynamic_crate =
666        dependency_formats[&crate_type].iter().any(|&linkage| linkage == Linkage::Dynamic);
667    !any_dynamic_crate
668}
669
670pub fn allocator_shim_contents(tcx: TyCtxt<'_>, kind: AllocatorKind) -> Vec<AllocatorMethod> {
671    let mut methods = Vec::new();
672
673    if kind == AllocatorKind::Default {
674        methods.extend(ALLOCATOR_METHODS.into_iter().copied());
675    }
676
677    // If the return value of allocator_kind_for_codegen is Some then
678    // alloc_error_handler_kind must also be Some.
679    if tcx.alloc_error_handler_kind(()).unwrap() == AllocatorKind::Default {
680        methods.push(AllocatorMethod {
681            name: ALLOC_ERROR_HANDLER,
682            special: None,
683            inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
684            output: AllocatorTy::Never,
685        });
686    }
687
688    methods
689}
690
691pub fn codegen_crate<B: ExtraBackendMethods>(backend: B, tcx: TyCtxt<'_>) -> OngoingCodegen<B> {
692    if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() {
693        // The target has no default cpu, but none is set explicitly
694        tcx.dcx().emit_fatal(errors::CpuRequired);
695    }
696
697    if let Some(target_cpu) = &tcx.sess.opts.cg.target_cpu
698        && tcx.sess.target.unsupported_cpus.contains(&target_cpu.into())
699    {
700        // The target cpu is explicitly listed as an unsupported cpu
701        tcx.dcx().emit_fatal(errors::CpuUnsupported { target_cpu: target_cpu.clone() });
702    }
703
704    let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
705
706    // Run the monomorphization collector and partition the collected items into
707    // codegen units.
708    let MonoItemPartitions { codegen_units, .. } = tcx.collect_and_partition_mono_items(());
709
710    // Force all codegen_unit queries so they are already either red or green
711    // when compile_codegen_unit accesses them. We are not able to re-execute
712    // the codegen_unit query from just the DepNode, so an unknown color would
713    // lead to having to re-execute compile_codegen_unit, possibly
714    // unnecessarily.
715    if tcx.dep_graph.is_fully_enabled() {
716        for cgu in codegen_units {
717            tcx.ensure_ok().codegen_unit(cgu.name());
718        }
719    }
720
721    // Codegen an allocator shim, if necessary.
722    let allocator_module = if let Some(kind) = allocator_kind_for_codegen(tcx) {
723        let llmod_id =
724            cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("allocator")).to_string();
725
726        tcx.sess.time("write_allocator_module", || {
727            let module =
728                backend.codegen_allocator(tcx, &llmod_id, &allocator_shim_contents(tcx, kind));
729            Some(ModuleCodegen::new_allocator(llmod_id, module))
730        })
731    } else {
732        None
733    };
734
735    let ongoing_codegen = start_async_codegen(backend.clone(), tcx, allocator_module);
736
737    // For better throughput during parallel processing by LLVM, we used to sort
738    // CGUs largest to smallest. This would lead to better thread utilization
739    // by, for example, preventing a large CGU from being processed last and
740    // having only one LLVM thread working while the rest remained idle.
741    //
742    // However, this strategy would lead to high memory usage, as it meant the
743    // LLVM-IR for all of the largest CGUs would be resident in memory at once.
744    //
745    // Instead, we can compromise by ordering CGUs such that the largest and
746    // smallest are first, second largest and smallest are next, etc. If there
747    // are large size variations, this can reduce memory usage significantly.
748    let codegen_units: Vec<_> = {
749        let mut sorted_cgus = codegen_units.iter().collect::<Vec<_>>();
750        sorted_cgus.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
751
752        let (first_half, second_half) = sorted_cgus.split_at(sorted_cgus.len() / 2);
753        first_half.iter().interleave(second_half.iter().rev()).copied().collect()
754    };
755
756    // Calculate the CGU reuse
757    let cgu_reuse = tcx.sess.time("find_cgu_reuse", || {
758        codegen_units.iter().map(|cgu| determine_cgu_reuse(tcx, cgu)).collect::<Vec<_>>()
759    });
760
761    crate::assert_module_sources::assert_module_sources(tcx, &|cgu_reuse_tracker| {
762        for (i, cgu) in codegen_units.iter().enumerate() {
763            let cgu_reuse = cgu_reuse[i];
764            cgu_reuse_tracker.set_actual_reuse(cgu.name().as_str(), cgu_reuse);
765        }
766    });
767
768    let mut total_codegen_time = Duration::new(0, 0);
769    let start_rss = tcx.sess.opts.unstable_opts.time_passes.then(|| get_resident_set_size());
770
771    // The non-parallel compiler can only translate codegen units to LLVM IR
772    // on a single thread, leading to a staircase effect where the N LLVM
773    // threads have to wait on the single codegen threads to generate work
774    // for them. The parallel compiler does not have this restriction, so
775    // we can pre-load the LLVM queue in parallel before handing off
776    // coordination to the OnGoingCodegen scheduler.
777    //
778    // This likely is a temporary measure. Once we don't have to support the
779    // non-parallel compiler anymore, we can compile CGUs end-to-end in
780    // parallel and get rid of the complicated scheduling logic.
781    let mut pre_compiled_cgus = if let Some(threads) = tcx.sess.threads() {
782        tcx.sess.time("compile_first_CGU_batch", || {
783            // Try to find one CGU to compile per thread.
784            let cgus: Vec<_> = cgu_reuse
785                .iter()
786                .enumerate()
787                .filter(|&(_, reuse)| reuse == &CguReuse::No)
788                .take(threads)
789                .collect();
790
791            // Compile the found CGUs in parallel.
792            let start_time = Instant::now();
793
794            let pre_compiled_cgus = par_map(cgus, |(i, _)| {
795                let module = backend.compile_codegen_unit(tcx, codegen_units[i].name());
796                (i, IntoDynSyncSend(module))
797            });
798
799            total_codegen_time += start_time.elapsed();
800
801            pre_compiled_cgus
802        })
803    } else {
804        FxHashMap::default()
805    };
806
807    for (i, cgu) in codegen_units.iter().enumerate() {
808        ongoing_codegen.wait_for_signal_to_codegen_item();
809        ongoing_codegen.check_for_errors(tcx.sess);
810
811        let cgu_reuse = cgu_reuse[i];
812
813        match cgu_reuse {
814            CguReuse::No => {
815                let (module, cost) = if let Some(cgu) = pre_compiled_cgus.remove(&i) {
816                    cgu.0
817                } else {
818                    let start_time = Instant::now();
819                    let module = backend.compile_codegen_unit(tcx, cgu.name());
820                    total_codegen_time += start_time.elapsed();
821                    module
822                };
823                // This will unwind if there are errors, which triggers our `AbortCodegenOnDrop`
824                // guard. Unfortunately, just skipping the `submit_codegened_module_to_llvm` makes
825                // compilation hang on post-monomorphization errors.
826                tcx.dcx().abort_if_errors();
827
828                submit_codegened_module_to_llvm(&ongoing_codegen.coordinator, module, cost);
829            }
830            CguReuse::PreLto => {
831                submit_pre_lto_module_to_llvm(
832                    tcx,
833                    &ongoing_codegen.coordinator,
834                    CachedModuleCodegen {
835                        name: cgu.name().to_string(),
836                        source: cgu.previous_work_product(tcx),
837                    },
838                );
839            }
840            CguReuse::PostLto => {
841                submit_post_lto_module_to_llvm(
842                    &ongoing_codegen.coordinator,
843                    CachedModuleCodegen {
844                        name: cgu.name().to_string(),
845                        source: cgu.previous_work_product(tcx),
846                    },
847                );
848            }
849        }
850    }
851
852    ongoing_codegen.codegen_finished(tcx);
853
854    // Since the main thread is sometimes blocked during codegen, we keep track
855    // -Ztime-passes output manually.
856    if tcx.sess.opts.unstable_opts.time_passes {
857        let end_rss = get_resident_set_size();
858
859        print_time_passes_entry(
860            "codegen_to_LLVM_IR",
861            total_codegen_time,
862            start_rss.unwrap(),
863            end_rss,
864            tcx.sess.opts.unstable_opts.time_passes_format,
865        );
866    }
867
868    ongoing_codegen.check_for_errors(tcx.sess);
869    ongoing_codegen
870}
871
872/// Returns whether a call from the current crate to the [`Instance`] would produce a call
873/// from `compiler_builtins` to a symbol the linker must resolve.
874///
875/// Such calls from `compiler_bultins` are effectively impossible for the linker to handle. Some
876/// linkers will optimize such that dead calls to unresolved symbols are not an error, but this is
877/// not guaranteed. So we used this function in codegen backends to ensure we do not generate any
878/// unlinkable calls.
879///
880/// Note that calls to LLVM intrinsics are uniquely okay because they won't make it to the linker.
881pub fn is_call_from_compiler_builtins_to_upstream_monomorphization<'tcx>(
882    tcx: TyCtxt<'tcx>,
883    instance: Instance<'tcx>,
884) -> bool {
885    fn is_llvm_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
886        if let Some(name) = tcx.codegen_fn_attrs(def_id).symbol_name {
887            name.as_str().starts_with("llvm.")
888        } else {
889            false
890        }
891    }
892
893    let def_id = instance.def_id();
894    !def_id.is_local()
895        && tcx.is_compiler_builtins(LOCAL_CRATE)
896        && !is_llvm_intrinsic(tcx, def_id)
897        && !tcx.should_codegen_locally(instance)
898}
899
900impl CrateInfo {
901    pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> CrateInfo {
902        let crate_types = tcx.crate_types().to_vec();
903        let exported_symbols = crate_types
904            .iter()
905            .map(|&c| (c, crate::back::linker::exported_symbols(tcx, c)))
906            .collect();
907        let linked_symbols =
908            crate_types.iter().map(|&c| (c, crate::back::linker::linked_symbols(tcx, c))).collect();
909        let local_crate_name = tcx.crate_name(LOCAL_CRATE);
910        let windows_subsystem = {
    'done:
        {
        for i in tcx.hir_krate_attrs() {
            #[allow(unused_imports)]
            use rustc_hir::attrs::AttributeKind::*;
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(WindowsSubsystem(kind)) => {
                    break 'done Some(*kind);
                }
                rustc_hir::Attribute::Unparsed(..) =>
                    {}
                    #[deny(unreachable_patterns)]
                    _ => {}
            }
        }
        None
    }
}find_attr!(tcx, crate, WindowsSubsystem(kind) => *kind);
911
912        // This list is used when generating the command line to pass through to
913        // system linker. The linker expects undefined symbols on the left of the
914        // command line to be defined in libraries on the right, not the other way
915        // around. For more info, see some comments in the add_used_library function
916        // below.
917        //
918        // In order to get this left-to-right dependency ordering, we use the reverse
919        // postorder of all crates putting the leaves at the rightmost positions.
920        let mut compiler_builtins = None;
921        let mut used_crates: Vec<_> = tcx
922            .postorder_cnums(())
923            .iter()
924            .rev()
925            .copied()
926            .filter(|&cnum| {
927                let link = !tcx.crate_dep_kind(cnum).macros_only();
928                if link && tcx.is_compiler_builtins(cnum) {
929                    compiler_builtins = Some(cnum);
930                    return false;
931                }
932                link
933            })
934            .collect();
935        // `compiler_builtins` are always placed last to ensure that they're linked correctly.
936        used_crates.extend(compiler_builtins);
937
938        let crates = tcx.crates(());
939        let n_crates = crates.len();
940        let mut info = CrateInfo {
941            target_cpu,
942            target_features: tcx.global_backend_features(()).clone(),
943            crate_types,
944            exported_symbols,
945            linked_symbols,
946            local_crate_name,
947            compiler_builtins,
948            profiler_runtime: None,
949            is_no_builtins: Default::default(),
950            native_libraries: Default::default(),
951            used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
952            crate_name: UnordMap::with_capacity(n_crates),
953            used_crates,
954            used_crate_source: UnordMap::with_capacity(n_crates),
955            dependency_formats: Arc::clone(tcx.dependency_formats(())),
956            windows_subsystem,
957            natvis_debugger_visualizers: Default::default(),
958            lint_level_specs: CodegenLintLevelSpecs::from_tcx(tcx),
959            metadata_symbol: exported_symbols::metadata_symbol_name(tcx),
960            each_linked_rlib_file_for_lto: Default::default(),
961            exported_symbols_for_lto: Default::default(),
962        };
963
964        info.native_libraries.reserve(n_crates);
965
966        for &cnum in crates.iter() {
967            info.native_libraries
968                .insert(cnum, tcx.native_libraries(cnum).iter().map(Into::into).collect());
969            info.crate_name.insert(cnum, tcx.crate_name(cnum));
970
971            let used_crate_source = tcx.used_crate_source(cnum);
972            info.used_crate_source.insert(cnum, Arc::clone(used_crate_source));
973            if tcx.is_profiler_runtime(cnum) {
974                info.profiler_runtime = Some(cnum);
975            }
976            if tcx.is_no_builtins(cnum) {
977                info.is_no_builtins.insert(cnum);
978            }
979        }
980
981        // Handle circular dependencies in the standard library.
982        // See comment before `add_linked_symbol_object` function for the details.
983        // If global LTO is enabled then almost everything (*) is glued into a single object file,
984        // so this logic is not necessary and can cause issues on some targets (due to weak lang
985        // item symbols being "privatized" to that object file), so we disable it.
986        // (*) Native libs, and `#[compiler_builtins]` and `#[no_builtins]` crates are not glued,
987        // and we assume that they cannot define weak lang items. This is not currently enforced
988        // by the compiler, but that's ok because all this stuff is unstable anyway.
989        let target = &tcx.sess.target;
990        if !are_upstream_rust_objects_already_included(tcx.sess) {
991            let add_prefix = match (target.is_like_windows, &target.arch) {
992                (true, Arch::X86) => |name: String, _: SymbolExportKind| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("_{0}", name))
    })format!("_{name}"),
993                (true, Arch::Arm64EC) => {
994                    // Only functions are decorated for arm64ec.
995                    |name: String, export_kind: SymbolExportKind| match export_kind {
996                        SymbolExportKind::Text => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("#{0}", name))
    })format!("#{name}"),
997                        _ => name,
998                    }
999                }
1000                _ => |name: String, _: SymbolExportKind| name,
1001            };
1002            let missing_weak_lang_items: FxIndexSet<(Symbol, SymbolExportKind)> = info
1003                .used_crates
1004                .iter()
1005                .flat_map(|&cnum| tcx.missing_lang_items(cnum))
1006                .filter(|l| l.is_weak())
1007                .filter_map(|&l| {
1008                    let name = l.link_name()?;
1009                    let export_kind = match l.target() {
1010                        Target::Fn => SymbolExportKind::Text,
1011                        Target::Static => SymbolExportKind::Data,
1012                        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("Don\'t know what the export kind is for lang item of kind {0:?}",
        l.target()))bug!(
1013                            "Don't know what the export kind is for lang item of kind {:?}",
1014                            l.target()
1015                        ),
1016                    };
1017                    lang_items::required(tcx, l).then_some((name, export_kind))
1018                })
1019                .collect();
1020
1021            // This loop only adds new items to values of the hash map, so the order in which we
1022            // iterate over the values is not important.
1023            #[allow(rustc::potential_query_instability)]
1024            info.linked_symbols
1025                .iter_mut()
1026                .filter(|(crate_type, _)| {
1027                    !#[allow(non_exhaustive_omitted_patterns)] match crate_type {
    CrateType::Rlib | CrateType::StaticLib => true,
    _ => false,
}matches!(crate_type, CrateType::Rlib | CrateType::StaticLib)
1028                })
1029                .for_each(|(_, linked_symbols)| {
1030                    let mut symbols = missing_weak_lang_items
1031                        .iter()
1032                        .map(|(item, export_kind)| {
1033                            (
1034                                add_prefix(
1035                                    mangle_internal_symbol(tcx, item.as_str()),
1036                                    *export_kind,
1037                                ),
1038                                *export_kind,
1039                            )
1040                        })
1041                        .collect::<Vec<_>>();
1042                    symbols.sort_unstable_by(|a, b| a.0.cmp(&b.0));
1043                    linked_symbols.extend(symbols);
1044                });
1045        }
1046
1047        let mut each_linked_rlib_for_lto = Vec::new();
1048        let mut each_linked_rlib_file_for_lto = Vec::new();
1049        if tcx.sess.lto() != config::Lto::No && tcx.sess.lto() != config::Lto::ThinLocal {
1050            drop(crate::back::link::each_linked_rlib(&info, None, &mut |cnum, path| {
1051                if crate::back::link::ignored_for_lto(tcx.sess, &info, cnum) {
1052                    return;
1053                }
1054
1055                each_linked_rlib_for_lto.push(cnum);
1056                each_linked_rlib_file_for_lto.push(path.to_path_buf());
1057            }));
1058        }
1059        info.each_linked_rlib_file_for_lto = each_linked_rlib_file_for_lto;
1060
1061        // FIXME move to -Zlink-only half such that each_linked_rlib_file_for_lto can be moved there too
1062        // Compute the set of symbols we need to retain when doing LTO (if we need to)
1063        info.exported_symbols_for_lto =
1064            crate::back::lto::exported_symbols_for_lto(tcx, &each_linked_rlib_for_lto);
1065
1066        let embed_visualizers = tcx.crate_types().iter().any(|&crate_type| match crate_type {
1067            CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::Sdylib => {
1068                // These are crate types for which we invoke the linker and can embed
1069                // NatVis visualizers.
1070                true
1071            }
1072            CrateType::ProcMacro => {
1073                // We could embed NatVis for proc macro crates too (to improve the debugging
1074                // experience for them) but it does not seem like a good default, since
1075                // this is a rare use case and we don't want to slow down the common case.
1076                false
1077            }
1078            CrateType::StaticLib | CrateType::Rlib => {
1079                // We don't invoke the linker for these, so we don't need to collect the NatVis for
1080                // them.
1081                false
1082            }
1083        });
1084
1085        if target.is_like_msvc && embed_visualizers {
1086            info.natvis_debugger_visualizers =
1087                collect_debugger_visualizers_transitive(tcx, DebuggerVisualizerType::Natvis);
1088        }
1089
1090        info
1091    }
1092}
1093
1094pub(crate) fn provide(providers: &mut Providers) {
1095    providers.backend_optimization_level = |tcx, cratenum| {
1096        let for_speed = match tcx.sess.opts.optimize {
1097            // If globally no optimisation is done, #[optimize] has no effect.
1098            //
1099            // This is done because if we ended up "upgrading" to `-O2` here, we’d populate the
1100            // pass manager and it is likely that some module-wide passes (such as inliner or
1101            // cross-function constant propagation) would ignore the `optnone` annotation we put
1102            // on the functions, thus necessarily involving these functions into optimisations.
1103            config::OptLevel::No => return config::OptLevel::No,
1104            // If globally optimise-speed is already specified, just use that level.
1105            config::OptLevel::Less => return config::OptLevel::Less,
1106            config::OptLevel::More => return config::OptLevel::More,
1107            config::OptLevel::Aggressive => return config::OptLevel::Aggressive,
1108            // If globally optimize-for-size has been requested, use -O2 instead (if optimize(size)
1109            // are present).
1110            config::OptLevel::Size => config::OptLevel::More,
1111            config::OptLevel::SizeMin => config::OptLevel::More,
1112        };
1113
1114        let defids = tcx.collect_and_partition_mono_items(cratenum).all_mono_items;
1115
1116        let any_for_speed = defids.items().any(|id| {
1117            let CodegenFnAttrs { optimize, .. } = tcx.codegen_fn_attrs(*id);
1118            #[allow(non_exhaustive_omitted_patterns)] match optimize {
    OptimizeAttr::Speed => true,
    _ => false,
}matches!(optimize, OptimizeAttr::Speed)
1119        });
1120
1121        if any_for_speed {
1122            return for_speed;
1123        }
1124
1125        tcx.sess.opts.optimize
1126    };
1127}
1128
1129pub fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguReuse {
1130    if !tcx.dep_graph.is_fully_enabled() {
1131        return CguReuse::No;
1132    }
1133
1134    let work_product_id = &cgu.work_product_id();
1135    if tcx.dep_graph.previous_work_product(work_product_id).is_none() {
1136        // We don't have anything cached for this CGU. This can happen
1137        // if the CGU did not exist in the previous session.
1138        return CguReuse::No;
1139    }
1140
1141    // Try to mark the CGU as green. If it we can do so, it means that nothing
1142    // affecting the LLVM module has changed and we can re-use a cached version.
1143    // If we compile with any kind of LTO, this means we can re-use the bitcode
1144    // of the Pre-LTO stage (possibly also the Post-LTO version but we'll only
1145    // know that later). If we are not doing LTO, there is only one optimized
1146    // version of each module, so we re-use that.
1147    let dep_node = cgu.codegen_dep_node(tcx);
1148    tcx.dep_graph.assert_dep_node_not_yet_allocated_in_current_session(tcx.sess, &dep_node, || {
1149        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("CompileCodegenUnit dep-node for CGU `{0}` already exists before marking.",
                cgu.name()))
    })format!(
1150            "CompileCodegenUnit dep-node for CGU `{}` already exists before marking.",
1151            cgu.name()
1152        )
1153    });
1154
1155    if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() {
1156        // We can re-use either the pre- or the post-thinlto state. If no LTO is
1157        // being performed then we can use post-LTO artifacts, otherwise we must
1158        // reuse pre-LTO artifacts
1159        match compute_per_cgu_lto_type(
1160            &tcx.sess.lto(),
1161            tcx.sess.opts.cg.linker_plugin_lto.enabled(),
1162            tcx.crate_types(),
1163        ) {
1164            ComputedLtoType::No => CguReuse::PostLto,
1165            _ => CguReuse::PreLto,
1166        }
1167    } else {
1168        CguReuse::No
1169    }
1170}