1use std::borrow::Borrow;
2use std::cell::{Cell, RefCell};
3use std::ffi::{CStr, c_char, c_uint};
4use std::marker::PhantomData;
5use std::ops::Deref;
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::common::AsCCharPtr;
38use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
39use crate::llvm::Metadata;
40use crate::type_::Type;
41use crate::value::Value;
42use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
43
44pub(crate) struct SCx<'ll> {
49 pub llmod: &'ll llvm::Module,
50 pub llcx: &'ll llvm::Context,
51 pub isize_ty: &'ll Type,
52}
53
54impl<'ll> Borrow<SCx<'ll>> for FullCx<'ll, '_> {
55 fn borrow(&self) -> &SCx<'ll> {
56 &self.scx
57 }
58}
59
60impl<'ll, 'tcx> Deref for FullCx<'ll, 'tcx> {
61 type Target = SimpleCx<'ll>;
62
63 #[inline]
64 fn deref(&self) -> &Self::Target {
65 &self.scx
66 }
67}
68
69pub(crate) struct GenericCx<'ll, T: Borrow<SCx<'ll>>>(T, PhantomData<SCx<'ll>>);
70
71impl<'ll, T: Borrow<SCx<'ll>>> Deref for GenericCx<'ll, T> {
72 type Target = T;
73
74 #[inline]
75 fn deref(&self) -> &Self::Target {
76 &self.0
77 }
78}
79
80pub(crate) type SimpleCx<'ll> = GenericCx<'ll, SCx<'ll>>;
81
82pub(crate) type CodegenCx<'ll, 'tcx> = GenericCx<'ll, FullCx<'ll, 'tcx>>;
86
87pub(crate) struct FullCx<'ll, 'tcx> {
88 pub tcx: TyCtxt<'tcx>,
89 pub scx: SimpleCx<'ll>,
90 pub use_dll_storage_attrs: bool,
91 pub tls_model: llvm::ThreadLocalMode,
92
93 pub codegen_unit: &'tcx CodegenUnit<'tcx>,
94
95 pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
97 pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>>,
99 pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,
101
102 pub const_globals: RefCell<FxHashMap<&'ll Value, &'ll Value>>,
104
105 pub statics_to_rauw: RefCell<Vec<(&'ll Value, &'ll Value)>>,
110
111 pub used_statics: RefCell<Vec<&'ll Value>>,
114
115 pub compiler_used_statics: RefCell<Vec<&'ll Value>>,
118
119 pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
121
122 pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
124
125 pub coverage_cx: Option<coverageinfo::CguCoverageContext<'ll, 'tcx>>,
127 pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
128
129 eh_personality: Cell<Option<&'ll Value>>,
130 eh_catch_typeinfo: Cell<Option<&'ll Value>>,
131 pub rust_try_fn: Cell<Option<(&'ll Type, &'ll Value)>>,
132
133 intrinsics: RefCell<FxHashMap<&'static str, (&'ll Type, &'ll Value)>>,
134
135 local_gen_sym_counter: Cell<usize>,
137
138 pub renamed_statics: RefCell<FxHashMap<DefId, &'ll Value>>,
143}
144
145fn to_llvm_tls_model(tls_model: TlsModel) -> llvm::ThreadLocalMode {
146 match tls_model {
147 TlsModel::GeneralDynamic => llvm::ThreadLocalMode::GeneralDynamic,
148 TlsModel::LocalDynamic => llvm::ThreadLocalMode::LocalDynamic,
149 TlsModel::InitialExec => llvm::ThreadLocalMode::InitialExec,
150 TlsModel::LocalExec => llvm::ThreadLocalMode::LocalExec,
151 TlsModel::Emulated => llvm::ThreadLocalMode::GeneralDynamic,
152 }
153}
154
155pub(crate) unsafe fn create_module<'ll>(
156 tcx: TyCtxt<'_>,
157 llcx: &'ll llvm::Context,
158 mod_name: &str,
159) -> &'ll llvm::Module {
160 let sess = tcx.sess;
161 let mod_name = SmallCStr::new(mod_name);
162 let llmod = unsafe { llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx) };
163
164 let mut target_data_layout = sess.target.data_layout.to_string();
165 let llvm_version = llvm_util::get_version();
166
167 if llvm_version < (20, 0, 0) {
168 if sess.target.arch == "aarch64" || sess.target.arch.starts_with("arm64") {
169 target_data_layout =
173 target_data_layout.replace("-p270:32:32-p271:32:32-p272:64:64", "");
174 }
175 if sess.target.arch.starts_with("sparc") {
176 target_data_layout = target_data_layout.replace("-i128:128", "");
179 }
180 if sess.target.arch.starts_with("mips64") {
181 target_data_layout = target_data_layout.replace("-i128:128", "");
184 }
185 if sess.target.arch.starts_with("powerpc64") {
186 target_data_layout = target_data_layout.replace("-i128:128", "");
189 }
190 if sess.target.arch.starts_with("wasm32") || sess.target.arch.starts_with("wasm64") {
191 target_data_layout = target_data_layout.replace("-i128:128", "");
194 }
195 }
196 if llvm_version < (21, 0, 0) {
197 if sess.target.arch == "nvptx64" {
198 target_data_layout = target_data_layout.replace("e-p6:32:32-i64", "e-i64");
200 }
201 }
202
203 {
205 let tm = crate::back::write::create_informational_target_machine(tcx.sess, false);
206 unsafe {
207 llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());
208 }
209
210 let llvm_data_layout = unsafe { llvm::LLVMGetDataLayoutStr(llmod) };
211 let llvm_data_layout =
212 str::from_utf8(unsafe { CStr::from_ptr(llvm_data_layout) }.to_bytes())
213 .expect("got a non-UTF8 data-layout from LLVM");
214
215 if target_data_layout != llvm_data_layout {
216 tcx.dcx().emit_err(crate::errors::MismatchedDataLayout {
217 rustc_target: sess.opts.target_triple.to_string().as_str(),
218 rustc_layout: target_data_layout.as_str(),
219 llvm_target: sess.target.llvm_target.borrow(),
220 llvm_layout: llvm_data_layout,
221 });
222 }
223 }
224
225 let data_layout = SmallCStr::new(&target_data_layout);
226 unsafe {
227 llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
228 }
229
230 let llvm_target = SmallCStr::new(&versioned_llvm_target(sess));
231 unsafe {
232 llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
233 }
234
235 let reloc_model = sess.relocation_model();
236 if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
237 unsafe {
238 llvm::LLVMRustSetModulePICLevel(llmod);
239 }
240 if reloc_model == RelocModel::Pie
243 || tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable)
244 {
245 unsafe {
246 llvm::LLVMRustSetModulePIELevel(llmod);
247 }
248 }
249 }
250
251 unsafe {
257 llvm::LLVMRustSetModuleCodeModel(llmod, to_llvm_code_model(sess.code_model()));
258 }
259
260 if !sess.needs_plt() {
263 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "RtLibUseGOT", 1);
264 }
265
266 if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
268 llvm::add_module_flag_u32(
269 llmod,
270 llvm::ModuleFlagMergeBehavior::Override,
271 "CFI Canonical Jump Tables",
272 1,
273 );
274 }
275
276 if sess.is_sanitizer_cfi_normalize_integers_enabled() {
279 llvm::add_module_flag_u32(
280 llmod,
281 llvm::ModuleFlagMergeBehavior::Override,
282 "cfi-normalize-integers",
283 1,
284 );
285 }
286
287 if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
290 llvm::add_module_flag_u32(
291 llmod,
292 llvm::ModuleFlagMergeBehavior::Override,
293 "EnableSplitLTOUnit",
294 1,
295 );
296 }
297
298 if sess.is_sanitizer_kcfi_enabled() {
300 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);
301
302 let pfe =
305 PatchableFunctionEntry::from_config(sess.opts.unstable_opts.patchable_function_entry);
306 if pfe.prefix() > 0 {
307 llvm::add_module_flag_u32(
308 llmod,
309 llvm::ModuleFlagMergeBehavior::Override,
310 "kcfi-offset",
311 pfe.prefix().into(),
312 );
313 }
314
315 if sess.is_sanitizer_kcfi_arity_enabled() {
318 if llvm_version < (21, 0, 0) {
320 tcx.dcx().emit_err(crate::errors::SanitizerKcfiArityRequiresLLVM2100);
321 }
322
323 llvm::add_module_flag_u32(
324 llmod,
325 llvm::ModuleFlagMergeBehavior::Override,
326 "kcfi-arity",
327 1,
328 );
329 }
330 }
331
332 if sess.target.is_like_msvc
334 || (sess.target.options.os == "windows"
335 && sess.target.options.env == "gnu"
336 && sess.target.options.abi == "llvm")
337 {
338 match sess.opts.cg.control_flow_guard {
339 CFGuard::Disabled => {}
340 CFGuard::NoChecks => {
341 llvm::add_module_flag_u32(
343 llmod,
344 llvm::ModuleFlagMergeBehavior::Warning,
345 "cfguard",
346 1,
347 );
348 }
349 CFGuard::Checks => {
350 llvm::add_module_flag_u32(
352 llmod,
353 llvm::ModuleFlagMergeBehavior::Warning,
354 "cfguard",
355 2,
356 );
357 }
358 }
359 }
360
361 if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
362 if sess.target.arch == "aarch64" {
363 llvm::add_module_flag_u32(
364 llmod,
365 llvm::ModuleFlagMergeBehavior::Min,
366 "branch-target-enforcement",
367 bti.into(),
368 );
369 llvm::add_module_flag_u32(
370 llmod,
371 llvm::ModuleFlagMergeBehavior::Min,
372 "sign-return-address",
373 pac_ret.is_some().into(),
374 );
375 let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, pc: false, key: PAuthKey::A });
376 llvm::add_module_flag_u32(
377 llmod,
378 llvm::ModuleFlagMergeBehavior::Min,
379 "branch-protection-pauth-lr",
380 pac_opts.pc.into(),
381 );
382 llvm::add_module_flag_u32(
383 llmod,
384 llvm::ModuleFlagMergeBehavior::Min,
385 "sign-return-address-all",
386 pac_opts.leaf.into(),
387 );
388 llvm::add_module_flag_u32(
389 llmod,
390 llvm::ModuleFlagMergeBehavior::Min,
391 "sign-return-address-with-bkey",
392 u32::from(pac_opts.key == PAuthKey::B),
393 );
394 } else {
395 bug!(
396 "branch-protection used on non-AArch64 target; \
397 this should be checked in rustc_session."
398 );
399 }
400 }
401
402 if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
404 llvm::add_module_flag_u32(
405 llmod,
406 llvm::ModuleFlagMergeBehavior::Override,
407 "cf-protection-branch",
408 1,
409 );
410 }
411 if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
412 llvm::add_module_flag_u32(
413 llmod,
414 llvm::ModuleFlagMergeBehavior::Override,
415 "cf-protection-return",
416 1,
417 );
418 }
419
420 if sess.opts.unstable_opts.virtual_function_elimination {
421 llvm::add_module_flag_u32(
422 llmod,
423 llvm::ModuleFlagMergeBehavior::Error,
424 "Virtual Function Elim",
425 1,
426 );
427 }
428
429 if sess.opts.unstable_opts.ehcont_guard {
431 llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Warning, "ehcontguard", 1);
432 }
433
434 match sess.opts.unstable_opts.function_return {
435 FunctionReturn::Keep => {}
436 FunctionReturn::ThunkExtern => {
437 llvm::add_module_flag_u32(
438 llmod,
439 llvm::ModuleFlagMergeBehavior::Override,
440 "function_return_thunk_extern",
441 1,
442 );
443 }
444 }
445
446 match (sess.opts.unstable_opts.small_data_threshold, sess.target.small_data_threshold_support())
447 {
448 (Some(threshold), SmallDataThresholdSupport::LlvmModuleFlag(flag)) => {
451 llvm::add_module_flag_u32(
452 llmod,
453 llvm::ModuleFlagMergeBehavior::Error,
454 &flag,
455 threshold as u32,
456 );
457 }
458 _ => (),
459 };
460
461 #[allow(clippy::option_env_unwrap)]
466 let rustc_producer =
467 format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
468 let name_metadata = unsafe {
469 llvm::LLVMMDStringInContext2(
470 llcx,
471 rustc_producer.as_c_char_ptr(),
472 rustc_producer.as_bytes().len(),
473 )
474 };
475 unsafe {
476 llvm::LLVMAddNamedMetadataOperand(
477 llmod,
478 c"llvm.ident".as_ptr(),
479 &llvm::LLVMMetadataAsValue(llcx, llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
480 );
481 }
482
483 let llvm_abiname = &sess.target.options.llvm_abiname;
489 if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
490 llvm::add_module_flag_str(
491 llmod,
492 llvm::ModuleFlagMergeBehavior::Error,
493 "target-abi",
494 llvm_abiname,
495 );
496 }
497
498 for (key, value, merge_behavior) in &sess.opts.unstable_opts.llvm_module_flag {
500 let merge_behavior = match merge_behavior.as_str() {
501 "error" => llvm::ModuleFlagMergeBehavior::Error,
502 "warning" => llvm::ModuleFlagMergeBehavior::Warning,
503 "require" => llvm::ModuleFlagMergeBehavior::Require,
504 "override" => llvm::ModuleFlagMergeBehavior::Override,
505 "append" => llvm::ModuleFlagMergeBehavior::Append,
506 "appendunique" => llvm::ModuleFlagMergeBehavior::AppendUnique,
507 "max" => llvm::ModuleFlagMergeBehavior::Max,
508 "min" => llvm::ModuleFlagMergeBehavior::Min,
509 _ => unreachable!(),
511 };
512 llvm::add_module_flag_u32(llmod, merge_behavior, key, *value);
513 }
514
515 llmod
516}
517
518impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
519 pub(crate) fn new(
520 tcx: TyCtxt<'tcx>,
521 codegen_unit: &'tcx CodegenUnit<'tcx>,
522 llvm_module: &'ll crate::ModuleLlvm,
523 ) -> Self {
524 let use_dll_storage_attrs = tcx.sess.target.is_like_windows;
577
578 let tls_model = to_llvm_tls_model(tcx.sess.tls_model());
579
580 let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
581
582 let coverage_cx =
583 tcx.sess.instrument_coverage().then(coverageinfo::CguCoverageContext::new);
584
585 let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
586 let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);
587 debuginfo::metadata::build_compile_unit_di_node(
588 tcx,
589 codegen_unit.name().as_str(),
590 &dctx,
591 );
592 Some(dctx)
593 } else {
594 None
595 };
596
597 GenericCx(
598 FullCx {
599 tcx,
600 scx: SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size),
601 use_dll_storage_attrs,
602 tls_model,
603 codegen_unit,
604 instances: Default::default(),
605 vtables: Default::default(),
606 const_str_cache: Default::default(),
607 const_globals: Default::default(),
608 statics_to_rauw: RefCell::new(Vec::new()),
609 used_statics: RefCell::new(Vec::new()),
610 compiler_used_statics: RefCell::new(Vec::new()),
611 type_lowering: Default::default(),
612 scalar_lltypes: Default::default(),
613 coverage_cx,
614 dbg_cx,
615 eh_personality: Cell::new(None),
616 eh_catch_typeinfo: Cell::new(None),
617 rust_try_fn: Cell::new(None),
618 intrinsics: Default::default(),
619 local_gen_sym_counter: Cell::new(0),
620 renamed_statics: Default::default(),
621 },
622 PhantomData,
623 )
624 }
625
626 pub(crate) fn statics_to_rauw(&self) -> &RefCell<Vec<(&'ll Value, &'ll Value)>> {
627 &self.statics_to_rauw
628 }
629
630 #[inline]
632 #[track_caller]
633 pub(crate) fn coverage_cx(&self) -> &coverageinfo::CguCoverageContext<'ll, 'tcx> {
634 self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
635 }
636
637 pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
638 let array = self.const_array(self.type_ptr(), values);
639
640 let g = llvm::add_global(self.llmod, self.val_ty(array), name);
641 llvm::set_initializer(g, array);
642 llvm::set_linkage(g, llvm::Linkage::AppendingLinkage);
643 llvm::set_section(g, c"llvm.metadata");
644 }
645}
646impl<'ll> SimpleCx<'ll> {
647 pub(crate) fn get_return_type(&self, ty: &'ll Type) -> &'ll Type {
648 assert_eq!(self.type_kind(ty), TypeKind::Function);
649 unsafe { llvm::LLVMGetReturnType(ty) }
650 }
651 pub(crate) fn get_type_of_global(&self, val: &'ll Value) -> &'ll Type {
652 unsafe { llvm::LLVMGlobalGetValueType(val) }
653 }
654 pub(crate) fn val_ty(&self, v: &'ll Value) -> &'ll Type {
655 common::val_ty(v)
656 }
657}
658impl<'ll> SimpleCx<'ll> {
659 pub(crate) fn new(
660 llmod: &'ll llvm::Module,
661 llcx: &'ll llvm::Context,
662 pointer_size: Size,
663 ) -> Self {
664 let isize_ty = llvm::Type::ix_llcx(llcx, pointer_size.bits());
665 Self(SCx { llmod, llcx, isize_ty }, PhantomData)
666 }
667}
668
669impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
670 pub(crate) fn get_metadata_value(&self, metadata: &'ll Metadata) -> &'ll Value {
671 llvm::LLVMMetadataAsValue(self.llcx(), metadata)
672 }
673
674 pub(crate) fn get_const_i64(&self, n: u64) -> &'ll Value {
677 let ty = unsafe { llvm::LLVMInt64TypeInContext(self.llcx()) };
678 unsafe { llvm::LLVMConstInt(ty, n, llvm::False) }
679 }
680
681 pub(crate) fn get_function(&self, name: &str) -> Option<&'ll Value> {
682 let name = SmallCStr::new(name);
683 unsafe { llvm::LLVMGetNamedFunction((**self).borrow().llmod, name.as_ptr()) }
684 }
685
686 pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId {
687 unsafe {
688 llvm::LLVMGetMDKindIDInContext(
689 self.llcx(),
690 name.as_ptr() as *const c_char,
691 name.len() as c_uint,
692 )
693 }
694 }
695
696 pub(crate) fn create_metadata(&self, name: String) -> Option<&'ll Metadata> {
697 Some(unsafe {
698 llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
699 })
700 }
701
702 pub(crate) fn get_functions(&self) -> Vec<&'ll Value> {
703 let mut functions = vec![];
704 let mut func = unsafe { llvm::LLVMGetFirstFunction(self.llmod()) };
705 while let Some(f) = func {
706 functions.push(f);
707 func = unsafe { llvm::LLVMGetNextFunction(f) }
708 }
709 functions
710 }
711}
712
713impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
714 fn vtables(
715 &self,
716 ) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>), &'ll Value>> {
717 &self.vtables
718 }
719
720 fn apply_vcall_visibility_metadata(
721 &self,
722 ty: Ty<'tcx>,
723 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
724 vtable: &'ll Value,
725 ) {
726 apply_vcall_visibility_metadata(self, ty, poly_trait_ref, vtable);
727 }
728
729 fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
730 get_fn(self, instance)
731 }
732
733 fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
734 get_fn(self, instance)
735 }
736
737 fn eh_personality(&self) -> &'ll Value {
738 if let Some(llpersonality) = self.eh_personality.get() {
759 return llpersonality;
760 }
761
762 let name = if wants_msvc_seh(self.sess()) {
763 Some("__CxxFrameHandler3")
764 } else if wants_wasm_eh(self.sess()) {
765 Some("__gxx_wasm_personality_v0")
770 } else {
771 None
772 };
773
774 let tcx = self.tcx;
775 let llfn = match tcx.lang_items().eh_personality() {
776 Some(def_id) if name.is_none() => self.get_fn_addr(ty::Instance::expect_resolve(
777 tcx,
778 self.typing_env(),
779 def_id,
780 ty::List::empty(),
781 DUMMY_SP,
782 )),
783 _ => {
784 let name = name.unwrap_or("rust_eh_personality");
785 if let Some(llfn) = self.get_declared_value(name) {
786 llfn
787 } else {
788 let fty = self.type_variadic_func(&[], self.type_i32());
789 let llfn = self.declare_cfn(name, llvm::UnnamedAddr::Global, fty);
790 let target_cpu = attributes::target_cpu_attr(self);
791 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[target_cpu]);
792 llfn
793 }
794 }
795 };
796 self.eh_personality.set(Some(llfn));
797 llfn
798 }
799
800 fn sess(&self) -> &Session {
801 self.tcx.sess
802 }
803
804 fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
805 self.codegen_unit
806 }
807
808 fn set_frame_pointer_type(&self, llfn: &'ll Value) {
809 if let Some(attr) = attributes::frame_pointer_type_attr(self) {
810 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[attr]);
811 }
812 }
813
814 fn apply_target_cpu_attr(&self, llfn: &'ll Value) {
815 let mut attrs = SmallVec::<[_; 2]>::new();
816 attrs.push(attributes::target_cpu_attr(self));
817 attrs.extend(attributes::tune_cpu_attr(self));
818 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
819 }
820
821 fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
822 let entry_name = self.sess().target.entry_name.as_ref();
823 if self.get_declared_value(entry_name).is_none() {
824 Some(self.declare_entry_fn(
825 entry_name,
826 llvm::CallConv::from_conv(
827 self.sess().target.entry_abi,
828 self.sess().target.arch.borrow(),
829 ),
830 llvm::UnnamedAddr::Global,
831 fn_type,
832 ))
833 } else {
834 None
837 }
838 }
839}
840
841impl<'ll> CodegenCx<'ll, '_> {
842 pub(crate) fn get_intrinsic(&self, key: &str) -> (&'ll Type, &'ll Value) {
843 if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
844 return v;
845 }
846
847 self.declare_intrinsic(key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
848 }
849
850 fn insert_intrinsic(
851 &self,
852 name: &'static str,
853 args: Option<&[&'ll llvm::Type]>,
854 ret: &'ll llvm::Type,
855 ) -> (&'ll llvm::Type, &'ll llvm::Value) {
856 let fn_ty = if let Some(args) = args {
857 self.type_func(args, ret)
858 } else {
859 self.type_variadic_func(&[], ret)
860 };
861 let f = self.declare_cfn(name, llvm::UnnamedAddr::No, fn_ty);
862 self.intrinsics.borrow_mut().insert(name, (fn_ty, f));
863 (fn_ty, f)
864 }
865
866 fn declare_intrinsic(&self, key: &str) -> Option<(&'ll Type, &'ll Value)> {
867 macro_rules! ifn {
868 ($name:expr, fn() -> $ret:expr) => (
869 if key == $name {
870 return Some(self.insert_intrinsic($name, Some(&[]), $ret));
871 }
872 );
873 ($name:expr, fn(...) -> $ret:expr) => (
874 if key == $name {
875 return Some(self.insert_intrinsic($name, None, $ret));
876 }
877 );
878 ($name:expr, fn($($arg:expr),*) -> $ret:expr) => (
879 if key == $name {
880 return Some(self.insert_intrinsic($name, Some(&[$($arg),*]), $ret));
881 }
882 );
883 }
884 macro_rules! mk_struct {
885 ($($field_ty:expr),*) => (self.type_struct( &[$($field_ty),*], false))
886 }
887
888 let ptr = self.type_ptr();
889 let void = self.type_void();
890 let i1 = self.type_i1();
891 let t_i8 = self.type_i8();
892 let t_i16 = self.type_i16();
893 let t_i32 = self.type_i32();
894 let t_i64 = self.type_i64();
895 let t_i128 = self.type_i128();
896 let t_isize = self.type_isize();
897 let t_f16 = self.type_f16();
898 let t_f32 = self.type_f32();
899 let t_f64 = self.type_f64();
900 let t_f128 = self.type_f128();
901 let t_metadata = self.type_metadata();
902 let t_token = self.type_token();
903
904 ifn!("llvm.wasm.get.exception", fn(t_token) -> ptr);
905 ifn!("llvm.wasm.get.ehselector", fn(t_token) -> t_i32);
906
907 ifn!("llvm.wasm.trunc.unsigned.i32.f32", fn(t_f32) -> t_i32);
908 ifn!("llvm.wasm.trunc.unsigned.i32.f64", fn(t_f64) -> t_i32);
909 ifn!("llvm.wasm.trunc.unsigned.i64.f32", fn(t_f32) -> t_i64);
910 ifn!("llvm.wasm.trunc.unsigned.i64.f64", fn(t_f64) -> t_i64);
911 ifn!("llvm.wasm.trunc.signed.i32.f32", fn(t_f32) -> t_i32);
912 ifn!("llvm.wasm.trunc.signed.i32.f64", fn(t_f64) -> t_i32);
913 ifn!("llvm.wasm.trunc.signed.i64.f32", fn(t_f32) -> t_i64);
914 ifn!("llvm.wasm.trunc.signed.i64.f64", fn(t_f64) -> t_i64);
915
916 ifn!("llvm.fptosi.sat.i8.f32", fn(t_f32) -> t_i8);
917 ifn!("llvm.fptosi.sat.i16.f32", fn(t_f32) -> t_i16);
918 ifn!("llvm.fptosi.sat.i32.f32", fn(t_f32) -> t_i32);
919 ifn!("llvm.fptosi.sat.i64.f32", fn(t_f32) -> t_i64);
920 ifn!("llvm.fptosi.sat.i128.f32", fn(t_f32) -> t_i128);
921 ifn!("llvm.fptosi.sat.i8.f64", fn(t_f64) -> t_i8);
922 ifn!("llvm.fptosi.sat.i16.f64", fn(t_f64) -> t_i16);
923 ifn!("llvm.fptosi.sat.i32.f64", fn(t_f64) -> t_i32);
924 ifn!("llvm.fptosi.sat.i64.f64", fn(t_f64) -> t_i64);
925 ifn!("llvm.fptosi.sat.i128.f64", fn(t_f64) -> t_i128);
926
927 ifn!("llvm.fptoui.sat.i8.f32", fn(t_f32) -> t_i8);
928 ifn!("llvm.fptoui.sat.i16.f32", fn(t_f32) -> t_i16);
929 ifn!("llvm.fptoui.sat.i32.f32", fn(t_f32) -> t_i32);
930 ifn!("llvm.fptoui.sat.i64.f32", fn(t_f32) -> t_i64);
931 ifn!("llvm.fptoui.sat.i128.f32", fn(t_f32) -> t_i128);
932 ifn!("llvm.fptoui.sat.i8.f64", fn(t_f64) -> t_i8);
933 ifn!("llvm.fptoui.sat.i16.f64", fn(t_f64) -> t_i16);
934 ifn!("llvm.fptoui.sat.i32.f64", fn(t_f64) -> t_i32);
935 ifn!("llvm.fptoui.sat.i64.f64", fn(t_f64) -> t_i64);
936 ifn!("llvm.fptoui.sat.i128.f64", fn(t_f64) -> t_i128);
937
938 ifn!("llvm.trap", fn() -> void);
939 ifn!("llvm.debugtrap", fn() -> void);
940 ifn!("llvm.frameaddress", fn(t_i32) -> ptr);
941
942 ifn!("llvm.powi.f16.i32", fn(t_f16, t_i32) -> t_f16);
943 ifn!("llvm.powi.f32.i32", fn(t_f32, t_i32) -> t_f32);
944 ifn!("llvm.powi.f64.i32", fn(t_f64, t_i32) -> t_f64);
945 ifn!("llvm.powi.f128.i32", fn(t_f128, t_i32) -> t_f128);
946
947 ifn!("llvm.pow.f16", fn(t_f16, t_f16) -> t_f16);
948 ifn!("llvm.pow.f32", fn(t_f32, t_f32) -> t_f32);
949 ifn!("llvm.pow.f64", fn(t_f64, t_f64) -> t_f64);
950 ifn!("llvm.pow.f128", fn(t_f128, t_f128) -> t_f128);
951
952 ifn!("llvm.sqrt.f16", fn(t_f16) -> t_f16);
953 ifn!("llvm.sqrt.f32", fn(t_f32) -> t_f32);
954 ifn!("llvm.sqrt.f64", fn(t_f64) -> t_f64);
955 ifn!("llvm.sqrt.f128", fn(t_f128) -> t_f128);
956
957 ifn!("llvm.sin.f16", fn(t_f16) -> t_f16);
958 ifn!("llvm.sin.f32", fn(t_f32) -> t_f32);
959 ifn!("llvm.sin.f64", fn(t_f64) -> t_f64);
960 ifn!("llvm.sin.f128", fn(t_f128) -> t_f128);
961
962 ifn!("llvm.cos.f16", fn(t_f16) -> t_f16);
963 ifn!("llvm.cos.f32", fn(t_f32) -> t_f32);
964 ifn!("llvm.cos.f64", fn(t_f64) -> t_f64);
965 ifn!("llvm.cos.f128", fn(t_f128) -> t_f128);
966
967 ifn!("llvm.exp.f16", fn(t_f16) -> t_f16);
968 ifn!("llvm.exp.f32", fn(t_f32) -> t_f32);
969 ifn!("llvm.exp.f64", fn(t_f64) -> t_f64);
970 ifn!("llvm.exp.f128", fn(t_f128) -> t_f128);
971
972 ifn!("llvm.exp2.f16", fn(t_f16) -> t_f16);
973 ifn!("llvm.exp2.f32", fn(t_f32) -> t_f32);
974 ifn!("llvm.exp2.f64", fn(t_f64) -> t_f64);
975 ifn!("llvm.exp2.f128", fn(t_f128) -> t_f128);
976
977 ifn!("llvm.log.f16", fn(t_f16) -> t_f16);
978 ifn!("llvm.log.f32", fn(t_f32) -> t_f32);
979 ifn!("llvm.log.f64", fn(t_f64) -> t_f64);
980 ifn!("llvm.log.f128", fn(t_f128) -> t_f128);
981
982 ifn!("llvm.log10.f16", fn(t_f16) -> t_f16);
983 ifn!("llvm.log10.f32", fn(t_f32) -> t_f32);
984 ifn!("llvm.log10.f64", fn(t_f64) -> t_f64);
985 ifn!("llvm.log10.f128", fn(t_f128) -> t_f128);
986
987 ifn!("llvm.log2.f16", fn(t_f16) -> t_f16);
988 ifn!("llvm.log2.f32", fn(t_f32) -> t_f32);
989 ifn!("llvm.log2.f64", fn(t_f64) -> t_f64);
990 ifn!("llvm.log2.f128", fn(t_f128) -> t_f128);
991
992 ifn!("llvm.fma.f16", fn(t_f16, t_f16, t_f16) -> t_f16);
993 ifn!("llvm.fma.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
994 ifn!("llvm.fma.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
995 ifn!("llvm.fma.f128", fn(t_f128, t_f128, t_f128) -> t_f128);
996
997 ifn!("llvm.fmuladd.f16", fn(t_f16, t_f16, t_f16) -> t_f16);
998 ifn!("llvm.fmuladd.f32", fn(t_f32, t_f32, t_f32) -> t_f32);
999 ifn!("llvm.fmuladd.f64", fn(t_f64, t_f64, t_f64) -> t_f64);
1000 ifn!("llvm.fmuladd.f128", fn(t_f128, t_f128, t_f128) -> t_f128);
1001
1002 ifn!("llvm.fabs.f16", fn(t_f16) -> t_f16);
1003 ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
1004 ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);
1005 ifn!("llvm.fabs.f128", fn(t_f128) -> t_f128);
1006
1007 ifn!("llvm.minnum.f16", fn(t_f16, t_f16) -> t_f16);
1008 ifn!("llvm.minnum.f32", fn(t_f32, t_f32) -> t_f32);
1009 ifn!("llvm.minnum.f64", fn(t_f64, t_f64) -> t_f64);
1010 ifn!("llvm.minnum.f128", fn(t_f128, t_f128) -> t_f128);
1011
1012 ifn!("llvm.minimum.f16", fn(t_f16, t_f16) -> t_f16);
1013 ifn!("llvm.minimum.f32", fn(t_f32, t_f32) -> t_f32);
1014 ifn!("llvm.minimum.f64", fn(t_f64, t_f64) -> t_f64);
1015 ifn!("llvm.maxnum.f16", fn(t_f16, t_f16) -> t_f16);
1021 ifn!("llvm.maxnum.f32", fn(t_f32, t_f32) -> t_f32);
1022 ifn!("llvm.maxnum.f64", fn(t_f64, t_f64) -> t_f64);
1023 ifn!("llvm.maxnum.f128", fn(t_f128, t_f128) -> t_f128);
1024
1025 ifn!("llvm.maximum.f16", fn(t_f16, t_f16) -> t_f16);
1026 ifn!("llvm.maximum.f32", fn(t_f32, t_f32) -> t_f32);
1027 ifn!("llvm.maximum.f64", fn(t_f64, t_f64) -> t_f64);
1028 ifn!("llvm.floor.f16", fn(t_f16) -> t_f16);
1034 ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
1035 ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
1036 ifn!("llvm.floor.f128", fn(t_f128) -> t_f128);
1037
1038 ifn!("llvm.ceil.f16", fn(t_f16) -> t_f16);
1039 ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
1040 ifn!("llvm.ceil.f64", fn(t_f64) -> t_f64);
1041 ifn!("llvm.ceil.f128", fn(t_f128) -> t_f128);
1042
1043 ifn!("llvm.trunc.f16", fn(t_f16) -> t_f16);
1044 ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
1045 ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
1046 ifn!("llvm.trunc.f128", fn(t_f128) -> t_f128);
1047
1048 ifn!("llvm.copysign.f16", fn(t_f16, t_f16) -> t_f16);
1049 ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
1050 ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
1051 ifn!("llvm.copysign.f128", fn(t_f128, t_f128) -> t_f128);
1052
1053 ifn!("llvm.round.f16", fn(t_f16) -> t_f16);
1054 ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
1055 ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
1056 ifn!("llvm.round.f128", fn(t_f128) -> t_f128);
1057
1058 ifn!("llvm.roundeven.f16", fn(t_f16) -> t_f16);
1059 ifn!("llvm.roundeven.f32", fn(t_f32) -> t_f32);
1060 ifn!("llvm.roundeven.f64", fn(t_f64) -> t_f64);
1061 ifn!("llvm.roundeven.f128", fn(t_f128) -> t_f128);
1062
1063 ifn!("llvm.rint.f16", fn(t_f16) -> t_f16);
1064 ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
1065 ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
1066 ifn!("llvm.rint.f128", fn(t_f128) -> t_f128);
1067
1068 ifn!("llvm.nearbyint.f16", fn(t_f16) -> t_f16);
1069 ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
1070 ifn!("llvm.nearbyint.f64", fn(t_f64) -> t_f64);
1071 ifn!("llvm.nearbyint.f128", fn(t_f128) -> t_f128);
1072
1073 ifn!("llvm.ctpop.i8", fn(t_i8) -> t_i8);
1074 ifn!("llvm.ctpop.i16", fn(t_i16) -> t_i16);
1075 ifn!("llvm.ctpop.i32", fn(t_i32) -> t_i32);
1076 ifn!("llvm.ctpop.i64", fn(t_i64) -> t_i64);
1077 ifn!("llvm.ctpop.i128", fn(t_i128) -> t_i128);
1078
1079 ifn!("llvm.ctlz.i8", fn(t_i8, i1) -> t_i8);
1080 ifn!("llvm.ctlz.i16", fn(t_i16, i1) -> t_i16);
1081 ifn!("llvm.ctlz.i32", fn(t_i32, i1) -> t_i32);
1082 ifn!("llvm.ctlz.i64", fn(t_i64, i1) -> t_i64);
1083 ifn!("llvm.ctlz.i128", fn(t_i128, i1) -> t_i128);
1084
1085 ifn!("llvm.cttz.i8", fn(t_i8, i1) -> t_i8);
1086 ifn!("llvm.cttz.i16", fn(t_i16, i1) -> t_i16);
1087 ifn!("llvm.cttz.i32", fn(t_i32, i1) -> t_i32);
1088 ifn!("llvm.cttz.i64", fn(t_i64, i1) -> t_i64);
1089 ifn!("llvm.cttz.i128", fn(t_i128, i1) -> t_i128);
1090
1091 ifn!("llvm.bswap.i16", fn(t_i16) -> t_i16);
1092 ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
1093 ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);
1094 ifn!("llvm.bswap.i128", fn(t_i128) -> t_i128);
1095
1096 ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
1097 ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
1098 ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
1099 ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);
1100 ifn!("llvm.bitreverse.i128", fn(t_i128) -> t_i128);
1101
1102 ifn!("llvm.fshl.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
1103 ifn!("llvm.fshl.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
1104 ifn!("llvm.fshl.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
1105 ifn!("llvm.fshl.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
1106 ifn!("llvm.fshl.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
1107
1108 ifn!("llvm.fshr.i8", fn(t_i8, t_i8, t_i8) -> t_i8);
1109 ifn!("llvm.fshr.i16", fn(t_i16, t_i16, t_i16) -> t_i16);
1110 ifn!("llvm.fshr.i32", fn(t_i32, t_i32, t_i32) -> t_i32);
1111 ifn!("llvm.fshr.i64", fn(t_i64, t_i64, t_i64) -> t_i64);
1112 ifn!("llvm.fshr.i128", fn(t_i128, t_i128, t_i128) -> t_i128);
1113
1114 ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
1115 ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
1116 ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
1117 ifn!("llvm.sadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
1118 ifn!("llvm.sadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
1119
1120 ifn!("llvm.uadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
1121 ifn!("llvm.uadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
1122 ifn!("llvm.uadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
1123 ifn!("llvm.uadd.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
1124 ifn!("llvm.uadd.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
1125
1126 ifn!("llvm.ssub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
1127 ifn!("llvm.ssub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
1128 ifn!("llvm.ssub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
1129 ifn!("llvm.ssub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
1130 ifn!("llvm.ssub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
1131
1132 ifn!("llvm.usub.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
1133 ifn!("llvm.usub.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
1134 ifn!("llvm.usub.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
1135 ifn!("llvm.usub.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
1136 ifn!("llvm.usub.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
1137
1138 ifn!("llvm.smul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
1139 ifn!("llvm.smul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
1140 ifn!("llvm.smul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
1141 ifn!("llvm.smul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
1142 ifn!("llvm.smul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
1143
1144 ifn!("llvm.umul.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct! {t_i8, i1});
1145 ifn!("llvm.umul.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct! {t_i16, i1});
1146 ifn!("llvm.umul.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct! {t_i32, i1});
1147 ifn!("llvm.umul.with.overflow.i64", fn(t_i64, t_i64) -> mk_struct! {t_i64, i1});
1148 ifn!("llvm.umul.with.overflow.i128", fn(t_i128, t_i128) -> mk_struct! {t_i128, i1});
1149
1150 ifn!("llvm.sadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
1151 ifn!("llvm.sadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
1152 ifn!("llvm.sadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
1153 ifn!("llvm.sadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
1154 ifn!("llvm.sadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
1155
1156 ifn!("llvm.uadd.sat.i8", fn(t_i8, t_i8) -> t_i8);
1157 ifn!("llvm.uadd.sat.i16", fn(t_i16, t_i16) -> t_i16);
1158 ifn!("llvm.uadd.sat.i32", fn(t_i32, t_i32) -> t_i32);
1159 ifn!("llvm.uadd.sat.i64", fn(t_i64, t_i64) -> t_i64);
1160 ifn!("llvm.uadd.sat.i128", fn(t_i128, t_i128) -> t_i128);
1161
1162 ifn!("llvm.ssub.sat.i8", fn(t_i8, t_i8) -> t_i8);
1163 ifn!("llvm.ssub.sat.i16", fn(t_i16, t_i16) -> t_i16);
1164 ifn!("llvm.ssub.sat.i32", fn(t_i32, t_i32) -> t_i32);
1165 ifn!("llvm.ssub.sat.i64", fn(t_i64, t_i64) -> t_i64);
1166 ifn!("llvm.ssub.sat.i128", fn(t_i128, t_i128) -> t_i128);
1167
1168 ifn!("llvm.usub.sat.i8", fn(t_i8, t_i8) -> t_i8);
1169 ifn!("llvm.usub.sat.i16", fn(t_i16, t_i16) -> t_i16);
1170 ifn!("llvm.usub.sat.i32", fn(t_i32, t_i32) -> t_i32);
1171 ifn!("llvm.usub.sat.i64", fn(t_i64, t_i64) -> t_i64);
1172 ifn!("llvm.usub.sat.i128", fn(t_i128, t_i128) -> t_i128);
1173
1174 ifn!("llvm.scmp.i8.i8", fn(t_i8, t_i8) -> t_i8);
1175 ifn!("llvm.scmp.i8.i16", fn(t_i16, t_i16) -> t_i8);
1176 ifn!("llvm.scmp.i8.i32", fn(t_i32, t_i32) -> t_i8);
1177 ifn!("llvm.scmp.i8.i64", fn(t_i64, t_i64) -> t_i8);
1178 ifn!("llvm.scmp.i8.i128", fn(t_i128, t_i128) -> t_i8);
1179
1180 ifn!("llvm.ucmp.i8.i8", fn(t_i8, t_i8) -> t_i8);
1181 ifn!("llvm.ucmp.i8.i16", fn(t_i16, t_i16) -> t_i8);
1182 ifn!("llvm.ucmp.i8.i32", fn(t_i32, t_i32) -> t_i8);
1183 ifn!("llvm.ucmp.i8.i64", fn(t_i64, t_i64) -> t_i8);
1184 ifn!("llvm.ucmp.i8.i128", fn(t_i128, t_i128) -> t_i8);
1185
1186 ifn!("llvm.lifetime.start.p0i8", fn(t_i64, ptr) -> void);
1187 ifn!("llvm.lifetime.end.p0i8", fn(t_i64, ptr) -> void);
1188
1189 ifn!("llvm.is.constant.i1", fn(i1) -> i1);
1194 ifn!("llvm.is.constant.i8", fn(t_i8) -> i1);
1195 ifn!("llvm.is.constant.i16", fn(t_i16) -> i1);
1196 ifn!("llvm.is.constant.i32", fn(t_i32) -> i1);
1197 ifn!("llvm.is.constant.i64", fn(t_i64) -> i1);
1198 ifn!("llvm.is.constant.i128", fn(t_i128) -> i1);
1199 ifn!("llvm.is.constant.isize", fn(t_isize) -> i1);
1200 ifn!("llvm.is.constant.f16", fn(t_f16) -> i1);
1201 ifn!("llvm.is.constant.f32", fn(t_f32) -> i1);
1202 ifn!("llvm.is.constant.f64", fn(t_f64) -> i1);
1203 ifn!("llvm.is.constant.f128", fn(t_f128) -> i1);
1204 ifn!("llvm.is.constant.ptr", fn(ptr) -> i1);
1205
1206 ifn!("llvm.expect.i1", fn(i1, i1) -> i1);
1207 ifn!("llvm.eh.typeid.for", fn(ptr) -> t_i32);
1208 ifn!("llvm.localescape", fn(...) -> void);
1209 ifn!("llvm.localrecover", fn(ptr, ptr, t_i32) -> ptr);
1210 ifn!("llvm.x86.seh.recoverfp", fn(ptr, ptr) -> ptr);
1211
1212 ifn!("llvm.assume", fn(i1) -> void);
1213 ifn!("llvm.prefetch", fn(ptr, t_i32, t_i32, t_i32) -> void);
1214
1215 match self.sess().target.arch.as_ref() {
1219 "avr" | "msp430" => ifn!("memcmp", fn(ptr, ptr, t_isize) -> t_i16),
1220 _ => ifn!("memcmp", fn(ptr, ptr, t_isize) -> t_i32),
1221 }
1222
1223 ifn!("llvm.va_start", fn(ptr) -> void);
1225 ifn!("llvm.va_end", fn(ptr) -> void);
1226 ifn!("llvm.va_copy", fn(ptr, ptr) -> void);
1227
1228 if self.sess().instrument_coverage() {
1229 ifn!("llvm.instrprof.increment", fn(ptr, t_i64, t_i32, t_i32) -> void);
1230 ifn!("llvm.instrprof.mcdc.parameters", fn(ptr, t_i64, t_i32) -> void);
1231 ifn!("llvm.instrprof.mcdc.tvbitmap.update", fn(ptr, t_i64, t_i32, ptr) -> void);
1232 }
1233
1234 ifn!("llvm.type.test", fn(ptr, t_metadata) -> i1);
1235 ifn!("llvm.type.checked.load", fn(ptr, t_i32, t_metadata) -> mk_struct! {ptr, i1});
1236
1237 if self.sess().opts.debuginfo != DebugInfo::None {
1238 ifn!("llvm.dbg.declare", fn(t_metadata, t_metadata) -> void);
1239 ifn!("llvm.dbg.value", fn(t_metadata, t_i64, t_metadata) -> void);
1240 }
1241
1242 ifn!("llvm.ptrmask", fn(ptr, t_isize) -> ptr);
1243
1244 None
1245 }
1246
1247 pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
1248 if let Some(eh_catch_typeinfo) = self.eh_catch_typeinfo.get() {
1249 return eh_catch_typeinfo;
1250 }
1251 let tcx = self.tcx;
1252 assert!(self.sess().target.os == "emscripten");
1253 let eh_catch_typeinfo = match tcx.lang_items().eh_catch_typeinfo() {
1254 Some(def_id) => self.get_static(def_id),
1255 _ => {
1256 let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
1257 self.declare_global(&mangle_internal_symbol(self.tcx, "rust_eh_catch_typeinfo"), ty)
1258 }
1259 };
1260 self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
1261 eh_catch_typeinfo
1262 }
1263}
1264
1265impl CodegenCx<'_, '_> {
1266 pub(crate) fn generate_local_symbol_name(&self, prefix: &str) -> String {
1269 let idx = self.local_gen_sym_counter.get();
1270 self.local_gen_sym_counter.set(idx + 1);
1271 let mut name = String::with_capacity(prefix.len() + 6);
1274 name.push_str(prefix);
1275 name.push('.');
1276 name.push_str(&(idx as u64).to_base(ALPHANUMERIC_ONLY));
1277 name
1278 }
1279}
1280
1281impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
1282 pub(crate) fn set_metadata<'a>(
1284 &self,
1285 val: &'a Value,
1286 kind_id: impl Into<llvm::MetadataKindId>,
1287 md: &'ll Metadata,
1288 ) {
1289 let node = self.get_metadata_value(md);
1290 llvm::LLVMSetMetadata(val, kind_id.into(), node);
1291 }
1292}
1293
1294impl HasDataLayout for CodegenCx<'_, '_> {
1295 #[inline]
1296 fn data_layout(&self) -> &TargetDataLayout {
1297 &self.tcx.data_layout
1298 }
1299}
1300
1301impl HasTargetSpec for CodegenCx<'_, '_> {
1302 #[inline]
1303 fn target_spec(&self) -> &Target {
1304 &self.tcx.sess.target
1305 }
1306}
1307
1308impl<'tcx> ty::layout::HasTyCtxt<'tcx> for CodegenCx<'_, 'tcx> {
1309 #[inline]
1310 fn tcx(&self) -> TyCtxt<'tcx> {
1311 self.tcx
1312 }
1313}
1314
1315impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
1316 fn typing_env(&self) -> ty::TypingEnv<'tcx> {
1317 ty::TypingEnv::fully_monomorphized()
1318 }
1319}
1320
1321impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
1322 #[inline]
1323 fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
1324 if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
1325 self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
1326 } else {
1327 self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
1328 }
1329 }
1330}
1331
1332impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
1333 #[inline]
1334 fn handle_fn_abi_err(
1335 &self,
1336 err: FnAbiError<'tcx>,
1337 span: Span,
1338 fn_abi_request: FnAbiRequest<'tcx>,
1339 ) -> ! {
1340 match err {
1341 FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
1342 self.tcx.dcx().emit_fatal(Spanned { span, node: err });
1343 }
1344 _ => match fn_abi_request {
1345 FnAbiRequest::OfFnPtr { sig, extra_args } => {
1346 span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}",);
1347 }
1348 FnAbiRequest::OfInstance { instance, extra_args } => {
1349 span_bug!(
1350 span,
1351 "`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}",
1352 );
1353 }
1354 },
1355 }
1356 }
1357}