rustc_codegen_llvm/llvm/
mod.rs

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