rustc_codegen_llvm/
context.rs

1use std::borrow::{Borrow, Cow};
2use std::cell::{Cell, RefCell};
3use std::ffi::{CStr, c_char, 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::back::versioned_llvm_target;
10use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
11use rustc_codegen_ssa::errors as ssa_errors;
12use rustc_codegen_ssa::traits::*;
13use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, ToBaseN};
14use rustc_data_structures::fx::FxHashMap;
15use rustc_data_structures::small_c_str::SmallCStr;
16use rustc_hir::def_id::DefId;
17use rustc_middle::middle::codegen_fn_attrs::PatchableFunctionEntry;
18use rustc_middle::mir::mono::CodegenUnit;
19use rustc_middle::ty::layout::{
20    FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
21};
22use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
23use rustc_middle::{bug, span_bug};
24use rustc_session::Session;
25use rustc_session::config::{
26    BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, FunctionReturn, PAuthKey, PacRet,
27};
28use rustc_span::source_map::Spanned;
29use rustc_span::{DUMMY_SP, Span};
30use rustc_symbol_mangling::mangle_internal_symbol;
31use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
32use smallvec::SmallVec;
33
34use crate::back::write::to_llvm_code_model;
35use crate::callee::get_fn;
36use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
37use crate::llvm::Metadata;
38use crate::type_::Type;
39use crate::value::Value;
40use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
41
42/// `TyCtxt` (and related cache datastructures) can't be move between threads.
43/// However, there are various cx related functions which we want to be available to the builder and
44/// other compiler pieces. Here we define a small subset which has enough information and can be
45/// moved around more freely.
46pub(crate) struct SCx<'ll> {
47    pub llmod: &'ll llvm::Module,
48    pub llcx: &'ll llvm::Context,
49    pub isize_ty: &'ll Type,
50}
51
52impl<'ll> Borrow<SCx<'ll>> for FullCx<'ll, '_> {
53    fn borrow(&self) -> &SCx<'ll> {
54        &self.scx
55    }
56}
57
58impl<'ll, 'tcx> Deref for FullCx<'ll, 'tcx> {
59    type Target = SimpleCx<'ll>;
60
61    #[inline]
62    fn deref(&self) -> &Self::Target {
63        &self.scx
64    }
65}
66
67pub(crate) struct GenericCx<'ll, T: Borrow<SCx<'ll>>>(T, PhantomData<SCx<'ll>>);
68
69impl<'ll, T: Borrow<SCx<'ll>>> Deref for GenericCx<'ll, T> {
70    type Target = T;
71
72    #[inline]
73    fn deref(&self) -> &Self::Target {
74        &self.0
75    }
76}
77
78impl<'ll, T: Borrow<SCx<'ll>>> DerefMut for GenericCx<'ll, T> {
79    #[inline]
80    fn deref_mut(&mut self) -> &mut Self::Target {
81        &mut self.0
82    }
83}
84
85pub(crate) type SimpleCx<'ll> = GenericCx<'ll, SCx<'ll>>;
86
87/// There is one `CodegenCx` per codegen unit. Each one has its own LLVM
88/// `llvm::Context` so that several codegen units may be processed in parallel.
89/// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
90pub(crate) type CodegenCx<'ll, 'tcx> = GenericCx<'ll, FullCx<'ll, 'tcx>>;
91
92pub(crate) struct FullCx<'ll, 'tcx> {
93    pub tcx: TyCtxt<'tcx>,
94    pub scx: SimpleCx<'ll>,
95    pub use_dll_storage_attrs: bool,
96    pub tls_model: llvm::ThreadLocalMode,
97
98    pub codegen_unit: &'tcx CodegenUnit<'tcx>,
99
100    /// Cache instances of monomorphic and polymorphic items
101    pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
102    /// Cache generated vtables
103    pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>>,
104    /// Cache of constant strings,
105    pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
106
107    /// Cache of emitted const globals (value -> global)
108    pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
109
110    /// List of globals for static variables which need to be passed to the
111    /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
112    /// (We have to make sure we don't invalidate any Values referring
113    /// to constants.)
114    pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
115
116    /// Statics that will be placed in the llvm.used variable
117    /// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
118    pub used_statics: Vec<&'ll Value>,
119
120    /// Statics that will be placed in the llvm.compiler.used variable
121    /// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
122    pub compiler_used_statics: Vec<&'ll Value>,
123
124    /// Mapping of non-scalar types to llvm types.
125    pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
126
127    /// Mapping of scalar types to llvm types.
128    pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
129
130    /// Extra per-CGU codegen state needed when coverage instrumentation is enabled.
131    pub coverage_cx: Option<coverageinfo::CguCoverageContext<'ll, 'tcx>>,
132    pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
133
134    eh_personality: Cell<Option<&'ll Value>>,
135    eh_catch_typeinfo: Cell<Option<&'ll Value>>,
136    pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,
137
138    intrinsics:
139        RefCell<FxHashMap<(Cow<'static, str>, SmallVec<[&'ll Type; 2]>), (&'ll Type, &'ll Value)>>,
140
141    /// A counter that is used for generating local symbol names
142    local_gen_sym_counter: Cell<usize>,
143
144    /// `codegen_static` will sometimes create a second global variable with a
145    /// different type and clear the symbol name of the original global.
146    /// `global_asm!` needs to be able to find this new global so that it can
147    /// compute the correct mangled symbol name to insert into the asm.
148    pub renamed_statics: RefCell<FxHashMap<DefId, &'ll Value>>,
149}
150
151fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
152    match tls_model {
153        TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
154        TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
155        TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
156        TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
157        TlsModel::Emulated => llvm::ThreadLocalMode::GeneralDynamic,
158    }
159}
160
161pub(crate) unsafe fn create_module<'ll>(
162    tcx: TyCtxt<'_>,
163    llcx: &'ll llvm::Context,
164    mod_name: &str,
165) -> &'ll llvm::Module {
166    let sess = tcx.sess;
167    let mod_name = SmallCStr::new(mod_name);
168    let llmod = unsafe { llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx) };
169
170    let cx = SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size());
171
172    let mut target_data_layout = sess.target.data_layout.to_string();
173    let llvm_version = llvm_util::get_version();
174
175    if llvm_version < (20, 0, 0) {
176        if sess.target.arch == "aarch64" || sess.target.arch.starts_with("arm64") {
177            // LLVM 20 defines three additional address spaces for alternate
178            // pointer kinds used in Windows.
179            // See https://github.com/llvm/llvm-project/pull/111879
180            target_data_layout =
181                target_data_layout.replace("-p270:32:32-p271:32:32-p272:64:64", "");
182        }
183        if sess.target.arch.starts_with("sparc") {
184            // LLVM 20 updates the sparc layout to correctly align 128 bit integers to 128 bit.
185            // See https://github.com/llvm/llvm-project/pull/106951
186            target_data_layout = target_data_layout.replace("-i128:128", "");
187        }
188        if sess.target.arch.starts_with("mips64") {
189            // LLVM 20 updates the mips64 layout to correctly align 128 bit integers to 128 bit.
190            // See https://github.com/llvm/llvm-project/pull/112084
191            target_data_layout = target_data_layout.replace("-i128:128", "");
192        }
193        if sess.target.arch.starts_with("powerpc64") {
194            // LLVM 20 updates the powerpc64 layout to correctly align 128 bit integers to 128 bit.
195            // See https://github.com/llvm/llvm-project/pull/118004
196            target_data_layout = target_data_layout.replace("-i128:128", "");
197        }
198        if sess.target.arch.starts_with("wasm32") || sess.target.arch.starts_with("wasm64") {
199            // LLVM 20 updates the wasm(32|64) layout to correctly align 128 bit integers to 128 bit.
200            // See https://github.com/llvm/llvm-project/pull/119204
201            target_data_layout = target_data_layout.replace("-i128:128", "");
202        }
203    }
204    if llvm_version < (21, 0, 0) {
205        if sess.target.arch == "nvptx64" {
206            // LLVM 21 updated the default layout on nvptx: https://github.com/llvm/llvm-project/pull/124961
207            target_data_layout = target_data_layout.replace("e-p6:32:32-i64", "e-i64");
208        }
209        if sess.target.arch == "amdgpu" {
210            // LLVM 21 adds the address width for address space 8.
211            // See https://github.com/llvm/llvm-project/pull/139419
212            target_data_layout = target_data_layout.replace("p8:128:128:128:48", "p8:128:128")
213        }
214    }
215    if llvm_version < (22, 0, 0) {
216        if sess.target.arch == "avr" {
217            // LLVM 22.0 updated the default layout on avr: https://github.com/llvm/llvm-project/pull/153010
218            target_data_layout = target_data_layout.replace("n8:16", "n8")
219        }
220    }
221
222    // Ensure the data-layout values hardcoded remain the defaults.
223    {
224        let tm = crate::back::write::create_informational_target_machine(sess, false);
225        unsafe {
226            llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());
227        }
228
229        let llvm_data_layout = unsafe { llvm::LLVMGetDataLayoutStr(llmod) };
230        let llvm_data_layout =
231            str::from_utf8(unsafe { CStr::from_ptr(llvm_data_layout) }.to_bytes())
232                .expect("got a non-UTF8 data-layout from LLVM");
233
234        if target_data_layout != llvm_data_layout {
235            tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
236                rustc_target: sess.opts.target_triple.to_string().as_str(),
237                rustc_layout: target_data_layout.as_str(),
238                llvm_target: sess.target.llvm_target.borrow(),
239                llvm_layout: llvm_data_layout,
240            });
241        }
242    }
243
244    let data_layout = SmallCStr::new(&target_data_layout);
245    unsafe {
246        llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
247    }
248
249    let llvm_target = SmallCStr::new(&versioned_llvm_target(sess));
250    unsafe {
251        llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
252    }
253
254    let reloc_model = sess.relocation_model();
255    if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
256        unsafe {
257            llvm::LLVMRustSetModulePICLevel(llmod);
258        }
259        // PIE is potentially more effective than PIC, but can only be used in executables.
260        // If all our outputs are executables, then we can relax PIC to PIE.
261        if reloc_model == RelocModel::Pie
262            || tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable)
263        {
264            unsafe {
265                llvm::LLVMRustSetModulePIELevel(llmod);
266            }
267        }
268    }
269
270    // Linking object files with different code models is undefined behavior
271    // because the compiler would have to generate additional code (to span
272    // longer jumps) if a larger code model is used with a smaller one.
273    //
274    // See https://reviews.llvm.org/D52322 and https://reviews.llvm.org/D52323.
275    unsafe {
276        llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
277    }
278
279    // If skipping the PLT is enabled, we need to add some module metadata
280    // to ensure intrinsic calls don't use it.
281    if !sess.needs_plt() {
282        llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "RtLibUseGOT", 1);
283    }
284
285    // Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
286    if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
287        llvm::add_module_flag_u32(
288            llmod,
289            llvm::ModuleFlagMergeBehavior::Override,
290            "CFI Canonical Jump Tables",
291            1,
292        );
293    }
294
295    // If we're normalizing integers with CFI, ensure LLVM generated functions do the same.
296    // See https://github.com/llvm/llvm-project/pull/104826
297    if sess.is_sanitizer_cfi_normalize_integers_enabled() {
298        llvm::add_module_flag_u32(
299            llmod,
300            llvm::ModuleFlagMergeBehavior::Override,
301            "cfi-normalize-integers",
302            1,
303        );
304    }
305
306    // Enable LTO unit splitting if specified or if CFI is enabled. (See
307    // https://reviews.llvm.org/D53891.)
308    if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
309        llvm::add_module_flag_u32(
310            llmod,
311            llvm::ModuleFlagMergeBehavior::Override,
312            "EnableSplitLTOUnit",
313            1,
314        );
315    }
316
317    // Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
318    if sess.is_sanitizer_kcfi_enabled() {
319        llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);
320
321        // Add "kcfi-offset" module flag with -Z patchable-function-entry (See
322        // https://reviews.llvm.org/D141172).
323        let pfe =
324            PatchableFunctionEntry::from_config(sess.opts.unstable_opts.patchable_function_entry);
325        if pfe.prefix() > 0 {
326            llvm::add_module_flag_u32(
327                llmod,
328                llvm::ModuleFlagMergeBehavior::Override,
329                "kcfi-offset",
330                pfe.prefix().into(),
331            );
332        }
333
334        // Add "kcfi-arity" module flag if KCFI arity indicator is enabled. (See
335        // https://github.com/llvm/llvm-project/pull/117121.)
336        if sess.is_sanitizer_kcfi_arity_enabled() {
337            // KCFI arity indicator requires LLVM 21.0.0 or later.
338            if llvm_version < (21, 0, 0) {
339                tcx.dcx().emit_err(crate::errors::SanitizerKcfiArityRequiresLLVM2100);
340            }
341
342            llvm::add_module_flag_u32(
343                llmod,
344                llvm::ModuleFlagMergeBehavior::Override,
345                "kcfi-arity",
346                1,
347            );
348        }
349    }
350
351    // Control Flow Guard is currently only supported by MSVC and LLVM on Windows.
352    if sess.target.is_like_msvc
353        || (sess.target.options.os == "windows"
354            && sess.target.options.env == "gnu"
355            && sess.target.options.abi == "llvm")
356    {
357        match sess.opts.cg.control_flow_guard {
358            CFGuard::Disabled => {}
359            CFGuard::NoChecks => {
360                // Set `cfguard=1` module flag to emit metadata only.
361                llvm::add_module_flag_u32(
362                    llmod,
363                    llvm::ModuleFlagMergeBehavior::Warning,
364                    "cfguard",
365                    1,
366                );
367            }
368            CFGuard::Checks => {
369                // Set `cfguard=2` module flag to emit metadata and checks.
370                llvm::add_module_flag_u32(
371                    llmod,
372                    llvm::ModuleFlagMergeBehavior::Warning,
373                    "cfguard",
374                    2,
375                );
376            }
377        }
378    }
379
380    if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
381        if sess.target.arch == "aarch64" {
382            llvm::add_module_flag_u32(
383                llmod,
384                llvm::ModuleFlagMergeBehavior::Min,
385                "branch-target-enforcement",
386                bti.into(),
387            );
388            llvm::add_module_flag_u32(
389                llmod,
390                llvm::ModuleFlagMergeBehavior::Min,
391                "sign-return-address",
392                pac_ret.is_some().into(),
393            );
394            let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, pc: false, key: PAuthKey::A });
395            llvm::add_module_flag_u32(
396                llmod,
397                llvm::ModuleFlagMergeBehavior::Min,
398                "branch-protection-pauth-lr",
399                pac_opts.pc.into(),
400            );
401            llvm::add_module_flag_u32(
402                llmod,
403                llvm::ModuleFlagMergeBehavior::Min,
404                "sign-return-address-all",
405                pac_opts.leaf.into(),
406            );
407            llvm::add_module_flag_u32(
408                llmod,
409                llvm::ModuleFlagMergeBehavior::Min,
410                "sign-return-address-with-bkey",
411                u32::from(pac_opts.key == PAuthKey::B),
412            );
413        } else {
414            bug!(
415                "branch-protection used on non-AArch64 target; \
416                  this should be checked in rustc_session."
417            );
418        }
419    }
420
421    // Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang).
422    if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
423        llvm::add_module_flag_u32(
424            llmod,
425            llvm::ModuleFlagMergeBehavior::Override,
426            "cf-protection-branch",
427            1,
428        );
429    }
430    if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
431        llvm::add_module_flag_u32(
432            llmod,
433            llvm::ModuleFlagMergeBehavior::Override,
434            "cf-protection-return",
435            1,
436        );
437    }
438
439    if sess.opts.unstable_opts.virtual_function_elimination {
440        llvm::add_module_flag_u32(
441            llmod,
442            llvm::ModuleFlagMergeBehavior::Error,
443            "Virtual Function Elim",
444            1,
445        );
446    }
447
448    // Set module flag to enable Windows EHCont Guard (/guard:ehcont).
449    if sess.opts.unstable_opts.ehcont_guard {
450        llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "ehcontguard", 1);
451    }
452
453    match sess.opts.unstable_opts.function_return {
454        FunctionReturn::Keep => {}
455        FunctionReturn::ThunkExtern => {
456            llvm::add_module_flag_u32(
457                llmod,
458                llvm::ModuleFlagMergeBehavior::Override,
459                "function_return_thunk_extern",
460                1,
461            );
462        }
463    }
464
465    match (sess.opts.unstable_opts.small_data_threshold, sess.target.small_data_threshold_support())
466    {
467        // Set up the small-data optimization limit for architectures that use
468        // an LLVM module flag to control this.
469        (Some(threshold), SmallDataThresholdSupport::LlvmModuleFlag(flag)) => {
470            llvm::add_module_flag_u32(
471                llmod,
472                llvm::ModuleFlagMergeBehavior::Error,
473                &flag,
474                threshold as u32,
475            );
476        }
477        _ => (),
478    };
479
480    // Insert `llvm.ident` metadata.
481    //
482    // On the wasm targets it will get hooked up to the "producer" sections
483    // `processed-by` information.
484    #[allow(clippy::option_env_unwrap)]
485    let rustc_producer =
486        format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
487
488    let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
489
490    unsafe {
491        llvm::LLVMAddNamedMetadataOperand(
492            llmod,
493            c"llvm.ident".as_ptr(),
494            &cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
495        );
496    }
497
498    // Emit RISC-V specific target-abi metadata
499    // to workaround lld as the LTO plugin not
500    // correctly setting target-abi for the LTO object
501    // FIXME: https://github.com/llvm/llvm-project/issues/50591
502    // If llvm_abiname is empty, emit nothing.
503    let llvm_abiname = &sess.target.options.llvm_abiname;
504    if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
505        llvm::add_module_flag_str(
506            llmod,
507            llvm::ModuleFlagMergeBehavior::Error,
508            "target-abi",
509            llvm_abiname,
510        );
511    }
512
513    // Add module flags specified via -Z llvm_module_flag
514    for (key, value, merge_behavior) in &sess.opts.unstable_opts.llvm_module_flag {
515        let merge_behavior = match merge_behavior.as_str() {
516            "error" => llvm::ModuleFlagMergeBehavior::Error,
517            "warning" => llvm::ModuleFlagMergeBehavior::Warning,
518            "require" => llvm::ModuleFlagMergeBehavior::Require,
519            "override" => llvm::ModuleFlagMergeBehavior::Override,
520            "append" => llvm::ModuleFlagMergeBehavior::Append,
521            "appendunique" => llvm::ModuleFlagMergeBehavior::AppendUnique,
522            "max" => llvm::ModuleFlagMergeBehavior::Max,
523            "min" => llvm::ModuleFlagMergeBehavior::Min,
524            // We already checked this during option parsing
525            _ => unreachable!(),
526        };
527        llvm::add_module_flag_u32(llmod, merge_behavior, key, *value);
528    }
529
530    llmod
531}
532
533impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
534    pub(crate) fn new(
535        tcx: TyCtxt<'tcx>,
536        codegen_unit: &'tcx CodegenUnit<'tcx>,
537        llvm_module: &'ll crate::ModuleLlvm,
538    ) -> Self {
539        // An interesting part of Windows which MSVC forces our hand on (and
540        // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
541        // attributes in LLVM IR as well as native dependencies (in C these
542        // correspond to `__declspec(dllimport)`).
543        //
544        // LD (BFD) in MinGW mode can often correctly guess `dllexport` but
545        // relying on that can result in issues like #50176.
546        // LLD won't support that and expects symbols with proper attributes.
547        // Because of that we make MinGW target emit dllexport just like MSVC.
548        // When it comes to dllimport we use it for constants but for functions
549        // rely on the linker to do the right thing. Opposed to dllexport this
550        // task is easy for them (both LD and LLD) and allows us to easily use
551        // symbols from static libraries in shared libraries.
552        //
553        // Whenever a dynamic library is built on Windows it must have its public
554        // interface specified by functions tagged with `dllexport` or otherwise
555        // they're not available to be linked against. This poses a few problems
556        // for the compiler, some of which are somewhat fundamental, but we use
557        // the `use_dll_storage_attrs` variable below to attach the `dllexport`
558        // attribute to all LLVM functions that are exported e.g., they're
559        // already tagged with external linkage). This is suboptimal for a few
560        // reasons:
561        //
562        // * If an object file will never be included in a dynamic library,
563        //   there's no need to attach the dllexport attribute. Most object
564        //   files in Rust are not destined to become part of a dll as binaries
565        //   are statically linked by default.
566        // * If the compiler is emitting both an rlib and a dylib, the same
567        //   source object file is currently used but with MSVC this may be less
568        //   feasible. The compiler may be able to get around this, but it may
569        //   involve some invasive changes to deal with this.
570        //
571        // The flip side of this situation is that whenever you link to a dll and
572        // you import a function from it, the import should be tagged with
573        // `dllimport`. At this time, however, the compiler does not emit
574        // `dllimport` for any declarations other than constants (where it is
575        // required), which is again suboptimal for even more reasons!
576        //
577        // * Calling a function imported from another dll without using
578        //   `dllimport` causes the linker/compiler to have extra overhead (one
579        //   `jmp` instruction on x86) when calling the function.
580        // * The same object file may be used in different circumstances, so a
581        //   function may be imported from a dll if the object is linked into a
582        //   dll, but it may be just linked against if linked into an rlib.
583        // * The compiler has no knowledge about whether native functions should
584        //   be tagged dllimport or not.
585        //
586        // For now the compiler takes the perf hit (I do not have any numbers to
587        // this effect) by marking very little as `dllimport` and praying the
588        // linker will take care of everything. Fixing this problem will likely
589        // require adding a few attributes to Rust itself (feature gated at the
590        // start) and then strongly recommending static linkage on Windows!
591        let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
592
593        let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
594
595        let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
596
597        let coverage_cx =
598            tcx.sess.instrument_coverage().then(coverageinfo::CguCoverageContext::new);
599
600        let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
601            let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);
602            debuginfo::metadata::build_compile_unit_di_node(
603                tcx,
604                codegen_unit.name().as_str(),
605                &dctx,
606            );
607            Some(dctx)
608        } else {
609            None
610        };
611
612        GenericCx(
613            FullCx {
614                tcx,
615                scx: SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size()),
616                use_dll_storage_attrs,
617                tls_model,
618                codegen_unit,
619                instances: Default::default(),
620                vtables: Default::default(),
621                const_str_cache: Default::default(),
622                const_globals: Default::default(),
623                statics_to_rauw: RefCell::new(Vec::new()),
624                used_statics: Vec::new(),
625                compiler_used_statics: Vec::new(),
626                type_lowering: Default::default(),
627                scalar_lltypes: Default::default(),
628                coverage_cx,
629                dbg_cx,
630                eh_personality: Cell::new(None),
631                eh_catch_typeinfo: Cell::new(None),
632                rust_try_fn: Cell::new(None),
633                intrinsics: Default::default(),
634                local_gen_sym_counter: Cell::new(0),
635                renamed_statics: Default::default(),
636            },
637            PhantomData,
638        )
639    }
640
641    pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
642        &self.statics_to_rauw
643    }
644
645    /// Extra state that is only available when coverage instrumentation is enabled.
646    #[inline]
647    #[track_caller]
648    pub(crate) fn coverage_cx(&self) -> &coverageinfo::CguCoverageContext<'ll, 'tcx> {
649        self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
650    }
651
652    pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
653        let array = self.const_array(self.type_ptr(), values);
654
655        let g = llvm::add_global(self.llmod, self.val_ty(array), name);
656        llvm::set_initializer(g, array);
657        llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
658        llvm::set_section(g, c"llvm.metadata");
659    }
660}
661impl<'ll> SimpleCx<'ll> {
662    pub(crate) fn get_type_of_global(&self, val: &'ll Value) -> &'ll Type {
663        unsafe { llvm::LLVMGlobalGetValueType(val) }
664    }
665    pub(crate) fn val_ty(&self, v: &'ll Value) -> &'ll Type {
666        common::val_ty(v)
667    }
668}
669impl<'ll> SimpleCx<'ll> {
670    pub(crate) fn new(
671        llmod: &'ll llvm::Module,
672        llcx: &'ll llvm::Context,
673        pointer_size: Size,
674    ) -> Self {
675        let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
676        Self(SCx { llmod, llcx, isize_ty }, PhantomData)
677    }
678}
679
680impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
681    pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
682        llvm::LLVMMetadataAsValue(self.llcx(), metadata)
683    }
684
685    pub(crate) fn get_const_int(&self, ty: &'ll Type, val: u64) -> &'ll Value {
686        unsafe { llvm::LLVMConstInt(ty, val, llvm::False) }
687    }
688
689    pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {
690        self.get_const_int(self.type_i64(), n)
691    }
692
693    pub(crate) fn get_const_i32(&self, n: u64) -> &'ll Value {
694        self.get_const_int(self.type_i32(), n)
695    }
696
697    pub(crate) fn get_const_i16(&self, n: u64) -> &'ll Value {
698        self.get_const_int(self.type_i16(), n)
699    }
700
701    pub(crate) fn get_const_i8(&self, n: u64) -> &'ll Value {
702        self.get_const_int(self.type_i8(), n)
703    }
704
705    pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
706        let name = SmallCStr::new(name);
707        unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }
708    }
709
710    pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId {
711        unsafe {
712            llvm::LLVMGetMDKindIDInContext(
713                self.llcx(),
714                name.as_ptr() as *const c_char,
715                name.len() as c_uint,
716            )
717        }
718    }
719
720    pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata {
721        unsafe {
722            llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
723        }
724    }
725}
726
727impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
728    fn vtables(
729        &self,
730    ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>> {
731        &self.vtables
732    }
733
734    fn apply_vcall_visibility_metadata(
735        &self,
736        ty: Ty<'tcx>,
737        poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
738        vtable: &'ll Value,
739    ) {
740        apply_vcall_visibility_metadata(self, ty, poly_trait_ref, vtable);
741    }
742
743    fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
744        get_fn(self, instance)
745    }
746
747    fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
748        get_fn(self, instance)
749    }
750
751    fn eh_personality(&self) -> &'ll Value {
752        // The exception handling personality function.
753        //
754        // If our compilation unit has the `eh_personality` lang item somewhere
755        // within it, then we just need to codegen that. Otherwise, we're
756        // building an rlib which will depend on some upstream implementation of
757        // this function, so we just codegen a generic reference to it. We don't
758        // specify any of the types for the function, we just make it a symbol
759        // that LLVM can later use.
760        //
761        // Note that MSVC is a little special here in that we don't use the
762        // `eh_personality` lang item at all. Currently LLVM has support for
763        // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
764        // *name of the personality function* to decide what kind of unwind side
765        // tables/landing pads to emit. It looks like Dwarf is used by default,
766        // injecting a dependency on the `_Unwind_Resume` symbol for resuming
767        // an "exception", but for MSVC we want to force SEH. This means that we
768        // can't actually have the personality function be our standard
769        // `rust_eh_personality` function, but rather we wired it up to the
770        // CRT's custom personality function, which forces LLVM to consider
771        // landing pads as "landing pads for SEH".
772        if let Some(llpersonality) = self.eh_personality.get() {
773            return llpersonality;
774        }
775
776        let name = if wants_msvc_seh(self.sess()) {
777            Some("__CxxFrameHandler3")
778        } else if wants_wasm_eh(self.sess()) {
779            // LLVM specifically tests for the name of the personality function
780            // There is no need for this function to exist anywhere, it will
781            // not be called. However, its name has to be "__gxx_wasm_personality_v0"
782            // for native wasm exceptions.
783            Some("__gxx_wasm_personality_v0")
784        } else {
785            None
786        };
787
788        let tcx = self.tcx;
789        let llfn = match tcx.lang_items().eh_personality() {
790            Some(def_id) if name.is_none() => self.get_fn_addr(ty::Instance::expect_resolve(
791                tcx,
792                self.typing_env(),
793                def_id,
794                ty::List::empty(),
795                DUMMY_SP,
796            )),
797            _ => {
798                let name = name.unwrap_or("rust_eh_personality");
799                if let Some(llfn) = self.get_declared_value(name) {
800                    llfn
801                } else {
802                    let fty = self.type_variadic_func(&[], self.type_i32());
803                    let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
804                    let target_cpu = attributes::target_cpu_attr(self);
805                    attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
806                    llfn
807                }
808            }
809        };
810        self.eh_personality.set(Some(llfn));
811        llfn
812    }
813
814    fn sess(&self) -> &Session {
815        self.tcx.sess
816    }
817
818    fn set_frame_pointer_type(&self, llfn: &'ll Value) {
819        if let Some(attr) = attributes::frame_pointer_type_attr(self) {
820            attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
821        }
822    }
823
824    fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
825        let mut attrs = SmallVec::<[_; 2]>::new();
826        attrs.push(attributes::target_cpu_attr(self));
827        attrs.extend(attributes::tune_cpu_attr(self));
828        attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
829    }
830
831    fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
832        let entry_name = self.sess().target.entry_name.as_ref();
833        if self.get_declared_value(entry_name).is_none() {
834            Some(self.declare_entry_fn(
835                entry_name,
836                llvm::CallConv::from_conv(
837                    self.sess().target.entry_abi,
838                    self.sess().target.arch.borrow(),
839                ),
840                llvm::UnnamedAddr::Global,
841                fn_type,
842            ))
843        } else {
844            // If the symbol already exists, it is an error: for example, the user wrote
845            // #[no_mangle] extern "C" fn main(..) {..}
846            None
847        }
848    }
849}
850
851impl<'ll> CodegenCx<'ll, '_> {
852    pub(crate) fn get_intrinsic(
853        &self,
854        base_name: Cow<'static, str>,
855        type_params: &[&'ll Type],
856    ) -> (&'ll Type, &'ll Value) {
857        *self
858            .intrinsics
859            .borrow_mut()
860            .entry((base_name, SmallVec::from_slice(type_params)))
861            .or_insert_with_key(|(base_name, type_params)| {
862                self.declare_intrinsic(base_name, type_params)
863            })
864    }
865
866    fn declare_intrinsic(
867        &self,
868        base_name: &str,
869        type_params: &[&'ll Type],
870    ) -> (&'ll Type, &'ll Value) {
871        // This isn't an "LLVM intrinsic", but LLVM's optimization passes
872        // recognize it like one (including turning it into `bcmp` sometimes)
873        // and we use it to implement intrinsics like `raw_eq` and `compare_bytes`
874        if base_name == "memcmp" {
875            let fn_ty = self
876                .type_func(&[self.type_ptr(), self.type_ptr(), self.type_isize()], self.type_int());
877            let f = self.declare_cfn("memcmp", llvm::UnnamedAddr::No, fn_ty);
878
879            return (fn_ty, f);
880        }
881
882        let intrinsic = llvm::Intrinsic::lookup(base_name.as_bytes())
883            .unwrap_or_else(|| bug!("Unknown intrinsic: `{base_name}`"));
884        let f = intrinsic.get_declaration(self.llmod, &type_params);
885
886        (self.get_type_of_global(f), f)
887    }
888
889    pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
890        if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
891            return eh_catch_typeinfo;
892        }
893        let tcx = self.tcx;
894        assert!(self.sess().target.os == "emscripten");
895        let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
896            Some(def_id) => self.get_static(def_id),
897            _ => {
898                let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
899                self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
900            }
901        };
902        self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
903        eh_catch_typeinfo
904    }
905}
906
907impl CodegenCx<'_, '_> {
908    /// Generates a new symbol name with the given prefix. This symbol name must
909    /// only be used for definitions with `internal` or `private` linkage.
910    pub(crate) fn generate_local_symbol_name(&self, prefix: &str) -> String {
911        let idx = self.local_gen_sym_counter.get();
912        self.local_gen_sym_counter.set(idx + 1);
913        // Include a '.' character, so there can be no accidental conflicts with
914        // user defined names
915        let mut name = String::with_capacity(prefix.len() + 6);
916        name.push_str(prefix);
917        name.push('.');
918        name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
919        name
920    }
921}
922
923impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
924    /// A wrapper for [`llvm::LLVMSetMetadata`], but it takes `Metadata` as a parameter instead of `Value`.
925    pub(crate) fn set_metadata<'a>(
926        &self,
927        val: &'a Value,
928        kind_id: impl Into<llvm::MetadataKindId>,
929        md: &'ll Metadata,
930    ) {
931        let node = self.get_metadata_value(md);
932        llvm::LLVMSetMetadata(val, kind_id.into(), node);
933    }
934}
935
936impl HasDataLayout for CodegenCx<'_, '_> {
937    #[inline]
938    fn data_layout(&self) -> &TargetDataLayout {
939        &self.tcx.data_layout
940    }
941}
942
943impl HasTargetSpec for CodegenCx<'_, '_> {
944    #[inline]
945    fn target_spec(&self) -> &Target {
946        &self.tcx.sess.target
947    }
948}
949
950impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
951    #[inline]
952    fn tcx(&self) -> TyCtxt<'tcx> {
953        self.tcx
954    }
955}
956
957impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
958    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
959        ty::TypingEnv::fully_monomorphized()
960    }
961}
962
963impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
964    #[inline]
965    fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
966        if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
967            self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
968        } else {
969            self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
970        }
971    }
972}
973
974impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
975    #[inline]
976    fn handle_fn_abi_err(
977        &self,
978        err: FnAbiError<'tcx>,
979        span: Span,
980        fn_abi_request: FnAbiRequest<'tcx>,
981    ) -> ! {
982        match err {
983            FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
984                self.tcx.dcx().emit_fatal(Spanned { span, node: err });
985            }
986            _ => match fn_abi_request {
987                FnAbiRequest::OfFnPtr { sig, extra_args } => {
988                    span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
989                }
990                FnAbiRequest::OfInstance { instance, extra_args } => {
991                    span_bug!(
992                        span,
993                        "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
994                    );
995                }
996            },
997        }
998    }
999}