]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/context.rs
Rollup merge of #107412 - tshepang:needless-check, r=wesleywiser
[rust.git] / compiler / rustc_codegen_llvm / src / context.rs
1 use crate::attributes;
2 use crate::back::write::to_llvm_code_model;
3 use crate::callee::get_fn;
4 use crate::coverageinfo;
5 use crate::debuginfo;
6 use crate::llvm;
7 use crate::llvm_util;
8 use crate::type_::Type;
9 use crate::value::Value;
10
11 use cstr::cstr;
12 use rustc_codegen_ssa::base::wants_msvc_seh;
13 use rustc_codegen_ssa::traits::*;
14 use rustc_data_structures::base_n;
15 use rustc_data_structures::fx::FxHashMap;
16 use rustc_data_structures::small_c_str::SmallCStr;
17 use rustc_hir::def_id::DefId;
18 use rustc_middle::mir::mono::CodegenUnit;
19 use rustc_middle::ty::layout::{
20     FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, LayoutError, LayoutOfHelpers,
21     TyAndLayout,
22 };
23 use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
24 use rustc_middle::{bug, span_bug};
25 use rustc_session::config::{BranchProtection, CFGuard, CFProtection};
26 use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
27 use rustc_session::Session;
28 use rustc_span::source_map::Span;
29 use rustc_span::source_map::Spanned;
30 use rustc_target::abi::{
31     call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
32 };
33 use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
34 use smallvec::SmallVec;
35
36 use std::cell::{Cell, RefCell};
37 use std::ffi::CStr;
38 use std::str;
39
40 /// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
41 /// `llvm::Context` so that several compilation units may be optimized in parallel.
42 /// All other LLVM data structures in the `CodegenCx` are tied to that `llvm::Context`.
43 pub struct CodegenCx<'ll, 'tcx> {
44     pub tcx: TyCtxt<'tcx>,
45     pub check_overflow: bool,
46     pub use_dll_storage_attrs: bool,
47     pub tls_model: llvm::ThreadLocalMode,
48
49     pub llmod: &'ll llvm::Module,
50     pub llcx: &'ll llvm::Context,
51     pub codegen_unit: &'tcx CodegenUnit<'tcx>,
52
53     /// Cache instances of monomorphic and polymorphic items
54     pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
55     /// Cache generated vtables
56     pub vtables:
57         RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
58     /// Cache of constant strings,
59     pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
60
61     /// Reverse-direction for const ptrs cast from globals.
62     ///
63     /// Key is a Value holding a `*T`,
64     /// Val is a Value holding a `*[T]`.
65     ///
66     /// Needed because LLVM loses pointer->pointee association
67     /// when we ptrcast, and we have to ptrcast during codegen
68     /// of a `[T]` const because we form a slice, a `(*T,usize)` pair, not
69     /// a pointer to an LLVM array type. Similar for trait objects.
70     pub const_unsized: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
71
72     /// Cache of emitted const globals (value -> global)
73     pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
74
75     /// List of globals for static variables which need to be passed to the
76     /// LLVM function ReplaceAllUsesWith (RAUW) when codegen is complete.
77     /// (We have to make sure we don't invalidate any Values referring
78     /// to constants.)
79     pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
80
81     /// Statics that will be placed in the llvm.used variable
82     /// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
83     pub used_statics: RefCell<Vec<&'ll Value>>,
84
85     /// Statics that will be placed in the llvm.compiler.used variable
86     /// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
87     pub compiler_used_statics: RefCell<Vec<&'ll Value>>,
88
89     /// Mapping of non-scalar types to llvm types and field remapping if needed.
90     pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), TypeLowering<'ll>>>,
91
92     /// Mapping of scalar types to llvm types.
93     pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
94
95     pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
96     pub isize_ty: &'ll Type,
97
98     pub coverage_cx: Option<coverageinfo::CrateCoverageContext<'ll, 'tcx>>,
99     pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
100
101     eh_personality: Cell<Option<&'ll Value>>,
102     eh_catch_typeinfo: Cell<Option<&'ll Value>>,
103     pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,
104
105     intrinsics: RefCell<FxHashMap<&'static str, (&'ll Type, &'ll Value)>>,
106
107     /// A counter that is used for generating local symbol names
108     local_gen_sym_counter: Cell<usize>,
109
110     /// `codegen_static` will sometimes create a second global variable with a
111     /// different type and clear the symbol name of the original global.
112     /// `global_asm!` needs to be able to find this new global so that it can
113     /// compute the correct mangled symbol name to insert into the asm.
114     pub renamed_statics: RefCell<FxHashMap<DefId, &'ll Value>>,
115 }
116
117 pub struct TypeLowering<'ll> {
118     /// Associated LLVM type
119     pub lltype: &'ll Type,
120
121     /// If padding is used the slice maps fields from source order
122     /// to llvm order.
123     pub field_remapping: Option<SmallVec<[u32; 4]>>,
124 }
125
126 fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
127     match tls_model {
128         TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
129         TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
130         TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
131         TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
132     }
133 }
134
135 pub unsafe fn create_module<'ll>(
136     tcx: TyCtxt<'_>,
137     llcx: &'ll llvm::Context,
138     mod_name: &str,
139 ) -> &'ll llvm::Module {
140     let sess = tcx.sess;
141     let mod_name = SmallCStr::new(mod_name);
142     let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
143
144     let mut target_data_layout = sess.target.data_layout.to_string();
145     let llvm_version = llvm_util::get_version();
146     if llvm_version < (14, 0, 0) {
147         if sess.target.llvm_target == "i686-pc-windows-msvc"
148             || sess.target.llvm_target == "i586-pc-windows-msvc"
149         {
150             target_data_layout =
151                 "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:32-n8:16:32-a:0:32-S32"
152                     .to_string();
153         } else if sess.target.arch == "wasm32" {
154             target_data_layout = target_data_layout.replace("-p10:8:8-p20:8:8", "");
155         }
156     }
157     if llvm_version < (16, 0, 0) {
158         if sess.target.arch == "s390x" {
159             target_data_layout = target_data_layout.replace("-v128:64", "");
160         } else if sess.target.arch == "riscv64" {
161             target_data_layout = target_data_layout.replace("-n32:64-", "-n64-");
162         }
163     }
164
165     // Ensure the data-layout values hardcoded remain the defaults.
166     if sess.target.is_builtin {
167         let tm = crate::back::write::create_informational_target_machine(tcx.sess);
168         llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
169         llvm::LLVMRustDisposeTargetMachine(tm);
170
171         let llvm_data_layout = llvm::LLVMGetDataLayoutStr(llmod);
172         let llvm_data_layout = str::from_utf8(CStr::from_ptr(llvm_data_layout).to_bytes())
173             .expect("got a non-UTF8 data-layout from LLVM");
174
175         // Unfortunately LLVM target specs change over time, and right now we
176         // don't have proper support to work with any more than one
177         // `data_layout` than the one that is in the rust-lang/rust repo. If
178         // this compiler is configured against a custom LLVM, we may have a
179         // differing data layout, even though we should update our own to use
180         // that one.
181         //
182         // As an interim hack, if CFG_LLVM_ROOT is not an empty string then we
183         // disable this check entirely as we may be configured with something
184         // that has a different target layout.
185         //
186         // Unsure if this will actually cause breakage when rustc is configured
187         // as such.
188         //
189         // FIXME(#34960)
190         let cfg_llvm_root = option_env!("CFG_LLVM_ROOT").unwrap_or("");
191         let custom_llvm_used = !cfg_llvm_root.trim().is_empty();
192
193         if !custom_llvm_used && target_data_layout != llvm_data_layout {
194             bug!(
195                 "data-layout for target `{rustc_target}`, `{rustc_layout}`, \
196                   differs from LLVM target's `{llvm_target}` default layout, `{llvm_layout}`",
197                 rustc_target = sess.opts.target_triple,
198                 rustc_layout = target_data_layout,
199                 llvm_target = sess.target.llvm_target,
200                 llvm_layout = llvm_data_layout
201             );
202         }
203     }
204
205     let data_layout = SmallCStr::new(&target_data_layout);
206     llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
207
208     let llvm_target = SmallCStr::new(&sess.target.llvm_target);
209     llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
210
211     let reloc_model = sess.relocation_model();
212     if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
213         llvm::LLVMRustSetModulePICLevel(llmod);
214         // PIE is potentially more effective than PIC, but can only be used in executables.
215         // If all our outputs are executables, then we can relax PIC to PIE.
216         if reloc_model == RelocModel::Pie
217             || sess.crate_types().iter().all(|ty| *ty == CrateType::Executable)
218         {
219             llvm::LLVMRustSetModulePIELevel(llmod);
220         }
221     }
222
223     // Linking object files with different code models is undefined behavior
224     // because the compiler would have to generate additional code (to span
225     // longer jumps) if a larger code model is used with a smaller one.
226     //
227     // See https://reviews.llvm.org/D52322 and https://reviews.llvm.org/D52323.
228     llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
229
230     // If skipping the PLT is enabled, we need to add some module metadata
231     // to ensure intrinsic calls don't use it.
232     if !sess.needs_plt() {
233         let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
234         llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
235     }
236
237     if sess.is_sanitizer_cfi_enabled() {
238         // FIXME(rcvalle): Add support for non canonical jump tables.
239         let canonical_jump_tables = "CFI Canonical Jump Tables\0".as_ptr().cast();
240         // FIXME(rcvalle): Add it with Override behavior flag.
241         llvm::LLVMRustAddModuleFlag(
242             llmod,
243             llvm::LLVMModFlagBehavior::Warning,
244             canonical_jump_tables,
245             1,
246         );
247     }
248
249     if sess.is_sanitizer_kcfi_enabled() {
250         let kcfi = "kcfi\0".as_ptr().cast();
251         llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
252     }
253
254     // Control Flow Guard is currently only supported by the MSVC linker on Windows.
255     if sess.target.is_like_msvc {
256         match sess.opts.cg.control_flow_guard {
257             CFGuard::Disabled => {}
258             CFGuard::NoChecks => {
259                 // Set `cfguard=1` module flag to emit metadata only.
260                 llvm::LLVMRustAddModuleFlag(
261                     llmod,
262                     llvm::LLVMModFlagBehavior::Warning,
263                     "cfguard\0".as_ptr() as *const _,
264                     1,
265                 )
266             }
267             CFGuard::Checks => {
268                 // Set `cfguard=2` module flag to emit metadata and checks.
269                 llvm::LLVMRustAddModuleFlag(
270                     llmod,
271                     llvm::LLVMModFlagBehavior::Warning,
272                     "cfguard\0".as_ptr() as *const _,
273                     2,
274                 )
275             }
276         }
277     }
278
279     if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
280         let behavior = if llvm_version >= (15, 0, 0) {
281             llvm::LLVMModFlagBehavior::Min
282         } else {
283             llvm::LLVMModFlagBehavior::Error
284         };
285
286         if sess.target.arch == "aarch64" {
287             llvm::LLVMRustAddModuleFlag(
288                 llmod,
289                 behavior,
290                 "branch-target-enforcement\0".as_ptr().cast(),
291                 bti.into(),
292             );
293             llvm::LLVMRustAddModuleFlag(
294                 llmod,
295                 behavior,
296                 "sign-return-address\0".as_ptr().cast(),
297                 pac_ret.is_some().into(),
298             );
299             let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
300             llvm::LLVMRustAddModuleFlag(
301                 llmod,
302                 behavior,
303                 "sign-return-address-all\0".as_ptr().cast(),
304                 pac_opts.leaf.into(),
305             );
306             llvm::LLVMRustAddModuleFlag(
307                 llmod,
308                 behavior,
309                 "sign-return-address-with-bkey\0".as_ptr().cast(),
310                 u32::from(pac_opts.key == PAuthKey::B),
311             );
312         } else {
313             bug!(
314                 "branch-protection used on non-AArch64 target; \
315                   this should be checked in rustc_session."
316             );
317         }
318     }
319
320     // Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang).
321     if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
322         llvm::LLVMRustAddModuleFlag(
323             llmod,
324             llvm::LLVMModFlagBehavior::Override,
325             "cf-protection-branch\0".as_ptr().cast(),
326             1,
327         )
328     }
329     if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
330         llvm::LLVMRustAddModuleFlag(
331             llmod,
332             llvm::LLVMModFlagBehavior::Override,
333             "cf-protection-return\0".as_ptr().cast(),
334             1,
335         )
336     }
337
338     if sess.opts.unstable_opts.virtual_function_elimination {
339         llvm::LLVMRustAddModuleFlag(
340             llmod,
341             llvm::LLVMModFlagBehavior::Error,
342             "Virtual Function Elim\0".as_ptr().cast(),
343             1,
344         );
345     }
346
347     llmod
348 }
349
350 impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
351     pub(crate) fn new(
352         tcx: TyCtxt<'tcx>,
353         codegen_unit: &'tcx CodegenUnit<'tcx>,
354         llvm_module: &'ll crate::ModuleLlvm,
355     ) -> Self {
356         // An interesting part of Windows which MSVC forces our hand on (and
357         // apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
358         // attributes in LLVM IR as well as native dependencies (in C these
359         // correspond to `__declspec(dllimport)`).
360         //
361         // LD (BFD) in MinGW mode can often correctly guess `dllexport` but
362         // relying on that can result in issues like #50176.
363         // LLD won't support that and expects symbols with proper attributes.
364         // Because of that we make MinGW target emit dllexport just like MSVC.
365         // When it comes to dllimport we use it for constants but for functions
366         // rely on the linker to do the right thing. Opposed to dllexport this
367         // task is easy for them (both LD and LLD) and allows us to easily use
368         // symbols from static libraries in shared libraries.
369         //
370         // Whenever a dynamic library is built on Windows it must have its public
371         // interface specified by functions tagged with `dllexport` or otherwise
372         // they're not available to be linked against. This poses a few problems
373         // for the compiler, some of which are somewhat fundamental, but we use
374         // the `use_dll_storage_attrs` variable below to attach the `dllexport`
375         // attribute to all LLVM functions that are exported e.g., they're
376         // already tagged with external linkage). This is suboptimal for a few
377         // reasons:
378         //
379         // * If an object file will never be included in a dynamic library,
380         //   there's no need to attach the dllexport attribute. Most object
381         //   files in Rust are not destined to become part of a dll as binaries
382         //   are statically linked by default.
383         // * If the compiler is emitting both an rlib and a dylib, the same
384         //   source object file is currently used but with MSVC this may be less
385         //   feasible. The compiler may be able to get around this, but it may
386         //   involve some invasive changes to deal with this.
387         //
388         // The flip side of this situation is that whenever you link to a dll and
389         // you import a function from it, the import should be tagged with
390         // `dllimport`. At this time, however, the compiler does not emit
391         // `dllimport` for any declarations other than constants (where it is
392         // required), which is again suboptimal for even more reasons!
393         //
394         // * Calling a function imported from another dll without using
395         //   `dllimport` causes the linker/compiler to have extra overhead (one
396         //   `jmp` instruction on x86) when calling the function.
397         // * The same object file may be used in different circumstances, so a
398         //   function may be imported from a dll if the object is linked into a
399         //   dll, but it may be just linked against if linked into an rlib.
400         // * The compiler has no knowledge about whether native functions should
401         //   be tagged dllimport or not.
402         //
403         // For now the compiler takes the perf hit (I do not have any numbers to
404         // this effect) by marking very little as `dllimport` and praying the
405         // linker will take care of everything. Fixing this problem will likely
406         // require adding a few attributes to Rust itself (feature gated at the
407         // start) and then strongly recommending static linkage on Windows!
408         let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
409
410         let check_overflow = tcx.sess.overflow_checks();
411
412         let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
413
414         let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
415
416         let coverage_cx = if tcx.sess.instrument_coverage() {
417             let covctx = coverageinfo::CrateCoverageContext::new();
418             Some(covctx)
419         } else {
420             None
421         };
422
423         let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
424             let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);
425             debuginfo::metadata::build_compile_unit_di_node(
426                 tcx,
427                 codegen_unit.name().as_str(),
428                 &dctx,
429             );
430             Some(dctx)
431         } else {
432             None
433         };
434
435         let isize_ty = Type::ix_llcx(llcx, tcx.data_layout.pointer_size.bits());
436
437         CodegenCx {
438             tcx,
439             check_overflow,
440             use_dll_storage_attrs,
441             tls_model,
442             llmod,
443             llcx,
444             codegen_unit,
445             instances: Default::default(),
446             vtables: Default::default(),
447             const_str_cache: Default::default(),
448             const_unsized: Default::default(),
449             const_globals: Default::default(),
450             statics_to_rauw: RefCell::new(Vec::new()),
451             used_statics: RefCell::new(Vec::new()),
452             compiler_used_statics: RefCell::new(Vec::new()),
453             type_lowering: Default::default(),
454             scalar_lltypes: Default::default(),
455             pointee_infos: Default::default(),
456             isize_ty,
457             coverage_cx,
458             dbg_cx,
459             eh_personality: Cell::new(None),
460             eh_catch_typeinfo: Cell::new(None),
461             rust_try_fn: Cell::new(None),
462             intrinsics: Default::default(),
463             local_gen_sym_counter: Cell::new(0),
464             renamed_statics: Default::default(),
465         }
466     }
467
468     pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
469         &self.statics_to_rauw
470     }
471
472     #[inline]
473     pub fn coverage_context(&self) -> Option<&coverageinfo::CrateCoverageContext<'ll, 'tcx>> {
474         self.coverage_cx.as_ref()
475     }
476
477     pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
478         let section = cstr!("llvm.metadata");
479         let array = self.const_array(self.type_ptr_to(self.type_i8()), values);
480
481         unsafe {
482             let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
483             llvm::LLVMSetInitializer(g, array);
484             llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
485             llvm::LLVMSetSection(g, section.as_ptr());
486         }
487     }
488 }
489
490 impl<'ll, 'tcx> MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
491     fn vtables(
492         &self,
493     ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>
494     {
495         &self.vtables
496     }
497
498     fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
499         get_fn(self, instance)
500     }
501
502     fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
503         get_fn(self, instance)
504     }
505
506     fn eh_personality(&self) -> &'ll Value {
507         // The exception handling personality function.
508         //
509         // If our compilation unit has the `eh_personality` lang item somewhere
510         // within it, then we just need to codegen that. Otherwise, we're
511         // building an rlib which will depend on some upstream implementation of
512         // this function, so we just codegen a generic reference to it. We don't
513         // specify any of the types for the function, we just make it a symbol
514         // that LLVM can later use.
515         //
516         // Note that MSVC is a little special here in that we don't use the
517         // `eh_personality` lang item at all. Currently LLVM has support for
518         // both Dwarf and SEH unwind mechanisms for MSVC targets and uses the
519         // *name of the personality function* to decide what kind of unwind side
520         // tables/landing pads to emit. It looks like Dwarf is used by default,
521         // injecting a dependency on the `_Unwind_Resume` symbol for resuming
522         // an "exception", but for MSVC we want to force SEH. This means that we
523         // can't actually have the personality function be our standard
524         // `rust_eh_personality` function, but rather we wired it up to the
525         // CRT's custom personality function, which forces LLVM to consider
526         // landing pads as "landing pads for SEH".
527         if let Some(llpersonality) = self.eh_personality.get() {
528             return llpersonality;
529         }
530         let tcx = self.tcx;
531         let llfn = match tcx.lang_items().eh_personality() {
532             Some(def_id) if !wants_msvc_seh(self.sess()) => self.get_fn_addr(
533                 ty::Instance::resolve(
534                     tcx,
535                     ty::ParamEnv::reveal_all(),
536                     def_id,
537                     tcx.intern_substs(&[]),
538                 )
539                 .unwrap()
540                 .unwrap(),
541             ),
542             _ => {
543                 let name = if wants_msvc_seh(self.sess()) {
544                     "__CxxFrameHandler3"
545                 } else {
546                     "rust_eh_personality"
547                 };
548                 if let Some(llfn) = self.get_declared_value(name) {
549                     llfn
550                 } else {
551                     let fty = self.type_variadic_func(&[], self.type_i32());
552                     let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
553                     let target_cpu = attributes::target_cpu_attr(self);
554                     attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
555                     llfn
556                 }
557             }
558         };
559         self.eh_personality.set(Some(llfn));
560         llfn
561     }
562
563     fn sess(&self) -> &Session {
564         self.tcx.sess
565     }
566
567     fn check_overflow(&self) -> bool {
568         self.check_overflow
569     }
570
571     fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
572         self.codegen_unit
573     }
574
575     fn set_frame_pointer_type(&self, llfn: &'ll Value) {
576         if let Some(attr) = attributes::frame_pointer_type_attr(self) {
577             attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
578         }
579     }
580
581     fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
582         let mut attrs = SmallVec::<[_; 2]>::new();
583         attrs.push(attributes::target_cpu_attr(self));
584         attrs.extend(attributes::tune_cpu_attr(self));
585         attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
586     }
587
588     fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
589         let entry_name = self.sess().target.entry_name.as_ref();
590         if self.get_declared_value(entry_name).is_none() {
591             Some(self.declare_entry_fn(
592                 entry_name,
593                 self.sess().target.entry_abi.into(),
594                 llvm::UnnamedAddr::Global,
595                 fn_type,
596             ))
597         } else {
598             // If the symbol already exists, it is an error: for example, the user wrote
599             // #[no_mangle] extern "C" fn main(..) {..}
600             // instead of #[start]
601             None
602         }
603     }
604 }
605
606 impl<'ll> CodegenCx<'ll, '_> {
607     pub(crate) fn get_intrinsic(&self, key: &str) -> (&'ll Type, &'ll Value) {
608         if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
609             return v;
610         }
611
612         self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
613     }
614
615     fn insert_intrinsic(
616         &self,
617         name: &'static str,
618         args: Option<&[&'ll llvm::Type]>,
619         ret: &'ll llvm::Type,
620     ) -> (&'ll llvm::Type, &'ll llvm::Value) {
621         let fn_ty = if let Some(args) = args {
622             self.type_func(args, ret)
623         } else {
624             self.type_variadic_func(&[], ret)
625         };
626         let f = self.declare_cfn(name, llvm::UnnamedAddr::No, fn_ty);
627         self.intrinsics.borrow_mut().insert(name, (fn_ty, f));
628         (fn_ty, f)
629     }
630
631     fn declare_intrinsic(&self, key: &str) -> Option<(&'ll Type, &'ll Value)> {
632         macro_rules! ifn {
633             ($name:expr, fn() -> $ret:expr) => (
634                 if key == $name {
635                     return Some(self.insert_intrinsic($name, Some(&[]), $ret));
636                 }
637             );
638             ($name:expr, fn(...) -> $ret:expr) => (
639                 if key == $name {
640                     return Some(self.insert_intrinsic($name, None, $ret));
641                 }
642             );
643             ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
644                 if key == $name {
645                     return Some(self.insert_intrinsic($name, Some(&[$($arg),*]), $ret));
646                 }
647             );
648         }
649         macro_rules! mk_struct {
650             ($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
651         }
652
653         let i8p = self.type_i8p();
654         let void = self.type_void();
655         let i1 = self.type_i1();
656         let t_i8 = self.type_i8();
657         let t_i16 = self.type_i16();
658         let t_i32 = self.type_i32();
659         let t_i64 = self.type_i64();
660         let t_i128 = self.type_i128();
661         let t_isize = self.type_isize();
662         let t_f32 = self.type_f32();
663         let t_f64 = self.type_f64();
664         let t_metadata = self.type_metadata();
665
666         ifn!("llvm.wasm.trunc.unsigned.i32.f32", fn(t_f32) -> t_i32);
667         ifn!("llvm.wasm.trunc.unsigned.i32.f64", fn(t_f64) -> t_i32);
668         ifn!("llvm.wasm.trunc.unsigned.i64.f32", fn(t_f32) -> t_i64);
669         ifn!("llvm.wasm.trunc.unsigned.i64.f64", fn(t_f64) -> t_i64);
670         ifn!("llvm.wasm.trunc.signed.i32.f32", fn(t_f32) -> t_i32);
671         ifn!("llvm.wasm.trunc.signed.i32.f64", fn(t_f64) -> t_i32);
672         ifn!("llvm.wasm.trunc.signed.i64.f32", fn(t_f32) -> t_i64);
673         ifn!("llvm.wasm.trunc.signed.i64.f64", fn(t_f64) -> t_i64);
674
675         ifn!("llvm.fptosi.sat.i8.f32", fn(t_f32) -> t_i8);
676         ifn!("llvm.fptosi.sat.i16.f32", fn(t_f32) -> t_i16);
677         ifn!("llvm.fptosi.sat.i32.f32", fn(t_f32) -> t_i32);
678         ifn!("llvm.fptosi.sat.i64.f32", fn(t_f32) -> t_i64);
679         ifn!("llvm.fptosi.sat.i128.f32", fn(t_f32) -> t_i128);
680         ifn!("llvm.fptosi.sat.i8.f64", fn(t_f64) -> t_i8);
681         ifn!("llvm.fptosi.sat.i16.f64", fn(t_f64) -> t_i16);
682         ifn!("llvm.fptosi.sat.i32.f64", fn(t_f64) -> t_i32);
683         ifn!("llvm.fptosi.sat.i64.f64", fn(t_f64) -> t_i64);
684         ifn!("llvm.fptosi.sat.i128.f64", fn(t_f64) -> t_i128);
685
686         ifn!("llvm.fptoui.sat.i8.f32", fn(t_f32) -> t_i8);
687         ifn!("llvm.fptoui.sat.i16.f32", fn(t_f32) -> t_i16);
688         ifn!("llvm.fptoui.sat.i32.f32", fn(t_f32) -> t_i32);
689         ifn!("llvm.fptoui.sat.i64.f32", fn(t_f32) -> t_i64);
690         ifn!("llvm.fptoui.sat.i128.f32", fn(t_f32) -> t_i128);
691         ifn!("llvm.fptoui.sat.i8.f64", fn(t_f64) -> t_i8);
692         ifn!("llvm.fptoui.sat.i16.f64", fn(t_f64) -> t_i16);
693         ifn!("llvm.fptoui.sat.i32.f64", fn(t_f64) -> t_i32);
694         ifn!("llvm.fptoui.sat.i64.f64", fn(t_f64) -> t_i64);
695         ifn!("llvm.fptoui.sat.i128.f64", fn(t_f64) -> t_i128);
696
697         ifn!("llvm.trap", fn() -> void);
698         ifn!("llvm.debugtrap", fn() -> void);
699         ifn!("llvm.frameaddress", fn(t_i32) -> i8p);
700
701         ifn!("llvm.powi.f32", fn(t_f32, t_i32) -> t_f32);
702         ifn!("llvm.powi.f64", fn(t_f64, t_i32) -> t_f64);
703
704         ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
705         ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
706
707         ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
708         ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
709
710         ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
711         ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
712
713         ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
714         ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
715
716         ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
717         ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
718
719         ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
720         ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
721
722         ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
723         ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
724
725         ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
726         ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
727
728         ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
729         ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
730
731         ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
732         ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
733
734         ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
735         ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
736
737         ifn!("llvm.minnum.f32", fn(t_f32, t_f32) -> t_f32);
738         ifn!("llvm.minnum.f64", fn(t_f64, t_f64) -> t_f64);
739         ifn!("llvm.maxnum.f32", fn(t_f32, t_f32) -> t_f32);
740         ifn!("llvm.maxnum.f64", fn(t_f64, t_f64) -> t_f64);
741
742         ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
743         ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
744
745         ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
746         ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
747
748         ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
749         ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
750
751         ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
752         ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
753         ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
754         ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
755
756         ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
757         ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
758         ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
759         ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
760
761         ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
762         ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
763         ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
764         ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
765         ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
766
767         ifn!("llvm.ctlz.i8", fn(t_i8, i1) -> t_i8);
768         ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
769         ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
770         ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
771         ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
772
773         ifn!("llvm.cttz.i8", fn(t_i8, i1) -> t_i8);
774         ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
775         ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
776         ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
777         ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
778
779         ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
780         ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
781         ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
782         ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
783
784         ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
785         ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
786         ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
787         ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
788         ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
789
790         ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
791         ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
792         ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
793         ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
794         ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
795
796         ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
797         ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
798         ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
799         ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
800         ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
801
802         ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
803         ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
804         ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
805         ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
806         ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
807
808         ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
809         ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
810         ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
811         ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
812         ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
813
814         ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
815         ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
816         ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
817         ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
818         ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
819
820         ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
821         ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
822         ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
823         ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
824         ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
825
826         ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
827         ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
828         ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
829         ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
830         ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
831
832         ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
833         ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
834         ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
835         ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
836         ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
837
838         ifn!("llvm.sadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
839         ifn!("llvm.sadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
840         ifn!("llvm.sadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
841         ifn!("llvm.sadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
842         ifn!("llvm.sadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
843
844         ifn!("llvm.uadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
845         ifn!("llvm.uadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
846         ifn!("llvm.uadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
847         ifn!("llvm.uadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
848         ifn!("llvm.uadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
849
850         ifn!("llvm.ssub.sat.i8", fn(t_i8, t_i8) -> t_i8);
851         ifn!("llvm.ssub.sat.i16", fn(t_i16, t_i16) -> t_i16);
852         ifn!("llvm.ssub.sat.i32", fn(t_i32, t_i32) -> t_i32);
853         ifn!("llvm.ssub.sat.i64", fn(t_i64, t_i64) -> t_i64);
854         ifn!("llvm.ssub.sat.i128", fn(t_i128, t_i128) -> t_i128);
855
856         ifn!("llvm.usub.sat.i8", fn(t_i8, t_i8) -> t_i8);
857         ifn!("llvm.usub.sat.i16", fn(t_i16, t_i16) -> t_i16);
858         ifn!("llvm.usub.sat.i32", fn(t_i32, t_i32) -> t_i32);
859         ifn!("llvm.usub.sat.i64", fn(t_i64, t_i64) -> t_i64);
860         ifn!("llvm.usub.sat.i128", fn(t_i128, t_i128) -> t_i128);
861
862         ifn!("llvm.lifetime.start.p0i8", fn(t_i64, i8p) -> void);
863         ifn!("llvm.lifetime.end.p0i8", fn(t_i64, i8p) -> void);
864
865         ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
866         ifn!("llvm.eh.typeid.for", fn(i8p) -> t_i32);
867         ifn!("llvm.localescape", fn(...) -> void);
868         ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
869         ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
870
871         ifn!("llvm.assume", fn(i1) -> void);
872         ifn!("llvm.prefetch", fn(i8p, t_i32, t_i32, t_i32) -> void);
873
874         // This isn't an "LLVM intrinsic", but LLVM's optimization passes
875         // recognize it like one and we assume it exists in `core::slice::cmp`
876         match self.sess().target.arch.as_ref() {
877             "avr" | "msp430" => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i16),
878             _ => ifn!("memcmp", fn(i8p, i8p, t_isize) -> t_i32),
879         }
880
881         // variadic intrinsics
882         ifn!("llvm.va_start", fn(i8p) -> void);
883         ifn!("llvm.va_end", fn(i8p) -> void);
884         ifn!("llvm.va_copy", fn(i8p, i8p) -> void);
885
886         if self.sess().instrument_coverage() {
887             ifn!("llvm.instrprof.increment", fn(i8p, t_i64, t_i32, t_i32) -> void);
888         }
889
890         ifn!("llvm.type.test", fn(i8p, t_metadata) -> i1);
891         ifn!("llvm.type.checked.load", fn(i8p, t_i32, t_metadata) -> mk_struct! {i8p, i1});
892
893         if self.sess().opts.debuginfo != DebugInfo::None {
894             ifn!("llvm.dbg.declare", fn(t_metadata, t_metadata) -> void);
895             ifn!("llvm.dbg.value", fn(t_metadata, t_i64, t_metadata) -> void);
896         }
897
898         ifn!("llvm.ptrmask", fn(i8p, t_isize) -> i8p);
899
900         None
901     }
902
903     pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
904         if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
905             return eh_catch_typeinfo;
906         }
907         let tcx = self.tcx;
908         assert!(self.sess().target.os == "emscripten");
909         let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
910             Some(def_id) => self.get_static(def_id),
911             _ => {
912                 let ty = self
913                     .type_struct(&[self.type_ptr_to(self.type_isize()), self.type_i8p()], false);
914                 self.declare_global("rust_eh_catch_typeinfo", ty)
915             }
916         };
917         let eh_catch_typeinfo = self.const_bitcast(eh_catch_typeinfo, self.type_i8p());
918         self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
919         eh_catch_typeinfo
920     }
921 }
922
923 impl CodegenCx<'_, '_> {
924     /// Generates a new symbol name with the given prefix. This symbol name must
925     /// only be used for definitions with `internal` or `private` linkage.
926     pub fn generate_local_symbol_name(&self, prefix: &str) -> String {
927         let idx = self.local_gen_sym_counter.get();
928         self.local_gen_sym_counter.set(idx + 1);
929         // Include a '.' character, so there can be no accidental conflicts with
930         // user defined names
931         let mut name = String::with_capacity(prefix.len() + 6);
932         name.push_str(prefix);
933         name.push('.');
934         base_n::push_str(idx as u128, base_n::ALPHANUMERIC_ONLY, &mut name);
935         name
936     }
937 }
938
939 impl HasDataLayout for CodegenCx<'_, '_> {
940     #[inline]
941     fn data_layout(&self) -> &TargetDataLayout {
942         &self.tcx.data_layout
943     }
944 }
945
946 impl HasTargetSpec for CodegenCx<'_, '_> {
947     #[inline]
948     fn target_spec(&self) -> &Target {
949         &self.tcx.sess.target
950     }
951 }
952
953 impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
954     #[inline]
955     fn tcx(&self) -> TyCtxt<'tcx> {
956         self.tcx
957     }
958 }
959
960 impl<'tcx, 'll> HasParamEnv<'tcx> for CodegenCx<'ll, 'tcx> {
961     fn param_env(&self) -> ty::ParamEnv<'tcx> {
962         ty::ParamEnv::reveal_all()
963     }
964 }
965
966 impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
967     type LayoutOfResult = TyAndLayout<'tcx>;
968
969     #[inline]
970     fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
971         if let LayoutError::SizeOverflow(_) = err {
972             self.sess().emit_fatal(Spanned { span, node: err })
973         } else {
974             span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
975         }
976     }
977 }
978
979 impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
980     type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
981
982     #[inline]
983     fn handle_fn_abi_err(
984         &self,
985         err: FnAbiError<'tcx>,
986         span: Span,
987         fn_abi_request: FnAbiRequest<'tcx>,
988     ) -> ! {
989         if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
990             self.sess().emit_fatal(Spanned { span, node: err })
991         } else {
992             match fn_abi_request {
993                 FnAbiRequest::OfFnPtr { sig, extra_args } => {
994                     span_bug!(
995                         span,
996                         "`fn_abi_of_fn_ptr({}, {:?})` failed: {}",
997                         sig,
998                         extra_args,
999                         err
1000                     );
1001                 }
1002                 FnAbiRequest::OfInstance { instance, extra_args } => {
1003                     span_bug!(
1004                         span,
1005                         "`fn_abi_of_instance({}, {:?})` failed: {}",
1006                         instance,
1007                         extra_args,
1008                         err
1009                     );
1010                 }
1011             }
1012         }
1013     }
1014 }