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
42pub(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
87pub(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 pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
102 pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>>,
104 pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
106
107 pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
109
110 pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
115
116 pub used_statics: Vec<&'ll Value>,
119
120 pub compiler_used_statics: Vec<&'ll Value>,
123
124 pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
126
127 pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
129
130 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 local_gen_sym_counter: Cell<usize>,
143
144 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 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 target_data_layout = target_data_layout.replace("-i128:128", "");
187 }
188 if sess.target.arch.starts_with("mips64") {
189 target_data_layout = target_data_layout.replace("-i128:128", "");
192 }
193 if sess.target.arch.starts_with("powerpc64") {
194 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 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 target_data_layout = target_data_layout.replace("e-p6:32:32-i64", "e-i64");
208 }
209 if sess.target.arch == "amdgpu" {
210 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 target_data_layout = target_data_layout.replace("n8:16", "n8")
219 }
220 }
221
222 {
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 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 unsafe {
276 llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
277 }
278
279 if !sess.needs_plt() {
282 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "RtLibUseGOT", 1);
283 }
284
285 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 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 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 if sess.is_sanitizer_kcfi_enabled() {
319 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);
320
321 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 if sess.is_sanitizer_kcfi_arity_enabled() {
337 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 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 llvm::add_module_flag_u32(
362 llmod,
363 llvm::ModuleFlagMergeBehavior::Warning,
364 "cfguard",
365 1,
366 );
367 }
368 CFGuard::Checks => {
369 llvm::add_module_flag_u32(
371 llmod,
372 llvm::ModuleFlagMergeBehavior::Warning,
373 "cfguard",
374 2,
375 );
376 }
377 }
378 }
379
380 if let Some(regparm_count) = sess.opts.unstable_opts.regparm {
381 llvm::add_module_flag_u32(
382 llmod,
383 llvm::ModuleFlagMergeBehavior::Error,
384 "NumRegisterParameters",
385 regparm_count,
386 );
387 }
388
389 if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
390 if sess.target.arch == "aarch64" {
391 llvm::add_module_flag_u32(
392 llmod,
393 llvm::ModuleFlagMergeBehavior::Min,
394 "branch-target-enforcement",
395 bti.into(),
396 );
397 llvm::add_module_flag_u32(
398 llmod,
399 llvm::ModuleFlagMergeBehavior::Min,
400 "sign-return-address",
401 pac_ret.is_some().into(),
402 );
403 let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, pc: false, key: PAuthKey::A });
404 llvm::add_module_flag_u32(
405 llmod,
406 llvm::ModuleFlagMergeBehavior::Min,
407 "branch-protection-pauth-lr",
408 pac_opts.pc.into(),
409 );
410 llvm::add_module_flag_u32(
411 llmod,
412 llvm::ModuleFlagMergeBehavior::Min,
413 "sign-return-address-all",
414 pac_opts.leaf.into(),
415 );
416 llvm::add_module_flag_u32(
417 llmod,
418 llvm::ModuleFlagMergeBehavior::Min,
419 "sign-return-address-with-bkey",
420 u32::from(pac_opts.key == PAuthKey::B),
421 );
422 } else {
423 bug!(
424 "branch-protection used on non-AArch64 target; \
425 this should be checked in rustc_session."
426 );
427 }
428 }
429
430 if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
432 llvm::add_module_flag_u32(
433 llmod,
434 llvm::ModuleFlagMergeBehavior::Override,
435 "cf-protection-branch",
436 1,
437 );
438 }
439 if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
440 llvm::add_module_flag_u32(
441 llmod,
442 llvm::ModuleFlagMergeBehavior::Override,
443 "cf-protection-return",
444 1,
445 );
446 }
447
448 if sess.opts.unstable_opts.virtual_function_elimination {
449 llvm::add_module_flag_u32(
450 llmod,
451 llvm::ModuleFlagMergeBehavior::Error,
452 "Virtual Function Elim",
453 1,
454 );
455 }
456
457 if sess.opts.unstable_opts.ehcont_guard {
459 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "ehcontguard", 1);
460 }
461
462 match sess.opts.unstable_opts.function_return {
463 FunctionReturn::Keep => {}
464 FunctionReturn::ThunkExtern => {
465 llvm::add_module_flag_u32(
466 llmod,
467 llvm::ModuleFlagMergeBehavior::Override,
468 "function_return_thunk_extern",
469 1,
470 );
471 }
472 }
473
474 if sess.opts.unstable_opts.indirect_branch_cs_prefix {
475 llvm::add_module_flag_u32(
476 llmod,
477 llvm::ModuleFlagMergeBehavior::Override,
478 "indirect_branch_cs_prefix",
479 1,
480 );
481 }
482
483 match (sess.opts.unstable_opts.small_data_threshold, sess.target.small_data_threshold_support())
484 {
485 (Some(threshold), SmallDataThresholdSupport::LlvmModuleFlag(flag)) => {
488 llvm::add_module_flag_u32(
489 llmod,
490 llvm::ModuleFlagMergeBehavior::Error,
491 &flag,
492 threshold as u32,
493 );
494 }
495 _ => (),
496 };
497
498 #[allow(clippy::option_env_unwrap)]
503 let rustc_producer =
504 format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
505
506 let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
507
508 unsafe {
509 llvm::LLVMAddNamedMetadataOperand(
510 llmod,
511 c"llvm.ident".as_ptr(),
512 &cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
513 );
514 }
515
516 let llvm_abiname = &sess.target.options.llvm_abiname;
522 if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
523 llvm::add_module_flag_str(
524 llmod,
525 llvm::ModuleFlagMergeBehavior::Error,
526 "target-abi",
527 llvm_abiname,
528 );
529 }
530
531 for (key, value, merge_behavior) in &sess.opts.unstable_opts.llvm_module_flag {
533 let merge_behavior = match merge_behavior.as_str() {
534 "error" => llvm::ModuleFlagMergeBehavior::Error,
535 "warning" => llvm::ModuleFlagMergeBehavior::Warning,
536 "require" => llvm::ModuleFlagMergeBehavior::Require,
537 "override" => llvm::ModuleFlagMergeBehavior::Override,
538 "append" => llvm::ModuleFlagMergeBehavior::Append,
539 "appendunique" => llvm::ModuleFlagMergeBehavior::AppendUnique,
540 "max" => llvm::ModuleFlagMergeBehavior::Max,
541 "min" => llvm::ModuleFlagMergeBehavior::Min,
542 _ => unreachable!(),
544 };
545 llvm::add_module_flag_u32(llmod, merge_behavior, key, *value);
546 }
547
548 llmod
549}
550
551impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
552 pub(crate) fn new(
553 tcx: TyCtxt<'tcx>,
554 codegen_unit: &'tcx CodegenUnit<'tcx>,
555 llvm_module: &'ll crate::ModuleLlvm,
556 ) -> Self {
557 let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
610
611 let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
612
613 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
614
615 let coverage_cx =
616 tcx.sess.instrument_coverage().then(coverageinfo::CguCoverageContext::new);
617
618 let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
619 let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);
620 debuginfo::metadata::build_compile_unit_di_node(
621 tcx,
622 codegen_unit.name().as_str(),
623 &dctx,
624 );
625 Some(dctx)
626 } else {
627 None
628 };
629
630 GenericCx(
631 FullCx {
632 tcx,
633 scx: SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size()),
634 use_dll_storage_attrs,
635 tls_model,
636 codegen_unit,
637 instances: Default::default(),
638 vtables: Default::default(),
639 const_str_cache: Default::default(),
640 const_globals: Default::default(),
641 statics_to_rauw: RefCell::new(Vec::new()),
642 used_statics: Vec::new(),
643 compiler_used_statics: Vec::new(),
644 type_lowering: Default::default(),
645 scalar_lltypes: Default::default(),
646 coverage_cx,
647 dbg_cx,
648 eh_personality: Cell::new(None),
649 eh_catch_typeinfo: Cell::new(None),
650 rust_try_fn: Cell::new(None),
651 intrinsics: Default::default(),
652 local_gen_sym_counter: Cell::new(0),
653 renamed_statics: Default::default(),
654 },
655 PhantomData,
656 )
657 }
658
659 pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
660 &self.statics_to_rauw
661 }
662
663 #[inline]
665 #[track_caller]
666 pub(crate) fn coverage_cx(&self) -> &coverageinfo::CguCoverageContext<'ll, 'tcx> {
667 self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
668 }
669
670 pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
671 let array = self.const_array(self.type_ptr(), values);
672
673 let g = llvm::add_global(self.llmod, self.val_ty(array), name);
674 llvm::set_initializer(g, array);
675 llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
676 llvm::set_section(g, c"llvm.metadata");
677 }
678}
679impl<'ll> SimpleCx<'ll> {
680 pub(crate) fn get_type_of_global(&self, val: &'ll Value) -> &'ll Type {
681 unsafe { llvm::LLVMGlobalGetValueType(val) }
682 }
683 pub(crate) fn val_ty(&self, v: &'ll Value) -> &'ll Type {
684 common::val_ty(v)
685 }
686}
687impl<'ll> SimpleCx<'ll> {
688 pub(crate) fn new(
689 llmod: &'ll llvm::Module,
690 llcx: &'ll llvm::Context,
691 pointer_size: Size,
692 ) -> Self {
693 let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
694 Self(SCx { llmod, llcx, isize_ty }, PhantomData)
695 }
696}
697
698impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
699 pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
700 llvm::LLVMMetadataAsValue(self.llcx(), metadata)
701 }
702
703 pub(crate) fn get_const_int(&self, ty: &'ll Type, val: u64) -> &'ll Value {
704 unsafe { llvm::LLVMConstInt(ty, val, llvm::FALSE) }
705 }
706
707 pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {
708 self.get_const_int(self.type_i64(), n)
709 }
710
711 pub(crate) fn get_const_i32(&self, n: u64) -> &'ll Value {
712 self.get_const_int(self.type_i32(), n)
713 }
714
715 pub(crate) fn get_const_i16(&self, n: u64) -> &'ll Value {
716 self.get_const_int(self.type_i16(), n)
717 }
718
719 pub(crate) fn get_const_i8(&self, n: u64) -> &'ll Value {
720 self.get_const_int(self.type_i8(), n)
721 }
722
723 pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
724 let name = SmallCStr::new(name);
725 unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }
726 }
727
728 pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId {
729 unsafe {
730 llvm::LLVMGetMDKindIDInContext(
731 self.llcx(),
732 name.as_ptr() as *const c_char,
733 name.len() as c_uint,
734 )
735 }
736 }
737
738 pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata {
739 unsafe {
740 llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
741 }
742 }
743}
744
745impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
746 fn vtables(
747 &self,
748 ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>> {
749 &self.vtables
750 }
751
752 fn apply_vcall_visibility_metadata(
753 &self,
754 ty: Ty<'tcx>,
755 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
756 vtable: &'ll Value,
757 ) {
758 apply_vcall_visibility_metadata(self, ty, poly_trait_ref, vtable);
759 }
760
761 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
762 get_fn(self, instance)
763 }
764
765 fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
766 get_fn(self, instance)
767 }
768
769 fn eh_personality(&self) -> &'ll Value {
770 if let Some(llpersonality) = self.eh_personality.get() {
791 return llpersonality;
792 }
793
794 let name = if wants_msvc_seh(self.sess()) {
795 Some("__CxxFrameHandler3")
796 } else if wants_wasm_eh(self.sess()) {
797 Some("__gxx_wasm_personality_v0")
802 } else {
803 None
804 };
805
806 let tcx = self.tcx;
807 let llfn = match tcx.lang_items().eh_personality() {
808 Some(def_id) if name.is_none() => self.get_fn_addr(ty::Instance::expect_resolve(
809 tcx,
810 self.typing_env(),
811 def_id,
812 ty::List::empty(),
813 DUMMY_SP,
814 )),
815 _ => {
816 let name = name.unwrap_or("rust_eh_personality");
817 if let Some(llfn) = self.get_declared_value(name) {
818 llfn
819 } else {
820 let fty = self.type_variadic_func(&[], self.type_i32());
821 let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
822 let target_cpu = attributes::target_cpu_attr(self);
823 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
824 llfn
825 }
826 }
827 };
828 self.eh_personality.set(Some(llfn));
829 llfn
830 }
831
832 fn sess(&self) -> &Session {
833 self.tcx.sess
834 }
835
836 fn set_frame_pointer_type(&self, llfn: &'ll Value) {
837 if let Some(attr) = attributes::frame_pointer_type_attr(self) {
838 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
839 }
840 }
841
842 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
843 let mut attrs = SmallVec::<[_; 2]>::new();
844 attrs.push(attributes::target_cpu_attr(self));
845 attrs.extend(attributes::tune_cpu_attr(self));
846 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
847 }
848
849 fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
850 let entry_name = self.sess().target.entry_name.as_ref();
851 if self.get_declared_value(entry_name).is_none() {
852 Some(self.declare_entry_fn(
853 entry_name,
854 llvm::CallConv::from_conv(
855 self.sess().target.entry_abi,
856 self.sess().target.arch.borrow(),
857 ),
858 llvm::UnnamedAddr::Global,
859 fn_type,
860 ))
861 } else {
862 None
865 }
866 }
867}
868
869impl<'ll> CodegenCx<'ll, '_> {
870 pub(crate) fn get_intrinsic(
871 &self,
872 base_name: Cow<'static, str>,
873 type_params: &[&'ll Type],
874 ) -> (&'ll Type, &'ll Value) {
875 *self
876 .intrinsics
877 .borrow_mut()
878 .entry((base_name, SmallVec::from_slice(type_params)))
879 .or_insert_with_key(|(base_name, type_params)| {
880 self.declare_intrinsic(base_name, type_params)
881 })
882 }
883
884 fn declare_intrinsic(
885 &self,
886 base_name: &str,
887 type_params: &[&'ll Type],
888 ) -> (&'ll Type, &'ll Value) {
889 if base_name == "memcmp" {
893 let fn_ty = self
894 .type_func(&[self.type_ptr(), self.type_ptr(), self.type_isize()], self.type_int());
895 let f = self.declare_cfn("memcmp", llvm::UnnamedAddr::No, fn_ty);
896
897 return (fn_ty, f);
898 }
899
900 let intrinsic = llvm::Intrinsic::lookup(base_name.as_bytes())
901 .unwrap_or_else(|| bug!("Unknown intrinsic: `{base_name}`"));
902 let f = intrinsic.get_declaration(self.llmod, &type_params);
903
904 (self.get_type_of_global(f), f)
905 }
906
907 pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
908 if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
909 return eh_catch_typeinfo;
910 }
911 let tcx = self.tcx;
912 assert!(self.sess().target.os == "emscripten");
913 let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
914 Some(def_id) => self.get_static(def_id),
915 _ => {
916 let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
917 self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
918 }
919 };
920 self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
921 eh_catch_typeinfo
922 }
923}
924
925impl CodegenCx<'_, '_> {
926 pub(crate) fn generate_local_symbol_name(&self, prefix: &str) -> String {
929 let idx = self.local_gen_sym_counter.get();
930 self.local_gen_sym_counter.set(idx + 1);
931 let mut name = String::with_capacity(prefix.len() + 6);
934 name.push_str(prefix);
935 name.push('.');
936 name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
937 name
938 }
939}
940
941impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
942 pub(crate) fn set_metadata<'a>(
944 &self,
945 val: &'a Value,
946 kind_id: impl Into<llvm::MetadataKindId>,
947 md: &'ll Metadata,
948 ) {
949 let node = self.get_metadata_value(md);
950 llvm::LLVMSetMetadata(val, kind_id.into(), node);
951 }
952}
953
954impl HasDataLayout for CodegenCx<'_, '_> {
955 #[inline]
956 fn data_layout(&self) -> &TargetDataLayout {
957 &self.tcx.data_layout
958 }
959}
960
961impl HasTargetSpec for CodegenCx<'_, '_> {
962 #[inline]
963 fn target_spec(&self) -> &Target {
964 &self.tcx.sess.target
965 }
966}
967
968impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
969 #[inline]
970 fn tcx(&self) -> TyCtxt<'tcx> {
971 self.tcx
972 }
973}
974
975impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
976 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
977 ty::TypingEnv::fully_monomorphized()
978 }
979}
980
981impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
982 #[inline]
983 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
984 if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
985 self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
986 } else {
987 self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
988 }
989 }
990}
991
992impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
993 #[inline]
994 fn handle_fn_abi_err(
995 &self,
996 err: FnAbiError<'tcx>,
997 span: Span,
998 fn_abi_request: FnAbiRequest<'tcx>,
999 ) -> ! {
1000 match err {
1001 FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
1002 self.tcx.dcx().emit_fatal(Spanned { span, node: err });
1003 }
1004 _ => match fn_abi_request {
1005 FnAbiRequest::OfFnPtr { sig, extra_args } => {
1006 span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
1007 }
1008 FnAbiRequest::OfInstance { instance, extra_args } => {
1009 span_bug!(
1010 span,
1011 "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
1012 );
1013 }
1014 },
1015 }
1016 }
1017}