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::common::TypeKind;
12use rustc_codegen_ssa::errors as ssa_errors;
13use rustc_codegen_ssa::traits::*;
14use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, ToBaseN};
15use rustc_data_structures::fx::FxHashMap;
16use rustc_data_structures::small_c_str::SmallCStr;
17use rustc_hir::def_id::DefId;
18use rustc_middle::middle::codegen_fn_attrs::PatchableFunctionEntry;
19use rustc_middle::mir::mono::CodegenUnit;
20use rustc_middle::ty::layout::{
21 FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
22};
23use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
24use rustc_middle::{bug, span_bug};
25use rustc_session::Session;
26use rustc_session::config::{
27 BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, FunctionReturn, PAuthKey, PacRet,
28};
29use rustc_span::source_map::Spanned;
30use rustc_span::{DUMMY_SP, Span};
31use rustc_symbol_mangling::mangle_internal_symbol;
32use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
33use smallvec::SmallVec;
34
35use crate::back::write::to_llvm_code_model;
36use crate::callee::get_fn;
37use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
38use crate::llvm::Metadata;
39use crate::type_::Type;
40use crate::value::Value;
41use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
42
43pub(crate) struct SCx<'ll> {
48 pub llmod: &'ll llvm::Module,
49 pub llcx: &'ll llvm::Context,
50 pub isize_ty: &'ll Type,
51}
52
53impl<'ll> Borrow<SCx<'ll>> for FullCx<'ll, '_> {
54 fn borrow(&self) -> &SCx<'ll> {
55 &self.scx
56 }
57}
58
59impl<'ll, 'tcx> Deref for FullCx<'ll, 'tcx> {
60 type Target = SimpleCx<'ll>;
61
62 #[inline]
63 fn deref(&self) -> &Self::Target {
64 &self.scx
65 }
66}
67
68pub(crate) struct GenericCx<'ll, T: Borrow<SCx<'ll>>>(T, PhantomData<SCx<'ll>>);
69
70impl<'ll, T: Borrow<SCx<'ll>>> Deref for GenericCx<'ll, T> {
71 type Target = T;
72
73 #[inline]
74 fn deref(&self) -> &Self::Target {
75 &self.0
76 }
77}
78
79impl<'ll, T: Borrow<SCx<'ll>>> DerefMut for GenericCx<'ll, T> {
80 #[inline]
81 fn deref_mut(&mut self) -> &mut Self::Target {
82 &mut self.0
83 }
84}
85
86pub(crate) type SimpleCx<'ll> = GenericCx<'ll, SCx<'ll>>;
87
88pub(crate) type CodegenCx<'ll, 'tcx> = GenericCx<'ll, FullCx<'ll, 'tcx>>;
92
93pub(crate) struct FullCx<'ll, 'tcx> {
94 pub tcx: TyCtxt<'tcx>,
95 pub scx: SimpleCx<'ll>,
96 pub use_dll_storage_attrs: bool,
97 pub tls_model: llvm::ThreadLocalMode,
98
99 pub codegen_unit: &'tcx CodegenUnit<'tcx>,
100
101 pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
103 pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>>,
105 pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
107
108 pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
110
111 pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
116
117 pub used_statics: Vec<&'ll Value>,
120
121 pub compiler_used_statics: Vec<&'ll Value>,
124
125 pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
127
128 pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
130
131 pub coverage_cx: Option<coverageinfo::CguCoverageContext<'ll, 'tcx>>,
133 pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
134
135 eh_personality: Cell<Option<&'ll Value>>,
136 eh_catch_typeinfo: Cell<Option<&'ll Value>>,
137 pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,
138
139 intrinsics:
140 RefCell<FxHashMap<(Cow<'static, str>, SmallVec<[&'ll Type; 2]>), (&'ll Type, &'ll Value)>>,
141
142 local_gen_sym_counter: Cell<usize>,
144
145 pub renamed_statics: RefCell<FxHashMap<DefId, &'ll Value>>,
150}
151
152fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
153 match tls_model {
154 TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
155 TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
156 TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
157 TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
158 TlsModel::Emulated => llvm::ThreadLocalMode::GeneralDynamic,
159 }
160}
161
162pub(crate) unsafe fn create_module<'ll>(
163 tcx: TyCtxt<'_>,
164 llcx: &'ll llvm::Context,
165 mod_name: &str,
166) -> &'ll llvm::Module {
167 let sess = tcx.sess;
168 let mod_name = SmallCStr::new(mod_name);
169 let llmod = unsafe { llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx) };
170
171 let cx = SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size());
172
173 let mut target_data_layout = sess.target.data_layout.to_string();
174 let llvm_version = llvm_util::get_version();
175
176 if llvm_version < (20, 0, 0) {
177 if sess.target.arch == "aarch64" || sess.target.arch.starts_with("arm64") {
178 target_data_layout =
182 target_data_layout.replace("-p270:32:32-p271:32:32-p272:64:64", "");
183 }
184 if sess.target.arch.starts_with("sparc") {
185 target_data_layout = target_data_layout.replace("-i128:128", "");
188 }
189 if sess.target.arch.starts_with("mips64") {
190 target_data_layout = target_data_layout.replace("-i128:128", "");
193 }
194 if sess.target.arch.starts_with("powerpc64") {
195 target_data_layout = target_data_layout.replace("-i128:128", "");
198 }
199 if sess.target.arch.starts_with("wasm32") || sess.target.arch.starts_with("wasm64") {
200 target_data_layout = target_data_layout.replace("-i128:128", "");
203 }
204 }
205 if llvm_version < (21, 0, 0) {
206 if sess.target.arch == "nvptx64" {
207 target_data_layout = target_data_layout.replace("e-p6:32:32-i64", "e-i64");
209 }
210 if sess.target.arch == "amdgpu" {
211 target_data_layout = target_data_layout.replace("p8:128:128:128:48", "p8:128:128")
214 }
215 }
216
217 {
219 let tm = crate::back::write::create_informational_target_machine(sess, false);
220 unsafe {
221 llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());
222 }
223
224 let llvm_data_layout = unsafe { llvm::LLVMGetDataLayoutStr(llmod) };
225 let llvm_data_layout =
226 str::from_utf8(unsafe { CStr::from_ptr(llvm_data_layout) }.to_bytes())
227 .expect("got a non-UTF8 data-layout from LLVM");
228
229 if target_data_layout != llvm_data_layout {
230 tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
231 rustc_target: sess.opts.target_triple.to_string().as_str(),
232 rustc_layout: target_data_layout.as_str(),
233 llvm_target: sess.target.llvm_target.borrow(),
234 llvm_layout: llvm_data_layout,
235 });
236 }
237 }
238
239 let data_layout = SmallCStr::new(&target_data_layout);
240 unsafe {
241 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
242 }
243
244 let llvm_target = SmallCStr::new(&versioned_llvm_target(sess));
245 unsafe {
246 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
247 }
248
249 let reloc_model = sess.relocation_model();
250 if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
251 unsafe {
252 llvm::LLVMRustSetModulePICLevel(llmod);
253 }
254 if reloc_model == RelocModel::Pie
257 || tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable)
258 {
259 unsafe {
260 llvm::LLVMRustSetModulePIELevel(llmod);
261 }
262 }
263 }
264
265 unsafe {
271 llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
272 }
273
274 if !sess.needs_plt() {
277 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "RtLibUseGOT", 1);
278 }
279
280 if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
282 llvm::add_module_flag_u32(
283 llmod,
284 llvm::ModuleFlagMergeBehavior::Override,
285 "CFI Canonical Jump Tables",
286 1,
287 );
288 }
289
290 if sess.is_sanitizer_cfi_normalize_integers_enabled() {
293 llvm::add_module_flag_u32(
294 llmod,
295 llvm::ModuleFlagMergeBehavior::Override,
296 "cfi-normalize-integers",
297 1,
298 );
299 }
300
301 if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
304 llvm::add_module_flag_u32(
305 llmod,
306 llvm::ModuleFlagMergeBehavior::Override,
307 "EnableSplitLTOUnit",
308 1,
309 );
310 }
311
312 if sess.is_sanitizer_kcfi_enabled() {
314 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);
315
316 let pfe =
319 PatchableFunctionEntry::from_config(sess.opts.unstable_opts.patchable_function_entry);
320 if pfe.prefix() > 0 {
321 llvm::add_module_flag_u32(
322 llmod,
323 llvm::ModuleFlagMergeBehavior::Override,
324 "kcfi-offset",
325 pfe.prefix().into(),
326 );
327 }
328
329 if sess.is_sanitizer_kcfi_arity_enabled() {
332 if llvm_version < (21, 0, 0) {
334 tcx.dcx().emit_err(crate::errors::SanitizerKcfiArityRequiresLLVM2100);
335 }
336
337 llvm::add_module_flag_u32(
338 llmod,
339 llvm::ModuleFlagMergeBehavior::Override,
340 "kcfi-arity",
341 1,
342 );
343 }
344 }
345
346 if sess.target.is_like_msvc
348 || (sess.target.options.os == "windows"
349 && sess.target.options.env == "gnu"
350 && sess.target.options.abi == "llvm")
351 {
352 match sess.opts.cg.control_flow_guard {
353 CFGuard::Disabled => {}
354 CFGuard::NoChecks => {
355 llvm::add_module_flag_u32(
357 llmod,
358 llvm::ModuleFlagMergeBehavior::Warning,
359 "cfguard",
360 1,
361 );
362 }
363 CFGuard::Checks => {
364 llvm::add_module_flag_u32(
366 llmod,
367 llvm::ModuleFlagMergeBehavior::Warning,
368 "cfguard",
369 2,
370 );
371 }
372 }
373 }
374
375 if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
376 if sess.target.arch == "aarch64" {
377 llvm::add_module_flag_u32(
378 llmod,
379 llvm::ModuleFlagMergeBehavior::Min,
380 "branch-target-enforcement",
381 bti.into(),
382 );
383 llvm::add_module_flag_u32(
384 llmod,
385 llvm::ModuleFlagMergeBehavior::Min,
386 "sign-return-address",
387 pac_ret.is_some().into(),
388 );
389 let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, pc: false, key: PAuthKey::A });
390 llvm::add_module_flag_u32(
391 llmod,
392 llvm::ModuleFlagMergeBehavior::Min,
393 "branch-protection-pauth-lr",
394 pac_opts.pc.into(),
395 );
396 llvm::add_module_flag_u32(
397 llmod,
398 llvm::ModuleFlagMergeBehavior::Min,
399 "sign-return-address-all",
400 pac_opts.leaf.into(),
401 );
402 llvm::add_module_flag_u32(
403 llmod,
404 llvm::ModuleFlagMergeBehavior::Min,
405 "sign-return-address-with-bkey",
406 u32::from(pac_opts.key == PAuthKey::B),
407 );
408 } else {
409 bug!(
410 "branch-protection used on non-AArch64 target; \
411 this should be checked in rustc_session."
412 );
413 }
414 }
415
416 if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
418 llvm::add_module_flag_u32(
419 llmod,
420 llvm::ModuleFlagMergeBehavior::Override,
421 "cf-protection-branch",
422 1,
423 );
424 }
425 if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
426 llvm::add_module_flag_u32(
427 llmod,
428 llvm::ModuleFlagMergeBehavior::Override,
429 "cf-protection-return",
430 1,
431 );
432 }
433
434 if sess.opts.unstable_opts.virtual_function_elimination {
435 llvm::add_module_flag_u32(
436 llmod,
437 llvm::ModuleFlagMergeBehavior::Error,
438 "Virtual Function Elim",
439 1,
440 );
441 }
442
443 if sess.opts.unstable_opts.ehcont_guard {
445 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "ehcontguard", 1);
446 }
447
448 match sess.opts.unstable_opts.function_return {
449 FunctionReturn::Keep => {}
450 FunctionReturn::ThunkExtern => {
451 llvm::add_module_flag_u32(
452 llmod,
453 llvm::ModuleFlagMergeBehavior::Override,
454 "function_return_thunk_extern",
455 1,
456 );
457 }
458 }
459
460 match (sess.opts.unstable_opts.small_data_threshold, sess.target.small_data_threshold_support())
461 {
462 (Some(threshold), SmallDataThresholdSupport::LlvmModuleFlag(flag)) => {
465 llvm::add_module_flag_u32(
466 llmod,
467 llvm::ModuleFlagMergeBehavior::Error,
468 &flag,
469 threshold as u32,
470 );
471 }
472 _ => (),
473 };
474
475 #[allow(clippy::option_env_unwrap)]
480 let rustc_producer =
481 format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
482
483 let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
484
485 unsafe {
486 llvm::LLVMAddNamedMetadataOperand(
487 llmod,
488 c"llvm.ident".as_ptr(),
489 &cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
490 );
491 }
492
493 let llvm_abiname = &sess.target.options.llvm_abiname;
499 if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
500 llvm::add_module_flag_str(
501 llmod,
502 llvm::ModuleFlagMergeBehavior::Error,
503 "target-abi",
504 llvm_abiname,
505 );
506 }
507
508 for (key, value, merge_behavior) in &sess.opts.unstable_opts.llvm_module_flag {
510 let merge_behavior = match merge_behavior.as_str() {
511 "error" => llvm::ModuleFlagMergeBehavior::Error,
512 "warning" => llvm::ModuleFlagMergeBehavior::Warning,
513 "require" => llvm::ModuleFlagMergeBehavior::Require,
514 "override" => llvm::ModuleFlagMergeBehavior::Override,
515 "append" => llvm::ModuleFlagMergeBehavior::Append,
516 "appendunique" => llvm::ModuleFlagMergeBehavior::AppendUnique,
517 "max" => llvm::ModuleFlagMergeBehavior::Max,
518 "min" => llvm::ModuleFlagMergeBehavior::Min,
519 _ => unreachable!(),
521 };
522 llvm::add_module_flag_u32(llmod, merge_behavior, key, *value);
523 }
524
525 llmod
526}
527
528impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
529 pub(crate) fn new(
530 tcx: TyCtxt<'tcx>,
531 codegen_unit: &'tcx CodegenUnit<'tcx>,
532 llvm_module: &'ll crate::ModuleLlvm,
533 ) -> Self {
534 let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
587
588 let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
589
590 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
591
592 let coverage_cx =
593 tcx.sess.instrument_coverage().then(coverageinfo::CguCoverageContext::new);
594
595 let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
596 let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);
597 debuginfo::metadata::build_compile_unit_di_node(
598 tcx,
599 codegen_unit.name().as_str(),
600 &dctx,
601 );
602 Some(dctx)
603 } else {
604 None
605 };
606
607 GenericCx(
608 FullCx {
609 tcx,
610 scx: SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size()),
611 use_dll_storage_attrs,
612 tls_model,
613 codegen_unit,
614 instances: Default::default(),
615 vtables: Default::default(),
616 const_str_cache: Default::default(),
617 const_globals: Default::default(),
618 statics_to_rauw: RefCell::new(Vec::new()),
619 used_statics: Vec::new(),
620 compiler_used_statics: Vec::new(),
621 type_lowering: Default::default(),
622 scalar_lltypes: Default::default(),
623 coverage_cx,
624 dbg_cx,
625 eh_personality: Cell::new(None),
626 eh_catch_typeinfo: Cell::new(None),
627 rust_try_fn: Cell::new(None),
628 intrinsics: Default::default(),
629 local_gen_sym_counter: Cell::new(0),
630 renamed_statics: Default::default(),
631 },
632 PhantomData,
633 )
634 }
635
636 pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
637 &self.statics_to_rauw
638 }
639
640 #[inline]
642 #[track_caller]
643 pub(crate) fn coverage_cx(&self) -> &coverageinfo::CguCoverageContext<'ll, 'tcx> {
644 self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
645 }
646
647 pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
648 let array = self.const_array(self.type_ptr(), values);
649
650 let g = llvm::add_global(self.llmod, self.val_ty(array), name);
651 llvm::set_initializer(g, array);
652 llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
653 llvm::set_section(g, c"llvm.metadata");
654 }
655}
656impl<'ll> SimpleCx<'ll> {
657 pub(crate) fn get_return_type(&self, ty: &'ll Type) -> &'ll Type {
658 assert_eq!(self.type_kind(ty), TypeKind::Function);
659 unsafe { llvm::LLVMGetReturnType(ty) }
660 }
661 pub(crate) fn get_type_of_global(&self, val: &'ll Value) -> &'ll Type {
662 unsafe { llvm::LLVMGlobalGetValueType(val) }
663 }
664 pub(crate) fn val_ty(&self, v: &'ll Value) -> &'ll Type {
665 common::val_ty(v)
666 }
667}
668impl<'ll> SimpleCx<'ll> {
669 pub(crate) fn new(
670 llmod: &'ll llvm::Module,
671 llcx: &'ll llvm::Context,
672 pointer_size: Size,
673 ) -> Self {
674 let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
675 Self(SCx { llmod, llcx, isize_ty }, PhantomData)
676 }
677}
678
679impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
680 pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
681 llvm::LLVMMetadataAsValue(self.llcx(), metadata)
682 }
683
684 pub(crate) fn get_const_int(&self, ty: &'ll Type, val: u64) -> &'ll Value {
685 unsafe { llvm::LLVMConstInt(ty, val, llvm::False) }
686 }
687
688 pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {
689 self.get_const_int(self.type_i64(), n)
690 }
691
692 pub(crate) fn get_const_i32(&self, n: u64) -> &'ll Value {
693 self.get_const_int(self.type_i32(), n)
694 }
695
696 pub(crate) fn get_const_i16(&self, n: u64) -> &'ll Value {
697 self.get_const_int(self.type_i16(), n)
698 }
699
700 pub(crate) fn get_const_i8(&self, n: u64) -> &'ll Value {
701 self.get_const_int(self.type_i8(), n)
702 }
703
704 pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
705 let name = SmallCStr::new(name);
706 unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }
707 }
708
709 pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId {
710 unsafe {
711 llvm::LLVMGetMDKindIDInContext(
712 self.llcx(),
713 name.as_ptr() as *const c_char,
714 name.len() as c_uint,
715 )
716 }
717 }
718
719 pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata {
720 unsafe {
721 llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
722 }
723 }
724
725 pub(crate) fn get_functions(&self) -> Vec<&'ll Value> {
726 let mut functions = vec![];
727 let mut func = unsafe { llvm::LLVMGetFirstFunction(self.llmod()) };
728 while let Some(f) = func {
729 functions.push(f);
730 func = unsafe { llvm::LLVMGetNextFunction(f) }
731 }
732 functions
733 }
734}
735
736impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
737 fn vtables(
738 &self,
739 ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>> {
740 &self.vtables
741 }
742
743 fn apply_vcall_visibility_metadata(
744 &self,
745 ty: Ty<'tcx>,
746 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
747 vtable: &'ll Value,
748 ) {
749 apply_vcall_visibility_metadata(self, ty, poly_trait_ref, vtable);
750 }
751
752 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
753 get_fn(self, instance)
754 }
755
756 fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
757 get_fn(self, instance)
758 }
759
760 fn eh_personality(&self) -> &'ll Value {
761 if let Some(llpersonality) = self.eh_personality.get() {
782 return llpersonality;
783 }
784
785 let name = if wants_msvc_seh(self.sess()) {
786 Some("__CxxFrameHandler3")
787 } else if wants_wasm_eh(self.sess()) {
788 Some("__gxx_wasm_personality_v0")
793 } else {
794 None
795 };
796
797 let tcx = self.tcx;
798 let llfn = match tcx.lang_items().eh_personality() {
799 Some(def_id) if name.is_none() => self.get_fn_addr(ty::Instance::expect_resolve(
800 tcx,
801 self.typing_env(),
802 def_id,
803 ty::List::empty(),
804 DUMMY_SP,
805 )),
806 _ => {
807 let name = name.unwrap_or("rust_eh_personality");
808 if let Some(llfn) = self.get_declared_value(name) {
809 llfn
810 } else {
811 let fty = self.type_variadic_func(&[], self.type_i32());
812 let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
813 let target_cpu = attributes::target_cpu_attr(self);
814 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
815 llfn
816 }
817 }
818 };
819 self.eh_personality.set(Some(llfn));
820 llfn
821 }
822
823 fn sess(&self) -> &Session {
824 self.tcx.sess
825 }
826
827 fn set_frame_pointer_type(&self, llfn: &'ll Value) {
828 if let Some(attr) = attributes::frame_pointer_type_attr(self) {
829 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
830 }
831 }
832
833 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
834 let mut attrs = SmallVec::<[_; 2]>::new();
835 attrs.push(attributes::target_cpu_attr(self));
836 attrs.extend(attributes::tune_cpu_attr(self));
837 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
838 }
839
840 fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
841 let entry_name = self.sess().target.entry_name.as_ref();
842 if self.get_declared_value(entry_name).is_none() {
843 Some(self.declare_entry_fn(
844 entry_name,
845 llvm::CallConv::from_conv(
846 self.sess().target.entry_abi,
847 self.sess().target.arch.borrow(),
848 ),
849 llvm::UnnamedAddr::Global,
850 fn_type,
851 ))
852 } else {
853 None
856 }
857 }
858}
859
860impl<'ll> CodegenCx<'ll, '_> {
861 pub(crate) fn get_intrinsic(
862 &self,
863 base_name: Cow<'static, str>,
864 type_params: &[&'ll Type],
865 ) -> (&'ll Type, &'ll Value) {
866 *self
867 .intrinsics
868 .borrow_mut()
869 .entry((base_name, SmallVec::from_slice(type_params)))
870 .or_insert_with_key(|(base_name, type_params)| {
871 self.declare_intrinsic(base_name, type_params)
872 })
873 }
874
875 fn declare_intrinsic(
876 &self,
877 base_name: &str,
878 type_params: &[&'ll Type],
879 ) -> (&'ll Type, &'ll Value) {
880 if base_name == "memcmp" {
884 let fn_ty = self
885 .type_func(&[self.type_ptr(), self.type_ptr(), self.type_isize()], self.type_int());
886 let f = self.declare_cfn("memcmp", llvm::UnnamedAddr::No, fn_ty);
887
888 return (fn_ty, f);
889 }
890
891 let intrinsic = llvm::Intrinsic::lookup(base_name.as_bytes())
892 .unwrap_or_else(|| bug!("Unknown intrinsic: `{base_name}`"));
893 let f = intrinsic.get_declaration(self.llmod, &type_params);
894
895 (self.get_type_of_global(f), f)
896 }
897
898 pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
899 if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
900 return eh_catch_typeinfo;
901 }
902 let tcx = self.tcx;
903 assert!(self.sess().target.os == "emscripten");
904 let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
905 Some(def_id) => self.get_static(def_id),
906 _ => {
907 let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
908 self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
909 }
910 };
911 self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
912 eh_catch_typeinfo
913 }
914}
915
916impl CodegenCx<'_, '_> {
917 pub(crate) fn generate_local_symbol_name(&self, prefix: &str) -> String {
920 let idx = self.local_gen_sym_counter.get();
921 self.local_gen_sym_counter.set(idx + 1);
922 let mut name = String::with_capacity(prefix.len() + 6);
925 name.push_str(prefix);
926 name.push('.');
927 name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
928 name
929 }
930}
931
932impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
933 pub(crate) fn set_metadata<'a>(
935 &self,
936 val: &'a Value,
937 kind_id: impl Into<llvm::MetadataKindId>,
938 md: &'ll Metadata,
939 ) {
940 let node = self.get_metadata_value(md);
941 llvm::LLVMSetMetadata(val, kind_id.into(), node);
942 }
943}
944
945impl HasDataLayout for CodegenCx<'_, '_> {
946 #[inline]
947 fn data_layout(&self) -> &TargetDataLayout {
948 &self.tcx.data_layout
949 }
950}
951
952impl HasTargetSpec for CodegenCx<'_, '_> {
953 #[inline]
954 fn target_spec(&self) -> &Target {
955 &self.tcx.sess.target
956 }
957}
958
959impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
960 #[inline]
961 fn tcx(&self) -> TyCtxt<'tcx> {
962 self.tcx
963 }
964}
965
966impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
967 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
968 ty::TypingEnv::fully_monomorphized()
969 }
970}
971
972impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
973 #[inline]
974 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
975 if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
976 self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
977 } else {
978 self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
979 }
980 }
981}
982
983impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
984 #[inline]
985 fn handle_fn_abi_err(
986 &self,
987 err: FnAbiError<'tcx>,
988 span: Span,
989 fn_abi_request: FnAbiRequest<'tcx>,
990 ) -> ! {
991 match err {
992 FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
993 self.tcx.dcx().emit_fatal(Spanned { span, node: err });
994 }
995 _ => match fn_abi_request {
996 FnAbiRequest::OfFnPtr { sig, extra_args } => {
997 span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
998 }
999 FnAbiRequest::OfInstance { instance, extra_args } => {
1000 span_bug!(
1001 span,
1002 "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
1003 );
1004 }
1005 },
1006 }
1007 }
1008}