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