Skip to main content

rustc_codegen_llvm/
context.rs

1use std::borrow::{Borrow, Cow};
2use std::cell::{Cell, RefCell};
3use std::ffi::{CStr, c_uint};
4use std::marker::PhantomData;
5use std::ops::{Deref, DerefMut};
6use std::str;
7
8use rustc_abi::{HasDataLayout, Size, TargetDataLayout, VariantIdx};
9use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
10use rustc_codegen_ssa::diagnostics as ssa_errors;
11use rustc_codegen_ssa::traits::*;
12use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, ToBaseN};
13use rustc_data_structures::fx::FxHashMap;
14use rustc_data_structures::small_c_str::SmallCStr;
15use rustc_hir::def_id::DefId;
16use rustc_middle::mono::CodegenUnit;
17use rustc_middle::ty::layout::{
18    FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
19    codegen_handle_fn_abi_err,
20};
21use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
22use rustc_sanitizers::ignorelist::{SanitizerIgnoreList, typename_for_ignore_list};
23use rustc_session::config::{
24    BranchProtection, CFGuard, CFProtection, DebugInfo, FunctionReturn, PAuthKey, PacRet,
25};
26use rustc_session::{PointerAuthSchema, Session};
27use rustc_span::{DUMMY_SP, Span, Symbol, bug, sym};
28use rustc_structures::CrateType;
29use rustc_target::spec::{
30    Arch, CfgAbi, Env, FramePointer, HasTargetSpec, Os, RelocModel, SmallDataThresholdSupport,
31    Target, TlsModel,
32};
33use smallvec::SmallVec;
34
35use crate::abi::to_llvm_calling_convention;
36use crate::back::llvm_backend::ModuleRefs;
37use crate::back::write::to_llvm_code_model;
38use crate::builder::gpu_offload::{OffloadGlobals, OffloadKernelGlobals};
39use crate::callee::get_fn;
40use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
41use crate::llvm::{self, Metadata, MetadataKindId, Module, Type, Value};
42use crate::{attributes, common, coverageinfo, debuginfo, llvm_util};
43
44/// `TyCtxt` (and related cache datastructures) can't be move between threads.
45/// However, there are various cx related functions which we want to be available to the builder and
46/// other compiler pieces. Here we define a small subset which has enough information and can be
47/// moved around more freely.
48pub(crate) struct SCx<'ll> {
49    pub llmod: llvm::Module<'ll>,
50    pub llcx: llvm::Context<'ll>,
51    pub isize_ty: Type<'ll>,
52}
53
54impl<'ll> Borrow<SCx<'ll>> for FullCx<'ll, '_> {
55    fn borrow(&self) -> &SCx<'ll> {
56        &self.scx
57    }
58}
59
60impl<'ll, 'tcx> Deref for FullCx<'ll, 'tcx> {
61    type Target = SimpleCx<'ll>;
62
63    #[inline]
64    fn deref(&self) -> &Self::Target {
65        &self.scx
66    }
67}
68
69pub(crate) struct GenericCx<'ll, T: Borrow<SCx<'ll>>>(T, PhantomData<SCx<'ll>>);
70
71impl<'ll, T: Borrow<SCx<'ll>>> Deref for GenericCx<'ll, T> {
72    type Target = T;
73
74    #[inline]
75    fn deref(&self) -> &Self::Target {
76        &self.0
77    }
78}
79
80impl<'ll, T: Borrow<SCx<'ll>>> DerefMut for GenericCx<'ll, T> {
81    #[inline]
82    fn deref_mut(&mut self) -> &mut Self::Target {
83        &mut self.0
84    }
85}
86
87pub(crate) type SimpleCx<'ll> = GenericCx<'ll, SCx<'ll>>;
88
89/// There is one `CodegenCx` per codegen unit. Each one has its own LLVM
90/// `llvm::Context` so that several codegen units may be processed in parallel.
91/// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
92pub(crate) type CodegenCx<'ll, 'tcx> = GenericCx<'ll, FullCx<'ll, 'tcx>>;
93
94pub(crate) struct FullCx<'ll, 'tcx> {
95    pub tcx: TyCtxt<'tcx>,
96    pub bitcode_needed: bool,
97    pub scx: SimpleCx<'ll>,
98    pub use_dll_storage_attrs: bool,
99    pub tls_model: llvm::ThreadLocalMode,
100
101    pub codegen_unit: &'tcx CodegenUnit<'tcx>,
102
103    /// Cache instances of monomorphic and polymorphic items
104    pub instances: RefCell<FxHashMap<Instance<'tcx>, Value<'ll>>>,
105    /// Cache instances of intrinsics
106    pub intrinsic_instances: RefCell<FxHashMap<Instance<'tcx>, Value<'ll>>>,
107    /// Cache generated vtables
108    pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), Value<'ll>>>,
109    /// Cache of constant strings,
110    pub const_str_cache: RefCell<FxHashMap<String, Value<'ll>>>,
111
112    /// Cache of emitted const globals (value -> global)
113    pub const_globals: RefCell<FxHashMap<Value<'ll>, Value<'ll>>>,
114
115    /// List of globals for static variables which need to be passed to the
116    /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
117    /// (We have to make sure we don't invalidate any Values referring
118    /// to constants.)
119    pub statics_to_rauw: RefCell<Vec<(Value<'ll>, Value<'ll>)>>,
120
121    /// Statics that will be placed in the llvm.used variable
122    /// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
123    pub used_statics: Vec<Value<'ll>>,
124
125    /// Statics that will be placed in the llvm.compiler.used variable
126    /// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
127    pub compiler_used_statics: RefCell<Vec<Value<'ll>>>,
128
129    /// Mapping of non-scalar types to llvm types.
130    pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), Type<'ll>>>,
131
132    /// Mapping of scalar types to llvm types.
133    pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, Type<'ll>>>,
134
135    /// Extra per-CGU codegen state needed when coverage instrumentation is enabled.
136    pub coverage_cx: Option<coverageinfo::CguCoverageContext<'ll, 'tcx>>,
137    pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
138    pub sanitizer_ignorelist: Option<SanitizerIgnoreList>,
139
140    eh_personality: Cell<Option<Value<'ll>>>,
141    pub rust_try_fn: Cell<Option<(Type<'ll>, Value<'ll>)>>,
142
143    intrinsics:
144        RefCell<FxHashMap<(Cow<'static, str>, SmallVec<[Type<'ll>; 2]>), (Type<'ll>, Value<'ll>)>>,
145
146    /// A counter that is used for generating local symbol names
147    local_gen_sym_counter: Cell<usize>,
148
149    /// A counter that is used for generating global symbol names
150    global_gen_sym_counter: Cell<usize>,
151
152    /// `codegen_static` will sometimes create a second global variable with a
153    /// different type and clear the symbol name of the original global.
154    /// `global_asm!` needs to be able to find this new global so that it can
155    /// compute the correct mangled symbol name to insert into the asm.
156    pub renamed_statics: RefCell<FxHashMap<DefId, Value<'ll>>>,
157
158    /// Cached Objective-C class type
159    pub objc_class_t: Cell<Option<Type<'ll>>>,
160
161    /// Cache of Objective-C class references
162    pub objc_classrefs: RefCell<FxHashMap<Symbol, Value<'ll>>>,
163
164    /// Cache of Objective-C selector references
165    pub objc_selrefs: RefCell<FxHashMap<Symbol, Value<'ll>>>,
166
167    /// Globals shared by the offloading runtime
168    pub offload_globals: RefCell<Option<OffloadGlobals<'ll>>>,
169
170    /// Cache of kernel-specific globals
171    pub offload_kernel_cache: RefCell<FxHashMap<String, OffloadKernelGlobals<'ll>>>,
172}
173
174fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
175    match tls_model {
176        TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
177        TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
178        TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
179        TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
180        TlsModel::Emulated => llvm::ThreadLocalMode::GeneralDynamic,
181    }
182}
183
184pub(crate) unsafe fn create_module<'ll>(
185    tcx: TyCtxt<'_>,
186    llcx: llvm::Context<'ll>,
187    mod_name: &str,
188) -> llvm::Module<'ll> {
189    let mod_name_cstr = SmallCStr::new(mod_name);
190    let llmod = unsafe { llvm::LLVMModuleCreateWithNameInContext(mod_name_cstr.as_ptr(), llcx) };
191    init_module(tcx, llcx, llmod);
192    llmod
193}
194
195fn init_module<'ll, 'tcx>(tcx: TyCtxt<'tcx>, llcx: llvm::Context<'ll>, llmod: llvm::Module<'ll>) {
196    let sess = tcx.sess;
197    let cx = SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size());
198
199    let mut target_data_layout = sess.target.data_layout.to_string();
200    let llvm_version = llvm_util::get_version();
201
202    if llvm_version < (23, 0, 0) {
203        if sess.target.arch == Arch::S390x {
204            // LLVM 23 updated the s390x layout to specify the stack alignment: https://github.com/llvm/llvm-project/pull/176041
205            target_data_layout = target_data_layout.replace("-S64", "");
206        }
207    }
208    if llvm_version < (24, 0, 0) {
209        if sess.target.arch == Arch::AmdGpu {
210            // LLVM 24 updated the amdgpu layout: http://github.com/llvm/llvm-project/pull/209746 Add BARRIER address space
211            target_data_layout = target_data_layout
212                .replace("-p10:32:32-p11:32:32-p12:32:32-p13:32:32-p14:32:32-p15:32:32", "");
213        }
214    }
215    // Ensure the data-layout values hardcoded remain the defaults.
216    {
217        let tm = crate::back::write::create_informational_target_machine(sess);
218        unsafe {
219            llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());
220        }
221
222        let llvm_data_layout = unsafe { llvm::LLVMGetDataLayoutStr(llmod) };
223        let llvm_data_layout =
224            str::from_utf8(unsafe { CStr::from_ptr(llvm_data_layout) }.to_bytes())
225                .expect("got a non-UTF8 data-layout from LLVM");
226
227        if target_data_layout != llvm_data_layout {
228            tcx.dcx().emit_err(crate::diagnostics::MismatchedDataLayout {
229                rustc_target: sess.opts.target_triple.to_string().as_str(),
230                rustc_layout: target_data_layout.as_str(),
231                llvm_target: sess.target.llvm_target.borrow(),
232                llvm_layout: llvm_data_layout,
233            });
234        }
235    }
236
237    let data_layout = SmallCStr::new(&target_data_layout);
238    unsafe {
239        llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
240    }
241
242    let llvm_target = SmallCStr::new(&llvm_util::llvm_target(sess));
243    unsafe {
244        llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
245    }
246
247    let reloc_model = sess.relocation_model();
248    if #[allow(non_exhaustive_omitted_patterns)] match reloc_model {
    RelocModel::Pic | RelocModel::Pie => true,
    _ => false,
}matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
249        unsafe {
250            llvm::LLVMRustSetModulePICLevel(llmod);
251        }
252        // PIE is potentially more effective than PIC, but can only be used in executables.
253        // If all our outputs are executables, then we can relax PIC to PIE.
254        if reloc_model == RelocModel::Pie
255            || tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable)
256        {
257            unsafe {
258                llvm::LLVMRustSetModulePIELevel(llmod);
259            }
260        }
261    }
262
263    // Linking object files with different code models is undefined behavior
264    // because the compiler would have to generate additional code (to span
265    // longer jumps) if a larger code model is used with a smaller one.
266    //
267    // See https://reviews.llvm.org/D52322 and https://reviews.llvm.org/D52323.
268    unsafe {
269        llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
270    }
271
272    if let Some(large_data_threshold) = sess.opts.unstable_opts.large_data_threshold {
273        unsafe {
274            llvm::LLVMRustSetModuleLargeDataThreshold(llmod, large_data_threshold);
275        }
276    }
277
278    // If skipping the PLT is enabled, we need to add some module metadata
279    // to ensure intrinsic calls don't use it.
280    if !sess.needs_plt() {
281        llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "RtLibUseGOT", 1);
282    }
283
284    // Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
285    if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
286        llvm::add_module_flag_u32(
287            llmod,
288            llvm::ModuleFlagMergeBehavior::Override,
289            "CFI Canonical Jump Tables",
290            1,
291        );
292    }
293
294    // If we're normalizing integers with CFI, ensure LLVM generated functions do the same.
295    // See https://github.com/llvm/llvm-project/pull/104826
296    if sess.is_sanitizer_cfi_normalize_integers_enabled() {
297        llvm::add_module_flag_u32(
298            llmod,
299            llvm::ModuleFlagMergeBehavior::Override,
300            "cfi-normalize-integers",
301            1,
302        );
303    }
304
305    // Enable LTO unit splitting if specified or if CFI is enabled. (See
306    // https://reviews.llvm.org/D53891.)
307    if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
308        llvm::add_module_flag_u32(
309            llmod,
310            llvm::ModuleFlagMergeBehavior::Override,
311            "EnableSplitLTOUnit",
312            1,
313        );
314    }
315
316    if sess.must_emit_unwind_tables() {
317        // This assertion checks that Max is the correct merge behavior.
318        // Async unwind tables are strictly more useful than sync uwtables.
319        const {
320            if !((llvm::UWTableKind::None as u32) < (llvm::UWTableKind::Sync as u32)) {
    ::core::panicking::panic("assertion failed: (llvm::UWTableKind::None as u32) < (llvm::UWTableKind::Sync as u32)")
};assert!((llvm::UWTableKind::None as u32) < (llvm::UWTableKind::Sync as u32));
321            if !((llvm::UWTableKind::Sync as u32) < (llvm::UWTableKind::Async as u32)) {
    ::core::panicking::panic("assertion failed: (llvm::UWTableKind::Sync as u32) < (llvm::UWTableKind::Async as u32)")
};assert!((llvm::UWTableKind::Sync as u32) < (llvm::UWTableKind::Async as u32));
322        }
323
324        llvm::add_module_flag_u32(
325            llmod,
326            llvm::ModuleFlagMergeBehavior::Max,
327            "uwtable",
328            match sess.opts.unstable_opts.use_sync_unwind {
329                Some(true) => llvm::UWTableKind::Sync as u32,
330                Some(false) | None => llvm::UWTableKind::Async as u32,
331            },
332        );
333    }
334
335    // Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
336    if sess.is_sanitizer_kcfi_enabled() {
337        llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);
338
339        // Add "kcfi-offset" module flag with -Z patchable-function-entry (See
340        // https://reviews.llvm.org/D141172).
341        let patchable_prefix_nops = sess.opts.unstable_opts.patchable_function_entry.prefix();
342        if patchable_prefix_nops > 0 {
343            llvm::add_module_flag_u32(
344                llmod,
345                llvm::ModuleFlagMergeBehavior::Override,
346                "kcfi-offset",
347                patchable_prefix_nops.into(),
348            );
349        }
350
351        // Add "kcfi-arity" module flag if KCFI arity indicator is enabled. (See
352        // https://github.com/llvm/llvm-project/pull/117121.)
353        if sess.is_sanitizer_kcfi_arity_enabled() {
354            llvm::add_module_flag_u32(
355                llmod,
356                llvm::ModuleFlagMergeBehavior::Override,
357                "kcfi-arity",
358                1,
359            );
360        }
361    }
362
363    // Control Flow Guard is currently only supported by MSVC and LLVM on Windows.
364    if sess.target.is_like_msvc
365        || (sess.target.options.os == Os::Windows
366            && sess.target.options.env == Env::Gnu
367            && sess.target.options.cfg_abi == CfgAbi::Llvm)
368    {
369        match sess.opts.cg.control_flow_guard {
370            CFGuard::Disabled => {}
371            CFGuard::NoChecks => {
372                // Set `cfguard=1` module flag to emit metadata only.
373                llvm::add_module_flag_u32(
374                    llmod,
375                    llvm::ModuleFlagMergeBehavior::Warning,
376                    "cfguard",
377                    1,
378                );
379            }
380            CFGuard::Checks => {
381                // Set `cfguard=2` module flag to emit metadata and checks.
382                llvm::add_module_flag_u32(
383                    llmod,
384                    llvm::ModuleFlagMergeBehavior::Warning,
385                    "cfguard",
386                    2,
387                );
388            }
389        }
390    }
391
392    if let Some(regparm_count) = sess.opts.unstable_opts.regparm {
393        llvm::add_module_flag_u32(
394            llmod,
395            llvm::ModuleFlagMergeBehavior::Error,
396            "NumRegisterParameters",
397            regparm_count,
398        );
399    }
400
401    if let Some(BranchProtection { bti, pac_ret, gcs }) = sess.branch_protection() {
402        if sess.target.arch == Arch::AArch64 {
403            llvm::add_module_flag_u32(
404                llmod,
405                llvm::ModuleFlagMergeBehavior::Min,
406                "branch-target-enforcement",
407                bti.into(),
408            );
409            llvm::add_module_flag_u32(
410                llmod,
411                llvm::ModuleFlagMergeBehavior::Min,
412                "sign-return-address",
413                pac_ret.is_some().into(),
414            );
415            let pac_opts = pac_ret.unwrap_or_else(|| {
416                // Windows on Arm only supports PAC key B.
417                let key = if sess.target.os == Os::Windows { PAuthKey::B } else { PAuthKey::A };
418                PacRet { leaf: false, pc: false, key }
419            });
420            llvm::add_module_flag_u32(
421                llmod,
422                llvm::ModuleFlagMergeBehavior::Min,
423                "branch-protection-pauth-lr",
424                pac_opts.pc.into(),
425            );
426            llvm::add_module_flag_u32(
427                llmod,
428                llvm::ModuleFlagMergeBehavior::Min,
429                "sign-return-address-all",
430                pac_opts.leaf.into(),
431            );
432            llvm::add_module_flag_u32(
433                llmod,
434                llvm::ModuleFlagMergeBehavior::Min,
435                "sign-return-address-with-bkey",
436                u32::from(pac_opts.key == PAuthKey::B),
437            );
438            llvm::add_module_flag_u32(
439                llmod,
440                llvm::ModuleFlagMergeBehavior::Min,
441                "guarded-control-stack",
442                gcs.into(),
443            );
444        } else {
445            ::rustc_span::macros::bug_impl(None,
    format_args!("branch-protection used on non-AArch64 target; this should be checked in rustc_session."),
    Location::caller());bug!(
446                "branch-protection used on non-AArch64 target; \
447                  this should be checked in rustc_session."
448            );
449        }
450    }
451
452    // Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang).
453    if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
454        llvm::add_module_flag_u32(
455            llmod,
456            llvm::ModuleFlagMergeBehavior::Override,
457            "cf-protection-branch",
458            1,
459        );
460    }
461    if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
462        llvm::add_module_flag_u32(
463            llmod,
464            llvm::ModuleFlagMergeBehavior::Override,
465            "cf-protection-return",
466            1,
467        );
468    }
469
470    if sess.opts.unstable_opts.virtual_function_elimination {
471        llvm::add_module_flag_u32(
472            llmod,
473            llvm::ModuleFlagMergeBehavior::Error,
474            "Virtual Function Elim",
475            1,
476        );
477    }
478
479    // Set module flag to enable Windows EHCont Guard (/guard:ehcont).
480    if sess.opts.unstable_opts.ehcont_guard {
481        llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "ehcontguard", 1);
482    }
483
484    match sess.opts.unstable_opts.function_return {
485        FunctionReturn::Keep => {}
486        FunctionReturn::ThunkExtern => {
487            llvm::add_module_flag_u32(
488                llmod,
489                llvm::ModuleFlagMergeBehavior::Override,
490                "function_return_thunk_extern",
491                1,
492            );
493        }
494    }
495
496    let fp = attributes::frame_pointer(sess);
497    if fp != FramePointer::MayOmit {
498        llvm::add_module_flag_u32(
499            llmod,
500            llvm::ModuleFlagMergeBehavior::Max,
501            "frame-pointer",
502            match fp {
503                FramePointer::Always => llvm::FramePointerKind::All as u32,
504                FramePointer::NonLeaf => llvm::FramePointerKind::NonLeaf as u32,
505                FramePointer::MayOmit => llvm::FramePointerKind::None as u32,
506            },
507        );
508    }
509
510    if sess.opts.unstable_opts.indirect_branch_cs_prefix {
511        llvm::add_module_flag_u32(
512            llmod,
513            llvm::ModuleFlagMergeBehavior::Override,
514            "indirect_branch_cs_prefix",
515            1,
516        );
517    }
518
519    // Set up the small-data optimization limit for architectures that use
520    // an LLVM module flag to control this.
521    if let Some(threshold) = sess.opts.unstable_opts.small_data_threshold
522        && let SmallDataThresholdSupport::LlvmModuleFlag(flag) =
523            sess.target.small_data_threshold_support()
524    {
525        llvm::add_module_flag_u32(
526            llmod,
527            llvm::ModuleFlagMergeBehavior::Error,
528            &flag,
529            threshold as u32,
530        );
531    }
532
533    // Insert `llvm.ident` metadata.
534    //
535    // On the wasm targets it will get hooked up to the "producer" sections
536    // `processed-by` information.
537    #[allow(clippy::option_env_unwrap)]
538    let rustc_producer =
539        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("rustc version {0}",
                ::core::option::Option::Some("1.102.0-nightly (5f4effd57 2026-11-13)").expect("CFG_VERSION")))
    })format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
540
541    let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
542    cx.module_add_named_metadata_node(llmod, c"llvm.ident", &[name_metadata]);
543
544    // Emit the target-abi module flag for LoongArch and RISC-V to pass the ABI to LTO.
545    let llvm_abiname = &sess.target.options.llvm_abiname;
546    if #[allow(non_exhaustive_omitted_patterns)] match sess.target.arch {
    Arch::LoongArch32 | Arch::LoongArch64 | Arch::RiscV32 | Arch::RiscV64 =>
        true,
    _ => false,
}matches!(
547        sess.target.arch,
548        Arch::LoongArch32 | Arch::LoongArch64 | Arch::RiscV32 | Arch::RiscV64
549    ) {
550        llvm::add_module_flag_str(
551            llmod,
552            llvm::ModuleFlagMergeBehavior::Error,
553            "target-abi",
554            llvm_abiname.desc(),
555        );
556    }
557
558    if llvm_version >= (24, 0, 0)
559        && let Some(floatabi) = sess.target.llvm_floatabi
560    {
561        llvm::add_module_flag_str(
562            llmod,
563            llvm::ModuleFlagMergeBehavior::Error,
564            "float-abi",
565            floatabi.desc(),
566        );
567    }
568
569    if llvm_version >= (24, 0, 0) {
570        if sess.target.singlethread(&sess.internal_target_features) {
571            llvm::add_module_flag_str(
572                llmod,
573                llvm::ModuleFlagMergeBehavior::Error,
574                "thread-model",
575                "single",
576            );
577        }
578        if wants_wasm_eh(&sess.target) {
579            llvm::add_module_flag_str(
580                llmod,
581                llvm::ModuleFlagMergeBehavior::Error,
582                "exception-model",
583                "wasm",
584            );
585        }
586    }
587
588    // Add module flags specified via -Z llvm_module_flag
589    for (key, value, merge_behavior) in &sess.opts.unstable_opts.llvm_module_flag {
590        let merge_behavior = match merge_behavior.as_str() {
591            "error" => llvm::ModuleFlagMergeBehavior::Error,
592            "warning" => llvm::ModuleFlagMergeBehavior::Warning,
593            "require" => llvm::ModuleFlagMergeBehavior::Require,
594            "override" => llvm::ModuleFlagMergeBehavior::Override,
595            "append" => llvm::ModuleFlagMergeBehavior::Append,
596            "appendunique" => llvm::ModuleFlagMergeBehavior::AppendUnique,
597            "max" => llvm::ModuleFlagMergeBehavior::Max,
598            "min" => llvm::ModuleFlagMergeBehavior::Min,
599            // We already checked this during option parsing
600            _ => ::core::panicking::unreachable_default_msg()unreachable!(),
601        };
602        llvm::add_module_flag_u32(llmod, merge_behavior, key, *value);
603    }
604}
605
606impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
607    pub(crate) fn new(
608        tcx: TyCtxt<'tcx>,
609        codegen_unit: &'tcx CodegenUnit<'tcx>,
610        module_refs: ModuleRefs<'ll>,
611        bitcode_needed: bool,
612    ) -> Self {
613        // An interesting part of Windows which MSVC forces our hand on (and
614        // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
615        // attributes in LLVM IR as well as native dependencies (in C these
616        // correspond to `__declspec(dllimport)`).
617        //
618        // LD (BFD) in MinGW mode can often correctly guess `dllexport` but
619        // relying on that can result in issues like #50176.
620        // LLD won't support that and expects symbols with proper attributes.
621        // Because of that we make MinGW target emit dllexport just like MSVC.
622        // When it comes to dllimport we use it for constants but for functions
623        // rely on the linker to do the right thing. Opposed to dllexport this
624        // task is easy for them (both LD and LLD) and allows us to easily use
625        // symbols from static libraries in shared libraries.
626        //
627        // Whenever a dynamic library is built on Windows it must have its public
628        // interface specified by functions tagged with `dllexport` or otherwise
629        // they're not available to be linked against. This poses a few problems
630        // for the compiler, some of which are somewhat fundamental, but we use
631        // the `use_dll_storage_attrs` variable below to attach the `dllexport`
632        // attribute to all LLVM functions that are exported e.g., they're
633        // already tagged with external linkage). This is suboptimal for a few
634        // reasons:
635        //
636        // * If an object file will never be included in a dynamic library,
637        //   there's no need to attach the dllexport attribute. Most object
638        //   files in Rust are not destined to become part of a dll as binaries
639        //   are statically linked by default.
640        // * If the compiler is emitting both an rlib and a dylib, the same
641        //   source object file is currently used but with MSVC this may be less
642        //   feasible. The compiler may be able to get around this, but it may
643        //   involve some invasive changes to deal with this.
644        //
645        // The flip side of this situation is that whenever you link to a dll and
646        // you import a function from it, the import should be tagged with
647        // `dllimport`. At this time, however, the compiler does not emit
648        // `dllimport` for any declarations other than constants (where it is
649        // required), which is again suboptimal for even more reasons!
650        //
651        // * Calling a function imported from another dll without using
652        //   `dllimport` causes the linker/compiler to have extra overhead (one
653        //   `jmp` instruction on x86) when calling the function.
654        // * The same object file may be used in different circumstances, so a
655        //   function may be imported from a dll if the object is linked into a
656        //   dll, but it may be just linked against if linked into an rlib.
657        // * The compiler has no knowledge about whether native functions should
658        //   be tagged dllimport or not.
659        //
660        // For now the compiler takes the perf hit (I do not have any numbers to
661        // this effect) by marking very little as `dllimport` and praying the
662        // linker will take care of everything. Fixing this problem will likely
663        // require adding a few attributes to Rust itself (feature gated at the
664        // start) and then strongly recommending static linkage on Windows!
665        let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
666
667        let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
668
669        let ModuleRefs { llcx, llmod, .. } = module_refs;
670
671        let coverage_cx =
672            tcx.sess.instrument_coverage().then(coverageinfo::CguCoverageContext::new);
673
674        let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
675            let dctx = debuginfo::CodegenUnitDebugContext::new(llmod, tcx.sess);
676            debuginfo::metadata::build_compile_unit_di_node(
677                tcx,
678                codegen_unit.name().as_str(),
679                &dctx,
680            );
681            Some(dctx)
682        } else {
683            None
684        };
685
686        // FIXME: This parses the ignorelist files for each CGU, which adds a performance overhead.
687        // Clang parses it once per frontend invocation. LLVM's `SpecialCaseList::inSection`
688        // mutates an internal `LazyInit` cache and is not thread-safe. We either need to wrap
689        // the queries in a lock or wait for LLVM to expose a thread-safe way to query it.
690        let sanitizer_ignorelist = if !tcx.sess.opts.unstable_opts.sanitizer_ignorelist.is_empty() {
691            for path in &tcx.sess.opts.unstable_opts.sanitizer_ignorelist {
692                let _ = tcx.sess.source_map().load_file(std::path::Path::new(path));
693            }
694            match SanitizerIgnoreList::new(&tcx.sess.opts.unstable_opts.sanitizer_ignorelist) {
695                Ok(list) => Some(list),
696                Err(err) => {
697                    tcx.dcx().fatal(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("failed to parse sanitizer ignorelist: {0}",
                err))
    })format!("failed to parse sanitizer ignorelist: {}", err));
698                }
699            }
700        } else {
701            None
702        };
703
704        GenericCx(
705            FullCx {
706                tcx,
707                bitcode_needed,
708                scx: SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size()),
709                use_dll_storage_attrs,
710                tls_model,
711                codegen_unit,
712                instances: Default::default(),
713                intrinsic_instances: Default::default(),
714                vtables: Default::default(),
715                const_str_cache: Default::default(),
716                const_globals: Default::default(),
717                statics_to_rauw: RefCell::new(Vec::new()),
718                used_statics: Vec::new(),
719                compiler_used_statics: Default::default(),
720                type_lowering: Default::default(),
721                scalar_lltypes: Default::default(),
722                coverage_cx,
723                dbg_cx,
724                sanitizer_ignorelist,
725                eh_personality: Cell::new(None),
726                rust_try_fn: Cell::new(None),
727                intrinsics: Default::default(),
728                local_gen_sym_counter: Cell::new(0),
729                global_gen_sym_counter: Cell::new(0),
730                renamed_statics: Default::default(),
731                objc_class_t: Cell::new(None),
732                objc_classrefs: Default::default(),
733                objc_selrefs: Default::default(),
734                offload_globals: Default::default(),
735                offload_kernel_cache: Default::default(),
736            },
737            PhantomData,
738        )
739    }
740
741    pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(Value<'ll>, Value<'ll>)>> {
742        &self.statics_to_rauw
743    }
744
745    /// Extra state that is only available when coverage instrumentation is enabled.
746    #[inline]
747    #[track_caller]
748    pub(crate) fn coverage_cx(&self) -> &coverageinfo::CguCoverageContext<'ll, 'tcx> {
749        self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
750    }
751
752    pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[Value<'ll>]) {
753        let array = self.const_array(self.type_ptr(), values);
754
755        let g = llvm::add_global(self.llmod, self.val_ty(array), name);
756        llvm::set_initializer(g, array);
757        llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
758        llvm::set_section(g, c"llvm.metadata");
759    }
760
761    /// The Objective-C ABI that is used.
762    ///
763    /// This corresponds to the `-fobjc-abi-version=` flag in Clang / GCC.
764    pub(crate) fn objc_abi_version(&self) -> u32 {
765        if !self.tcx.sess.target.is_like_darwin {
    ::core::panicking::panic("assertion failed: self.tcx.sess.target.is_like_darwin")
};assert!(self.tcx.sess.target.is_like_darwin);
766        if self.tcx.sess.target.arch == Arch::X86 && self.tcx.sess.target.os == Os::MacOs {
767            // 32-bit x86 macOS uses ABI version 1 (a.k.a. the "fragile ABI").
768            1
769        } else {
770            // All other Darwin-like targets we support use ABI version 2
771            // (a.k.a the "non-fragile ABI").
772            2
773        }
774    }
775
776    pub(crate) fn add_ptrauth_elf_got_flag(&self) {
777        llvm::add_module_flag_u32(
778            self.llmod,
779            llvm::ModuleFlagMergeBehavior::Error,
780            "ptrauth-elf-got",
781            1,
782        );
783    }
784
785    pub(crate) fn add_ptrauth_sign_personality_flag(&self) {
786        llvm::add_module_flag_u32(
787            self.llmod,
788            llvm::ModuleFlagMergeBehavior::Error,
789            "ptrauth-sign-personality",
790            1,
791        );
792    }
793
794    pub(crate) fn add_ptrauth_pauthabi_version_and_platform_flags(
795        &self,
796        aarch64_elf_pauthabi_version: u32,
797    ) {
798        // NOTE: This must correspond to llvm's AARCH64_PAUTH_PLATFORM_LLVM_LINUX, as defined in
799        // <llvm_root>/llvm/include/llvm/BinaryFormat/ELF.h.
800        // FIXME (jchlanda) extend possible values once we start supporting other platforms (for
801        // example: AARCH64_PAUTH_PLATFORM_BAREMETAL = 0x1);
802        const AARCH64_PAUTH_PLATFORM_LLVM_LINUX: u32 = 0x10000002;
803        llvm::add_module_flag_u32(
804            self.llmod,
805            llvm::ModuleFlagMergeBehavior::Error,
806            "aarch64-elf-pauthabi-platform",
807            AARCH64_PAUTH_PLATFORM_LLVM_LINUX,
808        );
809        llvm::add_module_flag_u32(
810            self.llmod,
811            llvm::ModuleFlagMergeBehavior::Error,
812            "aarch64-elf-pauthabi-version",
813            aarch64_elf_pauthabi_version,
814        );
815    }
816
817    // We do our best here to match what Clang does when compiling Objective-C natively.
818    // See Clang's `CGObjCCommonMac::EmitImageInfo`:
819    // https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/clang/lib/CodeGen/CGObjCMac.cpp#L5085
820    pub(crate) fn add_objc_module_flags(&self) {
821        let abi_version = self.objc_abi_version();
822
823        llvm::add_module_flag_u32(
824            self.llmod,
825            llvm::ModuleFlagMergeBehavior::Error,
826            "Objective-C Version",
827            abi_version,
828        );
829
830        llvm::add_module_flag_u32(
831            self.llmod,
832            llvm::ModuleFlagMergeBehavior::Error,
833            "Objective-C Image Info Version",
834            0,
835        );
836
837        llvm::add_module_flag_str(
838            self.llmod,
839            llvm::ModuleFlagMergeBehavior::Error,
840            "Objective-C Image Info Section",
841            match abi_version {
842                1 => "__OBJC,__image_info,regular",
843                2 => "__DATA,__objc_imageinfo,regular,no_dead_strip",
844                _ => ::core::panicking::unreachable_default_msg()unreachable!(),
845            },
846        );
847
848        if self.tcx.sess.target.env == Env::Sim {
849            llvm::add_module_flag_u32(
850                self.llmod,
851                llvm::ModuleFlagMergeBehavior::Error,
852                "Objective-C Is Simulated",
853                1 << 5,
854            );
855        }
856
857        llvm::add_module_flag_u32(
858            self.llmod,
859            llvm::ModuleFlagMergeBehavior::Error,
860            "Objective-C Class Properties",
861            1 << 6,
862        );
863    }
864
865    pub(crate) fn is_sanitizer_type_ignored(
866        &self,
867        sanitizer: &std::ffi::CStr,
868        fn_abi: &rustc_target::callconv::FnAbi<'tcx, Ty<'tcx>>,
869    ) -> bool {
870        self.sanitizer_ignorelist.as_ref().is_some_and(|ignorelist| {
871            let type_name = typename_for_ignore_list(self.tcx, fn_abi);
872            ignorelist.contains_prefix(sanitizer, c"type", &type_name)
873        })
874    }
875}
876impl<'ll> SimpleCx<'ll> {
877    pub(crate) fn get_type_of_global(&self, val: Value<'ll>) -> Type<'ll> {
878        unsafe { llvm::LLVMGlobalGetValueType(val) }
879    }
880    pub(crate) fn val_ty(&self, v: Value<'ll>) -> Type<'ll> {
881        common::val_ty(v)
882    }
883}
884impl<'ll> SimpleCx<'ll> {
885    pub(crate) fn new(
886        llmod: llvm::Module<'ll>,
887        llcx: llvm::Context<'ll>,
888        pointer_size: Size,
889    ) -> Self {
890        let isize_ty = llvm::LLVMIntTypeInContext(llcx, pointer_size.bits() as c_uint);
891        Self(SCx { llmod, llcx, isize_ty }, PhantomData)
892    }
893}
894
895impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
896    pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> Value<'ll> {
897        llvm::LLVMMetadataAsValue(self.llcx(), metadata)
898    }
899
900    pub(crate) fn get_const_int(&self, ty: Type<'ll>, val: u64) -> Value<'ll> {
901        unsafe { llvm::LLVMConstInt(ty, val, llvm::FALSE) }
902    }
903
904    pub(crate) fn get_const_i64(&self, n: u64) -> Value<'ll> {
905        self.get_const_int(self.type_i64(), n)
906    }
907
908    pub(crate) fn get_const_i32(&self, n: u64) -> Value<'ll> {
909        self.get_const_int(self.type_i32(), n)
910    }
911
912    pub(crate) fn get_const_i16(&self, n: u64) -> Value<'ll> {
913        self.get_const_int(self.type_i16(), n)
914    }
915
916    pub(crate) fn get_const_i8(&self, n: u64) -> Value<'ll> {
917        self.get_const_int(self.type_i8(), n)
918    }
919
920    pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId {
921        unsafe { llvm::LLVMGetMDKindIDInContext(self.llcx(), name.as_ptr(), name.len() as c_uint) }
922    }
923
924    pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata {
925        unsafe { llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr(), name.len()) }
926    }
927
928    pub(crate) fn get_functions(&self) -> Vec<Value<'ll>> {
929        let mut functions = ::alloc::vec::Vec::new()vec![];
930        let mut func = unsafe { llvm::LLVMGetFirstFunction(self.llmod()) };
931        while let Some(f) = func {
932            functions.push(f);
933            func = unsafe { llvm::LLVMGetNextFunction(f) }
934        }
935        functions
936    }
937}
938
939impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
940    fn vtables(
941        &self,
942    ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), Value<'ll>>> {
943        &self.vtables
944    }
945
946    fn apply_vcall_visibility_metadata(
947        &self,
948        ty: Ty<'tcx>,
949        poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
950        vtable: Value<'ll>,
951    ) {
952        apply_vcall_visibility_metadata(self, ty, poly_trait_ref, vtable);
953    }
954
955    fn get_fn(&self, instance: Instance<'tcx>) -> Value<'ll> {
956        get_fn(self, instance)
957    }
958
959    fn get_fn_addr(
960        &self,
961        instance: Instance<'tcx>,
962        pointer_auth_schema: Option<&PointerAuthSchema>,
963    ) -> Value<'ll> {
964        // When pointer authentication metadata is provided, `get_fn_addr` will
965        // attempt to sign the pointer using LLVM's `ConstPtrAuth` constant
966        // expression.
967        //
968        // FIXME(jchlanda) Currently, all function addresses requested from
969        // within LLVM codegen are signed. This behavior is too broad, resulting
970        // in the logic being applied to function values, not just pointers
971        // (addresses).
972        //
973        // See the discussion in the rust-lang issue:
974        // <https://github.com/rust-lang/rust/issues/152532>, and comment in
975        // builder's `ptrauth_operand_bundle`.
976        let llfn = get_fn(self, instance);
977        match pointer_auth_schema {
978            Some(schema) => common::maybe_sign_fn_ptr(self, instance, llfn, schema),
979            None => llfn,
980        }
981    }
982
983    fn eh_personality(&self) -> Value<'ll> {
984        // The exception handling personality function.
985        //
986        // If our compilation unit has the `eh_personality` lang item somewhere
987        // within it, then we just need to codegen that. Otherwise, we're
988        // building an rlib which will depend on some upstream implementation of
989        // this function, so we just codegen a generic reference to it. We don't
990        // specify any of the types for the function, we just make it a symbol
991        // that LLVM can later use.
992        //
993        // Note that MSVC is a little special here in that we don't use the
994        // `eh_personality` lang item at all. Currently LLVM has support for
995        // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
996        // *name of the personality function* to decide what kind of unwind side
997        // tables/landing pads to emit. It looks like Dwarf is used by default,
998        // injecting a dependency on the `_Unwind_Resume` symbol for resuming
999        // an "exception", but for MSVC we want to force SEH. This means that we
1000        // can't actually have the personality function be our standard
1001        // `rust_eh_personality` function, but rather we wired it up to the
1002        // CRT's custom personality function, which forces LLVM to consider
1003        // landing pads as "landing pads for SEH".
1004        if let Some(llpersonality) = self.eh_personality.get() {
1005            return llpersonality;
1006        }
1007
1008        let name = if wants_msvc_seh(&self.sess().target) {
1009            Some("__CxxFrameHandler3")
1010        } else if wants_wasm_eh(&self.sess().target) {
1011            // LLVM specifically tests for the name of the personality function
1012            // There is no need for this function to exist anywhere, it will
1013            // not be called. However, its name has to be "__gxx_wasm_personality_v0"
1014            // for native wasm exceptions.
1015            Some("__gxx_wasm_personality_v0")
1016        } else {
1017            None
1018        };
1019
1020        let tcx = self.tcx;
1021        let llfn = match tcx.lang_items().eh_personality() {
1022            Some(def_id) if name.is_none() => self.get_fn_addr(
1023                ty::Instance::expect_resolve(
1024                    tcx,
1025                    self.typing_env(),
1026                    def_id,
1027                    ty::GenericArgs::empty(),
1028                    DUMMY_SP,
1029                ),
1030                tcx.sess.pointer_authentication_functions(),
1031            ),
1032            _ => {
1033                let mangled_symbol;
1034                let name = match name {
1035                    Some(name) => name,
1036                    None => {
1037                        mangled_symbol = rustc_symbol_mangling::eh_personality_symbol(tcx);
1038                        mangled_symbol.as_str()
1039                    }
1040                };
1041                if let Some(llfn) = self.get_declared_value(name) {
1042                    llfn
1043                } else {
1044                    let fty = self.type_variadic_func(&[], self.type_i32());
1045                    let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
1046                    let target_cpu = attributes::target_cpu_attr(self, self.sess());
1047                    attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
1048                    llfn
1049                }
1050            }
1051        };
1052        self.eh_personality.set(Some(llfn));
1053        llfn
1054    }
1055
1056    fn sess(&self) -> &Session {
1057        self.tcx.sess
1058    }
1059
1060    fn set_frame_pointer_type(&self, llfn: Value<'ll>) {
1061        if let Some(attr) = attributes::frame_pointer_type_attr(self, self.sess()) {
1062            attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
1063        }
1064    }
1065
1066    fn apply_target_cpu_attr(&self, llfn: Value<'ll>) {
1067        let mut attrs = SmallVec::<[_; 2]>::new();
1068        attrs.push(attributes::target_cpu_attr(self, self.sess()));
1069        attrs.extend(attributes::tune_cpu_attr(self, self.sess()));
1070        attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
1071    }
1072
1073    fn apply_default_optimisation_attrs(&self, llfn: Value<'ll>) {
1074        let attrs = attributes::default_optimisation_attrs(self, self.sess());
1075        attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
1076    }
1077
1078    fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
1079        let entry_name = self.sess().target.entry_name.as_ref();
1080        if self.get_declared_value(entry_name).is_none() {
1081            let llfn = self.declare_entry_fn(
1082                entry_name,
1083                to_llvm_calling_convention(self.sess(), self.sess().target.entry_abi),
1084                llvm::UnnamedAddr::Global,
1085                fn_type,
1086            );
1087            attributes::apply_to_llfn(
1088                llfn,
1089                llvm::AttributePlace::Function,
1090                attributes::target_features_attr(self, self.tcx, ::alloc::vec::Vec::new()vec![]).as_slice(),
1091            );
1092            Some(llfn)
1093        } else {
1094            // If the symbol already exists, it is an error: for example, the user wrote
1095            // #[no_mangle] extern "C" fn main(..) {..}
1096            None
1097        }
1098    }
1099
1100    fn intrinsic_call_expects_place_always(&self, name: Symbol) -> bool {
1101        #[allow(non_exhaustive_omitted_patterns)] match name {
    sym::black_box => true,
    _ => false,
}matches!(name, sym::black_box)
1102    }
1103}
1104
1105impl<'ll> CodegenCx<'ll, '_> {
1106    pub(crate) fn get_intrinsic(
1107        &self,
1108        base_name: Cow<'static, str>,
1109        type_params: &[Type<'ll>],
1110    ) -> (Type<'ll>, Value<'ll>) {
1111        *self
1112            .intrinsics
1113            .borrow_mut()
1114            .entry((base_name, SmallVec::from_slice(type_params)))
1115            .or_insert_with_key(|(base_name, type_params)| {
1116                self.declare_intrinsic(base_name, type_params)
1117            })
1118    }
1119
1120    fn declare_intrinsic(
1121        &self,
1122        base_name: &str,
1123        type_params: &[Type<'ll>],
1124    ) -> (Type<'ll>, Value<'ll>) {
1125        match base_name {
1126            // This isn't an "LLVM intrinsic", but LLVM's optimization passes
1127            // recognize it like one (including turning it into `bcmp` sometimes)
1128            // and we use it to implement intrinsics like `raw_eq` and `compare_bytes`
1129            "memcmp" => {
1130                let fn_ty = self.type_func(
1131                    &[self.type_ptr(), self.type_ptr(), self.type_isize()],
1132                    self.type_int(),
1133                );
1134                let f = self.declare_cfn("memcmp", llvm::UnnamedAddr::No, fn_ty);
1135
1136                (fn_ty, f)
1137            }
1138            // Experimental retag intrinsics.
1139            // This form is used to retag a pointer that has already been stored in a register. It receives
1140            // the pointer and returns an alias with the same address, but different provenance.
1141            "__rust_retag_reg" => {
1142                let fn_ty = self.type_func(type_params, self.type_ptr());
1143                let llfn = self.declare_cfn(base_name, llvm::UnnamedAddr::No, fn_ty);
1144                let nounwind = llvm::AttributeKind::NoUnwind.create_attr(self.llcx);
1145                attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[nounwind]);
1146                (fn_ty, llfn)
1147            }
1148            // This form is used to retag a pointer that is stored in another place. It receives a pointer to the
1149            // place and returns `void`. This communicates the indirection  without requiring an explicit load and
1150            // store. If we used the `reg` form instead, then we would need to load the place, retag it, and then
1151            // store the result back, which would be undefined behavior for `readonly` places.
1152            "__rust_retag_mem" => {
1153                let fn_ty = self.type_func(type_params, self.type_void());
1154                let llfn = self.declare_cfn(base_name, llvm::UnnamedAddr::No, fn_ty);
1155                let nounwind = llvm::AttributeKind::NoUnwind.create_attr(self.llcx);
1156                attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[nounwind]);
1157                (fn_ty, llfn)
1158            }
1159            _ => {
1160                let intrinsic = llvm::Intrinsic::lookup(base_name.as_bytes())
1161                    .unwrap_or_else(|| ::rustc_span::macros::bug_impl(None,
    format_args!("Unknown intrinsic: `{0}`", base_name), Location::caller())bug!("Unknown intrinsic: `{base_name}`"));
1162                let f = intrinsic.get_declaration(self.llmod, type_params);
1163                (self.get_type_of_global(f), f)
1164            }
1165        }
1166    }
1167}
1168
1169impl CodegenCx<'_, '_> {
1170    /// Generates a new symbol name with the given prefix. This symbol name must
1171    /// only be used for definitions with `internal` or `private` linkage.
1172    pub(crate) fn generate_local_symbol_name(&self, prefix: &str) -> String {
1173        let idx = self.local_gen_sym_counter.get();
1174        self.local_gen_sym_counter.set(idx + 1);
1175        // Include a '.' character, so there can be no accidental conflicts with
1176        // user defined names
1177        let mut name = String::with_capacity(prefix.len() + 6);
1178        name.push_str(prefix);
1179        name.push('.');
1180        name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
1181        name
1182    }
1183
1184    /// Generates a new global symbol name with the given prefix.
1185    pub(crate) fn generate_global_symbol_name(&self) -> String {
1186        let idx = self.global_gen_sym_counter.get();
1187        self.global_gen_sym_counter.set(idx + 1);
1188
1189        let sym = self.codegen_unit.symbol_name();
1190        let prefix = sym.as_str();
1191        let mut name = String::with_capacity(prefix.len() + 6);
1192        name.push_str(prefix);
1193        name.push('.');
1194        name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
1195        name
1196    }
1197}
1198
1199impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
1200    /// Wrapper for `LLVMMDNodeInContext2`, i.e. `llvm::MDNode::get`.
1201    pub(crate) fn md_node_in_context(&self, md_list: &[&'ll Metadata]) -> &'ll Metadata {
1202        unsafe { llvm::LLVMMDNodeInContext2(self.llcx(), md_list.as_ptr(), md_list.len()) }
1203    }
1204
1205    /// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
1206    pub(crate) fn set_metadata(&self, val: Value<'ll>, kind_id: MetadataKindId, md: &'ll Metadata) {
1207        let node = self.get_metadata_value(md);
1208        llvm::LLVMSetMetadata(val, kind_id, node);
1209    }
1210
1211    /// Helper method for the sequence of calls:
1212    /// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1213    /// - `LLVMMetadataAsValue` (to adapt that node to an `llvm::Value`)
1214    /// - `LLVMSetMetadata` (to set that node as metadata of `kind_id` for `instruction`)
1215    pub(crate) fn set_metadata_node(
1216        &self,
1217        instruction: Value<'ll>,
1218        kind_id: MetadataKindId,
1219        md_list: &[&'ll Metadata],
1220    ) -> &'ll Metadata {
1221        let md = self.md_node_in_context(md_list);
1222        self.set_metadata(instruction, kind_id, md);
1223        md
1224    }
1225
1226    /// Helper method for the sequence of calls:
1227    /// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1228    /// - `LLVMMetadataAsValue` (to adapt that node to an `llvm::Value`)
1229    /// - `LLVMAddNamedMetadataOperand` (to set that node as metadata of `kind_name` for `module`)
1230    pub(crate) fn module_add_named_metadata_node(
1231        &self,
1232        module: Module<'ll>,
1233        kind_name: &CStr,
1234        md_list: &[&'ll Metadata],
1235    ) {
1236        let md = self.md_node_in_context(md_list);
1237        let md_as_val = self.get_metadata_value(md);
1238        unsafe { llvm::LLVMAddNamedMetadataOperand(module, kind_name.as_ptr(), md_as_val) };
1239    }
1240
1241    /// Helper method for the sequence of calls:
1242    /// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1243    /// - `LLVMGlobalAddMetadata` (to set that node as metadata of `kind_id` for `global`)
1244    pub(crate) fn global_add_metadata_node(
1245        &self,
1246        global: Value<'ll>,
1247        kind_id: MetadataKindId,
1248        md_list: &[&'ll Metadata],
1249    ) {
1250        let md = self.md_node_in_context(md_list);
1251        unsafe { llvm::LLVMGlobalAddMetadata(global, kind_id, md) };
1252    }
1253
1254    /// Helper method for the sequence of calls:
1255    /// - `LLVMMDNodeInContext2` (to create an `llvm::MDNode` from a list of metadata)
1256    /// - `LLVMGlobalSetMetadata` (to set that node as metadata of `kind_id` for `global`)
1257    pub(crate) fn global_set_metadata_node(
1258        &self,
1259        global: Value<'ll>,
1260        kind_id: MetadataKindId,
1261        md_list: &[&'ll Metadata],
1262    ) {
1263        let md = self.md_node_in_context(md_list);
1264        unsafe { llvm::LLVMGlobalSetMetadata(global, kind_id, md) };
1265    }
1266}
1267
1268impl HasDataLayout for CodegenCx<'_, '_> {
1269    #[inline]
1270    fn data_layout(&self) -> &TargetDataLayout {
1271        &self.tcx.data_layout
1272    }
1273}
1274
1275impl HasTargetSpec for CodegenCx<'_, '_> {
1276    #[inline]
1277    fn target_spec(&self) -> &Target {
1278        &self.tcx.sess.target
1279    }
1280}
1281
1282impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
1283    #[inline]
1284    fn tcx(&self) -> TyCtxt<'tcx> {
1285        self.tcx
1286    }
1287}
1288
1289impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
1290    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
1291        ty::TypingEnv::fully_monomorphized()
1292    }
1293}
1294
1295impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
1296    #[inline]
1297    fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
1298        if let LayoutError::SizeOverflow(_)
1299        | LayoutError::ReferencesError(_)
1300        | LayoutError::InvalidSimd { .. } = err
1301        {
1302            self.tcx.dcx().span_fatal(span, err.to_string())
1303        } else {
1304            self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
1305        }
1306    }
1307}
1308
1309impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
1310    #[inline]
1311    fn handle_fn_abi_err(
1312        &self,
1313        err: FnAbiError<'tcx>,
1314        span: Span,
1315        fn_abi_request: FnAbiRequest<'tcx>,
1316    ) -> ! {
1317        codegen_handle_fn_abi_err(self.tcx, err, span, fn_abi_request).raise_fatal()
1318    }
1319}