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
261/// Get the `name`d comdat from `llmod` and assign it to `llglobal`.
262///
263/// Inserts the comdat into `llmod` if it does not exist.
264/// It is an error to call this if the target does not support comdat.
265pub(crate) fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) {
266    unsafe {
267        let comdat = LLVMGetOrInsertComdat(llmod, name.as_ptr());
268        LLVMSetComdat(llglobal, comdat);
269    }
270}
271
272/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
273pub(crate) fn get_param(llfn: &Value, index: c_uint) -> &Value {
274    unsafe {
275        assert!(
276            index < LLVMCountParams(llfn),
277            "out of bounds argument access: {} out of {} arguments",
278            index,
279            LLVMCountParams(llfn)
280        );
281        LLVMGetParam(llfn, index)
282    }
283}
284
285/// Safe wrapper for `LLVMGetValueName2`
286/// Needs to allocate the value, because `set_value_name` will invalidate
287/// the pointer.
288pub(crate) fn get_value_name(value: &Value) -> Vec<u8> {
289    unsafe {
290        let mut len = 0;
291        let data = LLVMGetValueName2(value, &mut len);
292        std::slice::from_raw_parts(data.cast(), len).to_vec()
293    }
294}
295
296#[derive(Debug, Copy, Clone)]
297pub(crate) struct Intrinsic {
298    id: NonZero<c_uint>,
299}
300
301impl Intrinsic {
302    pub(crate) fn lookup(name: &[u8]) -> Option<Self> {
303        let id = unsafe { LLVMLookupIntrinsicID(name.as_c_char_ptr(), name.len()) };
304        NonZero::new(id).map(|id| Self { id })
305    }
306
307    pub(crate) fn get_declaration<'ll>(
308        self,
309        llmod: &'ll Module,
310        type_params: &[&'ll Type],
311    ) -> &'ll Value {
312        unsafe {
313            LLVMGetIntrinsicDeclaration(llmod, self.id, type_params.as_ptr(), type_params.len())
314        }
315    }
316}
317
318/// Safe wrapper for `LLVMSetValueName2` from a byte slice
319pub(crate) fn set_value_name(value: &Value, name: &[u8]) {
320    unsafe {
321        let data = name.as_c_char_ptr();
322        LLVMSetValueName2(value, data, name.len());
323    }
324}
325
326pub(crate) fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
327    String::from_utf8(RustString::build_byte_buffer(f))
328}
329
330pub(crate) fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
331    RustString::build_byte_buffer(f)
332}
333
334pub(crate) fn twine_to_string(tr: &Twine) -> String {
335    unsafe {
336        build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
337    }
338}
339
340pub(crate) fn last_error() -> Option<String> {
341    unsafe {
342        let cstr = LLVMRustGetLastError();
343        if cstr.is_null() {
344            None
345        } else {
346            let err = CStr::from_ptr(cstr).to_bytes();
347            let err = String::from_utf8_lossy(err).to_string();
348            libc::free(cstr as *mut _);
349            Some(err)
350        }
351    }
352}
353
354/// Owning pointer to an [`OperandBundle`] that will dispose of the bundle
355/// when dropped.
356pub(crate) struct OperandBundleBox<'a> {
357    raw: ptr::NonNull<OperandBundle<'a>>,
358}
359
360impl<'a> OperandBundleBox<'a> {
361    pub(crate) fn new(name: &str, vals: &[&'a Value]) -> Self {
362        let raw = unsafe {
363            LLVMCreateOperandBundle(
364                name.as_c_char_ptr(),
365                name.len(),
366                vals.as_ptr(),
367                vals.len() as c_uint,
368            )
369        };
370        Self { raw: ptr::NonNull::new(raw).unwrap() }
371    }
372
373    /// Dereferences to the underlying `&OperandBundle`.
374    ///
375    /// This can't be a `Deref` implementation because `OperandBundle` transitively
376    /// contains an extern type, which is incompatible with `Deref::Target: ?Sized`.
377    pub(crate) fn as_ref(&self) -> &OperandBundle<'a> {
378        // SAFETY: The returned reference is opaque and can only used for FFI.
379        // It is valid for as long as `&self` is.
380        unsafe { self.raw.as_ref() }
381    }
382}
383
384impl Drop for OperandBundleBox<'_> {
385    fn drop(&mut self) {
386        unsafe {
387            LLVMDisposeOperandBundle(self.raw);
388        }
389    }
390}
391
392pub(crate) fn add_module_flag_u32(
393    module: &Module,
394    merge_behavior: ModuleFlagMergeBehavior,
395    key: &str,
396    value: u32,
397) {
398    unsafe {
399        LLVMRustAddModuleFlagU32(module, merge_behavior, key.as_c_char_ptr(), key.len(), value);
400    }
401}
402
403pub(crate) fn add_module_flag_str(
404    module: &Module,
405    merge_behavior: ModuleFlagMergeBehavior,
406    key: &str,
407    value: &str,
408) {
409    unsafe {
410        LLVMRustAddModuleFlagString(
411            module,
412            merge_behavior,
413            key.as_c_char_ptr(),
414            key.len(),
415            value.as_c_char_ptr(),
416            value.len(),
417        );
418    }
419}
420
421pub(crate) fn set_dllimport_storage_class<'ll>(v: &'ll Value) {
422    unsafe {
423        LLVMSetDLLStorageClass(v, DLLStorageClass::DllImport);
424    }
425}
426
427pub(crate) fn set_dso_local<'ll>(v: &'ll Value) {
428    unsafe {
429        LLVMRustSetDSOLocal(v, true);
430    }
431}
432
433/// Safe wrapper for `LLVMAppendModuleInlineAsm`, which delegates to
434/// `Module::appendModuleInlineAsm`.
435pub(crate) fn append_module_inline_asm<'ll>(llmod: &'ll Module, asm: &[u8]) {
436    unsafe {
437        LLVMAppendModuleInlineAsm(llmod, asm.as_ptr(), asm.len());
438    }
439}