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