rustc_codegen_llvm/llvm/
mod.rs

1#![allow(non_snake_case)]
2
3use std::ffi::{CStr, CString};
4use std::num::NonZero;
5use std::ptr;
6use std::string::FromUtf8Error;
7
8use libc::c_uint;
9use rustc_abi::{Align, Size, WrappingRange};
10use rustc_llvm::RustString;
11
12pub(crate) use self::CallConv::*;
13pub(crate) use self::CodeGenOptSize::*;
14pub(crate) use self::conversions::*;
15pub(crate) use self::ffi::*;
16pub(crate) use self::metadata_kind::*;
17use crate::common::AsCCharPtr;
18
19mod conversions;
20pub(crate) mod diagnostic;
21pub(crate) mod enzyme_ffi;
22mod ffi;
23mod metadata_kind;
24
25pub(crate) use self::enzyme_ffi::*;
26
27impl LLVMRustResult {
28    pub(crate) fn into_result(self) -> Result<(), ()> {
29        match self {
30            LLVMRustResult::Success => Ok(()),
31            LLVMRustResult::Failure => Err(()),
32        }
33    }
34}
35
36pub(crate) fn AddFunctionAttributes<'ll>(
37    llfn: &'ll Value,
38    idx: AttributePlace,
39    attrs: &[&'ll Attribute],
40) {
41    unsafe {
42        LLVMRustAddFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
43    }
44}
45
46pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: &str) -> bool {
47    unsafe { LLVMRustHasFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
48}
49
50pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: &str) {
51    unsafe { LLVMRustRemoveFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
52}
53
54pub(crate) fn AddCallSiteAttributes<'ll>(
55    callsite: &'ll Value,
56    idx: AttributePlace,
57    attrs: &[&'ll Attribute],
58) {
59    unsafe {
60        LLVMRustAddCallSiteAttributes(callsite, idx.as_uint(), attrs.as_ptr(), attrs.len());
61    }
62}
63
64pub(crate) fn CreateAttrStringValue<'ll>(
65    llcx: &'ll Context,
66    attr: &str,
67    value: &str,
68) -> &'ll Attribute {
69    unsafe {
70        LLVMCreateStringAttribute(
71            llcx,
72            attr.as_c_char_ptr(),
73            attr.len().try_into().unwrap(),
74            value.as_c_char_ptr(),
75            value.len().try_into().unwrap(),
76        )
77    }
78}
79
80pub(crate) fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute {
81    unsafe {
82        LLVMCreateStringAttribute(
83            llcx,
84            attr.as_c_char_ptr(),
85            attr.len().try_into().unwrap(),
86            std::ptr::null(),
87            0,
88        )
89    }
90}
91
92pub(crate) fn CreateAlignmentAttr(llcx: &Context, bytes: u64) -> &Attribute {
93    unsafe { LLVMRustCreateAlignmentAttr(llcx, bytes) }
94}
95
96pub(crate) fn CreateDereferenceableAttr(llcx: &Context, bytes: u64) -> &Attribute {
97    unsafe { LLVMRustCreateDereferenceableAttr(llcx, bytes) }
98}
99
100pub(crate) fn CreateDereferenceableOrNullAttr(llcx: &Context, bytes: u64) -> &Attribute {
101    unsafe { LLVMRustCreateDereferenceableOrNullAttr(llcx, bytes) }
102}
103
104pub(crate) fn CreateByValAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
105    unsafe { LLVMRustCreateByValAttr(llcx, ty) }
106}
107
108pub(crate) fn CreateStructRetAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
109    unsafe { LLVMRustCreateStructRetAttr(llcx, ty) }
110}
111
112pub(crate) fn CreateUWTableAttr(llcx: &Context, async_: bool) -> &Attribute {
113    unsafe { LLVMRustCreateUWTableAttr(llcx, async_) }
114}
115
116pub(crate) fn CreateAllocSizeAttr(llcx: &Context, size_arg: u32) -> &Attribute {
117    unsafe { LLVMRustCreateAllocSizeAttr(llcx, size_arg) }
118}
119
120pub(crate) fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &Attribute {
121    unsafe { LLVMRustCreateAllocKindAttr(llcx, kind_arg.bits()) }
122}
123
124pub(crate) fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute {
125    let lower = range.start;
126    // LLVM treats the upper bound as exclusive, but allows wrapping.
127    let upper = range.end.wrapping_add(1);
128
129    // Pass each `u128` endpoint value as a `[u64; 2]` array, least-significant part first.
130    let as_u64_array = |x: u128| [x as u64, (x >> 64) as u64];
131    let lower_words: [u64; 2] = as_u64_array(lower);
132    let upper_words: [u64; 2] = as_u64_array(upper);
133
134    // To ensure that LLVM doesn't try to read beyond the `[u64; 2]` arrays,
135    // we must explicitly check that `size_bits` does not exceed 128.
136    let size_bits = size.bits();
137    assert!(size_bits <= 128);
138    // More robust assertions that are redundant with `size_bits <= 128` and
139    // should be optimized away.
140    assert!(size_bits.div_ceil(64) <= u64::try_from(lower_words.len()).unwrap());
141    assert!(size_bits.div_ceil(64) <= u64::try_from(upper_words.len()).unwrap());
142    let size_bits = c_uint::try_from(size_bits).unwrap();
143
144    unsafe {
145        LLVMRustCreateRangeAttribute(llcx, size_bits, lower_words.as_ptr(), upper_words.as_ptr())
146    }
147}
148
149#[derive(Copy, Clone)]
150pub(crate) enum AttributePlace {
151    ReturnValue,
152    Argument(u32),
153    Function,
154}
155
156impl AttributePlace {
157    pub(crate) fn as_uint(self) -> c_uint {
158        match self {
159            AttributePlace::ReturnValue => 0,
160            AttributePlace::Argument(i) => 1 + i,
161            AttributePlace::Function => !0,
162        }
163    }
164}
165
166#[derive(Copy, Clone, PartialEq)]
167#[repr(C)]
168pub(crate) enum CodeGenOptSize {
169    CodeGenOptSizeNone = 0,
170    CodeGenOptSizeDefault = 1,
171    CodeGenOptSizeAggressive = 2,
172}
173
174pub(crate) fn SetInstructionCallConv(instr: &Value, cc: CallConv) {
175    unsafe {
176        LLVMSetInstructionCallConv(instr, cc as c_uint);
177    }
178}
179pub(crate) fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
180    unsafe {
181        LLVMSetFunctionCallConv(fn_, cc as c_uint);
182    }
183}
184
185// Externally visible symbols that might appear in multiple codegen units need to appear in
186// their own comdat section so that the duplicates can be discarded at link time. This can for
187// example happen for generics when using multiple codegen units. This function simply uses the
188// value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
189// function.
190// For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52
191pub(crate) fn SetUniqueComdat(llmod: &Module, val: &Value) {
192    let name_buf = get_value_name(val);
193    let name =
194        CString::from_vec_with_nul(name_buf).or_else(|buf| CString::new(buf.into_bytes())).unwrap();
195    set_comdat(llmod, val, &name);
196}
197
198pub(crate) fn set_unnamed_address(global: &Value, unnamed: UnnamedAddr) {
199    LLVMSetUnnamedAddress(global, unnamed);
200}
201
202pub(crate) fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
203    unsafe {
204        LLVMSetThreadLocalMode(global, mode);
205    }
206}
207
208impl AttributeKind {
209    /// Create an LLVM Attribute with no associated value.
210    pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
211        unsafe { LLVMRustCreateAttrNoValue(llcx, self) }
212    }
213}
214
215impl MemoryEffects {
216    /// Create an LLVM Attribute with these memory effects.
217    pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
218        unsafe { LLVMRustCreateMemoryEffectsAttr(llcx, self) }
219    }
220}
221
222pub(crate) fn set_section(llglobal: &Value, section_name: &CStr) {
223    unsafe {
224        LLVMSetSection(llglobal, section_name.as_ptr());
225    }
226}
227
228pub(crate) fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr) -> &'a Value {
229    unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
230}
231
232pub(crate) fn set_initializer(llglobal: &Value, constant_val: &Value) {
233    unsafe {
234        LLVMSetInitializer(llglobal, constant_val);
235    }
236}
237
238pub(crate) fn set_global_constant(llglobal: &Value, is_constant: bool) {
239    LLVMSetGlobalConstant(llglobal, is_constant.to_llvm_bool());
240}
241
242pub(crate) fn get_linkage(llglobal: &Value) -> Linkage {
243    unsafe { LLVMGetLinkage(llglobal) }.to_rust()
244}
245
246pub(crate) fn set_linkage(llglobal: &Value, linkage: Linkage) {
247    unsafe {
248        LLVMSetLinkage(llglobal, linkage);
249    }
250}
251
252pub(crate) fn is_declaration(llglobal: &Value) -> bool {
253    unsafe { LLVMIsDeclaration(llglobal) }.is_true()
254}
255
256pub(crate) fn get_visibility(llglobal: &Value) -> Visibility {
257    unsafe { LLVMGetVisibility(llglobal) }.to_rust()
258}
259
260pub(crate) fn set_visibility(llglobal: &Value, visibility: Visibility) {
261    unsafe {
262        LLVMSetVisibility(llglobal, visibility);
263    }
264}
265
266pub(crate) fn set_alignment(llglobal: &Value, align: Align) {
267    unsafe {
268        ffi::LLVMSetAlignment(llglobal, align.bytes() as c_uint);
269    }
270}
271
272pub(crate) fn set_externally_initialized(llglobal: &Value, is_ext_init: bool) {
273    LLVMSetExternallyInitialized(llglobal, is_ext_init.to_llvm_bool());
274}
275
276/// Get the `name`d comdat from `llmod` and assign it to `llglobal`.
277///
278/// Inserts the comdat into `llmod` if it does not exist.
279/// It is an error to call this if the target does not support comdat.
280pub(crate) fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) {
281    unsafe {
282        let comdat = LLVMGetOrInsertComdat(llmod, name.as_ptr());
283        LLVMSetComdat(llglobal, comdat);
284    }
285}
286
287/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
288pub(crate) fn get_param(llfn: &Value, index: c_uint) -> &Value {
289    unsafe {
290        assert!(
291            index < LLVMCountParams(llfn),
292            "out of bounds argument access: {} out of {} arguments",
293            index,
294            LLVMCountParams(llfn)
295        );
296        LLVMGetParam(llfn, index)
297    }
298}
299
300/// Safe wrapper for `LLVMGetValueName2`
301/// Needs to allocate the value, because `set_value_name` will invalidate
302/// the pointer.
303pub(crate) fn get_value_name(value: &Value) -> Vec<u8> {
304    unsafe {
305        let mut len = 0;
306        let data = LLVMGetValueName2(value, &mut len);
307        std::slice::from_raw_parts(data.cast(), len).to_vec()
308    }
309}
310
311#[derive(Debug, Copy, Clone)]
312pub(crate) struct Intrinsic {
313    id: NonZero<c_uint>,
314}
315
316impl Intrinsic {
317    pub(crate) fn lookup(name: &[u8]) -> Option<Self> {
318        let id = unsafe { LLVMLookupIntrinsicID(name.as_c_char_ptr(), name.len()) };
319        NonZero::new(id).map(|id| Self { id })
320    }
321
322    pub(crate) fn get_declaration<'ll>(
323        self,
324        llmod: &'ll Module,
325        type_params: &[&'ll Type],
326    ) -> &'ll Value {
327        unsafe {
328            LLVMGetIntrinsicDeclaration(llmod, self.id, type_params.as_ptr(), type_params.len())
329        }
330    }
331}
332
333/// Safe wrapper for `LLVMSetValueName2` from a byte slice
334pub(crate) fn set_value_name(value: &Value, name: &[u8]) {
335    unsafe {
336        let data = name.as_c_char_ptr();
337        LLVMSetValueName2(value, data, name.len());
338    }
339}
340
341pub(crate) fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
342    String::from_utf8(RustString::build_byte_buffer(f))
343}
344
345pub(crate) fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
346    RustString::build_byte_buffer(f)
347}
348
349pub(crate) fn twine_to_string(tr: &Twine) -> String {
350    unsafe {
351        build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
352    }
353}
354
355pub(crate) fn last_error() -> Option<String> {
356    unsafe {
357        let cstr = LLVMRustGetLastError();
358        if cstr.is_null() {
359            None
360        } else {
361            let err = CStr::from_ptr(cstr).to_bytes();
362            let err = String::from_utf8_lossy(err).to_string();
363            libc::free(cstr as *mut _);
364            Some(err)
365        }
366    }
367}
368
369/// Owning pointer to an [`OperandBundle`] that will dispose of the bundle
370/// when dropped.
371pub(crate) struct OperandBundleBox<'a> {
372    raw: ptr::NonNull<OperandBundle<'a>>,
373}
374
375impl<'a> OperandBundleBox<'a> {
376    pub(crate) fn new(name: &str, vals: &[&'a Value]) -> Self {
377        let raw = unsafe {
378            LLVMCreateOperandBundle(
379                name.as_c_char_ptr(),
380                name.len(),
381                vals.as_ptr(),
382                vals.len() as c_uint,
383            )
384        };
385        Self { raw: ptr::NonNull::new(raw).unwrap() }
386    }
387
388    /// Dereferences to the underlying `&OperandBundle`.
389    ///
390    /// This can't be a `Deref` implementation because `OperandBundle` transitively
391    /// contains an extern type, which is incompatible with `Deref::Target: ?Sized`.
392    pub(crate) fn as_ref(&self) -> &OperandBundle<'a> {
393        // SAFETY: The returned reference is opaque and can only used for FFI.
394        // It is valid for as long as `&self` is.
395        unsafe { self.raw.as_ref() }
396    }
397}
398
399impl Drop for OperandBundleBox<'_> {
400    fn drop(&mut self) {
401        unsafe {
402            LLVMDisposeOperandBundle(self.raw);
403        }
404    }
405}
406
407pub(crate) fn add_module_flag_u32(
408    module: &Module,
409    merge_behavior: ModuleFlagMergeBehavior,
410    key: &str,
411    value: u32,
412) {
413    unsafe {
414        LLVMRustAddModuleFlagU32(module, merge_behavior, key.as_c_char_ptr(), key.len(), value);
415    }
416}
417
418pub(crate) fn add_module_flag_str(
419    module: &Module,
420    merge_behavior: ModuleFlagMergeBehavior,
421    key: &str,
422    value: &str,
423) {
424    unsafe {
425        LLVMRustAddModuleFlagString(
426            module,
427            merge_behavior,
428            key.as_c_char_ptr(),
429            key.len(),
430            value.as_c_char_ptr(),
431            value.len(),
432        );
433    }
434}
435
436pub(crate) fn set_dllimport_storage_class<'ll>(v: &'ll Value) {
437    unsafe {
438        LLVMSetDLLStorageClass(v, DLLStorageClass::DllImport);
439    }
440}
441
442pub(crate) fn set_dso_local<'ll>(v: &'ll Value) {
443    unsafe {
444        LLVMRustSetDSOLocal(v, true);
445    }
446}
447
448/// Safe wrapper for `LLVMAppendModuleInlineAsm`, which delegates to
449/// `Module::appendModuleInlineAsm`.
450pub(crate) fn append_module_inline_asm<'ll>(llmod: &'ll Module, asm: &[u8]) {
451    unsafe {
452        LLVMAppendModuleInlineAsm(llmod, asm.as_ptr(), asm.len());
453    }
454}