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 if sess.target.arch == "nvptx64" {
221 target_data_layout = target_data_layout.replace("-i256:256", "");
223 }
224 }
225
226 {
228 let tm = crate::back::write::create_informational_target_machine(sess, false);
229 unsafe {
230 llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());
231 }
232
233 let llvm_data_layout = unsafe { llvm::LLVMGetDataLayoutStr(llmod) };
234 let llvm_data_layout =
235 str::from_utf8(unsafe { CStr::from_ptr(llvm_data_layout) }.to_bytes())
236 .expect("got a non-UTF8 data-layout from LLVM");
237
238 if target_data_layout != llvm_data_layout {
239 tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
240 rustc_target: sess.opts.target_triple.to_string().as_str(),
241 rustc_layout: target_data_layout.as_str(),
242 llvm_target: sess.target.llvm_target.borrow(),
243 llvm_layout: llvm_data_layout,
244 });
245 }
246 }
247
248 let data_layout = SmallCStr::new(&target_data_layout);
249 unsafe {
250 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
251 }
252
253 let llvm_target = SmallCStr::new(&versioned_llvm_target(sess));
254 unsafe {
255 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
256 }
257
258 let reloc_model = sess.relocation_model();
259 if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
260 unsafe {
261 llvm::LLVMRustSetModulePICLevel(llmod);
262 }
263 if reloc_model == RelocModel::Pie
266 || tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable)
267 {
268 unsafe {
269 llvm::LLVMRustSetModulePIELevel(llmod);
270 }
271 }
272 }
273
274 unsafe {
280 llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
281 }
282
283 if !sess.needs_plt() {
286 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "RtLibUseGOT", 1);
287 }
288
289 if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
291 llvm::add_module_flag_u32(
292 llmod,
293 llvm::ModuleFlagMergeBehavior::Override,
294 "CFI Canonical Jump Tables",
295 1,
296 );
297 }
298
299 if sess.is_sanitizer_cfi_normalize_integers_enabled() {
302 llvm::add_module_flag_u32(
303 llmod,
304 llvm::ModuleFlagMergeBehavior::Override,
305 "cfi-normalize-integers",
306 1,
307 );
308 }
309
310 if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
313 llvm::add_module_flag_u32(
314 llmod,
315 llvm::ModuleFlagMergeBehavior::Override,
316 "EnableSplitLTOUnit",
317 1,
318 );
319 }
320
321 if sess.is_sanitizer_kcfi_enabled() {
323 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);
324
325 let pfe =
328 PatchableFunctionEntry::from_config(sess.opts.unstable_opts.patchable_function_entry);
329 if pfe.prefix() > 0 {
330 llvm::add_module_flag_u32(
331 llmod,
332 llvm::ModuleFlagMergeBehavior::Override,
333 "kcfi-offset",
334 pfe.prefix().into(),
335 );
336 }
337
338 if sess.is_sanitizer_kcfi_arity_enabled() {
341 if llvm_version < (21, 0, 0) {
343 tcx.dcx().emit_err(crate::errors::SanitizerKcfiArityRequiresLLVM2100);
344 }
345
346 llvm::add_module_flag_u32(
347 llmod,
348 llvm::ModuleFlagMergeBehavior::Override,
349 "kcfi-arity",
350 1,
351 );
352 }
353 }
354
355 if sess.target.is_like_msvc
357 || (sess.target.options.os == "windows"
358 && sess.target.options.env == "gnu"
359 && sess.target.options.abi == "llvm")
360 {
361 match sess.opts.cg.control_flow_guard {
362 CFGuard::Disabled => {}
363 CFGuard::NoChecks => {
364 llvm::add_module_flag_u32(
366 llmod,
367 llvm::ModuleFlagMergeBehavior::Warning,
368 "cfguard",
369 1,
370 );
371 }
372 CFGuard::Checks => {
373 llvm::add_module_flag_u32(
375 llmod,
376 llvm::ModuleFlagMergeBehavior::Warning,
377 "cfguard",
378 2,
379 );
380 }
381 }
382 }
383
384 if let Some(regparm_count) = sess.opts.unstable_opts.regparm {
385 llvm::add_module_flag_u32(
386 llmod,
387 llvm::ModuleFlagMergeBehavior::Error,
388 "NumRegisterParameters",
389 regparm_count,
390 );
391 }
392
393 if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
394 if sess.target.arch == "aarch64" {
395 llvm::add_module_flag_u32(
396 llmod,
397 llvm::ModuleFlagMergeBehavior::Min,
398 "branch-target-enforcement",
399 bti.into(),
400 );
401 llvm::add_module_flag_u32(
402 llmod,
403 llvm::ModuleFlagMergeBehavior::Min,
404 "sign-return-address",
405 pac_ret.is_some().into(),
406 );
407 let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, pc: false, key: PAuthKey::A });
408 llvm::add_module_flag_u32(
409 llmod,
410 llvm::ModuleFlagMergeBehavior::Min,
411 "branch-protection-pauth-lr",
412 pac_opts.pc.into(),
413 );
414 llvm::add_module_flag_u32(
415 llmod,
416 llvm::ModuleFlagMergeBehavior::Min,
417 "sign-return-address-all",
418 pac_opts.leaf.into(),
419 );
420 llvm::add_module_flag_u32(
421 llmod,
422 llvm::ModuleFlagMergeBehavior::Min,
423 "sign-return-address-with-bkey",
424 u32::from(pac_opts.key == PAuthKey::B),
425 );
426 } else {
427 bug!(
428 "branch-protection used on non-AArch64 target; \
429 this should be checked in rustc_session."
430 );
431 }
432 }
433
434 if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
436 llvm::add_module_flag_u32(
437 llmod,
438 llvm::ModuleFlagMergeBehavior::Override,
439 "cf-protection-branch",
440 1,
441 );
442 }
443 if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
444 llvm::add_module_flag_u32(
445 llmod,
446 llvm::ModuleFlagMergeBehavior::Override,
447 "cf-protection-return",
448 1,
449 );
450 }
451
452 if sess.opts.unstable_opts.virtual_function_elimination {
453 llvm::add_module_flag_u32(
454 llmod,
455 llvm::ModuleFlagMergeBehavior::Error,
456 "Virtual Function Elim",
457 1,
458 );
459 }
460
461 if sess.opts.unstable_opts.ehcont_guard {
463 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "ehcontguard", 1);
464 }
465
466 match sess.opts.unstable_opts.function_return {
467 FunctionReturn::Keep => {}
468 FunctionReturn::ThunkExtern => {
469 llvm::add_module_flag_u32(
470 llmod,
471 llvm::ModuleFlagMergeBehavior::Override,
472 "function_return_thunk_extern",
473 1,
474 );
475 }
476 }
477
478 if sess.opts.unstable_opts.indirect_branch_cs_prefix {
479 llvm::add_module_flag_u32(
480 llmod,
481 llvm::ModuleFlagMergeBehavior::Override,
482 "indirect_branch_cs_prefix",
483 1,
484 );
485 }
486
487 match (sess.opts.unstable_opts.small_data_threshold, sess.target.small_data_threshold_support())
488 {
489 (Some(threshold), SmallDataThresholdSupport::LlvmModuleFlag(flag)) => {
492 llvm::add_module_flag_u32(
493 llmod,
494 llvm::ModuleFlagMergeBehavior::Error,
495 &flag,
496 threshold as u32,
497 );
498 }
499 _ => (),
500 };
501
502 #[allow(clippy::option_env_unwrap)]
507 let rustc_producer =
508 format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
509
510 let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
511
512 unsafe {
513 llvm::LLVMAddNamedMetadataOperand(
514 llmod,
515 c"llvm.ident".as_ptr(),
516 &cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
517 );
518 }
519
520 let llvm_abiname = &sess.target.options.llvm_abiname;
526 if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
527 llvm::add_module_flag_str(
528 llmod,
529 llvm::ModuleFlagMergeBehavior::Error,
530 "target-abi",
531 llvm_abiname,
532 );
533 }
534
535 for (key, value, merge_behavior) in &sess.opts.unstable_opts.llvm_module_flag {
537 let merge_behavior = match merge_behavior.as_str() {
538 "error" => llvm::ModuleFlagMergeBehavior::Error,
539 "warning" => llvm::ModuleFlagMergeBehavior::Warning,
540 "require" => llvm::ModuleFlagMergeBehavior::Require,
541 "override" => llvm::ModuleFlagMergeBehavior::Override,
542 "append" => llvm::ModuleFlagMergeBehavior::Append,
543 "appendunique" => llvm::ModuleFlagMergeBehavior::AppendUnique,
544 "max" => llvm::ModuleFlagMergeBehavior::Max,
545 "min" => llvm::ModuleFlagMergeBehavior::Min,
546 _ => unreachable!(),
548 };
549 llvm::add_module_flag_u32(llmod, merge_behavior, key, *value);
550 }
551
552 llmod
553}
554
555impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
556 pub(crate) fn new(
557 tcx: TyCtxt<'tcx>,
558 codegen_unit: &'tcx CodegenUnit<'tcx>,
559 llvm_module: &'ll crate::ModuleLlvm,
560 ) -> Self {
561 let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
614
615 let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
616
617 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
618
619 let coverage_cx =
620 tcx.sess.instrument_coverage().then(coverageinfo::CguCoverageContext::new);
621
622 let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
623 let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);
624 debuginfo::metadata::build_compile_unit_di_node(
625 tcx,
626 codegen_unit.name().as_str(),
627 &dctx,
628 );
629 Some(dctx)
630 } else {
631 None
632 };
633
634 GenericCx(
635 FullCx {
636 tcx,
637 scx: SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size()),
638 use_dll_storage_attrs,
639 tls_model,
640 codegen_unit,
641 instances: Default::default(),
642 vtables: Default::default(),
643 const_str_cache: Default::default(),
644 const_globals: Default::default(),
645 statics_to_rauw: RefCell::new(Vec::new()),
646 used_statics: Vec::new(),
647 compiler_used_statics: Vec::new(),
648 type_lowering: Default::default(),
649 scalar_lltypes: Default::default(),
650 coverage_cx,
651 dbg_cx,
652 eh_personality: Cell::new(None),
653 eh_catch_typeinfo: Cell::new(None),
654 rust_try_fn: Cell::new(None),
655 intrinsics: Default::default(),
656 local_gen_sym_counter: Cell::new(0),
657 renamed_statics: Default::default(),
658 },
659 PhantomData,
660 )
661 }
662
663 pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
664 &self.statics_to_rauw
665 }
666
667 #[inline]
669 #[track_caller]
670 pub(crate) fn coverage_cx(&self) -> &coverageinfo::CguCoverageContext<'ll, 'tcx> {
671 self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
672 }
673
674 pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
675 let array = self.const_array(self.type_ptr(), values);
676
677 let g = llvm::add_global(self.llmod, self.val_ty(array), name);
678 llvm::set_initializer(g, array);
679 llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
680 llvm::set_section(g, c"llvm.metadata");
681 }
682}
683impl<'ll> SimpleCx<'ll> {
684 pub(crate) fn get_type_of_global(&self, val: &'ll Value) -> &'ll Type {
685 unsafe { llvm::LLVMGlobalGetValueType(val) }
686 }
687 pub(crate) fn val_ty(&self, v: &'ll Value) -> &'ll Type {
688 common::val_ty(v)
689 }
690}
691impl<'ll> SimpleCx<'ll> {
692 pub(crate) fn new(
693 llmod: &'ll llvm::Module,
694 llcx: &'ll llvm::Context,
695 pointer_size: Size,
696 ) -> Self {
697 let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
698 Self(SCx { llmod, llcx, isize_ty }, PhantomData)
699 }
700}
701
702impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
703 pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
704 llvm::LLVMMetadataAsValue(self.llcx(), metadata)
705 }
706
707 pub(crate) fn get_const_int(&self, ty: &'ll Type, val: u64) -> &'ll Value {
708 unsafe { llvm::LLVMConstInt(ty, val, llvm::FALSE) }
709 }
710
711 pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {
712 self.get_const_int(self.type_i64(), n)
713 }
714
715 pub(crate) fn get_const_i32(&self, n: u64) -> &'ll Value {
716 self.get_const_int(self.type_i32(), n)
717 }
718
719 pub(crate) fn get_const_i16(&self, n: u64) -> &'ll Value {
720 self.get_const_int(self.type_i16(), n)
721 }
722
723 pub(crate) fn get_const_i8(&self, n: u64) -> &'ll Value {
724 self.get_const_int(self.type_i8(), n)
725 }
726
727 pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
728 let name = SmallCStr::new(name);
729 unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }
730 }
731
732 pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId {
733 unsafe {
734 llvm::LLVMGetMDKindIDInContext(
735 self.llcx(),
736 name.as_ptr() as *const c_char,
737 name.len() as c_uint,
738 )
739 }
740 }
741
742 pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata {
743 unsafe {
744 llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
745 }
746 }
747}
748
749impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
750 fn vtables(
751 &self,
752 ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>> {
753 &self.vtables
754 }
755
756 fn apply_vcall_visibility_metadata(
757 &self,
758 ty: Ty<'tcx>,
759 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
760 vtable: &'ll Value,
761 ) {
762 apply_vcall_visibility_metadata(self, ty, poly_trait_ref, vtable);
763 }
764
765 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
766 get_fn(self, instance)
767 }
768
769 fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
770 get_fn(self, instance)
771 }
772
773 fn eh_personality(&self) -> &'ll Value {
774 if let Some(llpersonality) = self.eh_personality.get() {
795 return llpersonality;
796 }
797
798 let name = if wants_msvc_seh(self.sess()) {
799 Some("__CxxFrameHandler3")
800 } else if wants_wasm_eh(self.sess()) {
801 Some("__gxx_wasm_personality_v0")
806 } else {
807 None
808 };
809
810 let tcx = self.tcx;
811 let llfn = match tcx.lang_items().eh_personality() {
812 Some(def_id) if name.is_none() => self.get_fn_addr(ty::Instance::expect_resolve(
813 tcx,
814 self.typing_env(),
815 def_id,
816 ty::List::empty(),
817 DUMMY_SP,
818 )),
819 _ => {
820 let name = name.unwrap_or("rust_eh_personality");
821 if let Some(llfn) = self.get_declared_value(name) {
822 llfn
823 } else {
824 let fty = self.type_variadic_func(&[], self.type_i32());
825 let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
826 let target_cpu = attributes::target_cpu_attr(self);
827 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
828 llfn
829 }
830 }
831 };
832 self.eh_personality.set(Some(llfn));
833 llfn
834 }
835
836 fn sess(&self) -> &Session {
837 self.tcx.sess
838 }
839
840 fn set_frame_pointer_type(&self, llfn: &'ll Value) {
841 if let Some(attr) = attributes::frame_pointer_type_attr(self) {
842 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
843 }
844 }
845
846 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
847 let mut attrs = SmallVec::<[_; 2]>::new();
848 attrs.push(attributes::target_cpu_attr(self));
849 attrs.extend(attributes::tune_cpu_attr(self));
850 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
851 }
852
853 fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
854 let entry_name = self.sess().target.entry_name.as_ref();
855 if self.get_declared_value(entry_name).is_none() {
856 let llfn = self.declare_entry_fn(
857 entry_name,
858 llvm::CallConv::from_conv(
859 self.sess().target.entry_abi,
860 self.sess().target.arch.borrow(),
861 ),
862 llvm::UnnamedAddr::Global,
863 fn_type,
864 );
865 attributes::apply_to_llfn(
866 llfn,
867 llvm::AttributePlace::Function,
868 attributes::target_features_attr(self, vec![]).as_slice(),
869 );
870 Some(llfn)
871 } else {
872 None
875 }
876 }
877}
878
879impl<'ll> CodegenCx<'ll, '_> {
880 pub(crate) fn get_intrinsic(
881 &self,
882 base_name: Cow<'static, str>,
883 type_params: &[&'ll Type],
884 ) -> (&'ll Type, &'ll Value) {
885 *self
886 .intrinsics
887 .borrow_mut()
888 .entry((base_name, SmallVec::from_slice(type_params)))
889 .or_insert_with_key(|(base_name, type_params)| {
890 self.declare_intrinsic(base_name, type_params)
891 })
892 }
893
894 fn declare_intrinsic(
895 &self,
896 base_name: &str,
897 type_params: &[&'ll Type],
898 ) -> (&'ll Type, &'ll Value) {
899 if base_name == "memcmp" {
903 let fn_ty = self
904 .type_func(&[self.type_ptr(), self.type_ptr(), self.type_isize()], self.type_int());
905 let f = self.declare_cfn("memcmp", llvm::UnnamedAddr::No, fn_ty);
906
907 return (fn_ty, f);
908 }
909
910 let intrinsic = llvm::Intrinsic::lookup(base_name.as_bytes())
911 .unwrap_or_else(|| bug!("Unknown intrinsic: `{base_name}`"));
912 let f = intrinsic.get_declaration(self.llmod, &type_params);
913
914 (self.get_type_of_global(f), f)
915 }
916
917 pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
918 if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
919 return eh_catch_typeinfo;
920 }
921 let tcx = self.tcx;
922 assert!(self.sess().target.os == "emscripten");
923 let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
924 Some(def_id) => self.get_static(def_id),
925 _ => {
926 let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
927 self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
928 }
929 };
930 self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
931 eh_catch_typeinfo
932 }
933}
934
935impl CodegenCx<'_, '_> {
936 pub(crate) fn generate_local_symbol_name(&self, prefix: &str) -> String {
939 let idx = self.local_gen_sym_counter.get();
940 self.local_gen_sym_counter.set(idx + 1);
941 let mut name = String::with_capacity(prefix.len() + 6);
944 name.push_str(prefix);
945 name.push('.');
946 name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
947 name
948 }
949}
950
951impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
952 pub(crate) fn set_metadata<'a>(
954 &self,
955 val: &'a Value,
956 kind_id: impl Into<llvm::MetadataKindId>,
957 md: &'ll Metadata,
958 ) {
959 let node = self.get_metadata_value(md);
960 llvm::LLVMSetMetadata(val, kind_id.into(), node);
961 }
962}
963
964impl HasDataLayout for CodegenCx<'_, '_> {
965 #[inline]
966 fn data_layout(&self) -> &TargetDataLayout {
967 &self.tcx.data_layout
968 }
969}
970
971impl HasTargetSpec for CodegenCx<'_, '_> {
972 #[inline]
973 fn target_spec(&self) -> &Target {
974 &self.tcx.sess.target
975 }
976}
977
978impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
979 #[inline]
980 fn tcx(&self) -> TyCtxt<'tcx> {
981 self.tcx
982 }
983}
984
985impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
986 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
987 ty::TypingEnv::fully_monomorphized()
988 }
989}
990
991impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
992 #[inline]
993 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
994 if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
995 self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
996 } else {
997 self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
998 }
999 }
1000}
1001
1002impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
1003 #[inline]
1004 fn handle_fn_abi_err(
1005 &self,
1006 err: FnAbiError<'tcx>,
1007 span: Span,
1008 fn_abi_request: FnAbiRequest<'tcx>,
1009 ) -> ! {
1010 match err {
1011 FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
1012 self.tcx.dcx().emit_fatal(Spanned { span, node: err });
1013 }
1014 _ => match fn_abi_request {
1015 FnAbiRequest::OfFnPtr { sig, extra_args } => {
1016 span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
1017 }
1018 FnAbiRequest::OfInstance { instance, extra_args } => {
1019 span_bug!(
1020 span,
1021 "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
1022 );
1023 }
1024 },
1025 }
1026 }
1027}