rustc_codegen_llvm/
common.rs

1//! Code that is useful in various codegen modules.
2
3use std::borrow::Borrow;
4
5use libc::{c_char, c_uint};
6use rustc_abi::Primitive::Pointer;
7use rustc_abi::{self as abi, HasDataLayout as _};
8use rustc_ast::Mutability;
9use rustc_codegen_ssa::common::TypeKind;
10use rustc_codegen_ssa::traits::*;
11use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
12use rustc_hashes::Hash128;
13use rustc_hir::def_id::DefId;
14use rustc_middle::bug;
15use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
16use rustc_middle::ty::TyCtxt;
17use rustc_session::cstore::DllImport;
18use tracing::debug;
19
20use crate::consts::const_alloc_to_llvm;
21pub(crate) use crate::context::CodegenCx;
22use crate::context::{GenericCx, SCx};
23use crate::llvm::{self, BasicBlock, ConstantInt, FALSE, Metadata, TRUE, ToLlvmBool};
24use crate::type_::Type;
25use crate::value::Value;
26
27/*
28* A note on nomenclature of linking: "extern", "foreign", and "upcall".
29*
30* An "extern" is an LLVM symbol we wind up emitting an undefined external
31* reference to. This means "we don't have the thing in this compilation unit,
32* please make sure you link it in at runtime". This could be a reference to
33* C code found in a C library, or rust code found in a rust crate.
34*
35* Most "externs" are implicitly declared (automatically) as a result of a
36* user declaring an extern _module_ dependency; this causes the rust driver
37* to locate an extern crate, scan its compilation metadata, and emit extern
38* declarations for any symbols used by the declaring crate.
39*
40* A "foreign" is an extern that references C (or other non-rust ABI) code.
41* There is no metadata to scan for extern references so in these cases either
42* a header-digester like bindgen, or manual function prototypes, have to
43* serve as declarators. So these are usually given explicitly as prototype
44* declarations, in rust code, with ABI attributes on them noting which ABI to
45* link via.
46*
47* An "upcall" is a foreign call generated by the compiler (not corresponding
48* to any user-written call in the code) into the runtime library, to perform
49* some helper task such as bringing a task to life, allocating memory, etc.
50*
51*/
52
53/// A structure representing an active landing pad for the duration of a basic
54/// block.
55///
56/// Each `Block` may contain an instance of this, indicating whether the block
57/// is part of a landing pad or not. This is used to make decision about whether
58/// to emit `invoke` instructions (e.g., in a landing pad we don't continue to
59/// use `invoke`) and also about various function call metadata.
60///
61/// For GNU exceptions (`landingpad` + `resume` instructions) this structure is
62/// just a bunch of `None` instances (not too interesting), but for MSVC
63/// exceptions (`cleanuppad` + `cleanupret` instructions) this contains data.
64/// When inside of a landing pad, each function call in LLVM IR needs to be
65/// annotated with which landing pad it's a part of. This is accomplished via
66/// the `OperandBundleDef` value created for MSVC landing pads.
67pub(crate) struct Funclet<'ll> {
68    cleanuppad: &'ll Value,
69    operand: llvm::OperandBundleBox<'ll>,
70}
71
72impl<'ll> Funclet<'ll> {
73    pub(crate) fn new(cleanuppad: &'ll Value) -> Self {
74        Funclet { cleanuppad, operand: llvm::OperandBundleBox::new("funclet", &[cleanuppad]) }
75    }
76
77    pub(crate) fn cleanuppad(&self) -> &'ll Value {
78        self.cleanuppad
79    }
80
81    pub(crate) fn bundle(&self) -> &llvm::OperandBundle<'ll> {
82        self.operand.as_ref()
83    }
84}
85
86impl<'ll, CX: Borrow<SCx<'ll>>> BackendTypes for GenericCx<'ll, CX> {
87    type Value = &'ll Value;
88    type Metadata = &'ll Metadata;
89    // FIXME(eddyb) replace this with a `Function` "subclass" of `Value`.
90    type Function = &'ll Value;
91
92    type BasicBlock = &'ll BasicBlock;
93    type Type = &'ll Type;
94    type Funclet = Funclet<'ll>;
95
96    type DIScope = &'ll llvm::debuginfo::DIScope;
97    type DILocation = &'ll llvm::debuginfo::DILocation;
98    type DIVariable = &'ll llvm::debuginfo::DIVariable;
99}
100
101impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
102    pub(crate) fn const_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
103        let len = u64::try_from(elts.len()).expect("LLVMConstArray2 elements len overflow");
104        unsafe { llvm::LLVMConstArray2(ty, elts.as_ptr(), len) }
105    }
106
107    pub(crate) fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
108        bytes_in_context(self.llcx(), bytes)
109    }
110
111    pub(crate) fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
112        unsafe {
113            let idx = c_uint::try_from(idx).expect("LLVMGetAggregateElement index overflow");
114            let r = llvm::LLVMGetAggregateElement(v, idx).unwrap();
115
116            debug!("const_get_elt(v={:?}, idx={}, r={:?})", v, idx, r);
117
118            r
119        }
120    }
121
122    pub(crate) fn const_null(&self, t: &'ll Type) -> &'ll Value {
123        unsafe { llvm::LLVMConstNull(t) }
124    }
125}
126
127impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
128    fn const_null(&self, t: &'ll Type) -> &'ll Value {
129        unsafe { llvm::LLVMConstNull(t) }
130    }
131
132    fn const_undef(&self, t: &'ll Type) -> &'ll Value {
133        unsafe { llvm::LLVMGetUndef(t) }
134    }
135
136    fn const_poison(&self, t: &'ll Type) -> &'ll Value {
137        unsafe { llvm::LLVMGetPoison(t) }
138    }
139
140    fn const_bool(&self, val: bool) -> &'ll Value {
141        self.const_uint(self.type_i1(), val as u64)
142    }
143
144    fn const_i8(&self, i: i8) -> &'ll Value {
145        self.const_int(self.type_i8(), i as i64)
146    }
147
148    fn const_i16(&self, i: i16) -> &'ll Value {
149        self.const_int(self.type_i16(), i as i64)
150    }
151
152    fn const_i32(&self, i: i32) -> &'ll Value {
153        self.const_int(self.type_i32(), i as i64)
154    }
155
156    fn const_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
157        debug_assert!(
158            self.type_kind(t) == TypeKind::Integer,
159            "only allows integer types in const_int"
160        );
161        unsafe { llvm::LLVMConstInt(t, i as u64, TRUE) }
162    }
163
164    fn const_u8(&self, i: u8) -> &'ll Value {
165        self.const_uint(self.type_i8(), i as u64)
166    }
167
168    fn const_u32(&self, i: u32) -> &'ll Value {
169        self.const_uint(self.type_i32(), i as u64)
170    }
171
172    fn const_u64(&self, i: u64) -> &'ll Value {
173        self.const_uint(self.type_i64(), i)
174    }
175
176    fn const_u128(&self, i: u128) -> &'ll Value {
177        self.const_uint_big(self.type_i128(), i)
178    }
179
180    fn const_usize(&self, i: u64) -> &'ll Value {
181        let bit_size = self.data_layout().pointer_size().bits();
182        if bit_size < 64 {
183            // make sure it doesn't overflow
184            assert!(i < (1 << bit_size));
185        }
186
187        self.const_uint(self.isize_ty, i)
188    }
189
190    fn const_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
191        debug_assert!(
192            self.type_kind(t) == TypeKind::Integer,
193            "only allows integer types in const_uint"
194        );
195        unsafe { llvm::LLVMConstInt(t, i, FALSE) }
196    }
197
198    fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
199        debug_assert!(
200            self.type_kind(t) == TypeKind::Integer,
201            "only allows integer types in const_uint_big"
202        );
203        unsafe {
204            let words = [u as u64, (u >> 64) as u64];
205            llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
206        }
207    }
208
209    fn const_real(&self, t: &'ll Type, val: f64) -> &'ll Value {
210        unsafe { llvm::LLVMConstReal(t, val) }
211    }
212
213    fn const_str(&self, s: &str) -> (&'ll Value, &'ll Value) {
214        let mut const_str_cache = self.const_str_cache.borrow_mut();
215        let str_global = const_str_cache.get(s).copied().unwrap_or_else(|| {
216            let sc = self.const_bytes(s.as_bytes());
217            let sym = self.generate_local_symbol_name("str");
218            let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
219                bug!("symbol `{}` is already defined", sym);
220            });
221            llvm::set_initializer(g, sc);
222
223            llvm::set_global_constant(g, true);
224            llvm::set_unnamed_address(g, llvm::UnnamedAddr::Global);
225
226            llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
227            // Cast to default address space if globals are in a different addrspace
228            let g = self.const_pointercast(g, self.type_ptr());
229            const_str_cache.insert(s.to_owned(), g);
230            g
231        });
232        let len = s.len();
233        (str_global, self.const_usize(len as u64))
234    }
235
236    fn const_struct(&self, elts: &[&'ll Value], packed: bool) -> &'ll Value {
237        struct_in_context(self.llcx, elts, packed)
238    }
239
240    fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
241        let len = c_uint::try_from(elts.len()).expect("LLVMConstVector elements len overflow");
242        unsafe { llvm::LLVMConstVector(elts.as_ptr(), len) }
243    }
244
245    fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
246        try_as_const_integral(v).and_then(|v| unsafe {
247            let mut i = 0u64;
248            let success = llvm::LLVMRustConstIntGetZExtValue(v, &mut i);
249            success.then_some(i)
250        })
251    }
252
253    fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
254        try_as_const_integral(v).and_then(|v| unsafe {
255            let (mut lo, mut hi) = (0u64, 0u64);
256            let success = llvm::LLVMRustConstInt128Get(v, sign_ext, &mut hi, &mut lo);
257            success.then_some(hi_lo_to_u128(lo, hi))
258        })
259    }
260
261    fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: &'ll Type) -> &'ll Value {
262        let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
263        match cv {
264            Scalar::Int(int) => {
265                let data = int.to_bits(layout.size(self));
266                let llval = self.const_uint_big(self.type_ix(bitsize), data);
267                if matches!(layout.primitive(), Pointer(_)) {
268                    unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
269                } else {
270                    self.const_bitcast(llval, llty)
271                }
272            }
273            Scalar::Ptr(ptr, _size) => {
274                let (prov, offset) = ptr.prov_and_relative_offset();
275                let global_alloc = self.tcx.global_alloc(prov.alloc_id());
276                let base_addr = match global_alloc {
277                    GlobalAlloc::Memory(alloc) => {
278                        // For ZSTs directly codegen an aligned pointer.
279                        // This avoids generating a zero-sized constant value and actually needing a
280                        // real address at runtime.
281                        if alloc.inner().len() == 0 {
282                            assert_eq!(offset.bytes(), 0);
283                            let llval = self.const_usize(alloc.inner().align.bytes());
284                            return if matches!(layout.primitive(), Pointer(_)) {
285                                unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
286                            } else {
287                                self.const_bitcast(llval, llty)
288                            };
289                        } else {
290                            let init =
291                                const_alloc_to_llvm(self, alloc.inner(), /*static*/ false);
292                            let alloc = alloc.inner();
293                            let value = match alloc.mutability {
294                                Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
295                                _ => self.static_addr_of_impl(init, alloc.align, None),
296                            };
297                            if !self.sess().fewer_names() && llvm::get_value_name(value).is_empty()
298                            {
299                                let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
300                                    let mut hasher = StableHasher::new();
301                                    alloc.hash_stable(&mut hcx, &mut hasher);
302                                    hasher.finish::<Hash128>()
303                                });
304                                llvm::set_value_name(
305                                    value,
306                                    format!("alloc_{hash:032x}").as_bytes(),
307                                );
308                            }
309                            value
310                        }
311                    }
312                    GlobalAlloc::Function { instance, .. } => self.get_fn_addr(instance),
313                    GlobalAlloc::VTable(ty, dyn_ty) => {
314                        let alloc = self
315                            .tcx
316                            .global_alloc(self.tcx.vtable_allocation((
317                                ty,
318                                dyn_ty.principal().map(|principal| {
319                                    self.tcx.instantiate_bound_regions_with_erased(principal)
320                                }),
321                            )))
322                            .unwrap_memory();
323                        let init = const_alloc_to_llvm(self, alloc.inner(), /*static*/ false);
324                        self.static_addr_of_impl(init, alloc.inner().align, None)
325                    }
326                    GlobalAlloc::Static(def_id) => {
327                        assert!(self.tcx.is_static(def_id));
328                        assert!(!self.tcx.is_thread_local_static(def_id));
329                        self.get_static(def_id)
330                    }
331                    GlobalAlloc::TypeId { .. } => {
332                        // Drop the provenance, the offset contains the bytes of the hash
333                        let llval = self.const_usize(offset.bytes());
334                        return unsafe { llvm::LLVMConstIntToPtr(llval, llty) };
335                    }
336                };
337                let base_addr_space = global_alloc.address_space(self);
338                let llval = unsafe {
339                    llvm::LLVMConstInBoundsGEP2(
340                        self.type_i8(),
341                        // Cast to the required address space if necessary
342                        self.const_pointercast(base_addr, self.type_ptr_ext(base_addr_space)),
343                        &self.const_usize(offset.bytes()),
344                        1,
345                    )
346                };
347                if !matches!(layout.primitive(), Pointer(_)) {
348                    unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
349                } else {
350                    self.const_bitcast(llval, llty)
351                }
352            }
353        }
354    }
355
356    fn const_data_from_alloc(&self, alloc: ConstAllocation<'_>) -> Self::Value {
357        const_alloc_to_llvm(self, alloc.inner(), /*static*/ false)
358    }
359
360    fn const_ptr_byte_offset(&self, base_addr: Self::Value, offset: abi::Size) -> Self::Value {
361        unsafe {
362            llvm::LLVMConstInBoundsGEP2(
363                self.type_i8(),
364                base_addr,
365                &self.const_usize(offset.bytes()),
366                1,
367            )
368        }
369    }
370}
371
372/// Get the [LLVM type][Type] of a [`Value`].
373pub(crate) fn val_ty(v: &Value) -> &Type {
374    unsafe { llvm::LLVMTypeOf(v) }
375}
376
377pub(crate) fn bytes_in_context<'ll>(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
378    unsafe {
379        let ptr = bytes.as_ptr() as *const c_char;
380        llvm::LLVMConstStringInContext2(llcx, ptr, bytes.len(), TRUE)
381    }
382}
383
384pub(crate) fn named_struct<'ll>(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
385    let len = c_uint::try_from(elts.len()).expect("LLVMConstStructInContext elements len overflow");
386    unsafe { llvm::LLVMConstNamedStruct(ty, elts.as_ptr(), len) }
387}
388
389fn struct_in_context<'ll>(
390    llcx: &'ll llvm::Context,
391    elts: &[&'ll Value],
392    packed: bool,
393) -> &'ll Value {
394    let len = c_uint::try_from(elts.len()).expect("LLVMConstStructInContext elements len overflow");
395    unsafe { llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), len, packed.to_llvm_bool()) }
396}
397
398#[inline]
399fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
400    ((hi as u128) << 64) | (lo as u128)
401}
402
403fn try_as_const_integral(v: &Value) -> Option<&ConstantInt> {
404    unsafe { llvm::LLVMIsAConstantInt(v) }
405}
406
407pub(crate) fn get_dllimport<'tcx>(
408    tcx: TyCtxt<'tcx>,
409    id: DefId,
410    name: &str,
411) -> Option<&'tcx DllImport> {
412    tcx.native_library(id)
413        .and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name))
414}
415
416/// Extension trait for explicit casts to `*const c_char`.
417pub(crate) trait AsCCharPtr {
418    /// Equivalent to `self.as_ptr().cast()`, but only casts to `*const c_char`.
419    fn as_c_char_ptr(&self) -> *const c_char;
420}
421
422impl AsCCharPtr for str {
423    fn as_c_char_ptr(&self) -> *const c_char {
424        self.as_ptr().cast()
425    }
426}
427
428impl AsCCharPtr for [u8] {
429    fn as_c_char_ptr(&self) -> *const c_char {
430        self.as_ptr().cast()
431    }
432}