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